1 /* Functions for image support on window system.
2 Copyright (C) 1989, 1992-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #if defined HAVE_LIBPNG_PNG_H
27 # include <libpng/png.h>
35 /* This makes the fields of a Display accessible, in Xlib header files. */
37 #define XLIB_ILLEGAL_ACCESS
42 #include "dispextern.h"
43 #include "blockinput.h"
46 #include "character.h"
48 #include "termhooks.h"
53 #include <sys/types.h>
56 #define COLOR_TABLE_SUPPORT 1
58 typedef struct x_bitmap_record Bitmap_Record
;
59 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
60 #define NO_PIXMAP None
62 #define RGB_PIXEL_COLOR unsigned long
64 #define PIX_MASK_RETAIN 0
65 #define PIX_MASK_DRAW 1
66 #endif /* HAVE_X_WINDOWS */
73 /* W32_TODO : Color tables on W32. */
74 #undef COLOR_TABLE_SUPPORT
76 typedef struct w32_bitmap_record Bitmap_Record
;
77 #define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y)
80 #define RGB_PIXEL_COLOR COLORREF
82 #define PIX_MASK_RETAIN 0
83 #define PIX_MASK_DRAW 1
85 #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual
86 #define x_defined_color w32_defined_color
87 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
89 /* Functions from w32term.c that depend on XColor (so can't go in w32term.h
90 without modifying lots of files). */
91 extern void x_query_colors (struct frame
*f
, XColor
*colors
, int ncolors
);
92 extern void x_query_color (struct frame
*f
, XColor
*color
);
94 /* Version of libpng that we were compiled with, or -1 if no PNG
95 support was compiled in. This is tested by w32-win.el to correctly
96 set up the alist used to search for PNG libraries. */
97 Lisp_Object Qlibpng_version
;
98 #endif /* HAVE_NTGUI */
102 #include <sys/types.h>
103 #include <sys/stat.h>
105 #undef COLOR_TABLE_SUPPORT
107 typedef struct ns_bitmap_record Bitmap_Record
;
109 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
112 #define RGB_PIXEL_COLOR unsigned long
115 #define PIX_MASK_RETAIN 0
116 #define PIX_MASK_DRAW 1
118 #define FRAME_X_VISUAL FRAME_NS_DISPLAY_INFO(f)->visual
119 #define x_defined_color(f, name, color_def, alloc) \
120 ns_defined_color (f, name, color_def, alloc, 0)
121 #define FRAME_X_SCREEN(f) 0
122 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
126 /* The symbol `postscript' identifying images of this type. */
128 static Lisp_Object Qpostscript
;
130 static void x_disable_image (struct frame
*, struct image
*);
131 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
134 static void init_color_table (void);
135 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
136 #ifdef COLOR_TABLE_SUPPORT
137 static void free_color_table (void);
138 static unsigned long *colors_in_color_table (int *n
);
140 static Lisp_Object
Finit_image_library (Lisp_Object
, Lisp_Object
);
142 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
143 id, which is just an int that this section returns. Bitmaps are
144 reference counted so they can be shared among frames.
146 Bitmap indices are guaranteed to be > 0, so a negative number can
147 be used to indicate no bitmap.
149 If you use x_create_bitmap_from_data, then you must keep track of
150 the bitmaps yourself. That is, creating a bitmap from the same
151 data more than once will not be caught. */
155 XGetImage (Display
*display
, Pixmap pixmap
, int x
, int y
,
156 unsigned int width
, unsigned int height
,
157 unsigned long plane_mask
, int format
)
159 /* TODO: not sure what this function is supposed to do.. */
160 ns_retain_object (pixmap
);
164 /* use with imgs created by ns_image_for_XPM */
166 XGetPixel (XImagePtr ximage
, int x
, int y
)
168 return ns_get_pixel (ximage
, x
, y
);
171 /* use with imgs created by ns_image_for_XPM; alpha set to 1;
172 pixel is assumed to be in form RGB */
174 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
176 ns_put_pixel (ximage
, x
, y
, pixel
);
181 /* Functions to access the contents of a bitmap, given an id. */
184 x_bitmap_height (FRAME_PTR f
, ptrdiff_t id
)
186 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
190 x_bitmap_width (FRAME_PTR f
, ptrdiff_t id
)
192 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
195 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
197 x_bitmap_pixmap (FRAME_PTR f
, ptrdiff_t id
)
199 return (int) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
203 #ifdef HAVE_X_WINDOWS
205 x_bitmap_mask (FRAME_PTR f
, ptrdiff_t id
)
207 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
211 /* Allocate a new bitmap record. Returns index of new record. */
214 x_allocate_bitmap_record (FRAME_PTR f
)
216 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
219 if (dpyinfo
->bitmaps
== NULL
)
221 dpyinfo
->bitmaps_size
= 10;
223 = (Bitmap_Record
*) xmalloc (dpyinfo
->bitmaps_size
* sizeof (Bitmap_Record
));
224 dpyinfo
->bitmaps_last
= 1;
228 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
229 return ++dpyinfo
->bitmaps_last
;
231 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
232 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
235 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (Bitmap_Record
) / 2
236 < dpyinfo
->bitmaps_size
)
237 memory_full (SIZE_MAX
);
238 dpyinfo
->bitmaps_size
*= 2;
240 = (Bitmap_Record
*) xrealloc (dpyinfo
->bitmaps
,
241 dpyinfo
->bitmaps_size
* sizeof (Bitmap_Record
));
242 return ++dpyinfo
->bitmaps_last
;
245 /* Add one reference to the reference count of the bitmap with id ID. */
248 x_reference_bitmap (FRAME_PTR f
, int id
)
250 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
253 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
256 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
258 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
261 #ifdef HAVE_X_WINDOWS
263 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
264 bits
, width
, height
);
267 #endif /* HAVE_X_WINDOWS */
271 bitmap
= CreateBitmap (width
, height
,
272 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
273 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
277 #endif /* HAVE_NTGUI */
280 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
285 id
= x_allocate_bitmap_record (f
);
288 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
289 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
292 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
293 dpyinfo
->bitmaps
[id
- 1].height
= height
;
294 dpyinfo
->bitmaps
[id
- 1].width
= width
;
295 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
297 #ifdef HAVE_X_WINDOWS
298 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
299 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
300 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
301 #endif /* HAVE_X_WINDOWS */
304 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
305 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
306 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
307 #endif /* HAVE_NTGUI */
312 /* Create bitmap from file FILE for frame F. */
315 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
317 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
320 return -1; /* W32_TODO : bitmap support */
321 #endif /* HAVE_NTGUI */
325 void *bitmap
= ns_image_from_file (file
);
331 id
= x_allocate_bitmap_record (f
);
332 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
333 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
334 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
335 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
336 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
337 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
338 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
342 #ifdef HAVE_X_WINDOWS
343 unsigned int width
, height
;
345 int xhot
, yhot
, result
;
351 /* Look for an existing bitmap with the same name. */
352 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
354 if (dpyinfo
->bitmaps
[id
].refcount
355 && dpyinfo
->bitmaps
[id
].file
356 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
358 ++dpyinfo
->bitmaps
[id
].refcount
;
363 /* Search bitmap-file-path for the file, if appropriate. */
364 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
369 filename
= SSDATA (found
);
371 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
372 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
373 if (result
!= BitmapSuccess
)
376 id
= x_allocate_bitmap_record (f
);
377 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
378 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
379 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
380 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
381 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
382 dpyinfo
->bitmaps
[id
- 1].height
= height
;
383 dpyinfo
->bitmaps
[id
- 1].width
= width
;
384 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
387 #endif /* HAVE_X_WINDOWS */
393 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
395 #ifdef HAVE_X_WINDOWS
396 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
398 XFreePixmap (dpyinfo
->display
, bm
->mask
);
399 #endif /* HAVE_X_WINDOWS */
402 DeleteObject (bm
->pixmap
);
403 #endif /* HAVE_NTGUI */
406 ns_release_object (bm
->img
);
416 /* Remove reference to bitmap with id number ID. */
419 x_destroy_bitmap (FRAME_PTR f
, ptrdiff_t id
)
421 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
425 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
427 if (--bm
->refcount
== 0)
430 free_bitmap_record (dpyinfo
, bm
);
436 /* Free all the bitmaps for the display specified by DPYINFO. */
439 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
442 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
444 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
445 if (bm
->refcount
> 0)
446 free_bitmap_record (dpyinfo
, bm
);
448 dpyinfo
->bitmaps_last
= 0;
452 #ifdef HAVE_X_WINDOWS
454 /* Useful functions defined in the section
455 `Image type independent image structures' below. */
457 static unsigned long four_corners_best (XImagePtr ximg
,
460 unsigned long height
);
462 static int x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
,
463 int depth
, XImagePtr
*ximg
,
466 static void x_destroy_x_image (XImagePtr ximg
);
469 /* Create a mask of a bitmap. Note is this not a perfect mask.
470 It's nicer with some borders in this context */
473 x_create_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
476 XImagePtr ximg
, mask_img
;
477 unsigned long width
, height
;
480 unsigned long x
, y
, xp
, xm
, yp
, ym
;
483 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
488 pixmap
= x_bitmap_pixmap (f
, id
);
489 width
= x_bitmap_width (f
, id
);
490 height
= x_bitmap_height (f
, id
);
493 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
502 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
507 XDestroyImage (ximg
);
511 bg
= four_corners_best (ximg
, NULL
, width
, height
);
513 for (y
= 0; y
< ximg
->height
; ++y
)
515 for (x
= 0; x
< ximg
->width
; ++x
)
517 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
518 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
519 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
520 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
521 if (XGetPixel (ximg
, x
, y
) == bg
522 && XGetPixel (ximg
, x
, yp
) == bg
523 && XGetPixel (ximg
, x
, ym
) == bg
524 && XGetPixel (ximg
, xp
, y
) == bg
525 && XGetPixel (ximg
, xp
, yp
) == bg
526 && XGetPixel (ximg
, xp
, ym
) == bg
527 && XGetPixel (ximg
, xm
, y
) == bg
528 && XGetPixel (ximg
, xm
, yp
) == bg
529 && XGetPixel (ximg
, xm
, ym
) == bg
)
530 XPutPixel (mask_img
, x
, y
, 0);
532 XPutPixel (mask_img
, x
, y
, 1);
536 xassert (interrupt_input_blocked
);
537 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
538 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
540 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
542 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
543 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
545 XDestroyImage (ximg
);
546 x_destroy_x_image (mask_img
);
551 #endif /* HAVE_X_WINDOWS */
554 /***********************************************************************
556 ***********************************************************************/
558 /* List of supported image types. Use define_image_type to add new
559 types. Use lookup_image_type to find a type for a given symbol. */
561 static struct image_type
*image_types
;
563 /* The symbol `xbm' which is used as the type symbol for XBM images. */
565 static Lisp_Object Qxbm
;
569 Lisp_Object QCascent
, QCmargin
, QCrelief
;
570 Lisp_Object QCconversion
;
571 static Lisp_Object QCheuristic_mask
;
572 static Lisp_Object QCcolor_symbols
;
573 static Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
;
574 static Lisp_Object QCcrop
, QCrotation
;
578 static Lisp_Object Qcount
, Qextension_data
, Qdelay
;
579 static Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
581 /* Function prototypes. */
583 static Lisp_Object
define_image_type (struct image_type
*type
, int loaded
);
584 static struct image_type
*lookup_image_type (Lisp_Object symbol
);
585 static void image_error (const char *format
, Lisp_Object
, Lisp_Object
);
586 static void x_laplace (struct frame
*, struct image
*);
587 static void x_emboss (struct frame
*, struct image
*);
588 static int x_build_heuristic_mask (struct frame
*, struct image
*,
591 #define CACHE_IMAGE_TYPE(type, status) \
592 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
594 #define CACHE_IMAGE_TYPE(type, status)
597 #define ADD_IMAGE_TYPE(type) \
598 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
600 /* Define a new image type from TYPE. This adds a copy of TYPE to
601 image_types and caches the loading status of TYPE. */
604 define_image_type (struct image_type
*type
, int loaded
)
612 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
613 The initialized data segment is read-only. */
614 struct image_type
*p
= (struct image_type
*) xmalloc (sizeof *p
);
615 memcpy (p
, type
, sizeof *p
);
616 p
->next
= image_types
;
621 CACHE_IMAGE_TYPE (*type
->type
, success
);
626 /* Look up image type SYMBOL, and return a pointer to its image_type
627 structure. Value is null if SYMBOL is not a known image type. */
629 static inline struct image_type
*
630 lookup_image_type (Lisp_Object symbol
)
632 struct image_type
*type
;
634 /* We must initialize the image-type if it hasn't been already. */
635 if (NILP (Finit_image_library (symbol
, Vdynamic_library_alist
)))
636 return 0; /* unimplemented */
638 for (type
= image_types
; type
; type
= type
->next
)
639 if (EQ (symbol
, *type
->type
))
646 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
647 valid image specification is a list whose car is the symbol
648 `image', and whose rest is a property list. The property list must
649 contain a value for key `:type'. That value must be the name of a
650 supported image type. The rest of the property list depends on the
654 valid_image_p (Lisp_Object object
)
662 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
663 if (EQ (XCAR (tem
), QCtype
))
666 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
668 struct image_type
*type
;
669 type
= lookup_image_type (XCAR (tem
));
671 valid_p
= type
->valid_p (object
);
682 /* Log error message with format string FORMAT and argument ARG.
683 Signaling an error, e.g. when an image cannot be loaded, is not a
684 good idea because this would interrupt redisplay, and the error
685 message display would lead to another redisplay. This function
686 therefore simply displays a message. */
689 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
691 add_to_log (format
, arg1
, arg2
);
696 /***********************************************************************
698 ***********************************************************************/
700 enum image_value_type
702 IMAGE_DONT_CHECK_VALUE_TYPE
,
704 IMAGE_STRING_OR_NIL_VALUE
,
706 IMAGE_POSITIVE_INTEGER_VALUE
,
707 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
,
708 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
711 IMAGE_FUNCTION_VALUE
,
716 /* Structure used when parsing image specifications. */
720 /* Name of keyword. */
723 /* The type of value allowed. */
724 enum image_value_type type
;
726 /* Non-zero means key must be present. */
729 /* Used to recognize duplicate keywords in a property list. */
732 /* The value that was found. */
737 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
739 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
742 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
743 has the format (image KEYWORD VALUE ...). One of the keyword/
744 value pairs must be `:type TYPE'. KEYWORDS is a vector of
745 image_keywords structures of size NKEYWORDS describing other
746 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
749 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
750 int nkeywords
, Lisp_Object type
)
759 while (CONSP (plist
))
761 Lisp_Object key
, value
;
763 /* First element of a pair must be a symbol. */
765 plist
= XCDR (plist
);
769 /* There must follow a value. */
772 value
= XCAR (plist
);
773 plist
= XCDR (plist
);
775 /* Find key in KEYWORDS. Error if not found. */
776 for (i
= 0; i
< nkeywords
; ++i
)
777 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
783 /* Record that we recognized the keyword. If a keywords
784 was found more than once, it's an error. */
785 keywords
[i
].value
= value
;
788 if (keywords
[i
].count
> 1)
791 /* Check type of value against allowed type. */
792 switch (keywords
[i
].type
)
794 case IMAGE_STRING_VALUE
:
795 if (!STRINGP (value
))
799 case IMAGE_STRING_OR_NIL_VALUE
:
800 if (!STRINGP (value
) && !NILP (value
))
804 case IMAGE_SYMBOL_VALUE
:
805 if (!SYMBOLP (value
))
809 case IMAGE_POSITIVE_INTEGER_VALUE
:
810 if (!INTEGERP (value
) || XINT (value
) <= 0)
814 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
:
815 if (INTEGERP (value
) && XINT (value
) >= 0)
818 && INTEGERP (XCAR (value
)) && INTEGERP (XCDR (value
))
819 && XINT (XCAR (value
)) >= 0 && XINT (XCDR (value
)) >= 0)
823 case IMAGE_ASCENT_VALUE
:
824 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
826 else if (INTEGERP (value
)
828 && XINT (value
) <= 100)
832 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
833 if (!INTEGERP (value
) || XINT (value
) < 0)
837 case IMAGE_DONT_CHECK_VALUE_TYPE
:
840 case IMAGE_FUNCTION_VALUE
:
841 value
= indirect_function (value
);
842 if (!NILP (Ffunctionp (value
)))
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
->lisp_data
;
984 /***********************************************************************
985 Image type independent image structures
986 ***********************************************************************/
988 static void free_image (struct frame
*f
, struct image
*img
);
990 #define MAX_IMAGE_SIZE 6.0
991 /* Allocate and return a new image structure for image specification
992 SPEC. SPEC has a hash value of HASH. */
994 static struct image
*
995 make_image (Lisp_Object spec
, EMACS_UINT hash
)
997 struct image
*img
= (struct image
*) xmalloc (sizeof *img
);
998 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
1000 xassert (valid_image_p (spec
));
1001 memset (img
, 0, sizeof *img
);
1002 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
1003 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
1004 xassert (img
->type
!= NULL
);
1006 img
->lisp_data
= Qnil
;
1007 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
1009 img
->corners
[BOT_CORNER
] = -1; /* Full image */
1014 /* Free image IMG which was used on frame F, including its resources. */
1017 free_image (struct frame
*f
, struct image
*img
)
1021 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1023 /* Remove IMG from the hash table of its cache. */
1025 img
->prev
->next
= img
->next
;
1027 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1030 img
->next
->prev
= img
->prev
;
1032 c
->images
[img
->id
] = NULL
;
1034 /* Free resources, then free IMG. */
1035 img
->type
->free (f
, img
);
1040 /* Return 1 if the given widths and heights are valid for display;
1041 otherwise, return 0. */
1044 check_image_size (struct frame
*f
, int width
, int height
)
1048 if (width
<= 0 || height
<= 0)
1051 if (INTEGERP (Vmax_image_size
))
1052 return (width
<= XINT (Vmax_image_size
)
1053 && height
<= XINT (Vmax_image_size
));
1054 else if (FLOATP (Vmax_image_size
))
1058 w
= FRAME_PIXEL_WIDTH (f
);
1059 h
= FRAME_PIXEL_HEIGHT (f
);
1062 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1063 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1064 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1070 /* Prepare image IMG for display on frame F. Must be called before
1071 drawing an image. */
1074 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1078 /* We're about to display IMG, so set its timestamp to `now'. */
1080 img
->timestamp
= EMACS_SECS (t
);
1082 /* If IMG doesn't have a pixmap yet, load it now, using the image
1083 type dependent loader function. */
1084 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1085 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1090 /* Value is the number of pixels for the ascent of image IMG when
1091 drawn in face FACE. */
1094 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1099 if (slice
->height
== img
->height
)
1100 height
= img
->height
+ img
->vmargin
;
1101 else if (slice
->y
== 0)
1102 height
= slice
->height
+ img
->vmargin
;
1104 height
= slice
->height
;
1106 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1111 /* W32 specific version. Why?. ++kfs */
1112 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1113 - FONT_BASE (face
->font
)) / 2;
1115 /* This expression is arranged so that if the image can't be
1116 exactly centered, it will be moved slightly up. This is
1117 because a typical font is `top-heavy' (due to the presence
1118 uppercase letters), so the image placement should err towards
1119 being top-heavy too. It also just generally looks better. */
1120 ascent
= (height
+ FONT_BASE(face
->font
)
1121 - FONT_DESCENT(face
->font
) + 1) / 2;
1122 #endif /* HAVE_NTGUI */
1125 ascent
= height
/ 2;
1128 ascent
= (int) (height
* img
->ascent
/ 100.0);
1134 /* Image background colors. */
1136 /* Find the "best" corner color of a bitmap.
1137 On W32, XIMG is assumed to a device context with the bitmap selected. */
1139 static RGB_PIXEL_COLOR
1140 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1141 unsigned long width
, unsigned long height
)
1143 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1146 if (corners
&& corners
[BOT_CORNER
] >= 0)
1148 /* Get the colors at the corner_pixels of ximg. */
1149 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1150 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1151 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1152 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1156 /* Get the colors at the corner_pixels of ximg. */
1157 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1158 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1159 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1160 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1162 /* Choose the most frequently found color as background. */
1163 for (i
= best_count
= 0; i
< 4; ++i
)
1167 for (j
= n
= 0; j
< 4; ++j
)
1168 if (corner_pixels
[i
] == corner_pixels
[j
])
1172 best
= corner_pixels
[i
], best_count
= n
;
1178 /* Portability macros */
1182 #define Destroy_Image(img_dc, prev) \
1183 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1185 #define Free_Pixmap(display, pixmap) \
1186 DeleteObject (pixmap)
1188 #elif defined (HAVE_NS)
1190 #define Destroy_Image(ximg, dummy) \
1191 ns_release_object (ximg)
1193 #define Free_Pixmap(display, pixmap) \
1194 ns_release_object (pixmap)
1198 #define Destroy_Image(ximg, dummy) \
1199 XDestroyImage (ximg)
1201 #define Free_Pixmap(display, pixmap) \
1202 XFreePixmap (display, pixmap)
1204 #endif /* !HAVE_NTGUI && !HAVE_NS */
1207 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1208 it is guessed heuristically. If non-zero, XIMG is an existing
1209 XImage object (or device context with the image selected on W32) to
1210 use for the heuristic. */
1213 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1215 if (! img
->background_valid
)
1216 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1218 int free_ximg
= !ximg
;
1221 #endif /* HAVE_NTGUI */
1226 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1227 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1229 HDC frame_dc
= get_frame_dc (f
);
1230 ximg
= CreateCompatibleDC (frame_dc
);
1231 release_frame_dc (f
, frame_dc
);
1232 prev
= SelectObject (ximg
, img
->pixmap
);
1233 #endif /* !HAVE_NTGUI */
1236 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1239 Destroy_Image (ximg
, prev
);
1241 img
->background_valid
= 1;
1244 return img
->background
;
1247 /* Return the `background_transparent' field of IMG. If IMG doesn't
1248 have one yet, it is guessed heuristically. If non-zero, MASK is an
1249 existing XImage object to use for the heuristic. */
1252 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1254 if (! img
->background_transparent_valid
)
1255 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1259 int free_mask
= !mask
;
1262 #endif /* HAVE_NTGUI */
1267 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1268 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1270 HDC frame_dc
= get_frame_dc (f
);
1271 mask
= CreateCompatibleDC (frame_dc
);
1272 release_frame_dc (f
, frame_dc
);
1273 prev
= SelectObject (mask
, img
->mask
);
1274 #endif /* HAVE_NTGUI */
1277 img
->background_transparent
1278 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1281 Destroy_Image (mask
, prev
);
1284 img
->background_transparent
= 0;
1286 img
->background_transparent_valid
= 1;
1289 return img
->background_transparent
;
1293 /***********************************************************************
1294 Helper functions for X image types
1295 ***********************************************************************/
1297 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1299 static void x_clear_image (struct frame
*f
, struct image
*img
);
1300 static unsigned long x_alloc_image_color (struct frame
*f
,
1302 Lisp_Object color_name
,
1303 unsigned long dflt
);
1306 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1307 free the pixmap if any. MASK_P non-zero means clear the mask
1308 pixmap if any. COLORS_P non-zero means free colors allocated for
1309 the image, if any. */
1312 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1315 if (pixmap_p
&& img
->pixmap
)
1317 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1318 img
->pixmap
= NO_PIXMAP
;
1319 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1320 img
->background_valid
= 0;
1323 if (mask_p
&& img
->mask
)
1325 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1326 img
->mask
= NO_PIXMAP
;
1327 img
->background_transparent_valid
= 0;
1330 if (colors_p
&& img
->ncolors
)
1332 /* W32_TODO: color table support. */
1333 #ifdef HAVE_X_WINDOWS
1334 x_free_colors (f
, img
->colors
, img
->ncolors
);
1335 #endif /* HAVE_X_WINDOWS */
1336 xfree (img
->colors
);
1343 /* Free X resources of image IMG which is used on frame F. */
1346 x_clear_image (struct frame
*f
, struct image
*img
)
1349 x_clear_image_1 (f
, img
, 1, 1, 1);
1354 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1355 cannot be allocated, use DFLT. Add a newly allocated color to
1356 IMG->colors, so that it can be freed again. Value is the pixel
1359 static unsigned long
1360 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1364 unsigned long result
;
1366 xassert (STRINGP (color_name
));
1368 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1369 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1372 /* This isn't called frequently so we get away with simply
1373 reallocating the color vector to the needed size, here. */
1376 (unsigned long *) xrealloc (img
->colors
,
1377 img
->ncolors
* sizeof *img
->colors
);
1378 img
->colors
[img
->ncolors
- 1] = color
.pixel
;
1379 result
= color
.pixel
;
1389 /***********************************************************************
1391 ***********************************************************************/
1393 static void cache_image (struct frame
*f
, struct image
*img
);
1394 static void postprocess_image (struct frame
*, struct image
*);
1396 /* Return a new, initialized image cache that is allocated from the
1397 heap. Call free_image_cache to free an image cache. */
1399 struct image_cache
*
1400 make_image_cache (void)
1402 struct image_cache
*c
= (struct image_cache
*) xmalloc (sizeof *c
);
1405 memset (c
, 0, sizeof *c
);
1407 c
->images
= (struct image
**) xmalloc (c
->size
* sizeof *c
->images
);
1408 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1409 c
->buckets
= (struct image
**) xmalloc (size
);
1410 memset (c
->buckets
, 0, size
);
1415 /* Find an image matching SPEC in the cache, and return it. If no
1416 image is found, return NULL. */
1417 static struct image
*
1418 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1421 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1422 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1424 if (!c
) return NULL
;
1426 /* If the image spec does not specify a background color, the cached
1427 image must have the same background color as the current frame.
1428 The foreground color must also match, for the sake of monochrome
1431 In fact, we could ignore the foreground color matching condition
1432 for color images, or if the image spec specifies :foreground;
1433 similarly we could ignore the background color matching condition
1434 for formats that don't use transparency (such as jpeg), or if the
1435 image spec specifies :background. However, the extra memory
1436 usage is probably negligible in practice, so we don't bother. */
1438 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1439 if (img
->hash
== hash
1440 && !NILP (Fequal (img
->spec
, spec
))
1441 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1442 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1448 /* Search frame F for an image with spec SPEC, and free it. */
1451 uncache_image (struct frame
*f
, Lisp_Object spec
)
1453 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1456 free_image (f
, img
);
1457 /* As display glyphs may still be referring to the image ID, we
1458 must garbage the frame (Bug#6426). */
1459 SET_FRAME_GARBAGED (f
);
1464 /* Free image cache of frame F. Be aware that X frames share images
1468 free_image_cache (struct frame
*f
)
1470 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1475 /* Cache should not be referenced by any frame when freed. */
1476 xassert (c
->refcount
== 0);
1478 for (i
= 0; i
< c
->used
; ++i
)
1479 free_image (f
, c
->images
[i
]);
1483 FRAME_IMAGE_CACHE (f
) = NULL
;
1488 /* Clear image cache of frame F. FILTER=t means free all images.
1489 FILTER=nil means clear only images that haven't been
1490 displayed for some time.
1491 Else, only free the images which have FILTER in their `dependencies'.
1492 Should be called from time to time to reduce the number of loaded images.
1493 If image-cache-eviction-delay is non-nil, this frees images in the cache
1494 which weren't displayed for at least that many seconds. */
1497 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1499 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1505 /* Block input so that we won't be interrupted by a SIGIO
1506 while being in an inconsistent state. */
1511 /* Filter image cache. */
1512 for (i
= 0; i
< c
->used
; ++i
)
1514 struct image
*img
= c
->images
[i
];
1515 if (img
&& (EQ (Qt
, filter
)
1516 || !NILP (Fmember (filter
, img
->dependencies
))))
1518 free_image (f
, img
);
1523 else if (INTEGERP (Vimage_cache_eviction_delay
))
1525 /* Free cache based on timestamp. */
1528 int delay
, nimages
= 0;
1530 for (i
= 0; i
< c
->used
; ++i
)
1534 /* If the number of cached images has grown unusually large,
1535 decrease the cache eviction delay (Bug#6230). */
1536 delay
= XFASTINT (Vimage_cache_eviction_delay
);
1538 delay
= max (1, 1600 * delay
/ (nimages
*nimages
));
1541 old
= EMACS_SECS (t
) - delay
;
1543 for (i
= 0; i
< c
->used
; ++i
)
1545 struct image
*img
= c
->images
[i
];
1546 if (img
&& img
->timestamp
< old
)
1548 free_image (f
, img
);
1554 /* We may be clearing the image cache because, for example,
1555 Emacs was iconified for a longer period of time. In that
1556 case, current matrices may still contain references to
1557 images freed above. So, clear these matrices. */
1560 Lisp_Object tail
, frame
;
1562 FOR_EACH_FRAME (tail
, frame
)
1564 struct frame
*fr
= XFRAME (frame
);
1565 if (FRAME_IMAGE_CACHE (fr
) == c
)
1566 clear_current_matrices (fr
);
1569 ++windows_or_buffers_changed
;
1577 clear_image_caches (Lisp_Object filter
)
1579 /* FIXME: We want to do
1580 * struct terminal *t;
1581 * for (t = terminal_list; t; t = t->next_terminal)
1582 * clear_image_cache (t, filter); */
1583 Lisp_Object tail
, frame
;
1584 FOR_EACH_FRAME (tail
, frame
)
1585 if (FRAME_WINDOW_P (XFRAME (frame
)))
1586 clear_image_cache (XFRAME (frame
), filter
);
1589 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1591 doc
: /* Clear the image cache.
1592 FILTER nil or a frame means clear all images in the selected frame.
1593 FILTER t means clear the image caches of all frames.
1594 Anything else, means only clear those images which refer to FILTER,
1595 which is then usually a filename. */)
1596 (Lisp_Object filter
)
1598 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1599 clear_image_caches (filter
);
1601 clear_image_cache (check_x_frame (filter
), Qt
);
1607 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1609 doc
: /* Fush the image with specification SPEC on frame FRAME.
1610 This removes the image from the Emacs image cache. If SPEC specifies
1611 an image file, the next redisplay of this image will read from the
1612 current contents of that file.
1614 FRAME nil or omitted means use the selected frame.
1615 FRAME t means refresh the image on all frames. */)
1616 (Lisp_Object spec
, Lisp_Object frame
)
1618 if (!valid_image_p (spec
))
1619 error ("Invalid image specification");
1624 FOR_EACH_FRAME (tail
, frame
)
1626 struct frame
*f
= XFRAME (frame
);
1627 if (FRAME_WINDOW_P (f
))
1628 uncache_image (f
, spec
);
1632 uncache_image (check_x_frame (frame
), spec
);
1638 /* Compute masks and transform image IMG on frame F, as specified
1639 by the image's specification, */
1642 postprocess_image (struct frame
*f
, struct image
*img
)
1644 /* Manipulation of the image's mask. */
1647 Lisp_Object conversion
, spec
;
1652 /* `:heuristic-mask t'
1654 means build a mask heuristically.
1655 `:heuristic-mask (R G B)'
1656 `:mask (heuristic (R G B))'
1657 means build a mask from color (R G B) in the
1660 means remove a mask, if any. */
1662 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1664 x_build_heuristic_mask (f
, img
, mask
);
1669 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1671 if (EQ (mask
, Qheuristic
))
1672 x_build_heuristic_mask (f
, img
, Qt
);
1673 else if (CONSP (mask
)
1674 && EQ (XCAR (mask
), Qheuristic
))
1676 if (CONSP (XCDR (mask
)))
1677 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1679 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1681 else if (NILP (mask
) && found_p
&& img
->mask
)
1683 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1684 img
->mask
= NO_PIXMAP
;
1689 /* Should we apply an image transformation algorithm? */
1690 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1691 if (EQ (conversion
, Qdisabled
))
1692 x_disable_image (f
, img
);
1693 else if (EQ (conversion
, Qlaplace
))
1695 else if (EQ (conversion
, Qemboss
))
1697 else if (CONSP (conversion
)
1698 && EQ (XCAR (conversion
), Qedge_detection
))
1701 tem
= XCDR (conversion
);
1703 x_edge_detection (f
, img
,
1704 Fplist_get (tem
, QCmatrix
),
1705 Fplist_get (tem
, QCcolor_adjustment
));
1711 /* Return the id of image with Lisp specification SPEC on frame F.
1712 SPEC must be a valid Lisp image specification (see valid_image_p). */
1715 lookup_image (struct frame
*f
, Lisp_Object spec
)
1721 /* F must be a window-system frame, and SPEC must be a valid image
1723 xassert (FRAME_WINDOW_P (f
));
1724 xassert (valid_image_p (spec
));
1726 /* Look up SPEC in the hash table of the image cache. */
1727 hash
= sxhash (spec
, 0);
1728 img
= search_image_cache (f
, spec
, hash
);
1729 if (img
&& img
->load_failed_p
)
1731 free_image (f
, img
);
1735 /* If not found, create a new image and cache it. */
1739 img
= make_image (spec
, hash
);
1740 cache_image (f
, img
);
1741 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1742 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1743 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1745 /* If we can't load the image, and we don't have a width and
1746 height, use some arbitrary width and height so that we can
1747 draw a rectangle for it. */
1748 if (img
->load_failed_p
)
1752 value
= image_spec_value (spec
, QCwidth
, NULL
);
1753 img
->width
= (INTEGERP (value
)
1754 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1755 value
= image_spec_value (spec
, QCheight
, NULL
);
1756 img
->height
= (INTEGERP (value
)
1757 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1761 /* Handle image type independent image attributes
1762 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1763 `:background COLOR'. */
1764 Lisp_Object ascent
, margin
, relief
, bg
;
1766 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1767 if (INTEGERP (ascent
))
1768 img
->ascent
= XFASTINT (ascent
);
1769 else if (EQ (ascent
, Qcenter
))
1770 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1772 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1773 if (INTEGERP (margin
) && XINT (margin
) >= 0)
1774 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1775 else if (CONSP (margin
) && INTEGERP (XCAR (margin
))
1776 && INTEGERP (XCDR (margin
)))
1778 if (XINT (XCAR (margin
)) > 0)
1779 img
->hmargin
= XFASTINT (XCAR (margin
));
1780 if (XINT (XCDR (margin
)) > 0)
1781 img
->vmargin
= XFASTINT (XCDR (margin
));
1784 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1785 if (INTEGERP (relief
))
1787 img
->relief
= XINT (relief
);
1788 img
->hmargin
+= eabs (img
->relief
);
1789 img
->vmargin
+= eabs (img
->relief
);
1792 if (! img
->background_valid
)
1794 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1798 = x_alloc_image_color (f
, img
, bg
,
1799 FRAME_BACKGROUND_PIXEL (f
));
1800 img
->background_valid
= 1;
1804 /* Do image transformations and compute masks, unless we
1805 don't have the image yet. */
1806 if (!EQ (*img
->type
->type
, Qpostscript
))
1807 postprocess_image (f
, img
);
1813 /* We're using IMG, so set its timestamp to `now'. */
1814 EMACS_GET_TIME (now
);
1815 img
->timestamp
= EMACS_SECS (now
);
1817 /* Value is the image id. */
1822 /* Cache image IMG in the image cache of frame F. */
1825 cache_image (struct frame
*f
, struct image
*img
)
1827 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1830 /* Find a free slot in c->images. */
1831 for (i
= 0; i
< c
->used
; ++i
)
1832 if (c
->images
[i
] == NULL
)
1835 /* If no free slot found, maybe enlarge c->images. */
1836 if (i
== c
->used
&& c
->used
== c
->size
)
1838 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *c
->images
/ 2 < c
->size
)
1839 memory_full (SIZE_MAX
);
1841 c
->images
= (struct image
**) xrealloc (c
->images
,
1842 c
->size
* sizeof *c
->images
);
1845 /* Add IMG to c->images, and assign IMG an id. */
1851 /* Add IMG to the cache's hash table. */
1852 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1853 img
->next
= c
->buckets
[i
];
1855 img
->next
->prev
= img
;
1857 c
->buckets
[i
] = img
;
1861 /* Call FN on every image in the image cache of frame F. Used to mark
1862 Lisp Objects in the image cache. */
1864 /* Mark Lisp objects in image IMG. */
1867 mark_image (struct image
*img
)
1869 mark_object (img
->spec
);
1870 mark_object (img
->dependencies
);
1872 if (!NILP (img
->lisp_data
))
1873 mark_object (img
->lisp_data
);
1878 mark_image_cache (struct image_cache
*c
)
1883 for (i
= 0; i
< c
->used
; ++i
)
1885 mark_image (c
->images
[i
]);
1891 /***********************************************************************
1892 X / NS / W32 support code
1893 ***********************************************************************/
1897 /* Macro for defining functions that will be loaded from image DLLs. */
1898 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1900 /* Macro for loading those image functions from the library. */
1901 #define LOAD_IMGLIB_FN(lib,func) { \
1902 fn_##func = (void *) GetProcAddress (lib, #func); \
1903 if (!fn_##func) return 0; \
1906 #endif /* HAVE_NTGUI */
1908 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1909 XImagePtr
*, Pixmap
*);
1910 static void x_destroy_x_image (XImagePtr
);
1911 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1913 /* Return nonzero if XIMG's size WIDTH x HEIGHT doesn't break the
1915 WIDTH and HEIGHT must both be positive.
1916 If XIMG is null, assume it is a bitmap. */
1918 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1920 #ifdef HAVE_X_WINDOWS
1921 /* Respect Xlib's limits: it cannot deal with images that have more
1922 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1923 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1926 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1927 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1930 int bitmap_pad
, depth
, bytes_per_line
;
1933 bitmap_pad
= ximg
->bitmap_pad
;
1934 depth
= ximg
->depth
;
1935 bytes_per_line
= ximg
->bytes_per_line
;
1941 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1943 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1944 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1946 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1947 For now, assume that every image size is allowed on these systems. */
1952 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1953 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1954 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1955 via xmalloc. Print error messages via image_error if an error
1956 occurs. Value is non-zero if successful.
1958 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1959 should indicate the bit depth of the image. */
1962 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1963 XImagePtr
*ximg
, Pixmap
*pixmap
)
1965 #ifdef HAVE_X_WINDOWS
1966 Display
*display
= FRAME_X_DISPLAY (f
);
1967 Window window
= FRAME_X_WINDOW (f
);
1968 Screen
*screen
= FRAME_X_SCREEN (f
);
1970 xassert (interrupt_input_blocked
);
1973 depth
= DefaultDepthOfScreen (screen
);
1974 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1975 depth
, ZPixmap
, 0, NULL
, width
, height
,
1976 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1979 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1983 if (! x_check_image_size (*ximg
, width
, height
))
1985 x_destroy_x_image (*ximg
);
1987 image_error ("Image too large (%dx%d)",
1988 make_number (width
), make_number (height
));
1992 /* Allocate image raster. */
1993 (*ximg
)->data
= (char *) xmalloc ((*ximg
)->bytes_per_line
* height
);
1995 /* Allocate a pixmap of the same size. */
1996 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1997 if (*pixmap
== NO_PIXMAP
)
1999 x_destroy_x_image (*ximg
);
2001 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
2006 #endif /* HAVE_X_WINDOWS */
2010 BITMAPINFOHEADER
*header
;
2012 int scanline_width_bits
;
2014 int palette_colors
= 0;
2019 if (depth
!= 1 && depth
!= 4 && depth
!= 8
2020 && depth
!= 16 && depth
!= 24 && depth
!= 32)
2022 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
2026 scanline_width_bits
= width
* depth
;
2027 remainder
= scanline_width_bits
% 32;
2030 scanline_width_bits
+= 32 - remainder
;
2032 /* Bitmaps with a depth less than 16 need a palette. */
2033 /* BITMAPINFO structure already contains the first RGBQUAD. */
2035 palette_colors
= 1 << depth
- 1;
2037 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2039 header
= &(*ximg
)->info
.bmiHeader
;
2040 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2041 header
->biSize
= sizeof (*header
);
2042 header
->biWidth
= width
;
2043 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2044 header
->biPlanes
= 1;
2045 header
->biBitCount
= depth
;
2046 header
->biCompression
= BI_RGB
;
2047 header
->biClrUsed
= palette_colors
;
2049 /* TODO: fill in palette. */
2052 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2053 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2054 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2055 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2056 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2057 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2058 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2059 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2062 hdc
= get_frame_dc (f
);
2064 /* Create a DIBSection and raster array for the bitmap,
2065 and store its handle in *pixmap. */
2066 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2067 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2068 /* casting avoids a GCC warning */
2069 (void **)&((*ximg
)->data
), NULL
, 0);
2071 /* Realize display palette and garbage all frames. */
2072 release_frame_dc (f
, hdc
);
2074 if (*pixmap
== NULL
)
2076 DWORD err
= GetLastError ();
2077 Lisp_Object errcode
;
2078 /* All system errors are < 10000, so the following is safe. */
2079 XSETINT (errcode
, (int) err
);
2080 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2081 x_destroy_x_image (*ximg
);
2087 #endif /* HAVE_NTGUI */
2090 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2094 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2103 /* Destroy XImage XIMG. Free XIMG->data. */
2106 x_destroy_x_image (XImagePtr ximg
)
2108 xassert (interrupt_input_blocked
);
2111 #ifdef HAVE_X_WINDOWS
2114 XDestroyImage (ximg
);
2115 #endif /* HAVE_X_WINDOWS */
2117 /* Data will be freed by DestroyObject. */
2120 #endif /* HAVE_NTGUI */
2122 ns_release_object (ximg
);
2123 #endif /* HAVE_NS */
2128 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2129 are width and height of both the image and pixmap. */
2132 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2134 #ifdef HAVE_X_WINDOWS
2137 xassert (interrupt_input_blocked
);
2138 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2139 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2140 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2141 #endif /* HAVE_X_WINDOWS */
2144 #if 0 /* I don't think this is necessary looking at where it is used. */
2145 HDC hdc
= get_frame_dc (f
);
2146 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2147 release_frame_dc (f
, hdc
);
2149 #endif /* HAVE_NTGUI */
2152 xassert (ximg
== pixmap
);
2153 ns_retain_object (ximg
);
2158 /***********************************************************************
2160 ***********************************************************************/
2162 /* Find image file FILE. Look in data-directory/images, then
2163 x-bitmap-file-path. Value is the encoded full name of the file
2164 found, or nil if not found. */
2167 x_find_image_file (Lisp_Object file
)
2169 Lisp_Object file_found
, search_path
;
2172 /* TODO I think this should use something like image-load-path
2173 instead. Unfortunately, that can contain non-string elements. */
2174 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2176 Vx_bitmap_file_path
);
2178 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2179 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2185 file_found
= ENCODE_FILE (file_found
);
2193 /* Read FILE into memory. Value is a pointer to a buffer allocated
2194 with xmalloc holding FILE's contents. Value is null if an error
2195 occurred. *SIZE is set to the size of the file. */
2197 static unsigned char *
2198 slurp_file (char *file
, ptrdiff_t *size
)
2201 unsigned char *buf
= NULL
;
2204 if (stat (file
, &st
) == 0
2205 && (fp
= fopen (file
, "rb")) != NULL
2206 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
2207 && (buf
= (unsigned char *) xmalloc (st
.st_size
),
2208 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2229 /***********************************************************************
2231 ***********************************************************************/
2233 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2234 static int xbm_load (struct frame
*f
, struct image
*img
);
2235 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2236 unsigned char *, unsigned char *);
2237 static int xbm_image_p (Lisp_Object object
);
2238 static int xbm_read_bitmap_data (struct frame
*f
,
2239 unsigned char *, unsigned char *,
2240 int *, int *, char **, int);
2241 static int xbm_file_p (Lisp_Object
);
2244 /* Indices of image specification fields in xbm_format, below. */
2246 enum xbm_keyword_index
2264 /* Vector of image_keyword structures describing the format
2265 of valid XBM image specifications. */
2267 static const struct image_keyword xbm_format
[XBM_LAST
] =
2269 {":type", IMAGE_SYMBOL_VALUE
, 1},
2270 {":file", IMAGE_STRING_VALUE
, 0},
2271 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2272 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2273 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2274 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2275 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2276 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2277 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
2278 {":relief", IMAGE_INTEGER_VALUE
, 0},
2279 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2280 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2281 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2284 /* Structure describing the image type XBM. */
2286 static struct image_type xbm_type
=
2295 /* Tokens returned from xbm_scan. */
2304 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2305 A valid specification is a list starting with the symbol `image'
2306 The rest of the list is a property list which must contain an
2309 If the specification specifies a file to load, it must contain
2310 an entry `:file FILENAME' where FILENAME is a string.
2312 If the specification is for a bitmap loaded from memory it must
2313 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2314 WIDTH and HEIGHT are integers > 0. DATA may be:
2316 1. a string large enough to hold the bitmap data, i.e. it must
2317 have a size >= (WIDTH + 7) / 8 * HEIGHT
2319 2. a bool-vector of size >= WIDTH * HEIGHT
2321 3. a vector of strings or bool-vectors, one for each line of the
2324 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2325 may not be specified in this case because they are defined in the
2328 Both the file and data forms may contain the additional entries
2329 `:background COLOR' and `:foreground COLOR'. If not present,
2330 foreground and background of the frame on which the image is
2331 displayed is used. */
2334 xbm_image_p (Lisp_Object object
)
2336 struct image_keyword kw
[XBM_LAST
];
2338 memcpy (kw
, xbm_format
, sizeof kw
);
2339 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2342 xassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2344 if (kw
[XBM_FILE
].count
)
2346 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2349 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2351 /* In-memory XBM file. */
2352 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2358 EMACS_INT width
, height
;
2360 /* Entries for `:width', `:height' and `:data' must be present. */
2361 if (!kw
[XBM_WIDTH
].count
2362 || !kw
[XBM_HEIGHT
].count
2363 || !kw
[XBM_DATA
].count
)
2366 data
= kw
[XBM_DATA
].value
;
2367 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2368 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2370 /* Check type of data, and width and height against contents of
2376 /* Number of elements of the vector must be >= height. */
2377 if (ASIZE (data
) < height
)
2380 /* Each string or bool-vector in data must be large enough
2381 for one line of the image. */
2382 for (i
= 0; i
< height
; ++i
)
2384 Lisp_Object elt
= XVECTOR (data
)->contents
[i
];
2389 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2392 else if (BOOL_VECTOR_P (elt
))
2394 if (XBOOL_VECTOR (elt
)->size
< width
)
2401 else if (STRINGP (data
))
2404 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2407 else if (BOOL_VECTOR_P (data
))
2409 if (XBOOL_VECTOR (data
)->size
/ height
< width
)
2420 /* Scan a bitmap file. FP is the stream to read from. Value is
2421 either an enumerator from enum xbm_token, or a character for a
2422 single-character token, or 0 at end of file. If scanning an
2423 identifier, store the lexeme of the identifier in SVAL. If
2424 scanning a number, store its value in *IVAL. */
2427 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2433 /* Skip white space. */
2434 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
2439 else if (isdigit (c
))
2441 int value
= 0, digit
;
2443 if (c
== '0' && *s
< end
)
2446 if (c
== 'x' || c
== 'X')
2453 else if (c
>= 'a' && c
<= 'f')
2454 digit
= c
- 'a' + 10;
2455 else if (c
>= 'A' && c
<= 'F')
2456 digit
= c
- 'A' + 10;
2459 value
= 16 * value
+ digit
;
2462 else if (isdigit (c
))
2466 && (c
= *(*s
)++, isdigit (c
)))
2467 value
= 8 * value
+ c
- '0';
2474 && (c
= *(*s
)++, isdigit (c
)))
2475 value
= 10 * value
+ c
- '0';
2483 else if (isalpha (c
) || c
== '_')
2487 && (c
= *(*s
)++, (isalnum (c
) || c
== '_')))
2494 else if (c
== '/' && **s
== '*')
2496 /* C-style comment. */
2498 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2512 /* Create a Windows bitmap from X bitmap data. */
2514 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2516 static unsigned char swap_nibble
[16]
2517 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2518 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2519 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2520 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2522 unsigned char *bits
, *p
;
2525 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2526 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2527 bits
= (unsigned char *) alloca (height
* w2
);
2528 memset (bits
, 0, height
* w2
);
2529 for (i
= 0; i
< height
; i
++)
2532 for (j
= 0; j
< w1
; j
++)
2534 /* Bitswap XBM bytes to match how Windows does things. */
2535 unsigned char c
= *data
++;
2536 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2537 | (swap_nibble
[(c
>>4) & 0xf]));
2540 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2546 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2547 COLORREF foreground
, COLORREF background
)
2549 HDC hdc
, old_img_dc
, new_img_dc
;
2550 HGDIOBJ old_prev
, new_prev
;
2553 hdc
= get_frame_dc (f
);
2554 old_img_dc
= CreateCompatibleDC (hdc
);
2555 new_img_dc
= CreateCompatibleDC (hdc
);
2556 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2557 release_frame_dc (f
, hdc
);
2558 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2559 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2560 /* Windows convention for mono bitmaps is black = background,
2561 white = foreground. */
2562 SetTextColor (new_img_dc
, background
);
2563 SetBkColor (new_img_dc
, foreground
);
2565 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2568 SelectObject (old_img_dc
, old_prev
);
2569 SelectObject (new_img_dc
, new_prev
);
2570 DeleteDC (old_img_dc
);
2571 DeleteDC (new_img_dc
);
2572 DeleteObject (img
->pixmap
);
2573 if (new_pixmap
== 0)
2574 fprintf (stderr
, "Failed to convert image to color.\n");
2576 img
->pixmap
= new_pixmap
;
2579 #define XBM_BIT_SHUFFLE(b) (~(b))
2583 #define XBM_BIT_SHUFFLE(b) (b)
2585 #endif /* HAVE_NTGUI */
2589 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2590 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2591 int non_default_colors
)
2595 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2597 /* If colors were specified, transfer the bitmap to a color one. */
2598 if (non_default_colors
)
2599 convert_mono_to_color_image (f
, img
, fg
, bg
);
2601 #elif defined (HAVE_NS)
2602 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2606 (x_check_image_size (0, img
->width
, img
->height
)
2607 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2610 img
->width
, img
->height
,
2612 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2614 #endif /* !HAVE_NTGUI && !HAVE_NS */
2619 /* Replacement for XReadBitmapFileData which isn't available under old
2620 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2621 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2622 the image. Return in *DATA the bitmap data allocated with xmalloc.
2623 Value is non-zero if successful. DATA null means just test if
2624 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2625 is non-zero, inhibit the call to image_error when the image size is
2626 invalid (the bitmap remains unread). */
2629 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2630 int *width
, int *height
, char **data
,
2631 int inhibit_image_error
)
2633 unsigned char *s
= contents
;
2634 char buffer
[BUFSIZ
];
2637 int bytes_per_line
, i
, nbytes
;
2643 LA1 = xbm_scan (&s, end, buffer, &value)
2645 #define expect(TOKEN) \
2646 if (LA1 != (TOKEN)) \
2651 #define expect_ident(IDENT) \
2652 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2657 *width
= *height
= -1;
2660 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2662 /* Parse defines for width, height and hot-spots. */
2666 expect_ident ("define");
2667 expect (XBM_TK_IDENT
);
2669 if (LA1
== XBM_TK_NUMBER
)
2671 char *q
= strrchr (buffer
, '_');
2672 q
= q
? q
+ 1 : buffer
;
2673 if (strcmp (q
, "width") == 0)
2675 else if (strcmp (q
, "height") == 0)
2678 expect (XBM_TK_NUMBER
);
2681 if (!check_image_size (f
, *width
, *height
))
2683 if (!inhibit_image_error
)
2684 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2687 else if (data
== NULL
)
2690 /* Parse bits. Must start with `static'. */
2691 expect_ident ("static");
2692 if (LA1
== XBM_TK_IDENT
)
2694 if (strcmp (buffer
, "unsigned") == 0)
2697 expect_ident ("char");
2699 else if (strcmp (buffer
, "short") == 0)
2703 if (*width
% 16 && *width
% 16 < 9)
2706 else if (strcmp (buffer
, "char") == 0)
2714 expect (XBM_TK_IDENT
);
2720 if (! x_check_image_size (0, *width
, *height
))
2722 if (!inhibit_image_error
)
2723 image_error ("Image too large (%dx%d)",
2724 make_number (*width
), make_number (*height
));
2727 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2728 nbytes
= bytes_per_line
* *height
;
2729 p
= *data
= (char *) xmalloc (nbytes
);
2733 for (i
= 0; i
< nbytes
; i
+= 2)
2736 expect (XBM_TK_NUMBER
);
2738 *p
++ = XBM_BIT_SHUFFLE (val
);
2739 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2740 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2742 if (LA1
== ',' || LA1
== '}')
2750 for (i
= 0; i
< nbytes
; ++i
)
2753 expect (XBM_TK_NUMBER
);
2755 *p
++ = XBM_BIT_SHUFFLE (val
);
2757 if (LA1
== ',' || LA1
== '}')
2782 /* Load XBM image IMG which will be displayed on frame F from buffer
2783 CONTENTS. END is the end of the buffer. Value is non-zero if
2787 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2794 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2798 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2799 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2800 int non_default_colors
= 0;
2803 xassert (img
->width
> 0 && img
->height
> 0);
2805 /* Get foreground and background colors, maybe allocate colors. */
2806 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2809 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2810 non_default_colors
= 1;
2812 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2815 background
= x_alloc_image_color (f
, img
, value
, background
);
2816 img
->background
= background
;
2817 img
->background_valid
= 1;
2818 non_default_colors
= 1;
2821 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2822 foreground
, background
,
2823 non_default_colors
);
2826 if (img
->pixmap
== NO_PIXMAP
)
2828 x_clear_image (f
, img
);
2829 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2835 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2841 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2844 xbm_file_p (Lisp_Object data
)
2847 return (STRINGP (data
)
2848 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2849 (SDATA (data
) + SBYTES (data
)),
2854 /* Fill image IMG which is used on frame F with pixmap data. Value is
2855 non-zero if successful. */
2858 xbm_load (struct frame
*f
, struct image
*img
)
2861 Lisp_Object file_name
;
2863 xassert (xbm_image_p (img
->spec
));
2865 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2866 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2867 if (STRINGP (file_name
))
2870 unsigned char *contents
;
2873 file
= x_find_image_file (file_name
);
2874 if (!STRINGP (file
))
2876 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2880 contents
= slurp_file (SSDATA (file
), &size
);
2881 if (contents
== NULL
)
2883 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2887 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2892 struct image_keyword fmt
[XBM_LAST
];
2894 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2895 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2896 int non_default_colors
= 0;
2899 int in_memory_file_p
= 0;
2901 /* See if data looks like an in-memory XBM file. */
2902 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2903 in_memory_file_p
= xbm_file_p (data
);
2905 /* Parse the image specification. */
2906 memcpy (fmt
, xbm_format
, sizeof fmt
);
2907 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2911 /* Get specified width, and height. */
2912 if (!in_memory_file_p
)
2914 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2915 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2916 xassert (img
->width
> 0 && img
->height
> 0);
2917 if (!check_image_size (f
, img
->width
, img
->height
))
2919 image_error ("Invalid image size (see `max-image-size')",
2925 /* Get foreground and background colors, maybe allocate colors. */
2926 if (fmt
[XBM_FOREGROUND
].count
2927 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2929 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2931 non_default_colors
= 1;
2934 if (fmt
[XBM_BACKGROUND
].count
2935 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2937 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2939 non_default_colors
= 1;
2942 if (in_memory_file_p
)
2943 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2952 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2954 p
= bits
= (char *) alloca (nbytes
* img
->height
);
2955 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2957 Lisp_Object line
= XVECTOR (data
)->contents
[i
];
2959 memcpy (p
, SDATA (line
), nbytes
);
2961 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2964 else if (STRINGP (data
))
2965 bits
= SSDATA (data
);
2967 bits
= (char *) XBOOL_VECTOR (data
)->data
;
2973 /* Windows mono bitmaps are reversed compared with X. */
2974 invertedBits
= bits
;
2975 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2977 bits
= (char *) alloca (nbytes
);
2978 for (i
= 0; i
< nbytes
; i
++)
2979 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2982 /* Create the pixmap. */
2984 if (x_check_image_size (0, img
->width
, img
->height
))
2985 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2986 foreground
, background
,
2987 non_default_colors
);
2989 img
->pixmap
= NO_PIXMAP
;
2995 image_error ("Unable to create pixmap for XBM image `%s'",
2997 x_clear_image (f
, img
);
3007 /***********************************************************************
3009 ***********************************************************************/
3011 #if defined (HAVE_XPM) || defined (HAVE_NS)
3013 static int xpm_image_p (Lisp_Object object
);
3014 static int xpm_load (struct frame
*f
, struct image
*img
);
3015 static int xpm_valid_color_symbols_p (Lisp_Object
);
3017 #endif /* HAVE_XPM || HAVE_NS */
3021 /* Indicate to xpm.h that we don't have Xlib. */
3023 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3024 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3025 #define XColor xpm_XColor
3026 #define XImage xpm_XImage
3027 #define Display xpm_Display
3028 #define PIXEL_ALREADY_TYPEDEFED
3029 #include "X11/xpm.h"
3034 #undef PIXEL_ALREADY_TYPEDEFED
3036 #include "X11/xpm.h"
3037 #endif /* HAVE_NTGUI */
3038 #endif /* HAVE_XPM */
3040 #if defined (HAVE_XPM) || defined (HAVE_NS)
3041 /* The symbol `xpm' identifying XPM-format images. */
3043 static Lisp_Object Qxpm
;
3045 /* Indices of image specification fields in xpm_format, below. */
3047 enum xpm_keyword_index
3063 /* Vector of image_keyword structures describing the format
3064 of valid XPM image specifications. */
3066 static const struct image_keyword xpm_format
[XPM_LAST
] =
3068 {":type", IMAGE_SYMBOL_VALUE
, 1},
3069 {":file", IMAGE_STRING_VALUE
, 0},
3070 {":data", IMAGE_STRING_VALUE
, 0},
3071 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3072 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
3073 {":relief", IMAGE_INTEGER_VALUE
, 0},
3074 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3075 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3076 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3077 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3078 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3081 /* Structure describing the image type XPM. */
3083 static struct image_type xpm_type
=
3092 #ifdef HAVE_X_WINDOWS
3094 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3095 functions for allocating image colors. Our own functions handle
3096 color allocation failures more gracefully than the ones on the XPM
3099 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3100 #define ALLOC_XPM_COLORS
3102 #endif /* HAVE_X_WINDOWS */
3104 #ifdef ALLOC_XPM_COLORS
3106 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3107 static void xpm_free_color_cache (void);
3108 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3109 static int xpm_color_bucket (char *);
3110 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3113 /* An entry in a hash table used to cache color definitions of named
3114 colors. This cache is necessary to speed up XPM image loading in
3115 case we do color allocations ourselves. Without it, we would need
3116 a call to XParseColor per pixel in the image. */
3118 struct xpm_cached_color
3120 /* Next in collision chain. */
3121 struct xpm_cached_color
*next
;
3123 /* Color definition (RGB and pixel color). */
3130 /* The hash table used for the color cache, and its bucket vector
3133 #define XPM_COLOR_CACHE_BUCKETS 1001
3134 static struct xpm_cached_color
**xpm_color_cache
;
3136 /* Initialize the color cache. */
3139 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3141 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3142 xpm_color_cache
= (struct xpm_cached_color
**) xmalloc (nbytes
);
3143 memset (xpm_color_cache
, 0, nbytes
);
3144 init_color_table ();
3146 if (attrs
->valuemask
& XpmColorSymbols
)
3151 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3152 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3153 attrs
->colorsymbols
[i
].value
, &color
))
3155 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3157 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3162 /* Free the color cache. */
3165 xpm_free_color_cache (void)
3167 struct xpm_cached_color
*p
, *next
;
3170 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3171 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3177 xfree (xpm_color_cache
);
3178 xpm_color_cache
= NULL
;
3179 free_color_table ();
3182 /* Return the bucket index for color named COLOR_NAME in the color
3186 xpm_color_bucket (char *color_name
)
3188 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3189 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3193 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3194 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3197 static struct xpm_cached_color
*
3198 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3201 struct xpm_cached_color
*p
;
3204 bucket
= xpm_color_bucket (color_name
);
3206 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3207 p
= (struct xpm_cached_color
*) xmalloc (nbytes
);
3208 strcpy (p
->name
, color_name
);
3210 p
->next
= xpm_color_cache
[bucket
];
3211 xpm_color_cache
[bucket
] = p
;
3215 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3216 return the cached definition in *COLOR. Otherwise, make a new
3217 entry in the cache and allocate the color. Value is zero if color
3218 allocation failed. */
3221 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3223 struct xpm_cached_color
*p
;
3224 int h
= xpm_color_bucket (color_name
);
3226 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3227 if (strcmp (p
->name
, color_name
) == 0)
3232 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3235 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3237 p
= xpm_cache_color (f
, color_name
, color
, h
);
3239 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3240 with transparency, and it's useful. */
3241 else if (strcmp ("opaque", color_name
) == 0)
3243 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3244 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3245 p
= xpm_cache_color (f
, color_name
, color
, h
);
3252 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3253 CLOSURE is a pointer to the frame on which we allocate the
3254 color. Return in *COLOR the allocated color. Value is non-zero
3258 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3261 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3265 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3266 is a pointer to the frame on which we allocate the color. Value is
3267 non-zero if successful. */
3270 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3275 #endif /* ALLOC_XPM_COLORS */
3280 /* XPM library details. */
3282 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3283 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3284 xpm_XImage
**, XpmAttributes
*));
3285 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3286 xpm_XImage
**, XpmAttributes
*));
3287 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3290 init_xpm_functions (Lisp_Object libraries
)
3294 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3297 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3298 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3299 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3300 LOAD_IMGLIB_FN (library
, XImageFree
);
3304 #endif /* HAVE_NTGUI */
3307 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3308 for XPM images. Such a list must consist of conses whose car and
3312 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3314 while (CONSP (color_symbols
))
3316 Lisp_Object sym
= XCAR (color_symbols
);
3318 || !STRINGP (XCAR (sym
))
3319 || !STRINGP (XCDR (sym
)))
3321 color_symbols
= XCDR (color_symbols
);
3324 return NILP (color_symbols
);
3328 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3331 xpm_image_p (Lisp_Object object
)
3333 struct image_keyword fmt
[XPM_LAST
];
3334 memcpy (fmt
, xpm_format
, sizeof fmt
);
3335 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3336 /* Either `:file' or `:data' must be present. */
3337 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3338 /* Either no `:color-symbols' or it's a list of conses
3339 whose car and cdr are strings. */
3340 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3341 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3344 #endif /* HAVE_XPM || HAVE_NS */
3346 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3348 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3350 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3353 XpmAttributes attrs
;
3354 Pixmap bitmap
, mask
;
3356 memset (&attrs
, 0, sizeof attrs
);
3358 attrs
.visual
= FRAME_X_VISUAL (f
);
3359 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3360 attrs
.valuemask
|= XpmVisual
;
3361 attrs
.valuemask
|= XpmColormap
;
3363 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3364 (char **) bits
, &bitmap
, &mask
, &attrs
);
3365 if (rc
!= XpmSuccess
)
3367 XpmFreeAttributes (&attrs
);
3371 id
= x_allocate_bitmap_record (f
);
3372 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3373 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3374 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3375 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3376 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3377 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3378 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3379 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3381 XpmFreeAttributes (&attrs
);
3384 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3386 /* Load image IMG which will be displayed on frame F. Value is
3387 non-zero if successful. */
3392 xpm_load (struct frame
*f
, struct image
*img
)
3395 XpmAttributes attrs
;
3396 Lisp_Object specified_file
, color_symbols
;
3399 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3400 #endif /* HAVE_NTGUI */
3402 /* Configure the XPM lib. Use the visual of frame F. Allocate
3403 close colors. Return colors allocated. */
3404 memset (&attrs
, 0, sizeof attrs
);
3407 attrs
.visual
= FRAME_X_VISUAL (f
);
3408 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3409 attrs
.valuemask
|= XpmVisual
;
3410 attrs
.valuemask
|= XpmColormap
;
3411 #endif /* HAVE_NTGUI */
3413 #ifdef ALLOC_XPM_COLORS
3414 /* Allocate colors with our own functions which handle
3415 failing color allocation more gracefully. */
3416 attrs
.color_closure
= f
;
3417 attrs
.alloc_color
= xpm_alloc_color
;
3418 attrs
.free_colors
= xpm_free_colors
;
3419 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3420 #else /* not ALLOC_XPM_COLORS */
3421 /* Let the XPM lib allocate colors. */
3422 attrs
.valuemask
|= XpmReturnAllocPixels
;
3423 #ifdef XpmAllocCloseColors
3424 attrs
.alloc_close_colors
= 1;
3425 attrs
.valuemask
|= XpmAllocCloseColors
;
3426 #else /* not XpmAllocCloseColors */
3427 attrs
.closeness
= 600;
3428 attrs
.valuemask
|= XpmCloseness
;
3429 #endif /* not XpmAllocCloseColors */
3430 #endif /* ALLOC_XPM_COLORS */
3432 /* If image specification contains symbolic color definitions, add
3433 these to `attrs'. */
3434 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3435 if (CONSP (color_symbols
))
3438 XpmColorSymbol
*xpm_syms
;
3441 attrs
.valuemask
|= XpmColorSymbols
;
3443 /* Count number of symbols. */
3444 attrs
.numsymbols
= 0;
3445 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3448 /* Allocate an XpmColorSymbol array. */
3449 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3450 xpm_syms
= (XpmColorSymbol
*) alloca (size
);
3451 memset (xpm_syms
, 0, size
);
3452 attrs
.colorsymbols
= xpm_syms
;
3454 /* Fill the color symbol array. */
3455 for (tail
= color_symbols
, i
= 0;
3457 ++i
, tail
= XCDR (tail
))
3461 char *empty_string
= (char *) "";
3463 if (!CONSP (XCAR (tail
)))
3465 xpm_syms
[i
].name
= empty_string
;
3466 xpm_syms
[i
].value
= empty_string
;
3469 name
= XCAR (XCAR (tail
));
3470 color
= XCDR (XCAR (tail
));
3473 xpm_syms
[i
].name
= (char *) alloca (SCHARS (name
) + 1);
3474 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3477 xpm_syms
[i
].name
= empty_string
;
3478 if (STRINGP (color
))
3480 xpm_syms
[i
].value
= (char *) alloca (SCHARS (color
) + 1);
3481 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3484 xpm_syms
[i
].value
= empty_string
;
3488 /* Create a pixmap for the image, either from a file, or from a
3489 string buffer containing data in the same format as an XPM file. */
3490 #ifdef ALLOC_XPM_COLORS
3491 xpm_init_color_cache (f
, &attrs
);
3494 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3498 HDC frame_dc
= get_frame_dc (f
);
3499 hdc
= CreateCompatibleDC (frame_dc
);
3500 release_frame_dc (f
, frame_dc
);
3502 #endif /* HAVE_NTGUI */
3504 if (STRINGP (specified_file
))
3506 Lisp_Object file
= x_find_image_file (specified_file
);
3507 if (!STRINGP (file
))
3509 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3510 #ifdef ALLOC_XPM_COLORS
3511 xpm_free_color_cache ();
3517 /* XpmReadFileToPixmap is not available in the Windows port of
3518 libxpm. But XpmReadFileToImage almost does what we want. */
3519 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3520 &xpm_image
, &xpm_mask
,
3523 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3524 SSDATA (file
), &img
->pixmap
, &img
->mask
,
3526 #endif /* HAVE_NTGUI */
3530 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3531 if (!STRINGP (buffer
))
3533 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3534 #ifdef ALLOC_XPM_COLORS
3535 xpm_free_color_cache ();
3540 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3541 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3542 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3543 &xpm_image
, &xpm_mask
,
3546 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3548 &img
->pixmap
, &img
->mask
,
3550 #endif /* HAVE_NTGUI */
3553 if (rc
== XpmSuccess
)
3555 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3556 img
->colors
= colors_in_color_table (&img
->ncolors
);
3557 #else /* not ALLOC_XPM_COLORS */
3561 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3562 plus some duplicate attributes. */
3563 if (xpm_image
&& xpm_image
->bitmap
)
3565 img
->pixmap
= xpm_image
->bitmap
;
3566 /* XImageFree in libXpm frees XImage struct without destroying
3567 the bitmap, which is what we want. */
3568 fn_XImageFree (xpm_image
);
3570 if (xpm_mask
&& xpm_mask
->bitmap
)
3572 /* The mask appears to be inverted compared with what we expect.
3573 TODO: invert our expectations. See other places where we
3574 have to invert bits because our idea of masks is backwards. */
3576 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3578 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3579 SelectObject (hdc
, old_obj
);
3581 img
->mask
= xpm_mask
->bitmap
;
3582 fn_XImageFree (xpm_mask
);
3587 #endif /* HAVE_NTGUI */
3589 /* Remember allocated colors. */
3590 img
->ncolors
= attrs
.nalloc_pixels
;
3591 img
->colors
= (unsigned long *) xmalloc (img
->ncolors
3592 * sizeof *img
->colors
);
3593 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3595 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3596 #ifdef DEBUG_X_COLORS
3597 register_color (img
->colors
[i
]);
3600 #endif /* not ALLOC_XPM_COLORS */
3602 img
->width
= attrs
.width
;
3603 img
->height
= attrs
.height
;
3604 xassert (img
->width
> 0 && img
->height
> 0);
3606 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3608 fn_XpmFreeAttributes (&attrs
);
3610 XpmFreeAttributes (&attrs
);
3611 #endif /* HAVE_NTGUI */
3617 #endif /* HAVE_NTGUI */
3622 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3625 case XpmFileInvalid
:
3626 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3630 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3633 case XpmColorFailed
:
3634 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3638 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3643 #ifdef ALLOC_XPM_COLORS
3644 xpm_free_color_cache ();
3646 return rc
== XpmSuccess
;
3649 #endif /* HAVE_XPM */
3651 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3653 /* XPM support functions for NS where libxpm is not available.
3654 Only XPM version 3 (without any extensions) is supported. */
3656 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3658 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3659 const unsigned char *, int);
3660 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3662 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3663 const unsigned char *, int);
3665 /* Tokens returned from xpm_scan. */
3674 /* Scan an XPM data and return a character (< 256) or a token defined
3675 by enum xpm_token above. *S and END are the start (inclusive) and
3676 the end (exclusive) addresses of the data, respectively. Advance
3677 *S while scanning. If token is either XPM_TK_IDENT or
3678 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3679 length of the corresponding token, respectively. */
3682 xpm_scan (const unsigned char **s
,
3683 const unsigned char *end
,
3684 const unsigned char **beg
,
3691 /* Skip white-space. */
3692 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
3695 /* gnus-pointer.xpm uses '-' in its identifier.
3696 sb-dir-plus.xpm uses '+' in its identifier. */
3697 if (isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3701 && (c
= **s
, isalnum (c
) || c
== '_' || c
== '-' || c
== '+'))
3704 return XPM_TK_IDENT
;
3709 while (*s
< end
&& **s
!= '"')
3714 return XPM_TK_STRING
;
3718 if (*s
< end
&& **s
== '*')
3720 /* C-style comment. */
3724 while (*s
< end
&& *(*s
)++ != '*')
3727 while (*s
< end
&& **s
!= '/');
3741 /* Functions for color table lookup in XPM data. A key is a string
3742 specifying the color of each pixel in XPM data. A value is either
3743 an integer that specifies a pixel color, Qt that specifies
3744 transparency, or Qnil for the unspecified color. If the length of
3745 the key string is one, a vector is used as a table. Otherwise, a
3746 hash table is used. */
3749 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3750 const unsigned char *,
3753 Lisp_Object (**get_func
) (Lisp_Object
,
3754 const unsigned char *,
3757 *put_func
= xpm_put_color_table_v
;
3758 *get_func
= xpm_get_color_table_v
;
3759 return Fmake_vector (make_number (256), Qnil
);
3763 xpm_put_color_table_v (Lisp_Object color_table
,
3764 const unsigned char *chars_start
,
3768 XVECTOR (color_table
)->contents
[*chars_start
] = color
;
3772 xpm_get_color_table_v (Lisp_Object color_table
,
3773 const unsigned char *chars_start
,
3776 return XVECTOR (color_table
)->contents
[*chars_start
];
3780 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3781 const unsigned char *,
3784 Lisp_Object (**get_func
) (Lisp_Object
,
3785 const unsigned char *,
3788 *put_func
= xpm_put_color_table_h
;
3789 *get_func
= xpm_get_color_table_h
;
3790 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3791 make_float (DEFAULT_REHASH_SIZE
),
3792 make_float (DEFAULT_REHASH_THRESHOLD
),
3797 xpm_put_color_table_h (Lisp_Object color_table
,
3798 const unsigned char *chars_start
,
3802 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3803 EMACS_UINT hash_code
;
3804 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3806 hash_lookup (table
, chars
, &hash_code
);
3807 hash_put (table
, chars
, color
, hash_code
);
3811 xpm_get_color_table_h (Lisp_Object color_table
,
3812 const unsigned char *chars_start
,
3815 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3816 int i
= hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
),
3819 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3822 enum xpm_color_key
{
3830 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3833 xpm_str_to_color_key (const char *s
)
3838 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3840 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3846 xpm_load_image (struct frame
*f
,
3848 const unsigned char *contents
,
3849 const unsigned char *end
)
3851 const unsigned char *s
= contents
, *beg
, *str
;
3852 unsigned char buffer
[BUFSIZ
];
3853 int width
, height
, x
, y
;
3854 int num_colors
, chars_per_pixel
;
3857 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3858 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3859 Lisp_Object frame
, color_symbols
, color_table
;
3860 int best_key
, have_mask
= 0;
3861 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3864 LA1 = xpm_scan (&s, end, &beg, &len)
3866 #define expect(TOKEN) \
3867 if (LA1 != (TOKEN)) \
3872 #define expect_ident(IDENT) \
3873 if (LA1 == XPM_TK_IDENT \
3874 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3879 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3883 expect_ident ("static");
3884 expect_ident ("char");
3886 expect (XPM_TK_IDENT
);
3891 expect (XPM_TK_STRING
);
3894 memcpy (buffer
, beg
, len
);
3896 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3897 &num_colors
, &chars_per_pixel
) != 4
3898 || width
<= 0 || height
<= 0
3899 || num_colors
<= 0 || chars_per_pixel
<= 0)
3902 if (!check_image_size (f
, width
, height
))
3904 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3908 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3909 &ximg
, &img
->pixmap
)
3911 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3912 &mask_img
, &img
->mask
)
3916 image_error ("Image too large", Qnil
, Qnil
);
3922 XSETFRAME (frame
, f
);
3923 if (!NILP (Fxw_display_color_p (frame
)))
3924 best_key
= XPM_COLOR_KEY_C
;
3925 else if (!NILP (Fx_display_grayscale_p (frame
)))
3926 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3927 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3929 best_key
= XPM_COLOR_KEY_M
;
3931 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3932 if (chars_per_pixel
== 1)
3933 color_table
= xpm_make_color_table_v (&put_color_table
,
3936 color_table
= xpm_make_color_table_h (&put_color_table
,
3939 while (num_colors
-- > 0)
3941 char *color
, *max_color
;
3942 int key
, next_key
, max_key
= 0;
3943 Lisp_Object symbol_color
= Qnil
, color_val
;
3946 expect (XPM_TK_STRING
);
3947 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3949 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3950 buffer
[len
- chars_per_pixel
] = '\0';
3952 str
= strtok (buffer
, " \t");
3955 key
= xpm_str_to_color_key (str
);
3960 color
= strtok (NULL
, " \t");
3964 while ((str
= strtok (NULL
, " \t")) != NULL
)
3966 next_key
= xpm_str_to_color_key (str
);
3969 color
[strlen (color
)] = ' ';
3972 if (key
== XPM_COLOR_KEY_S
)
3974 if (NILP (symbol_color
))
3975 symbol_color
= build_string (color
);
3977 else if (max_key
< key
&& key
<= best_key
)
3987 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3989 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3991 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3993 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
3995 else if (x_defined_color (f
, SDATA (XCDR (specified_color
)),
3997 color_val
= make_number (cdef
.pixel
);
4000 if (NILP (color_val
) && max_key
> 0)
4002 if (xstrcasecmp (max_color
, "None") == 0)
4004 else if (x_defined_color (f
, max_color
, &cdef
, 0))
4005 color_val
= make_number (cdef
.pixel
);
4007 if (!NILP (color_val
))
4008 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
4013 for (y
= 0; y
< height
; y
++)
4015 expect (XPM_TK_STRING
);
4017 if (len
< width
* chars_per_pixel
)
4019 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
4021 Lisp_Object color_val
=
4022 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4024 XPutPixel (ximg
, x
, y
,
4025 (INTEGERP (color_val
) ? XINT (color_val
)
4026 : FRAME_FOREGROUND_PIXEL (f
)));
4028 XPutPixel (mask_img
, x
, y
,
4029 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4030 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4032 if (EQ (color_val
, Qt
))
4033 ns_set_alpha (ximg
, x
, y
, 0);
4041 img
->height
= height
;
4043 /* Maybe fill in the background field while we have ximg handy. */
4044 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4045 IMAGE_BACKGROUND (img
, f
, ximg
);
4047 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4048 x_destroy_x_image (ximg
);
4052 /* Fill in the background_transparent field while we have the
4054 image_background_transparent (img
, f
, mask_img
);
4056 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4057 x_destroy_x_image (mask_img
);
4061 x_destroy_x_image (mask_img
);
4062 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4063 img
->mask
= NO_PIXMAP
;
4069 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4071 x_destroy_x_image (ximg
);
4072 x_destroy_x_image (mask_img
);
4073 x_clear_image (f
, img
);
4082 xpm_load (struct frame
*f
,
4086 Lisp_Object file_name
;
4088 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4089 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4090 if (STRINGP (file_name
))
4093 unsigned char *contents
;
4096 file
= x_find_image_file (file_name
);
4097 if (!STRINGP (file
))
4099 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4103 contents
= slurp_file (SDATA (file
), &size
);
4104 if (contents
== NULL
)
4106 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4110 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4117 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4118 if (!STRINGP (data
))
4120 image_error ("Invalid image data `%s'", data
, Qnil
);
4123 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4124 SDATA (data
) + SBYTES (data
));
4130 #endif /* HAVE_NS && !HAVE_XPM */
4134 /***********************************************************************
4136 ***********************************************************************/
4138 #ifdef COLOR_TABLE_SUPPORT
4140 /* An entry in the color table mapping an RGB color to a pixel color. */
4145 unsigned long pixel
;
4147 /* Next in color table collision list. */
4148 struct ct_color
*next
;
4151 /* The bucket vector size to use. Must be prime. */
4155 /* Value is a hash of the RGB color given by R, G, and B. */
4157 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4159 /* The color hash table. */
4161 static struct ct_color
**ct_table
;
4163 /* Number of entries in the color table. */
4165 static int ct_colors_allocated
;
4167 /* Initialize the color table. */
4170 init_color_table (void)
4172 int size
= CT_SIZE
* sizeof (*ct_table
);
4173 ct_table
= (struct ct_color
**) xmalloc (size
);
4174 memset (ct_table
, 0, size
);
4175 ct_colors_allocated
= 0;
4179 /* Free memory associated with the color table. */
4182 free_color_table (void)
4185 struct ct_color
*p
, *next
;
4187 for (i
= 0; i
< CT_SIZE
; ++i
)
4188 for (p
= ct_table
[i
]; p
; p
= next
)
4199 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4200 entry for that color already is in the color table, return the
4201 pixel color of that entry. Otherwise, allocate a new color for R,
4202 G, B, and make an entry in the color table. */
4204 static unsigned long
4205 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4207 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4208 int i
= hash
% CT_SIZE
;
4210 Display_Info
*dpyinfo
;
4212 /* Handle TrueColor visuals specially, which improves performance by
4213 two orders of magnitude. Freeing colors on TrueColor visuals is
4214 a nop, and pixel colors specify RGB values directly. See also
4215 the Xlib spec, chapter 3.1. */
4216 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4217 if (dpyinfo
->red_bits
> 0)
4219 unsigned long pr
, pg
, pb
;
4221 /* Apply gamma-correction like normal color allocation does. */
4225 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4226 gamma_correct (f
, &color
);
4227 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4230 /* Scale down RGB values to the visual's bits per RGB, and shift
4231 them to the right position in the pixel color. Note that the
4232 original RGB values are 16-bit values, as usual in X. */
4233 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4234 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4235 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4237 /* Assemble the pixel color. */
4238 return pr
| pg
| pb
;
4241 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4242 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4248 #ifdef HAVE_X_WINDOWS
4257 cmap
= FRAME_X_COLORMAP (f
);
4258 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4261 ++ct_colors_allocated
;
4262 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4266 p
->pixel
= color
.pixel
;
4267 p
->next
= ct_table
[i
];
4271 return FRAME_FOREGROUND_PIXEL (f
);
4276 color
= PALETTERGB (r
, g
, b
);
4278 color
= RGB_TO_ULONG (r
, g
, b
);
4279 #endif /* HAVE_NTGUI */
4280 ++ct_colors_allocated
;
4281 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4286 p
->next
= ct_table
[i
];
4288 #endif /* HAVE_X_WINDOWS */
4296 /* Look up pixel color PIXEL which is used on frame F in the color
4297 table. If not already present, allocate it. Value is PIXEL. */
4299 static unsigned long
4300 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4302 int i
= pixel
% CT_SIZE
;
4305 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4306 if (p
->pixel
== pixel
)
4315 #ifdef HAVE_X_WINDOWS
4316 cmap
= FRAME_X_COLORMAP (f
);
4317 color
.pixel
= pixel
;
4318 x_query_color (f
, &color
);
4319 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4322 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4323 color
.pixel
= pixel
;
4324 XQueryColor (NULL
, cmap
, &color
);
4325 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4327 #endif /* HAVE_X_WINDOWS */
4331 ++ct_colors_allocated
;
4333 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4338 p
->next
= ct_table
[i
];
4342 return FRAME_FOREGROUND_PIXEL (f
);
4348 /* Value is a vector of all pixel colors contained in the color table,
4349 allocated via xmalloc. Set *N to the number of colors. */
4351 static unsigned long *
4352 colors_in_color_table (int *n
)
4356 unsigned long *colors
;
4358 if (ct_colors_allocated
== 0)
4365 colors
= (unsigned long *) xmalloc (ct_colors_allocated
4367 *n
= ct_colors_allocated
;
4369 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4370 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4371 colors
[j
++] = p
->pixel
;
4377 #else /* COLOR_TABLE_SUPPORT */
4379 static unsigned long
4380 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4382 unsigned long pixel
;
4385 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4386 #endif /* HAVE_NTGUI */
4389 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4390 #endif /* HAVE_NS */
4395 init_color_table (void)
4398 #endif /* COLOR_TABLE_SUPPORT */
4401 /***********************************************************************
4403 ***********************************************************************/
4405 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4406 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4407 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4410 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4411 #endif /* HAVE_NTGUI */
4413 /* Edge detection matrices for different edge-detection
4416 static int emboss_matrix
[9] = {
4418 2, -1, 0, /* y - 1 */
4420 0, 1, -2 /* y + 1 */
4423 static int laplace_matrix
[9] = {
4425 1, 0, 0, /* y - 1 */
4427 0, 0, -1 /* y + 1 */
4430 /* Value is the intensity of the color whose red/green/blue values
4433 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4436 /* On frame F, return an array of XColor structures describing image
4437 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4438 non-zero means also fill the red/green/blue members of the XColor
4439 structures. Value is a pointer to the array of XColors structures,
4440 allocated with xmalloc; it must be freed by the caller. */
4443 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4447 XImagePtr_or_DC ximg
;
4451 #endif /* HAVE_NTGUI */
4453 colors
= (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *colors
);
4456 /* Get the X image IMG->pixmap. */
4457 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4458 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4460 /* Load the image into a memory device context. */
4461 hdc
= get_frame_dc (f
);
4462 ximg
= CreateCompatibleDC (hdc
);
4463 release_frame_dc (f
, hdc
);
4464 prev
= SelectObject (ximg
, img
->pixmap
);
4465 #endif /* HAVE_NTGUI */
4467 /* Fill the `pixel' members of the XColor array. I wished there
4468 were an easy and portable way to circumvent XGetPixel. */
4470 for (y
= 0; y
< img
->height
; ++y
)
4474 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4475 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4476 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4478 x_query_colors (f
, row
, img
->width
);
4482 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4484 /* W32_TODO: palette support needed here? */
4485 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4488 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4489 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4490 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4493 #endif /* HAVE_X_WINDOWS */
4496 Destroy_Image (ximg
, prev
);
4503 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4504 created with CreateDIBSection, with the pointer to the bit values
4505 stored in ximg->data. */
4508 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4510 int width
= ximg
->info
.bmiHeader
.biWidth
;
4511 unsigned char * pixel
;
4513 /* True color images. */
4514 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4516 int rowbytes
= width
* 3;
4517 /* Ensure scanlines are aligned on 4 byte boundaries. */
4519 rowbytes
+= 4 - (rowbytes
% 4);
4521 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4522 /* Windows bitmaps are in BGR order. */
4523 *pixel
= GetBValue (color
);
4524 *(pixel
+ 1) = GetGValue (color
);
4525 *(pixel
+ 2) = GetRValue (color
);
4527 /* Monochrome images. */
4528 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4530 int rowbytes
= width
/ 8;
4531 /* Ensure scanlines are aligned on 4 byte boundaries. */
4533 rowbytes
+= 4 - (rowbytes
% 4);
4534 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4535 /* Filter out palette info. */
4536 if (color
& 0x00ffffff)
4537 *pixel
= *pixel
| (1 << x
% 8);
4539 *pixel
= *pixel
& ~(1 << x
% 8);
4542 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4545 #endif /* HAVE_NTGUI */
4547 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4548 RGB members are set. F is the frame on which this all happens.
4549 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4552 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4555 XImagePtr oimg
= NULL
;
4559 init_color_table ();
4561 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4564 for (y
= 0; y
< img
->height
; ++y
)
4565 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4567 unsigned long pixel
;
4568 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4569 XPutPixel (oimg
, x
, y
, pixel
);
4573 x_clear_image_1 (f
, img
, 1, 0, 1);
4575 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4576 x_destroy_x_image (oimg
);
4577 img
->pixmap
= pixmap
;
4578 #ifdef COLOR_TABLE_SUPPORT
4579 img
->colors
= colors_in_color_table (&img
->ncolors
);
4580 free_color_table ();
4581 #endif /* COLOR_TABLE_SUPPORT */
4585 /* On frame F, perform edge-detection on image IMG.
4587 MATRIX is a nine-element array specifying the transformation
4588 matrix. See emboss_matrix for an example.
4590 COLOR_ADJUST is a color adjustment added to each pixel of the
4594 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4596 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4600 for (i
= sum
= 0; i
< 9; ++i
)
4601 sum
+= eabs (matrix
[i
]);
4603 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4605 new = (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *new);
4607 for (y
= 0; y
< img
->height
; ++y
)
4609 p
= COLOR (new, 0, y
);
4610 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4611 p
= COLOR (new, img
->width
- 1, y
);
4612 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4615 for (x
= 1; x
< img
->width
- 1; ++x
)
4617 p
= COLOR (new, x
, 0);
4618 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4619 p
= COLOR (new, x
, img
->height
- 1);
4620 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4623 for (y
= 1; y
< img
->height
- 1; ++y
)
4625 p
= COLOR (new, 1, y
);
4627 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4629 int r
, g
, b
, yy
, xx
;
4632 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4633 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4636 XColor
*t
= COLOR (colors
, xx
, yy
);
4637 r
+= matrix
[i
] * t
->red
;
4638 g
+= matrix
[i
] * t
->green
;
4639 b
+= matrix
[i
] * t
->blue
;
4642 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4643 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4644 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4645 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4650 x_from_xcolors (f
, img
, new);
4656 /* Perform the pre-defined `emboss' edge-detection on image IMG
4660 x_emboss (struct frame
*f
, struct image
*img
)
4662 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4666 /* Transform image IMG which is used on frame F with a Laplace
4667 edge-detection algorithm. The result is an image that can be used
4668 to draw disabled buttons, for example. */
4671 x_laplace (struct frame
*f
, struct image
*img
)
4673 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4677 /* Perform edge-detection on image IMG on frame F, with specified
4678 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4680 MATRIX must be either
4682 - a list of at least 9 numbers in row-major form
4683 - a vector of at least 9 numbers
4685 COLOR_ADJUST nil means use a default; otherwise it must be a
4689 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4690 Lisp_Object color_adjust
)
4698 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4699 ++i
, matrix
= XCDR (matrix
))
4700 trans
[i
] = XFLOATINT (XCAR (matrix
));
4702 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4704 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4705 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4708 if (NILP (color_adjust
))
4709 color_adjust
= make_number (0xffff / 2);
4711 if (i
== 9 && NUMBERP (color_adjust
))
4712 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4716 /* Transform image IMG on frame F so that it looks disabled. */
4719 x_disable_image (struct frame
*f
, struct image
*img
)
4721 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4723 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4725 int n_planes
= dpyinfo
->n_planes
;
4726 #endif /* HAVE_NTGUI */
4730 /* Color (or grayscale). Convert to gray, and equalize. Just
4731 drawing such images with a stipple can look very odd, so
4732 we're using this method instead. */
4733 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4735 const int h
= 15000;
4736 const int l
= 30000;
4738 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4742 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4743 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4744 p
->red
= p
->green
= p
->blue
= i2
;
4747 x_from_xcolors (f
, img
, colors
);
4750 /* Draw a cross over the disabled image, if we must or if we
4752 if (n_planes
< 2 || cross_disabled_images
)
4755 Display
*dpy
= FRAME_X_DISPLAY (f
);
4758 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4760 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4762 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4763 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4764 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4765 img
->width
- 1, img
->height
- 1);
4766 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4772 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4773 XSetForeground (dpy
, gc
, MaskForeground (f
));
4774 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4775 img
->width
- 1, img
->height
- 1);
4776 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4780 #endif /* !HAVE_NS */
4785 hdc
= get_frame_dc (f
);
4786 bmpdc
= CreateCompatibleDC (hdc
);
4787 release_frame_dc (f
, hdc
);
4789 prev
= SelectObject (bmpdc
, img
->pixmap
);
4791 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4792 MoveToEx (bmpdc
, 0, 0, NULL
);
4793 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4794 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4795 LineTo (bmpdc
, img
->width
- 1, 0);
4799 SelectObject (bmpdc
, img
->mask
);
4800 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4801 MoveToEx (bmpdc
, 0, 0, NULL
);
4802 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4803 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4804 LineTo (bmpdc
, img
->width
- 1, 0);
4806 SelectObject (bmpdc
, prev
);
4808 #endif /* HAVE_NTGUI */
4813 /* Build a mask for image IMG which is used on frame F. FILE is the
4814 name of an image file, for error messages. HOW determines how to
4815 determine the background color of IMG. If it is a list '(R G B)',
4816 with R, G, and B being integers >= 0, take that as the color of the
4817 background. Otherwise, determine the background color of IMG
4818 heuristically. Value is non-zero if successful. */
4821 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4823 XImagePtr_or_DC ximg
;
4831 #endif /* HAVE_NTGUI */
4832 int x
, y
, rc
, use_img_background
;
4833 unsigned long bg
= 0;
4837 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4838 img
->mask
= NO_PIXMAP
;
4839 img
->background_transparent_valid
= 0;
4844 /* Create an image and pixmap serving as mask. */
4845 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4846 &mask_img
, &img
->mask
);
4849 #endif /* !HAVE_NS */
4851 /* Get the X image of IMG->pixmap. */
4852 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4853 img
->width
, img
->height
,
4856 /* Create the bit array serving as mask. */
4857 row_width
= (img
->width
+ 7) / 8;
4858 mask_img
= xmalloc (row_width
* img
->height
);
4859 memset (mask_img
, 0, row_width
* img
->height
);
4861 /* Create a memory device context for IMG->pixmap. */
4862 frame_dc
= get_frame_dc (f
);
4863 ximg
= CreateCompatibleDC (frame_dc
);
4864 release_frame_dc (f
, frame_dc
);
4865 prev
= SelectObject (ximg
, img
->pixmap
);
4866 #endif /* HAVE_NTGUI */
4868 /* Determine the background color of ximg. If HOW is `(R G B)'
4869 take that as color. Otherwise, use the image's background color. */
4870 use_img_background
= 1;
4876 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4878 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4882 if (i
== 3 && NILP (how
))
4884 char color_name
[30];
4885 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4888 0x00ffffff & /* Filter out palette info. */
4889 #endif /* HAVE_NTGUI */
4890 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4891 use_img_background
= 0;
4895 if (use_img_background
)
4896 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4898 /* Set all bits in mask_img to 1 whose color in ximg is different
4899 from the background color bg. */
4901 for (y
= 0; y
< img
->height
; ++y
)
4902 for (x
= 0; x
< img
->width
; ++x
)
4904 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4905 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4907 if (XGetPixel (ximg
, x
, y
) == bg
)
4908 ns_set_alpha (ximg
, x
, y
, 0);
4909 #endif /* HAVE_NS */
4911 /* Fill in the background_transparent field while we have the mask handy. */
4912 image_background_transparent (img
, f
, mask_img
);
4914 /* Put mask_img into img->mask. */
4915 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4916 x_destroy_x_image (mask_img
);
4917 #endif /* !HAVE_NS */
4919 for (y
= 0; y
< img
->height
; ++y
)
4920 for (x
= 0; x
< img
->width
; ++x
)
4922 COLORREF p
= GetPixel (ximg
, x
, y
);
4924 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4927 /* Create the mask image. */
4928 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4930 /* Fill in the background_transparent field while we have the mask handy. */
4931 SelectObject (ximg
, img
->mask
);
4932 image_background_transparent (img
, f
, ximg
);
4934 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4936 #endif /* HAVE_NTGUI */
4938 Destroy_Image (ximg
, prev
);
4944 /***********************************************************************
4945 PBM (mono, gray, color)
4946 ***********************************************************************/
4948 static int pbm_image_p (Lisp_Object object
);
4949 static int pbm_load (struct frame
*f
, struct image
*img
);
4950 static int pbm_scan_number (unsigned char **, unsigned char *);
4952 /* The symbol `pbm' identifying images of this type. */
4954 static Lisp_Object Qpbm
;
4956 /* Indices of image specification fields in gs_format, below. */
4958 enum pbm_keyword_index
4974 /* Vector of image_keyword structures describing the format
4975 of valid user-defined image specifications. */
4977 static const struct image_keyword pbm_format
[PBM_LAST
] =
4979 {":type", IMAGE_SYMBOL_VALUE
, 1},
4980 {":file", IMAGE_STRING_VALUE
, 0},
4981 {":data", IMAGE_STRING_VALUE
, 0},
4982 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4983 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
4984 {":relief", IMAGE_INTEGER_VALUE
, 0},
4985 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4986 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4987 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4988 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4989 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4992 /* Structure describing the image type `pbm'. */
4994 static struct image_type pbm_type
=
5004 /* Return non-zero if OBJECT is a valid PBM image specification. */
5007 pbm_image_p (Lisp_Object object
)
5009 struct image_keyword fmt
[PBM_LAST
];
5011 memcpy (fmt
, pbm_format
, sizeof fmt
);
5013 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
5016 /* Must specify either :data or :file. */
5017 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5021 /* Scan a decimal number from *S and return it. Advance *S while
5022 reading the number. END is the end of the string. Value is -1 at
5026 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5028 int c
= 0, val
= -1;
5032 /* Skip white-space. */
5033 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
5038 /* Skip comment to end of line. */
5039 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5042 else if (isdigit (c
))
5044 /* Read decimal number. */
5046 while (*s
< end
&& (c
= *(*s
)++, isdigit (c
)))
5047 val
= 10 * val
+ c
- '0';
5059 #if 0 /* Unused. ++kfs */
5061 /* Read FILE into memory. Value is a pointer to a buffer allocated
5062 with xmalloc holding FILE's contents. Value is null if an error
5063 occurred. *SIZE is set to the size of the file. */
5066 pbm_read_file (Lisp_Object file
, int *size
)
5072 if (stat (SDATA (file
), &st
) == 0
5073 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5074 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
5075 && (buf
= (char *) xmalloc (st
.st_size
),
5076 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5095 #endif /* HAVE_NTGUI */
5097 /* Load PBM image IMG for use on frame F. */
5100 pbm_load (struct frame
*f
, struct image
*img
)
5103 int width
, height
, max_color_idx
= 0;
5105 Lisp_Object file
, specified_file
;
5106 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5107 unsigned char *contents
= NULL
;
5108 unsigned char *end
, *p
;
5111 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5113 if (STRINGP (specified_file
))
5115 file
= x_find_image_file (specified_file
);
5116 if (!STRINGP (file
))
5118 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5122 contents
= slurp_file (SSDATA (file
), &size
);
5123 if (contents
== NULL
)
5125 image_error ("Error reading `%s'", file
, Qnil
);
5130 end
= contents
+ size
;
5135 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5136 if (!STRINGP (data
))
5138 image_error ("Invalid image data `%s'", data
, Qnil
);
5142 end
= p
+ SBYTES (data
);
5145 /* Check magic number. */
5146 if (end
- p
< 2 || *p
++ != 'P')
5148 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5157 raw_p
= 0, type
= PBM_MONO
;
5161 raw_p
= 0, type
= PBM_GRAY
;
5165 raw_p
= 0, type
= PBM_COLOR
;
5169 raw_p
= 1, type
= PBM_MONO
;
5173 raw_p
= 1, type
= PBM_GRAY
;
5177 raw_p
= 1, type
= PBM_COLOR
;
5181 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5185 /* Read width, height, maximum color-component. Characters
5186 starting with `#' up to the end of a line are ignored. */
5187 width
= pbm_scan_number (&p
, end
);
5188 height
= pbm_scan_number (&p
, end
);
5190 if (type
!= PBM_MONO
)
5192 max_color_idx
= pbm_scan_number (&p
, end
);
5193 if (max_color_idx
> 65535 || max_color_idx
< 0)
5195 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5200 if (!check_image_size (f
, width
, height
))
5202 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5206 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5207 &ximg
, &img
->pixmap
))
5210 /* Initialize the color hash table. */
5211 init_color_table ();
5213 if (type
== PBM_MONO
)
5216 struct image_keyword fmt
[PBM_LAST
];
5217 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5218 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5220 /* Parse the image specification. */
5221 memcpy (fmt
, pbm_format
, sizeof fmt
);
5222 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5224 /* Get foreground and background colors, maybe allocate colors. */
5225 if (fmt
[PBM_FOREGROUND
].count
5226 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5227 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5228 if (fmt
[PBM_BACKGROUND
].count
5229 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5231 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5232 img
->background
= bg
;
5233 img
->background_valid
= 1;
5236 for (y
= 0; y
< height
; ++y
)
5237 for (x
= 0; x
< width
; ++x
)
5245 x_destroy_x_image (ximg
);
5246 x_clear_image (f
, img
);
5247 image_error ("Invalid image size in image `%s'",
5257 g
= pbm_scan_number (&p
, end
);
5259 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5264 int expected_size
= height
* width
;
5265 if (max_color_idx
> 255)
5267 if (type
== PBM_COLOR
)
5270 if (raw_p
&& p
+ expected_size
> end
)
5272 x_destroy_x_image (ximg
);
5273 x_clear_image (f
, img
);
5274 image_error ("Invalid image size in image `%s'",
5279 for (y
= 0; y
< height
; ++y
)
5280 for (x
= 0; x
< width
; ++x
)
5284 if (type
== PBM_GRAY
&& raw_p
)
5287 if (max_color_idx
> 255)
5288 r
= g
= b
= r
* 256 + *p
++;
5290 else if (type
== PBM_GRAY
)
5291 r
= g
= b
= pbm_scan_number (&p
, end
);
5295 if (max_color_idx
> 255)
5298 if (max_color_idx
> 255)
5301 if (max_color_idx
> 255)
5306 r
= pbm_scan_number (&p
, end
);
5307 g
= pbm_scan_number (&p
, end
);
5308 b
= pbm_scan_number (&p
, end
);
5311 if (r
< 0 || g
< 0 || b
< 0)
5313 x_destroy_x_image (ximg
);
5314 image_error ("Invalid pixel value in image `%s'",
5319 /* RGB values are now in the range 0..max_color_idx.
5320 Scale this to the range 0..0xffff supported by X. */
5321 r
= (double) r
* 65535 / max_color_idx
;
5322 g
= (double) g
* 65535 / max_color_idx
;
5323 b
= (double) b
* 65535 / max_color_idx
;
5324 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5328 #ifdef COLOR_TABLE_SUPPORT
5329 /* Store in IMG->colors the colors allocated for the image, and
5330 free the color table. */
5331 img
->colors
= colors_in_color_table (&img
->ncolors
);
5332 free_color_table ();
5333 #endif /* COLOR_TABLE_SUPPORT */
5336 img
->height
= height
;
5338 /* Maybe fill in the background field while we have ximg handy. */
5340 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5341 /* Casting avoids a GCC warning. */
5342 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5344 /* Put the image into a pixmap. */
5345 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5346 x_destroy_x_image (ximg
);
5348 /* X and W32 versions did it here, MAC version above. ++kfs
5350 img->height = height; */
5357 /***********************************************************************
5359 ***********************************************************************/
5361 #if defined (HAVE_PNG) || defined (HAVE_NS)
5363 /* Function prototypes. */
5365 static int png_image_p (Lisp_Object object
);
5366 static int png_load (struct frame
*f
, struct image
*img
);
5368 /* The symbol `png' identifying images of this type. */
5370 static Lisp_Object Qpng
;
5372 /* Indices of image specification fields in png_format, below. */
5374 enum png_keyword_index
5389 /* Vector of image_keyword structures describing the format
5390 of valid user-defined image specifications. */
5392 static const struct image_keyword png_format
[PNG_LAST
] =
5394 {":type", IMAGE_SYMBOL_VALUE
, 1},
5395 {":data", IMAGE_STRING_VALUE
, 0},
5396 {":file", IMAGE_STRING_VALUE
, 0},
5397 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5398 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5399 {":relief", IMAGE_INTEGER_VALUE
, 0},
5400 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5401 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5402 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5403 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5406 /* Structure describing the image type `png'. */
5408 static struct image_type png_type
=
5417 /* Return non-zero if OBJECT is a valid PNG image specification. */
5420 png_image_p (Lisp_Object object
)
5422 struct image_keyword fmt
[PNG_LAST
];
5423 memcpy (fmt
, png_format
, sizeof fmt
);
5425 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5428 /* Must specify either the :data or :file keyword. */
5429 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5432 #endif /* HAVE_PNG || HAVE_NS */
5438 /* PNG library details. */
5440 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5441 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5442 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5443 png_error_ptr
, png_error_ptr
));
5444 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5445 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5446 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5447 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5448 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5449 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5450 png_uint_32
*, png_uint_32
*,
5451 int *, int *, int *, int *, int *));
5452 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5453 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5454 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5455 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5456 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5458 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5459 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5460 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5461 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5462 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5463 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5464 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5466 #if (PNG_LIBPNG_VER >= 10500)
5467 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5468 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5469 #endif /* libpng version >= 1.5 */
5472 init_png_functions (Lisp_Object libraries
)
5476 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5479 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5480 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5481 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5482 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5483 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5484 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5485 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5486 LOAD_IMGLIB_FN (library
, png_read_info
);
5487 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5488 LOAD_IMGLIB_FN (library
, png_get_valid
);
5489 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5490 LOAD_IMGLIB_FN (library
, png_set_expand
);
5491 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5492 LOAD_IMGLIB_FN (library
, png_set_background
);
5493 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5494 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5495 LOAD_IMGLIB_FN (library
, png_get_channels
);
5496 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5497 LOAD_IMGLIB_FN (library
, png_read_image
);
5498 LOAD_IMGLIB_FN (library
, png_read_end
);
5499 LOAD_IMGLIB_FN (library
, png_error
);
5501 #if (PNG_LIBPNG_VER >= 10500)
5502 LOAD_IMGLIB_FN (library
, png_longjmp
);
5503 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5504 #endif /* libpng version >= 1.5 */
5510 #define fn_png_get_io_ptr png_get_io_ptr
5511 #define fn_png_sig_cmp png_sig_cmp
5512 #define fn_png_create_read_struct png_create_read_struct
5513 #define fn_png_create_info_struct png_create_info_struct
5514 #define fn_png_destroy_read_struct png_destroy_read_struct
5515 #define fn_png_set_read_fn png_set_read_fn
5516 #define fn_png_set_sig_bytes png_set_sig_bytes
5517 #define fn_png_read_info png_read_info
5518 #define fn_png_get_IHDR png_get_IHDR
5519 #define fn_png_get_valid png_get_valid
5520 #define fn_png_set_strip_16 png_set_strip_16
5521 #define fn_png_set_expand png_set_expand
5522 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5523 #define fn_png_set_background png_set_background
5524 #define fn_png_get_bKGD png_get_bKGD
5525 #define fn_png_read_update_info png_read_update_info
5526 #define fn_png_get_channels png_get_channels
5527 #define fn_png_get_rowbytes png_get_rowbytes
5528 #define fn_png_read_image png_read_image
5529 #define fn_png_read_end png_read_end
5530 #define fn_png_error png_error
5532 #if (PNG_LIBPNG_VER >= 10500)
5533 #define fn_png_longjmp png_longjmp
5534 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5535 #endif /* libpng version >= 1.5 */
5537 #endif /* HAVE_NTGUI */
5540 #if (PNG_LIBPNG_VER < 10500)
5541 #define PNG_LONGJMP(ptr) (longjmp ((ptr)->jmpbuf, 1))
5542 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5544 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5545 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5546 #define PNG_JMPBUF(ptr) \
5547 (*fn_png_set_longjmp_fn((ptr), longjmp, sizeof (jmp_buf)))
5550 /* Error and warning handlers installed when the PNG library
5553 static void my_png_error (png_struct
*, const char *) NO_RETURN
;
5555 my_png_error (png_struct
*png_ptr
, const char *msg
)
5557 xassert (png_ptr
!= NULL
);
5558 /* Avoid compiler warning about deprecated direct access to
5559 png_ptr's fields in libpng versions 1.4.x. */
5560 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5561 PNG_LONGJMP (png_ptr
);
5566 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5568 xassert (png_ptr
!= NULL
);
5569 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5572 /* Memory source for PNG decoding. */
5574 struct png_memory_storage
5576 unsigned char *bytes
; /* The data */
5577 ptrdiff_t len
; /* How big is it? */
5578 ptrdiff_t index
; /* Where are we? */
5582 /* Function set as reader function when reading PNG image from memory.
5583 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5584 bytes from the input to DATA. */
5587 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5589 struct png_memory_storage
*tbr
5590 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5592 if (length
> tbr
->len
- tbr
->index
)
5593 fn_png_error (png_ptr
, "Read error");
5595 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5596 tbr
->index
= tbr
->index
+ length
;
5600 /* Function set as reader function when reading PNG image from a file.
5601 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5602 bytes from the input to DATA. */
5605 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5607 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5609 if (fread (data
, 1, length
, fp
) < length
)
5610 fn_png_error (png_ptr
, "Read error");
5614 /* Load PNG image IMG for use on frame F. Value is non-zero if
5618 png_load (struct frame
*f
, struct image
*img
)
5620 Lisp_Object file
, specified_file
;
5621 Lisp_Object specified_data
;
5624 XImagePtr ximg
, mask_img
= NULL
;
5625 png_struct
*png_ptr
= NULL
;
5626 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5627 FILE *volatile fp
= NULL
;
5629 png_byte
* volatile pixels
= NULL
;
5630 png_byte
** volatile rows
= NULL
;
5631 png_uint_32 width
, height
;
5632 int bit_depth
, color_type
, interlace_type
;
5634 png_uint_32 row_bytes
;
5636 struct png_memory_storage tbr
; /* Data to be read */
5638 /* Find out what file to load. */
5639 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5640 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5642 if (NILP (specified_data
))
5644 file
= x_find_image_file (specified_file
);
5645 if (!STRINGP (file
))
5647 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5651 /* Open the image file. */
5652 fp
= fopen (SSDATA (file
), "rb");
5655 image_error ("Cannot open image file `%s'", file
, Qnil
);
5659 /* Check PNG signature. */
5660 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5661 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5663 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5670 if (!STRINGP (specified_data
))
5672 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5676 /* Read from memory. */
5677 tbr
.bytes
= SDATA (specified_data
);
5678 tbr
.len
= SBYTES (specified_data
);
5681 /* Check PNG signature. */
5682 if (tbr
.len
< sizeof sig
5683 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5685 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5689 /* Need to skip past the signature. */
5690 tbr
.bytes
+= sizeof (sig
);
5693 /* Initialize read and info structs for PNG lib. */
5694 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5699 if (fp
) fclose (fp
);
5703 info_ptr
= fn_png_create_info_struct (png_ptr
);
5706 fn_png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
5707 if (fp
) fclose (fp
);
5711 end_info
= fn_png_create_info_struct (png_ptr
);
5714 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
5715 if (fp
) fclose (fp
);
5719 /* Set error jump-back. We come back here when the PNG library
5720 detects an error. */
5721 if (setjmp (PNG_JMPBUF (png_ptr
)))
5725 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5728 if (fp
) fclose (fp
);
5732 /* Read image info. */
5733 if (!NILP (specified_data
))
5734 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5736 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5738 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5739 fn_png_read_info (png_ptr
, info_ptr
);
5740 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5741 &interlace_type
, NULL
, NULL
);
5743 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5744 && check_image_size (f
, width
, height
)))
5746 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5750 /* Create the X image and pixmap now, so that the work below can be
5751 omitted if the image is too large for X. */
5752 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5756 /* If image contains simply transparency data, we prefer to
5757 construct a clipping mask. */
5758 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5763 /* This function is easier to write if we only have to handle
5764 one data format: RGB or RGBA with 8 bits per channel. Let's
5765 transform other formats into that format. */
5767 /* Strip more than 8 bits per channel. */
5768 if (bit_depth
== 16)
5769 fn_png_set_strip_16 (png_ptr
);
5771 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5773 fn_png_set_expand (png_ptr
);
5775 /* Convert grayscale images to RGB. */
5776 if (color_type
== PNG_COLOR_TYPE_GRAY
5777 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5778 fn_png_set_gray_to_rgb (png_ptr
);
5780 /* Handle alpha channel by combining the image with a background
5781 color. Do this only if a real alpha channel is supplied. For
5782 simple transparency, we prefer a clipping mask. */
5785 /* png_color_16 *image_bg; */
5786 Lisp_Object specified_bg
5787 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5788 int shift
= (bit_depth
== 16) ? 0 : 8;
5790 if (STRINGP (specified_bg
))
5791 /* The user specified `:background', use that. */
5794 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5796 png_color_16 user_bg
;
5798 memset (&user_bg
, 0, sizeof user_bg
);
5799 user_bg
.red
= color
.red
>> shift
;
5800 user_bg
.green
= color
.green
>> shift
;
5801 user_bg
.blue
= color
.blue
>> shift
;
5803 fn_png_set_background (png_ptr
, &user_bg
,
5804 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5809 /* We use the current frame background, ignoring any default
5810 background color set by the image. */
5811 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5813 png_color_16 frame_background
;
5815 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5816 x_query_color (f
, &color
);
5818 memset (&frame_background
, 0, sizeof frame_background
);
5819 frame_background
.red
= color
.red
>> shift
;
5820 frame_background
.green
= color
.green
>> shift
;
5821 frame_background
.blue
= color
.blue
>> shift
;
5822 #endif /* HAVE_X_WINDOWS */
5824 fn_png_set_background (png_ptr
, &frame_background
,
5825 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5829 /* Update info structure. */
5830 fn_png_read_update_info (png_ptr
, info_ptr
);
5832 /* Get number of channels. Valid values are 1 for grayscale images
5833 and images with a palette, 2 for grayscale images with transparency
5834 information (alpha channel), 3 for RGB images, and 4 for RGB
5835 images with alpha channel, i.e. RGBA. If conversions above were
5836 sufficient we should only have 3 or 4 channels here. */
5837 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5838 xassert (channels
== 3 || channels
== 4);
5840 /* Number of bytes needed for one row of the image. */
5841 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5843 /* Allocate memory for the image. */
5844 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5845 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5846 memory_full (SIZE_MAX
);
5847 pixels
= (png_byte
*) xmalloc (sizeof *pixels
* row_bytes
* height
);
5848 rows
= (png_byte
**) xmalloc (height
* sizeof *rows
);
5849 for (i
= 0; i
< height
; ++i
)
5850 rows
[i
] = pixels
+ i
* row_bytes
;
5852 /* Read the entire image. */
5853 fn_png_read_image (png_ptr
, rows
);
5854 fn_png_read_end (png_ptr
, info_ptr
);
5861 /* Create an image and pixmap serving as mask if the PNG image
5862 contains an alpha channel. */
5865 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5866 &mask_img
, &img
->mask
))
5868 x_destroy_x_image (ximg
);
5869 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5870 img
->pixmap
= NO_PIXMAP
;
5874 /* Fill the X image and mask from PNG data. */
5875 init_color_table ();
5877 for (y
= 0; y
< height
; ++y
)
5879 png_byte
*p
= rows
[y
];
5881 for (x
= 0; x
< width
; ++x
)
5888 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5889 /* An alpha channel, aka mask channel, associates variable
5890 transparency with an image. Where other image formats
5891 support binary transparency---fully transparent or fully
5892 opaque---PNG allows up to 254 levels of partial transparency.
5893 The PNG library implements partial transparency by combining
5894 the image with a specified background color.
5896 I'm not sure how to handle this here nicely: because the
5897 background on which the image is displayed may change, for
5898 real alpha channel support, it would be necessary to create
5899 a new image for each possible background.
5901 What I'm doing now is that a mask is created if we have
5902 boolean transparency information. Otherwise I'm using
5903 the frame's background color to combine the image with. */
5908 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5914 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5915 /* Set IMG's background color from the PNG image, unless the user
5919 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5921 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5922 img
->background_valid
= 1;
5926 #ifdef COLOR_TABLE_SUPPORT
5927 /* Remember colors allocated for this image. */
5928 img
->colors
= colors_in_color_table (&img
->ncolors
);
5929 free_color_table ();
5930 #endif /* COLOR_TABLE_SUPPORT */
5933 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5938 img
->height
= height
;
5940 /* Maybe fill in the background field while we have ximg handy.
5941 Casting avoids a GCC warning. */
5942 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5944 /* Put the image into the pixmap, then free the X image and its buffer. */
5945 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5946 x_destroy_x_image (ximg
);
5948 /* Same for the mask. */
5951 /* Fill in the background_transparent field while we have the
5952 mask handy. Casting avoids a GCC warning. */
5953 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5955 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5956 x_destroy_x_image (mask_img
);
5962 #else /* HAVE_PNG */
5966 png_load (struct frame
*f
, struct image
*img
)
5968 return ns_load_image(f
, img
,
5969 image_spec_value (img
->spec
, QCfile
, NULL
),
5970 image_spec_value (img
->spec
, QCdata
, NULL
));
5972 #endif /* HAVE_NS */
5975 #endif /* !HAVE_PNG */
5979 /***********************************************************************
5981 ***********************************************************************/
5983 #if defined (HAVE_JPEG) || defined (HAVE_NS)
5985 static int jpeg_image_p (Lisp_Object object
);
5986 static int jpeg_load (struct frame
*f
, struct image
*img
);
5988 /* The symbol `jpeg' identifying images of this type. */
5990 static Lisp_Object Qjpeg
;
5992 /* Indices of image specification fields in gs_format, below. */
5994 enum jpeg_keyword_index
6003 JPEG_HEURISTIC_MASK
,
6009 /* Vector of image_keyword structures describing the format
6010 of valid user-defined image specifications. */
6012 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
6014 {":type", IMAGE_SYMBOL_VALUE
, 1},
6015 {":data", IMAGE_STRING_VALUE
, 0},
6016 {":file", IMAGE_STRING_VALUE
, 0},
6017 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6018 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6019 {":relief", IMAGE_INTEGER_VALUE
, 0},
6020 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6021 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6022 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6023 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6026 /* Structure describing the image type `jpeg'. */
6028 static struct image_type jpeg_type
=
6037 /* Return non-zero if OBJECT is a valid JPEG image specification. */
6040 jpeg_image_p (Lisp_Object object
)
6042 struct image_keyword fmt
[JPEG_LAST
];
6044 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6046 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6049 /* Must specify either the :data or :file keyword. */
6050 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6053 #endif /* HAVE_JPEG || HAVE_NS */
6057 /* Work around a warning about HAVE_STDLIB_H being redefined in
6059 #ifdef HAVE_STDLIB_H
6060 #undef HAVE_STDLIB_H
6061 #endif /* HAVE_STLIB_H */
6063 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6064 /* In older releases of the jpeg library, jpeglib.h will define boolean
6065 differently depending on __WIN32__, so make sure it is defined. */
6069 #include <jpeglib.h>
6072 #ifdef HAVE_STLIB_H_1
6073 #define HAVE_STDLIB_H 1
6078 /* JPEG library details. */
6079 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6080 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6081 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6082 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6083 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6084 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6085 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6086 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6089 init_jpeg_functions (Lisp_Object libraries
)
6093 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6096 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6097 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6098 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6099 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6100 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6101 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6102 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6103 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6107 /* Wrapper since we can't directly assign the function pointer
6108 to another function pointer that was declared more completely easily. */
6110 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6112 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6117 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a)
6118 #define fn_jpeg_start_decompress jpeg_start_decompress
6119 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6120 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6121 #define fn_jpeg_read_header jpeg_read_header
6122 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6123 #define fn_jpeg_std_error jpeg_std_error
6124 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6126 #endif /* HAVE_NTGUI */
6128 struct my_jpeg_error_mgr
6130 struct jpeg_error_mgr pub
;
6131 jmp_buf setjmp_buffer
;
6135 static void my_error_exit (j_common_ptr
) NO_RETURN
;
6137 my_error_exit (j_common_ptr cinfo
)
6139 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6140 longjmp (mgr
->setjmp_buffer
, 1);
6144 /* Init source method for JPEG data source manager. Called by
6145 jpeg_read_header() before any data is actually read. See
6146 libjpeg.doc from the JPEG lib distribution. */
6149 our_common_init_source (j_decompress_ptr cinfo
)
6154 /* Method to terminate data source. Called by
6155 jpeg_finish_decompress() after all data has been processed. */
6158 our_common_term_source (j_decompress_ptr cinfo
)
6163 /* Fill input buffer method for JPEG data source manager. Called
6164 whenever more data is needed. We read the whole image in one step,
6165 so this only adds a fake end of input marker at the end. */
6167 static JOCTET our_memory_buffer
[2];
6170 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6172 /* Insert a fake EOI marker. */
6173 struct jpeg_source_mgr
*src
= cinfo
->src
;
6175 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6176 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6178 src
->next_input_byte
= our_memory_buffer
;
6179 src
->bytes_in_buffer
= 2;
6184 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6185 is the JPEG data source manager. */
6188 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6190 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6194 if (num_bytes
> src
->bytes_in_buffer
)
6195 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6197 src
->bytes_in_buffer
-= num_bytes
;
6198 src
->next_input_byte
+= num_bytes
;
6203 /* Set up the JPEG lib for reading an image from DATA which contains
6204 LEN bytes. CINFO is the decompression info structure created for
6205 reading the image. */
6208 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, unsigned int len
)
6210 struct jpeg_source_mgr
*src
;
6212 if (cinfo
->src
== NULL
)
6214 /* First time for this JPEG object? */
6215 cinfo
->src
= (struct jpeg_source_mgr
*)
6216 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6217 sizeof (struct jpeg_source_mgr
));
6218 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6219 src
->next_input_byte
= data
;
6222 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6223 src
->init_source
= our_common_init_source
;
6224 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6225 src
->skip_input_data
= our_memory_skip_input_data
;
6226 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6227 src
->term_source
= our_common_term_source
;
6228 src
->bytes_in_buffer
= len
;
6229 src
->next_input_byte
= data
;
6233 struct jpeg_stdio_mgr
6235 struct jpeg_source_mgr mgr
;
6242 /* Size of buffer to read JPEG from file.
6243 Not too big, as we want to use alloc_small. */
6244 #define JPEG_STDIO_BUFFER_SIZE 8192
6247 /* Fill input buffer method for JPEG data source manager. Called
6248 whenever more data is needed. The data is read from a FILE *. */
6251 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6253 struct jpeg_stdio_mgr
*src
;
6255 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6260 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6262 src
->mgr
.bytes_in_buffer
= bytes
;
6265 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6267 src
->buffer
[0] = (JOCTET
) 0xFF;
6268 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6269 src
->mgr
.bytes_in_buffer
= 2;
6271 src
->mgr
.next_input_byte
= src
->buffer
;
6278 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6279 is the JPEG data source manager. */
6282 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6284 struct jpeg_stdio_mgr
*src
;
6285 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6287 while (num_bytes
> 0 && !src
->finished
)
6289 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6291 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6292 src
->mgr
.next_input_byte
+= num_bytes
;
6297 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6298 src
->mgr
.bytes_in_buffer
= 0;
6299 src
->mgr
.next_input_byte
= NULL
;
6301 our_stdio_fill_input_buffer (cinfo
);
6307 /* Set up the JPEG lib for reading an image from a FILE *.
6308 CINFO is the decompression info structure created for
6309 reading the image. */
6312 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6314 struct jpeg_stdio_mgr
*src
;
6316 if (cinfo
->src
!= NULL
)
6317 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6320 /* First time for this JPEG object? */
6321 cinfo
->src
= (struct jpeg_source_mgr
*)
6322 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6323 sizeof (struct jpeg_stdio_mgr
));
6324 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6325 src
->buffer
= (JOCTET
*)
6326 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6327 JPEG_STDIO_BUFFER_SIZE
);
6332 src
->mgr
.init_source
= our_common_init_source
;
6333 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6334 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6335 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6336 src
->mgr
.term_source
= our_common_term_source
;
6337 src
->mgr
.bytes_in_buffer
= 0;
6338 src
->mgr
.next_input_byte
= NULL
;
6342 /* Load image IMG for use on frame F. Patterned after example.c
6343 from the JPEG lib. */
6346 jpeg_load (struct frame
*f
, struct image
*img
)
6348 struct jpeg_decompress_struct cinfo
;
6349 struct my_jpeg_error_mgr mgr
;
6350 Lisp_Object file
, specified_file
;
6351 Lisp_Object specified_data
;
6352 FILE * volatile fp
= NULL
;
6354 int row_stride
, x
, y
;
6355 XImagePtr ximg
= NULL
;
6357 unsigned long *colors
;
6360 /* Open the JPEG file. */
6361 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6362 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6364 if (NILP (specified_data
))
6366 file
= x_find_image_file (specified_file
);
6367 if (!STRINGP (file
))
6369 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6373 fp
= fopen (SSDATA (file
), "rb");
6376 image_error ("Cannot open `%s'", file
, Qnil
);
6380 else if (!STRINGP (specified_data
))
6382 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6386 /* Customize libjpeg's error handling to call my_error_exit when an
6387 error is detected. This function will perform a longjmp. */
6388 cinfo
.err
= fn_jpeg_std_error (&mgr
.pub
);
6389 mgr
.pub
.error_exit
= my_error_exit
;
6391 if ((rc
= setjmp (mgr
.setjmp_buffer
)) != 0)
6395 /* Called from my_error_exit. Display a JPEG error. */
6396 char buf
[JMSG_LENGTH_MAX
];
6397 cinfo
.err
->format_message ((j_common_ptr
) &cinfo
, buf
);
6398 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6399 build_string (buf
));
6402 /* Close the input file and destroy the JPEG object. */
6404 fclose ((FILE *) fp
);
6405 fn_jpeg_destroy_decompress (&cinfo
);
6407 /* If we already have an XImage, free that. */
6408 x_destroy_x_image (ximg
);
6410 /* Free pixmap and colors. */
6411 x_clear_image (f
, img
);
6415 /* Create the JPEG decompression object. Let it read from fp.
6416 Read the JPEG image header. */
6417 fn_jpeg_CreateDecompress (&cinfo
, JPEG_LIB_VERSION
, sizeof (cinfo
));
6419 if (NILP (specified_data
))
6420 jpeg_file_src (&cinfo
, (FILE *) fp
);
6422 jpeg_memory_src (&cinfo
, SDATA (specified_data
),
6423 SBYTES (specified_data
));
6425 fn_jpeg_read_header (&cinfo
, 1);
6427 /* Customize decompression so that color quantization will be used.
6428 Start decompression. */
6429 cinfo
.quantize_colors
= 1;
6430 fn_jpeg_start_decompress (&cinfo
);
6431 width
= img
->width
= cinfo
.output_width
;
6432 height
= img
->height
= cinfo
.output_height
;
6434 if (!check_image_size (f
, width
, height
))
6436 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6437 longjmp (mgr
.setjmp_buffer
, 2);
6440 /* Create X image and pixmap. */
6441 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6442 longjmp (mgr
.setjmp_buffer
, 2);
6444 /* Allocate colors. When color quantization is used,
6445 cinfo.actual_number_of_colors has been set with the number of
6446 colors generated, and cinfo.colormap is a two-dimensional array
6447 of color indices in the range 0..cinfo.actual_number_of_colors.
6448 No more than 255 colors will be generated. */
6452 if (cinfo
.out_color_components
> 2)
6453 ir
= 0, ig
= 1, ib
= 2;
6454 else if (cinfo
.out_color_components
> 1)
6455 ir
= 0, ig
= 1, ib
= 0;
6457 ir
= 0, ig
= 0, ib
= 0;
6459 /* Use the color table mechanism because it handles colors that
6460 cannot be allocated nicely. Such colors will be replaced with
6461 a default color, and we don't have to care about which colors
6462 can be freed safely, and which can't. */
6463 init_color_table ();
6464 colors
= (unsigned long *) alloca (cinfo
.actual_number_of_colors
6467 for (i
= 0; i
< cinfo
.actual_number_of_colors
; ++i
)
6469 /* Multiply RGB values with 255 because X expects RGB values
6470 in the range 0..0xffff. */
6471 int r
= cinfo
.colormap
[ir
][i
] << 8;
6472 int g
= cinfo
.colormap
[ig
][i
] << 8;
6473 int b
= cinfo
.colormap
[ib
][i
] << 8;
6474 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6477 #ifdef COLOR_TABLE_SUPPORT
6478 /* Remember those colors actually allocated. */
6479 img
->colors
= colors_in_color_table (&img
->ncolors
);
6480 free_color_table ();
6481 #endif /* COLOR_TABLE_SUPPORT */
6485 row_stride
= width
* cinfo
.output_components
;
6486 buffer
= cinfo
.mem
->alloc_sarray ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
,
6488 for (y
= 0; y
< height
; ++y
)
6490 fn_jpeg_read_scanlines (&cinfo
, buffer
, 1);
6491 for (x
= 0; x
< cinfo
.output_width
; ++x
)
6492 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6496 fn_jpeg_finish_decompress (&cinfo
);
6497 fn_jpeg_destroy_decompress (&cinfo
);
6499 fclose ((FILE *) fp
);
6501 /* Maybe fill in the background field while we have ximg handy. */
6502 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6503 /* Casting avoids a GCC warning. */
6504 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6506 /* Put the image into the pixmap. */
6507 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6508 x_destroy_x_image (ximg
);
6512 #else /* HAVE_JPEG */
6516 jpeg_load (struct frame
*f
, struct image
*img
)
6518 return ns_load_image (f
, img
,
6519 image_spec_value (img
->spec
, QCfile
, NULL
),
6520 image_spec_value (img
->spec
, QCdata
, NULL
));
6522 #endif /* HAVE_NS */
6524 #endif /* !HAVE_JPEG */
6528 /***********************************************************************
6530 ***********************************************************************/
6532 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6534 static int tiff_image_p (Lisp_Object object
);
6535 static int tiff_load (struct frame
*f
, struct image
*img
);
6537 /* The symbol `tiff' identifying images of this type. */
6539 static Lisp_Object Qtiff
;
6541 /* Indices of image specification fields in tiff_format, below. */
6543 enum tiff_keyword_index
6552 TIFF_HEURISTIC_MASK
,
6559 /* Vector of image_keyword structures describing the format
6560 of valid user-defined image specifications. */
6562 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6564 {":type", IMAGE_SYMBOL_VALUE
, 1},
6565 {":data", IMAGE_STRING_VALUE
, 0},
6566 {":file", IMAGE_STRING_VALUE
, 0},
6567 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6568 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6569 {":relief", IMAGE_INTEGER_VALUE
, 0},
6570 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6571 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6572 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6573 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6574 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6577 /* Structure describing the image type `tiff'. */
6579 static struct image_type tiff_type
=
6588 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6591 tiff_image_p (Lisp_Object object
)
6593 struct image_keyword fmt
[TIFF_LAST
];
6594 memcpy (fmt
, tiff_format
, sizeof fmt
);
6596 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6599 /* Must specify either the :data or :file keyword. */
6600 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6603 #endif /* HAVE_TIFF || HAVE_NS */
6611 /* TIFF library details. */
6612 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6613 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6614 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6615 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6616 TIFFReadWriteProc
, TIFFReadWriteProc
,
6617 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6618 TIFFMapFileProc
, TIFFUnmapFileProc
));
6619 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6620 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6621 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6622 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6625 init_tiff_functions (Lisp_Object libraries
)
6629 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6632 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6633 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6634 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6635 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6636 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6637 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6638 LOAD_IMGLIB_FN (library
, TIFFClose
);
6639 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6645 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6646 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6647 #define fn_TIFFOpen TIFFOpen
6648 #define fn_TIFFClientOpen TIFFClientOpen
6649 #define fn_TIFFGetField TIFFGetField
6650 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6651 #define fn_TIFFClose TIFFClose
6652 #define fn_TIFFSetDirectory TIFFSetDirectory
6653 #endif /* HAVE_NTGUI */
6656 /* Reading from a memory buffer for TIFF images Based on the PNG
6657 memory source, but we have to provide a lot of extra functions.
6660 We really only need to implement read and seek, but I am not
6661 convinced that the TIFF library is smart enough not to destroy
6662 itself if we only hand it the function pointers we need to
6667 unsigned char *bytes
;
6674 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6676 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6678 size
= min (size
, src
->len
- src
->index
);
6679 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6685 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6691 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6693 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6698 case SEEK_SET
: /* Go from beginning of source. */
6702 case SEEK_END
: /* Go from end of source. */
6703 idx
= src
->len
+ off
;
6706 case SEEK_CUR
: /* Go from current position. */
6707 idx
= src
->index
+ off
;
6710 default: /* Invalid `whence'. */
6714 if (idx
> src
->len
|| idx
< 0)
6722 tiff_close_memory (thandle_t data
)
6729 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6731 /* It is already _IN_ memory. */
6736 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6738 /* We don't need to do this. */
6742 tiff_size_of_memory (thandle_t data
)
6744 return ((tiff_memory_source
*) data
)->len
;
6748 static void tiff_error_handler (const char *, const char *, va_list)
6749 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6751 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6756 len
= sprintf (buf
, "TIFF error: %s ", title
);
6757 vsprintf (buf
+ len
, format
, ap
);
6758 add_to_log (buf
, Qnil
, Qnil
);
6762 static void tiff_warning_handler (const char *, const char *, va_list)
6763 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6765 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6770 len
= sprintf (buf
, "TIFF warning: %s ", title
);
6771 vsprintf (buf
+ len
, format
, ap
);
6772 add_to_log (buf
, Qnil
, Qnil
);
6776 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6780 tiff_load (struct frame
*f
, struct image
*img
)
6782 Lisp_Object file
, specified_file
;
6783 Lisp_Object specified_data
;
6785 int width
, height
, x
, y
, count
;
6789 tiff_memory_source memsrc
;
6792 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6793 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6795 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6796 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6798 if (NILP (specified_data
))
6800 /* Read from a file */
6801 file
= x_find_image_file (specified_file
);
6802 if (!STRINGP (file
))
6804 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6808 /* Try to open the image file. */
6809 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6812 image_error ("Cannot open `%s'", file
, Qnil
);
6818 if (!STRINGP (specified_data
))
6820 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6824 /* Memory source! */
6825 memsrc
.bytes
= SDATA (specified_data
);
6826 memsrc
.len
= SBYTES (specified_data
);
6829 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6830 tiff_read_from_memory
,
6831 tiff_write_from_memory
,
6832 tiff_seek_in_memory
,
6834 tiff_size_of_memory
,
6840 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6845 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6846 if (INTEGERP (image
))
6848 int ino
= XFASTINT (image
);
6849 if (!fn_TIFFSetDirectory (tiff
, ino
))
6851 image_error ("Invalid image number `%s' in image `%s'",
6853 fn_TIFFClose (tiff
);
6858 /* Get width and height of the image, and allocate a raster buffer
6859 of width x height 32-bit values. */
6860 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6861 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6863 if (!check_image_size (f
, width
, height
))
6865 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6866 fn_TIFFClose (tiff
);
6870 /* Create the X image and pixmap. */
6871 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
6872 && x_create_x_image_and_pixmap (f
, width
, height
, 0,
6873 &ximg
, &img
->pixmap
)))
6875 fn_TIFFClose (tiff
);
6879 buf
= (uint32
*) xmalloc (sizeof *buf
* width
* height
);
6881 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6883 /* Count the number of images in the file. */
6884 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
6888 img
->lisp_data
= Fcons (Qcount
,
6889 Fcons (make_number (count
),
6892 fn_TIFFClose (tiff
);
6895 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6900 /* Initialize the color table. */
6901 init_color_table ();
6903 /* Process the pixel raster. Origin is in the lower-left corner. */
6904 for (y
= 0; y
< height
; ++y
)
6906 uint32
*row
= buf
+ y
* width
;
6908 for (x
= 0; x
< width
; ++x
)
6910 uint32 abgr
= row
[x
];
6911 int r
= TIFFGetR (abgr
) << 8;
6912 int g
= TIFFGetG (abgr
) << 8;
6913 int b
= TIFFGetB (abgr
) << 8;
6914 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6918 #ifdef COLOR_TABLE_SUPPORT
6919 /* Remember the colors allocated for the image. Free the color table. */
6920 img
->colors
= colors_in_color_table (&img
->ncolors
);
6921 free_color_table ();
6922 #endif /* COLOR_TABLE_SUPPORT */
6925 img
->height
= height
;
6927 /* Maybe fill in the background field while we have ximg handy. */
6928 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6929 /* Casting avoids a GCC warning on W32. */
6930 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6932 /* Put the image into the pixmap, then free the X image and its buffer. */
6933 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6934 x_destroy_x_image (ximg
);
6940 #else /* HAVE_TIFF */
6944 tiff_load (struct frame
*f
, struct image
*img
)
6946 return ns_load_image (f
, img
,
6947 image_spec_value (img
->spec
, QCfile
, NULL
),
6948 image_spec_value (img
->spec
, QCdata
, NULL
));
6950 #endif /* HAVE_NS */
6952 #endif /* !HAVE_TIFF */
6956 /***********************************************************************
6958 ***********************************************************************/
6960 #if defined (HAVE_GIF) || defined (HAVE_NS)
6962 static int gif_image_p (Lisp_Object object
);
6963 static int gif_load (struct frame
*f
, struct image
*img
);
6964 static void gif_clear_image (struct frame
*f
, struct image
*img
);
6966 /* The symbol `gif' identifying images of this type. */
6968 static Lisp_Object Qgif
;
6970 /* Indices of image specification fields in gif_format, below. */
6972 enum gif_keyword_index
6988 /* Vector of image_keyword structures describing the format
6989 of valid user-defined image specifications. */
6991 static const struct image_keyword gif_format
[GIF_LAST
] =
6993 {":type", IMAGE_SYMBOL_VALUE
, 1},
6994 {":data", IMAGE_STRING_VALUE
, 0},
6995 {":file", IMAGE_STRING_VALUE
, 0},
6996 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6997 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6998 {":relief", IMAGE_INTEGER_VALUE
, 0},
6999 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7000 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7001 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7002 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7003 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7006 /* Structure describing the image type `gif'. */
7008 static struct image_type gif_type
=
7017 /* Free X resources of GIF image IMG which is used on frame F. */
7020 gif_clear_image (struct frame
*f
, struct image
*img
)
7022 img
->lisp_data
= Qnil
;
7023 x_clear_image (f
, img
);
7026 /* Return non-zero if OBJECT is a valid GIF image specification. */
7029 gif_image_p (Lisp_Object object
)
7031 struct image_keyword fmt
[GIF_LAST
];
7032 memcpy (fmt
, gif_format
, sizeof fmt
);
7034 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7037 /* Must specify either the :data or :file keyword. */
7038 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7041 #endif /* HAVE_GIF */
7045 #if defined (HAVE_NTGUI)
7046 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7047 Undefine before redefining to avoid a preprocessor warning. */
7051 /* avoid conflict with QuickdrawText.h */
7052 #define DrawText gif_DrawText
7053 #include <gif_lib.h>
7056 #else /* HAVE_NTGUI */
7058 #include <gif_lib.h>
7060 #endif /* HAVE_NTGUI */
7065 /* GIF library details. */
7066 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7067 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7068 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7069 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7072 init_gif_functions (Lisp_Object libraries
)
7076 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7079 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7080 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7081 LOAD_IMGLIB_FN (library
, DGifOpen
);
7082 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7088 #define fn_DGifCloseFile DGifCloseFile
7089 #define fn_DGifSlurp DGifSlurp
7090 #define fn_DGifOpen DGifOpen
7091 #define fn_DGifOpenFileName DGifOpenFileName
7093 #endif /* HAVE_NTGUI */
7095 /* Reading a GIF image from memory
7096 Based on the PNG memory stuff to a certain extent. */
7100 unsigned char *bytes
;
7106 /* Make the current memory source available to gif_read_from_memory.
7107 It's done this way because not all versions of libungif support
7108 a UserData field in the GifFileType structure. */
7109 static gif_memory_source
*current_gif_memory_src
;
7112 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7114 gif_memory_source
*src
= current_gif_memory_src
;
7116 if (len
> src
->len
- src
->index
)
7119 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7125 /* Load GIF image IMG for use on frame F. Value is non-zero if
7128 static const int interlace_start
[] = {0, 4, 2, 1};
7129 static const int interlace_increment
[] = {8, 8, 4, 2};
7131 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7134 gif_load (struct frame
*f
, struct image
*img
)
7137 int rc
, width
, height
, x
, y
, i
, j
;
7139 ColorMapObject
*gif_color_map
;
7140 unsigned long pixel_colors
[256];
7142 int image_height
, image_width
;
7143 gif_memory_source memsrc
;
7144 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7145 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7146 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7147 unsigned long bgcolor
= 0;
7150 if (NILP (specified_data
))
7152 file
= x_find_image_file (specified_file
);
7153 if (!STRINGP (file
))
7155 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7159 /* Open the GIF file. */
7160 gif
= fn_DGifOpenFileName (SSDATA (file
));
7163 image_error ("Cannot open `%s'", file
, Qnil
);
7169 if (!STRINGP (specified_data
))
7171 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7175 /* Read from memory! */
7176 current_gif_memory_src
= &memsrc
;
7177 memsrc
.bytes
= SDATA (specified_data
);
7178 memsrc
.len
= SBYTES (specified_data
);
7181 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7184 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7189 /* Before reading entire contents, check the declared image size. */
7190 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7192 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7193 fn_DGifCloseFile (gif
);
7197 /* Read entire contents. */
7198 rc
= fn_DGifSlurp (gif
);
7199 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7201 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7202 fn_DGifCloseFile (gif
);
7206 /* Which sub-image are we to display? */
7208 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7209 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7210 if (idx
< 0 || idx
>= gif
->ImageCount
)
7212 image_error ("Invalid image number `%s' in image `%s'",
7213 image_number
, img
->spec
);
7214 fn_DGifCloseFile (gif
);
7219 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[idx
].ImageDesc
.Top
;
7220 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[idx
].ImageDesc
.Left
;
7221 image_height
= gif
->SavedImages
[idx
].ImageDesc
.Height
;
7222 img
->corners
[BOT_CORNER
] = img
->corners
[TOP_CORNER
] + image_height
;
7223 image_width
= gif
->SavedImages
[idx
].ImageDesc
.Width
;
7224 img
->corners
[RIGHT_CORNER
] = img
->corners
[LEFT_CORNER
] + image_width
;
7226 width
= img
->width
= max (gif
->SWidth
,
7227 max (gif
->Image
.Left
+ gif
->Image
.Width
,
7228 img
->corners
[RIGHT_CORNER
]));
7229 height
= img
->height
= max (gif
->SHeight
,
7230 max (gif
->Image
.Top
+ gif
->Image
.Height
,
7231 img
->corners
[BOT_CORNER
]));
7233 if (!check_image_size (f
, width
, height
))
7235 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7236 fn_DGifCloseFile (gif
);
7240 /* Create the X image and pixmap. */
7241 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7243 fn_DGifCloseFile (gif
);
7247 /* Clear the part of the screen image not covered by the image.
7248 Full animated GIF support requires more here (see the gif89 spec,
7249 disposal methods). Let's simply assume that the part not covered
7250 by a sub-image is in the frame's background color. */
7251 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7252 for (x
= 0; x
< width
; ++x
)
7253 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7255 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7256 for (x
= 0; x
< width
; ++x
)
7257 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7259 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7261 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7262 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7263 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7264 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7267 /* Read the GIF image into the X image. */
7269 /* FIXME: With the current implementation, loading an animated gif
7270 is quadratic in the number of animation frames, since each frame
7271 is a separate struct image. We must provide a way for a single
7272 gif_load call to construct and save all animation frames. */
7274 init_color_table ();
7275 if (STRINGP (specified_bg
))
7276 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7277 FRAME_BACKGROUND_PIXEL (f
));
7278 for (j
= 0; j
<= idx
; ++j
)
7280 /* We use a local variable `raster' here because RasterBits is a
7281 char *, which invites problems with bytes >= 0x80. */
7282 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7283 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7284 int transparency_color_index
= -1;
7287 /* Find the Graphic Control Extension block for this sub-image.
7288 Extract the disposal method and transparency color. */
7289 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7291 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7293 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7294 && extblock
->ByteCount
== 4
7295 && extblock
->Bytes
[0] & 1)
7297 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7298 to background". Treat any other value like 2. */
7299 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7300 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7305 /* We can't "keep in place" the first subimage. */
7309 /* Allocate subimage colors. */
7310 memset (pixel_colors
, 0, sizeof pixel_colors
);
7311 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7313 gif_color_map
= gif
->SColorMap
;
7316 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7318 if (transparency_color_index
== i
)
7319 pixel_colors
[i
] = STRINGP (specified_bg
)
7320 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7323 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7324 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7325 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7326 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7330 /* Apply the pixel values. */
7331 if (gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7335 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7337 y
++, row
+= interlace_increment
[pass
])
7339 if (row
>= image_height
)
7341 row
= interlace_start
[++pass
];
7342 while (row
>= image_height
)
7343 row
= interlace_start
[++pass
];
7346 for (x
= 0; x
< image_width
; x
++)
7348 int c
= raster
[y
* image_width
+ x
];
7349 if (transparency_color_index
!= c
|| disposal
!= 1)
7350 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7351 row
+ img
->corners
[TOP_CORNER
], pixel_colors
[c
]);
7357 for (y
= 0; y
< image_height
; ++y
)
7358 for (x
= 0; x
< image_width
; ++x
)
7360 int c
= raster
[y
* image_width
+ x
];
7361 if (transparency_color_index
!= c
|| disposal
!= 1)
7362 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7363 y
+ img
->corners
[TOP_CORNER
], pixel_colors
[c
]);
7368 #ifdef COLOR_TABLE_SUPPORT
7369 img
->colors
= colors_in_color_table (&img
->ncolors
);
7370 free_color_table ();
7371 #endif /* COLOR_TABLE_SUPPORT */
7373 /* Save GIF image extension data for `image-metadata'.
7374 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7375 img
->lisp_data
= Qnil
;
7376 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7378 unsigned int delay
= 0;
7379 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7380 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7381 /* Append (... FUNCTION "BYTES") */
7384 = Fcons (make_number (ext
->Function
),
7385 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7387 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7388 && ext
->ByteCount
== 4)
7390 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7391 delay
|= ext
->Bytes
[1];
7394 img
->lisp_data
= Fcons (Qextension_data
,
7395 Fcons (img
->lisp_data
, Qnil
));
7399 Fcons (make_float (((double) delay
) * 0.01),
7403 if (gif
->ImageCount
> 1)
7404 img
->lisp_data
= Fcons (Qcount
,
7405 Fcons (make_number (gif
->ImageCount
),
7408 fn_DGifCloseFile (gif
);
7410 /* Maybe fill in the background field while we have ximg handy. */
7411 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7412 /* Casting avoids a GCC warning. */
7413 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7415 /* Put the image into the pixmap, then free the X image and its buffer. */
7416 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7417 x_destroy_x_image (ximg
);
7422 #else /* !HAVE_GIF */
7426 gif_load (struct frame
*f
, struct image
*img
)
7428 return ns_load_image (f
, img
,
7429 image_spec_value (img
->spec
, QCfile
, NULL
),
7430 image_spec_value (img
->spec
, QCdata
, NULL
));
7432 #endif /* HAVE_NS */
7434 #endif /* HAVE_GIF */
7437 /***********************************************************************
7439 ***********************************************************************/
7440 #if defined (HAVE_IMAGEMAGICK)
7442 static Lisp_Object Qimagemagick
;
7444 static int imagemagick_image_p (Lisp_Object
);
7445 static int imagemagick_load (struct frame
*, struct image
*);
7446 static void imagemagick_clear_image (struct frame
*, struct image
*);
7448 /* Indices of image specification fields in imagemagick_format. */
7450 enum imagemagick_keyword_index
7458 IMAGEMAGICK_ALGORITHM
,
7459 IMAGEMAGICK_HEURISTIC_MASK
,
7461 IMAGEMAGICK_BACKGROUND
,
7464 IMAGEMAGICK_ROTATION
,
7469 /* Vector of image_keyword structures describing the format
7470 of valid user-defined image specifications. */
7472 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7474 {":type", IMAGE_SYMBOL_VALUE
, 1},
7475 {":data", IMAGE_STRING_VALUE
, 0},
7476 {":file", IMAGE_STRING_VALUE
, 0},
7477 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7478 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7479 {":relief", IMAGE_INTEGER_VALUE
, 0},
7480 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7481 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7482 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7483 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7484 {":height", IMAGE_INTEGER_VALUE
, 0},
7485 {":width", IMAGE_INTEGER_VALUE
, 0},
7486 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7487 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7490 /* Structure describing the image type for any image handled via
7493 static struct image_type imagemagick_type
=
7496 imagemagick_image_p
,
7498 imagemagick_clear_image
,
7502 /* Free X resources of imagemagick image IMG which is used on frame F. */
7505 imagemagick_clear_image (struct frame
*f
,
7508 x_clear_image (f
, img
);
7511 /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do
7512 this by calling parse_image_spec and supplying the keywords that
7513 identify the IMAGEMAGICK format. */
7516 imagemagick_image_p (Lisp_Object object
)
7518 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7519 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7521 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7524 /* Must specify either the :data or :file keyword. */
7525 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7528 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7529 Therefore rename the function so it doesnt collide with ImageMagick. */
7530 #define DrawRectangle DrawRectangleGif
7531 #include <wand/MagickWand.h>
7533 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7534 Emacs seems to work fine with the hidden version, so unhide it. */
7535 #include <magick/version.h>
7536 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7537 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7538 MagickPixelPacket
*);
7541 /* Helper function for imagemagick_load, which does the actual loading
7542 given contents and size, apart from frame and image structures,
7543 passed from imagemagick_load. Uses librimagemagick to do most of
7544 the image processing.
7546 F is a pointer to the Emacs frame; IMG to the image structure to
7547 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
7548 be parsed; SIZE is the number of bytes of data; and FILENAME is
7549 either the file name or the image data.
7551 Return non-zero if successful. */
7554 imagemagick_load_image (struct frame
*f
, struct image
*img
,
7555 unsigned char *contents
, unsigned int size
,
7561 MagickBooleanType status
;
7567 MagickWand
*image_wand
;
7568 MagickWand
*ping_wand
;
7569 PixelIterator
*iterator
;
7571 MagickPixelPacket pixel
;
7576 int desired_width
, desired_height
;
7578 int imagemagick_rendermethod
;
7580 ImageInfo
*image_info
;
7581 ExceptionInfo
*exception
;
7585 /* Handle image index for image types who can contain more than one image.
7586 Interface :index is same as for GIF. First we "ping" the image to see how
7587 many sub-images it contains. Pinging is faster than loading the image to
7588 find out things about it. */
7590 /* Initialize the imagemagick environment. */
7591 MagickWandGenesis ();
7592 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7593 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7594 ping_wand
= NewMagickWand ();
7595 MagickSetResolution (ping_wand
, 2, 2);
7596 if (filename
!= NULL
)
7598 status
= MagickPingImage (ping_wand
, filename
);
7602 status
= MagickPingImageBlob (ping_wand
, contents
, size
);
7605 if (ino
>= MagickGetNumberImages (ping_wand
))
7607 image_error ("Invalid image number `%s' in image `%s'",
7609 DestroyMagickWand (ping_wand
);
7613 if (MagickGetNumberImages(ping_wand
) > 1)
7616 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7619 DestroyMagickWand (ping_wand
);
7621 /* Now we know how many images are inside the file. If it's not a
7622 bundle, the number is one. */
7624 if (filename
!= NULL
)
7626 image_info
= CloneImageInfo ((ImageInfo
*) NULL
);
7627 (void) strcpy (image_info
->filename
, filename
);
7628 image_info
->number_scenes
= 1;
7629 image_info
->scene
= ino
;
7630 exception
= AcquireExceptionInfo ();
7632 im_image
= ReadImage (image_info
, exception
);
7633 DestroyExceptionInfo (exception
);
7635 if (im_image
== NULL
)
7636 goto imagemagick_no_wand
;
7637 image_wand
= NewMagickWandFromImage (im_image
);
7638 DestroyImage(im_image
);
7642 image_wand
= NewMagickWand ();
7643 if (MagickReadImageBlob (image_wand
, contents
, size
) == MagickFalse
)
7644 goto imagemagick_error
;
7647 /* If width and/or height is set in the display spec assume we want
7648 to scale to those values. If either h or w is unspecified, the
7649 unspecified should be calculated from the specified to preserve
7652 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7653 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7654 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7655 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7657 height
= MagickGetImageHeight (image_wand
);
7658 width
= MagickGetImageWidth (image_wand
);
7660 if (desired_width
!= -1 && desired_height
== -1)
7661 /* w known, calculate h. */
7662 desired_height
= (double) desired_width
/ width
* height
;
7663 if (desired_width
== -1 && desired_height
!= -1)
7664 /* h known, calculate w. */
7665 desired_width
= (double) desired_height
/ height
* width
;
7666 if (desired_width
!= -1 && desired_height
!= -1)
7668 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7669 if (status
== MagickFalse
)
7671 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7672 goto imagemagick_error
;
7676 /* crop behaves similar to image slicing in Emacs but is more memory
7678 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7680 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7682 /* After some testing, it seems MagickCropImage is the fastest crop
7683 function in ImageMagick. This crop function seems to do less copying
7684 than the alternatives, but it still reads the entire image into memory
7685 before croping, which is aparently difficult to avoid when using
7688 w
= XFASTINT (XCAR (crop
));
7690 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7692 h
= XFASTINT (XCAR (crop
));
7694 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7696 x
= XFASTINT (XCAR (crop
));
7698 if (CONSP (crop
) && INTEGERP (XCAR (crop
)))
7700 y
= XFASTINT (XCAR (crop
));
7701 MagickCropImage (image_wand
, w
, h
, x
, y
);
7707 /* Furthermore :rotation. we need background color and angle for
7710 TODO background handling for rotation specified_bg =
7711 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7713 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7716 PixelWand
* background
= NewPixelWand ();
7717 PixelSetColor (background
, "#ffffff");/*TODO remove hardcode*/
7719 rotation
= extract_float (value
);
7721 status
= MagickRotateImage (image_wand
, background
, rotation
);
7722 DestroyPixelWand (background
);
7723 if (status
== MagickFalse
)
7725 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7726 goto imagemagick_error
;
7730 /* Finally we are done manipulating the image. Figure out the
7731 resulting width/height and transfer ownerwship to Emacs. */
7732 height
= MagickGetImageHeight (image_wand
);
7733 width
= MagickGetImageWidth (image_wand
);
7735 if (! (width
<= INT_MAX
&& height
<= INT_MAX
7736 && check_image_size (f
, width
, height
)))
7738 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7739 goto imagemagick_error
;
7742 /* We can now get a valid pixel buffer from the imagemagick file, if all
7745 init_color_table ();
7746 imagemagick_rendermethod
= (INTEGERP (Vimagemagick_render_type
)
7747 ? XFASTINT (Vimagemagick_render_type
) : 0);
7748 if (imagemagick_rendermethod
== 0)
7750 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7751 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7752 &ximg
, &img
->pixmap
))
7754 #ifdef COLOR_TABLE_SUPPORT
7755 free_color_table ();
7757 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7758 goto imagemagick_error
;
7761 /* Copy imagegmagick image to x with primitive yet robust pixel
7762 pusher loop. This has been tested a lot with many different
7765 /* Copy pixels from the imagemagick image structure to the x image map. */
7766 iterator
= NewPixelIterator (image_wand
);
7767 if (iterator
== (PixelIterator
*) NULL
)
7769 #ifdef COLOR_TABLE_SUPPORT
7770 free_color_table ();
7772 x_destroy_x_image (ximg
);
7773 image_error ("Imagemagick pixel iterator creation failed",
7775 goto imagemagick_error
;
7778 for (y
= 0; y
< (long) MagickGetImageHeight (image_wand
); y
++)
7780 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7781 if (pixels
== (PixelWand
**) NULL
)
7783 for (x
= 0; x
< (long) width
; x
++)
7785 PixelGetMagickColor (pixels
[x
], &pixel
);
7786 XPutPixel (ximg
, x
, y
,
7787 lookup_rgb_color (f
,
7793 DestroyPixelIterator (iterator
);
7796 if (imagemagick_rendermethod
== 1)
7798 /* Magicexportimage is normaly faster than pixelpushing. This
7799 method is also well tested. Some aspects of this method are
7800 ad-hoc and needs to be more researched. */
7801 int imagedepth
= 24;/*MagickGetImageDepth(image_wand);*/
7802 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP";/*"RGBP";*/
7803 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7804 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7805 &ximg
, &img
->pixmap
))
7807 #ifdef COLOR_TABLE_SUPPORT
7808 free_color_table ();
7810 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7811 goto imagemagick_error
;
7815 /* Oddly, the below code doesnt seem to work:*/
7816 /* switch(ximg->bitmap_unit){ */
7818 /* pixelwidth=CharPixel; */
7821 /* pixelwidth=ShortPixel; */
7824 /* pixelwidth=LongPixel; */
7828 Here im just guessing the format of the bitmap.
7829 happens to work fine for:
7832 seems about 3 times as fast as pixel pushing(not carefully measured)
7834 pixelwidth
= CharPixel
;/*??? TODO figure out*/
7835 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7836 MagickExportImagePixels (image_wand
,
7841 /*&(img->pixmap));*/
7844 image_error ("You dont have MagickExportImagePixels, upgrade ImageMagick!",
7850 #ifdef COLOR_TABLE_SUPPORT
7851 /* Remember colors allocated for this image. */
7852 img
->colors
= colors_in_color_table (&img
->ncolors
);
7853 free_color_table ();
7854 #endif /* COLOR_TABLE_SUPPORT */
7858 img
->height
= height
;
7860 /* Put the image into the pixmap, then free the X image and its
7862 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7863 x_destroy_x_image (ximg
);
7866 /* Final cleanup. image_wand should be the only resource left. */
7867 DestroyMagickWand (image_wand
);
7868 /* `MagickWandTerminus' terminates the imagemagick environment. */
7869 MagickWandTerminus ();
7874 DestroyMagickWand (image_wand
);
7875 imagemagick_no_wand
:
7876 MagickWandTerminus ();
7877 /* TODO more cleanup. */
7878 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7883 /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if
7884 successful. this function will go into the imagemagick_type structure, and
7885 the prototype thus needs to be compatible with that structure. */
7888 imagemagick_load (struct frame
*f
, struct image
*img
)
7891 Lisp_Object file_name
;
7893 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7894 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7895 if (STRINGP (file_name
))
7899 file
= x_find_image_file (file_name
);
7900 if (!STRINGP (file
))
7902 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7905 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
7907 /* Else its not a file, its a lisp object. Load the image from a
7908 lisp object rather than a file. */
7913 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7914 if (!STRINGP (data
))
7916 image_error ("Invalid image data `%s'", data
, Qnil
);
7919 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
7920 SBYTES (data
), NULL
);
7926 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
7927 doc
: /* Return a list of image types supported by ImageMagick.
7928 Each entry in this list is a symbol named after an ImageMagick format
7929 tag. See the ImageMagick manual for a list of ImageMagick formats and
7930 their descriptions (http://www.imagemagick.org/script/formats.php).
7932 Note that ImageMagick recognizes many file-types that Emacs does not
7933 recognize as images, such as C. See `imagemagick-types-inhibit'. */)
7936 Lisp_Object typelist
= Qnil
;
7939 char **imtypes
= GetMagickList ("*", &numf
, &ex
);
7941 Lisp_Object Qimagemagicktype
;
7942 for (i
= 0; i
< numf
; i
++)
7944 Qimagemagicktype
= intern (imtypes
[i
]);
7945 typelist
= Fcons (Qimagemagicktype
, typelist
);
7950 #endif /* defined (HAVE_IMAGEMAGICK) */
7954 /***********************************************************************
7956 ***********************************************************************/
7958 #if defined (HAVE_RSVG)
7960 /* Function prototypes. */
7962 static int svg_image_p (Lisp_Object object
);
7963 static int svg_load (struct frame
*f
, struct image
*img
);
7965 static int svg_load_image (struct frame
*, struct image
*,
7966 unsigned char *, ptrdiff_t);
7968 /* The symbol `svg' identifying images of this type. */
7970 static Lisp_Object Qsvg
;
7972 /* Indices of image specification fields in svg_format, below. */
7974 enum svg_keyword_index
7989 /* Vector of image_keyword structures describing the format
7990 of valid user-defined image specifications. */
7992 static const struct image_keyword svg_format
[SVG_LAST
] =
7994 {":type", IMAGE_SYMBOL_VALUE
, 1},
7995 {":data", IMAGE_STRING_VALUE
, 0},
7996 {":file", IMAGE_STRING_VALUE
, 0},
7997 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7998 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7999 {":relief", IMAGE_INTEGER_VALUE
, 0},
8000 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8001 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8002 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8003 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8006 /* Structure describing the image type `svg'. Its the same type of
8007 structure defined for all image formats, handled by emacs image
8008 functions. See struct image_type in dispextern.h. */
8010 static struct image_type svg_type
=
8012 /* An identifier showing that this is an image structure for the SVG format. */
8014 /* Handle to a function that can be used to identify a SVG file. */
8016 /* Handle to function used to load a SVG file. */
8018 /* Handle to function to free sresources for SVG. */
8020 /* An internal field to link to the next image type in a list of
8021 image types, will be filled in when registering the format. */
8026 /* Return non-zero if OBJECT is a valid SVG image specification. Do
8027 this by calling parse_image_spec and supplying the keywords that
8028 identify the SVG format. */
8031 svg_image_p (Lisp_Object object
)
8033 struct image_keyword fmt
[SVG_LAST
];
8034 memcpy (fmt
, svg_format
, sizeof fmt
);
8036 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8039 /* Must specify either the :data or :file keyword. */
8040 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8043 #include <librsvg/rsvg.h>
8047 /* SVG library functions. */
8048 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
);
8049 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
);
8050 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
);
8051 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
);
8052 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
);
8054 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
);
8055 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
);
8056 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
);
8057 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
);
8058 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
);
8059 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
);
8060 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
);
8061 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
);
8063 DEF_IMGLIB_FN (void, g_type_init
);
8064 DEF_IMGLIB_FN (void, g_object_unref
);
8065 DEF_IMGLIB_FN (void, g_error_free
);
8067 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8070 init_svg_functions (Lisp_Object libraries
)
8072 HMODULE library
, gdklib
, glib
, gobject
;
8074 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
8075 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
8076 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
8077 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
8080 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8081 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8082 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8083 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8084 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8086 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8087 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8088 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8089 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8090 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8091 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8092 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8093 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8095 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8096 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8097 LOAD_IMGLIB_FN (glib
, g_error_free
);
8103 /* The following aliases for library functions allow dynamic loading
8104 to be used on some platforms. */
8105 #define fn_rsvg_handle_new rsvg_handle_new
8106 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8107 #define fn_rsvg_handle_write rsvg_handle_write
8108 #define fn_rsvg_handle_close rsvg_handle_close
8109 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8111 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8112 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8113 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8114 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8115 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8116 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8117 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8118 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8120 #define fn_g_type_init g_type_init
8121 #define fn_g_object_unref g_object_unref
8122 #define fn_g_error_free g_error_free
8123 #endif /* !HAVE_NTGUI */
8125 /* Load SVG image IMG for use on frame F. Value is non-zero if
8126 successful. this function will go into the svg_type structure, and
8127 the prototype thus needs to be compatible with that structure. */
8130 svg_load (struct frame
*f
, struct image
*img
)
8133 Lisp_Object file_name
;
8135 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8136 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8137 if (STRINGP (file_name
))
8140 unsigned char *contents
;
8143 file
= x_find_image_file (file_name
);
8144 if (!STRINGP (file
))
8146 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8150 /* Read the entire file into memory. */
8151 contents
= slurp_file (SSDATA (file
), &size
);
8152 if (contents
== NULL
)
8154 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8157 /* If the file was slurped into memory properly, parse it. */
8158 success_p
= svg_load_image (f
, img
, contents
, size
);
8161 /* Else its not a file, its a lisp object. Load the image from a
8162 lisp object rather than a file. */
8167 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8168 if (!STRINGP (data
))
8170 image_error ("Invalid image data `%s'", data
, Qnil
);
8173 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8179 /* svg_load_image is a helper function for svg_load, which does the
8180 actual loading given contents and size, apart from frame and image
8181 structures, passed from svg_load.
8183 Uses librsvg to do most of the image processing.
8185 Returns non-zero when successful. */
8187 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8188 struct image
*img
, /* Pointer to emacs image structure. */
8189 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8190 ptrdiff_t size
) /* Size of data in bytes. */
8192 RsvgHandle
*rsvg_handle
;
8193 RsvgDimensionData dimension_data
;
8198 const guint8
*pixels
;
8201 Lisp_Object specified_bg
;
8206 /* g_type_init is a glib function that must be called prior to using
8207 gnome type library functions. */
8209 /* Make a handle to a new rsvg object. */
8210 rsvg_handle
= fn_rsvg_handle_new ();
8212 /* Parse the contents argument and fill in the rsvg_handle. */
8213 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8214 if (err
) goto rsvg_error
;
8216 /* The parsing is complete, rsvg_handle is ready to used, close it
8217 for further writes. */
8218 fn_rsvg_handle_close (rsvg_handle
, &err
);
8219 if (err
) goto rsvg_error
;
8221 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8222 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8224 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8228 /* We can now get a valid pixel buffer from the svg file, if all
8230 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8231 if (!pixbuf
) goto rsvg_error
;
8232 fn_g_object_unref (rsvg_handle
);
8234 /* Extract some meta data from the svg handle. */
8235 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8236 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8237 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8238 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8240 /* Validate the svg meta data. */
8241 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8242 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8243 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8244 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8246 /* Try to create a x pixmap to hold the svg pixmap. */
8247 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8249 fn_g_object_unref (pixbuf
);
8253 init_color_table ();
8255 /* Handle alpha channel by combining the image with a background
8257 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8258 if (!STRINGP (specified_bg
)
8259 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8262 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8263 x_query_color (f
, &background
);
8265 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8269 /* SVG pixmaps specify transparency in the last byte, so right
8270 shift 8 bits to get rid of it, since emacs doesn't support
8272 background
.red
>>= 8;
8273 background
.green
>>= 8;
8274 background
.blue
>>= 8;
8276 /* This loop handles opacity values, since Emacs assumes
8277 non-transparent images. Each pixel must be "flattened" by
8278 calculating the resulting color, given the transparency of the
8279 pixel, and the image background color. */
8280 for (y
= 0; y
< height
; ++y
)
8282 for (x
= 0; x
< width
; ++x
)
8292 opacity
= *pixels
++;
8294 red
= ((red
* opacity
)
8295 + (background
.red
* ((1 << 8) - opacity
)));
8296 green
= ((green
* opacity
)
8297 + (background
.green
* ((1 << 8) - opacity
)));
8298 blue
= ((blue
* opacity
)
8299 + (background
.blue
* ((1 << 8) - opacity
)));
8301 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8304 pixels
+= rowstride
- 4 * width
;
8307 #ifdef COLOR_TABLE_SUPPORT
8308 /* Remember colors allocated for this image. */
8309 img
->colors
= colors_in_color_table (&img
->ncolors
);
8310 free_color_table ();
8311 #endif /* COLOR_TABLE_SUPPORT */
8313 fn_g_object_unref (pixbuf
);
8316 img
->height
= height
;
8318 /* Maybe fill in the background field while we have ximg handy.
8319 Casting avoids a GCC warning. */
8320 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8322 /* Put the image into the pixmap, then free the X image and its
8324 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8325 x_destroy_x_image (ximg
);
8330 fn_g_object_unref (rsvg_handle
);
8331 /* FIXME: Use error->message so the user knows what is the actual
8332 problem with the image. */
8333 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8334 fn_g_error_free (err
);
8338 #endif /* defined (HAVE_RSVG) */
8343 /***********************************************************************
8345 ***********************************************************************/
8347 #ifdef HAVE_X_WINDOWS
8348 #define HAVE_GHOSTSCRIPT 1
8349 #endif /* HAVE_X_WINDOWS */
8351 #ifdef HAVE_GHOSTSCRIPT
8353 static int gs_image_p (Lisp_Object object
);
8354 static int gs_load (struct frame
*f
, struct image
*img
);
8355 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8357 /* Keyword symbols. */
8359 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8361 /* Indices of image specification fields in gs_format, below. */
8363 enum gs_keyword_index
8381 /* Vector of image_keyword structures describing the format
8382 of valid user-defined image specifications. */
8384 static const struct image_keyword gs_format
[GS_LAST
] =
8386 {":type", IMAGE_SYMBOL_VALUE
, 1},
8387 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8388 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8389 {":file", IMAGE_STRING_VALUE
, 1},
8390 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8391 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8392 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8393 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
8394 {":relief", IMAGE_INTEGER_VALUE
, 0},
8395 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8396 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8397 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8398 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8401 /* Structure describing the image type `ghostscript'. */
8403 static struct image_type gs_type
=
8413 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8416 gs_clear_image (struct frame
*f
, struct image
*img
)
8418 x_clear_image (f
, img
);
8422 /* Return non-zero if OBJECT is a valid Ghostscript image
8426 gs_image_p (Lisp_Object object
)
8428 struct image_keyword fmt
[GS_LAST
];
8432 memcpy (fmt
, gs_format
, sizeof fmt
);
8434 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8437 /* Bounding box must be a list or vector containing 4 integers. */
8438 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8441 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8442 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8447 else if (VECTORP (tem
))
8449 if (ASIZE (tem
) != 4)
8451 for (i
= 0; i
< 4; ++i
)
8452 if (!INTEGERP (XVECTOR (tem
)->contents
[i
]))
8462 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8466 gs_load (struct frame
*f
, struct image
*img
)
8469 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8471 double in_width
, in_height
;
8472 Lisp_Object pixel_colors
= Qnil
;
8474 /* Compute pixel size of pixmap needed from the given size in the
8475 image specification. Sizes in the specification are in pt. 1 pt
8476 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8478 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8479 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8480 img
->width
= in_width
* FRAME_X_DISPLAY_INFO (f
)->resx
;
8481 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8482 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8483 img
->height
= in_height
* FRAME_X_DISPLAY_INFO (f
)->resy
;
8485 if (!check_image_size (f
, img
->width
, img
->height
))
8487 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8491 /* Create the pixmap. */
8492 xassert (img
->pixmap
== NO_PIXMAP
);
8494 if (x_check_image_size (0, img
->width
, img
->height
))
8496 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8498 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8499 img
->width
, img
->height
,
8500 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8506 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8510 /* Call the loader to fill the pixmap. It returns a process object
8511 if successful. We do not record_unwind_protect here because
8512 other places in redisplay like calling window scroll functions
8513 don't either. Let the Lisp loader use `unwind-protect' instead. */
8514 sprintf (buffer
, "%lu %lu",
8515 (unsigned long) FRAME_X_WINDOW (f
),
8516 (unsigned long) img
->pixmap
);
8517 window_and_pixmap_id
= build_string (buffer
);
8519 sprintf (buffer
, "%lu %lu",
8520 FRAME_FOREGROUND_PIXEL (f
),
8521 FRAME_BACKGROUND_PIXEL (f
));
8522 pixel_colors
= build_string (buffer
);
8524 XSETFRAME (frame
, f
);
8525 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8527 loader
= intern ("gs-load-image");
8529 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
8530 make_number (img
->width
),
8531 make_number (img
->height
),
8532 window_and_pixmap_id
,
8534 return PROCESSP (img
->lisp_data
);
8538 /* Kill the Ghostscript process that was started to fill PIXMAP on
8539 frame F. Called from XTread_socket when receiving an event
8540 telling Emacs that Ghostscript has finished drawing. */
8543 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8545 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8549 /* Find the image containing PIXMAP. */
8550 for (i
= 0; i
< c
->used
; ++i
)
8551 if (c
->images
[i
]->pixmap
== pixmap
)
8554 /* Should someone in between have cleared the image cache, for
8555 instance, give up. */
8559 /* Kill the GS process. We should have found PIXMAP in the image
8560 cache and its image should contain a process object. */
8562 xassert (PROCESSP (img
->lisp_data
));
8563 Fkill_process (img
->lisp_data
, Qnil
);
8564 img
->lisp_data
= Qnil
;
8566 #if defined (HAVE_X_WINDOWS)
8568 /* On displays with a mutable colormap, figure out the colors
8569 allocated for the image by looking at the pixels of an XImage for
8571 class = FRAME_X_VISUAL (f
)->class;
8572 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8578 /* Try to get an XImage for img->pixmep. */
8579 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8580 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8585 /* Initialize the color table. */
8586 init_color_table ();
8588 /* For each pixel of the image, look its color up in the
8589 color table. After having done so, the color table will
8590 contain an entry for each color used by the image. */
8591 for (y
= 0; y
< img
->height
; ++y
)
8592 for (x
= 0; x
< img
->width
; ++x
)
8594 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8595 lookup_pixel_color (f
, pixel
);
8598 /* Record colors in the image. Free color table and XImage. */
8599 #ifdef COLOR_TABLE_SUPPORT
8600 img
->colors
= colors_in_color_table (&img
->ncolors
);
8601 free_color_table ();
8603 XDestroyImage (ximg
);
8605 #if 0 /* This doesn't seem to be the case. If we free the colors
8606 here, we get a BadAccess later in x_clear_image when
8607 freeing the colors. */
8608 /* We have allocated colors once, but Ghostscript has also
8609 allocated colors on behalf of us. So, to get the
8610 reference counts right, free them once. */
8612 x_free_colors (f
, img
->colors
, img
->ncolors
);
8616 image_error ("Cannot get X image of `%s'; colors will not be freed",
8621 #endif /* HAVE_X_WINDOWS */
8623 /* Now that we have the pixmap, compute mask and transform the
8624 image if requested. */
8626 postprocess_image (f
, img
);
8630 #endif /* HAVE_GHOSTSCRIPT */
8633 /***********************************************************************
8635 ***********************************************************************/
8639 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8640 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8643 return valid_image_p (spec
) ? Qt
: Qnil
;
8647 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8652 if (valid_image_p (spec
))
8653 id
= lookup_image (SELECTED_FRAME (), spec
);
8656 return make_number (id
);
8659 #endif /* GLYPH_DEBUG != 0 */
8662 /***********************************************************************
8664 ***********************************************************************/
8667 /* Image types that rely on external libraries are loaded dynamically
8668 if the library is available. */
8669 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8670 define_image_type (image_type, init_lib_fn (libraries))
8672 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8673 define_image_type (image_type, 1)
8674 #endif /* HAVE_NTGUI */
8676 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8677 doc
: /* Initialize image library implementing image type TYPE.
8678 Return non-nil if TYPE is a supported image type.
8680 Image types pbm and xbm are prebuilt; other types are loaded here.
8681 Libraries to load are specified in alist LIBRARIES (usually, the value
8682 of `dynamic-library-alist', which see). */)
8683 (Lisp_Object type
, Lisp_Object libraries
)
8686 /* Don't try to reload the library. */
8687 Lisp_Object tested
= Fassq (type
, Vlibrary_cache
);
8689 return XCDR (tested
);
8692 /* Types pbm and xbm are built-in and always available. */
8693 if (EQ (type
, Qpbm
) || EQ (type
, Qxbm
))
8696 #if defined (HAVE_XPM) || defined (HAVE_NS)
8697 if (EQ (type
, Qxpm
))
8698 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8701 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8702 if (EQ (type
, Qjpeg
))
8703 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8706 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8707 if (EQ (type
, Qtiff
))
8708 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8711 #if defined (HAVE_GIF) || defined (HAVE_NS)
8712 if (EQ (type
, Qgif
))
8713 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8716 #if defined (HAVE_PNG) || defined (HAVE_NS)
8717 if (EQ (type
, Qpng
))
8718 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8721 #if defined (HAVE_RSVG)
8722 if (EQ (type
, Qsvg
))
8723 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8726 #if defined (HAVE_IMAGEMAGICK)
8727 if (EQ (type
, Qimagemagick
))
8728 return CHECK_LIB_AVAILABLE (&imagemagick_type
, init_imagemagick_functions
,
8732 #ifdef HAVE_GHOSTSCRIPT
8733 if (EQ (type
, Qpostscript
))
8734 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8737 /* If the type is not recognized, avoid testing it ever again. */
8738 CACHE_IMAGE_TYPE (type
, Qnil
);
8743 syms_of_image (void)
8745 /* Initialize this only once, since that's what we do with Vimage_types
8746 and they are supposed to be in sync. Initializing here gives correct
8747 operation on GNU/Linux of calling dump-emacs after loading some images. */
8750 /* Must be defined now becase we're going to update it below, while
8751 defining the supported image types. */
8752 DEFVAR_LISP ("image-types", Vimage_types
,
8753 doc
: /* List of potentially supported image types.
8754 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8755 To check whether it is really supported, use `image-type-available-p'. */);
8756 Vimage_types
= Qnil
;
8758 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8759 doc
: /* Maximum size of images.
8760 Emacs will not load an image into memory if its pixel width or
8761 pixel height exceeds this limit.
8763 If the value is an integer, it directly specifies the maximum
8764 image height and width, measured in pixels. If it is a floating
8765 point number, it specifies the maximum image height and width
8766 as a ratio to the frame height and width. If the value is
8767 non-numeric, there is no explicit limit on the size of images. */);
8768 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8770 DEFSYM (Qpbm
, "pbm");
8771 ADD_IMAGE_TYPE (Qpbm
);
8773 DEFSYM (Qxbm
, "xbm");
8774 ADD_IMAGE_TYPE (Qxbm
);
8776 define_image_type (&xbm_type
, 1);
8777 define_image_type (&pbm_type
, 1);
8779 DEFSYM (Qcount
, "count");
8780 DEFSYM (Qextension_data
, "extension-data");
8781 DEFSYM (Qdelay
, "delay");
8783 DEFSYM (QCascent
, ":ascent");
8784 DEFSYM (QCmargin
, ":margin");
8785 DEFSYM (QCrelief
, ":relief");
8786 DEFSYM (QCconversion
, ":conversion");
8787 DEFSYM (QCcolor_symbols
, ":color-symbols");
8788 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
8789 DEFSYM (QCindex
, ":index");
8790 DEFSYM (QCgeometry
, ":geometry");
8791 DEFSYM (QCcrop
, ":crop");
8792 DEFSYM (QCrotation
, ":rotation");
8793 DEFSYM (QCmatrix
, ":matrix");
8794 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
8795 DEFSYM (QCmask
, ":mask");
8797 DEFSYM (Qlaplace
, "laplace");
8798 DEFSYM (Qemboss
, "emboss");
8799 DEFSYM (Qedge_detection
, "edge-detection");
8800 DEFSYM (Qheuristic
, "heuristic");
8802 DEFSYM (Qpostscript
, "postscript");
8803 #ifdef HAVE_GHOSTSCRIPT
8804 ADD_IMAGE_TYPE (Qpostscript
);
8805 DEFSYM (QCloader
, ":loader");
8806 DEFSYM (QCbounding_box
, ":bounding-box");
8807 DEFSYM (QCpt_width
, ":pt-width");
8808 DEFSYM (QCpt_height
, ":pt-height");
8809 #endif /* HAVE_GHOSTSCRIPT */
8812 DEFSYM (Qlibpng_version
, "libpng-version");
8813 Fset (Qlibpng_version
,
8815 make_number (PNG_LIBPNG_VER
)
8822 #if defined (HAVE_XPM) || defined (HAVE_NS)
8823 DEFSYM (Qxpm
, "xpm");
8824 ADD_IMAGE_TYPE (Qxpm
);
8827 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8828 DEFSYM (Qjpeg
, "jpeg");
8829 ADD_IMAGE_TYPE (Qjpeg
);
8832 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8833 DEFSYM (Qtiff
, "tiff");
8834 ADD_IMAGE_TYPE (Qtiff
);
8837 #if defined (HAVE_GIF) || defined (HAVE_NS)
8838 DEFSYM (Qgif
, "gif");
8839 ADD_IMAGE_TYPE (Qgif
);
8842 #if defined (HAVE_PNG) || defined (HAVE_NS)
8843 DEFSYM (Qpng
, "png");
8844 ADD_IMAGE_TYPE (Qpng
);
8847 #if defined (HAVE_IMAGEMAGICK)
8848 DEFSYM (Qimagemagick
, "imagemagick");
8849 ADD_IMAGE_TYPE (Qimagemagick
);
8852 #if defined (HAVE_RSVG)
8853 DEFSYM (Qsvg
, "svg");
8854 ADD_IMAGE_TYPE (Qsvg
);
8856 /* Other libraries used directly by svg code. */
8857 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
8858 DEFSYM (Qglib
, "glib");
8859 DEFSYM (Qgobject
, "gobject");
8860 #endif /* HAVE_NTGUI */
8861 #endif /* HAVE_RSVG */
8863 defsubr (&Sinit_image_library
);
8864 #ifdef HAVE_IMAGEMAGICK
8865 defsubr (&Simagemagick_types
);
8867 defsubr (&Sclear_image_cache
);
8868 defsubr (&Simage_flush
);
8869 defsubr (&Simage_size
);
8870 defsubr (&Simage_mask_p
);
8871 defsubr (&Simage_metadata
);
8875 defsubr (&Slookup_image
);
8878 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8879 doc
: /* Non-nil means always draw a cross over disabled images.
8880 Disabled images are those having a `:conversion disabled' property.
8881 A cross is always drawn on black & white displays. */);
8882 cross_disabled_images
= 0;
8884 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8885 doc
: /* List of directories to search for window system bitmap files. */);
8886 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8888 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
8889 doc
: /* Maximum time after which images are removed from the cache.
8890 When an image has not been displayed this many seconds, Emacs
8891 automatically removes it from the image cache. If the cache contains
8892 a large number of images, the actual eviction time may be shorter.
8893 The value can also be nil, meaning the cache is never cleared.
8895 The function `clear-image-cache' disregards this variable. */);
8896 Vimage_cache_eviction_delay
= make_number (300);
8897 #ifdef HAVE_IMAGEMAGICK
8898 DEFVAR_LISP ("imagemagick-render-type", Vimagemagick_render_type
,
8899 doc
: /* Choose between ImageMagick render methods. */);