1 /* Functions for image support on window system.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
28 #if defined HAVE_LIBPNG_PNG_H
29 # include <libpng/png.h>
37 /* This makes the fields of a Display accessible, in Xlib header files. */
39 #define XLIB_ILLEGAL_ACCESS
44 #include "dispextern.h"
45 #include "blockinput.h"
48 #include "character.h"
50 #include "termhooks.h"
55 #include <sys/types.h>
58 #define COLOR_TABLE_SUPPORT 1
60 typedef struct x_bitmap_record Bitmap_Record
;
61 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
62 #define NO_PIXMAP None
64 #define RGB_PIXEL_COLOR unsigned long
66 #define PIX_MASK_RETAIN 0
67 #define PIX_MASK_DRAW 1
68 #endif /* HAVE_X_WINDOWS */
74 /* W32_TODO : Color tables on W32. */
75 #undef COLOR_TABLE_SUPPORT
77 typedef struct w32_bitmap_record Bitmap_Record
;
78 #define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y)
81 #define RGB_PIXEL_COLOR COLORREF
83 #define PIX_MASK_RETAIN 0
84 #define PIX_MASK_DRAW 1
86 #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual
87 #define x_defined_color w32_defined_color
88 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
90 /* Functions from w32term.c that depend on XColor (so can't go in w32term.h
91 without modifying lots of files). */
92 extern void x_query_colors (struct frame
*f
, XColor
*colors
, int ncolors
);
93 extern void x_query_color (struct frame
*f
, XColor
*color
);
95 /* Version of libpng that we were compiled with, or -1 if no PNG
96 support was compiled in. This is tested by w32-win.el to correctly
97 set up the alist used to search for PNG libraries. */
98 Lisp_Object Qlibpng_version
;
99 #endif /* HAVE_NTGUI */
103 #include <sys/types.h>
104 #include <sys/stat.h>
106 #undef COLOR_TABLE_SUPPORT
108 typedef struct ns_bitmap_record Bitmap_Record
;
110 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
113 #define RGB_PIXEL_COLOR unsigned long
116 #define PIX_MASK_RETAIN 0
117 #define PIX_MASK_DRAW 1
119 #define FRAME_X_VISUAL FRAME_NS_DISPLAY_INFO(f)->visual
120 #define x_defined_color(f, name, color_def, alloc) \
121 ns_defined_color (f, name, color_def, alloc, 0)
122 #define FRAME_X_SCREEN(f) 0
123 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
127 /* The symbol `postscript' identifying images of this type. */
129 Lisp_Object Qpostscript
;
131 static void x_disable_image (struct frame
*, struct image
*);
132 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
135 static void init_color_table (void);
136 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
137 #ifdef COLOR_TABLE_SUPPORT
138 static void free_color_table (void);
139 static unsigned long *colors_in_color_table (int *n
);
140 static unsigned long lookup_pixel_color (struct frame
*f
, unsigned long p
);
143 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
144 id, which is just an int that this section returns. Bitmaps are
145 reference counted so they can be shared among frames.
147 Bitmap indices are guaranteed to be > 0, so a negative number can
148 be used to indicate no bitmap.
150 If you use x_create_bitmap_from_data, then you must keep track of
151 the bitmaps yourself. That is, creating a bitmap from the same
152 data more than once will not be caught. */
156 XGetImage (Display
*display
, Pixmap pixmap
, int x
, int y
,
157 unsigned int width
, unsigned int height
,
158 unsigned long plane_mask
, int format
)
160 /* TODO: not sure what this function is supposed to do.. */
161 ns_retain_object (pixmap
);
165 /* use with imgs created by ns_image_for_XPM */
167 XGetPixel (XImagePtr ximage
, int x
, int y
)
169 return ns_get_pixel (ximage
, x
, y
);
172 /* use with imgs created by ns_image_for_XPM; alpha set to 1;
173 pixel is assumed to be in form RGB */
175 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
177 ns_put_pixel (ximage
, x
, y
, pixel
);
182 /* Functions to access the contents of a bitmap, given an id. */
185 x_bitmap_height (FRAME_PTR f
, int id
)
187 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
191 x_bitmap_width (FRAME_PTR f
, int id
)
193 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
196 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
198 x_bitmap_pixmap (FRAME_PTR f
, int id
)
200 return (int) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
204 #ifdef HAVE_X_WINDOWS
206 x_bitmap_mask (FRAME_PTR f
, int id
)
208 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
212 /* Allocate a new bitmap record. Returns index of new record. */
215 x_allocate_bitmap_record (FRAME_PTR f
)
217 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
220 if (dpyinfo
->bitmaps
== NULL
)
222 dpyinfo
->bitmaps_size
= 10;
224 = (Bitmap_Record
*) xmalloc (dpyinfo
->bitmaps_size
* sizeof (Bitmap_Record
));
225 dpyinfo
->bitmaps_last
= 1;
229 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
230 return ++dpyinfo
->bitmaps_last
;
232 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
233 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
236 dpyinfo
->bitmaps_size
*= 2;
238 = (Bitmap_Record
*) xrealloc (dpyinfo
->bitmaps
,
239 dpyinfo
->bitmaps_size
* sizeof (Bitmap_Record
));
240 return ++dpyinfo
->bitmaps_last
;
243 /* Add one reference to the reference count of the bitmap with id ID. */
246 x_reference_bitmap (FRAME_PTR f
, int id
)
248 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
251 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
254 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
256 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
259 #ifdef HAVE_X_WINDOWS
261 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
262 bits
, width
, height
);
265 #endif /* HAVE_X_WINDOWS */
269 bitmap
= CreateBitmap (width
, height
,
270 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
271 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
275 #endif /* HAVE_NTGUI */
278 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
283 id
= x_allocate_bitmap_record (f
);
286 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
287 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
290 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
291 dpyinfo
->bitmaps
[id
- 1].height
= height
;
292 dpyinfo
->bitmaps
[id
- 1].width
= width
;
293 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
295 #ifdef HAVE_X_WINDOWS
296 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
297 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
298 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
299 #endif /* HAVE_X_WINDOWS */
302 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
303 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
304 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
305 #endif /* HAVE_NTGUI */
310 /* Create bitmap from file FILE for frame F. */
313 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
315 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
318 return -1; /* W32_TODO : bitmap support */
319 #endif /* HAVE_NTGUI */
323 void *bitmap
= ns_image_from_file (file
);
329 id
= x_allocate_bitmap_record (f
);
330 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
331 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
332 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
333 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
334 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
335 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
336 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
340 #ifdef HAVE_X_WINDOWS
341 unsigned int width
, height
;
343 int xhot
, yhot
, result
, id
;
348 /* Look for an existing bitmap with the same name. */
349 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
351 if (dpyinfo
->bitmaps
[id
].refcount
352 && dpyinfo
->bitmaps
[id
].file
353 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
355 ++dpyinfo
->bitmaps
[id
].refcount
;
360 /* Search bitmap-file-path for the file, if appropriate. */
361 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
366 filename
= SSDATA (found
);
368 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
369 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
370 if (result
!= BitmapSuccess
)
373 id
= x_allocate_bitmap_record (f
);
374 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
375 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
376 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
377 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
378 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
379 dpyinfo
->bitmaps
[id
- 1].height
= height
;
380 dpyinfo
->bitmaps
[id
- 1].width
= width
;
381 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
384 #endif /* HAVE_X_WINDOWS */
390 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
392 #ifdef HAVE_X_WINDOWS
393 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
395 XFreePixmap (dpyinfo
->display
, bm
->mask
);
396 #endif /* HAVE_X_WINDOWS */
399 DeleteObject (bm
->pixmap
);
400 #endif /* HAVE_NTGUI */
403 ns_release_object (bm
->img
);
413 /* Remove reference to bitmap with id number ID. */
416 x_destroy_bitmap (FRAME_PTR f
, int id
)
418 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
422 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
424 if (--bm
->refcount
== 0)
427 free_bitmap_record (dpyinfo
, bm
);
433 /* Free all the bitmaps for the display specified by DPYINFO. */
436 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
439 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
441 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
442 if (bm
->refcount
> 0)
443 free_bitmap_record (dpyinfo
, bm
);
445 dpyinfo
->bitmaps_last
= 0;
449 #ifdef HAVE_X_WINDOWS
451 /* Useful functions defined in the section
452 `Image type independent image structures' below. */
454 static unsigned long four_corners_best (XImagePtr ximg
,
457 unsigned long height
);
459 static int x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
,
460 int depth
, XImagePtr
*ximg
,
463 static void x_destroy_x_image (XImagePtr ximg
);
466 /* Create a mask of a bitmap. Note is this not a perfect mask.
467 It's nicer with some borders in this context */
470 x_create_bitmap_mask (struct frame
*f
, int id
)
473 XImagePtr ximg
, mask_img
;
474 unsigned long width
, height
;
477 unsigned long x
, y
, xp
, xm
, yp
, ym
;
480 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
485 pixmap
= x_bitmap_pixmap (f
, id
);
486 width
= x_bitmap_width (f
, id
);
487 height
= x_bitmap_height (f
, id
);
490 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
499 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
504 XDestroyImage (ximg
);
508 bg
= four_corners_best (ximg
, NULL
, width
, height
);
510 for (y
= 0; y
< ximg
->height
; ++y
)
512 for (x
= 0; x
< ximg
->width
; ++x
)
514 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
515 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
516 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
517 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
518 if (XGetPixel (ximg
, x
, y
) == bg
519 && XGetPixel (ximg
, x
, yp
) == bg
520 && XGetPixel (ximg
, x
, ym
) == bg
521 && XGetPixel (ximg
, xp
, y
) == bg
522 && XGetPixel (ximg
, xp
, yp
) == bg
523 && XGetPixel (ximg
, xp
, ym
) == bg
524 && XGetPixel (ximg
, xm
, y
) == bg
525 && XGetPixel (ximg
, xm
, yp
) == bg
526 && XGetPixel (ximg
, xm
, ym
) == bg
)
527 XPutPixel (mask_img
, x
, y
, 0);
529 XPutPixel (mask_img
, x
, y
, 1);
533 xassert (interrupt_input_blocked
);
534 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
535 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
537 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
539 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
540 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
542 XDestroyImage (ximg
);
543 x_destroy_x_image (mask_img
);
548 #endif /* HAVE_X_WINDOWS */
551 /***********************************************************************
553 ***********************************************************************/
555 /* Value is the number of elements of vector VECTOR. */
557 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
559 /* List of supported image types. Use define_image_type to add new
560 types. Use lookup_image_type to find a type for a given symbol. */
562 static struct image_type
*image_types
;
564 /* Cache for delayed-loading image types. */
566 static Lisp_Object Vimage_type_cache
;
568 /* The symbol `xbm' which is used as the type symbol for XBM images. */
574 Lisp_Object QCascent
, QCmargin
, QCrelief
, Qcount
, Qextension_data
;
575 Lisp_Object QCconversion
, QCcolor_symbols
, QCheuristic_mask
;
576 Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
, QCcrop
, QCrotation
;
580 Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
582 /* Function prototypes. */
584 static Lisp_Object
define_image_type (struct image_type
*type
, int loaded
);
585 static struct image_type
*lookup_image_type (Lisp_Object symbol
);
586 static void image_error (const char *format
, Lisp_Object
, Lisp_Object
);
587 static void x_laplace (struct frame
*, struct image
*);
588 static void x_emboss (struct frame
*, struct image
*);
589 static int x_build_heuristic_mask (struct frame
*, struct image
*,
592 #define CACHE_IMAGE_TYPE(type, status) \
593 do { Vimage_type_cache = Fcons (Fcons (type, status), Vimage_type_cache); } while (0)
595 #define ADD_IMAGE_TYPE(type) \
596 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
598 /* Define a new image type from TYPE. This adds a copy of TYPE to
599 image_types and caches the loading status of TYPE. */
602 define_image_type (struct image_type
*type
, int loaded
)
610 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
611 The initialized data segment is read-only. */
612 struct image_type
*p
= (struct image_type
*) xmalloc (sizeof *p
);
613 memcpy (p
, type
, sizeof *p
);
614 p
->next
= image_types
;
619 CACHE_IMAGE_TYPE (*type
->type
, success
);
624 /* Look up image type SYMBOL, and return a pointer to its image_type
625 structure. Value is null if SYMBOL is not a known image type. */
627 static INLINE
struct image_type
*
628 lookup_image_type (Lisp_Object symbol
)
630 struct image_type
*type
;
632 /* We must initialize the image-type if it hasn't been already. */
633 if (NILP (Finit_image_library (symbol
, Vdynamic_library_alist
)))
634 return 0; /* unimplemented */
636 for (type
= image_types
; type
; type
= type
->next
)
637 if (EQ (symbol
, *type
->type
))
644 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
645 valid image specification is a list whose car is the symbol
646 `image', and whose rest is a property list. The property list must
647 contain a value for key `:type'. That value must be the name of a
648 supported image type. The rest of the property list depends on the
652 valid_image_p (Lisp_Object object
)
660 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
661 if (EQ (XCAR (tem
), QCtype
))
664 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
666 struct image_type
*type
;
667 type
= lookup_image_type (XCAR (tem
));
669 valid_p
= type
->valid_p (object
);
680 /* Log error message with format string FORMAT and argument ARG.
681 Signaling an error, e.g. when an image cannot be loaded, is not a
682 good idea because this would interrupt redisplay, and the error
683 message display would lead to another redisplay. This function
684 therefore simply displays a message. */
687 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
689 add_to_log (format
, arg1
, arg2
);
694 /***********************************************************************
696 ***********************************************************************/
698 enum image_value_type
700 IMAGE_DONT_CHECK_VALUE_TYPE
,
702 IMAGE_STRING_OR_NIL_VALUE
,
704 IMAGE_POSITIVE_INTEGER_VALUE
,
705 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
,
706 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
709 IMAGE_FUNCTION_VALUE
,
714 /* Structure used when parsing image specifications. */
718 /* Name of keyword. */
721 /* The type of value allowed. */
722 enum image_value_type type
;
724 /* Non-zero means key must be present. */
727 /* Used to recognize duplicate keywords in a property list. */
730 /* The value that was found. */
735 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
737 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
740 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
741 has the format (image KEYWORD VALUE ...). One of the keyword/
742 value pairs must be `:type TYPE'. KEYWORDS is a vector of
743 image_keywords structures of size NKEYWORDS describing other
744 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
747 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
748 int nkeywords
, Lisp_Object type
)
757 while (CONSP (plist
))
759 Lisp_Object key
, value
;
761 /* First element of a pair must be a symbol. */
763 plist
= XCDR (plist
);
767 /* There must follow a value. */
770 value
= XCAR (plist
);
771 plist
= XCDR (plist
);
773 /* Find key in KEYWORDS. Error if not found. */
774 for (i
= 0; i
< nkeywords
; ++i
)
775 if (strcmp (keywords
[i
].name
, SDATA (SYMBOL_NAME (key
))) == 0)
781 /* Record that we recognized the keyword. If a keywords
782 was found more than once, it's an error. */
783 keywords
[i
].value
= value
;
786 if (keywords
[i
].count
> 1)
789 /* Check type of value against allowed type. */
790 switch (keywords
[i
].type
)
792 case IMAGE_STRING_VALUE
:
793 if (!STRINGP (value
))
797 case IMAGE_STRING_OR_NIL_VALUE
:
798 if (!STRINGP (value
) && !NILP (value
))
802 case IMAGE_SYMBOL_VALUE
:
803 if (!SYMBOLP (value
))
807 case IMAGE_POSITIVE_INTEGER_VALUE
:
808 if (!INTEGERP (value
) || XINT (value
) <= 0)
812 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
:
813 if (INTEGERP (value
) && XINT (value
) >= 0)
816 && INTEGERP (XCAR (value
)) && INTEGERP (XCDR (value
))
817 && XINT (XCAR (value
)) >= 0 && XINT (XCDR (value
)) >= 0)
821 case IMAGE_ASCENT_VALUE
:
822 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
824 else if (INTEGERP (value
)
826 && XINT (value
) <= 100)
830 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
831 if (!INTEGERP (value
) || XINT (value
) < 0)
835 case IMAGE_DONT_CHECK_VALUE_TYPE
:
838 case IMAGE_FUNCTION_VALUE
:
839 value
= indirect_function (value
);
842 || (CONSP (value
) && EQ (XCAR (value
), Qlambda
)))
846 case IMAGE_NUMBER_VALUE
:
847 if (!INTEGERP (value
) && !FLOATP (value
))
851 case IMAGE_INTEGER_VALUE
:
852 if (!INTEGERP (value
))
856 case IMAGE_BOOL_VALUE
:
857 if (!NILP (value
) && !EQ (value
, Qt
))
866 if (EQ (key
, QCtype
) && !EQ (type
, value
))
870 /* Check that all mandatory fields are present. */
871 for (i
= 0; i
< nkeywords
; ++i
)
872 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
879 /* Return the value of KEY in image specification SPEC. Value is nil
880 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
881 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
884 image_spec_value (Lisp_Object spec
, Lisp_Object key
, int *found
)
888 xassert (valid_image_p (spec
));
890 for (tail
= XCDR (spec
);
891 CONSP (tail
) && CONSP (XCDR (tail
));
892 tail
= XCDR (XCDR (tail
)))
894 if (EQ (XCAR (tail
), key
))
898 return XCAR (XCDR (tail
));
908 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
909 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
910 PIXELS non-nil means return the size in pixels, otherwise return the
911 size in canonical character units.
912 FRAME is the frame on which the image will be displayed. FRAME nil
913 or omitted means use the selected frame. */)
914 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
919 if (valid_image_p (spec
))
921 struct frame
*f
= check_x_frame (frame
);
922 int id
= lookup_image (f
, spec
);
923 struct image
*img
= IMAGE_FROM_ID (f
, id
);
924 int width
= img
->width
+ 2 * img
->hmargin
;
925 int height
= img
->height
+ 2 * img
->vmargin
;
928 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
929 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
931 size
= Fcons (make_number (width
), make_number (height
));
934 error ("Invalid image specification");
940 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
941 doc
: /* Return t if image SPEC has a mask bitmap.
942 FRAME is the frame on which the image will be displayed. FRAME nil
943 or omitted means use the selected frame. */)
944 (Lisp_Object spec
, Lisp_Object frame
)
949 if (valid_image_p (spec
))
951 struct frame
*f
= check_x_frame (frame
);
952 int id
= lookup_image (f
, spec
);
953 struct image
*img
= IMAGE_FROM_ID (f
, id
);
958 error ("Invalid image specification");
963 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
964 doc
: /* Return metadata for image SPEC.
965 FRAME is the frame on which the image will be displayed. FRAME nil
966 or omitted means use the selected frame. */)
967 (Lisp_Object spec
, Lisp_Object frame
)
972 if (valid_image_p (spec
))
974 struct frame
*f
= check_x_frame (frame
);
975 int id
= lookup_image (f
, spec
);
976 struct image
*img
= IMAGE_FROM_ID (f
, id
);
977 ext
= img
->data
.lisp_val
;
984 /***********************************************************************
985 Image type independent image structures
986 ***********************************************************************/
988 static struct image
*make_image (Lisp_Object spec
, unsigned hash
);
989 static void free_image (struct frame
*f
, struct image
*img
);
990 static int check_image_size (struct frame
*f
, int width
, int height
);
992 #define MAX_IMAGE_SIZE 6.0
993 /* Allocate and return a new image structure for image specification
994 SPEC. SPEC has a hash value of HASH. */
996 static struct image
*
997 make_image (Lisp_Object spec
, unsigned int hash
)
999 struct image
*img
= (struct image
*) xmalloc (sizeof *img
);
1000 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
1002 xassert (valid_image_p (spec
));
1003 memset (img
, 0, sizeof *img
);
1004 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
1005 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
1006 xassert (img
->type
!= NULL
);
1008 img
->data
.lisp_val
= Qnil
;
1009 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
1011 img
->corners
[BOT_CORNER
] = -1; /* Full image */
1016 /* Free image IMG which was used on frame F, including its resources. */
1019 free_image (struct frame
*f
, struct image
*img
)
1023 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1025 /* Remove IMG from the hash table of its cache. */
1027 img
->prev
->next
= img
->next
;
1029 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1032 img
->next
->prev
= img
->prev
;
1034 c
->images
[img
->id
] = NULL
;
1036 /* Free resources, then free IMG. */
1037 img
->type
->free (f
, img
);
1042 /* Return 1 if the given widths and heights are valid for display;
1043 otherwise, return 0. */
1046 check_image_size (struct frame
*f
, int width
, int height
)
1050 if (width
<= 0 || height
<= 0)
1053 if (INTEGERP (Vmax_image_size
))
1054 w
= h
= XINT (Vmax_image_size
);
1055 else if (FLOATP (Vmax_image_size
))
1059 w
= FRAME_PIXEL_WIDTH (f
);
1060 h
= FRAME_PIXEL_HEIGHT (f
);
1063 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1064 w
= (int) (XFLOAT_DATA (Vmax_image_size
) * w
);
1065 h
= (int) (XFLOAT_DATA (Vmax_image_size
) * h
);
1070 return (width
<= w
&& height
<= h
);
1073 /* Prepare image IMG for display on frame F. Must be called before
1074 drawing an image. */
1077 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1081 /* We're about to display IMG, so set its timestamp to `now'. */
1083 img
->timestamp
= EMACS_SECS (t
);
1085 /* If IMG doesn't have a pixmap yet, load it now, using the image
1086 type dependent loader function. */
1087 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1088 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1093 /* Value is the number of pixels for the ascent of image IMG when
1094 drawn in face FACE. */
1097 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1102 if (slice
->height
== img
->height
)
1103 height
= img
->height
+ img
->vmargin
;
1104 else if (slice
->y
== 0)
1105 height
= slice
->height
+ img
->vmargin
;
1107 height
= slice
->height
;
1109 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1114 /* W32 specific version. Why?. ++kfs */
1115 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1116 - FONT_BASE (face
->font
)) / 2;
1118 /* This expression is arranged so that if the image can't be
1119 exactly centered, it will be moved slightly up. This is
1120 because a typical font is `top-heavy' (due to the presence
1121 uppercase letters), so the image placement should err towards
1122 being top-heavy too. It also just generally looks better. */
1123 ascent
= (height
+ FONT_BASE(face
->font
)
1124 - FONT_DESCENT(face
->font
) + 1) / 2;
1125 #endif /* HAVE_NTGUI */
1128 ascent
= height
/ 2;
1131 ascent
= (int) (height
* img
->ascent
/ 100.0);
1137 /* Image background colors. */
1139 /* Find the "best" corner color of a bitmap.
1140 On W32, XIMG is assumed to a device context with the bitmap selected. */
1142 static RGB_PIXEL_COLOR
1143 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1144 unsigned long width
, unsigned long height
)
1146 RGB_PIXEL_COLOR corner_pixels
[4], best
;
1149 if (corners
&& corners
[BOT_CORNER
] >= 0)
1151 /* Get the colors at the corner_pixels of ximg. */
1152 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1153 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1154 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1155 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1159 /* Get the colors at the corner_pixels of ximg. */
1160 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1161 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1162 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1163 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1165 /* Choose the most frequently found color as background. */
1166 for (i
= best_count
= 0; i
< 4; ++i
)
1170 for (j
= n
= 0; j
< 4; ++j
)
1171 if (corner_pixels
[i
] == corner_pixels
[j
])
1175 best
= corner_pixels
[i
], best_count
= n
;
1181 /* Portability macros */
1185 #define Destroy_Image(img_dc, prev) \
1186 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1188 #define Free_Pixmap(display, pixmap) \
1189 DeleteObject (pixmap)
1191 #elif defined (HAVE_NS)
1193 #define Destroy_Image(ximg, dummy) \
1194 ns_release_object (ximg)
1196 #define Free_Pixmap(display, pixmap) \
1197 ns_release_object (pixmap)
1201 #define Destroy_Image(ximg, dummy) \
1202 XDestroyImage (ximg)
1204 #define Free_Pixmap(display, pixmap) \
1205 XFreePixmap (display, pixmap)
1207 #endif /* !HAVE_NTGUI && !HAVE_NS */
1210 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1211 it is guessed heuristically. If non-zero, XIMG is an existing
1212 XImage object (or device context with the image selected on W32) to
1213 use for the heuristic. */
1216 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1218 if (! img
->background_valid
)
1219 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1221 int free_ximg
= !ximg
;
1224 #endif /* HAVE_NTGUI */
1229 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1230 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1232 HDC frame_dc
= get_frame_dc (f
);
1233 ximg
= CreateCompatibleDC (frame_dc
);
1234 release_frame_dc (f
, frame_dc
);
1235 prev
= SelectObject (ximg
, img
->pixmap
);
1236 #endif /* !HAVE_NTGUI */
1239 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1242 Destroy_Image (ximg
, prev
);
1244 img
->background_valid
= 1;
1247 return img
->background
;
1250 /* Return the `background_transparent' field of IMG. If IMG doesn't
1251 have one yet, it is guessed heuristically. If non-zero, MASK is an
1252 existing XImage object to use for the heuristic. */
1255 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1257 if (! img
->background_transparent_valid
)
1258 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1262 int free_mask
= !mask
;
1265 #endif /* HAVE_NTGUI */
1270 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1271 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1273 HDC frame_dc
= get_frame_dc (f
);
1274 mask
= CreateCompatibleDC (frame_dc
);
1275 release_frame_dc (f
, frame_dc
);
1276 prev
= SelectObject (mask
, img
->mask
);
1277 #endif /* HAVE_NTGUI */
1280 img
->background_transparent
1281 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1284 Destroy_Image (mask
, prev
);
1287 img
->background_transparent
= 0;
1289 img
->background_transparent_valid
= 1;
1292 return img
->background_transparent
;
1296 /***********************************************************************
1297 Helper functions for X image types
1298 ***********************************************************************/
1300 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1302 static void x_clear_image (struct frame
*f
, struct image
*img
);
1303 static unsigned long x_alloc_image_color (struct frame
*f
,
1305 Lisp_Object color_name
,
1306 unsigned long dflt
);
1309 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1310 free the pixmap if any. MASK_P non-zero means clear the mask
1311 pixmap if any. COLORS_P non-zero means free colors allocated for
1312 the image, if any. */
1315 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1318 if (pixmap_p
&& img
->pixmap
)
1320 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1321 img
->pixmap
= NO_PIXMAP
;
1322 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1323 img
->background_valid
= 0;
1326 if (mask_p
&& img
->mask
)
1328 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1329 img
->mask
= NO_PIXMAP
;
1330 img
->background_transparent_valid
= 0;
1333 if (colors_p
&& img
->ncolors
)
1335 /* W32_TODO: color table support. */
1336 #ifdef HAVE_X_WINDOWS
1337 x_free_colors (f
, img
->colors
, img
->ncolors
);
1338 #endif /* HAVE_X_WINDOWS */
1339 xfree (img
->colors
);
1346 /* Free X resources of image IMG which is used on frame F. */
1349 x_clear_image (struct frame
*f
, struct image
*img
)
1352 x_clear_image_1 (f
, img
, 1, 1, 1);
1357 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1358 cannot be allocated, use DFLT. Add a newly allocated color to
1359 IMG->colors, so that it can be freed again. Value is the pixel
1362 static unsigned long
1363 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1367 unsigned long result
;
1369 xassert (STRINGP (color_name
));
1371 if (x_defined_color (f
, SDATA (color_name
), &color
, 1))
1373 /* This isn't called frequently so we get away with simply
1374 reallocating the color vector to the needed size, here. */
1377 (unsigned long *) xrealloc (img
->colors
,
1378 img
->ncolors
* sizeof *img
->colors
);
1379 img
->colors
[img
->ncolors
- 1] = color
.pixel
;
1380 result
= color
.pixel
;
1390 /***********************************************************************
1392 ***********************************************************************/
1394 static struct image
*search_image_cache (struct frame
*, Lisp_Object
, unsigned);
1395 static void cache_image (struct frame
*f
, struct image
*img
);
1396 static void postprocess_image (struct frame
*, struct image
*);
1398 /* Return a new, initialized image cache that is allocated from the
1399 heap. Call free_image_cache to free an image cache. */
1401 struct image_cache
*
1402 make_image_cache (void)
1404 struct image_cache
*c
= (struct image_cache
*) xmalloc (sizeof *c
);
1407 memset (c
, 0, sizeof *c
);
1409 c
->images
= (struct image
**) xmalloc (c
->size
* sizeof *c
->images
);
1410 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1411 c
->buckets
= (struct image
**) xmalloc (size
);
1412 memset (c
->buckets
, 0, size
);
1417 /* Find an image matching SPEC in the cache, and return it. If no
1418 image is found, return NULL. */
1419 static struct image
*
1420 search_image_cache (struct frame
*f
, Lisp_Object spec
, unsigned int hash
)
1423 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1424 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1426 if (!c
) return NULL
;
1428 /* If the image spec does not specify a background color, the cached
1429 image must have the same background color as the current frame.
1430 The foreground color must also match, for the sake of monochrome
1433 In fact, we could ignore the foreground color matching condition
1434 for color images, or if the image spec specifies :foreground;
1435 similarly we could ignore the background color matching condition
1436 for formats that don't use transparency (such as jpeg), or if the
1437 image spec specifies :background. However, the extra memory
1438 usage is probably negligible in practice, so we don't bother. */
1440 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1441 if (img
->hash
== hash
1442 && !NILP (Fequal (img
->spec
, spec
))
1443 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1444 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1450 /* Search frame F for an image with spec SPEC, and free it. */
1453 uncache_image (struct frame
*f
, Lisp_Object spec
)
1455 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1458 free_image (f
, img
);
1459 /* As display glyphs may still be referring to the image ID, we
1460 must garbage the frame (Bug#6426). */
1461 SET_FRAME_GARBAGED (f
);
1466 /* Free image cache of frame F. Be aware that X frames share images
1470 free_image_cache (struct frame
*f
)
1472 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1477 /* Cache should not be referenced by any frame when freed. */
1478 xassert (c
->refcount
== 0);
1480 for (i
= 0; i
< c
->used
; ++i
)
1481 free_image (f
, c
->images
[i
]);
1485 FRAME_IMAGE_CACHE (f
) = NULL
;
1490 /* Clear image cache of frame F. FILTER=t means free all images.
1491 FILTER=nil means clear only images that haven't been
1492 displayed for some time.
1493 Else, only free the images which have FILTER in their `dependencies'.
1494 Should be called from time to time to reduce the number of loaded images.
1495 If image-cache-eviction-delay is non-nil, this frees images in the cache
1496 which weren't displayed for at least that many seconds. */
1499 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1501 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1507 /* Block input so that we won't be interrupted by a SIGIO
1508 while being in an inconsistent state. */
1513 /* Filter image cache. */
1514 for (i
= 0; i
< c
->used
; ++i
)
1516 struct image
*img
= c
->images
[i
];
1517 if (img
&& (EQ (Qt
, filter
)
1518 || !NILP (Fmember (filter
, img
->dependencies
))))
1520 free_image (f
, img
);
1525 else if (INTEGERP (Vimage_cache_eviction_delay
))
1527 /* Free cache based on timestamp. */
1530 int delay
, nimages
= 0;
1532 for (i
= 0; i
< c
->used
; ++i
)
1536 /* If the number of cached images has grown unusually large,
1537 decrease the cache eviction delay (Bug#6230). */
1538 delay
= XFASTINT (Vimage_cache_eviction_delay
);
1540 delay
= max (1, 1600 * delay
/ (nimages
*nimages
));
1543 old
= EMACS_SECS (t
) - delay
;
1545 for (i
= 0; i
< c
->used
; ++i
)
1547 struct image
*img
= c
->images
[i
];
1548 if (img
&& img
->timestamp
< old
)
1550 free_image (f
, img
);
1556 /* We may be clearing the image cache because, for example,
1557 Emacs was iconified for a longer period of time. In that
1558 case, current matrices may still contain references to
1559 images freed above. So, clear these matrices. */
1562 Lisp_Object tail
, frame
;
1564 FOR_EACH_FRAME (tail
, frame
)
1566 struct frame
*f
= XFRAME (frame
);
1567 if (FRAME_IMAGE_CACHE (f
) == c
)
1568 clear_current_matrices (f
);
1571 ++windows_or_buffers_changed
;
1579 clear_image_caches (Lisp_Object filter
)
1581 /* FIXME: We want to do
1582 * struct terminal *t;
1583 * for (t = terminal_list; t; t = t->next_terminal)
1584 * clear_image_cache (t, filter); */
1585 Lisp_Object tail
, frame
;
1586 FOR_EACH_FRAME (tail
, frame
)
1587 if (FRAME_WINDOW_P (XFRAME (frame
)))
1588 clear_image_cache (XFRAME (frame
), filter
);
1591 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1593 doc
: /* Clear the image cache.
1594 FILTER nil or a frame means clear all images in the selected frame.
1595 FILTER t means clear the image caches of all frames.
1596 Anything else, means only clear those images which refer to FILTER,
1597 which is then usually a filename. */)
1598 (Lisp_Object filter
)
1600 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1601 clear_image_caches (filter
);
1603 clear_image_cache (check_x_frame (filter
), Qt
);
1609 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1611 doc
: /* Fush the image with specification SPEC on frame FRAME.
1612 This removes the image from the Emacs image cache. If SPEC specifies
1613 an image file, the next redisplay of this image will read from the
1614 current contents of that file.
1616 FRAME nil or omitted means use the selected frame.
1617 FRAME t means refresh the image on all frames. */)
1618 (Lisp_Object spec
, Lisp_Object frame
)
1620 if (!valid_image_p (spec
))
1621 error ("Invalid image specification");
1626 FOR_EACH_FRAME (tail
, frame
)
1628 struct frame
*f
= XFRAME (frame
);
1629 if (FRAME_WINDOW_P (f
))
1630 uncache_image (f
, spec
);
1634 uncache_image (check_x_frame (frame
), spec
);
1640 /* Compute masks and transform image IMG on frame F, as specified
1641 by the image's specification, */
1644 postprocess_image (struct frame
*f
, struct image
*img
)
1646 /* Manipulation of the image's mask. */
1649 Lisp_Object conversion
, spec
;
1654 /* `:heuristic-mask t'
1656 means build a mask heuristically.
1657 `:heuristic-mask (R G B)'
1658 `:mask (heuristic (R G B))'
1659 means build a mask from color (R G B) in the
1662 means remove a mask, if any. */
1664 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1666 x_build_heuristic_mask (f
, img
, mask
);
1671 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1673 if (EQ (mask
, Qheuristic
))
1674 x_build_heuristic_mask (f
, img
, Qt
);
1675 else if (CONSP (mask
)
1676 && EQ (XCAR (mask
), Qheuristic
))
1678 if (CONSP (XCDR (mask
)))
1679 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1681 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1683 else if (NILP (mask
) && found_p
&& img
->mask
)
1685 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1686 img
->mask
= NO_PIXMAP
;
1691 /* Should we apply an image transformation algorithm? */
1692 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1693 if (EQ (conversion
, Qdisabled
))
1694 x_disable_image (f
, img
);
1695 else if (EQ (conversion
, Qlaplace
))
1697 else if (EQ (conversion
, Qemboss
))
1699 else if (CONSP (conversion
)
1700 && EQ (XCAR (conversion
), Qedge_detection
))
1703 tem
= XCDR (conversion
);
1705 x_edge_detection (f
, img
,
1706 Fplist_get (tem
, QCmatrix
),
1707 Fplist_get (tem
, QCcolor_adjustment
));
1713 /* Return the id of image with Lisp specification SPEC on frame F.
1714 SPEC must be a valid Lisp image specification (see valid_image_p). */
1717 lookup_image (struct frame
*f
, Lisp_Object spec
)
1719 struct image_cache
*c
;
1724 /* F must be a window-system frame, and SPEC must be a valid image
1726 xassert (FRAME_WINDOW_P (f
));
1727 xassert (valid_image_p (spec
));
1729 c
= FRAME_IMAGE_CACHE (f
);
1731 /* Look up SPEC in the hash table of the image cache. */
1732 hash
= sxhash (spec
, 0);
1733 img
= search_image_cache (f
, spec
, hash
);
1734 if (img
&& img
->load_failed_p
)
1736 free_image (f
, img
);
1740 /* If not found, create a new image and cache it. */
1744 img
= make_image (spec
, hash
);
1745 cache_image (f
, img
);
1746 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1747 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1748 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1750 /* If we can't load the image, and we don't have a width and
1751 height, use some arbitrary width and height so that we can
1752 draw a rectangle for it. */
1753 if (img
->load_failed_p
)
1757 value
= image_spec_value (spec
, QCwidth
, NULL
);
1758 img
->width
= (INTEGERP (value
)
1759 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1760 value
= image_spec_value (spec
, QCheight
, NULL
);
1761 img
->height
= (INTEGERP (value
)
1762 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1766 /* Handle image type independent image attributes
1767 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1768 `:background COLOR'. */
1769 Lisp_Object ascent
, margin
, relief
, bg
;
1771 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1772 if (INTEGERP (ascent
))
1773 img
->ascent
= XFASTINT (ascent
);
1774 else if (EQ (ascent
, Qcenter
))
1775 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1777 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1778 if (INTEGERP (margin
) && XINT (margin
) >= 0)
1779 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1780 else if (CONSP (margin
) && INTEGERP (XCAR (margin
))
1781 && INTEGERP (XCDR (margin
)))
1783 if (XINT (XCAR (margin
)) > 0)
1784 img
->hmargin
= XFASTINT (XCAR (margin
));
1785 if (XINT (XCDR (margin
)) > 0)
1786 img
->vmargin
= XFASTINT (XCDR (margin
));
1789 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1790 if (INTEGERP (relief
))
1792 img
->relief
= XINT (relief
);
1793 img
->hmargin
+= eabs (img
->relief
);
1794 img
->vmargin
+= eabs (img
->relief
);
1797 if (! img
->background_valid
)
1799 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1803 = x_alloc_image_color (f
, img
, bg
,
1804 FRAME_BACKGROUND_PIXEL (f
));
1805 img
->background_valid
= 1;
1809 /* Do image transformations and compute masks, unless we
1810 don't have the image yet. */
1811 if (!EQ (*img
->type
->type
, Qpostscript
))
1812 postprocess_image (f
, img
);
1818 /* We're using IMG, so set its timestamp to `now'. */
1819 EMACS_GET_TIME (now
);
1820 img
->timestamp
= EMACS_SECS (now
);
1822 /* Value is the image id. */
1827 /* Cache image IMG in the image cache of frame F. */
1830 cache_image (struct frame
*f
, struct image
*img
)
1832 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1835 /* Find a free slot in c->images. */
1836 for (i
= 0; i
< c
->used
; ++i
)
1837 if (c
->images
[i
] == NULL
)
1840 /* If no free slot found, maybe enlarge c->images. */
1841 if (i
== c
->used
&& c
->used
== c
->size
)
1844 c
->images
= (struct image
**) xrealloc (c
->images
,
1845 c
->size
* sizeof *c
->images
);
1848 /* Add IMG to c->images, and assign IMG an id. */
1854 /* Add IMG to the cache's hash table. */
1855 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1856 img
->next
= c
->buckets
[i
];
1858 img
->next
->prev
= img
;
1860 c
->buckets
[i
] = img
;
1864 /* Call FN on every image in the image cache of frame F. Used to mark
1865 Lisp Objects in the image cache. */
1867 /* Mark Lisp objects in image IMG. */
1870 mark_image (struct image
*img
)
1872 mark_object (img
->spec
);
1873 mark_object (img
->dependencies
);
1875 if (!NILP (img
->data
.lisp_val
))
1876 mark_object (img
->data
.lisp_val
);
1881 mark_image_cache (struct image_cache
*c
)
1886 for (i
= 0; i
< c
->used
; ++i
)
1888 mark_image (c
->images
[i
]);
1894 /***********************************************************************
1895 X / NS / W32 support code
1896 ***********************************************************************/
1900 /* Macro for defining functions that will be loaded from image DLLs. */
1901 #define DEF_IMGLIB_FN(func,args) int (FAR CDECL *fn_##func)args
1903 /* Macro for loading those image functions from the library. */
1904 #define LOAD_IMGLIB_FN(lib,func) { \
1905 fn_##func = (void *) GetProcAddress (lib, #func); \
1906 if (!fn_##func) return 0; \
1909 /* Load a DLL implementing an image type.
1910 The argument LIBRARIES is usually the variable
1911 `dynamic-library-alist', which associates a symbol, identifying
1912 an external DLL library, to a list of possible filenames.
1913 The function returns NULL if no library could be loaded for
1914 the given symbol, or if the library was previously loaded;
1915 else the handle of the DLL. */
1917 w32_delayed_load (Lisp_Object libraries
, Lisp_Object type
)
1919 HMODULE library
= NULL
;
1921 if (CONSP (libraries
) && NILP (Fassq (type
, Vimage_type_cache
)))
1923 Lisp_Object dlls
= Fassq (type
, libraries
);
1926 for (dlls
= XCDR (dlls
); CONSP (dlls
); dlls
= XCDR (dlls
))
1928 CHECK_STRING_CAR (dlls
);
1929 if (library
= LoadLibrary (SDATA (XCAR (dlls
))))
1937 #endif /* HAVE_NTGUI */
1939 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1940 XImagePtr
*, Pixmap
*);
1941 static void x_destroy_x_image (XImagePtr
);
1942 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1945 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1946 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1947 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1948 via xmalloc. Print error messages via image_error if an error
1949 occurs. Value is non-zero if successful.
1951 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1952 should indicate the bit depth of the image. */
1955 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1956 XImagePtr
*ximg
, Pixmap
*pixmap
)
1958 #ifdef HAVE_X_WINDOWS
1959 Display
*display
= FRAME_X_DISPLAY (f
);
1960 Window window
= FRAME_X_WINDOW (f
);
1961 Screen
*screen
= FRAME_X_SCREEN (f
);
1963 xassert (interrupt_input_blocked
);
1966 depth
= DefaultDepthOfScreen (screen
);
1967 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1968 depth
, ZPixmap
, 0, NULL
, width
, height
,
1969 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1972 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1976 /* Allocate image raster. */
1977 (*ximg
)->data
= (char *) xmalloc ((*ximg
)->bytes_per_line
* height
);
1979 /* Allocate a pixmap of the same size. */
1980 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1981 if (*pixmap
== NO_PIXMAP
)
1983 x_destroy_x_image (*ximg
);
1985 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1990 #endif /* HAVE_X_WINDOWS */
1994 BITMAPINFOHEADER
*header
;
1996 int scanline_width_bits
;
1998 int palette_colors
= 0;
2003 if (depth
!= 1 && depth
!= 4 && depth
!= 8
2004 && depth
!= 16 && depth
!= 24 && depth
!= 32)
2006 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
2010 scanline_width_bits
= width
* depth
;
2011 remainder
= scanline_width_bits
% 32;
2014 scanline_width_bits
+= 32 - remainder
;
2016 /* Bitmaps with a depth less than 16 need a palette. */
2017 /* BITMAPINFO structure already contains the first RGBQUAD. */
2019 palette_colors
= 1 << depth
- 1;
2021 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2024 image_error ("Unable to allocate memory for XImage", Qnil
, Qnil
);
2028 header
= &(*ximg
)->info
.bmiHeader
;
2029 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2030 header
->biSize
= sizeof (*header
);
2031 header
->biWidth
= width
;
2032 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2033 header
->biPlanes
= 1;
2034 header
->biBitCount
= depth
;
2035 header
->biCompression
= BI_RGB
;
2036 header
->biClrUsed
= palette_colors
;
2038 /* TODO: fill in palette. */
2041 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2042 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2043 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2044 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2045 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2046 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2047 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2048 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2051 hdc
= get_frame_dc (f
);
2053 /* Create a DIBSection and raster array for the bitmap,
2054 and store its handle in *pixmap. */
2055 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2056 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2057 /* casting avoids a GCC warning */
2058 (void **)&((*ximg
)->data
), NULL
, 0);
2060 /* Realize display palette and garbage all frames. */
2061 release_frame_dc (f
, hdc
);
2063 if (*pixmap
== NULL
)
2065 DWORD err
= GetLastError ();
2066 Lisp_Object errcode
;
2067 /* All system errors are < 10000, so the following is safe. */
2068 XSETINT (errcode
, (int) err
);
2069 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2070 x_destroy_x_image (*ximg
);
2076 #endif /* HAVE_NTGUI */
2079 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2083 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2092 /* Destroy XImage XIMG. Free XIMG->data. */
2095 x_destroy_x_image (XImagePtr ximg
)
2097 xassert (interrupt_input_blocked
);
2100 #ifdef HAVE_X_WINDOWS
2103 XDestroyImage (ximg
);
2104 #endif /* HAVE_X_WINDOWS */
2106 /* Data will be freed by DestroyObject. */
2109 #endif /* HAVE_NTGUI */
2111 ns_release_object (ximg
);
2112 #endif /* HAVE_NS */
2117 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2118 are width and height of both the image and pixmap. */
2121 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2123 #ifdef HAVE_X_WINDOWS
2126 xassert (interrupt_input_blocked
);
2127 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2128 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2129 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2130 #endif /* HAVE_X_WINDOWS */
2133 #if 0 /* I don't think this is necessary looking at where it is used. */
2134 HDC hdc
= get_frame_dc (f
);
2135 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2136 release_frame_dc (f
, hdc
);
2138 #endif /* HAVE_NTGUI */
2141 xassert (ximg
== pixmap
);
2142 ns_retain_object (ximg
);
2147 /***********************************************************************
2149 ***********************************************************************/
2151 static unsigned char *slurp_file (char *, int *);
2154 /* Find image file FILE. Look in data-directory/images, then
2155 x-bitmap-file-path. Value is the encoded full name of the file
2156 found, or nil if not found. */
2159 x_find_image_file (Lisp_Object file
)
2161 Lisp_Object file_found
, search_path
;
2164 /* TODO I think this should use something like image-load-path
2165 instead. Unfortunately, that can contain non-string elements. */
2166 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2168 Vx_bitmap_file_path
);
2170 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2171 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2177 file_found
= ENCODE_FILE (file_found
);
2185 /* Read FILE into memory. Value is a pointer to a buffer allocated
2186 with xmalloc holding FILE's contents. Value is null if an error
2187 occurred. *SIZE is set to the size of the file. */
2189 static unsigned char *
2190 slurp_file (char *file
, int *size
)
2193 unsigned char *buf
= NULL
;
2196 if (stat (file
, &st
) == 0
2197 && (fp
= fopen (file
, "rb")) != NULL
2198 && (buf
= (unsigned char *) xmalloc (st
.st_size
),
2199 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2220 /***********************************************************************
2222 ***********************************************************************/
2224 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2225 static int xbm_load (struct frame
*f
, struct image
*img
);
2226 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2227 unsigned char *, unsigned char *);
2228 static int xbm_image_p (Lisp_Object object
);
2229 static int xbm_read_bitmap_data (struct frame
*f
,
2230 unsigned char *, unsigned char *,
2231 int *, int *, unsigned char **, int);
2232 static int xbm_file_p (Lisp_Object
);
2235 /* Indices of image specification fields in xbm_format, below. */
2237 enum xbm_keyword_index
2255 /* Vector of image_keyword structures describing the format
2256 of valid XBM image specifications. */
2258 static const struct image_keyword xbm_format
[XBM_LAST
] =
2260 {":type", IMAGE_SYMBOL_VALUE
, 1},
2261 {":file", IMAGE_STRING_VALUE
, 0},
2262 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2263 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2264 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2265 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2266 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2267 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2268 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
2269 {":relief", IMAGE_INTEGER_VALUE
, 0},
2270 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2271 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2272 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2275 /* Structure describing the image type XBM. */
2277 static struct image_type xbm_type
=
2286 /* Tokens returned from xbm_scan. */
2295 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2296 A valid specification is a list starting with the symbol `image'
2297 The rest of the list is a property list which must contain an
2300 If the specification specifies a file to load, it must contain
2301 an entry `:file FILENAME' where FILENAME is a string.
2303 If the specification is for a bitmap loaded from memory it must
2304 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2305 WIDTH and HEIGHT are integers > 0. DATA may be:
2307 1. a string large enough to hold the bitmap data, i.e. it must
2308 have a size >= (WIDTH + 7) / 8 * HEIGHT
2310 2. a bool-vector of size >= WIDTH * HEIGHT
2312 3. a vector of strings or bool-vectors, one for each line of the
2315 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2316 may not be specified in this case because they are defined in the
2319 Both the file and data forms may contain the additional entries
2320 `:background COLOR' and `:foreground COLOR'. If not present,
2321 foreground and background of the frame on which the image is
2322 displayed is used. */
2325 xbm_image_p (Lisp_Object object
)
2327 struct image_keyword kw
[XBM_LAST
];
2329 memcpy (kw
, xbm_format
, sizeof kw
);
2330 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2333 xassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2335 if (kw
[XBM_FILE
].count
)
2337 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2340 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2342 /* In-memory XBM file. */
2343 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2351 /* Entries for `:width', `:height' and `:data' must be present. */
2352 if (!kw
[XBM_WIDTH
].count
2353 || !kw
[XBM_HEIGHT
].count
2354 || !kw
[XBM_DATA
].count
)
2357 data
= kw
[XBM_DATA
].value
;
2358 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2359 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2361 /* Check type of data, and width and height against contents of
2367 /* Number of elements of the vector must be >= height. */
2368 if (XVECTOR (data
)->size
< height
)
2371 /* Each string or bool-vector in data must be large enough
2372 for one line of the image. */
2373 for (i
= 0; i
< height
; ++i
)
2375 Lisp_Object elt
= XVECTOR (data
)->contents
[i
];
2380 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2383 else if (BOOL_VECTOR_P (elt
))
2385 if (XBOOL_VECTOR (elt
)->size
< width
)
2392 else if (STRINGP (data
))
2395 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2398 else if (BOOL_VECTOR_P (data
))
2400 if (XBOOL_VECTOR (data
)->size
< width
* height
)
2411 /* Scan a bitmap file. FP is the stream to read from. Value is
2412 either an enumerator from enum xbm_token, or a character for a
2413 single-character token, or 0 at end of file. If scanning an
2414 identifier, store the lexeme of the identifier in SVAL. If
2415 scanning a number, store its value in *IVAL. */
2418 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2424 /* Skip white space. */
2425 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
2430 else if (isdigit (c
))
2432 int value
= 0, digit
;
2434 if (c
== '0' && *s
< end
)
2437 if (c
== 'x' || c
== 'X')
2444 else if (c
>= 'a' && c
<= 'f')
2445 digit
= c
- 'a' + 10;
2446 else if (c
>= 'A' && c
<= 'F')
2447 digit
= c
- 'A' + 10;
2450 value
= 16 * value
+ digit
;
2453 else if (isdigit (c
))
2457 && (c
= *(*s
)++, isdigit (c
)))
2458 value
= 8 * value
+ c
- '0';
2465 && (c
= *(*s
)++, isdigit (c
)))
2466 value
= 10 * value
+ c
- '0';
2474 else if (isalpha (c
) || c
== '_')
2478 && (c
= *(*s
)++, (isalnum (c
) || c
== '_')))
2485 else if (c
== '/' && **s
== '*')
2487 /* C-style comment. */
2489 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2503 /* Create a Windows bitmap from X bitmap data. */
2505 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2507 static unsigned char swap_nibble
[16]
2508 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2509 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2510 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2511 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2513 unsigned char *bits
, *p
;
2516 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2517 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2518 bits
= (unsigned char *) alloca (height
* w2
);
2519 memset (bits
, 0, height
* w2
);
2520 for (i
= 0; i
< height
; i
++)
2523 for (j
= 0; j
< w1
; j
++)
2525 /* Bitswap XBM bytes to match how Windows does things. */
2526 unsigned char c
= *data
++;
2527 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2528 | (swap_nibble
[(c
>>4) & 0xf]));
2531 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2537 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2538 COLORREF foreground
, COLORREF background
)
2540 HDC hdc
, old_img_dc
, new_img_dc
;
2541 HGDIOBJ old_prev
, new_prev
;
2544 hdc
= get_frame_dc (f
);
2545 old_img_dc
= CreateCompatibleDC (hdc
);
2546 new_img_dc
= CreateCompatibleDC (hdc
);
2547 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2548 release_frame_dc (f
, hdc
);
2549 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2550 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2551 /* Windows convention for mono bitmaps is black = background,
2552 white = foreground. */
2553 SetTextColor (new_img_dc
, background
);
2554 SetBkColor (new_img_dc
, foreground
);
2556 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2559 SelectObject (old_img_dc
, old_prev
);
2560 SelectObject (new_img_dc
, new_prev
);
2561 DeleteDC (old_img_dc
);
2562 DeleteDC (new_img_dc
);
2563 DeleteObject (img
->pixmap
);
2564 if (new_pixmap
== 0)
2565 fprintf (stderr
, "Failed to convert image to color.\n");
2567 img
->pixmap
= new_pixmap
;
2570 #define XBM_BIT_SHUFFLE(b) (~(b))
2574 #define XBM_BIT_SHUFFLE(b) (b)
2576 #endif /* HAVE_NTGUI */
2580 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2581 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2582 int non_default_colors
)
2586 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2588 /* If colors were specified, transfer the bitmap to a color one. */
2589 if (non_default_colors
)
2590 convert_mono_to_color_image (f
, img
, fg
, bg
);
2592 #elif defined (HAVE_NS)
2593 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2597 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2600 img
->width
, img
->height
,
2602 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
2603 #endif /* !HAVE_NTGUI && !HAVE_NS */
2608 /* Replacement for XReadBitmapFileData which isn't available under old
2609 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2610 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2611 the image. Return in *DATA the bitmap data allocated with xmalloc.
2612 Value is non-zero if successful. DATA null means just test if
2613 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2614 is non-zero, inhibit the call to image_error when the image size is
2615 invalid (the bitmap remains unread). */
2618 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2619 int *width
, int *height
, unsigned char **data
,
2620 int inhibit_image_error
)
2622 unsigned char *s
= contents
;
2623 char buffer
[BUFSIZ
];
2626 int bytes_per_line
, i
, nbytes
;
2632 LA1 = xbm_scan (&s, end, buffer, &value)
2634 #define expect(TOKEN) \
2635 if (LA1 != (TOKEN)) \
2640 #define expect_ident(IDENT) \
2641 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2646 *width
= *height
= -1;
2649 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2651 /* Parse defines for width, height and hot-spots. */
2655 expect_ident ("define");
2656 expect (XBM_TK_IDENT
);
2658 if (LA1
== XBM_TK_NUMBER
)
2660 char *p
= strrchr (buffer
, '_');
2661 p
= p
? p
+ 1 : buffer
;
2662 if (strcmp (p
, "width") == 0)
2664 else if (strcmp (p
, "height") == 0)
2667 expect (XBM_TK_NUMBER
);
2670 if (!check_image_size (f
, *width
, *height
))
2672 if (!inhibit_image_error
)
2673 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2676 else if (data
== NULL
)
2679 /* Parse bits. Must start with `static'. */
2680 expect_ident ("static");
2681 if (LA1
== XBM_TK_IDENT
)
2683 if (strcmp (buffer
, "unsigned") == 0)
2686 expect_ident ("char");
2688 else if (strcmp (buffer
, "short") == 0)
2692 if (*width
% 16 && *width
% 16 < 9)
2695 else if (strcmp (buffer
, "char") == 0)
2703 expect (XBM_TK_IDENT
);
2709 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2710 nbytes
= bytes_per_line
* *height
;
2711 p
= *data
= (unsigned char *) xmalloc (nbytes
);
2715 for (i
= 0; i
< nbytes
; i
+= 2)
2718 expect (XBM_TK_NUMBER
);
2720 *p
++ = XBM_BIT_SHUFFLE (val
);
2721 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2722 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2724 if (LA1
== ',' || LA1
== '}')
2732 for (i
= 0; i
< nbytes
; ++i
)
2735 expect (XBM_TK_NUMBER
);
2737 *p
++ = XBM_BIT_SHUFFLE (val
);
2739 if (LA1
== ',' || LA1
== '}')
2764 /* Load XBM image IMG which will be displayed on frame F from buffer
2765 CONTENTS. END is the end of the buffer. Value is non-zero if
2769 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2773 unsigned char *data
;
2776 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2780 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2781 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2782 int non_default_colors
= 0;
2785 xassert (img
->width
> 0 && img
->height
> 0);
2787 /* Get foreground and background colors, maybe allocate colors. */
2788 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2791 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2792 non_default_colors
= 1;
2794 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2797 background
= x_alloc_image_color (f
, img
, value
, background
);
2798 img
->background
= background
;
2799 img
->background_valid
= 1;
2800 non_default_colors
= 1;
2803 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2804 foreground
, background
,
2805 non_default_colors
);
2808 if (img
->pixmap
== NO_PIXMAP
)
2810 x_clear_image (f
, img
);
2811 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2817 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2823 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2826 xbm_file_p (Lisp_Object data
)
2829 return (STRINGP (data
)
2830 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2831 (SDATA (data
) + SBYTES (data
)),
2836 /* Fill image IMG which is used on frame F with pixmap data. Value is
2837 non-zero if successful. */
2840 xbm_load (struct frame
*f
, struct image
*img
)
2843 Lisp_Object file_name
;
2845 xassert (xbm_image_p (img
->spec
));
2847 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2848 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2849 if (STRINGP (file_name
))
2852 unsigned char *contents
;
2855 file
= x_find_image_file (file_name
);
2856 if (!STRINGP (file
))
2858 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2862 contents
= slurp_file (SDATA (file
), &size
);
2863 if (contents
== NULL
)
2865 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2869 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2873 struct image_keyword fmt
[XBM_LAST
];
2875 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2876 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2877 int non_default_colors
= 0;
2880 int in_memory_file_p
= 0;
2882 /* See if data looks like an in-memory XBM file. */
2883 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2884 in_memory_file_p
= xbm_file_p (data
);
2886 /* Parse the image specification. */
2887 memcpy (fmt
, xbm_format
, sizeof fmt
);
2888 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2891 /* Get specified width, and height. */
2892 if (!in_memory_file_p
)
2894 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2895 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2896 xassert (img
->width
> 0 && img
->height
> 0);
2899 /* Get foreground and background colors, maybe allocate colors. */
2900 if (fmt
[XBM_FOREGROUND
].count
2901 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2903 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2905 non_default_colors
= 1;
2908 if (fmt
[XBM_BACKGROUND
].count
2909 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2911 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2913 non_default_colors
= 1;
2916 if (in_memory_file_p
)
2917 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2926 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2928 p
= bits
= (char *) alloca (nbytes
* img
->height
);
2929 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2931 Lisp_Object line
= XVECTOR (data
)->contents
[i
];
2933 memcpy (p
, SDATA (line
), nbytes
);
2935 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2938 else if (STRINGP (data
))
2939 bits
= SDATA (data
);
2941 bits
= XBOOL_VECTOR (data
)->data
;
2947 /* Windows mono bitmaps are reversed compared with X. */
2948 invertedBits
= bits
;
2949 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2951 bits
= (char *) alloca (nbytes
);
2952 for (i
= 0; i
< nbytes
; i
++)
2953 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2956 /* Create the pixmap. */
2958 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2959 foreground
, background
,
2960 non_default_colors
);
2965 image_error ("Unable to create pixmap for XBM image `%s'",
2967 x_clear_image (f
, img
);
2977 /***********************************************************************
2979 ***********************************************************************/
2981 #if defined (HAVE_XPM) || defined (HAVE_NS)
2983 static int xpm_image_p (Lisp_Object object
);
2984 static int xpm_load (struct frame
*f
, struct image
*img
);
2985 static int xpm_valid_color_symbols_p (Lisp_Object
);
2987 #endif /* HAVE_XPM || HAVE_NS */
2991 /* Indicate to xpm.h that we don't have Xlib. */
2993 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
2994 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
2995 #define XColor xpm_XColor
2996 #define XImage xpm_XImage
2997 #define Display xpm_Display
2998 #define PIXEL_ALREADY_TYPEDEFED
2999 #include "X11/xpm.h"
3004 #undef PIXEL_ALREADY_TYPEDEFED
3006 #include "X11/xpm.h"
3007 #endif /* HAVE_NTGUI */
3008 #endif /* HAVE_XPM */
3010 #if defined (HAVE_XPM) || defined (HAVE_NS)
3011 /* The symbol `xpm' identifying XPM-format images. */
3015 /* Indices of image specification fields in xpm_format, below. */
3017 enum xpm_keyword_index
3033 /* Vector of image_keyword structures describing the format
3034 of valid XPM image specifications. */
3036 static const struct image_keyword xpm_format
[XPM_LAST
] =
3038 {":type", IMAGE_SYMBOL_VALUE
, 1},
3039 {":file", IMAGE_STRING_VALUE
, 0},
3040 {":data", IMAGE_STRING_VALUE
, 0},
3041 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3042 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
3043 {":relief", IMAGE_INTEGER_VALUE
, 0},
3044 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3045 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3046 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3047 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3048 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3051 /* Structure describing the image type XPM. */
3053 static struct image_type xpm_type
=
3062 #ifdef HAVE_X_WINDOWS
3064 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3065 functions for allocating image colors. Our own functions handle
3066 color allocation failures more gracefully than the ones on the XPM
3069 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3070 #define ALLOC_XPM_COLORS
3072 #endif /* HAVE_X_WINDOWS */
3074 #ifdef ALLOC_XPM_COLORS
3076 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3077 static void xpm_free_color_cache (void);
3078 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3079 static int xpm_color_bucket (char *);
3080 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3083 /* An entry in a hash table used to cache color definitions of named
3084 colors. This cache is necessary to speed up XPM image loading in
3085 case we do color allocations ourselves. Without it, we would need
3086 a call to XParseColor per pixel in the image. */
3088 struct xpm_cached_color
3090 /* Next in collision chain. */
3091 struct xpm_cached_color
*next
;
3093 /* Color definition (RGB and pixel color). */
3100 /* The hash table used for the color cache, and its bucket vector
3103 #define XPM_COLOR_CACHE_BUCKETS 1001
3104 struct xpm_cached_color
**xpm_color_cache
;
3106 /* Initialize the color cache. */
3109 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3111 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3112 xpm_color_cache
= (struct xpm_cached_color
**) xmalloc (nbytes
);
3113 memset (xpm_color_cache
, 0, nbytes
);
3114 init_color_table ();
3116 if (attrs
->valuemask
& XpmColorSymbols
)
3121 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3122 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3123 attrs
->colorsymbols
[i
].value
, &color
))
3125 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3127 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3132 /* Free the color cache. */
3135 xpm_free_color_cache (void)
3137 struct xpm_cached_color
*p
, *next
;
3140 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3141 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3147 xfree (xpm_color_cache
);
3148 xpm_color_cache
= NULL
;
3149 free_color_table ();
3152 /* Return the bucket index for color named COLOR_NAME in the color
3156 xpm_color_bucket (char *color_name
)
3161 for (s
= color_name
; *s
; ++s
)
3163 return h
%= XPM_COLOR_CACHE_BUCKETS
;
3167 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3168 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3171 static struct xpm_cached_color
*
3172 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3175 struct xpm_cached_color
*p
;
3178 bucket
= xpm_color_bucket (color_name
);
3180 nbytes
= sizeof *p
+ strlen (color_name
);
3181 p
= (struct xpm_cached_color
*) xmalloc (nbytes
);
3182 strcpy (p
->name
, color_name
);
3184 p
->next
= xpm_color_cache
[bucket
];
3185 xpm_color_cache
[bucket
] = p
;
3189 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3190 return the cached definition in *COLOR. Otherwise, make a new
3191 entry in the cache and allocate the color. Value is zero if color
3192 allocation failed. */
3195 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3197 struct xpm_cached_color
*p
;
3198 int h
= xpm_color_bucket (color_name
);
3200 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3201 if (strcmp (p
->name
, color_name
) == 0)
3206 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3209 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3211 p
= xpm_cache_color (f
, color_name
, color
, h
);
3213 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3214 with transparency, and it's useful. */
3215 else if (strcmp ("opaque", color_name
) == 0)
3217 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3218 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3219 p
= xpm_cache_color (f
, color_name
, color
, h
);
3226 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3227 CLOSURE is a pointer to the frame on which we allocate the
3228 color. Return in *COLOR the allocated color. Value is non-zero
3232 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3235 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3239 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3240 is a pointer to the frame on which we allocate the color. Value is
3241 non-zero if successful. */
3244 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3249 #endif /* ALLOC_XPM_COLORS */
3254 /* XPM library details. */
3256 DEF_IMGLIB_FN (XpmFreeAttributes
, (XpmAttributes
*));
3257 DEF_IMGLIB_FN (XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3258 xpm_XImage
**, XpmAttributes
*));
3259 DEF_IMGLIB_FN (XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3260 xpm_XImage
**, XpmAttributes
*));
3261 DEF_IMGLIB_FN (XImageFree
, (xpm_XImage
*));
3264 init_xpm_functions (Lisp_Object libraries
)
3268 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3271 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3272 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3273 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3274 LOAD_IMGLIB_FN (library
, XImageFree
);
3278 #endif /* HAVE_NTGUI */
3281 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3282 for XPM images. Such a list must consist of conses whose car and
3286 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3288 while (CONSP (color_symbols
))
3290 Lisp_Object sym
= XCAR (color_symbols
);
3292 || !STRINGP (XCAR (sym
))
3293 || !STRINGP (XCDR (sym
)))
3295 color_symbols
= XCDR (color_symbols
);
3298 return NILP (color_symbols
);
3302 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3305 xpm_image_p (Lisp_Object object
)
3307 struct image_keyword fmt
[XPM_LAST
];
3308 memcpy (fmt
, xpm_format
, sizeof fmt
);
3309 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3310 /* Either `:file' or `:data' must be present. */
3311 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3312 /* Either no `:color-symbols' or it's a list of conses
3313 whose car and cdr are strings. */
3314 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3315 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3318 #endif /* HAVE_XPM || HAVE_NS */
3320 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
3322 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3324 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3326 XpmAttributes attrs
;
3327 Pixmap bitmap
, mask
;
3329 memset (&attrs
, 0, sizeof attrs
);
3331 attrs
.visual
= FRAME_X_VISUAL (f
);
3332 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3333 attrs
.valuemask
|= XpmVisual
;
3334 attrs
.valuemask
|= XpmColormap
;
3336 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3337 (char **) bits
, &bitmap
, &mask
, &attrs
);
3338 if (rc
!= XpmSuccess
)
3340 XpmFreeAttributes (&attrs
);
3344 id
= x_allocate_bitmap_record (f
);
3345 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3346 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3347 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3348 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3349 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3350 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3351 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3352 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3354 XpmFreeAttributes (&attrs
);
3357 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3359 /* Load image IMG which will be displayed on frame F. Value is
3360 non-zero if successful. */
3365 xpm_load (struct frame
*f
, struct image
*img
)
3368 XpmAttributes attrs
;
3369 Lisp_Object specified_file
, color_symbols
;
3372 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3373 #endif /* HAVE_NTGUI */
3375 /* Configure the XPM lib. Use the visual of frame F. Allocate
3376 close colors. Return colors allocated. */
3377 memset (&attrs
, 0, sizeof attrs
);
3380 attrs
.visual
= FRAME_X_VISUAL (f
);
3381 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3382 attrs
.valuemask
|= XpmVisual
;
3383 attrs
.valuemask
|= XpmColormap
;
3384 #endif /* HAVE_NTGUI */
3386 #ifdef ALLOC_XPM_COLORS
3387 /* Allocate colors with our own functions which handle
3388 failing color allocation more gracefully. */
3389 attrs
.color_closure
= f
;
3390 attrs
.alloc_color
= xpm_alloc_color
;
3391 attrs
.free_colors
= xpm_free_colors
;
3392 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3393 #else /* not ALLOC_XPM_COLORS */
3394 /* Let the XPM lib allocate colors. */
3395 attrs
.valuemask
|= XpmReturnAllocPixels
;
3396 #ifdef XpmAllocCloseColors
3397 attrs
.alloc_close_colors
= 1;
3398 attrs
.valuemask
|= XpmAllocCloseColors
;
3399 #else /* not XpmAllocCloseColors */
3400 attrs
.closeness
= 600;
3401 attrs
.valuemask
|= XpmCloseness
;
3402 #endif /* not XpmAllocCloseColors */
3403 #endif /* ALLOC_XPM_COLORS */
3405 /* If image specification contains symbolic color definitions, add
3406 these to `attrs'. */
3407 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3408 if (CONSP (color_symbols
))
3411 XpmColorSymbol
*xpm_syms
;
3414 attrs
.valuemask
|= XpmColorSymbols
;
3416 /* Count number of symbols. */
3417 attrs
.numsymbols
= 0;
3418 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3421 /* Allocate an XpmColorSymbol array. */
3422 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3423 xpm_syms
= (XpmColorSymbol
*) alloca (size
);
3424 memset (xpm_syms
, 0, size
);
3425 attrs
.colorsymbols
= xpm_syms
;
3427 /* Fill the color symbol array. */
3428 for (tail
= color_symbols
, i
= 0;
3430 ++i
, tail
= XCDR (tail
))
3435 if (!CONSP (XCAR (tail
)))
3437 xpm_syms
[i
].name
= "";
3438 xpm_syms
[i
].value
= "";
3441 name
= XCAR (XCAR (tail
));
3442 color
= XCDR (XCAR (tail
));
3445 xpm_syms
[i
].name
= (char *) alloca (SCHARS (name
) + 1);
3446 strcpy (xpm_syms
[i
].name
, SDATA (name
));
3449 xpm_syms
[i
].name
= "";
3450 if (STRINGP (color
))
3452 xpm_syms
[i
].value
= (char *) alloca (SCHARS (color
) + 1);
3453 strcpy (xpm_syms
[i
].value
, SDATA (color
));
3456 xpm_syms
[i
].value
= "";
3460 /* Create a pixmap for the image, either from a file, or from a
3461 string buffer containing data in the same format as an XPM file. */
3462 #ifdef ALLOC_XPM_COLORS
3463 xpm_init_color_cache (f
, &attrs
);
3466 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3470 HDC frame_dc
= get_frame_dc (f
);
3471 hdc
= CreateCompatibleDC (frame_dc
);
3472 release_frame_dc (f
, frame_dc
);
3474 #endif /* HAVE_NTGUI */
3476 if (STRINGP (specified_file
))
3478 Lisp_Object file
= x_find_image_file (specified_file
);
3479 if (!STRINGP (file
))
3481 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3482 #ifdef ALLOC_XPM_COLORS
3483 xpm_free_color_cache ();
3489 /* XpmReadFileToPixmap is not available in the Windows port of
3490 libxpm. But XpmReadFileToImage almost does what we want. */
3491 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3492 &xpm_image
, &xpm_mask
,
3495 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3496 SDATA (file
), &img
->pixmap
, &img
->mask
,
3498 #endif /* HAVE_NTGUI */
3502 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3503 if (!STRINGP (buffer
))
3505 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3506 #ifdef ALLOC_XPM_COLORS
3507 xpm_free_color_cache ();
3512 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3513 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3514 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3515 &xpm_image
, &xpm_mask
,
3518 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3520 &img
->pixmap
, &img
->mask
,
3522 #endif /* HAVE_NTGUI */
3525 if (rc
== XpmSuccess
)
3527 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3528 img
->colors
= colors_in_color_table (&img
->ncolors
);
3529 #else /* not ALLOC_XPM_COLORS */
3533 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3534 plus some duplicate attributes. */
3535 if (xpm_image
&& xpm_image
->bitmap
)
3537 img
->pixmap
= xpm_image
->bitmap
;
3538 /* XImageFree in libXpm frees XImage struct without destroying
3539 the bitmap, which is what we want. */
3540 fn_XImageFree (xpm_image
);
3542 if (xpm_mask
&& xpm_mask
->bitmap
)
3544 /* The mask appears to be inverted compared with what we expect.
3545 TODO: invert our expectations. See other places where we
3546 have to invert bits because our idea of masks is backwards. */
3548 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3550 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3551 SelectObject (hdc
, old_obj
);
3553 img
->mask
= xpm_mask
->bitmap
;
3554 fn_XImageFree (xpm_mask
);
3559 #endif /* HAVE_NTGUI */
3561 /* Remember allocated colors. */
3562 img
->ncolors
= attrs
.nalloc_pixels
;
3563 img
->colors
= (unsigned long *) xmalloc (img
->ncolors
3564 * sizeof *img
->colors
);
3565 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3567 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3568 #ifdef DEBUG_X_COLORS
3569 register_color (img
->colors
[i
]);
3572 #endif /* not ALLOC_XPM_COLORS */
3574 img
->width
= attrs
.width
;
3575 img
->height
= attrs
.height
;
3576 xassert (img
->width
> 0 && img
->height
> 0);
3578 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3580 fn_XpmFreeAttributes (&attrs
);
3582 XpmFreeAttributes (&attrs
);
3583 #endif /* HAVE_NTGUI */
3589 #endif /* HAVE_NTGUI */
3594 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3597 case XpmFileInvalid
:
3598 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3602 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3605 case XpmColorFailed
:
3606 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3610 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3615 #ifdef ALLOC_XPM_COLORS
3616 xpm_free_color_cache ();
3618 return rc
== XpmSuccess
;
3621 #endif /* HAVE_XPM */
3623 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3625 /* XPM support functions for NS where libxpm is not available.
3626 Only XPM version 3 (without any extensions) is supported. */
3628 static int xpm_scan (const unsigned char **, const unsigned char *,
3629 const unsigned char **, int *);
3630 static Lisp_Object xpm_make_color_table_v
3631 (void (**) (Lisp_Object
, const unsigned char *, int, Lisp_Object
),
3632 Lisp_Object (**) (Lisp_Object
, const unsigned char *, int));
3633 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3635 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3636 const unsigned char *, int);
3637 static Lisp_Object xpm_make_color_table_h
3638 (void (**) (Lisp_Object
, const unsigned char *, int, Lisp_Object
),
3639 Lisp_Object (**) (Lisp_Object
, const unsigned char *, int));
3640 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3642 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3643 const unsigned char *, int);
3644 static int xpm_str_to_color_key (const char *);
3645 static int xpm_load_image (struct frame
*, struct image
*,
3646 const unsigned char *, const unsigned char *);
3648 /* Tokens returned from xpm_scan. */
3657 /* Scan an XPM data and return a character (< 256) or a token defined
3658 by enum xpm_token above. *S and END are the start (inclusive) and
3659 the end (exclusive) addresses of the data, respectively. Advance
3660 *S while scanning. If token is either XPM_TK_IDENT or
3661 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3662 length of the corresponding token, respectively. */
3665 xpm_scan (const unsigned char **s
,
3666 const unsigned char *end
,
3667 const unsigned char **beg
,
3674 /* Skip white-space. */
3675 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
3678 /* gnus-pointer.xpm uses '-' in its identifier.
3679 sb-dir-plus.xpm uses '+' in its identifier. */
3680 if (isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3684 && (c
= **s
, isalnum (c
) || c
== '_' || c
== '-' || c
== '+'))
3687 return XPM_TK_IDENT
;
3692 while (*s
< end
&& **s
!= '"')
3697 return XPM_TK_STRING
;
3701 if (*s
< end
&& **s
== '*')
3703 /* C-style comment. */
3707 while (*s
< end
&& *(*s
)++ != '*')
3710 while (*s
< end
&& **s
!= '/');
3724 /* Functions for color table lookup in XPM data. A key is a string
3725 specifying the color of each pixel in XPM data. A value is either
3726 an integer that specifies a pixel color, Qt that specifies
3727 transparency, or Qnil for the unspecified color. If the length of
3728 the key string is one, a vector is used as a table. Otherwise, a
3729 hash table is used. */
3732 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3733 const unsigned char *,
3736 Lisp_Object (**get_func
) (Lisp_Object
,
3737 const unsigned char *,
3740 *put_func
= xpm_put_color_table_v
;
3741 *get_func
= xpm_get_color_table_v
;
3742 return Fmake_vector (make_number (256), Qnil
);
3746 xpm_put_color_table_v (Lisp_Object color_table
,
3747 const unsigned char *chars_start
,
3751 XVECTOR (color_table
)->contents
[*chars_start
] = color
;
3755 xpm_get_color_table_v (Lisp_Object color_table
,
3756 const unsigned char *chars_start
,
3759 return XVECTOR (color_table
)->contents
[*chars_start
];
3763 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3764 const unsigned char *,
3767 Lisp_Object (**get_func
) (Lisp_Object
,
3768 const unsigned char *,
3771 *put_func
= xpm_put_color_table_h
;
3772 *get_func
= xpm_get_color_table_h
;
3773 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3774 make_float (DEFAULT_REHASH_SIZE
),
3775 make_float (DEFAULT_REHASH_THRESHOLD
),
3780 xpm_put_color_table_h (Lisp_Object color_table
,
3781 const unsigned char *chars_start
,
3785 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3787 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3789 hash_lookup (table
, chars
, &hash_code
);
3790 hash_put (table
, chars
, color
, hash_code
);
3794 xpm_get_color_table_h (Lisp_Object color_table
,
3795 const unsigned char *chars_start
,
3798 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3799 int i
= hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
),
3802 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3805 enum xpm_color_key
{
3813 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3816 xpm_str_to_color_key (const char *s
)
3821 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3823 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3829 xpm_load_image (struct frame
*f
,
3831 const unsigned char *contents
,
3832 const unsigned char *end
)
3834 const unsigned char *s
= contents
, *beg
, *str
;
3835 unsigned char buffer
[BUFSIZ
];
3836 int width
, height
, x
, y
;
3837 int num_colors
, chars_per_pixel
;
3839 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3840 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3841 Lisp_Object frame
, color_symbols
, color_table
;
3842 int best_key
, have_mask
= 0;
3843 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3846 LA1 = xpm_scan (&s, end, &beg, &len)
3848 #define expect(TOKEN) \
3849 if (LA1 != (TOKEN)) \
3854 #define expect_ident(IDENT) \
3855 if (LA1 == XPM_TK_IDENT \
3856 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3861 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3865 expect_ident ("static");
3866 expect_ident ("char");
3868 expect (XPM_TK_IDENT
);
3873 expect (XPM_TK_STRING
);
3876 memcpy (buffer
, beg
, len
);
3878 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3879 &num_colors
, &chars_per_pixel
) != 4
3880 || width
<= 0 || height
<= 0
3881 || num_colors
<= 0 || chars_per_pixel
<= 0)
3884 if (!check_image_size (f
, width
, height
))
3886 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3892 XSETFRAME (frame
, f
);
3893 if (!NILP (Fxw_display_color_p (frame
)))
3894 best_key
= XPM_COLOR_KEY_C
;
3895 else if (!NILP (Fx_display_grayscale_p (frame
)))
3896 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3897 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3899 best_key
= XPM_COLOR_KEY_M
;
3901 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3902 if (chars_per_pixel
== 1)
3903 color_table
= xpm_make_color_table_v (&put_color_table
,
3906 color_table
= xpm_make_color_table_h (&put_color_table
,
3909 while (num_colors
-- > 0)
3911 unsigned char *color
, *max_color
;
3912 int key
, next_key
, max_key
= 0;
3913 Lisp_Object symbol_color
= Qnil
, color_val
;
3916 expect (XPM_TK_STRING
);
3917 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3919 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3920 buffer
[len
- chars_per_pixel
] = '\0';
3922 str
= strtok (buffer
, " \t");
3925 key
= xpm_str_to_color_key (str
);
3930 color
= strtok (NULL
, " \t");
3934 while ((str
= strtok (NULL
, " \t")) != NULL
)
3936 next_key
= xpm_str_to_color_key (str
);
3939 color
[strlen (color
)] = ' ';
3942 if (key
== XPM_COLOR_KEY_S
)
3944 if (NILP (symbol_color
))
3945 symbol_color
= build_string (color
);
3947 else if (max_key
< key
&& key
<= best_key
)
3957 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3959 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3961 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3963 if (xstrcasecmp (SDATA (XCDR (specified_color
)), "None") == 0)
3965 else if (x_defined_color (f
, SDATA (XCDR (specified_color
)),
3967 color_val
= make_number (cdef
.pixel
);
3970 if (NILP (color_val
) && max_key
> 0)
3972 if (xstrcasecmp (max_color
, "None") == 0)
3974 else if (x_defined_color (f
, max_color
, &cdef
, 0))
3975 color_val
= make_number (cdef
.pixel
);
3977 if (!NILP (color_val
))
3978 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
3983 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3984 &ximg
, &img
->pixmap
)
3986 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3987 &mask_img
, &img
->mask
)
3991 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3995 for (y
= 0; y
< height
; y
++)
3997 expect (XPM_TK_STRING
);
3999 if (len
< width
* chars_per_pixel
)
4001 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
4003 Lisp_Object color_val
=
4004 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4006 XPutPixel (ximg
, x
, y
,
4007 (INTEGERP (color_val
) ? XINT (color_val
)
4008 : FRAME_FOREGROUND_PIXEL (f
)));
4010 XPutPixel (mask_img
, x
, y
,
4011 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4012 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4014 if (EQ (color_val
, Qt
))
4015 ns_set_alpha (ximg
, x
, y
, 0);
4023 img
->height
= height
;
4025 /* Maybe fill in the background field while we have ximg handy. */
4026 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4027 IMAGE_BACKGROUND (img
, f
, ximg
);
4029 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4030 x_destroy_x_image (ximg
);
4034 /* Fill in the background_transparent field while we have the
4036 image_background_transparent (img
, f
, mask_img
);
4038 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4039 x_destroy_x_image (mask_img
);
4043 x_destroy_x_image (mask_img
);
4044 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4045 img
->mask
= NO_PIXMAP
;
4051 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4053 x_destroy_x_image (ximg
);
4054 x_destroy_x_image (mask_img
);
4055 x_clear_image (f
, img
);
4064 xpm_load (struct frame
*f
,
4068 Lisp_Object file_name
;
4070 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4071 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4072 if (STRINGP (file_name
))
4075 unsigned char *contents
;
4078 file
= x_find_image_file (file_name
);
4079 if (!STRINGP (file
))
4081 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4085 contents
= slurp_file (SDATA (file
), &size
);
4086 if (contents
== NULL
)
4088 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4092 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4099 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4100 if (!STRINGP (data
))
4102 image_error ("Invalid image data `%s'", data
, Qnil
);
4105 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4106 SDATA (data
) + SBYTES (data
));
4112 #endif /* HAVE_NS && !HAVE_XPM */
4116 /***********************************************************************
4118 ***********************************************************************/
4120 #ifdef COLOR_TABLE_SUPPORT
4122 /* An entry in the color table mapping an RGB color to a pixel color. */
4127 unsigned long pixel
;
4129 /* Next in color table collision list. */
4130 struct ct_color
*next
;
4133 /* The bucket vector size to use. Must be prime. */
4137 /* Value is a hash of the RGB color given by R, G, and B. */
4139 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4141 /* The color hash table. */
4143 struct ct_color
**ct_table
;
4145 /* Number of entries in the color table. */
4147 int ct_colors_allocated
;
4149 /* Initialize the color table. */
4152 init_color_table (void)
4154 int size
= CT_SIZE
* sizeof (*ct_table
);
4155 ct_table
= (struct ct_color
**) xmalloc (size
);
4156 memset (ct_table
, 0, size
);
4157 ct_colors_allocated
= 0;
4161 /* Free memory associated with the color table. */
4164 free_color_table (void)
4167 struct ct_color
*p
, *next
;
4169 for (i
= 0; i
< CT_SIZE
; ++i
)
4170 for (p
= ct_table
[i
]; p
; p
= next
)
4181 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4182 entry for that color already is in the color table, return the
4183 pixel color of that entry. Otherwise, allocate a new color for R,
4184 G, B, and make an entry in the color table. */
4186 static unsigned long
4187 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4189 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4190 int i
= hash
% CT_SIZE
;
4192 Display_Info
*dpyinfo
;
4194 /* Handle TrueColor visuals specially, which improves performance by
4195 two orders of magnitude. Freeing colors on TrueColor visuals is
4196 a nop, and pixel colors specify RGB values directly. See also
4197 the Xlib spec, chapter 3.1. */
4198 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4199 if (dpyinfo
->red_bits
> 0)
4201 unsigned long pr
, pg
, pb
;
4203 /* Apply gamma-correction like normal color allocation does. */
4207 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4208 gamma_correct (f
, &color
);
4209 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4212 /* Scale down RGB values to the visual's bits per RGB, and shift
4213 them to the right position in the pixel color. Note that the
4214 original RGB values are 16-bit values, as usual in X. */
4215 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4216 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4217 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4219 /* Assemble the pixel color. */
4220 return pr
| pg
| pb
;
4223 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4224 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4230 #ifdef HAVE_X_WINDOWS
4239 cmap
= FRAME_X_COLORMAP (f
);
4240 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4243 ++ct_colors_allocated
;
4244 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4248 p
->pixel
= color
.pixel
;
4249 p
->next
= ct_table
[i
];
4253 return FRAME_FOREGROUND_PIXEL (f
);
4258 color
= PALETTERGB (r
, g
, b
);
4260 color
= RGB_TO_ULONG (r
, g
, b
);
4261 #endif /* HAVE_NTGUI */
4262 ++ct_colors_allocated
;
4263 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4268 p
->next
= ct_table
[i
];
4270 #endif /* HAVE_X_WINDOWS */
4278 /* Look up pixel color PIXEL which is used on frame F in the color
4279 table. If not already present, allocate it. Value is PIXEL. */
4281 static unsigned long
4282 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4284 int i
= pixel
% CT_SIZE
;
4287 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4288 if (p
->pixel
== pixel
)
4297 #ifdef HAVE_X_WINDOWS
4298 cmap
= FRAME_X_COLORMAP (f
);
4299 color
.pixel
= pixel
;
4300 x_query_color (f
, &color
);
4301 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4304 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4305 color
.pixel
= pixel
;
4306 XQueryColor (NULL
, cmap
, &color
);
4307 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4309 #endif /* HAVE_X_WINDOWS */
4313 ++ct_colors_allocated
;
4315 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4320 p
->next
= ct_table
[i
];
4324 return FRAME_FOREGROUND_PIXEL (f
);
4330 /* Value is a vector of all pixel colors contained in the color table,
4331 allocated via xmalloc. Set *N to the number of colors. */
4333 static unsigned long *
4334 colors_in_color_table (int *n
)
4338 unsigned long *colors
;
4340 if (ct_colors_allocated
== 0)
4347 colors
= (unsigned long *) xmalloc (ct_colors_allocated
4349 *n
= ct_colors_allocated
;
4351 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4352 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4353 colors
[j
++] = p
->pixel
;
4359 #else /* COLOR_TABLE_SUPPORT */
4361 static unsigned long
4362 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4364 unsigned long pixel
;
4367 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4368 #endif /* HAVE_NTGUI */
4371 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4372 #endif /* HAVE_NS */
4377 init_color_table (void)
4380 #endif /* COLOR_TABLE_SUPPORT */
4383 /***********************************************************************
4385 ***********************************************************************/
4387 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4388 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4389 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4392 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4393 #endif /* HAVE_NTGUI */
4395 /* Edge detection matrices for different edge-detection
4398 static int emboss_matrix
[9] = {
4400 2, -1, 0, /* y - 1 */
4402 0, 1, -2 /* y + 1 */
4405 static int laplace_matrix
[9] = {
4407 1, 0, 0, /* y - 1 */
4409 0, 0, -1 /* y + 1 */
4412 /* Value is the intensity of the color whose red/green/blue values
4415 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4418 /* On frame F, return an array of XColor structures describing image
4419 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4420 non-zero means also fill the red/green/blue members of the XColor
4421 structures. Value is a pointer to the array of XColors structures,
4422 allocated with xmalloc; it must be freed by the caller. */
4425 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4429 XImagePtr_or_DC ximg
;
4433 #endif /* HAVE_NTGUI */
4435 colors
= (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *colors
);
4438 /* Get the X image IMG->pixmap. */
4439 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4440 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4442 /* Load the image into a memory device context. */
4443 hdc
= get_frame_dc (f
);
4444 ximg
= CreateCompatibleDC (hdc
);
4445 release_frame_dc (f
, hdc
);
4446 prev
= SelectObject (ximg
, img
->pixmap
);
4447 #endif /* HAVE_NTGUI */
4449 /* Fill the `pixel' members of the XColor array. I wished there
4450 were an easy and portable way to circumvent XGetPixel. */
4452 for (y
= 0; y
< img
->height
; ++y
)
4456 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4457 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4458 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4460 x_query_colors (f
, row
, img
->width
);
4464 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4466 /* W32_TODO: palette support needed here? */
4467 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4470 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4471 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4472 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4475 #endif /* HAVE_X_WINDOWS */
4478 Destroy_Image (ximg
, prev
);
4485 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4486 created with CreateDIBSection, with the pointer to the bit values
4487 stored in ximg->data. */
4490 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4492 int width
= ximg
->info
.bmiHeader
.biWidth
;
4493 int height
= ximg
->info
.bmiHeader
.biHeight
;
4494 unsigned char * pixel
;
4496 /* True color images. */
4497 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4499 int rowbytes
= width
* 3;
4500 /* Ensure scanlines are aligned on 4 byte boundaries. */
4502 rowbytes
+= 4 - (rowbytes
% 4);
4504 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4505 /* Windows bitmaps are in BGR order. */
4506 *pixel
= GetBValue (color
);
4507 *(pixel
+ 1) = GetGValue (color
);
4508 *(pixel
+ 2) = GetRValue (color
);
4510 /* Monochrome images. */
4511 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4513 int rowbytes
= width
/ 8;
4514 /* Ensure scanlines are aligned on 4 byte boundaries. */
4516 rowbytes
+= 4 - (rowbytes
% 4);
4517 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4518 /* Filter out palette info. */
4519 if (color
& 0x00ffffff)
4520 *pixel
= *pixel
| (1 << x
% 8);
4522 *pixel
= *pixel
& ~(1 << x
% 8);
4525 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4528 #endif /* HAVE_NTGUI */
4530 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4531 RGB members are set. F is the frame on which this all happens.
4532 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4535 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4538 XImagePtr oimg
= NULL
;
4542 init_color_table ();
4544 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4547 for (y
= 0; y
< img
->height
; ++y
)
4548 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4550 unsigned long pixel
;
4551 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4552 XPutPixel (oimg
, x
, y
, pixel
);
4556 x_clear_image_1 (f
, img
, 1, 0, 1);
4558 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4559 x_destroy_x_image (oimg
);
4560 img
->pixmap
= pixmap
;
4561 #ifdef COLOR_TABLE_SUPPORT
4562 img
->colors
= colors_in_color_table (&img
->ncolors
);
4563 free_color_table ();
4564 #endif /* COLOR_TABLE_SUPPORT */
4568 /* On frame F, perform edge-detection on image IMG.
4570 MATRIX is a nine-element array specifying the transformation
4571 matrix. See emboss_matrix for an example.
4573 COLOR_ADJUST is a color adjustment added to each pixel of the
4577 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4579 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4583 for (i
= sum
= 0; i
< 9; ++i
)
4584 sum
+= eabs (matrix
[i
]);
4586 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4588 new = (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *new);
4590 for (y
= 0; y
< img
->height
; ++y
)
4592 p
= COLOR (new, 0, y
);
4593 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4594 p
= COLOR (new, img
->width
- 1, y
);
4595 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4598 for (x
= 1; x
< img
->width
- 1; ++x
)
4600 p
= COLOR (new, x
, 0);
4601 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4602 p
= COLOR (new, x
, img
->height
- 1);
4603 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4606 for (y
= 1; y
< img
->height
- 1; ++y
)
4608 p
= COLOR (new, 1, y
);
4610 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4612 int r
, g
, b
, y1
, x1
;
4615 for (y1
= y
- 1; y1
< y
+ 2; ++y1
)
4616 for (x1
= x
- 1; x1
< x
+ 2; ++x1
, ++i
)
4619 XColor
*t
= COLOR (colors
, x1
, y1
);
4620 r
+= matrix
[i
] * t
->red
;
4621 g
+= matrix
[i
] * t
->green
;
4622 b
+= matrix
[i
] * t
->blue
;
4625 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4626 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4627 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4628 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4633 x_from_xcolors (f
, img
, new);
4639 /* Perform the pre-defined `emboss' edge-detection on image IMG
4643 x_emboss (struct frame
*f
, struct image
*img
)
4645 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4649 /* Transform image IMG which is used on frame F with a Laplace
4650 edge-detection algorithm. The result is an image that can be used
4651 to draw disabled buttons, for example. */
4654 x_laplace (struct frame
*f
, struct image
*img
)
4656 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4660 /* Perform edge-detection on image IMG on frame F, with specified
4661 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4663 MATRIX must be either
4665 - a list of at least 9 numbers in row-major form
4666 - a vector of at least 9 numbers
4668 COLOR_ADJUST nil means use a default; otherwise it must be a
4672 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4673 Lisp_Object color_adjust
)
4681 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4682 ++i
, matrix
= XCDR (matrix
))
4683 trans
[i
] = XFLOATINT (XCAR (matrix
));
4685 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4687 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4688 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4691 if (NILP (color_adjust
))
4692 color_adjust
= make_number (0xffff / 2);
4694 if (i
== 9 && NUMBERP (color_adjust
))
4695 x_detect_edges (f
, img
, trans
, (int) XFLOATINT (color_adjust
));
4699 /* Transform image IMG on frame F so that it looks disabled. */
4702 x_disable_image (struct frame
*f
, struct image
*img
)
4704 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4706 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4708 int n_planes
= dpyinfo
->n_planes
;
4709 #endif /* HAVE_NTGUI */
4713 /* Color (or grayscale). Convert to gray, and equalize. Just
4714 drawing such images with a stipple can look very odd, so
4715 we're using this method instead. */
4716 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4718 const int h
= 15000;
4719 const int l
= 30000;
4721 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4725 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4726 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4727 p
->red
= p
->green
= p
->blue
= i2
;
4730 x_from_xcolors (f
, img
, colors
);
4733 /* Draw a cross over the disabled image, if we must or if we
4735 if (n_planes
< 2 || cross_disabled_images
)
4738 Display
*dpy
= FRAME_X_DISPLAY (f
);
4741 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4743 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4745 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4746 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4747 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4748 img
->width
- 1, img
->height
- 1);
4749 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4755 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4756 XSetForeground (dpy
, gc
, MaskForeground (f
));
4757 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4758 img
->width
- 1, img
->height
- 1);
4759 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4763 #endif /* !HAVE_NS */
4768 hdc
= get_frame_dc (f
);
4769 bmpdc
= CreateCompatibleDC (hdc
);
4770 release_frame_dc (f
, hdc
);
4772 prev
= SelectObject (bmpdc
, img
->pixmap
);
4774 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4775 MoveToEx (bmpdc
, 0, 0, NULL
);
4776 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4777 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4778 LineTo (bmpdc
, img
->width
- 1, 0);
4782 SelectObject (bmpdc
, img
->mask
);
4783 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4784 MoveToEx (bmpdc
, 0, 0, NULL
);
4785 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4786 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4787 LineTo (bmpdc
, img
->width
- 1, 0);
4789 SelectObject (bmpdc
, prev
);
4791 #endif /* HAVE_NTGUI */
4796 /* Build a mask for image IMG which is used on frame F. FILE is the
4797 name of an image file, for error messages. HOW determines how to
4798 determine the background color of IMG. If it is a list '(R G B)',
4799 with R, G, and B being integers >= 0, take that as the color of the
4800 background. Otherwise, determine the background color of IMG
4801 heuristically. Value is non-zero if successful. */
4804 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4806 XImagePtr_or_DC ximg
;
4814 #endif /* HAVE_NTGUI */
4815 int x
, y
, rc
, use_img_background
;
4816 unsigned long bg
= 0;
4820 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4821 img
->mask
= NO_PIXMAP
;
4822 img
->background_transparent_valid
= 0;
4827 /* Create an image and pixmap serving as mask. */
4828 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4829 &mask_img
, &img
->mask
);
4832 #endif /* !HAVE_NS */
4834 /* Get the X image of IMG->pixmap. */
4835 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4836 img
->width
, img
->height
,
4839 /* Create the bit array serving as mask. */
4840 row_width
= (img
->width
+ 7) / 8;
4841 mask_img
= xmalloc (row_width
* img
->height
);
4842 memset (mask_img
, 0, row_width
* img
->height
);
4844 /* Create a memory device context for IMG->pixmap. */
4845 frame_dc
= get_frame_dc (f
);
4846 ximg
= CreateCompatibleDC (frame_dc
);
4847 release_frame_dc (f
, frame_dc
);
4848 prev
= SelectObject (ximg
, img
->pixmap
);
4849 #endif /* HAVE_NTGUI */
4851 /* Determine the background color of ximg. If HOW is `(R G B)'
4852 take that as color. Otherwise, use the image's background color. */
4853 use_img_background
= 1;
4859 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4861 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4865 if (i
== 3 && NILP (how
))
4867 char color_name
[30];
4868 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4871 0x00ffffff & /* Filter out palette info. */
4872 #endif /* HAVE_NTGUI */
4873 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4874 use_img_background
= 0;
4878 if (use_img_background
)
4879 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4881 /* Set all bits in mask_img to 1 whose color in ximg is different
4882 from the background color bg. */
4884 for (y
= 0; y
< img
->height
; ++y
)
4885 for (x
= 0; x
< img
->width
; ++x
)
4887 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4888 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4890 if (XGetPixel (ximg
, x
, y
) == bg
)
4891 ns_set_alpha (ximg
, x
, y
, 0);
4892 #endif /* HAVE_NS */
4894 /* Fill in the background_transparent field while we have the mask handy. */
4895 image_background_transparent (img
, f
, mask_img
);
4897 /* Put mask_img into img->mask. */
4898 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4899 x_destroy_x_image (mask_img
);
4900 #endif /* !HAVE_NS */
4902 for (y
= 0; y
< img
->height
; ++y
)
4903 for (x
= 0; x
< img
->width
; ++x
)
4905 COLORREF p
= GetPixel (ximg
, x
, y
);
4907 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4910 /* Create the mask image. */
4911 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4913 /* Fill in the background_transparent field while we have the mask handy. */
4914 SelectObject (ximg
, img
->mask
);
4915 image_background_transparent (img
, f
, ximg
);
4917 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4919 #endif /* HAVE_NTGUI */
4921 Destroy_Image (ximg
, prev
);
4927 /***********************************************************************
4928 PBM (mono, gray, color)
4929 ***********************************************************************/
4931 static int pbm_image_p (Lisp_Object object
);
4932 static int pbm_load (struct frame
*f
, struct image
*img
);
4933 static int pbm_scan_number (unsigned char **, unsigned char *);
4935 /* The symbol `pbm' identifying images of this type. */
4939 /* Indices of image specification fields in gs_format, below. */
4941 enum pbm_keyword_index
4957 /* Vector of image_keyword structures describing the format
4958 of valid user-defined image specifications. */
4960 static const struct image_keyword pbm_format
[PBM_LAST
] =
4962 {":type", IMAGE_SYMBOL_VALUE
, 1},
4963 {":file", IMAGE_STRING_VALUE
, 0},
4964 {":data", IMAGE_STRING_VALUE
, 0},
4965 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4966 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
4967 {":relief", IMAGE_INTEGER_VALUE
, 0},
4968 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4969 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4970 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4971 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4972 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4975 /* Structure describing the image type `pbm'. */
4977 static struct image_type pbm_type
=
4987 /* Return non-zero if OBJECT is a valid PBM image specification. */
4990 pbm_image_p (Lisp_Object object
)
4992 struct image_keyword fmt
[PBM_LAST
];
4994 memcpy (fmt
, pbm_format
, sizeof fmt
);
4996 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
4999 /* Must specify either :data or :file. */
5000 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5004 /* Scan a decimal number from *S and return it. Advance *S while
5005 reading the number. END is the end of the string. Value is -1 at
5009 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5011 int c
= 0, val
= -1;
5015 /* Skip white-space. */
5016 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
5021 /* Skip comment to end of line. */
5022 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5025 else if (isdigit (c
))
5027 /* Read decimal number. */
5029 while (*s
< end
&& (c
= *(*s
)++, isdigit (c
)))
5030 val
= 10 * val
+ c
- '0';
5042 #if 0 /* Unused. ++kfs */
5044 /* Read FILE into memory. Value is a pointer to a buffer allocated
5045 with xmalloc holding FILE's contents. Value is null if an error
5046 occurred. *SIZE is set to the size of the file. */
5049 pbm_read_file (file
, size
)
5057 if (stat (SDATA (file
), &st
) == 0
5058 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5059 && (buf
= (char *) xmalloc (st
.st_size
),
5060 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5079 #endif /* HAVE_NTGUI */
5081 /* Load PBM image IMG for use on frame F. */
5084 pbm_load (struct frame
*f
, struct image
*img
)
5087 int width
, height
, max_color_idx
= 0;
5089 Lisp_Object file
, specified_file
;
5090 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5091 unsigned char *contents
= NULL
;
5092 unsigned char *end
, *p
;
5095 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5097 if (STRINGP (specified_file
))
5099 file
= x_find_image_file (specified_file
);
5100 if (!STRINGP (file
))
5102 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5106 contents
= slurp_file (SDATA (file
), &size
);
5107 if (contents
== NULL
)
5109 image_error ("Error reading `%s'", file
, Qnil
);
5114 end
= contents
+ size
;
5119 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5120 if (!STRINGP (data
))
5122 image_error ("Invalid image data `%s'", data
, Qnil
);
5126 end
= p
+ SBYTES (data
);
5129 /* Check magic number. */
5130 if (end
- p
< 2 || *p
++ != 'P')
5132 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5141 raw_p
= 0, type
= PBM_MONO
;
5145 raw_p
= 0, type
= PBM_GRAY
;
5149 raw_p
= 0, type
= PBM_COLOR
;
5153 raw_p
= 1, type
= PBM_MONO
;
5157 raw_p
= 1, type
= PBM_GRAY
;
5161 raw_p
= 1, type
= PBM_COLOR
;
5165 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5169 /* Read width, height, maximum color-component. Characters
5170 starting with `#' up to the end of a line are ignored. */
5171 width
= pbm_scan_number (&p
, end
);
5172 height
= pbm_scan_number (&p
, end
);
5174 if (type
!= PBM_MONO
)
5176 max_color_idx
= pbm_scan_number (&p
, end
);
5177 if (max_color_idx
> 65535 || max_color_idx
< 0)
5179 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5184 if (!check_image_size (f
, width
, height
))
5186 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5190 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5191 &ximg
, &img
->pixmap
))
5194 /* Initialize the color hash table. */
5195 init_color_table ();
5197 if (type
== PBM_MONO
)
5200 struct image_keyword fmt
[PBM_LAST
];
5201 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5202 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5204 /* Parse the image specification. */
5205 memcpy (fmt
, pbm_format
, sizeof fmt
);
5206 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5208 /* Get foreground and background colors, maybe allocate colors. */
5209 if (fmt
[PBM_FOREGROUND
].count
5210 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5211 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5212 if (fmt
[PBM_BACKGROUND
].count
5213 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5215 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5216 img
->background
= bg
;
5217 img
->background_valid
= 1;
5220 for (y
= 0; y
< height
; ++y
)
5221 for (x
= 0; x
< width
; ++x
)
5229 x_destroy_x_image (ximg
);
5230 x_clear_image (f
, img
);
5231 image_error ("Invalid image size in image `%s'",
5241 g
= pbm_scan_number (&p
, end
);
5243 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5248 int expected_size
= height
* width
;
5249 if (max_color_idx
> 255)
5251 if (type
== PBM_COLOR
)
5254 if (raw_p
&& p
+ expected_size
> end
)
5256 x_destroy_x_image (ximg
);
5257 x_clear_image (f
, img
);
5258 image_error ("Invalid image size in image `%s'",
5263 for (y
= 0; y
< height
; ++y
)
5264 for (x
= 0; x
< width
; ++x
)
5268 if (type
== PBM_GRAY
&& raw_p
)
5271 if (max_color_idx
> 255)
5272 r
= g
= b
= r
* 256 + *p
++;
5274 else if (type
== PBM_GRAY
)
5275 r
= g
= b
= pbm_scan_number (&p
, end
);
5279 if (max_color_idx
> 255)
5282 if (max_color_idx
> 255)
5285 if (max_color_idx
> 255)
5290 r
= pbm_scan_number (&p
, end
);
5291 g
= pbm_scan_number (&p
, end
);
5292 b
= pbm_scan_number (&p
, end
);
5295 if (r
< 0 || g
< 0 || b
< 0)
5297 x_destroy_x_image (ximg
);
5298 image_error ("Invalid pixel value in image `%s'",
5303 /* RGB values are now in the range 0..max_color_idx.
5304 Scale this to the range 0..0xffff supported by X. */
5305 r
= (double) r
* 65535 / max_color_idx
;
5306 g
= (double) g
* 65535 / max_color_idx
;
5307 b
= (double) b
* 65535 / max_color_idx
;
5308 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5312 #ifdef COLOR_TABLE_SUPPORT
5313 /* Store in IMG->colors the colors allocated for the image, and
5314 free the color table. */
5315 img
->colors
= colors_in_color_table (&img
->ncolors
);
5316 free_color_table ();
5317 #endif /* COLOR_TABLE_SUPPORT */
5320 img
->height
= height
;
5322 /* Maybe fill in the background field while we have ximg handy. */
5324 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5325 /* Casting avoids a GCC warning. */
5326 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5328 /* Put the image into a pixmap. */
5329 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5330 x_destroy_x_image (ximg
);
5332 /* X and W32 versions did it here, MAC version above. ++kfs
5334 img->height = height; */
5341 /***********************************************************************
5343 ***********************************************************************/
5345 #if defined (HAVE_PNG) || defined (HAVE_NS)
5347 /* Function prototypes. */
5349 static int png_image_p (Lisp_Object object
);
5350 static int png_load (struct frame
*f
, struct image
*img
);
5352 /* The symbol `png' identifying images of this type. */
5356 /* Indices of image specification fields in png_format, below. */
5358 enum png_keyword_index
5373 /* Vector of image_keyword structures describing the format
5374 of valid user-defined image specifications. */
5376 static const struct image_keyword png_format
[PNG_LAST
] =
5378 {":type", IMAGE_SYMBOL_VALUE
, 1},
5379 {":data", IMAGE_STRING_VALUE
, 0},
5380 {":file", IMAGE_STRING_VALUE
, 0},
5381 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5382 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5383 {":relief", IMAGE_INTEGER_VALUE
, 0},
5384 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5385 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5386 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5387 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5390 /* Structure describing the image type `png'. */
5392 static struct image_type png_type
=
5401 /* Return non-zero if OBJECT is a valid PNG image specification. */
5404 png_image_p (Lisp_Object object
)
5406 struct image_keyword fmt
[PNG_LAST
];
5407 memcpy (fmt
, png_format
, sizeof fmt
);
5409 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5412 /* Must specify either the :data or :file keyword. */
5413 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5416 #endif /* HAVE_PNG || HAVE_NS */
5422 /* PNG library details. */
5424 DEF_IMGLIB_FN (png_get_io_ptr
, (png_structp
));
5425 DEF_IMGLIB_FN (png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5426 DEF_IMGLIB_FN (png_create_read_struct
, (png_const_charp
, png_voidp
,
5427 png_error_ptr
, png_error_ptr
));
5428 DEF_IMGLIB_FN (png_create_info_struct
, (png_structp
));
5429 DEF_IMGLIB_FN (png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5430 DEF_IMGLIB_FN (png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5431 DEF_IMGLIB_FN (png_set_sig_bytes
, (png_structp
, int));
5432 DEF_IMGLIB_FN (png_read_info
, (png_structp
, png_infop
));
5433 DEF_IMGLIB_FN (png_get_IHDR
, (png_structp
, png_infop
,
5434 png_uint_32
*, png_uint_32
*,
5435 int *, int *, int *, int *, int *));
5436 DEF_IMGLIB_FN (png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5437 DEF_IMGLIB_FN (png_set_strip_16
, (png_structp
));
5438 DEF_IMGLIB_FN (png_set_expand
, (png_structp
));
5439 DEF_IMGLIB_FN (png_set_gray_to_rgb
, (png_structp
));
5440 DEF_IMGLIB_FN (png_set_background
, (png_structp
, png_color_16p
,
5442 DEF_IMGLIB_FN (png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5443 DEF_IMGLIB_FN (png_read_update_info
, (png_structp
, png_infop
));
5444 DEF_IMGLIB_FN (png_get_channels
, (png_structp
, png_infop
));
5445 DEF_IMGLIB_FN (png_get_rowbytes
, (png_structp
, png_infop
));
5446 DEF_IMGLIB_FN (png_read_image
, (png_structp
, png_bytepp
));
5447 DEF_IMGLIB_FN (png_read_end
, (png_structp
, png_infop
));
5448 DEF_IMGLIB_FN (png_error
, (png_structp
, png_const_charp
));
5451 init_png_functions (Lisp_Object libraries
)
5455 /* Try loading libpng under probable names. */
5456 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5459 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5460 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5461 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5462 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5463 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5464 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5465 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5466 LOAD_IMGLIB_FN (library
, png_read_info
);
5467 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5468 LOAD_IMGLIB_FN (library
, png_get_valid
);
5469 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5470 LOAD_IMGLIB_FN (library
, png_set_expand
);
5471 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5472 LOAD_IMGLIB_FN (library
, png_set_background
);
5473 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5474 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5475 LOAD_IMGLIB_FN (library
, png_get_channels
);
5476 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5477 LOAD_IMGLIB_FN (library
, png_read_image
);
5478 LOAD_IMGLIB_FN (library
, png_read_end
);
5479 LOAD_IMGLIB_FN (library
, png_error
);
5484 #define fn_png_get_io_ptr png_get_io_ptr
5485 #define fn_png_sig_cmp png_sig_cmp
5486 #define fn_png_create_read_struct png_create_read_struct
5487 #define fn_png_create_info_struct png_create_info_struct
5488 #define fn_png_destroy_read_struct png_destroy_read_struct
5489 #define fn_png_set_read_fn png_set_read_fn
5490 #define fn_png_set_sig_bytes png_set_sig_bytes
5491 #define fn_png_read_info png_read_info
5492 #define fn_png_get_IHDR png_get_IHDR
5493 #define fn_png_get_valid png_get_valid
5494 #define fn_png_set_strip_16 png_set_strip_16
5495 #define fn_png_set_expand png_set_expand
5496 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5497 #define fn_png_set_background png_set_background
5498 #define fn_png_get_bKGD png_get_bKGD
5499 #define fn_png_read_update_info png_read_update_info
5500 #define fn_png_get_channels png_get_channels
5501 #define fn_png_get_rowbytes png_get_rowbytes
5502 #define fn_png_read_image png_read_image
5503 #define fn_png_read_end png_read_end
5504 #define fn_png_error png_error
5506 #endif /* HAVE_NTGUI */
5508 /* Error and warning handlers installed when the PNG library
5512 my_png_error (png_struct
*png_ptr
, const char *msg
)
5514 xassert (png_ptr
!= NULL
);
5515 /* Avoid compiler warning about deprecated direct access to
5516 png_ptr's fields in libpng versions 1.4.x. */
5517 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5518 longjmp (png_ptr
->jmpbuf
, 1);
5523 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5525 xassert (png_ptr
!= NULL
);
5526 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5529 /* Memory source for PNG decoding. */
5531 struct png_memory_storage
5533 unsigned char *bytes
; /* The data */
5534 size_t len
; /* How big is it? */
5535 int index
; /* Where are we? */
5539 /* Function set as reader function when reading PNG image from memory.
5540 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5541 bytes from the input to DATA. */
5544 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5546 struct png_memory_storage
*tbr
5547 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5549 if (length
> tbr
->len
- tbr
->index
)
5550 fn_png_error (png_ptr
, "Read error");
5552 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5553 tbr
->index
= tbr
->index
+ length
;
5557 /* Function set as reader function when reading PNG image from a file.
5558 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5559 bytes from the input to DATA. */
5562 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5564 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5566 if (fread (data
, 1, length
, fp
) < length
)
5567 fn_png_error (png_ptr
, "Read error");
5571 /* Load PNG image IMG for use on frame F. Value is non-zero if
5575 png_load (struct frame
*f
, struct image
*img
)
5577 Lisp_Object file
, specified_file
;
5578 Lisp_Object specified_data
;
5580 XImagePtr ximg
, mask_img
= NULL
;
5581 png_struct
*png_ptr
= NULL
;
5582 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5583 FILE *volatile fp
= NULL
;
5585 png_byte
* volatile pixels
= NULL
;
5586 png_byte
** volatile rows
= NULL
;
5587 png_uint_32 width
, height
;
5588 int bit_depth
, color_type
, interlace_type
;
5590 png_uint_32 row_bytes
;
5592 struct png_memory_storage tbr
; /* Data to be read */
5594 /* Find out what file to load. */
5595 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5596 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5598 if (NILP (specified_data
))
5600 file
= x_find_image_file (specified_file
);
5601 if (!STRINGP (file
))
5603 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5607 /* Open the image file. */
5608 fp
= fopen (SDATA (file
), "rb");
5611 image_error ("Cannot open image file `%s'", file
, Qnil
);
5615 /* Check PNG signature. */
5616 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5617 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5619 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5626 if (!STRINGP (specified_data
))
5628 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5632 /* Read from memory. */
5633 tbr
.bytes
= SDATA (specified_data
);
5634 tbr
.len
= SBYTES (specified_data
);
5637 /* Check PNG signature. */
5638 if (tbr
.len
< sizeof sig
5639 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5641 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5645 /* Need to skip past the signature. */
5646 tbr
.bytes
+= sizeof (sig
);
5649 /* Initialize read and info structs for PNG lib. Casting return
5650 value avoids a GCC warning on W32. */
5651 png_ptr
= (png_structp
)fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5656 if (fp
) fclose (fp
);
5660 /* Casting return value avoids a GCC warning on W32. */
5661 info_ptr
= (png_infop
)fn_png_create_info_struct (png_ptr
);
5664 fn_png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
5665 if (fp
) fclose (fp
);
5669 /* Casting return value avoids a GCC warning on W32. */
5670 end_info
= (png_infop
)fn_png_create_info_struct (png_ptr
);
5673 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
5674 if (fp
) fclose (fp
);
5678 /* Set error jump-back. We come back here when the PNG library
5679 detects an error. */
5680 if (setjmp (png_ptr
->jmpbuf
))
5684 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5687 if (fp
) fclose (fp
);
5691 /* Read image info. */
5692 if (!NILP (specified_data
))
5693 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5695 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5697 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5698 fn_png_read_info (png_ptr
, info_ptr
);
5699 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5700 &interlace_type
, NULL
, NULL
);
5702 if (!check_image_size (f
, width
, height
))
5704 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5707 /* If image contains simply transparency data, we prefer to
5708 construct a clipping mask. */
5709 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5714 /* This function is easier to write if we only have to handle
5715 one data format: RGB or RGBA with 8 bits per channel. Let's
5716 transform other formats into that format. */
5718 /* Strip more than 8 bits per channel. */
5719 if (bit_depth
== 16)
5720 fn_png_set_strip_16 (png_ptr
);
5722 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5724 fn_png_set_expand (png_ptr
);
5726 /* Convert grayscale images to RGB. */
5727 if (color_type
== PNG_COLOR_TYPE_GRAY
5728 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5729 fn_png_set_gray_to_rgb (png_ptr
);
5731 /* Handle alpha channel by combining the image with a background
5732 color. Do this only if a real alpha channel is supplied. For
5733 simple transparency, we prefer a clipping mask. */
5736 /* png_color_16 *image_bg; */
5737 Lisp_Object specified_bg
5738 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5739 int shift
= (bit_depth
== 16) ? 0 : 8;
5741 if (STRINGP (specified_bg
))
5742 /* The user specified `:background', use that. */
5745 if (x_defined_color (f
, SDATA (specified_bg
), &color
, 0))
5747 png_color_16 user_bg
;
5749 memset (&user_bg
, 0, sizeof user_bg
);
5750 user_bg
.red
= color
.red
>> shift
;
5751 user_bg
.green
= color
.green
>> shift
;
5752 user_bg
.blue
= color
.blue
>> shift
;
5754 fn_png_set_background (png_ptr
, &user_bg
,
5755 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5760 /* We use the current frame background, ignoring any default
5761 background color set by the image. */
5762 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5764 png_color_16 frame_background
;
5766 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5767 x_query_color (f
, &color
);
5769 memset (&frame_background
, 0, sizeof frame_background
);
5770 frame_background
.red
= color
.red
>> shift
;
5771 frame_background
.green
= color
.green
>> shift
;
5772 frame_background
.blue
= color
.blue
>> shift
;
5773 #endif /* HAVE_X_WINDOWS */
5775 fn_png_set_background (png_ptr
, &frame_background
,
5776 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5780 /* Update info structure. */
5781 fn_png_read_update_info (png_ptr
, info_ptr
);
5783 /* Get number of channels. Valid values are 1 for grayscale images
5784 and images with a palette, 2 for grayscale images with transparency
5785 information (alpha channel), 3 for RGB images, and 4 for RGB
5786 images with alpha channel, i.e. RGBA. If conversions above were
5787 sufficient we should only have 3 or 4 channels here. */
5788 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5789 xassert (channels
== 3 || channels
== 4);
5791 /* Number of bytes needed for one row of the image. */
5792 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5794 /* Allocate memory for the image. */
5795 pixels
= (png_byte
*) xmalloc (row_bytes
* height
* sizeof *pixels
);
5796 rows
= (png_byte
**) xmalloc (height
* sizeof *rows
);
5797 for (i
= 0; i
< height
; ++i
)
5798 rows
[i
] = pixels
+ i
* row_bytes
;
5800 /* Read the entire image. */
5801 fn_png_read_image (png_ptr
, rows
);
5802 fn_png_read_end (png_ptr
, info_ptr
);
5809 /* Create the X image and pixmap. */
5810 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5814 /* Create an image and pixmap serving as mask if the PNG image
5815 contains an alpha channel. */
5818 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5819 &mask_img
, &img
->mask
))
5821 x_destroy_x_image (ximg
);
5822 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5823 img
->pixmap
= NO_PIXMAP
;
5827 /* Fill the X image and mask from PNG data. */
5828 init_color_table ();
5830 for (y
= 0; y
< height
; ++y
)
5832 png_byte
*p
= rows
[y
];
5834 for (x
= 0; x
< width
; ++x
)
5841 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5842 /* An alpha channel, aka mask channel, associates variable
5843 transparency with an image. Where other image formats
5844 support binary transparency---fully transparent or fully
5845 opaque---PNG allows up to 254 levels of partial transparency.
5846 The PNG library implements partial transparency by combining
5847 the image with a specified background color.
5849 I'm not sure how to handle this here nicely: because the
5850 background on which the image is displayed may change, for
5851 real alpha channel support, it would be necessary to create
5852 a new image for each possible background.
5854 What I'm doing now is that a mask is created if we have
5855 boolean transparency information. Otherwise I'm using
5856 the frame's background color to combine the image with. */
5861 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5867 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5868 /* Set IMG's background color from the PNG image, unless the user
5872 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5874 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5875 img
->background_valid
= 1;
5879 #ifdef COLOR_TABLE_SUPPORT
5880 /* Remember colors allocated for this image. */
5881 img
->colors
= colors_in_color_table (&img
->ncolors
);
5882 free_color_table ();
5883 #endif /* COLOR_TABLE_SUPPORT */
5886 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5891 img
->height
= height
;
5893 /* Maybe fill in the background field while we have ximg handy.
5894 Casting avoids a GCC warning. */
5895 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5897 /* Put the image into the pixmap, then free the X image and its buffer. */
5898 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5899 x_destroy_x_image (ximg
);
5901 /* Same for the mask. */
5904 /* Fill in the background_transparent field while we have the
5905 mask handy. Casting avoids a GCC warning. */
5906 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5908 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5909 x_destroy_x_image (mask_img
);
5915 #else /* HAVE_PNG */
5919 png_load (struct frame
*f
, struct image
*img
)
5921 return ns_load_image(f
, img
,
5922 image_spec_value (img
->spec
, QCfile
, NULL
),
5923 image_spec_value (img
->spec
, QCdata
, NULL
));
5925 #endif /* HAVE_NS */
5928 #endif /* !HAVE_PNG */
5932 /***********************************************************************
5934 ***********************************************************************/
5936 #if defined (HAVE_JPEG) || defined (HAVE_NS)
5938 static int jpeg_image_p (Lisp_Object object
);
5939 static int jpeg_load (struct frame
*f
, struct image
*img
);
5941 /* The symbol `jpeg' identifying images of this type. */
5945 /* Indices of image specification fields in gs_format, below. */
5947 enum jpeg_keyword_index
5956 JPEG_HEURISTIC_MASK
,
5962 /* Vector of image_keyword structures describing the format
5963 of valid user-defined image specifications. */
5965 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
5967 {":type", IMAGE_SYMBOL_VALUE
, 1},
5968 {":data", IMAGE_STRING_VALUE
, 0},
5969 {":file", IMAGE_STRING_VALUE
, 0},
5970 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5971 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5972 {":relief", IMAGE_INTEGER_VALUE
, 0},
5973 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5974 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5975 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5976 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5979 /* Structure describing the image type `jpeg'. */
5981 static struct image_type jpeg_type
=
5990 /* Return non-zero if OBJECT is a valid JPEG image specification. */
5993 jpeg_image_p (Lisp_Object object
)
5995 struct image_keyword fmt
[JPEG_LAST
];
5997 memcpy (fmt
, jpeg_format
, sizeof fmt
);
5999 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6002 /* Must specify either the :data or :file keyword. */
6003 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6006 #endif /* HAVE_JPEG || HAVE_NS */
6010 /* Work around a warning about HAVE_STDLIB_H being redefined in
6012 #ifdef HAVE_STDLIB_H
6013 #define HAVE_STDLIB_H_1
6014 #undef HAVE_STDLIB_H
6015 #endif /* HAVE_STLIB_H */
6017 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6018 /* In older releases of the jpeg library, jpeglib.h will define boolean
6019 differently depending on __WIN32__, so make sure it is defined. */
6023 #include <jpeglib.h>
6026 #ifdef HAVE_STLIB_H_1
6027 #define HAVE_STDLIB_H 1
6032 /* JPEG library details. */
6033 DEF_IMGLIB_FN (jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6034 DEF_IMGLIB_FN (jpeg_start_decompress
, (j_decompress_ptr
));
6035 DEF_IMGLIB_FN (jpeg_finish_decompress
, (j_decompress_ptr
));
6036 DEF_IMGLIB_FN (jpeg_destroy_decompress
, (j_decompress_ptr
));
6037 DEF_IMGLIB_FN (jpeg_read_header
, (j_decompress_ptr
, boolean
));
6038 DEF_IMGLIB_FN (jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6039 DEF_IMGLIB_FN (jpeg_std_error
, (struct jpeg_error_mgr
*));
6040 DEF_IMGLIB_FN (jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6043 init_jpeg_functions (Lisp_Object libraries
)
6047 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6050 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6051 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6052 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6053 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6054 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6055 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6056 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6057 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6061 /* Wrapper since we can't directly assign the function pointer
6062 to another function pointer that was declared more completely easily. */
6064 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6066 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6071 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a)
6072 #define fn_jpeg_start_decompress jpeg_start_decompress
6073 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6074 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6075 #define fn_jpeg_read_header jpeg_read_header
6076 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6077 #define fn_jpeg_std_error jpeg_std_error
6078 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6080 #endif /* HAVE_NTGUI */
6082 struct my_jpeg_error_mgr
6084 struct jpeg_error_mgr pub
;
6085 jmp_buf setjmp_buffer
;
6090 my_error_exit (j_common_ptr cinfo
)
6092 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6093 longjmp (mgr
->setjmp_buffer
, 1);
6097 /* Init source method for JPEG data source manager. Called by
6098 jpeg_read_header() before any data is actually read. See
6099 libjpeg.doc from the JPEG lib distribution. */
6102 our_common_init_source (j_decompress_ptr cinfo
)
6107 /* Method to terminate data source. Called by
6108 jpeg_finish_decompress() after all data has been processed. */
6111 our_common_term_source (j_decompress_ptr cinfo
)
6116 /* Fill input buffer method for JPEG data source manager. Called
6117 whenever more data is needed. We read the whole image in one step,
6118 so this only adds a fake end of input marker at the end. */
6120 static JOCTET our_memory_buffer
[2];
6123 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6125 /* Insert a fake EOI marker. */
6126 struct jpeg_source_mgr
*src
= cinfo
->src
;
6128 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6129 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6131 src
->next_input_byte
= our_memory_buffer
;
6132 src
->bytes_in_buffer
= 2;
6137 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6138 is the JPEG data source manager. */
6141 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6143 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6147 if (num_bytes
> src
->bytes_in_buffer
)
6148 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6150 src
->bytes_in_buffer
-= num_bytes
;
6151 src
->next_input_byte
+= num_bytes
;
6156 /* Set up the JPEG lib for reading an image from DATA which contains
6157 LEN bytes. CINFO is the decompression info structure created for
6158 reading the image. */
6161 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, unsigned int len
)
6163 struct jpeg_source_mgr
*src
;
6165 if (cinfo
->src
== NULL
)
6167 /* First time for this JPEG object? */
6168 cinfo
->src
= (struct jpeg_source_mgr
*)
6169 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6170 sizeof (struct jpeg_source_mgr
));
6171 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6172 src
->next_input_byte
= data
;
6175 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6176 src
->init_source
= our_common_init_source
;
6177 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6178 src
->skip_input_data
= our_memory_skip_input_data
;
6179 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6180 src
->term_source
= our_common_term_source
;
6181 src
->bytes_in_buffer
= len
;
6182 src
->next_input_byte
= data
;
6186 struct jpeg_stdio_mgr
6188 struct jpeg_source_mgr mgr
;
6195 /* Size of buffer to read JPEG from file.
6196 Not too big, as we want to use alloc_small. */
6197 #define JPEG_STDIO_BUFFER_SIZE 8192
6200 /* Fill input buffer method for JPEG data source manager. Called
6201 whenever more data is needed. The data is read from a FILE *. */
6204 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6206 struct jpeg_stdio_mgr
*src
;
6208 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6213 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6215 src
->mgr
.bytes_in_buffer
= bytes
;
6218 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6220 src
->buffer
[0] = (JOCTET
) 0xFF;
6221 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6222 src
->mgr
.bytes_in_buffer
= 2;
6224 src
->mgr
.next_input_byte
= src
->buffer
;
6231 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6232 is the JPEG data source manager. */
6235 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6237 struct jpeg_stdio_mgr
*src
;
6238 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6240 while (num_bytes
> 0 && !src
->finished
)
6242 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6244 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6245 src
->mgr
.next_input_byte
+= num_bytes
;
6250 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6251 src
->mgr
.bytes_in_buffer
= 0;
6252 src
->mgr
.next_input_byte
= NULL
;
6254 our_stdio_fill_input_buffer (cinfo
);
6260 /* Set up the JPEG lib for reading an image from a FILE *.
6261 CINFO is the decompression info structure created for
6262 reading the image. */
6265 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6267 struct jpeg_stdio_mgr
*src
;
6269 if (cinfo
->src
!= NULL
)
6270 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6273 /* First time for this JPEG object? */
6274 cinfo
->src
= (struct jpeg_source_mgr
*)
6275 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6276 sizeof (struct jpeg_stdio_mgr
));
6277 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6278 src
->buffer
= (JOCTET
*)
6279 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6280 JPEG_STDIO_BUFFER_SIZE
);
6285 src
->mgr
.init_source
= our_common_init_source
;
6286 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6287 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6288 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6289 src
->mgr
.term_source
= our_common_term_source
;
6290 src
->mgr
.bytes_in_buffer
= 0;
6291 src
->mgr
.next_input_byte
= NULL
;
6295 /* Load image IMG for use on frame F. Patterned after example.c
6296 from the JPEG lib. */
6299 jpeg_load (struct frame
*f
, struct image
*img
)
6301 struct jpeg_decompress_struct cinfo
;
6302 struct my_jpeg_error_mgr mgr
;
6303 Lisp_Object file
, specified_file
;
6304 Lisp_Object specified_data
;
6305 FILE * volatile fp
= NULL
;
6307 int row_stride
, x
, y
;
6308 XImagePtr ximg
= NULL
;
6310 unsigned long *colors
;
6313 /* Open the JPEG file. */
6314 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6315 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6317 if (NILP (specified_data
))
6319 file
= x_find_image_file (specified_file
);
6320 if (!STRINGP (file
))
6322 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6326 fp
= fopen (SDATA (file
), "rb");
6329 image_error ("Cannot open `%s'", file
, Qnil
);
6333 else if (!STRINGP (specified_data
))
6335 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6339 /* Customize libjpeg's error handling to call my_error_exit when an
6340 error is detected. This function will perform a longjmp.
6341 Casting return value avoids a GCC warning on W32. */
6342 cinfo
.err
= (struct jpeg_error_mgr
*)fn_jpeg_std_error (&mgr
.pub
);
6343 mgr
.pub
.error_exit
= my_error_exit
;
6345 if ((rc
= setjmp (mgr
.setjmp_buffer
)) != 0)
6349 /* Called from my_error_exit. Display a JPEG error. */
6350 char buffer
[JMSG_LENGTH_MAX
];
6351 cinfo
.err
->format_message ((j_common_ptr
) &cinfo
, buffer
);
6352 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6353 build_string (buffer
));
6356 /* Close the input file and destroy the JPEG object. */
6358 fclose ((FILE *) fp
);
6359 fn_jpeg_destroy_decompress (&cinfo
);
6361 /* If we already have an XImage, free that. */
6362 x_destroy_x_image (ximg
);
6364 /* Free pixmap and colors. */
6365 x_clear_image (f
, img
);
6369 /* Create the JPEG decompression object. Let it read from fp.
6370 Read the JPEG image header. */
6371 fn_jpeg_CreateDecompress (&cinfo
, JPEG_LIB_VERSION
, sizeof (cinfo
));
6373 if (NILP (specified_data
))
6374 jpeg_file_src (&cinfo
, (FILE *) fp
);
6376 jpeg_memory_src (&cinfo
, SDATA (specified_data
),
6377 SBYTES (specified_data
));
6379 fn_jpeg_read_header (&cinfo
, 1);
6381 /* Customize decompression so that color quantization will be used.
6382 Start decompression. */
6383 cinfo
.quantize_colors
= 1;
6384 fn_jpeg_start_decompress (&cinfo
);
6385 width
= img
->width
= cinfo
.output_width
;
6386 height
= img
->height
= cinfo
.output_height
;
6388 if (!check_image_size (f
, width
, height
))
6390 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6391 longjmp (mgr
.setjmp_buffer
, 2);
6394 /* Create X image and pixmap. */
6395 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6396 longjmp (mgr
.setjmp_buffer
, 2);
6398 /* Allocate colors. When color quantization is used,
6399 cinfo.actual_number_of_colors has been set with the number of
6400 colors generated, and cinfo.colormap is a two-dimensional array
6401 of color indices in the range 0..cinfo.actual_number_of_colors.
6402 No more than 255 colors will be generated. */
6406 if (cinfo
.out_color_components
> 2)
6407 ir
= 0, ig
= 1, ib
= 2;
6408 else if (cinfo
.out_color_components
> 1)
6409 ir
= 0, ig
= 1, ib
= 0;
6411 ir
= 0, ig
= 0, ib
= 0;
6413 /* Use the color table mechanism because it handles colors that
6414 cannot be allocated nicely. Such colors will be replaced with
6415 a default color, and we don't have to care about which colors
6416 can be freed safely, and which can't. */
6417 init_color_table ();
6418 colors
= (unsigned long *) alloca (cinfo
.actual_number_of_colors
6421 for (i
= 0; i
< cinfo
.actual_number_of_colors
; ++i
)
6423 /* Multiply RGB values with 255 because X expects RGB values
6424 in the range 0..0xffff. */
6425 int r
= cinfo
.colormap
[ir
][i
] << 8;
6426 int g
= cinfo
.colormap
[ig
][i
] << 8;
6427 int b
= cinfo
.colormap
[ib
][i
] << 8;
6428 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6431 #ifdef COLOR_TABLE_SUPPORT
6432 /* Remember those colors actually allocated. */
6433 img
->colors
= colors_in_color_table (&img
->ncolors
);
6434 free_color_table ();
6435 #endif /* COLOR_TABLE_SUPPORT */
6439 row_stride
= width
* cinfo
.output_components
;
6440 buffer
= cinfo
.mem
->alloc_sarray ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
,
6442 for (y
= 0; y
< height
; ++y
)
6444 fn_jpeg_read_scanlines (&cinfo
, buffer
, 1);
6445 for (x
= 0; x
< cinfo
.output_width
; ++x
)
6446 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6450 fn_jpeg_finish_decompress (&cinfo
);
6451 fn_jpeg_destroy_decompress (&cinfo
);
6453 fclose ((FILE *) fp
);
6455 /* Maybe fill in the background field while we have ximg handy. */
6456 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6457 /* Casting avoids a GCC warning. */
6458 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6460 /* Put the image into the pixmap. */
6461 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6462 x_destroy_x_image (ximg
);
6466 #else /* HAVE_JPEG */
6470 jpeg_load (struct frame
*f
, struct image
*img
)
6472 return ns_load_image (f
, img
,
6473 image_spec_value (img
->spec
, QCfile
, NULL
),
6474 image_spec_value (img
->spec
, QCdata
, NULL
));
6476 #endif /* HAVE_NS */
6478 #endif /* !HAVE_JPEG */
6482 /***********************************************************************
6484 ***********************************************************************/
6486 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6488 static int tiff_image_p (Lisp_Object object
);
6489 static int tiff_load (struct frame
*f
, struct image
*img
);
6491 /* The symbol `tiff' identifying images of this type. */
6495 /* Indices of image specification fields in tiff_format, below. */
6497 enum tiff_keyword_index
6506 TIFF_HEURISTIC_MASK
,
6513 /* Vector of image_keyword structures describing the format
6514 of valid user-defined image specifications. */
6516 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6518 {":type", IMAGE_SYMBOL_VALUE
, 1},
6519 {":data", IMAGE_STRING_VALUE
, 0},
6520 {":file", IMAGE_STRING_VALUE
, 0},
6521 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6522 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6523 {":relief", IMAGE_INTEGER_VALUE
, 0},
6524 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6525 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6526 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6527 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6528 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6531 /* Structure describing the image type `tiff'. */
6533 static struct image_type tiff_type
=
6542 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6545 tiff_image_p (Lisp_Object object
)
6547 struct image_keyword fmt
[TIFF_LAST
];
6548 memcpy (fmt
, tiff_format
, sizeof fmt
);
6550 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6553 /* Must specify either the :data or :file keyword. */
6554 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6557 #endif /* HAVE_TIFF || HAVE_NS */
6565 /* TIFF library details. */
6566 DEF_IMGLIB_FN (TIFFSetErrorHandler
, (TIFFErrorHandler
));
6567 DEF_IMGLIB_FN (TIFFSetWarningHandler
, (TIFFErrorHandler
));
6568 DEF_IMGLIB_FN (TIFFOpen
, (const char *, const char *));
6569 DEF_IMGLIB_FN (TIFFClientOpen
, (const char *, const char *, thandle_t
,
6570 TIFFReadWriteProc
, TIFFReadWriteProc
,
6571 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6572 TIFFMapFileProc
, TIFFUnmapFileProc
));
6573 DEF_IMGLIB_FN (TIFFGetField
, (TIFF
*, ttag_t
, ...));
6574 DEF_IMGLIB_FN (TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6575 DEF_IMGLIB_FN (TIFFClose
, (TIFF
*));
6576 DEF_IMGLIB_FN (TIFFSetDirectory
, (TIFF
*, tdir_t
));
6579 init_tiff_functions (Lisp_Object libraries
)
6583 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6586 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6587 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6588 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6589 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6590 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6591 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6592 LOAD_IMGLIB_FN (library
, TIFFClose
);
6593 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6599 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6600 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6601 #define fn_TIFFOpen TIFFOpen
6602 #define fn_TIFFClientOpen TIFFClientOpen
6603 #define fn_TIFFGetField TIFFGetField
6604 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6605 #define fn_TIFFClose TIFFClose
6606 #define fn_TIFFSetDirectory TIFFSetDirectory
6607 #endif /* HAVE_NTGUI */
6610 /* Reading from a memory buffer for TIFF images Based on the PNG
6611 memory source, but we have to provide a lot of extra functions.
6614 We really only need to implement read and seek, but I am not
6615 convinced that the TIFF library is smart enough not to destroy
6616 itself if we only hand it the function pointers we need to
6621 unsigned char *bytes
;
6628 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6630 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6632 if (size
> src
->len
- src
->index
)
6634 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6640 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6646 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6648 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6653 case SEEK_SET
: /* Go from beginning of source. */
6657 case SEEK_END
: /* Go from end of source. */
6658 idx
= src
->len
+ off
;
6661 case SEEK_CUR
: /* Go from current position. */
6662 idx
= src
->index
+ off
;
6665 default: /* Invalid `whence'. */
6669 if (idx
> src
->len
|| idx
< 0)
6677 tiff_close_memory (thandle_t data
)
6684 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6686 /* It is already _IN_ memory. */
6691 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6693 /* We don't need to do this. */
6697 tiff_size_of_memory (thandle_t data
)
6699 return ((tiff_memory_source
*) data
)->len
;
6704 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6709 len
= sprintf (buf
, "TIFF error: %s ", title
);
6710 vsprintf (buf
+ len
, format
, ap
);
6711 add_to_log (buf
, Qnil
, Qnil
);
6716 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6721 len
= sprintf (buf
, "TIFF warning: %s ", title
);
6722 vsprintf (buf
+ len
, format
, ap
);
6723 add_to_log (buf
, Qnil
, Qnil
);
6727 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6731 tiff_load (struct frame
*f
, struct image
*img
)
6733 Lisp_Object file
, specified_file
;
6734 Lisp_Object specified_data
;
6736 int width
, height
, x
, y
, count
;
6740 tiff_memory_source memsrc
;
6743 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6744 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6746 fn_TIFFSetErrorHandler (tiff_error_handler
);
6747 fn_TIFFSetWarningHandler (tiff_warning_handler
);
6749 if (NILP (specified_data
))
6751 /* Read from a file */
6752 file
= x_find_image_file (specified_file
);
6753 if (!STRINGP (file
))
6755 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6759 /* Try to open the image file. Casting return value avoids a
6760 GCC warning on W32. */
6761 tiff
= (TIFF
*)fn_TIFFOpen (SDATA (file
), "r");
6764 image_error ("Cannot open `%s'", file
, Qnil
);
6770 if (!STRINGP (specified_data
))
6772 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6776 /* Memory source! */
6777 memsrc
.bytes
= SDATA (specified_data
);
6778 memsrc
.len
= SBYTES (specified_data
);
6781 /* Casting arguments return value avoids a GCC warning on W32. */
6782 tiff
= (TIFF
*)fn_TIFFClientOpen ("memory_source", "r",
6783 (thandle_t
) &memsrc
,
6784 (TIFFReadWriteProc
) tiff_read_from_memory
,
6785 (TIFFReadWriteProc
) tiff_write_from_memory
,
6786 tiff_seek_in_memory
,
6788 tiff_size_of_memory
,
6794 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6799 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6800 if (INTEGERP (image
))
6802 int ino
= XFASTINT (image
);
6803 if (!fn_TIFFSetDirectory (tiff
, ino
))
6805 image_error ("Invalid image number `%s' in image `%s'",
6807 fn_TIFFClose (tiff
);
6812 /* Get width and height of the image, and allocate a raster buffer
6813 of width x height 32-bit values. */
6814 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6815 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6817 if (!check_image_size (f
, width
, height
))
6819 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6820 fn_TIFFClose (tiff
);
6824 buf
= (uint32
*) xmalloc (width
* height
* sizeof *buf
);
6826 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6828 /* Count the number of images in the file. */
6829 for (count
= 1, rc2
= 1; rc2
; count
++)
6830 rc2
= fn_TIFFSetDirectory (tiff
, count
);
6833 img
->data
.lisp_val
= Fcons (Qcount
,
6834 Fcons (make_number (count
),
6835 img
->data
.lisp_val
));
6837 fn_TIFFClose (tiff
);
6840 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6845 /* Create the X image and pixmap. */
6846 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6852 /* Initialize the color table. */
6853 init_color_table ();
6855 /* Process the pixel raster. Origin is in the lower-left corner. */
6856 for (y
= 0; y
< height
; ++y
)
6858 uint32
*row
= buf
+ y
* width
;
6860 for (x
= 0; x
< width
; ++x
)
6862 uint32 abgr
= row
[x
];
6863 int r
= TIFFGetR (abgr
) << 8;
6864 int g
= TIFFGetG (abgr
) << 8;
6865 int b
= TIFFGetB (abgr
) << 8;
6866 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6870 #ifdef COLOR_TABLE_SUPPORT
6871 /* Remember the colors allocated for the image. Free the color table. */
6872 img
->colors
= colors_in_color_table (&img
->ncolors
);
6873 free_color_table ();
6874 #endif /* COLOR_TABLE_SUPPORT */
6877 img
->height
= height
;
6879 /* Maybe fill in the background field while we have ximg handy. */
6880 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6881 /* Casting avoids a GCC warning on W32. */
6882 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6884 /* Put the image into the pixmap, then free the X image and its buffer. */
6885 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6886 x_destroy_x_image (ximg
);
6892 #else /* HAVE_TIFF */
6896 tiff_load (struct frame
*f
, struct image
*img
)
6898 return ns_load_image (f
, img
,
6899 image_spec_value (img
->spec
, QCfile
, NULL
),
6900 image_spec_value (img
->spec
, QCdata
, NULL
));
6902 #endif /* HAVE_NS */
6904 #endif /* !HAVE_TIFF */
6908 /***********************************************************************
6910 ***********************************************************************/
6912 #if defined (HAVE_GIF) || defined (HAVE_NS)
6914 static int gif_image_p (Lisp_Object object
);
6915 static int gif_load (struct frame
*f
, struct image
*img
);
6916 static void gif_clear_image (struct frame
*f
, struct image
*img
);
6918 /* The symbol `gif' identifying images of this type. */
6922 /* Indices of image specification fields in gif_format, below. */
6924 enum gif_keyword_index
6940 /* Vector of image_keyword structures describing the format
6941 of valid user-defined image specifications. */
6943 static const struct image_keyword gif_format
[GIF_LAST
] =
6945 {":type", IMAGE_SYMBOL_VALUE
, 1},
6946 {":data", IMAGE_STRING_VALUE
, 0},
6947 {":file", IMAGE_STRING_VALUE
, 0},
6948 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6949 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6950 {":relief", IMAGE_INTEGER_VALUE
, 0},
6951 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6952 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6953 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6954 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
6955 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6958 /* Structure describing the image type `gif'. */
6960 static struct image_type gif_type
=
6969 /* Free X resources of GIF image IMG which is used on frame F. */
6972 gif_clear_image (struct frame
*f
, struct image
*img
)
6974 /* IMG->data.ptr_val may contain metadata with extension data. */
6975 img
->data
.lisp_val
= Qnil
;
6976 x_clear_image (f
, img
);
6979 /* Return non-zero if OBJECT is a valid GIF image specification. */
6982 gif_image_p (Lisp_Object object
)
6984 struct image_keyword fmt
[GIF_LAST
];
6985 memcpy (fmt
, gif_format
, sizeof fmt
);
6987 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
6990 /* Must specify either the :data or :file keyword. */
6991 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
6994 #endif /* HAVE_GIF */
6998 #if defined (HAVE_NTGUI)
6999 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7000 Undefine before redefining to avoid a preprocessor warning. */
7004 /* avoid conflict with QuickdrawText.h */
7005 #define DrawText gif_DrawText
7006 #include <gif_lib.h>
7009 #else /* HAVE_NTGUI */
7011 #include <gif_lib.h>
7013 #endif /* HAVE_NTGUI */
7018 /* GIF library details. */
7019 DEF_IMGLIB_FN (DGifCloseFile
, (GifFileType
*));
7020 DEF_IMGLIB_FN (DGifSlurp
, (GifFileType
*));
7021 DEF_IMGLIB_FN (DGifOpen
, (void *, InputFunc
));
7022 DEF_IMGLIB_FN (DGifOpenFileName
, (const char *));
7025 init_gif_functions (Lisp_Object libraries
)
7029 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7032 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7033 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7034 LOAD_IMGLIB_FN (library
, DGifOpen
);
7035 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7041 #define fn_DGifCloseFile DGifCloseFile
7042 #define fn_DGifSlurp DGifSlurp
7043 #define fn_DGifOpen DGifOpen
7044 #define fn_DGifOpenFileName DGifOpenFileName
7046 #endif /* HAVE_NTGUI */
7048 /* Reading a GIF image from memory
7049 Based on the PNG memory stuff to a certain extent. */
7053 unsigned char *bytes
;
7059 /* Make the current memory source available to gif_read_from_memory.
7060 It's done this way because not all versions of libungif support
7061 a UserData field in the GifFileType structure. */
7062 static gif_memory_source
*current_gif_memory_src
;
7065 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7067 gif_memory_source
*src
= current_gif_memory_src
;
7069 if (len
> src
->len
- src
->index
)
7072 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7078 /* Load GIF image IMG for use on frame F. Value is non-zero if
7081 static const int interlace_start
[] = {0, 4, 2, 1};
7082 static const int interlace_increment
[] = {8, 8, 4, 2};
7084 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7087 gif_load (struct frame
*f
, struct image
*img
)
7089 Lisp_Object file
, specified_file
;
7090 Lisp_Object specified_data
;
7091 int rc
, width
, height
, x
, y
, i
;
7092 boolean transparent_p
;
7094 ColorMapObject
*gif_color_map
;
7095 unsigned long pixel_colors
[256];
7098 int ino
, image_height
, image_width
;
7099 gif_memory_source memsrc
;
7100 unsigned char *raster
;
7101 unsigned int transparency_color_index
;
7103 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7104 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7106 if (NILP (specified_data
))
7108 file
= x_find_image_file (specified_file
);
7109 if (!STRINGP (file
))
7111 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7115 /* Open the GIF file. Casting return value avoids a GCC warning
7117 gif
= (GifFileType
*)fn_DGifOpenFileName (SDATA (file
));
7120 image_error ("Cannot open `%s'", file
, Qnil
);
7126 if (!STRINGP (specified_data
))
7128 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7132 /* Read from memory! */
7133 current_gif_memory_src
= &memsrc
;
7134 memsrc
.bytes
= SDATA (specified_data
);
7135 memsrc
.len
= SBYTES (specified_data
);
7138 /* Casting return value avoids a GCC warning on W32. */
7139 gif
= (GifFileType
*) fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7142 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7147 /* Before reading entire contents, check the declared image size. */
7148 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7150 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7151 fn_DGifCloseFile (gif
);
7155 /* Read entire contents. */
7156 rc
= fn_DGifSlurp (gif
);
7157 if (rc
== GIF_ERROR
)
7159 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7160 fn_DGifCloseFile (gif
);
7164 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7165 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7166 if (ino
>= gif
->ImageCount
)
7168 image_error ("Invalid image number `%s' in image `%s'",
7170 fn_DGifCloseFile (gif
);
7174 for (i
= 0; i
< gif
->SavedImages
[ino
].ExtensionBlockCount
; i
++)
7175 if ((gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Function
7176 == GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7177 && gif
->SavedImages
[ino
].ExtensionBlocks
[i
].ByteCount
== 4
7178 /* Transparency enabled? */
7179 && gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Bytes
[0] & 1)
7182 transparency_color_index
7183 = (unsigned char) gif
->SavedImages
[ino
].ExtensionBlocks
[i
].Bytes
[3];
7186 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[ino
].ImageDesc
.Top
;
7187 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[ino
].ImageDesc
.Left
;
7188 image_height
= gif
->SavedImages
[ino
].ImageDesc
.Height
;
7189 img
->corners
[BOT_CORNER
] = img
->corners
[TOP_CORNER
] + image_height
;
7190 image_width
= gif
->SavedImages
[ino
].ImageDesc
.Width
;
7191 img
->corners
[RIGHT_CORNER
] = img
->corners
[LEFT_CORNER
] + image_width
;
7193 width
= img
->width
= max (gif
->SWidth
,
7194 max (gif
->Image
.Left
+ gif
->Image
.Width
,
7195 img
->corners
[RIGHT_CORNER
]));
7196 height
= img
->height
= max (gif
->SHeight
,
7197 max (gif
->Image
.Top
+ gif
->Image
.Height
,
7198 img
->corners
[BOT_CORNER
]));
7200 if (!check_image_size (f
, width
, height
))
7202 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7203 fn_DGifCloseFile (gif
);
7207 /* Create the X image and pixmap. */
7208 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7210 fn_DGifCloseFile (gif
);
7214 /* Allocate colors. */
7215 gif_color_map
= gif
->SavedImages
[ino
].ImageDesc
.ColorMap
;
7217 gif_color_map
= gif
->SColorMap
;
7218 init_color_table ();
7219 memset (pixel_colors
, 0, sizeof pixel_colors
);
7222 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7224 if (transparent_p
&& transparency_color_index
== i
)
7226 Lisp_Object specified_bg
7227 = image_spec_value (img
->spec
, QCbackground
, NULL
);
7228 pixel_colors
[i
] = STRINGP (specified_bg
)
7229 ? x_alloc_image_color (f
, img
, specified_bg
,
7230 FRAME_BACKGROUND_PIXEL (f
))
7231 : FRAME_BACKGROUND_PIXEL (f
);
7235 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7236 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7237 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7238 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7242 #ifdef COLOR_TABLE_SUPPORT
7243 img
->colors
= colors_in_color_table (&img
->ncolors
);
7244 free_color_table ();
7245 #endif /* COLOR_TABLE_SUPPORT */
7247 /* Clear the part of the screen image that are not covered by
7248 the image from the GIF file. Full animated GIF support
7249 requires more than can be done here (see the gif89 spec,
7250 disposal methods). Let's simply assume that the part
7251 not covered by a sub-image is in the frame's background color. */
7252 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7253 for (x
= 0; x
< width
; ++x
)
7254 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7256 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7257 for (x
= 0; x
< width
; ++x
)
7258 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7260 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7262 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7263 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7264 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7265 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7268 /* Read the GIF image into the X image. We use a local variable
7269 `raster' here because RasterBits below is a char *, and invites
7270 problems with bytes >= 0x80. */
7271 raster
= (unsigned char *) gif
->SavedImages
[ino
].RasterBits
;
7273 if (gif
->SavedImages
[ino
].ImageDesc
.Interlace
)
7276 int row
= interlace_start
[0];
7280 for (y
= 0; y
< image_height
; y
++)
7282 if (row
>= image_height
)
7284 row
= interlace_start
[++pass
];
7285 while (row
>= image_height
)
7286 row
= interlace_start
[++pass
];
7289 for (x
= 0; x
< image_width
; x
++)
7291 int i
= raster
[(y
* image_width
) + x
];
7292 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7293 row
+ img
->corners
[TOP_CORNER
], pixel_colors
[i
]);
7296 row
+= interlace_increment
[pass
];
7301 for (y
= 0; y
< image_height
; ++y
)
7302 for (x
= 0; x
< image_width
; ++x
)
7304 int i
= raster
[y
* image_width
+ x
];
7305 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7306 y
+ img
->corners
[TOP_CORNER
], pixel_colors
[i
]);
7310 /* Save GIF image extension data for `image-metadata'.
7311 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7312 img
->data
.lisp_val
= Qnil
;
7313 if (gif
->SavedImages
[ino
].ExtensionBlockCount
> 0)
7315 ExtensionBlock
*ext
= gif
->SavedImages
[ino
].ExtensionBlocks
;
7316 for (i
= 0; i
< gif
->SavedImages
[ino
].ExtensionBlockCount
; i
++, ext
++)
7317 /* Append (... FUNCTION "BYTES") */
7318 img
->data
.lisp_val
= Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7319 Fcons (make_number (ext
->Function
),
7320 img
->data
.lisp_val
));
7321 img
->data
.lisp_val
= Fcons (Qextension_data
,
7322 Fcons (Fnreverse (img
->data
.lisp_val
),
7325 if (gif
->ImageCount
> 1)
7326 img
->data
.lisp_val
= Fcons (Qcount
,
7327 Fcons (make_number (gif
->ImageCount
),
7328 img
->data
.lisp_val
));
7330 fn_DGifCloseFile (gif
);
7332 /* Maybe fill in the background field while we have ximg handy. */
7333 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7334 /* Casting avoids a GCC warning. */
7335 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7337 /* Put the image into the pixmap, then free the X image and its buffer. */
7338 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7339 x_destroy_x_image (ximg
);
7344 #else /* !HAVE_GIF */
7348 gif_load (struct frame
*f
, struct image
*img
)
7350 return ns_load_image (f
, img
,
7351 image_spec_value (img
->spec
, QCfile
, NULL
),
7352 image_spec_value (img
->spec
, QCdata
, NULL
));
7354 #endif /* HAVE_NS */
7356 #endif /* HAVE_GIF */
7359 /***********************************************************************
7361 ***********************************************************************/
7362 #if defined (HAVE_IMAGEMAGICK)
7364 /* The symbol `imagemagick' identifying images of this type. */
7366 Lisp_Object Qimagemagick
;
7367 /* Indices of image specification fields in imagemagick_format, below. */
7369 enum imagemagick_keyword_index
7377 IMAGEMAGICK_ALGORITHM
,
7378 IMAGEMAGICK_HEURISTIC_MASK
,
7380 IMAGEMAGICK_BACKGROUND
,
7383 IMAGEMAGICK_ROTATION
,
7388 /* Vector of image_keyword structures describing the format
7389 of valid user-defined image specifications. */
7391 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7393 {":type", IMAGE_SYMBOL_VALUE
, 1},
7394 {":data", IMAGE_STRING_VALUE
, 0},
7395 {":file", IMAGE_STRING_VALUE
, 0},
7396 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7397 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7398 {":relief", IMAGE_INTEGER_VALUE
, 0},
7399 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7400 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7401 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7402 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7403 {":height", IMAGE_INTEGER_VALUE
, 0},
7404 {":width", IMAGE_INTEGER_VALUE
, 0},
7405 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7406 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7408 /* Free X resources of imagemagick image IMG which is used on frame F. */
7411 imagemagick_clear_image (struct frame
*f
,
7414 x_clear_image (f
, img
);
7419 /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do
7420 this by calling parse_image_spec and supplying the keywords that
7421 identify the IMAGEMAGICK format. */
7424 imagemagick_image_p (Lisp_Object object
)
7426 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7427 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7429 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7432 /* Must specify either the :data or :file keyword. */
7433 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7436 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7437 Therefore rename the function so it doesnt collide with ImageMagick. */
7438 #define DrawRectangle DrawRectangleGif
7439 #include <wand/MagickWand.h>
7441 /* imagemagick_load_image is a helper function for imagemagick_load,
7442 which does the actual loading given contents and size, apart from
7443 frame and image structures, passed from imagemagick_load.
7445 Uses librimagemagick to do most of the image processing.
7447 non-zero when successful.
7451 imagemagick_load_image (/* Pointer to emacs frame structure. */
7453 /* Pointer to emacs image structure. */
7455 /* String containing the IMAGEMAGICK data to
7457 unsigned char *contents
,
7458 /* Size of data in bytes. */
7460 /* Filename, either pass filename or
7462 unsigned char *filename
)
7464 unsigned long width
;
7465 unsigned long height
;
7471 Lisp_Object specified_bg
;
7476 MagickWand
*image_wand
;
7477 MagickWand
*ping_wand
;
7478 PixelIterator
*iterator
;
7480 MagickPixelPacket pixel
;
7483 Lisp_Object crop
, geometry
;
7485 int desired_width
, desired_height
;
7487 int imagemagick_rendermethod
;
7489 ImageInfo
*image_info
;
7490 ExceptionInfo
*exception
;
7494 /* Handle image index for image types who can contain more than one
7495 image. Interface :index is same as for GIF. First we "ping" the
7496 image to see how many sub-images it contains. Pinging is faster
7497 than loading the image to find out things about it. */
7499 /* `MagickWandGenesis' initializes the imagemagick environment. */
7500 MagickWandGenesis ();
7501 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7502 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7503 ping_wand
= NewMagickWand ();
7504 MagickSetResolution (ping_wand
, 2, 2);
7505 if (filename
!= NULL
)
7507 status
= MagickPingImage (ping_wand
, filename
);
7511 status
= MagickPingImageBlob (ping_wand
, contents
, size
);
7514 if (ino
>= MagickGetNumberImages (ping_wand
))
7516 image_error ("Invalid image number `%s' in image `%s'",
7518 DestroyMagickWand (ping_wand
);
7522 if (MagickGetNumberImages(ping_wand
) > 1)
7523 img
->data
.lisp_val
=
7525 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7526 img
->data
.lisp_val
));
7528 DestroyMagickWand (ping_wand
);
7530 /* Now, after pinging, we know how many images are inside the
7531 file. If its not a bundle, just one. */
7533 if (filename
!= NULL
)
7535 image_info
= CloneImageInfo ((ImageInfo
*) NULL
);
7536 (void) strcpy (image_info
->filename
, filename
);
7537 image_info
->number_scenes
= 1;
7538 image_info
->scene
= ino
;
7539 exception
= AcquireExceptionInfo ();
7541 im_image
= ReadImage (image_info
, exception
);
7542 DestroyExceptionInfo (exception
);
7544 if (im_image
!= NULL
)
7546 image_wand
= NewMagickWandFromImage (im_image
);
7547 DestroyImage(im_image
);
7548 status
= MagickTrue
;
7551 status
= MagickFalse
;
7555 image_wand
= NewMagickWand ();
7556 status
= MagickReadImageBlob (image_wand
, contents
, size
);
7559 if (status
== MagickFalse
) goto imagemagick_error
;
7561 /* If width and/or height is set in the display spec assume we want
7562 to scale to those values. if either h or w is unspecified, the
7563 unspecified should be calculated from the specified to preserve
7566 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7567 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7568 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7569 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7571 height
= MagickGetImageHeight (image_wand
);
7572 width
= MagickGetImageWidth (image_wand
);
7574 if(desired_width
!= -1 && desired_height
== -1)
7576 /* w known, calculate h. */
7577 desired_height
= (double) desired_width
/ width
* height
;
7579 if(desired_width
== -1 && desired_height
!= -1)
7581 /* h known, calculate w. */
7582 desired_width
= (double) desired_height
/ height
* width
;
7584 if(desired_width
!= -1 && desired_height
!= -1)
7586 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7587 if (status
== MagickFalse
)
7589 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7590 goto imagemagick_error
;
7595 /* crop behaves similar to image slicing in Emacs but is more memory
7597 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7599 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7601 /* After some testing, it seems MagickCropImage is the fastest
7602 crop function in ImageMagick. This crop function seems to do
7603 less copying than the alternatives, but it still reads the
7604 entire image into memory before croping, which is aparently
7605 difficult to avoid when using imagemagick. */
7608 w
= XFASTINT (XCAR (crop
));
7610 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7612 h
= XFASTINT (XCAR (crop
));
7614 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7616 x
= XFASTINT (XCAR (crop
));
7618 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7620 y
= XFASTINT (XCAR (crop
));
7621 MagickCropImage (image_wand
, w
, h
, x
, y
);
7627 /* Furthermore :rotation. we need background color and angle for
7630 TODO background handling for rotation specified_bg =
7631 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7633 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7636 PixelWand
* background
= NewPixelWand ();
7637 PixelSetColor (background
, "#ffffff");/*TODO remove hardcode*/
7639 rotation
= extract_float (value
);
7641 status
= MagickRotateImage (image_wand
, background
, rotation
);
7642 DestroyPixelWand (background
);
7643 if (status
== MagickFalse
)
7645 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7646 goto imagemagick_error
;
7650 /* Finaly we are done manipulating the image, figure out resulting
7651 width, height, and then transfer ownerwship to Emacs. */
7652 height
= MagickGetImageHeight (image_wand
);
7653 width
= MagickGetImageWidth (image_wand
);
7655 if (! check_image_size (f
, width
, height
))
7657 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7658 goto imagemagick_error
;
7661 /* We can now get a valid pixel buffer from the imagemagick file, if all
7664 init_color_table ();
7665 imagemagick_rendermethod
= (INTEGERP (Vimagemagick_render_type
)
7666 ? XFASTINT (Vimagemagick_render_type
) : 0);
7667 if (imagemagick_rendermethod
== 0)
7669 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7670 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7671 &ximg
, &img
->pixmap
))
7673 #ifdef COLOR_TABLE_SUPPORT
7674 free_color_table ();
7676 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7677 goto imagemagick_error
;
7680 /* Copy imagegmagick image to x with primitive yet robust pixel
7681 pusher loop. This has been tested a lot with many different
7684 /* Copy pixels from the imagemagick image structure to the x image map. */
7685 iterator
= NewPixelIterator (image_wand
);
7686 if (iterator
== (PixelIterator
*) NULL
)
7688 #ifdef COLOR_TABLE_SUPPORT
7689 free_color_table ();
7691 x_destroy_x_image (ximg
);
7692 image_error ("Imagemagick pixel iterator creation failed",
7694 goto imagemagick_error
;
7697 for (y
= 0; y
< (long) MagickGetImageHeight (image_wand
); y
++)
7699 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7700 if (pixels
== (PixelWand
**) NULL
)
7702 for (x
= 0; x
< (long) width
; x
++)
7704 PixelGetMagickColor (pixels
[x
], &pixel
);
7705 XPutPixel (ximg
, x
, y
,
7706 lookup_rgb_color (f
,
7712 DestroyPixelIterator (iterator
);
7715 if (imagemagick_rendermethod
== 1)
7717 /* Magicexportimage is normaly faster than pixelpushing. This
7718 method is also well tested. Some aspects of this method are
7719 ad-hoc and needs to be more researched. */
7720 int imagedepth
= 24;/*MagickGetImageDepth(image_wand);*/
7721 char* exportdepth
= imagedepth
<= 8 ? "I" : "BGRP";/*"RGBP";*/
7722 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7723 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7724 &ximg
, &img
->pixmap
))
7726 #ifdef COLOR_TABLE_SUPPORT
7727 free_color_table ();
7729 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7730 goto imagemagick_error
;
7734 /* Oddly, the below code doesnt seem to work:*/
7735 /* switch(ximg->bitmap_unit){ */
7737 /* pixelwidth=CharPixel; */
7740 /* pixelwidth=ShortPixel; */
7743 /* pixelwidth=LongPixel; */
7747 Here im just guessing the format of the bitmap.
7748 happens to work fine for:
7751 seems about 3 times as fast as pixel pushing(not carefully measured)
7753 pixelwidth
= CharPixel
;/*??? TODO figure out*/
7754 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7755 MagickExportImagePixels (image_wand
,
7760 /*&(img->pixmap));*/
7763 image_error ("You dont have MagickExportImagePixels, upgrade ImageMagick!",
7769 #ifdef COLOR_TABLE_SUPPORT
7770 /* Remember colors allocated for this image. */
7771 img
->colors
= colors_in_color_table (&img
->ncolors
);
7772 free_color_table ();
7773 #endif /* COLOR_TABLE_SUPPORT */
7777 img
->height
= height
;
7779 /* Put the image into the pixmap, then free the X image and its
7781 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7782 x_destroy_x_image (ximg
);
7785 /* Final cleanup. image_wand should be the only resource left. */
7786 DestroyMagickWand (image_wand
);
7787 /* `MagickWandTerminus' terminates the imagemagick environment. */
7788 MagickWandTerminus ();
7793 DestroyMagickWand (image_wand
);
7794 MagickWandTerminus ();
7795 /* TODO more cleanup. */
7796 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7801 /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if
7802 successful. this function will go into the imagemagick_type structure, and
7803 the prototype thus needs to be compatible with that structure. */
7806 imagemagick_load (struct frame
*f
,
7810 Lisp_Object file_name
;
7812 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7813 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7814 if (STRINGP (file_name
))
7818 file
= x_find_image_file (file_name
);
7819 if (!STRINGP (file
))
7821 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7824 success_p
= imagemagick_load_image (f
, img
, 0, 0, SDATA (file
));
7826 /* Else its not a file, its a lisp object. Load the image from a
7827 lisp object rather than a file. */
7832 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7833 if (!STRINGP (data
))
7835 image_error ("Invalid image data `%s'", data
, Qnil
);
7838 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
7839 SBYTES (data
), NULL
);
7845 /* Structure describing the image type `imagemagick'. Its the same
7846 type of structure defined for all image formats, handled by Emacs
7847 image functions. See struct image_type in dispextern.h. */
7849 static struct image_type imagemagick_type
=
7851 /* An identifier showing that this is an image structure for the
7852 IMAGEMAGICK format. */
7854 /* Handle to a function that can be used to identify a IMAGEMAGICK
7856 imagemagick_image_p
,
7857 /* Handle to function used to load a IMAGEMAGICK file. */
7859 /* Handle to function to free resources for IMAGEMAGICK. */
7860 imagemagick_clear_image
,
7861 /* An internal field to link to the next image type in a list of
7862 image types, will be filled in when registering the format. */
7869 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
7870 doc
: /* Return image file types supported by ImageMagick.
7871 Since ImageMagick recognizes a lot of file-types that clash with Emacs,
7872 such as .c, we want to be able to alter the list at the lisp level. */)
7875 Lisp_Object typelist
= Qnil
;
7878 char **imtypes
= GetMagickList ("*", &numf
, &ex
);
7880 Lisp_Object Qimagemagicktype
;
7881 for (i
= 0; i
< numf
; i
++)
7883 Qimagemagicktype
= intern (imtypes
[i
]);
7884 typelist
= Fcons (Qimagemagicktype
, typelist
);
7889 #endif /* defined (HAVE_IMAGEMAGICK) */
7893 /***********************************************************************
7895 ***********************************************************************/
7897 #if defined (HAVE_RSVG)
7899 /* Function prototypes. */
7901 static int svg_image_p (Lisp_Object object
);
7902 static int svg_load (struct frame
*f
, struct image
*img
);
7904 static int svg_load_image (struct frame
*, struct image
*,
7905 unsigned char *, unsigned int);
7907 /* The symbol `svg' identifying images of this type. */
7911 /* Indices of image specification fields in svg_format, below. */
7913 enum svg_keyword_index
7928 /* Vector of image_keyword structures describing the format
7929 of valid user-defined image specifications. */
7931 static const struct image_keyword svg_format
[SVG_LAST
] =
7933 {":type", IMAGE_SYMBOL_VALUE
, 1},
7934 {":data", IMAGE_STRING_VALUE
, 0},
7935 {":file", IMAGE_STRING_VALUE
, 0},
7936 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7937 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7938 {":relief", IMAGE_INTEGER_VALUE
, 0},
7939 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7940 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7941 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7942 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7945 /* Structure describing the image type `svg'. Its the same type of
7946 structure defined for all image formats, handled by emacs image
7947 functions. See struct image_type in dispextern.h. */
7949 static struct image_type svg_type
=
7951 /* An identifier showing that this is an image structure for the SVG format. */
7953 /* Handle to a function that can be used to identify a SVG file. */
7955 /* Handle to function used to load a SVG file. */
7957 /* Handle to function to free sresources for SVG. */
7959 /* An internal field to link to the next image type in a list of
7960 image types, will be filled in when registering the format. */
7965 /* Return non-zero if OBJECT is a valid SVG image specification. Do
7966 this by calling parse_image_spec and supplying the keywords that
7967 identify the SVG format. */
7970 svg_image_p (Lisp_Object object
)
7972 struct image_keyword fmt
[SVG_LAST
];
7973 memcpy (fmt
, svg_format
, sizeof fmt
);
7975 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
7978 /* Must specify either the :data or :file keyword. */
7979 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
7982 #include <librsvg/rsvg.h>
7986 /* SVG library functions. */
7987 DEF_IMGLIB_FN (rsvg_handle_new
);
7988 DEF_IMGLIB_FN (rsvg_handle_get_dimensions
);
7989 DEF_IMGLIB_FN (rsvg_handle_write
);
7990 DEF_IMGLIB_FN (rsvg_handle_close
);
7991 DEF_IMGLIB_FN (rsvg_handle_get_pixbuf
);
7992 DEF_IMGLIB_FN (rsvg_handle_free
);
7994 DEF_IMGLIB_FN (gdk_pixbuf_get_width
);
7995 DEF_IMGLIB_FN (gdk_pixbuf_get_height
);
7996 DEF_IMGLIB_FN (gdk_pixbuf_get_pixels
);
7997 DEF_IMGLIB_FN (gdk_pixbuf_get_rowstride
);
7998 DEF_IMGLIB_FN (gdk_pixbuf_get_colorspace
);
7999 DEF_IMGLIB_FN (gdk_pixbuf_get_n_channels
);
8000 DEF_IMGLIB_FN (gdk_pixbuf_get_has_alpha
);
8001 DEF_IMGLIB_FN (gdk_pixbuf_get_bits_per_sample
);
8003 DEF_IMGLIB_FN (g_type_init
);
8004 DEF_IMGLIB_FN (g_object_unref
);
8005 DEF_IMGLIB_FN (g_error_free
);
8007 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8010 init_svg_functions (Lisp_Object libraries
)
8012 HMODULE library
, gdklib
, glib
, gobject
;
8014 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
8015 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
8016 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
8017 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
8020 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8021 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8022 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8023 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8024 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8025 LOAD_IMGLIB_FN (library
, rsvg_handle_free
);
8027 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8028 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8029 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8030 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8031 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8032 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8033 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8034 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8036 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8037 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8038 LOAD_IMGLIB_FN (glib
, g_error_free
);
8044 /* The following aliases for library functions allow dynamic loading
8045 to be used on some platforms. */
8046 #define fn_rsvg_handle_new rsvg_handle_new
8047 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8048 #define fn_rsvg_handle_write rsvg_handle_write
8049 #define fn_rsvg_handle_close rsvg_handle_close
8050 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8051 #define fn_rsvg_handle_free rsvg_handle_free
8053 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8054 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8055 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8056 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8057 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8058 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8059 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8060 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8062 #define fn_g_type_init g_type_init
8063 #define fn_g_object_unref g_object_unref
8064 #define fn_g_error_free g_error_free
8065 #endif /* !HAVE_NTGUI */
8067 /* Load SVG image IMG for use on frame F. Value is non-zero if
8068 successful. this function will go into the svg_type structure, and
8069 the prototype thus needs to be compatible with that structure. */
8072 svg_load (struct frame
*f
, struct image
*img
)
8075 Lisp_Object file_name
;
8077 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8078 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8079 if (STRINGP (file_name
))
8082 unsigned char *contents
;
8085 file
= x_find_image_file (file_name
);
8086 if (!STRINGP (file
))
8088 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8092 /* Read the entire file into memory. */
8093 contents
= slurp_file (SDATA (file
), &size
);
8094 if (contents
== NULL
)
8096 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8099 /* If the file was slurped into memory properly, parse it. */
8100 success_p
= svg_load_image (f
, img
, contents
, size
);
8103 /* Else its not a file, its a lisp object. Load the image from a
8104 lisp object rather than a file. */
8109 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8110 if (!STRINGP (data
))
8112 image_error ("Invalid image data `%s'", data
, Qnil
);
8115 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8121 /* svg_load_image is a helper function for svg_load, which does the
8122 actual loading given contents and size, apart from frame and image
8123 structures, passed from svg_load.
8125 Uses librsvg to do most of the image processing.
8127 Returns non-zero when successful. */
8129 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8130 struct image
*img
, /* Pointer to emacs image structure. */
8131 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8132 unsigned int size
) /* Size of data in bytes. */
8134 RsvgHandle
*rsvg_handle
;
8135 RsvgDimensionData dimension_data
;
8136 GError
*error
= NULL
;
8140 const guint8
*pixels
;
8143 Lisp_Object specified_bg
;
8148 /* g_type_init is a glib function that must be called prior to using
8149 gnome type library functions. */
8151 /* Make a handle to a new rsvg object. */
8152 rsvg_handle
= (RsvgHandle
*) fn_rsvg_handle_new ();
8154 /* Parse the contents argument and fill in the rsvg_handle. */
8155 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &error
);
8156 if (error
) goto rsvg_error
;
8158 /* The parsing is complete, rsvg_handle is ready to used, close it
8159 for further writes. */
8160 fn_rsvg_handle_close (rsvg_handle
, &error
);
8161 if (error
) goto rsvg_error
;
8163 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8164 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8166 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8170 /* We can now get a valid pixel buffer from the svg file, if all
8172 pixbuf
= (GdkPixbuf
*) fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8173 if (!pixbuf
) goto rsvg_error
;
8174 fn_g_object_unref (rsvg_handle
);
8176 /* Extract some meta data from the svg handle. */
8177 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8178 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8179 pixels
= (const guint8
*) fn_gdk_pixbuf_get_pixels (pixbuf
);
8180 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8182 /* Validate the svg meta data. */
8183 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8184 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8185 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8186 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8188 /* Try to create a x pixmap to hold the svg pixmap. */
8189 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8191 fn_g_object_unref (pixbuf
);
8195 init_color_table ();
8197 /* Handle alpha channel by combining the image with a background
8199 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8200 if (!STRINGP (specified_bg
)
8201 || !x_defined_color (f
, SDATA (specified_bg
), &background
, 0))
8204 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8205 x_query_color (f
, &background
);
8207 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8211 /* SVG pixmaps specify transparency in the last byte, so right
8212 shift 8 bits to get rid of it, since emacs doesn't support
8214 background
.red
>>= 8;
8215 background
.green
>>= 8;
8216 background
.blue
>>= 8;
8218 /* This loop handles opacity values, since Emacs assumes
8219 non-transparent images. Each pixel must be "flattened" by
8220 calculating the resulting color, given the transparency of the
8221 pixel, and the image background color. */
8222 for (y
= 0; y
< height
; ++y
)
8224 for (x
= 0; x
< width
; ++x
)
8234 opacity
= *pixels
++;
8236 red
= ((red
* opacity
)
8237 + (background
.red
* ((1 << 8) - opacity
)));
8238 green
= ((green
* opacity
)
8239 + (background
.green
* ((1 << 8) - opacity
)));
8240 blue
= ((blue
* opacity
)
8241 + (background
.blue
* ((1 << 8) - opacity
)));
8243 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8246 pixels
+= rowstride
- 4 * width
;
8249 #ifdef COLOR_TABLE_SUPPORT
8250 /* Remember colors allocated for this image. */
8251 img
->colors
= colors_in_color_table (&img
->ncolors
);
8252 free_color_table ();
8253 #endif /* COLOR_TABLE_SUPPORT */
8255 fn_g_object_unref (pixbuf
);
8258 img
->height
= height
;
8260 /* Maybe fill in the background field while we have ximg handy.
8261 Casting avoids a GCC warning. */
8262 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8264 /* Put the image into the pixmap, then free the X image and its
8266 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8267 x_destroy_x_image (ximg
);
8272 fn_g_object_unref (rsvg_handle
);
8273 /* FIXME: Use error->message so the user knows what is the actual
8274 problem with the image. */
8275 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8276 fn_g_error_free (error
);
8280 #endif /* defined (HAVE_RSVG) */
8285 /***********************************************************************
8287 ***********************************************************************/
8289 #ifdef HAVE_X_WINDOWS
8290 #define HAVE_GHOSTSCRIPT 1
8291 #endif /* HAVE_X_WINDOWS */
8293 #ifdef HAVE_GHOSTSCRIPT
8295 static int gs_image_p (Lisp_Object object
);
8296 static int gs_load (struct frame
*f
, struct image
*img
);
8297 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8299 /* Keyword symbols. */
8301 Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8303 /* Indices of image specification fields in gs_format, below. */
8305 enum gs_keyword_index
8323 /* Vector of image_keyword structures describing the format
8324 of valid user-defined image specifications. */
8326 static const struct image_keyword gs_format
[GS_LAST
] =
8328 {":type", IMAGE_SYMBOL_VALUE
, 1},
8329 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8330 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8331 {":file", IMAGE_STRING_VALUE
, 1},
8332 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8333 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8334 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8335 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
8336 {":relief", IMAGE_INTEGER_VALUE
, 0},
8337 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8338 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8339 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8340 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8343 /* Structure describing the image type `ghostscript'. */
8345 static struct image_type gs_type
=
8355 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8358 gs_clear_image (struct frame
*f
, struct image
*img
)
8360 /* IMG->data.ptr_val may contain a recorded colormap. */
8361 xfree (img
->data
.ptr_val
);
8362 x_clear_image (f
, img
);
8366 /* Return non-zero if OBJECT is a valid Ghostscript image
8370 gs_image_p (Lisp_Object object
)
8372 struct image_keyword fmt
[GS_LAST
];
8376 memcpy (fmt
, gs_format
, sizeof fmt
);
8378 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8381 /* Bounding box must be a list or vector containing 4 integers. */
8382 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8385 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8386 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8391 else if (VECTORP (tem
))
8393 if (XVECTOR (tem
)->size
!= 4)
8395 for (i
= 0; i
< 4; ++i
)
8396 if (!INTEGERP (XVECTOR (tem
)->contents
[i
]))
8406 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8410 gs_load (struct frame
*f
, struct image
*img
)
8413 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8415 double in_width
, in_height
;
8416 Lisp_Object pixel_colors
= Qnil
;
8418 /* Compute pixel size of pixmap needed from the given size in the
8419 image specification. Sizes in the specification are in pt. 1 pt
8420 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8422 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8423 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8424 img
->width
= in_width
* FRAME_X_DISPLAY_INFO (f
)->resx
;
8425 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8426 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8427 img
->height
= in_height
* FRAME_X_DISPLAY_INFO (f
)->resy
;
8429 if (!check_image_size (f
, img
->width
, img
->height
))
8431 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8435 /* Create the pixmap. */
8436 xassert (img
->pixmap
== NO_PIXMAP
);
8438 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8440 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8441 img
->width
, img
->height
,
8442 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8447 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8451 /* Call the loader to fill the pixmap. It returns a process object
8452 if successful. We do not record_unwind_protect here because
8453 other places in redisplay like calling window scroll functions
8454 don't either. Let the Lisp loader use `unwind-protect' instead. */
8455 sprintf (buffer
, "%lu %lu",
8456 (unsigned long) FRAME_X_WINDOW (f
),
8457 (unsigned long) img
->pixmap
);
8458 window_and_pixmap_id
= build_string (buffer
);
8460 sprintf (buffer
, "%lu %lu",
8461 FRAME_FOREGROUND_PIXEL (f
),
8462 FRAME_BACKGROUND_PIXEL (f
));
8463 pixel_colors
= build_string (buffer
);
8465 XSETFRAME (frame
, f
);
8466 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8468 loader
= intern ("gs-load-image");
8470 img
->data
.lisp_val
= call6 (loader
, frame
, img
->spec
,
8471 make_number (img
->width
),
8472 make_number (img
->height
),
8473 window_and_pixmap_id
,
8475 return PROCESSP (img
->data
.lisp_val
);
8479 /* Kill the Ghostscript process that was started to fill PIXMAP on
8480 frame F. Called from XTread_socket when receiving an event
8481 telling Emacs that Ghostscript has finished drawing. */
8484 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8486 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8490 /* Find the image containing PIXMAP. */
8491 for (i
= 0; i
< c
->used
; ++i
)
8492 if (c
->images
[i
]->pixmap
== pixmap
)
8495 /* Should someone in between have cleared the image cache, for
8496 instance, give up. */
8500 /* Kill the GS process. We should have found PIXMAP in the image
8501 cache and its image should contain a process object. */
8503 xassert (PROCESSP (img
->data
.lisp_val
));
8504 Fkill_process (img
->data
.lisp_val
, Qnil
);
8505 img
->data
.lisp_val
= Qnil
;
8507 #if defined (HAVE_X_WINDOWS)
8509 /* On displays with a mutable colormap, figure out the colors
8510 allocated for the image by looking at the pixels of an XImage for
8512 class = FRAME_X_VISUAL (f
)->class;
8513 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8519 /* Try to get an XImage for img->pixmep. */
8520 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8521 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8526 /* Initialize the color table. */
8527 init_color_table ();
8529 /* For each pixel of the image, look its color up in the
8530 color table. After having done so, the color table will
8531 contain an entry for each color used by the image. */
8532 for (y
= 0; y
< img
->height
; ++y
)
8533 for (x
= 0; x
< img
->width
; ++x
)
8535 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8536 lookup_pixel_color (f
, pixel
);
8539 /* Record colors in the image. Free color table and XImage. */
8540 #ifdef COLOR_TABLE_SUPPORT
8541 img
->colors
= colors_in_color_table (&img
->ncolors
);
8542 free_color_table ();
8544 XDestroyImage (ximg
);
8546 #if 0 /* This doesn't seem to be the case. If we free the colors
8547 here, we get a BadAccess later in x_clear_image when
8548 freeing the colors. */
8549 /* We have allocated colors once, but Ghostscript has also
8550 allocated colors on behalf of us. So, to get the
8551 reference counts right, free them once. */
8553 x_free_colors (f
, img
->colors
, img
->ncolors
);
8557 image_error ("Cannot get X image of `%s'; colors will not be freed",
8562 #endif /* HAVE_X_WINDOWS */
8564 /* Now that we have the pixmap, compute mask and transform the
8565 image if requested. */
8567 postprocess_image (f
, img
);
8571 #endif /* HAVE_GHOSTSCRIPT */
8574 /***********************************************************************
8576 ***********************************************************************/
8580 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8581 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8584 return valid_image_p (spec
) ? Qt
: Qnil
;
8588 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8593 if (valid_image_p (spec
))
8594 id
= lookup_image (SELECTED_FRAME (), spec
);
8597 return make_number (id
);
8600 #endif /* GLYPH_DEBUG != 0 */
8603 /***********************************************************************
8605 ***********************************************************************/
8608 /* Image types that rely on external libraries are loaded dynamically
8609 if the library is available. */
8610 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8611 define_image_type (image_type, init_lib_fn (libraries))
8613 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8614 define_image_type (image_type, 1)
8615 #endif /* HAVE_NTGUI */
8617 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8618 doc
: /* Initialize image library implementing image type TYPE.
8619 Return non-nil if TYPE is a supported image type.
8621 Image types pbm and xbm are prebuilt; other types are loaded here.
8622 Libraries to load are specified in alist LIBRARIES (usually, the value
8623 of `dynamic-library-alist', which see). */)
8624 (Lisp_Object type
, Lisp_Object libraries
)
8628 /* Don't try to reload the library. */
8629 tested
= Fassq (type
, Vimage_type_cache
);
8631 return XCDR (tested
);
8633 #if defined (HAVE_XPM) || defined (HAVE_NS)
8634 if (EQ (type
, Qxpm
))
8635 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8638 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8639 if (EQ (type
, Qjpeg
))
8640 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8643 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8644 if (EQ (type
, Qtiff
))
8645 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8648 #if defined (HAVE_GIF) || defined (HAVE_NS)
8649 if (EQ (type
, Qgif
))
8650 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8653 #if defined (HAVE_PNG) || defined (HAVE_NS)
8654 if (EQ (type
, Qpng
))
8655 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8658 #if defined (HAVE_RSVG)
8659 if (EQ (type
, Qsvg
))
8660 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8663 #if defined (HAVE_IMAGEMAGICK)
8664 if (EQ (type
, Qimagemagick
))
8666 return CHECK_LIB_AVAILABLE (&imagemagick_type
, init_imagemagick_functions
,
8671 #ifdef HAVE_GHOSTSCRIPT
8672 if (EQ (type
, Qpostscript
))
8673 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8676 /* If the type is not recognized, avoid testing it ever again. */
8677 CACHE_IMAGE_TYPE (type
, Qnil
);
8682 syms_of_image (void)
8684 /* Initialize this only once, since that's what we do with Vimage_types
8685 and they are supposed to be in sync. Initializing here gives correct
8686 operation on GNU/Linux of calling dump-emacs after loading some images. */
8689 /* Must be defined now becase we're going to update it below, while
8690 defining the supported image types. */
8691 DEFVAR_LISP ("image-types", Vimage_types
,
8692 doc
: /* List of potentially supported image types.
8693 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8694 To check whether it is really supported, use `image-type-available-p'. */);
8695 Vimage_types
= Qnil
;
8697 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8698 doc
: /* Maximum size of images.
8699 Emacs will not load an image into memory if its pixel width or
8700 pixel height exceeds this limit.
8702 If the value is an integer, it directly specifies the maximum
8703 image height and width, measured in pixels. If it is a floating
8704 point number, it specifies the maximum image height and width
8705 as a ratio to the frame height and width. If the value is
8706 non-numeric, there is no explicit limit on the size of images. */);
8707 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8709 Vimage_type_cache
= Qnil
;
8710 staticpro (&Vimage_type_cache
);
8712 Qpbm
= intern_c_string ("pbm");
8714 ADD_IMAGE_TYPE (Qpbm
);
8716 Qxbm
= intern_c_string ("xbm");
8718 ADD_IMAGE_TYPE (Qxbm
);
8720 define_image_type (&xbm_type
, 1);
8721 define_image_type (&pbm_type
, 1);
8723 Qcount
= intern_c_string ("count");
8724 staticpro (&Qcount
);
8725 Qextension_data
= intern_c_string ("extension-data");
8726 staticpro (&Qextension_data
);
8728 QCascent
= intern_c_string (":ascent");
8729 staticpro (&QCascent
);
8730 QCmargin
= intern_c_string (":margin");
8731 staticpro (&QCmargin
);
8732 QCrelief
= intern_c_string (":relief");
8733 staticpro (&QCrelief
);
8734 QCconversion
= intern_c_string (":conversion");
8735 staticpro (&QCconversion
);
8736 QCcolor_symbols
= intern_c_string (":color-symbols");
8737 staticpro (&QCcolor_symbols
);
8738 QCheuristic_mask
= intern_c_string (":heuristic-mask");
8739 staticpro (&QCheuristic_mask
);
8740 QCindex
= intern_c_string (":index");
8741 staticpro (&QCindex
);
8742 QCgeometry
= intern_c_string (":geometry");
8743 staticpro (&QCgeometry
);
8744 QCcrop
= intern_c_string (":crop");
8745 staticpro (&QCcrop
);
8746 QCrotation
= intern_c_string (":rotation");
8747 staticpro (&QCrotation
);
8748 QCmatrix
= intern_c_string (":matrix");
8749 staticpro (&QCmatrix
);
8750 QCcolor_adjustment
= intern_c_string (":color-adjustment");
8751 staticpro (&QCcolor_adjustment
);
8752 QCmask
= intern_c_string (":mask");
8753 staticpro (&QCmask
);
8755 Qlaplace
= intern_c_string ("laplace");
8756 staticpro (&Qlaplace
);
8757 Qemboss
= intern_c_string ("emboss");
8758 staticpro (&Qemboss
);
8759 Qedge_detection
= intern_c_string ("edge-detection");
8760 staticpro (&Qedge_detection
);
8761 Qheuristic
= intern_c_string ("heuristic");
8762 staticpro (&Qheuristic
);
8764 Qpostscript
= intern_c_string ("postscript");
8765 staticpro (&Qpostscript
);
8766 #ifdef HAVE_GHOSTSCRIPT
8767 ADD_IMAGE_TYPE (Qpostscript
);
8768 QCloader
= intern_c_string (":loader");
8769 staticpro (&QCloader
);
8770 QCbounding_box
= intern_c_string (":bounding-box");
8771 staticpro (&QCbounding_box
);
8772 QCpt_width
= intern_c_string (":pt-width");
8773 staticpro (&QCpt_width
);
8774 QCpt_height
= intern_c_string (":pt-height");
8775 staticpro (&QCpt_height
);
8776 #endif /* HAVE_GHOSTSCRIPT */
8779 Qlibpng_version
= intern_c_string ("libpng-version");
8780 staticpro (&Qlibpng_version
);
8781 Fset (Qlibpng_version
,
8783 make_number (PNG_LIBPNG_VER
)
8790 #if defined (HAVE_XPM) || defined (HAVE_NS)
8791 Qxpm
= intern_c_string ("xpm");
8793 ADD_IMAGE_TYPE (Qxpm
);
8796 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8797 Qjpeg
= intern_c_string ("jpeg");
8799 ADD_IMAGE_TYPE (Qjpeg
);
8802 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8803 Qtiff
= intern_c_string ("tiff");
8805 ADD_IMAGE_TYPE (Qtiff
);
8808 #if defined (HAVE_GIF) || defined (HAVE_NS)
8809 Qgif
= intern_c_string ("gif");
8811 ADD_IMAGE_TYPE (Qgif
);
8814 #if defined (HAVE_PNG) || defined (HAVE_NS)
8815 Qpng
= intern_c_string ("png");
8817 ADD_IMAGE_TYPE (Qpng
);
8820 #if defined (HAVE_IMAGEMAGICK)
8821 Qimagemagick
= intern_c_string ("imagemagick");
8822 staticpro (&Qimagemagick
);
8823 ADD_IMAGE_TYPE (Qimagemagick
);
8826 #if defined (HAVE_RSVG)
8827 Qsvg
= intern_c_string ("svg");
8829 ADD_IMAGE_TYPE (Qsvg
);
8831 /* Other libraries used directly by svg code. */
8832 Qgdk_pixbuf
= intern_c_string ("gdk-pixbuf");
8833 staticpro (&Qgdk_pixbuf
);
8834 Qglib
= intern_c_string ("glib");
8836 Qgobject
= intern_c_string ("gobject");
8837 staticpro (&Qgobject
);
8838 #endif /* HAVE_NTGUI */
8839 #endif /* HAVE_RSVG */
8841 defsubr (&Sinit_image_library
);
8842 #ifdef HAVE_IMAGEMAGICK
8843 defsubr (&Simagemagick_types
);
8845 defsubr (&Sclear_image_cache
);
8846 defsubr (&Simage_flush
);
8847 defsubr (&Simage_size
);
8848 defsubr (&Simage_mask_p
);
8849 defsubr (&Simage_metadata
);
8853 defsubr (&Slookup_image
);
8856 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8857 doc
: /* Non-nil means always draw a cross over disabled images.
8858 Disabled images are those having a `:conversion disabled' property.
8859 A cross is always drawn on black & white displays. */);
8860 cross_disabled_images
= 0;
8862 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8863 doc
: /* List of directories to search for window system bitmap files. */);
8864 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8866 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
8867 doc
: /* Maximum time after which images are removed from the cache.
8868 When an image has not been displayed this many seconds, Emacs
8869 automatically removes it from the image cache. If the cache contains
8870 a large number of images, the actual eviction time may be shorter.
8871 The value can also be nil, meaning the cache is never cleared.
8873 The function `clear-image-cache' disregards this variable. */);
8874 Vimage_cache_eviction_delay
= make_number (300);
8875 #ifdef HAVE_IMAGEMAGICK
8876 DEFVAR_LISP ("imagemagick-render-type", Vimagemagick_render_type
,
8877 doc
: /* Choose between ImageMagick render methods. */);