1 /* Functions for image support on window system.
3 Copyright (C) 1989, 1992-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #if defined HAVE_LIBPNG_PNG_H
26 # include <libpng/png.h>
36 /* This makes the fields of a Display accessible, in Xlib header files. */
38 #define XLIB_ILLEGAL_ACCESS
43 #include "dispextern.h"
44 #include "blockinput.h"
47 #include "character.h"
49 #include "termhooks.h"
52 #ifdef HAVE_SYS_STAT_H
54 #endif /* HAVE_SYS_STAT_H */
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif /* HAVE_SYS_TYPES_H */
60 #ifdef HAVE_WINDOW_SYSTEM
62 #endif /* HAVE_WINDOW_SYSTEM */
65 #define COLOR_TABLE_SUPPORT 1
67 typedef struct x_bitmap_record Bitmap_Record
;
68 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
69 #define NO_PIXMAP None
71 #define RGB_PIXEL_COLOR unsigned long
73 #define PIX_MASK_RETAIN 0
74 #define PIX_MASK_DRAW 1
75 #endif /* HAVE_X_WINDOWS */
79 /* W32_TODO : Color tables on W32. */
80 #undef COLOR_TABLE_SUPPORT
82 typedef struct w32_bitmap_record Bitmap_Record
;
83 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
86 #define RGB_PIXEL_COLOR COLORREF
88 #define PIX_MASK_RETAIN 0
89 #define PIX_MASK_DRAW 1
91 #define x_defined_color w32_defined_color
92 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
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 */
101 #undef COLOR_TABLE_SUPPORT
103 typedef struct ns_bitmap_record Bitmap_Record
;
105 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
108 #define RGB_PIXEL_COLOR unsigned long
111 #define PIX_MASK_RETAIN 0
112 #define PIX_MASK_DRAW 1
114 #define x_defined_color(f, name, color_def, alloc) \
115 ns_defined_color (f, name, color_def, alloc, 0)
116 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
120 /* The symbol `postscript' identifying images of this type. */
122 static Lisp_Object Qpostscript
;
124 static void x_disable_image (struct frame
*, struct image
*);
125 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
128 static void init_color_table (void);
129 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
130 #ifdef COLOR_TABLE_SUPPORT
131 static void free_color_table (void);
132 static unsigned long *colors_in_color_table (int *n
);
135 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
136 id, which is just an int that this section returns. Bitmaps are
137 reference counted so they can be shared among frames.
139 Bitmap indices are guaranteed to be > 0, so a negative number can
140 be used to indicate no bitmap.
142 If you use x_create_bitmap_from_data, then you must keep track of
143 the bitmaps yourself. That is, creating a bitmap from the same
144 data more than once will not be caught. */
148 XGetImage (Display
*display
, Pixmap pixmap
, int x
, int y
,
149 unsigned int width
, unsigned int height
,
150 unsigned long plane_mask
, int format
)
152 /* TODO: not sure what this function is supposed to do.. */
153 ns_retain_object (pixmap
);
157 /* use with imgs created by ns_image_for_XPM */
159 XGetPixel (XImagePtr ximage
, int x
, int y
)
161 return ns_get_pixel (ximage
, x
, y
);
164 /* use with imgs created by ns_image_for_XPM; alpha set to 1;
165 pixel is assumed to be in form RGB */
167 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
169 ns_put_pixel (ximage
, x
, y
, pixel
);
174 /* Functions to access the contents of a bitmap, given an id. */
177 x_bitmap_height (FRAME_PTR f
, ptrdiff_t id
)
179 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
183 x_bitmap_width (FRAME_PTR f
, ptrdiff_t id
)
185 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
188 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
190 x_bitmap_pixmap (FRAME_PTR f
, ptrdiff_t id
)
192 /* HAVE_NTGUI needs the explicit cast here. */
193 return (int) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
197 #ifdef HAVE_X_WINDOWS
199 x_bitmap_mask (FRAME_PTR f
, ptrdiff_t id
)
201 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
205 /* Allocate a new bitmap record. Returns index of new record. */
208 x_allocate_bitmap_record (FRAME_PTR f
)
210 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
213 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
214 return ++dpyinfo
->bitmaps_last
;
216 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
217 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
221 xpalloc (dpyinfo
->bitmaps
, &dpyinfo
->bitmaps_size
,
222 10, -1, sizeof *dpyinfo
->bitmaps
);
223 return ++dpyinfo
->bitmaps_last
;
226 /* Add one reference to the reference count of the bitmap with id ID. */
229 x_reference_bitmap (FRAME_PTR f
, ptrdiff_t id
)
231 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
234 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
237 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
239 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
242 #ifdef HAVE_X_WINDOWS
244 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
245 bits
, width
, height
);
248 #endif /* HAVE_X_WINDOWS */
252 bitmap
= CreateBitmap (width
, height
,
253 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
254 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
258 #endif /* HAVE_NTGUI */
261 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
266 id
= x_allocate_bitmap_record (f
);
269 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
270 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
273 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
274 dpyinfo
->bitmaps
[id
- 1].height
= height
;
275 dpyinfo
->bitmaps
[id
- 1].width
= width
;
276 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
278 #ifdef HAVE_X_WINDOWS
279 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
280 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
281 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
282 #endif /* HAVE_X_WINDOWS */
285 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
286 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
287 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
288 #endif /* HAVE_NTGUI */
293 /* Create bitmap from file FILE for frame F. */
296 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
298 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
301 return -1; /* W32_TODO : bitmap support */
302 #endif /* HAVE_NTGUI */
306 void *bitmap
= ns_image_from_file (file
);
312 id
= x_allocate_bitmap_record (f
);
313 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
314 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
315 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
316 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
317 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
318 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
319 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
323 #ifdef HAVE_X_WINDOWS
324 unsigned int width
, height
;
326 int xhot
, yhot
, result
;
332 /* Look for an existing bitmap with the same name. */
333 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
335 if (dpyinfo
->bitmaps
[id
].refcount
336 && dpyinfo
->bitmaps
[id
].file
337 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
339 ++dpyinfo
->bitmaps
[id
].refcount
;
344 /* Search bitmap-file-path for the file, if appropriate. */
345 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
350 filename
= SSDATA (found
);
352 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
353 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
354 if (result
!= BitmapSuccess
)
357 id
= x_allocate_bitmap_record (f
);
358 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
359 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
360 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
361 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
362 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
363 dpyinfo
->bitmaps
[id
- 1].height
= height
;
364 dpyinfo
->bitmaps
[id
- 1].width
= width
;
365 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
368 #endif /* HAVE_X_WINDOWS */
374 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
376 #ifdef HAVE_X_WINDOWS
377 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
379 XFreePixmap (dpyinfo
->display
, bm
->mask
);
380 #endif /* HAVE_X_WINDOWS */
383 DeleteObject (bm
->pixmap
);
384 #endif /* HAVE_NTGUI */
387 ns_release_object (bm
->img
);
397 /* Remove reference to bitmap with id number ID. */
400 x_destroy_bitmap (FRAME_PTR f
, ptrdiff_t id
)
402 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
406 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
408 if (--bm
->refcount
== 0)
411 free_bitmap_record (dpyinfo
, bm
);
417 /* Free all the bitmaps for the display specified by DPYINFO. */
420 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
423 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
425 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
426 if (bm
->refcount
> 0)
427 free_bitmap_record (dpyinfo
, bm
);
429 dpyinfo
->bitmaps_last
= 0;
433 #ifdef HAVE_X_WINDOWS
435 /* Useful functions defined in the section
436 `Image type independent image structures' below. */
438 static unsigned long four_corners_best (XImagePtr ximg
,
441 unsigned long height
);
443 static int x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
,
444 int depth
, XImagePtr
*ximg
,
447 static void x_destroy_x_image (XImagePtr ximg
);
450 /* Create a mask of a bitmap. Note is this not a perfect mask.
451 It's nicer with some borders in this context */
454 x_create_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
457 XImagePtr ximg
, mask_img
;
458 unsigned long width
, height
;
461 unsigned long x
, y
, xp
, xm
, yp
, ym
;
464 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
469 pixmap
= x_bitmap_pixmap (f
, id
);
470 width
= x_bitmap_width (f
, id
);
471 height
= x_bitmap_height (f
, id
);
474 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
483 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
488 XDestroyImage (ximg
);
492 bg
= four_corners_best (ximg
, NULL
, width
, height
);
494 for (y
= 0; y
< ximg
->height
; ++y
)
496 for (x
= 0; x
< ximg
->width
; ++x
)
498 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
499 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
500 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
501 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
502 if (XGetPixel (ximg
, x
, y
) == bg
503 && XGetPixel (ximg
, x
, yp
) == bg
504 && XGetPixel (ximg
, x
, ym
) == bg
505 && XGetPixel (ximg
, xp
, y
) == bg
506 && XGetPixel (ximg
, xp
, yp
) == bg
507 && XGetPixel (ximg
, xp
, ym
) == bg
508 && XGetPixel (ximg
, xm
, y
) == bg
509 && XGetPixel (ximg
, xm
, yp
) == bg
510 && XGetPixel (ximg
, xm
, ym
) == bg
)
511 XPutPixel (mask_img
, x
, y
, 0);
513 XPutPixel (mask_img
, x
, y
, 1);
517 eassert (interrupt_input_blocked
);
518 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
519 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
521 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
523 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
524 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
526 XDestroyImage (ximg
);
527 x_destroy_x_image (mask_img
);
532 #endif /* HAVE_X_WINDOWS */
535 /***********************************************************************
537 ***********************************************************************/
539 /* List of supported image types. Use define_image_type to add new
540 types. Use lookup_image_type to find a type for a given symbol. */
542 static struct image_type
*image_types
;
544 /* The symbol `xbm' which is used as the type symbol for XBM images. */
546 static Lisp_Object Qxbm
;
550 Lisp_Object QCascent
, QCmargin
, QCrelief
;
551 Lisp_Object QCconversion
;
552 static Lisp_Object QCheuristic_mask
;
553 static Lisp_Object QCcolor_symbols
;
554 static Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
;
555 static Lisp_Object QCcrop
, QCrotation
;
559 static Lisp_Object Qcount
, Qextension_data
, Qdelay
;
560 static Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
562 /* Function prototypes. */
564 static Lisp_Object
define_image_type (struct image_type
*type
, int loaded
);
565 static struct image_type
*lookup_image_type (Lisp_Object symbol
);
566 static void image_error (const char *format
, Lisp_Object
, Lisp_Object
);
567 static void x_laplace (struct frame
*, struct image
*);
568 static void x_emboss (struct frame
*, struct image
*);
569 static int x_build_heuristic_mask (struct frame
*, struct image
*,
572 #define CACHE_IMAGE_TYPE(type, status) \
573 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
575 #define CACHE_IMAGE_TYPE(type, status)
578 #define ADD_IMAGE_TYPE(type) \
579 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
581 /* Define a new image type from TYPE. This adds a copy of TYPE to
582 image_types and caches the loading status of TYPE. */
585 define_image_type (struct image_type
*type
, int loaded
)
593 struct image_type
*p
;
594 Lisp_Object target_type
= *(type
->type
);
595 for (p
= image_types
; p
; p
= p
->next
)
596 if (EQ (*(p
->type
), target_type
))
599 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
600 The initialized data segment is read-only. */
601 p
= xmalloc (sizeof *p
);
603 p
->next
= image_types
;
608 CACHE_IMAGE_TYPE (*type
->type
, success
);
613 /* Look up image type SYMBOL, and return a pointer to its image_type
614 structure. Value is null if SYMBOL is not a known image type. */
616 static inline struct image_type
*
617 lookup_image_type (Lisp_Object symbol
)
619 struct image_type
*type
;
621 /* We must initialize the image-type if it hasn't been already. */
622 if (NILP (Finit_image_library (symbol
, Vdynamic_library_alist
)))
623 return 0; /* unimplemented */
625 for (type
= image_types
; type
; type
= type
->next
)
626 if (EQ (symbol
, *type
->type
))
633 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
634 valid image specification is a list whose car is the symbol
635 `image', and whose rest is a property list. The property list must
636 contain a value for key `:type'. That value must be the name of a
637 supported image type. The rest of the property list depends on the
641 valid_image_p (Lisp_Object object
)
649 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
650 if (EQ (XCAR (tem
), QCtype
))
653 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
655 struct image_type
*type
;
656 type
= lookup_image_type (XCAR (tem
));
658 valid_p
= type
->valid_p (object
);
669 /* Log error message with format string FORMAT and argument ARG.
670 Signaling an error, e.g. when an image cannot be loaded, is not a
671 good idea because this would interrupt redisplay, and the error
672 message display would lead to another redisplay. This function
673 therefore simply displays a message. */
676 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
678 add_to_log (format
, arg1
, arg2
);
683 /***********************************************************************
685 ***********************************************************************/
687 enum image_value_type
689 IMAGE_DONT_CHECK_VALUE_TYPE
,
691 IMAGE_STRING_OR_NIL_VALUE
,
693 IMAGE_POSITIVE_INTEGER_VALUE
,
694 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
,
695 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
698 IMAGE_FUNCTION_VALUE
,
703 /* Structure used when parsing image specifications. */
707 /* Name of keyword. */
710 /* The type of value allowed. */
711 enum image_value_type type
;
713 /* Non-zero means key must be present. */
716 /* Used to recognize duplicate keywords in a property list. */
719 /* The value that was found. */
724 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
726 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
729 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
730 has the format (image KEYWORD VALUE ...). One of the keyword/
731 value pairs must be `:type TYPE'. KEYWORDS is a vector of
732 image_keywords structures of size NKEYWORDS describing other
733 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
736 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
737 int nkeywords
, Lisp_Object type
)
746 while (CONSP (plist
))
748 Lisp_Object key
, value
;
750 /* First element of a pair must be a symbol. */
752 plist
= XCDR (plist
);
756 /* There must follow a value. */
759 value
= XCAR (plist
);
760 plist
= XCDR (plist
);
762 /* Find key in KEYWORDS. Error if not found. */
763 for (i
= 0; i
< nkeywords
; ++i
)
764 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
770 /* Record that we recognized the keyword. If a keywords
771 was found more than once, it's an error. */
772 keywords
[i
].value
= value
;
773 if (keywords
[i
].count
> 1)
777 /* Check type of value against allowed type. */
778 switch (keywords
[i
].type
)
780 case IMAGE_STRING_VALUE
:
781 if (!STRINGP (value
))
785 case IMAGE_STRING_OR_NIL_VALUE
:
786 if (!STRINGP (value
) && !NILP (value
))
790 case IMAGE_SYMBOL_VALUE
:
791 if (!SYMBOLP (value
))
795 case IMAGE_POSITIVE_INTEGER_VALUE
:
796 if (! RANGED_INTEGERP (1, value
, INT_MAX
))
800 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
:
801 if (RANGED_INTEGERP (0, value
, INT_MAX
))
804 && RANGED_INTEGERP (0, XCAR (value
), INT_MAX
)
805 && RANGED_INTEGERP (0, XCDR (value
), INT_MAX
))
809 case IMAGE_ASCENT_VALUE
:
810 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
812 else if (RANGED_INTEGERP (0, value
, 100))
816 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
817 /* Unlike the other integer-related cases, this one does not
818 verify that VALUE fits in 'int'. This is because callers
820 if (!INTEGERP (value
) || XINT (value
) < 0)
824 case IMAGE_DONT_CHECK_VALUE_TYPE
:
827 case IMAGE_FUNCTION_VALUE
:
828 value
= indirect_function (value
);
829 if (!NILP (Ffunctionp (value
)))
833 case IMAGE_NUMBER_VALUE
:
834 if (!INTEGERP (value
) && !FLOATP (value
))
838 case IMAGE_INTEGER_VALUE
:
839 if (! TYPE_RANGED_INTEGERP (int, value
))
843 case IMAGE_BOOL_VALUE
:
844 if (!NILP (value
) && !EQ (value
, Qt
))
853 if (EQ (key
, QCtype
) && !EQ (type
, value
))
857 /* Check that all mandatory fields are present. */
858 for (i
= 0; i
< nkeywords
; ++i
)
859 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
866 /* Return the value of KEY in image specification SPEC. Value is nil
867 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
868 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
871 image_spec_value (Lisp_Object spec
, Lisp_Object key
, int *found
)
875 eassert (valid_image_p (spec
));
877 for (tail
= XCDR (spec
);
878 CONSP (tail
) && CONSP (XCDR (tail
));
879 tail
= XCDR (XCDR (tail
)))
881 if (EQ (XCAR (tail
), key
))
885 return XCAR (XCDR (tail
));
895 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
896 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
897 PIXELS non-nil means return the size in pixels, otherwise return the
898 size in canonical character units.
899 FRAME is the frame on which the image will be displayed. FRAME nil
900 or omitted means use the selected frame. */)
901 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
906 if (valid_image_p (spec
))
908 struct frame
*f
= check_x_frame (frame
);
909 ptrdiff_t id
= lookup_image (f
, spec
);
910 struct image
*img
= IMAGE_FROM_ID (f
, id
);
911 int width
= img
->width
+ 2 * img
->hmargin
;
912 int height
= img
->height
+ 2 * img
->vmargin
;
915 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
916 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
918 size
= Fcons (make_number (width
), make_number (height
));
921 error ("Invalid image specification");
927 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
928 doc
: /* Return t if image SPEC has a mask bitmap.
929 FRAME is the frame on which the image will be displayed. FRAME nil
930 or omitted means use the selected frame. */)
931 (Lisp_Object spec
, Lisp_Object frame
)
936 if (valid_image_p (spec
))
938 struct frame
*f
= check_x_frame (frame
);
939 ptrdiff_t id
= lookup_image (f
, spec
);
940 struct image
*img
= IMAGE_FROM_ID (f
, id
);
945 error ("Invalid image specification");
950 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
951 doc
: /* Return metadata for image SPEC.
952 FRAME is the frame on which the image will be displayed. FRAME nil
953 or omitted means use the selected frame. */)
954 (Lisp_Object spec
, Lisp_Object frame
)
959 if (valid_image_p (spec
))
961 struct frame
*f
= check_x_frame (frame
);
962 ptrdiff_t id
= lookup_image (f
, spec
);
963 struct image
*img
= IMAGE_FROM_ID (f
, id
);
964 ext
= img
->lisp_data
;
971 /***********************************************************************
972 Image type independent image structures
973 ***********************************************************************/
975 static void free_image (struct frame
*f
, struct image
*img
);
977 #define MAX_IMAGE_SIZE 10.0
978 /* Allocate and return a new image structure for image specification
979 SPEC. SPEC has a hash value of HASH. */
981 static struct image
*
982 make_image (Lisp_Object spec
, EMACS_UINT hash
)
984 struct image
*img
= xzalloc (sizeof *img
);
985 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
987 eassert (valid_image_p (spec
));
988 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
989 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
990 eassert (img
->type
!= NULL
);
992 img
->lisp_data
= Qnil
;
993 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
995 img
->corners
[BOT_CORNER
] = -1; /* Full image */
1000 /* Free image IMG which was used on frame F, including its resources. */
1003 free_image (struct frame
*f
, struct image
*img
)
1007 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1009 /* Remove IMG from the hash table of its cache. */
1011 img
->prev
->next
= img
->next
;
1013 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1016 img
->next
->prev
= img
->prev
;
1018 c
->images
[img
->id
] = NULL
;
1020 /* Free resources, then free IMG. */
1021 img
->type
->free (f
, img
);
1026 /* Return 1 if the given widths and heights are valid for display;
1027 otherwise, return 0. */
1030 check_image_size (struct frame
*f
, int width
, int height
)
1034 if (width
<= 0 || height
<= 0)
1037 if (INTEGERP (Vmax_image_size
))
1038 return (width
<= XINT (Vmax_image_size
)
1039 && height
<= XINT (Vmax_image_size
));
1040 else if (FLOATP (Vmax_image_size
))
1044 w
= FRAME_PIXEL_WIDTH (f
);
1045 h
= FRAME_PIXEL_HEIGHT (f
);
1048 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1049 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1050 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1056 /* Prepare image IMG for display on frame F. Must be called before
1057 drawing an image. */
1060 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1062 /* We're about to display IMG, so set its timestamp to `now'. */
1063 img
->timestamp
= current_emacs_time ();
1065 /* If IMG doesn't have a pixmap yet, load it now, using the image
1066 type dependent loader function. */
1067 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1068 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1073 /* Value is the number of pixels for the ascent of image IMG when
1074 drawn in face FACE. */
1077 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1082 if (slice
->height
== img
->height
)
1083 height
= img
->height
+ img
->vmargin
;
1084 else if (slice
->y
== 0)
1085 height
= slice
->height
+ img
->vmargin
;
1087 height
= slice
->height
;
1089 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1094 /* W32 specific version. Why?. ++kfs */
1095 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1096 - FONT_BASE (face
->font
)) / 2;
1098 /* This expression is arranged so that if the image can't be
1099 exactly centered, it will be moved slightly up. This is
1100 because a typical font is `top-heavy' (due to the presence
1101 uppercase letters), so the image placement should err towards
1102 being top-heavy too. It also just generally looks better. */
1103 ascent
= (height
+ FONT_BASE (face
->font
)
1104 - FONT_DESCENT (face
->font
) + 1) / 2;
1105 #endif /* HAVE_NTGUI */
1108 ascent
= height
/ 2;
1111 ascent
= height
* (img
->ascent
/ 100.0);
1117 /* Image background colors. */
1119 /* Find the "best" corner color of a bitmap.
1120 On W32, XIMG is assumed to a device context with the bitmap selected. */
1122 static RGB_PIXEL_COLOR
1123 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1124 unsigned long width
, unsigned long height
)
1126 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1129 if (corners
&& corners
[BOT_CORNER
] >= 0)
1131 /* Get the colors at the corner_pixels of ximg. */
1132 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1133 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1134 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1135 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1139 /* Get the colors at the corner_pixels of ximg. */
1140 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1141 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1142 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1143 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1145 /* Choose the most frequently found color as background. */
1146 for (i
= best_count
= 0; i
< 4; ++i
)
1150 for (j
= n
= 0; j
< 4; ++j
)
1151 if (corner_pixels
[i
] == corner_pixels
[j
])
1155 best
= corner_pixels
[i
], best_count
= n
;
1161 /* Portability macros */
1165 #define Destroy_Image(img_dc, prev) \
1166 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1168 #define Free_Pixmap(display, pixmap) \
1169 DeleteObject (pixmap)
1171 #elif defined (HAVE_NS)
1173 #define Destroy_Image(ximg, dummy) \
1174 ns_release_object (ximg)
1176 #define Free_Pixmap(display, pixmap) \
1177 ns_release_object (pixmap)
1181 #define Destroy_Image(ximg, dummy) \
1182 XDestroyImage (ximg)
1184 #define Free_Pixmap(display, pixmap) \
1185 XFreePixmap (display, pixmap)
1187 #endif /* !HAVE_NTGUI && !HAVE_NS */
1190 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1191 it is guessed heuristically. If non-zero, XIMG is an existing
1192 XImage object (or device context with the image selected on W32) to
1193 use for the heuristic. */
1196 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1198 if (! img
->background_valid
)
1199 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1201 int free_ximg
= !ximg
;
1204 #endif /* HAVE_NTGUI */
1209 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1210 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1212 HDC frame_dc
= get_frame_dc (f
);
1213 ximg
= CreateCompatibleDC (frame_dc
);
1214 release_frame_dc (f
, frame_dc
);
1215 prev
= SelectObject (ximg
, img
->pixmap
);
1216 #endif /* !HAVE_NTGUI */
1219 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1222 Destroy_Image (ximg
, prev
);
1224 img
->background_valid
= 1;
1227 return img
->background
;
1230 /* Return the `background_transparent' field of IMG. If IMG doesn't
1231 have one yet, it is guessed heuristically. If non-zero, MASK is an
1232 existing XImage object to use for the heuristic. */
1235 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1237 if (! img
->background_transparent_valid
)
1238 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1242 int free_mask
= !mask
;
1245 #endif /* HAVE_NTGUI */
1250 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1251 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1253 HDC frame_dc
= get_frame_dc (f
);
1254 mask
= CreateCompatibleDC (frame_dc
);
1255 release_frame_dc (f
, frame_dc
);
1256 prev
= SelectObject (mask
, img
->mask
);
1257 #endif /* HAVE_NTGUI */
1260 img
->background_transparent
1261 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1264 Destroy_Image (mask
, prev
);
1267 img
->background_transparent
= 0;
1269 img
->background_transparent_valid
= 1;
1272 return img
->background_transparent
;
1276 /***********************************************************************
1277 Helper functions for X image types
1278 ***********************************************************************/
1280 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1282 static void x_clear_image (struct frame
*f
, struct image
*img
);
1283 static unsigned long x_alloc_image_color (struct frame
*f
,
1285 Lisp_Object color_name
,
1286 unsigned long dflt
);
1289 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1290 free the pixmap if any. MASK_P non-zero means clear the mask
1291 pixmap if any. COLORS_P non-zero means free colors allocated for
1292 the image, if any. */
1295 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1298 if (pixmap_p
&& img
->pixmap
)
1300 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1301 img
->pixmap
= NO_PIXMAP
;
1302 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1303 img
->background_valid
= 0;
1306 if (mask_p
&& img
->mask
)
1308 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1309 img
->mask
= NO_PIXMAP
;
1310 img
->background_transparent_valid
= 0;
1313 if (colors_p
&& img
->ncolors
)
1315 /* W32_TODO: color table support. */
1316 #ifdef HAVE_X_WINDOWS
1317 x_free_colors (f
, img
->colors
, img
->ncolors
);
1318 #endif /* HAVE_X_WINDOWS */
1319 xfree (img
->colors
);
1326 /* Free X resources of image IMG which is used on frame F. */
1329 x_clear_image (struct frame
*f
, struct image
*img
)
1332 x_clear_image_1 (f
, img
, 1, 1, 1);
1337 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1338 cannot be allocated, use DFLT. Add a newly allocated color to
1339 IMG->colors, so that it can be freed again. Value is the pixel
1342 static unsigned long
1343 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1347 unsigned long result
;
1349 eassert (STRINGP (color_name
));
1351 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1352 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1355 /* This isn't called frequently so we get away with simply
1356 reallocating the color vector to the needed size, here. */
1357 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1358 img
->colors
= xrealloc (img
->colors
, ncolors
* sizeof *img
->colors
);
1359 img
->colors
[ncolors
- 1] = color
.pixel
;
1360 img
->ncolors
= ncolors
;
1361 result
= color
.pixel
;
1371 /***********************************************************************
1373 ***********************************************************************/
1375 static void cache_image (struct frame
*f
, struct image
*img
);
1376 static void postprocess_image (struct frame
*, struct image
*);
1378 /* Return a new, initialized image cache that is allocated from the
1379 heap. Call free_image_cache to free an image cache. */
1381 struct image_cache
*
1382 make_image_cache (void)
1384 struct image_cache
*c
= xzalloc (sizeof *c
);
1388 c
->images
= xmalloc (size
* sizeof *c
->images
);
1390 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1391 c
->buckets
= xzalloc (size
);
1396 /* Find an image matching SPEC in the cache, and return it. If no
1397 image is found, return NULL. */
1398 static struct image
*
1399 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1402 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1403 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1405 if (!c
) return NULL
;
1407 /* If the image spec does not specify a background color, the cached
1408 image must have the same background color as the current frame.
1409 The foreground color must also match, for the sake of monochrome
1412 In fact, we could ignore the foreground color matching condition
1413 for color images, or if the image spec specifies :foreground;
1414 similarly we could ignore the background color matching condition
1415 for formats that don't use transparency (such as jpeg), or if the
1416 image spec specifies :background. However, the extra memory
1417 usage is probably negligible in practice, so we don't bother. */
1419 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1420 if (img
->hash
== hash
1421 && !NILP (Fequal (img
->spec
, spec
))
1422 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1423 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1429 /* Search frame F for an image with spec SPEC, and free it. */
1432 uncache_image (struct frame
*f
, Lisp_Object spec
)
1434 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1437 free_image (f
, img
);
1438 /* As display glyphs may still be referring to the image ID, we
1439 must garbage the frame (Bug#6426). */
1440 SET_FRAME_GARBAGED (f
);
1445 /* Free image cache of frame F. Be aware that X frames share images
1449 free_image_cache (struct frame
*f
)
1451 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1456 /* Cache should not be referenced by any frame when freed. */
1457 eassert (c
->refcount
== 0);
1459 for (i
= 0; i
< c
->used
; ++i
)
1460 free_image (f
, c
->images
[i
]);
1464 FRAME_IMAGE_CACHE (f
) = NULL
;
1469 /* Clear image cache of frame F. FILTER=t means free all images.
1470 FILTER=nil means clear only images that haven't been
1471 displayed for some time.
1472 Else, only free the images which have FILTER in their `dependencies'.
1473 Should be called from time to time to reduce the number of loaded images.
1474 If image-cache-eviction-delay is non-nil, this frees images in the cache
1475 which weren't displayed for at least that many seconds. */
1478 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1480 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1484 ptrdiff_t i
, nfreed
= 0;
1486 /* Block input so that we won't be interrupted by a SIGIO
1487 while being in an inconsistent state. */
1492 /* Filter image cache. */
1493 for (i
= 0; i
< c
->used
; ++i
)
1495 struct image
*img
= c
->images
[i
];
1496 if (img
&& (EQ (Qt
, filter
)
1497 || !NILP (Fmember (filter
, img
->dependencies
))))
1499 free_image (f
, img
);
1504 else if (INTEGERP (Vimage_cache_eviction_delay
))
1506 /* Free cache based on timestamp. */
1509 ptrdiff_t nimages
= 0;
1511 for (i
= 0; i
< c
->used
; ++i
)
1515 /* If the number of cached images has grown unusually large,
1516 decrease the cache eviction delay (Bug#6230). */
1517 delay
= XINT (Vimage_cache_eviction_delay
);
1519 delay
= 1600 * delay
/ nimages
/ nimages
;
1520 delay
= max (delay
, 1);
1522 t
= current_emacs_time ();
1523 old
= sub_emacs_time (t
, EMACS_TIME_FROM_DOUBLE (delay
));
1525 for (i
= 0; i
< c
->used
; ++i
)
1527 struct image
*img
= c
->images
[i
];
1528 if (img
&& EMACS_TIME_LT (img
->timestamp
, old
))
1530 free_image (f
, img
);
1536 /* We may be clearing the image cache because, for example,
1537 Emacs was iconified for a longer period of time. In that
1538 case, current matrices may still contain references to
1539 images freed above. So, clear these matrices. */
1542 Lisp_Object tail
, frame
;
1544 FOR_EACH_FRAME (tail
, frame
)
1546 struct frame
*fr
= XFRAME (frame
);
1547 if (FRAME_IMAGE_CACHE (fr
) == c
)
1548 clear_current_matrices (fr
);
1551 ++windows_or_buffers_changed
;
1559 clear_image_caches (Lisp_Object filter
)
1561 /* FIXME: We want to do
1562 * struct terminal *t;
1563 * for (t = terminal_list; t; t = t->next_terminal)
1564 * clear_image_cache (t, filter); */
1565 Lisp_Object tail
, frame
;
1566 FOR_EACH_FRAME (tail
, frame
)
1567 if (FRAME_WINDOW_P (XFRAME (frame
)))
1568 clear_image_cache (XFRAME (frame
), filter
);
1571 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1573 doc
: /* Clear the image cache.
1574 FILTER nil or a frame means clear all images in the selected frame.
1575 FILTER t means clear the image caches of all frames.
1576 Anything else, means only clear those images which refer to FILTER,
1577 which is then usually a filename. */)
1578 (Lisp_Object filter
)
1580 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1581 clear_image_caches (filter
);
1583 clear_image_cache (check_x_frame (filter
), Qt
);
1589 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1591 doc
: /* Fush the image with specification SPEC on frame FRAME.
1592 This removes the image from the Emacs image cache. If SPEC specifies
1593 an image file, the next redisplay of this image will read from the
1594 current contents of that file.
1596 FRAME nil or omitted means use the selected frame.
1597 FRAME t means refresh the image on all frames. */)
1598 (Lisp_Object spec
, Lisp_Object frame
)
1600 if (!valid_image_p (spec
))
1601 error ("Invalid image specification");
1606 FOR_EACH_FRAME (tail
, frame
)
1608 struct frame
*f
= XFRAME (frame
);
1609 if (FRAME_WINDOW_P (f
))
1610 uncache_image (f
, spec
);
1614 uncache_image (check_x_frame (frame
), spec
);
1620 /* Compute masks and transform image IMG on frame F, as specified
1621 by the image's specification, */
1624 postprocess_image (struct frame
*f
, struct image
*img
)
1626 /* Manipulation of the image's mask. */
1629 Lisp_Object conversion
, spec
;
1634 /* `:heuristic-mask t'
1636 means build a mask heuristically.
1637 `:heuristic-mask (R G B)'
1638 `:mask (heuristic (R G B))'
1639 means build a mask from color (R G B) in the
1642 means remove a mask, if any. */
1644 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1646 x_build_heuristic_mask (f
, img
, mask
);
1651 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1653 if (EQ (mask
, Qheuristic
))
1654 x_build_heuristic_mask (f
, img
, Qt
);
1655 else if (CONSP (mask
)
1656 && EQ (XCAR (mask
), Qheuristic
))
1658 if (CONSP (XCDR (mask
)))
1659 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1661 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1663 else if (NILP (mask
) && found_p
&& img
->mask
)
1665 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1666 img
->mask
= NO_PIXMAP
;
1671 /* Should we apply an image transformation algorithm? */
1672 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1673 if (EQ (conversion
, Qdisabled
))
1674 x_disable_image (f
, img
);
1675 else if (EQ (conversion
, Qlaplace
))
1677 else if (EQ (conversion
, Qemboss
))
1679 else if (CONSP (conversion
)
1680 && EQ (XCAR (conversion
), Qedge_detection
))
1683 tem
= XCDR (conversion
);
1685 x_edge_detection (f
, img
,
1686 Fplist_get (tem
, QCmatrix
),
1687 Fplist_get (tem
, QCcolor_adjustment
));
1693 /* Return the id of image with Lisp specification SPEC on frame F.
1694 SPEC must be a valid Lisp image specification (see valid_image_p). */
1697 lookup_image (struct frame
*f
, Lisp_Object spec
)
1702 /* F must be a window-system frame, and SPEC must be a valid image
1704 eassert (FRAME_WINDOW_P (f
));
1705 eassert (valid_image_p (spec
));
1707 /* Look up SPEC in the hash table of the image cache. */
1708 hash
= sxhash (spec
, 0);
1709 img
= search_image_cache (f
, spec
, hash
);
1710 if (img
&& img
->load_failed_p
)
1712 free_image (f
, img
);
1716 /* If not found, create a new image and cache it. */
1720 img
= make_image (spec
, hash
);
1721 cache_image (f
, img
);
1722 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1723 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1724 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1726 /* If we can't load the image, and we don't have a width and
1727 height, use some arbitrary width and height so that we can
1728 draw a rectangle for it. */
1729 if (img
->load_failed_p
)
1733 value
= image_spec_value (spec
, QCwidth
, NULL
);
1734 img
->width
= (INTEGERP (value
)
1735 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1736 value
= image_spec_value (spec
, QCheight
, NULL
);
1737 img
->height
= (INTEGERP (value
)
1738 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1742 /* Handle image type independent image attributes
1743 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1744 `:background COLOR'. */
1745 Lisp_Object ascent
, margin
, relief
, bg
;
1748 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1749 if (INTEGERP (ascent
))
1750 img
->ascent
= XFASTINT (ascent
);
1751 else if (EQ (ascent
, Qcenter
))
1752 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1754 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1755 if (INTEGERP (margin
))
1756 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1757 else if (CONSP (margin
))
1759 img
->hmargin
= XFASTINT (XCAR (margin
));
1760 img
->vmargin
= XFASTINT (XCDR (margin
));
1763 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1764 relief_bound
= INT_MAX
- max (img
->hmargin
, img
->vmargin
);
1765 if (RANGED_INTEGERP (- relief_bound
, relief
, relief_bound
))
1767 img
->relief
= XINT (relief
);
1768 img
->hmargin
+= eabs (img
->relief
);
1769 img
->vmargin
+= eabs (img
->relief
);
1772 if (! img
->background_valid
)
1774 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1778 = x_alloc_image_color (f
, img
, bg
,
1779 FRAME_BACKGROUND_PIXEL (f
));
1780 img
->background_valid
= 1;
1784 /* Do image transformations and compute masks, unless we
1785 don't have the image yet. */
1786 if (!EQ (*img
->type
->type
, Qpostscript
))
1787 postprocess_image (f
, img
);
1793 /* We're using IMG, so set its timestamp to `now'. */
1794 img
->timestamp
= current_emacs_time ();
1796 /* Value is the image id. */
1801 /* Cache image IMG in the image cache of frame F. */
1804 cache_image (struct frame
*f
, struct image
*img
)
1806 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1809 /* Find a free slot in c->images. */
1810 for (i
= 0; i
< c
->used
; ++i
)
1811 if (c
->images
[i
] == NULL
)
1814 /* If no free slot found, maybe enlarge c->images. */
1815 if (i
== c
->used
&& c
->used
== c
->size
)
1816 c
->images
= xpalloc (c
->images
, &c
->size
, 1, -1, sizeof *c
->images
);
1818 /* Add IMG to c->images, and assign IMG an id. */
1824 /* Add IMG to the cache's hash table. */
1825 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1826 img
->next
= c
->buckets
[i
];
1828 img
->next
->prev
= img
;
1830 c
->buckets
[i
] = img
;
1834 /* Call FN on every image in the image cache of frame F. Used to mark
1835 Lisp Objects in the image cache. */
1837 /* Mark Lisp objects in image IMG. */
1840 mark_image (struct image
*img
)
1842 mark_object (img
->spec
);
1843 mark_object (img
->dependencies
);
1845 if (!NILP (img
->lisp_data
))
1846 mark_object (img
->lisp_data
);
1851 mark_image_cache (struct image_cache
*c
)
1856 for (i
= 0; i
< c
->used
; ++i
)
1858 mark_image (c
->images
[i
]);
1864 /***********************************************************************
1865 X / NS / W32 support code
1866 ***********************************************************************/
1870 /* Macro for defining functions that will be loaded from image DLLs. */
1871 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1873 /* Macro for loading those image functions from the library. */
1874 #define LOAD_IMGLIB_FN(lib,func) { \
1875 fn_##func = (void *) GetProcAddress (lib, #func); \
1876 if (!fn_##func) return 0; \
1879 #endif /* HAVE_NTGUI */
1881 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1882 XImagePtr
*, Pixmap
*);
1883 static void x_destroy_x_image (XImagePtr
);
1884 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1886 /* Return nonzero if XIMG's size WIDTH x HEIGHT doesn't break the
1888 WIDTH and HEIGHT must both be positive.
1889 If XIMG is null, assume it is a bitmap. */
1891 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1893 #ifdef HAVE_X_WINDOWS
1894 /* Respect Xlib's limits: it cannot deal with images that have more
1895 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1896 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1899 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1900 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1903 int bitmap_pad
, depth
, bytes_per_line
;
1906 bitmap_pad
= ximg
->bitmap_pad
;
1907 depth
= ximg
->depth
;
1908 bytes_per_line
= ximg
->bytes_per_line
;
1914 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1916 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1917 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1919 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1920 For now, assume that every image size is allowed on these systems. */
1925 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1926 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1927 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1928 via xmalloc. Print error messages via image_error if an error
1929 occurs. Value is non-zero if successful.
1931 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1932 should indicate the bit depth of the image. */
1935 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1936 XImagePtr
*ximg
, Pixmap
*pixmap
)
1938 #ifdef HAVE_X_WINDOWS
1939 Display
*display
= FRAME_X_DISPLAY (f
);
1940 Window window
= FRAME_X_WINDOW (f
);
1941 Screen
*screen
= FRAME_X_SCREEN (f
);
1943 eassert (interrupt_input_blocked
);
1946 depth
= DefaultDepthOfScreen (screen
);
1947 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1948 depth
, ZPixmap
, 0, NULL
, width
, height
,
1949 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1952 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1956 if (! x_check_image_size (*ximg
, width
, height
))
1958 x_destroy_x_image (*ximg
);
1960 image_error ("Image too large (%dx%d)",
1961 make_number (width
), make_number (height
));
1965 /* Allocate image raster. */
1966 (*ximg
)->data
= xmalloc ((*ximg
)->bytes_per_line
* height
);
1968 /* Allocate a pixmap of the same size. */
1969 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1970 if (*pixmap
== NO_PIXMAP
)
1972 x_destroy_x_image (*ximg
);
1974 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1979 #endif /* HAVE_X_WINDOWS */
1983 BITMAPINFOHEADER
*header
;
1985 int scanline_width_bits
;
1987 int palette_colors
= 0;
1992 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1993 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1995 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
1999 scanline_width_bits
= width
* depth
;
2000 remainder
= scanline_width_bits
% 32;
2003 scanline_width_bits
+= 32 - remainder
;
2005 /* Bitmaps with a depth less than 16 need a palette. */
2006 /* BITMAPINFO structure already contains the first RGBQUAD. */
2008 palette_colors
= 1 << (depth
- 1);
2010 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2012 header
= &(*ximg
)->info
.bmiHeader
;
2013 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2014 header
->biSize
= sizeof (*header
);
2015 header
->biWidth
= width
;
2016 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2017 header
->biPlanes
= 1;
2018 header
->biBitCount
= depth
;
2019 header
->biCompression
= BI_RGB
;
2020 header
->biClrUsed
= palette_colors
;
2022 /* TODO: fill in palette. */
2025 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2026 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2027 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2028 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2029 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2030 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2031 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2032 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2035 hdc
= get_frame_dc (f
);
2037 /* Create a DIBSection and raster array for the bitmap,
2038 and store its handle in *pixmap. */
2039 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2040 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2041 /* casting avoids a GCC warning */
2042 (void **)&((*ximg
)->data
), NULL
, 0);
2044 /* Realize display palette and garbage all frames. */
2045 release_frame_dc (f
, hdc
);
2047 if (*pixmap
== NULL
)
2049 DWORD err
= GetLastError ();
2050 Lisp_Object errcode
;
2051 /* All system errors are < 10000, so the following is safe. */
2052 XSETINT (errcode
, err
);
2053 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2054 x_destroy_x_image (*ximg
);
2060 #endif /* HAVE_NTGUI */
2063 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2067 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2076 /* Destroy XImage XIMG. Free XIMG->data. */
2079 x_destroy_x_image (XImagePtr ximg
)
2081 eassert (interrupt_input_blocked
);
2084 #ifdef HAVE_X_WINDOWS
2087 XDestroyImage (ximg
);
2088 #endif /* HAVE_X_WINDOWS */
2090 /* Data will be freed by DestroyObject. */
2093 #endif /* HAVE_NTGUI */
2095 ns_release_object (ximg
);
2096 #endif /* HAVE_NS */
2101 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2102 are width and height of both the image and pixmap. */
2105 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2107 #ifdef HAVE_X_WINDOWS
2110 eassert (interrupt_input_blocked
);
2111 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2112 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2113 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2114 #endif /* HAVE_X_WINDOWS */
2117 #if 0 /* I don't think this is necessary looking at where it is used. */
2118 HDC hdc
= get_frame_dc (f
);
2119 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2120 release_frame_dc (f
, hdc
);
2122 #endif /* HAVE_NTGUI */
2125 eassert (ximg
== pixmap
);
2126 ns_retain_object (ximg
);
2131 /***********************************************************************
2133 ***********************************************************************/
2135 /* Find image file FILE. Look in data-directory/images, then
2136 x-bitmap-file-path. Value is the encoded full name of the file
2137 found, or nil if not found. */
2140 x_find_image_file (Lisp_Object file
)
2142 Lisp_Object file_found
, search_path
;
2145 /* TODO I think this should use something like image-load-path
2146 instead. Unfortunately, that can contain non-string elements. */
2147 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2149 Vx_bitmap_file_path
);
2151 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2152 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2158 file_found
= ENCODE_FILE (file_found
);
2166 /* Read FILE into memory. Value is a pointer to a buffer allocated
2167 with xmalloc holding FILE's contents. Value is null if an error
2168 occurred. *SIZE is set to the size of the file. */
2170 static unsigned char *
2171 slurp_file (char *file
, ptrdiff_t *size
)
2174 unsigned char *buf
= NULL
;
2177 if (stat (file
, &st
) == 0
2178 && (fp
= fopen (file
, "rb")) != NULL
2179 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
2180 && (buf
= xmalloc (st
.st_size
),
2181 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2202 /***********************************************************************
2204 ***********************************************************************/
2206 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2207 static int xbm_load (struct frame
*f
, struct image
*img
);
2208 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2209 unsigned char *, unsigned char *);
2210 static int xbm_image_p (Lisp_Object object
);
2211 static int xbm_read_bitmap_data (struct frame
*f
,
2212 unsigned char *, unsigned char *,
2213 int *, int *, char **, int);
2214 static int xbm_file_p (Lisp_Object
);
2217 /* Indices of image specification fields in xbm_format, below. */
2219 enum xbm_keyword_index
2237 /* Vector of image_keyword structures describing the format
2238 of valid XBM image specifications. */
2240 static const struct image_keyword xbm_format
[XBM_LAST
] =
2242 {":type", IMAGE_SYMBOL_VALUE
, 1},
2243 {":file", IMAGE_STRING_VALUE
, 0},
2244 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2245 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2246 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2247 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2248 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2249 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2250 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
2251 {":relief", IMAGE_INTEGER_VALUE
, 0},
2252 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2253 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2254 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2257 /* Structure describing the image type XBM. */
2259 static struct image_type xbm_type
=
2268 /* Tokens returned from xbm_scan. */
2277 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2278 A valid specification is a list starting with the symbol `image'
2279 The rest of the list is a property list which must contain an
2282 If the specification specifies a file to load, it must contain
2283 an entry `:file FILENAME' where FILENAME is a string.
2285 If the specification is for a bitmap loaded from memory it must
2286 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2287 WIDTH and HEIGHT are integers > 0. DATA may be:
2289 1. a string large enough to hold the bitmap data, i.e. it must
2290 have a size >= (WIDTH + 7) / 8 * HEIGHT
2292 2. a bool-vector of size >= WIDTH * HEIGHT
2294 3. a vector of strings or bool-vectors, one for each line of the
2297 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2298 may not be specified in this case because they are defined in the
2301 Both the file and data forms may contain the additional entries
2302 `:background COLOR' and `:foreground COLOR'. If not present,
2303 foreground and background of the frame on which the image is
2304 displayed is used. */
2307 xbm_image_p (Lisp_Object object
)
2309 struct image_keyword kw
[XBM_LAST
];
2311 memcpy (kw
, xbm_format
, sizeof kw
);
2312 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2315 eassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2317 if (kw
[XBM_FILE
].count
)
2319 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2322 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2324 /* In-memory XBM file. */
2325 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2333 /* Entries for `:width', `:height' and `:data' must be present. */
2334 if (!kw
[XBM_WIDTH
].count
2335 || !kw
[XBM_HEIGHT
].count
2336 || !kw
[XBM_DATA
].count
)
2339 data
= kw
[XBM_DATA
].value
;
2340 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2341 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2343 /* Check type of data, and width and height against contents of
2349 /* Number of elements of the vector must be >= height. */
2350 if (ASIZE (data
) < height
)
2353 /* Each string or bool-vector in data must be large enough
2354 for one line of the image. */
2355 for (i
= 0; i
< height
; ++i
)
2357 Lisp_Object elt
= AREF (data
, i
);
2362 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2365 else if (BOOL_VECTOR_P (elt
))
2367 if (XBOOL_VECTOR (elt
)->size
< width
)
2374 else if (STRINGP (data
))
2377 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2380 else if (BOOL_VECTOR_P (data
))
2382 if (XBOOL_VECTOR (data
)->size
/ height
< width
)
2393 /* Scan a bitmap file. FP is the stream to read from. Value is
2394 either an enumerator from enum xbm_token, or a character for a
2395 single-character token, or 0 at end of file. If scanning an
2396 identifier, store the lexeme of the identifier in SVAL. If
2397 scanning a number, store its value in *IVAL. */
2400 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2406 /* Skip white space. */
2407 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
2412 else if (c_isdigit (c
))
2414 int value
= 0, digit
;
2416 if (c
== '0' && *s
< end
)
2419 if (c
== 'x' || c
== 'X')
2426 else if (c
>= 'a' && c
<= 'f')
2427 digit
= c
- 'a' + 10;
2428 else if (c
>= 'A' && c
<= 'F')
2429 digit
= c
- 'A' + 10;
2432 value
= 16 * value
+ digit
;
2435 else if (c_isdigit (c
))
2439 && (c
= *(*s
)++, c_isdigit (c
)))
2440 value
= 8 * value
+ c
- '0';
2447 && (c
= *(*s
)++, c_isdigit (c
)))
2448 value
= 10 * value
+ c
- '0';
2456 else if (c_isalpha (c
) || c
== '_')
2460 && (c
= *(*s
)++, (c_isalnum (c
) || c
== '_')))
2467 else if (c
== '/' && **s
== '*')
2469 /* C-style comment. */
2471 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2485 /* Create a Windows bitmap from X bitmap data. */
2487 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2489 static unsigned char swap_nibble
[16]
2490 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2491 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2492 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2493 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2495 unsigned char *bits
, *p
;
2498 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2499 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2500 bits
= alloca (height
* w2
);
2501 memset (bits
, 0, height
* w2
);
2502 for (i
= 0; i
< height
; i
++)
2505 for (j
= 0; j
< w1
; j
++)
2507 /* Bitswap XBM bytes to match how Windows does things. */
2508 unsigned char c
= *data
++;
2509 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2510 | (swap_nibble
[(c
>>4) & 0xf]));
2513 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2519 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2520 COLORREF foreground
, COLORREF background
)
2522 HDC hdc
, old_img_dc
, new_img_dc
;
2523 HGDIOBJ old_prev
, new_prev
;
2526 hdc
= get_frame_dc (f
);
2527 old_img_dc
= CreateCompatibleDC (hdc
);
2528 new_img_dc
= CreateCompatibleDC (hdc
);
2529 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2530 release_frame_dc (f
, hdc
);
2531 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2532 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2533 /* Windows convention for mono bitmaps is black = background,
2534 white = foreground. */
2535 SetTextColor (new_img_dc
, background
);
2536 SetBkColor (new_img_dc
, foreground
);
2538 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2541 SelectObject (old_img_dc
, old_prev
);
2542 SelectObject (new_img_dc
, new_prev
);
2543 DeleteDC (old_img_dc
);
2544 DeleteDC (new_img_dc
);
2545 DeleteObject (img
->pixmap
);
2546 if (new_pixmap
== 0)
2547 fprintf (stderr
, "Failed to convert image to color.\n");
2549 img
->pixmap
= new_pixmap
;
2552 #define XBM_BIT_SHUFFLE(b) (~(b))
2556 #define XBM_BIT_SHUFFLE(b) (b)
2558 #endif /* HAVE_NTGUI */
2562 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2563 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2564 int non_default_colors
)
2568 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2570 /* If colors were specified, transfer the bitmap to a color one. */
2571 if (non_default_colors
)
2572 convert_mono_to_color_image (f
, img
, fg
, bg
);
2574 #elif defined (HAVE_NS)
2575 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2579 (x_check_image_size (0, img
->width
, img
->height
)
2580 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2583 img
->width
, img
->height
,
2585 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2587 #endif /* !HAVE_NTGUI && !HAVE_NS */
2592 /* Replacement for XReadBitmapFileData which isn't available under old
2593 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2594 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2595 the image. Return in *DATA the bitmap data allocated with xmalloc.
2596 Value is non-zero if successful. DATA null means just test if
2597 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2598 is non-zero, inhibit the call to image_error when the image size is
2599 invalid (the bitmap remains unread). */
2602 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2603 int *width
, int *height
, char **data
,
2604 int inhibit_image_error
)
2606 unsigned char *s
= contents
;
2607 char buffer
[BUFSIZ
];
2610 int bytes_per_line
, i
, nbytes
;
2616 LA1 = xbm_scan (&s, end, buffer, &value)
2618 #define expect(TOKEN) \
2619 if (LA1 != (TOKEN)) \
2624 #define expect_ident(IDENT) \
2625 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2630 *width
= *height
= -1;
2633 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2635 /* Parse defines for width, height and hot-spots. */
2639 expect_ident ("define");
2640 expect (XBM_TK_IDENT
);
2642 if (LA1
== XBM_TK_NUMBER
)
2644 char *q
= strrchr (buffer
, '_');
2645 q
= q
? q
+ 1 : buffer
;
2646 if (strcmp (q
, "width") == 0)
2648 else if (strcmp (q
, "height") == 0)
2651 expect (XBM_TK_NUMBER
);
2654 if (!check_image_size (f
, *width
, *height
))
2656 if (!inhibit_image_error
)
2657 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2660 else if (data
== NULL
)
2663 /* Parse bits. Must start with `static'. */
2664 expect_ident ("static");
2665 if (LA1
== XBM_TK_IDENT
)
2667 if (strcmp (buffer
, "unsigned") == 0)
2670 expect_ident ("char");
2672 else if (strcmp (buffer
, "short") == 0)
2676 if (*width
% 16 && *width
% 16 < 9)
2679 else if (strcmp (buffer
, "char") == 0)
2687 expect (XBM_TK_IDENT
);
2693 if (! x_check_image_size (0, *width
, *height
))
2695 if (!inhibit_image_error
)
2696 image_error ("Image too large (%dx%d)",
2697 make_number (*width
), make_number (*height
));
2700 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2701 nbytes
= bytes_per_line
* *height
;
2702 p
= *data
= xmalloc (nbytes
);
2706 for (i
= 0; i
< nbytes
; i
+= 2)
2709 expect (XBM_TK_NUMBER
);
2711 *p
++ = XBM_BIT_SHUFFLE (val
);
2712 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2713 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2715 if (LA1
== ',' || LA1
== '}')
2723 for (i
= 0; i
< nbytes
; ++i
)
2726 expect (XBM_TK_NUMBER
);
2728 *p
++ = XBM_BIT_SHUFFLE (val
);
2730 if (LA1
== ',' || LA1
== '}')
2755 /* Load XBM image IMG which will be displayed on frame F from buffer
2756 CONTENTS. END is the end of the buffer. Value is non-zero if
2760 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2767 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2771 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2772 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2773 int non_default_colors
= 0;
2776 eassert (img
->width
> 0 && img
->height
> 0);
2778 /* Get foreground and background colors, maybe allocate colors. */
2779 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2782 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2783 non_default_colors
= 1;
2785 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2788 background
= x_alloc_image_color (f
, img
, value
, background
);
2789 img
->background
= background
;
2790 img
->background_valid
= 1;
2791 non_default_colors
= 1;
2794 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2795 foreground
, background
,
2796 non_default_colors
);
2799 if (img
->pixmap
== NO_PIXMAP
)
2801 x_clear_image (f
, img
);
2802 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2808 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2814 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2817 xbm_file_p (Lisp_Object data
)
2820 return (STRINGP (data
)
2821 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2822 (SDATA (data
) + SBYTES (data
)),
2827 /* Fill image IMG which is used on frame F with pixmap data. Value is
2828 non-zero if successful. */
2831 xbm_load (struct frame
*f
, struct image
*img
)
2834 Lisp_Object file_name
;
2836 eassert (xbm_image_p (img
->spec
));
2838 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2839 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2840 if (STRINGP (file_name
))
2843 unsigned char *contents
;
2846 file
= x_find_image_file (file_name
);
2847 if (!STRINGP (file
))
2849 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2853 contents
= slurp_file (SSDATA (file
), &size
);
2854 if (contents
== NULL
)
2856 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2860 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2865 struct image_keyword fmt
[XBM_LAST
];
2867 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2868 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2869 int non_default_colors
= 0;
2872 int in_memory_file_p
= 0;
2874 /* See if data looks like an in-memory XBM file. */
2875 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2876 in_memory_file_p
= xbm_file_p (data
);
2878 /* Parse the image specification. */
2879 memcpy (fmt
, xbm_format
, sizeof fmt
);
2880 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2884 /* Get specified width, and height. */
2885 if (!in_memory_file_p
)
2887 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2888 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2889 eassert (img
->width
> 0 && img
->height
> 0);
2890 if (!check_image_size (f
, img
->width
, img
->height
))
2892 image_error ("Invalid image size (see `max-image-size')",
2898 /* Get foreground and background colors, maybe allocate colors. */
2899 if (fmt
[XBM_FOREGROUND
].count
2900 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2902 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2904 non_default_colors
= 1;
2907 if (fmt
[XBM_BACKGROUND
].count
2908 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2910 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2912 non_default_colors
= 1;
2915 if (in_memory_file_p
)
2916 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2925 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2927 p
= bits
= alloca (nbytes
* img
->height
);
2928 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2930 Lisp_Object line
= AREF (data
, i
);
2932 memcpy (p
, SDATA (line
), nbytes
);
2934 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2937 else if (STRINGP (data
))
2938 bits
= SSDATA (data
);
2940 bits
= (char *) XBOOL_VECTOR (data
)->data
;
2946 /* Windows mono bitmaps are reversed compared with X. */
2947 invertedBits
= bits
;
2948 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2950 bits
= alloca (nbytes
);
2951 for (i
= 0; i
< nbytes
; i
++)
2952 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2955 /* Create the pixmap. */
2957 if (x_check_image_size (0, img
->width
, img
->height
))
2958 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2959 foreground
, background
,
2960 non_default_colors
);
2962 img
->pixmap
= NO_PIXMAP
;
2968 image_error ("Unable to create pixmap for XBM image `%s'",
2970 x_clear_image (f
, img
);
2980 /***********************************************************************
2982 ***********************************************************************/
2984 #if defined (HAVE_XPM) || defined (HAVE_NS)
2986 static int xpm_image_p (Lisp_Object object
);
2987 static int xpm_load (struct frame
*f
, struct image
*img
);
2988 static int xpm_valid_color_symbols_p (Lisp_Object
);
2990 #endif /* HAVE_XPM || HAVE_NS */
2994 /* Indicate to xpm.h that we don't have Xlib. */
2996 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
2997 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
2998 #define XColor xpm_XColor
2999 #define XImage xpm_XImage
3000 #define Display xpm_Display
3001 #define PIXEL_ALREADY_TYPEDEFED
3002 #include "X11/xpm.h"
3007 #undef PIXEL_ALREADY_TYPEDEFED
3009 #include "X11/xpm.h"
3010 #endif /* HAVE_NTGUI */
3011 #endif /* HAVE_XPM */
3013 #if defined (HAVE_XPM) || defined (HAVE_NS)
3014 /* The symbol `xpm' identifying XPM-format images. */
3016 static Lisp_Object Qxpm
;
3018 /* Indices of image specification fields in xpm_format, below. */
3020 enum xpm_keyword_index
3036 /* Vector of image_keyword structures describing the format
3037 of valid XPM image specifications. */
3039 static const struct image_keyword xpm_format
[XPM_LAST
] =
3041 {":type", IMAGE_SYMBOL_VALUE
, 1},
3042 {":file", IMAGE_STRING_VALUE
, 0},
3043 {":data", IMAGE_STRING_VALUE
, 0},
3044 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3045 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
3046 {":relief", IMAGE_INTEGER_VALUE
, 0},
3047 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3048 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3049 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3050 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3051 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3054 /* Structure describing the image type XPM. */
3056 static struct image_type xpm_type
=
3065 #ifdef HAVE_X_WINDOWS
3067 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3068 functions for allocating image colors. Our own functions handle
3069 color allocation failures more gracefully than the ones on the XPM
3072 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3073 #define ALLOC_XPM_COLORS
3075 #endif /* HAVE_X_WINDOWS */
3077 #ifdef ALLOC_XPM_COLORS
3079 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3080 static void xpm_free_color_cache (void);
3081 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3082 static int xpm_color_bucket (char *);
3083 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3086 /* An entry in a hash table used to cache color definitions of named
3087 colors. This cache is necessary to speed up XPM image loading in
3088 case we do color allocations ourselves. Without it, we would need
3089 a call to XParseColor per pixel in the image. */
3091 struct xpm_cached_color
3093 /* Next in collision chain. */
3094 struct xpm_cached_color
*next
;
3096 /* Color definition (RGB and pixel color). */
3103 /* The hash table used for the color cache, and its bucket vector
3106 #define XPM_COLOR_CACHE_BUCKETS 1001
3107 static struct xpm_cached_color
**xpm_color_cache
;
3109 /* Initialize the color cache. */
3112 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3114 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3115 xpm_color_cache
= xzalloc (nbytes
);
3116 init_color_table ();
3118 if (attrs
->valuemask
& XpmColorSymbols
)
3123 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3124 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3125 attrs
->colorsymbols
[i
].value
, &color
))
3127 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3129 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3134 /* Free the color cache. */
3137 xpm_free_color_cache (void)
3139 struct xpm_cached_color
*p
, *next
;
3142 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3143 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3149 xfree (xpm_color_cache
);
3150 xpm_color_cache
= NULL
;
3151 free_color_table ();
3154 /* Return the bucket index for color named COLOR_NAME in the color
3158 xpm_color_bucket (char *color_name
)
3160 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3161 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3165 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3166 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3169 static struct xpm_cached_color
*
3170 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3173 struct xpm_cached_color
*p
;
3176 bucket
= xpm_color_bucket (color_name
);
3178 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3179 p
= xmalloc (nbytes
);
3180 strcpy (p
->name
, color_name
);
3182 p
->next
= xpm_color_cache
[bucket
];
3183 xpm_color_cache
[bucket
] = p
;
3187 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3188 return the cached definition in *COLOR. Otherwise, make a new
3189 entry in the cache and allocate the color. Value is zero if color
3190 allocation failed. */
3193 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3195 struct xpm_cached_color
*p
;
3196 int h
= xpm_color_bucket (color_name
);
3198 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3199 if (strcmp (p
->name
, color_name
) == 0)
3204 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3207 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3209 p
= xpm_cache_color (f
, color_name
, color
, h
);
3211 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3212 with transparency, and it's useful. */
3213 else if (strcmp ("opaque", color_name
) == 0)
3215 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3216 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3217 p
= xpm_cache_color (f
, color_name
, color
, h
);
3224 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3225 CLOSURE is a pointer to the frame on which we allocate the
3226 color. Return in *COLOR the allocated color. Value is non-zero
3230 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3233 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3237 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3238 is a pointer to the frame on which we allocate the color. Value is
3239 non-zero if successful. */
3242 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3247 #endif /* ALLOC_XPM_COLORS */
3252 /* XPM library details. */
3254 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3255 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3256 xpm_XImage
**, XpmAttributes
*));
3257 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3258 xpm_XImage
**, XpmAttributes
*));
3259 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3262 init_xpm_functions (Lisp_Object libraries
)
3266 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3269 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3270 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3271 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3272 LOAD_IMGLIB_FN (library
, XImageFree
);
3276 #endif /* HAVE_NTGUI */
3279 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3280 for XPM images. Such a list must consist of conses whose car and
3284 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3286 while (CONSP (color_symbols
))
3288 Lisp_Object sym
= XCAR (color_symbols
);
3290 || !STRINGP (XCAR (sym
))
3291 || !STRINGP (XCDR (sym
)))
3293 color_symbols
= XCDR (color_symbols
);
3296 return NILP (color_symbols
);
3300 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3303 xpm_image_p (Lisp_Object object
)
3305 struct image_keyword fmt
[XPM_LAST
];
3306 memcpy (fmt
, xpm_format
, sizeof fmt
);
3307 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3308 /* Either `:file' or `:data' must be present. */
3309 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3310 /* Either no `:color-symbols' or it's a list of conses
3311 whose car and cdr are strings. */
3312 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3313 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3316 #endif /* HAVE_XPM || HAVE_NS */
3318 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3320 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3322 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3325 XpmAttributes attrs
;
3326 Pixmap bitmap
, mask
;
3328 memset (&attrs
, 0, sizeof attrs
);
3330 attrs
.visual
= FRAME_X_VISUAL (f
);
3331 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3332 attrs
.valuemask
|= XpmVisual
;
3333 attrs
.valuemask
|= XpmColormap
;
3335 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3336 (char **) bits
, &bitmap
, &mask
, &attrs
);
3337 if (rc
!= XpmSuccess
)
3339 XpmFreeAttributes (&attrs
);
3343 id
= x_allocate_bitmap_record (f
);
3344 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3345 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3346 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3347 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3348 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3349 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3350 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3351 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3353 XpmFreeAttributes (&attrs
);
3356 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3358 /* Load image IMG which will be displayed on frame F. Value is
3359 non-zero if successful. */
3364 xpm_load (struct frame
*f
, struct image
*img
)
3367 XpmAttributes attrs
;
3368 Lisp_Object specified_file
, color_symbols
;
3371 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3372 #endif /* HAVE_NTGUI */
3374 /* Configure the XPM lib. Use the visual of frame F. Allocate
3375 close colors. Return colors allocated. */
3376 memset (&attrs
, 0, sizeof attrs
);
3379 attrs
.visual
= FRAME_X_VISUAL (f
);
3380 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3381 attrs
.valuemask
|= XpmVisual
;
3382 attrs
.valuemask
|= XpmColormap
;
3383 #endif /* HAVE_NTGUI */
3385 #ifdef ALLOC_XPM_COLORS
3386 /* Allocate colors with our own functions which handle
3387 failing color allocation more gracefully. */
3388 attrs
.color_closure
= f
;
3389 attrs
.alloc_color
= xpm_alloc_color
;
3390 attrs
.free_colors
= xpm_free_colors
;
3391 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3392 #else /* not ALLOC_XPM_COLORS */
3393 /* Let the XPM lib allocate colors. */
3394 attrs
.valuemask
|= XpmReturnAllocPixels
;
3395 #ifdef XpmAllocCloseColors
3396 attrs
.alloc_close_colors
= 1;
3397 attrs
.valuemask
|= XpmAllocCloseColors
;
3398 #else /* not XpmAllocCloseColors */
3399 attrs
.closeness
= 600;
3400 attrs
.valuemask
|= XpmCloseness
;
3401 #endif /* not XpmAllocCloseColors */
3402 #endif /* ALLOC_XPM_COLORS */
3404 /* If image specification contains symbolic color definitions, add
3405 these to `attrs'. */
3406 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3407 if (CONSP (color_symbols
))
3410 XpmColorSymbol
*xpm_syms
;
3413 attrs
.valuemask
|= XpmColorSymbols
;
3415 /* Count number of symbols. */
3416 attrs
.numsymbols
= 0;
3417 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3420 /* Allocate an XpmColorSymbol array. */
3421 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3422 xpm_syms
= alloca (size
);
3423 memset (xpm_syms
, 0, size
);
3424 attrs
.colorsymbols
= xpm_syms
;
3426 /* Fill the color symbol array. */
3427 for (tail
= color_symbols
, i
= 0;
3429 ++i
, tail
= XCDR (tail
))
3433 char *empty_string
= (char *) "";
3435 if (!CONSP (XCAR (tail
)))
3437 xpm_syms
[i
].name
= empty_string
;
3438 xpm_syms
[i
].value
= empty_string
;
3441 name
= XCAR (XCAR (tail
));
3442 color
= XCDR (XCAR (tail
));
3445 xpm_syms
[i
].name
= alloca (SCHARS (name
) + 1);
3446 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3449 xpm_syms
[i
].name
= empty_string
;
3450 if (STRINGP (color
))
3452 xpm_syms
[i
].value
= alloca (SCHARS (color
) + 1);
3453 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3456 xpm_syms
[i
].value
= empty_string
;
3460 /* Create a pixmap for the image, either from a file, or from a
3461 string buffer containing data in the same format as an XPM file. */
3462 #ifdef ALLOC_XPM_COLORS
3463 xpm_init_color_cache (f
, &attrs
);
3466 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3470 HDC frame_dc
= get_frame_dc (f
);
3471 hdc
= CreateCompatibleDC (frame_dc
);
3472 release_frame_dc (f
, frame_dc
);
3474 #endif /* HAVE_NTGUI */
3476 if (STRINGP (specified_file
))
3478 Lisp_Object file
= x_find_image_file (specified_file
);
3479 if (!STRINGP (file
))
3481 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3482 #ifdef ALLOC_XPM_COLORS
3483 xpm_free_color_cache ();
3489 /* XpmReadFileToPixmap is not available in the Windows port of
3490 libxpm. But XpmReadFileToImage almost does what we want. */
3491 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3492 &xpm_image
, &xpm_mask
,
3495 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3496 SSDATA (file
), &img
->pixmap
, &img
->mask
,
3498 #endif /* HAVE_NTGUI */
3502 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3503 if (!STRINGP (buffer
))
3505 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3506 #ifdef ALLOC_XPM_COLORS
3507 xpm_free_color_cache ();
3512 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3513 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3514 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3515 &xpm_image
, &xpm_mask
,
3518 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3520 &img
->pixmap
, &img
->mask
,
3522 #endif /* HAVE_NTGUI */
3525 if (rc
== XpmSuccess
)
3527 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3528 img
->colors
= colors_in_color_table (&img
->ncolors
);
3529 #else /* not ALLOC_XPM_COLORS */
3533 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3534 plus some duplicate attributes. */
3535 if (xpm_image
&& xpm_image
->bitmap
)
3537 img
->pixmap
= xpm_image
->bitmap
;
3538 /* XImageFree in libXpm frees XImage struct without destroying
3539 the bitmap, which is what we want. */
3540 fn_XImageFree (xpm_image
);
3542 if (xpm_mask
&& xpm_mask
->bitmap
)
3544 /* The mask appears to be inverted compared with what we expect.
3545 TODO: invert our expectations. See other places where we
3546 have to invert bits because our idea of masks is backwards. */
3548 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3550 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3551 SelectObject (hdc
, old_obj
);
3553 img
->mask
= xpm_mask
->bitmap
;
3554 fn_XImageFree (xpm_mask
);
3559 #endif /* HAVE_NTGUI */
3561 /* Remember allocated colors. */
3562 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3563 img
->ncolors
= attrs
.nalloc_pixels
;
3564 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3566 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3567 #ifdef DEBUG_X_COLORS
3568 register_color (img
->colors
[i
]);
3571 #endif /* not ALLOC_XPM_COLORS */
3573 img
->width
= attrs
.width
;
3574 img
->height
= attrs
.height
;
3575 eassert (img
->width
> 0 && img
->height
> 0);
3577 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3579 fn_XpmFreeAttributes (&attrs
);
3581 XpmFreeAttributes (&attrs
);
3582 #endif /* HAVE_NTGUI */
3588 #endif /* HAVE_NTGUI */
3593 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3596 case XpmFileInvalid
:
3597 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3601 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3604 case XpmColorFailed
:
3605 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3609 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3614 #ifdef ALLOC_XPM_COLORS
3615 xpm_free_color_cache ();
3617 return rc
== XpmSuccess
;
3620 #endif /* HAVE_XPM */
3622 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3624 /* XPM support functions for NS where libxpm is not available.
3625 Only XPM version 3 (without any extensions) is supported. */
3627 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3629 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3630 const unsigned char *, int);
3631 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3633 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3634 const unsigned char *, int);
3636 /* Tokens returned from xpm_scan. */
3645 /* Scan an XPM data and return a character (< 256) or a token defined
3646 by enum xpm_token above. *S and END are the start (inclusive) and
3647 the end (exclusive) addresses of the data, respectively. Advance
3648 *S while scanning. If token is either XPM_TK_IDENT or
3649 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3650 length of the corresponding token, respectively. */
3653 xpm_scan (const unsigned char **s
,
3654 const unsigned char *end
,
3655 const unsigned char **beg
,
3662 /* Skip white-space. */
3663 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
3666 /* gnus-pointer.xpm uses '-' in its identifier.
3667 sb-dir-plus.xpm uses '+' in its identifier. */
3668 if (c_isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3672 && (c
= **s
, c_isalnum (c
)
3673 || c
== '_' || c
== '-' || c
== '+'))
3676 return XPM_TK_IDENT
;
3681 while (*s
< end
&& **s
!= '"')
3686 return XPM_TK_STRING
;
3690 if (*s
< end
&& **s
== '*')
3692 /* C-style comment. */
3696 while (*s
< end
&& *(*s
)++ != '*')
3699 while (*s
< end
&& **s
!= '/');
3713 /* Functions for color table lookup in XPM data. A key is a string
3714 specifying the color of each pixel in XPM data. A value is either
3715 an integer that specifies a pixel color, Qt that specifies
3716 transparency, or Qnil for the unspecified color. If the length of
3717 the key string is one, a vector is used as a table. Otherwise, a
3718 hash table is used. */
3721 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3722 const unsigned char *,
3725 Lisp_Object (**get_func
) (Lisp_Object
,
3726 const unsigned char *,
3729 *put_func
= xpm_put_color_table_v
;
3730 *get_func
= xpm_get_color_table_v
;
3731 return Fmake_vector (make_number (256), Qnil
);
3735 xpm_put_color_table_v (Lisp_Object color_table
,
3736 const unsigned char *chars_start
,
3740 ASET (color_table
, *chars_start
, color
);
3744 xpm_get_color_table_v (Lisp_Object color_table
,
3745 const unsigned char *chars_start
,
3748 return AREF (color_table
, *chars_start
);
3752 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3753 const unsigned char *,
3756 Lisp_Object (**get_func
) (Lisp_Object
,
3757 const unsigned char *,
3760 *put_func
= xpm_put_color_table_h
;
3761 *get_func
= xpm_get_color_table_h
;
3762 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3763 make_float (DEFAULT_REHASH_SIZE
),
3764 make_float (DEFAULT_REHASH_THRESHOLD
),
3769 xpm_put_color_table_h (Lisp_Object color_table
,
3770 const unsigned char *chars_start
,
3774 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3775 EMACS_UINT hash_code
;
3776 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3778 hash_lookup (table
, chars
, &hash_code
);
3779 hash_put (table
, chars
, color
, hash_code
);
3783 xpm_get_color_table_h (Lisp_Object color_table
,
3784 const unsigned char *chars_start
,
3787 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3789 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3791 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3794 enum xpm_color_key
{
3802 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3805 xpm_str_to_color_key (const char *s
)
3810 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3812 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3818 xpm_load_image (struct frame
*f
,
3820 const unsigned char *contents
,
3821 const unsigned char *end
)
3823 const unsigned char *s
= contents
, *beg
, *str
;
3824 unsigned char buffer
[BUFSIZ
];
3825 int width
, height
, x
, y
;
3826 int num_colors
, chars_per_pixel
;
3829 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3830 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3831 Lisp_Object frame
, color_symbols
, color_table
;
3832 int best_key
, have_mask
= 0;
3833 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3836 LA1 = xpm_scan (&s, end, &beg, &len)
3838 #define expect(TOKEN) \
3839 if (LA1 != (TOKEN)) \
3844 #define expect_ident(IDENT) \
3845 if (LA1 == XPM_TK_IDENT \
3846 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3851 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3855 expect_ident ("static");
3856 expect_ident ("char");
3858 expect (XPM_TK_IDENT
);
3863 expect (XPM_TK_STRING
);
3866 memcpy (buffer
, beg
, len
);
3868 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3869 &num_colors
, &chars_per_pixel
) != 4
3870 || width
<= 0 || height
<= 0
3871 || num_colors
<= 0 || chars_per_pixel
<= 0)
3874 if (!check_image_size (f
, width
, height
))
3876 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3880 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3881 &ximg
, &img
->pixmap
)
3883 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3884 &mask_img
, &img
->mask
)
3888 image_error ("Image too large", Qnil
, Qnil
);
3894 XSETFRAME (frame
, f
);
3895 if (!NILP (Fxw_display_color_p (frame
)))
3896 best_key
= XPM_COLOR_KEY_C
;
3897 else if (!NILP (Fx_display_grayscale_p (frame
)))
3898 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3899 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3901 best_key
= XPM_COLOR_KEY_M
;
3903 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3904 if (chars_per_pixel
== 1)
3905 color_table
= xpm_make_color_table_v (&put_color_table
,
3908 color_table
= xpm_make_color_table_h (&put_color_table
,
3911 while (num_colors
-- > 0)
3913 char *color
, *max_color
;
3914 int key
, next_key
, max_key
= 0;
3915 Lisp_Object symbol_color
= Qnil
, color_val
;
3918 expect (XPM_TK_STRING
);
3919 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3921 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3922 buffer
[len
- chars_per_pixel
] = '\0';
3924 str
= strtok (buffer
, " \t");
3927 key
= xpm_str_to_color_key (str
);
3932 color
= strtok (NULL
, " \t");
3936 while ((str
= strtok (NULL
, " \t")) != NULL
)
3938 next_key
= xpm_str_to_color_key (str
);
3941 color
[strlen (color
)] = ' ';
3944 if (key
== XPM_COLOR_KEY_S
)
3946 if (NILP (symbol_color
))
3947 symbol_color
= build_string (color
);
3949 else if (max_key
< key
&& key
<= best_key
)
3959 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3961 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3963 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3965 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
3967 else if (x_defined_color (f
, SSDATA (XCDR (specified_color
)),
3969 color_val
= make_number (cdef
.pixel
);
3972 if (NILP (color_val
) && max_key
> 0)
3974 if (xstrcasecmp (max_color
, "None") == 0)
3976 else if (x_defined_color (f
, max_color
, &cdef
, 0))
3977 color_val
= make_number (cdef
.pixel
);
3979 if (!NILP (color_val
))
3980 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
3985 for (y
= 0; y
< height
; y
++)
3987 expect (XPM_TK_STRING
);
3989 if (len
< width
* chars_per_pixel
)
3991 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
3993 Lisp_Object color_val
=
3994 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
3996 XPutPixel (ximg
, x
, y
,
3997 (INTEGERP (color_val
) ? XINT (color_val
)
3998 : FRAME_FOREGROUND_PIXEL (f
)));
4000 XPutPixel (mask_img
, x
, y
,
4001 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4002 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4004 if (EQ (color_val
, Qt
))
4005 ns_set_alpha (ximg
, x
, y
, 0);
4013 img
->height
= height
;
4015 /* Maybe fill in the background field while we have ximg handy. */
4016 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4017 IMAGE_BACKGROUND (img
, f
, ximg
);
4019 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4020 x_destroy_x_image (ximg
);
4024 /* Fill in the background_transparent field while we have the
4026 image_background_transparent (img
, f
, mask_img
);
4028 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4029 x_destroy_x_image (mask_img
);
4033 x_destroy_x_image (mask_img
);
4034 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4035 img
->mask
= NO_PIXMAP
;
4041 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4042 x_destroy_x_image (ximg
);
4043 x_destroy_x_image (mask_img
);
4044 x_clear_image (f
, img
);
4053 xpm_load (struct frame
*f
,
4057 Lisp_Object file_name
;
4059 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4060 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4061 if (STRINGP (file_name
))
4064 unsigned char *contents
;
4067 file
= x_find_image_file (file_name
);
4068 if (!STRINGP (file
))
4070 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4074 contents
= slurp_file (SSDATA (file
), &size
);
4075 if (contents
== NULL
)
4077 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4081 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4088 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4089 if (!STRINGP (data
))
4091 image_error ("Invalid image data `%s'", data
, Qnil
);
4094 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4095 SDATA (data
) + SBYTES (data
));
4101 #endif /* HAVE_NS && !HAVE_XPM */
4105 /***********************************************************************
4107 ***********************************************************************/
4109 #ifdef COLOR_TABLE_SUPPORT
4111 /* An entry in the color table mapping an RGB color to a pixel color. */
4116 unsigned long pixel
;
4118 /* Next in color table collision list. */
4119 struct ct_color
*next
;
4122 /* The bucket vector size to use. Must be prime. */
4126 /* Value is a hash of the RGB color given by R, G, and B. */
4128 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4130 /* The color hash table. */
4132 static struct ct_color
**ct_table
;
4134 /* Number of entries in the color table. */
4136 static int ct_colors_allocated
;
4139 ct_colors_allocated_max
=
4141 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4144 /* Initialize the color table. */
4147 init_color_table (void)
4149 int size
= CT_SIZE
* sizeof (*ct_table
);
4150 ct_table
= xzalloc (size
);
4151 ct_colors_allocated
= 0;
4155 /* Free memory associated with the color table. */
4158 free_color_table (void)
4161 struct ct_color
*p
, *next
;
4163 for (i
= 0; i
< CT_SIZE
; ++i
)
4164 for (p
= ct_table
[i
]; p
; p
= next
)
4175 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4176 entry for that color already is in the color table, return the
4177 pixel color of that entry. Otherwise, allocate a new color for R,
4178 G, B, and make an entry in the color table. */
4180 static unsigned long
4181 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4183 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4184 int i
= hash
% CT_SIZE
;
4186 Display_Info
*dpyinfo
;
4188 /* Handle TrueColor visuals specially, which improves performance by
4189 two orders of magnitude. Freeing colors on TrueColor visuals is
4190 a nop, and pixel colors specify RGB values directly. See also
4191 the Xlib spec, chapter 3.1. */
4192 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4193 if (dpyinfo
->red_bits
> 0)
4195 unsigned long pr
, pg
, pb
;
4197 /* Apply gamma-correction like normal color allocation does. */
4201 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4202 gamma_correct (f
, &color
);
4203 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4206 /* Scale down RGB values to the visual's bits per RGB, and shift
4207 them to the right position in the pixel color. Note that the
4208 original RGB values are 16-bit values, as usual in X. */
4209 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4210 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4211 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4213 /* Assemble the pixel color. */
4214 return pr
| pg
| pb
;
4217 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4218 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4224 #ifdef HAVE_X_WINDOWS
4232 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4233 return FRAME_FOREGROUND_PIXEL (f
);
4235 #ifdef HAVE_X_WINDOWS
4240 cmap
= FRAME_X_COLORMAP (f
);
4241 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4244 ++ct_colors_allocated
;
4245 p
= xmalloc (sizeof *p
);
4249 p
->pixel
= color
.pixel
;
4250 p
->next
= ct_table
[i
];
4254 return FRAME_FOREGROUND_PIXEL (f
);
4258 color
= PALETTERGB (r
, g
, b
);
4260 color
= RGB_TO_ULONG (r
, g
, b
);
4261 #endif /* HAVE_NTGUI */
4262 ++ct_colors_allocated
;
4263 p
= xmalloc (sizeof *p
);
4268 p
->next
= ct_table
[i
];
4270 #endif /* HAVE_X_WINDOWS */
4278 /* Look up pixel color PIXEL which is used on frame F in the color
4279 table. If not already present, allocate it. Value is PIXEL. */
4281 static unsigned long
4282 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4284 int i
= pixel
% CT_SIZE
;
4287 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4288 if (p
->pixel
== pixel
)
4297 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4298 return FRAME_FOREGROUND_PIXEL (f
);
4300 #ifdef HAVE_X_WINDOWS
4301 cmap
= FRAME_X_COLORMAP (f
);
4302 color
.pixel
= pixel
;
4303 x_query_color (f
, &color
);
4304 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4307 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4308 color
.pixel
= pixel
;
4309 XQueryColor (NULL
, cmap
, &color
);
4310 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4312 #endif /* HAVE_X_WINDOWS */
4316 ++ct_colors_allocated
;
4318 p
= xmalloc (sizeof *p
);
4323 p
->next
= ct_table
[i
];
4327 return FRAME_FOREGROUND_PIXEL (f
);
4333 /* Value is a vector of all pixel colors contained in the color table,
4334 allocated via xmalloc. Set *N to the number of colors. */
4336 static unsigned long *
4337 colors_in_color_table (int *n
)
4341 unsigned long *colors
;
4343 if (ct_colors_allocated
== 0)
4350 colors
= xmalloc (ct_colors_allocated
* sizeof *colors
);
4351 *n
= ct_colors_allocated
;
4353 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4354 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4355 colors
[j
++] = p
->pixel
;
4361 #else /* COLOR_TABLE_SUPPORT */
4363 static unsigned long
4364 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4366 unsigned long pixel
;
4369 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4370 #endif /* HAVE_NTGUI */
4373 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4374 #endif /* HAVE_NS */
4379 init_color_table (void)
4382 #endif /* COLOR_TABLE_SUPPORT */
4385 /***********************************************************************
4387 ***********************************************************************/
4389 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4390 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4391 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4394 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4395 #endif /* HAVE_NTGUI */
4397 /* Edge detection matrices for different edge-detection
4400 static int emboss_matrix
[9] = {
4402 2, -1, 0, /* y - 1 */
4404 0, 1, -2 /* y + 1 */
4407 static int laplace_matrix
[9] = {
4409 1, 0, 0, /* y - 1 */
4411 0, 0, -1 /* y + 1 */
4414 /* Value is the intensity of the color whose red/green/blue values
4417 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4420 /* On frame F, return an array of XColor structures describing image
4421 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4422 non-zero means also fill the red/green/blue members of the XColor
4423 structures. Value is a pointer to the array of XColors structures,
4424 allocated with xmalloc; it must be freed by the caller. */
4427 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4431 XImagePtr_or_DC ximg
;
4435 #endif /* HAVE_NTGUI */
4437 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4438 memory_full (SIZE_MAX
);
4439 colors
= xmalloc (sizeof *colors
* img
->width
* img
->height
);
4442 /* Get the X image IMG->pixmap. */
4443 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4444 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4446 /* Load the image into a memory device context. */
4447 hdc
= get_frame_dc (f
);
4448 ximg
= CreateCompatibleDC (hdc
);
4449 release_frame_dc (f
, hdc
);
4450 prev
= SelectObject (ximg
, img
->pixmap
);
4451 #endif /* HAVE_NTGUI */
4453 /* Fill the `pixel' members of the XColor array. I wished there
4454 were an easy and portable way to circumvent XGetPixel. */
4456 for (y
= 0; y
< img
->height
; ++y
)
4458 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4460 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4461 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4463 x_query_colors (f
, row
, img
->width
);
4467 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4469 /* W32_TODO: palette support needed here? */
4470 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4473 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4474 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4475 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4478 #endif /* HAVE_X_WINDOWS */
4481 Destroy_Image (ximg
, prev
);
4488 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4489 created with CreateDIBSection, with the pointer to the bit values
4490 stored in ximg->data. */
4493 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4495 int width
= ximg
->info
.bmiHeader
.biWidth
;
4496 unsigned char * pixel
;
4498 /* True color images. */
4499 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4501 int rowbytes
= width
* 3;
4502 /* Ensure scanlines are aligned on 4 byte boundaries. */
4504 rowbytes
+= 4 - (rowbytes
% 4);
4506 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4507 /* Windows bitmaps are in BGR order. */
4508 *pixel
= GetBValue (color
);
4509 *(pixel
+ 1) = GetGValue (color
);
4510 *(pixel
+ 2) = GetRValue (color
);
4512 /* Monochrome images. */
4513 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4515 int rowbytes
= width
/ 8;
4516 /* Ensure scanlines are aligned on 4 byte boundaries. */
4518 rowbytes
+= 4 - (rowbytes
% 4);
4519 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4520 /* Filter out palette info. */
4521 if (color
& 0x00ffffff)
4522 *pixel
= *pixel
| (1 << x
% 8);
4524 *pixel
= *pixel
& ~(1 << x
% 8);
4527 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4530 #endif /* HAVE_NTGUI */
4532 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4533 RGB members are set. F is the frame on which this all happens.
4534 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4537 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4540 XImagePtr oimg
= NULL
;
4544 init_color_table ();
4546 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4549 for (y
= 0; y
< img
->height
; ++y
)
4550 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4552 unsigned long pixel
;
4553 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4554 XPutPixel (oimg
, x
, y
, pixel
);
4558 x_clear_image_1 (f
, img
, 1, 0, 1);
4560 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4561 x_destroy_x_image (oimg
);
4562 img
->pixmap
= pixmap
;
4563 #ifdef COLOR_TABLE_SUPPORT
4564 img
->colors
= colors_in_color_table (&img
->ncolors
);
4565 free_color_table ();
4566 #endif /* COLOR_TABLE_SUPPORT */
4570 /* On frame F, perform edge-detection on image IMG.
4572 MATRIX is a nine-element array specifying the transformation
4573 matrix. See emboss_matrix for an example.
4575 COLOR_ADJUST is a color adjustment added to each pixel of the
4579 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4581 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4585 for (i
= sum
= 0; i
< 9; ++i
)
4586 sum
+= eabs (matrix
[i
]);
4588 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4590 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4591 memory_full (SIZE_MAX
);
4592 new = xmalloc (sizeof *new * img
->width
* img
->height
);
4594 for (y
= 0; y
< img
->height
; ++y
)
4596 p
= COLOR (new, 0, y
);
4597 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4598 p
= COLOR (new, img
->width
- 1, y
);
4599 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4602 for (x
= 1; x
< img
->width
- 1; ++x
)
4604 p
= COLOR (new, x
, 0);
4605 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4606 p
= COLOR (new, x
, img
->height
- 1);
4607 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4610 for (y
= 1; y
< img
->height
- 1; ++y
)
4612 p
= COLOR (new, 1, y
);
4614 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4616 int r
, g
, b
, yy
, xx
;
4619 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4620 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4623 XColor
*t
= COLOR (colors
, xx
, yy
);
4624 r
+= matrix
[i
] * t
->red
;
4625 g
+= matrix
[i
] * t
->green
;
4626 b
+= matrix
[i
] * t
->blue
;
4629 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4630 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4631 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4632 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4637 x_from_xcolors (f
, img
, new);
4643 /* Perform the pre-defined `emboss' edge-detection on image IMG
4647 x_emboss (struct frame
*f
, struct image
*img
)
4649 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4653 /* Transform image IMG which is used on frame F with a Laplace
4654 edge-detection algorithm. The result is an image that can be used
4655 to draw disabled buttons, for example. */
4658 x_laplace (struct frame
*f
, struct image
*img
)
4660 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4664 /* Perform edge-detection on image IMG on frame F, with specified
4665 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4667 MATRIX must be either
4669 - a list of at least 9 numbers in row-major form
4670 - a vector of at least 9 numbers
4672 COLOR_ADJUST nil means use a default; otherwise it must be a
4676 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4677 Lisp_Object color_adjust
)
4685 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4686 ++i
, matrix
= XCDR (matrix
))
4687 trans
[i
] = XFLOATINT (XCAR (matrix
));
4689 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4691 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4692 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4695 if (NILP (color_adjust
))
4696 color_adjust
= make_number (0xffff / 2);
4698 if (i
== 9 && NUMBERP (color_adjust
))
4699 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4703 /* Transform image IMG on frame F so that it looks disabled. */
4706 x_disable_image (struct frame
*f
, struct image
*img
)
4708 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4710 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4712 int n_planes
= dpyinfo
->n_planes
;
4713 #endif /* HAVE_NTGUI */
4717 /* Color (or grayscale). Convert to gray, and equalize. Just
4718 drawing such images with a stipple can look very odd, so
4719 we're using this method instead. */
4720 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4722 const int h
= 15000;
4723 const int l
= 30000;
4725 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4729 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4730 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4731 p
->red
= p
->green
= p
->blue
= i2
;
4734 x_from_xcolors (f
, img
, colors
);
4737 /* Draw a cross over the disabled image, if we must or if we
4739 if (n_planes
< 2 || cross_disabled_images
)
4742 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4744 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4746 Display
*dpy
= FRAME_X_DISPLAY (f
);
4747 GC gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4748 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4749 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4750 img
->width
- 1, img
->height
- 1);
4751 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4757 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4758 XSetForeground (dpy
, gc
, MaskForeground (f
));
4759 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4760 img
->width
- 1, img
->height
- 1);
4761 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4765 #endif /* !HAVE_NS */
4770 hdc
= get_frame_dc (f
);
4771 bmpdc
= CreateCompatibleDC (hdc
);
4772 release_frame_dc (f
, hdc
);
4774 prev
= SelectObject (bmpdc
, img
->pixmap
);
4776 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4777 MoveToEx (bmpdc
, 0, 0, NULL
);
4778 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4779 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4780 LineTo (bmpdc
, img
->width
- 1, 0);
4784 SelectObject (bmpdc
, img
->mask
);
4785 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4786 MoveToEx (bmpdc
, 0, 0, NULL
);
4787 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4788 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4789 LineTo (bmpdc
, img
->width
- 1, 0);
4791 SelectObject (bmpdc
, prev
);
4793 #endif /* HAVE_NTGUI */
4798 /* Build a mask for image IMG which is used on frame F. FILE is the
4799 name of an image file, for error messages. HOW determines how to
4800 determine the background color of IMG. If it is a list '(R G B)',
4801 with R, G, and B being integers >= 0, take that as the color of the
4802 background. Otherwise, determine the background color of IMG
4803 heuristically. Value is non-zero if successful. */
4806 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4808 XImagePtr_or_DC ximg
;
4816 #endif /* HAVE_NTGUI */
4817 int x
, y
, rc
, use_img_background
;
4818 unsigned long bg
= 0;
4822 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4823 img
->mask
= NO_PIXMAP
;
4824 img
->background_transparent_valid
= 0;
4829 /* Create an image and pixmap serving as mask. */
4830 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4831 &mask_img
, &img
->mask
);
4834 #endif /* !HAVE_NS */
4836 /* Get the X image of IMG->pixmap. */
4837 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4838 img
->width
, img
->height
,
4841 /* Create the bit array serving as mask. */
4842 row_width
= (img
->width
+ 7) / 8;
4843 mask_img
= xzalloc (row_width
* img
->height
);
4845 /* Create a memory device context for IMG->pixmap. */
4846 frame_dc
= get_frame_dc (f
);
4847 ximg
= CreateCompatibleDC (frame_dc
);
4848 release_frame_dc (f
, frame_dc
);
4849 prev
= SelectObject (ximg
, img
->pixmap
);
4850 #endif /* HAVE_NTGUI */
4852 /* Determine the background color of ximg. If HOW is `(R G B)'
4853 take that as color. Otherwise, use the image's background color. */
4854 use_img_background
= 1;
4860 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4862 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4866 if (i
== 3 && NILP (how
))
4868 char color_name
[30];
4869 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4872 0x00ffffff & /* Filter out palette info. */
4873 #endif /* HAVE_NTGUI */
4874 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4875 use_img_background
= 0;
4879 if (use_img_background
)
4880 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4882 /* Set all bits in mask_img to 1 whose color in ximg is different
4883 from the background color bg. */
4885 for (y
= 0; y
< img
->height
; ++y
)
4886 for (x
= 0; x
< img
->width
; ++x
)
4888 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4889 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4891 if (XGetPixel (ximg
, x
, y
) == bg
)
4892 ns_set_alpha (ximg
, x
, y
, 0);
4893 #endif /* HAVE_NS */
4895 /* Fill in the background_transparent field while we have the mask handy. */
4896 image_background_transparent (img
, f
, mask_img
);
4898 /* Put mask_img into img->mask. */
4899 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4900 x_destroy_x_image (mask_img
);
4901 #endif /* !HAVE_NS */
4903 for (y
= 0; y
< img
->height
; ++y
)
4904 for (x
= 0; x
< img
->width
; ++x
)
4906 COLORREF p
= GetPixel (ximg
, x
, y
);
4908 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4911 /* Create the mask image. */
4912 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4914 /* Fill in the background_transparent field while we have the mask handy. */
4915 SelectObject (ximg
, img
->mask
);
4916 image_background_transparent (img
, f
, ximg
);
4918 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4920 #endif /* HAVE_NTGUI */
4922 Destroy_Image (ximg
, prev
);
4928 /***********************************************************************
4929 PBM (mono, gray, color)
4930 ***********************************************************************/
4932 static int pbm_image_p (Lisp_Object object
);
4933 static int pbm_load (struct frame
*f
, struct image
*img
);
4934 static int pbm_scan_number (unsigned char **, unsigned char *);
4936 /* The symbol `pbm' identifying images of this type. */
4938 static Lisp_Object Qpbm
;
4940 /* Indices of image specification fields in gs_format, below. */
4942 enum pbm_keyword_index
4958 /* Vector of image_keyword structures describing the format
4959 of valid user-defined image specifications. */
4961 static const struct image_keyword pbm_format
[PBM_LAST
] =
4963 {":type", IMAGE_SYMBOL_VALUE
, 1},
4964 {":file", IMAGE_STRING_VALUE
, 0},
4965 {":data", IMAGE_STRING_VALUE
, 0},
4966 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4967 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
4968 {":relief", IMAGE_INTEGER_VALUE
, 0},
4969 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4970 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4971 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4972 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4973 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4976 /* Structure describing the image type `pbm'. */
4978 static struct image_type pbm_type
=
4988 /* Return non-zero if OBJECT is a valid PBM image specification. */
4991 pbm_image_p (Lisp_Object object
)
4993 struct image_keyword fmt
[PBM_LAST
];
4995 memcpy (fmt
, pbm_format
, sizeof fmt
);
4997 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
5000 /* Must specify either :data or :file. */
5001 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5005 /* Scan a decimal number from *S and return it. Advance *S while
5006 reading the number. END is the end of the string. Value is -1 at
5010 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5012 int c
= 0, val
= -1;
5016 /* Skip white-space. */
5017 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
5022 /* Skip comment to end of line. */
5023 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5026 else if (c_isdigit (c
))
5028 /* Read decimal number. */
5030 while (*s
< end
&& (c
= *(*s
)++, c_isdigit (c
)))
5031 val
= 10 * val
+ c
- '0';
5043 #if 0 /* Unused. ++kfs */
5045 /* Read FILE into memory. Value is a pointer to a buffer allocated
5046 with xmalloc holding FILE's contents. Value is null if an error
5047 occurred. *SIZE is set to the size of the file. */
5050 pbm_read_file (Lisp_Object file
, int *size
)
5056 if (stat (SDATA (file
), &st
) == 0
5057 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5058 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
5059 && (buf
= xmalloc (st
.st_size
),
5060 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5079 #endif /* HAVE_NTGUI */
5081 /* Load PBM image IMG for use on frame F. */
5084 pbm_load (struct frame
*f
, struct image
*img
)
5087 int width
, height
, max_color_idx
= 0;
5089 Lisp_Object file
, specified_file
;
5090 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5091 unsigned char *contents
= NULL
;
5092 unsigned char *end
, *p
;
5095 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5097 if (STRINGP (specified_file
))
5099 file
= x_find_image_file (specified_file
);
5100 if (!STRINGP (file
))
5102 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5106 contents
= slurp_file (SSDATA (file
), &size
);
5107 if (contents
== NULL
)
5109 image_error ("Error reading `%s'", file
, Qnil
);
5114 end
= contents
+ size
;
5119 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5120 if (!STRINGP (data
))
5122 image_error ("Invalid image data `%s'", data
, Qnil
);
5126 end
= p
+ SBYTES (data
);
5129 /* Check magic number. */
5130 if (end
- p
< 2 || *p
++ != 'P')
5132 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5141 raw_p
= 0, type
= PBM_MONO
;
5145 raw_p
= 0, type
= PBM_GRAY
;
5149 raw_p
= 0, type
= PBM_COLOR
;
5153 raw_p
= 1, type
= PBM_MONO
;
5157 raw_p
= 1, type
= PBM_GRAY
;
5161 raw_p
= 1, type
= PBM_COLOR
;
5165 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5169 /* Read width, height, maximum color-component. Characters
5170 starting with `#' up to the end of a line are ignored. */
5171 width
= pbm_scan_number (&p
, end
);
5172 height
= pbm_scan_number (&p
, end
);
5174 if (type
!= PBM_MONO
)
5176 max_color_idx
= pbm_scan_number (&p
, end
);
5177 if (max_color_idx
> 65535 || max_color_idx
< 0)
5179 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5184 if (!check_image_size (f
, width
, height
))
5186 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5190 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5191 &ximg
, &img
->pixmap
))
5194 /* Initialize the color hash table. */
5195 init_color_table ();
5197 if (type
== PBM_MONO
)
5200 struct image_keyword fmt
[PBM_LAST
];
5201 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5202 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5204 /* Parse the image specification. */
5205 memcpy (fmt
, pbm_format
, sizeof fmt
);
5206 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5208 /* Get foreground and background colors, maybe allocate colors. */
5209 if (fmt
[PBM_FOREGROUND
].count
5210 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5211 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5212 if (fmt
[PBM_BACKGROUND
].count
5213 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5215 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5216 img
->background
= bg
;
5217 img
->background_valid
= 1;
5220 for (y
= 0; y
< height
; ++y
)
5221 for (x
= 0; x
< width
; ++x
)
5229 x_destroy_x_image (ximg
);
5230 x_clear_image (f
, img
);
5231 image_error ("Invalid image size in image `%s'",
5241 g
= pbm_scan_number (&p
, end
);
5243 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5248 int expected_size
= height
* width
;
5249 if (max_color_idx
> 255)
5251 if (type
== PBM_COLOR
)
5254 if (raw_p
&& p
+ expected_size
> end
)
5256 x_destroy_x_image (ximg
);
5257 x_clear_image (f
, img
);
5258 image_error ("Invalid image size in image `%s'",
5263 for (y
= 0; y
< height
; ++y
)
5264 for (x
= 0; x
< width
; ++x
)
5268 if (type
== PBM_GRAY
&& raw_p
)
5271 if (max_color_idx
> 255)
5272 r
= g
= b
= r
* 256 + *p
++;
5274 else if (type
== PBM_GRAY
)
5275 r
= g
= b
= pbm_scan_number (&p
, end
);
5279 if (max_color_idx
> 255)
5282 if (max_color_idx
> 255)
5285 if (max_color_idx
> 255)
5290 r
= pbm_scan_number (&p
, end
);
5291 g
= pbm_scan_number (&p
, end
);
5292 b
= pbm_scan_number (&p
, end
);
5295 if (r
< 0 || g
< 0 || b
< 0)
5297 x_destroy_x_image (ximg
);
5298 image_error ("Invalid pixel value in image `%s'",
5303 /* RGB values are now in the range 0..max_color_idx.
5304 Scale this to the range 0..0xffff supported by X. */
5305 r
= (double) r
* 65535 / max_color_idx
;
5306 g
= (double) g
* 65535 / max_color_idx
;
5307 b
= (double) b
* 65535 / max_color_idx
;
5308 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5312 #ifdef COLOR_TABLE_SUPPORT
5313 /* Store in IMG->colors the colors allocated for the image, and
5314 free the color table. */
5315 img
->colors
= colors_in_color_table (&img
->ncolors
);
5316 free_color_table ();
5317 #endif /* COLOR_TABLE_SUPPORT */
5320 img
->height
= height
;
5322 /* Maybe fill in the background field while we have ximg handy. */
5324 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5325 /* Casting avoids a GCC warning. */
5326 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5328 /* Put the image into a pixmap. */
5329 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5330 x_destroy_x_image (ximg
);
5332 /* X and W32 versions did it here, MAC version above. ++kfs
5334 img->height = height; */
5341 /***********************************************************************
5343 ***********************************************************************/
5345 #if defined (HAVE_PNG) || defined (HAVE_NS)
5347 /* Function prototypes. */
5349 static int png_image_p (Lisp_Object object
);
5350 static int png_load (struct frame
*f
, struct image
*img
);
5352 /* The symbol `png' identifying images of this type. */
5354 static Lisp_Object Qpng
;
5356 /* Indices of image specification fields in png_format, below. */
5358 enum png_keyword_index
5373 /* Vector of image_keyword structures describing the format
5374 of valid user-defined image specifications. */
5376 static const struct image_keyword png_format
[PNG_LAST
] =
5378 {":type", IMAGE_SYMBOL_VALUE
, 1},
5379 {":data", IMAGE_STRING_VALUE
, 0},
5380 {":file", IMAGE_STRING_VALUE
, 0},
5381 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5382 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5383 {":relief", IMAGE_INTEGER_VALUE
, 0},
5384 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5385 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5386 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5387 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5390 /* Structure describing the image type `png'. */
5392 static struct image_type png_type
=
5401 /* Return non-zero if OBJECT is a valid PNG image specification. */
5404 png_image_p (Lisp_Object object
)
5406 struct image_keyword fmt
[PNG_LAST
];
5407 memcpy (fmt
, png_format
, sizeof fmt
);
5409 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5412 /* Must specify either the :data or :file keyword. */
5413 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5416 #endif /* HAVE_PNG || HAVE_NS */
5422 /* PNG library details. */
5424 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5425 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5426 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5427 png_error_ptr
, png_error_ptr
));
5428 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5429 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5430 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5431 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5432 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5433 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5434 png_uint_32
*, png_uint_32
*,
5435 int *, int *, int *, int *, int *));
5436 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5437 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5438 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5439 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5440 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5442 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5443 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5444 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5445 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5446 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5447 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5448 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5450 #if (PNG_LIBPNG_VER >= 10500)
5451 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5452 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5453 #endif /* libpng version >= 1.5 */
5456 init_png_functions (Lisp_Object libraries
)
5460 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5463 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5464 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5465 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5466 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5467 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5468 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5469 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5470 LOAD_IMGLIB_FN (library
, png_read_info
);
5471 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5472 LOAD_IMGLIB_FN (library
, png_get_valid
);
5473 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5474 LOAD_IMGLIB_FN (library
, png_set_expand
);
5475 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5476 LOAD_IMGLIB_FN (library
, png_set_background
);
5477 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5478 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5479 LOAD_IMGLIB_FN (library
, png_get_channels
);
5480 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5481 LOAD_IMGLIB_FN (library
, png_read_image
);
5482 LOAD_IMGLIB_FN (library
, png_read_end
);
5483 LOAD_IMGLIB_FN (library
, png_error
);
5485 #if (PNG_LIBPNG_VER >= 10500)
5486 LOAD_IMGLIB_FN (library
, png_longjmp
);
5487 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5488 #endif /* libpng version >= 1.5 */
5494 #define fn_png_get_io_ptr png_get_io_ptr
5495 #define fn_png_sig_cmp png_sig_cmp
5496 #define fn_png_create_read_struct png_create_read_struct
5497 #define fn_png_create_info_struct png_create_info_struct
5498 #define fn_png_destroy_read_struct png_destroy_read_struct
5499 #define fn_png_set_read_fn png_set_read_fn
5500 #define fn_png_set_sig_bytes png_set_sig_bytes
5501 #define fn_png_read_info png_read_info
5502 #define fn_png_get_IHDR png_get_IHDR
5503 #define fn_png_get_valid png_get_valid
5504 #define fn_png_set_strip_16 png_set_strip_16
5505 #define fn_png_set_expand png_set_expand
5506 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5507 #define fn_png_set_background png_set_background
5508 #define fn_png_get_bKGD png_get_bKGD
5509 #define fn_png_read_update_info png_read_update_info
5510 #define fn_png_get_channels png_get_channels
5511 #define fn_png_get_rowbytes png_get_rowbytes
5512 #define fn_png_read_image png_read_image
5513 #define fn_png_read_end png_read_end
5514 #define fn_png_error png_error
5516 #if (PNG_LIBPNG_VER >= 10500)
5517 #define fn_png_longjmp png_longjmp
5518 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5519 #endif /* libpng version >= 1.5 */
5521 #endif /* HAVE_NTGUI */
5523 /* Possibly inefficient/inexact substitutes for _setjmp and _longjmp.
5524 Do not use sys_setjmp, as PNG supports only jmp_buf. The _longjmp
5525 substitute may munge the signal mask, but that should be OK here.
5526 MinGW (MS-Windows) uses _setjmp and defines setjmp to _setjmp in
5527 the system header setjmp.h; don't mess up that. */
5528 #ifndef HAVE__SETJMP
5529 # define _setjmp(j) setjmp (j)
5530 # define _longjmp longjmp
5533 #if (PNG_LIBPNG_VER < 10500)
5534 #define PNG_LONGJMP(ptr) (_longjmp ((ptr)->jmpbuf, 1))
5535 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5537 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5538 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5539 #define PNG_JMPBUF(ptr) \
5540 (*fn_png_set_longjmp_fn ((ptr), _longjmp, sizeof (jmp_buf)))
5543 /* Error and warning handlers installed when the PNG library
5546 static _Noreturn
void
5547 my_png_error (png_struct
*png_ptr
, const char *msg
)
5549 eassert (png_ptr
!= NULL
);
5550 /* Avoid compiler warning about deprecated direct access to
5551 png_ptr's fields in libpng versions 1.4.x. */
5552 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5553 PNG_LONGJMP (png_ptr
);
5558 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5560 eassert (png_ptr
!= NULL
);
5561 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5564 /* Memory source for PNG decoding. */
5566 struct png_memory_storage
5568 unsigned char *bytes
; /* The data */
5569 ptrdiff_t len
; /* How big is it? */
5570 ptrdiff_t index
; /* Where are we? */
5574 /* Function set as reader function when reading PNG image from memory.
5575 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5576 bytes from the input to DATA. */
5579 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5581 struct png_memory_storage
*tbr
5582 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5584 if (length
> tbr
->len
- tbr
->index
)
5585 fn_png_error (png_ptr
, "Read error");
5587 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5588 tbr
->index
= tbr
->index
+ length
;
5592 /* Function set as reader function when reading PNG image from a file.
5593 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5594 bytes from the input to DATA. */
5597 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5599 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5601 if (fread (data
, 1, length
, fp
) < length
)
5602 fn_png_error (png_ptr
, "Read error");
5606 /* Load PNG image IMG for use on frame F. Value is non-zero if
5609 struct png_load_context
5611 /* These are members so that longjmp doesn't munge local variables. */
5612 png_struct
*png_ptr
;
5621 png_load_body (struct frame
*f
, struct image
*img
, struct png_load_context
*c
)
5623 Lisp_Object file
, specified_file
;
5624 Lisp_Object specified_data
;
5627 XImagePtr ximg
, mask_img
= NULL
;
5628 png_struct
*png_ptr
;
5629 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5632 png_byte
*pixels
= NULL
;
5633 png_byte
**rows
= NULL
;
5634 png_uint_32 width
, height
;
5635 int bit_depth
, color_type
, interlace_type
;
5637 png_uint_32 row_bytes
;
5639 struct png_memory_storage tbr
; /* Data to be read */
5641 /* Find out what file to load. */
5642 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5643 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5645 if (NILP (specified_data
))
5647 file
= x_find_image_file (specified_file
);
5648 if (!STRINGP (file
))
5650 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5654 /* Open the image file. */
5655 fp
= fopen (SSDATA (file
), "rb");
5658 image_error ("Cannot open image file `%s'", file
, Qnil
);
5662 /* Check PNG signature. */
5663 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5664 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5666 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5673 if (!STRINGP (specified_data
))
5675 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5679 /* Read from memory. */
5680 tbr
.bytes
= SDATA (specified_data
);
5681 tbr
.len
= SBYTES (specified_data
);
5684 /* Check PNG signature. */
5685 if (tbr
.len
< sizeof sig
5686 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5688 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5692 /* Need to skip past the signature. */
5693 tbr
.bytes
+= sizeof (sig
);
5696 /* Initialize read and info structs for PNG lib. */
5697 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5702 info_ptr
= fn_png_create_info_struct (png_ptr
);
5703 end_info
= fn_png_create_info_struct (png_ptr
);
5706 c
->png_ptr
= png_ptr
;
5707 c
->info_ptr
= info_ptr
;
5708 c
->end_info
= end_info
;
5713 if (! (info_ptr
&& end_info
))
5715 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5720 if (fp
) fclose (fp
);
5724 /* Set error jump-back. We come back here when the PNG library
5725 detects an error. */
5726 if (_setjmp (PNG_JMPBUF (png_ptr
)))
5730 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5738 /* Silence a bogus diagnostic; see GCC bug 54561. */
5739 IF_LINT (fp
= c
->fp
);
5741 /* Read image info. */
5742 if (!NILP (specified_data
))
5743 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5745 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5747 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5748 fn_png_read_info (png_ptr
, info_ptr
);
5749 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5750 &interlace_type
, NULL
, NULL
);
5752 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5753 && check_image_size (f
, width
, height
)))
5755 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5759 /* Create the X image and pixmap now, so that the work below can be
5760 omitted if the image is too large for X. */
5761 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5765 /* If image contains simply transparency data, we prefer to
5766 construct a clipping mask. */
5767 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5772 /* This function is easier to write if we only have to handle
5773 one data format: RGB or RGBA with 8 bits per channel. Let's
5774 transform other formats into that format. */
5776 /* Strip more than 8 bits per channel. */
5777 if (bit_depth
== 16)
5778 fn_png_set_strip_16 (png_ptr
);
5780 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5782 fn_png_set_expand (png_ptr
);
5784 /* Convert grayscale images to RGB. */
5785 if (color_type
== PNG_COLOR_TYPE_GRAY
5786 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5787 fn_png_set_gray_to_rgb (png_ptr
);
5789 /* Handle alpha channel by combining the image with a background
5790 color. Do this only if a real alpha channel is supplied. For
5791 simple transparency, we prefer a clipping mask. */
5794 /* png_color_16 *image_bg; */
5795 Lisp_Object specified_bg
5796 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5797 int shift
= (bit_depth
== 16) ? 0 : 8;
5799 if (STRINGP (specified_bg
))
5800 /* The user specified `:background', use that. */
5803 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5805 png_color_16 user_bg
;
5807 memset (&user_bg
, 0, sizeof user_bg
);
5808 user_bg
.red
= color
.red
>> shift
;
5809 user_bg
.green
= color
.green
>> shift
;
5810 user_bg
.blue
= color
.blue
>> shift
;
5812 fn_png_set_background (png_ptr
, &user_bg
,
5813 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5818 /* We use the current frame background, ignoring any default
5819 background color set by the image. */
5820 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5822 png_color_16 frame_background
;
5824 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5825 x_query_color (f
, &color
);
5827 memset (&frame_background
, 0, sizeof frame_background
);
5828 frame_background
.red
= color
.red
>> shift
;
5829 frame_background
.green
= color
.green
>> shift
;
5830 frame_background
.blue
= color
.blue
>> shift
;
5831 #endif /* HAVE_X_WINDOWS */
5833 fn_png_set_background (png_ptr
, &frame_background
,
5834 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5838 /* Update info structure. */
5839 fn_png_read_update_info (png_ptr
, info_ptr
);
5841 /* Get number of channels. Valid values are 1 for grayscale images
5842 and images with a palette, 2 for grayscale images with transparency
5843 information (alpha channel), 3 for RGB images, and 4 for RGB
5844 images with alpha channel, i.e. RGBA. If conversions above were
5845 sufficient we should only have 3 or 4 channels here. */
5846 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5847 eassert (channels
== 3 || channels
== 4);
5849 /* Number of bytes needed for one row of the image. */
5850 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5852 /* Allocate memory for the image. */
5853 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5854 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5855 memory_full (SIZE_MAX
);
5856 c
->pixels
= pixels
= xmalloc (sizeof *pixels
* row_bytes
* height
);
5857 c
->rows
= rows
= xmalloc (height
* sizeof *rows
);
5858 for (i
= 0; i
< height
; ++i
)
5859 rows
[i
] = pixels
+ i
* row_bytes
;
5861 /* Read the entire image. */
5862 fn_png_read_image (png_ptr
, rows
);
5863 fn_png_read_end (png_ptr
, info_ptr
);
5870 /* Create an image and pixmap serving as mask if the PNG image
5871 contains an alpha channel. */
5874 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5875 &mask_img
, &img
->mask
))
5877 x_destroy_x_image (ximg
);
5878 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5879 img
->pixmap
= NO_PIXMAP
;
5883 /* Fill the X image and mask from PNG data. */
5884 init_color_table ();
5886 for (y
= 0; y
< height
; ++y
)
5888 png_byte
*p
= rows
[y
];
5890 for (x
= 0; x
< width
; ++x
)
5897 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5898 /* An alpha channel, aka mask channel, associates variable
5899 transparency with an image. Where other image formats
5900 support binary transparency---fully transparent or fully
5901 opaque---PNG allows up to 254 levels of partial transparency.
5902 The PNG library implements partial transparency by combining
5903 the image with a specified background color.
5905 I'm not sure how to handle this here nicely: because the
5906 background on which the image is displayed may change, for
5907 real alpha channel support, it would be necessary to create
5908 a new image for each possible background.
5910 What I'm doing now is that a mask is created if we have
5911 boolean transparency information. Otherwise I'm using
5912 the frame's background color to combine the image with. */
5917 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5923 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5924 /* Set IMG's background color from the PNG image, unless the user
5928 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5930 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5931 img
->background_valid
= 1;
5935 #ifdef COLOR_TABLE_SUPPORT
5936 /* Remember colors allocated for this image. */
5937 img
->colors
= colors_in_color_table (&img
->ncolors
);
5938 free_color_table ();
5939 #endif /* COLOR_TABLE_SUPPORT */
5942 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5947 img
->height
= height
;
5949 /* Maybe fill in the background field while we have ximg handy.
5950 Casting avoids a GCC warning. */
5951 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5953 /* Put the image into the pixmap, then free the X image and its buffer. */
5954 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5955 x_destroy_x_image (ximg
);
5957 /* Same for the mask. */
5960 /* Fill in the background_transparent field while we have the
5961 mask handy. Casting avoids a GCC warning. */
5962 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5964 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5965 x_destroy_x_image (mask_img
);
5972 png_load (struct frame
*f
, struct image
*img
)
5974 struct png_load_context c
;
5975 return png_load_body (f
, img
, &c
);
5978 #else /* HAVE_PNG */
5982 png_load (struct frame
*f
, struct image
*img
)
5984 return ns_load_image (f
, img
,
5985 image_spec_value (img
->spec
, QCfile
, NULL
),
5986 image_spec_value (img
->spec
, QCdata
, NULL
));
5988 #endif /* HAVE_NS */
5991 #endif /* !HAVE_PNG */
5995 /***********************************************************************
5997 ***********************************************************************/
5999 #if defined (HAVE_JPEG) || defined (HAVE_NS)
6001 static int jpeg_image_p (Lisp_Object object
);
6002 static int jpeg_load (struct frame
*f
, struct image
*img
);
6004 /* The symbol `jpeg' identifying images of this type. */
6006 static Lisp_Object Qjpeg
;
6008 /* Indices of image specification fields in gs_format, below. */
6010 enum jpeg_keyword_index
6019 JPEG_HEURISTIC_MASK
,
6025 /* Vector of image_keyword structures describing the format
6026 of valid user-defined image specifications. */
6028 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
6030 {":type", IMAGE_SYMBOL_VALUE
, 1},
6031 {":data", IMAGE_STRING_VALUE
, 0},
6032 {":file", IMAGE_STRING_VALUE
, 0},
6033 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6034 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6035 {":relief", IMAGE_INTEGER_VALUE
, 0},
6036 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6037 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6038 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6039 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6042 /* Structure describing the image type `jpeg'. */
6044 static struct image_type jpeg_type
=
6053 /* Return non-zero if OBJECT is a valid JPEG image specification. */
6056 jpeg_image_p (Lisp_Object object
)
6058 struct image_keyword fmt
[JPEG_LAST
];
6060 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6062 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6065 /* Must specify either the :data or :file keyword. */
6066 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6069 #endif /* HAVE_JPEG || HAVE_NS */
6073 /* Work around a warning about HAVE_STDLIB_H being redefined in
6075 #ifdef HAVE_STDLIB_H
6076 #undef HAVE_STDLIB_H
6077 #endif /* HAVE_STLIB_H */
6079 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6080 /* In older releases of the jpeg library, jpeglib.h will define boolean
6081 differently depending on __WIN32__, so make sure it is defined. */
6085 #include <jpeglib.h>
6088 #ifdef HAVE_STLIB_H_1
6089 #define HAVE_STDLIB_H 1
6094 /* JPEG library details. */
6095 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6096 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6097 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6098 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6099 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6100 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6101 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6102 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6105 init_jpeg_functions (Lisp_Object libraries
)
6109 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6112 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6113 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6114 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6115 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6116 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6117 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6118 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6119 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6123 /* Wrapper since we can't directly assign the function pointer
6124 to another function pointer that was declared more completely easily. */
6126 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6128 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6133 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6134 #define fn_jpeg_start_decompress jpeg_start_decompress
6135 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6136 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6137 #define fn_jpeg_read_header jpeg_read_header
6138 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6139 #define fn_jpeg_std_error jpeg_std_error
6140 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6142 #endif /* HAVE_NTGUI */
6144 struct my_jpeg_error_mgr
6146 struct jpeg_error_mgr pub
;
6147 sys_jmp_buf setjmp_buffer
;
6149 /* The remaining members are so that longjmp doesn't munge local
6151 struct jpeg_decompress_struct cinfo
;
6155 MY_JPEG_INVALID_IMAGE_SIZE
,
6156 MY_JPEG_CANNOT_CREATE_X
6164 static _Noreturn
void
6165 my_error_exit (j_common_ptr cinfo
)
6167 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6168 mgr
->failure_code
= MY_JPEG_ERROR_EXIT
;
6169 sys_longjmp (mgr
->setjmp_buffer
, 1);
6173 /* Init source method for JPEG data source manager. Called by
6174 jpeg_read_header() before any data is actually read. See
6175 libjpeg.doc from the JPEG lib distribution. */
6178 our_common_init_source (j_decompress_ptr cinfo
)
6183 /* Method to terminate data source. Called by
6184 jpeg_finish_decompress() after all data has been processed. */
6187 our_common_term_source (j_decompress_ptr cinfo
)
6192 /* Fill input buffer method for JPEG data source manager. Called
6193 whenever more data is needed. We read the whole image in one step,
6194 so this only adds a fake end of input marker at the end. */
6196 static JOCTET our_memory_buffer
[2];
6199 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6201 /* Insert a fake EOI marker. */
6202 struct jpeg_source_mgr
*src
= cinfo
->src
;
6204 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6205 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6207 src
->next_input_byte
= our_memory_buffer
;
6208 src
->bytes_in_buffer
= 2;
6213 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6214 is the JPEG data source manager. */
6217 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6219 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6223 if (num_bytes
> src
->bytes_in_buffer
)
6224 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6226 src
->bytes_in_buffer
-= num_bytes
;
6227 src
->next_input_byte
+= num_bytes
;
6232 /* Set up the JPEG lib for reading an image from DATA which contains
6233 LEN bytes. CINFO is the decompression info structure created for
6234 reading the image. */
6237 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, ptrdiff_t len
)
6239 struct jpeg_source_mgr
*src
;
6241 if (cinfo
->src
== NULL
)
6243 /* First time for this JPEG object? */
6244 cinfo
->src
= (struct jpeg_source_mgr
*)
6245 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6246 sizeof (struct jpeg_source_mgr
));
6247 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6248 src
->next_input_byte
= data
;
6251 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6252 src
->init_source
= our_common_init_source
;
6253 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6254 src
->skip_input_data
= our_memory_skip_input_data
;
6255 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6256 src
->term_source
= our_common_term_source
;
6257 src
->bytes_in_buffer
= len
;
6258 src
->next_input_byte
= data
;
6262 struct jpeg_stdio_mgr
6264 struct jpeg_source_mgr mgr
;
6271 /* Size of buffer to read JPEG from file.
6272 Not too big, as we want to use alloc_small. */
6273 #define JPEG_STDIO_BUFFER_SIZE 8192
6276 /* Fill input buffer method for JPEG data source manager. Called
6277 whenever more data is needed. The data is read from a FILE *. */
6280 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6282 struct jpeg_stdio_mgr
*src
;
6284 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6289 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6291 src
->mgr
.bytes_in_buffer
= bytes
;
6294 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6296 src
->buffer
[0] = (JOCTET
) 0xFF;
6297 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6298 src
->mgr
.bytes_in_buffer
= 2;
6300 src
->mgr
.next_input_byte
= src
->buffer
;
6307 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6308 is the JPEG data source manager. */
6311 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6313 struct jpeg_stdio_mgr
*src
;
6314 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6316 while (num_bytes
> 0 && !src
->finished
)
6318 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6320 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6321 src
->mgr
.next_input_byte
+= num_bytes
;
6326 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6327 src
->mgr
.bytes_in_buffer
= 0;
6328 src
->mgr
.next_input_byte
= NULL
;
6330 our_stdio_fill_input_buffer (cinfo
);
6336 /* Set up the JPEG lib for reading an image from a FILE *.
6337 CINFO is the decompression info structure created for
6338 reading the image. */
6341 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6343 struct jpeg_stdio_mgr
*src
;
6345 if (cinfo
->src
!= NULL
)
6346 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6349 /* First time for this JPEG object? */
6350 cinfo
->src
= (struct jpeg_source_mgr
*)
6351 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6352 sizeof (struct jpeg_stdio_mgr
));
6353 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6354 src
->buffer
= (JOCTET
*)
6355 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6356 JPEG_STDIO_BUFFER_SIZE
);
6361 src
->mgr
.init_source
= our_common_init_source
;
6362 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6363 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6364 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6365 src
->mgr
.term_source
= our_common_term_source
;
6366 src
->mgr
.bytes_in_buffer
= 0;
6367 src
->mgr
.next_input_byte
= NULL
;
6371 /* Load image IMG for use on frame F. Patterned after example.c
6372 from the JPEG lib. */
6375 jpeg_load_body (struct frame
*f
, struct image
*img
,
6376 struct my_jpeg_error_mgr
*mgr
)
6378 Lisp_Object file
, specified_file
;
6379 Lisp_Object specified_data
;
6382 int row_stride
, x
, y
;
6383 XImagePtr ximg
= NULL
;
6384 unsigned long *colors
;
6387 /* Open the JPEG file. */
6388 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6389 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6391 if (NILP (specified_data
))
6393 file
= x_find_image_file (specified_file
);
6394 if (!STRINGP (file
))
6396 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6400 fp
= fopen (SSDATA (file
), "rb");
6403 image_error ("Cannot open `%s'", file
, Qnil
);
6407 else if (!STRINGP (specified_data
))
6409 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6413 IF_LINT (mgr
->fp
= fp
);
6415 /* Customize libjpeg's error handling to call my_error_exit when an
6416 error is detected. This function will perform a longjmp. */
6417 mgr
->cinfo
.err
= fn_jpeg_std_error (&mgr
->pub
);
6418 mgr
->pub
.error_exit
= my_error_exit
;
6419 if (sys_setjmp (mgr
->setjmp_buffer
))
6421 switch (mgr
->failure_code
)
6423 case MY_JPEG_ERROR_EXIT
:
6425 char buf
[JMSG_LENGTH_MAX
];
6426 mgr
->cinfo
.err
->format_message ((j_common_ptr
) &mgr
->cinfo
, buf
);
6427 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6428 build_string (buf
));
6432 case MY_JPEG_INVALID_IMAGE_SIZE
:
6433 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6436 case MY_JPEG_CANNOT_CREATE_X
:
6440 /* Close the input file and destroy the JPEG object. */
6443 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6445 /* If we already have an XImage, free that. */
6446 x_destroy_x_image (ximg
);
6448 /* Free pixmap and colors. */
6449 x_clear_image (f
, img
);
6453 /* Silence a bogus diagnostic; see GCC bug 54561. */
6454 IF_LINT (fp
= mgr
->fp
);
6456 /* Create the JPEG decompression object. Let it read from fp.
6457 Read the JPEG image header. */
6458 fn_jpeg_CreateDecompress (&mgr
->cinfo
, JPEG_LIB_VERSION
, sizeof *&mgr
->cinfo
);
6460 if (NILP (specified_data
))
6461 jpeg_file_src (&mgr
->cinfo
, fp
);
6463 jpeg_memory_src (&mgr
->cinfo
, SDATA (specified_data
),
6464 SBYTES (specified_data
));
6466 fn_jpeg_read_header (&mgr
->cinfo
, 1);
6468 /* Customize decompression so that color quantization will be used.
6469 Start decompression. */
6470 mgr
->cinfo
.quantize_colors
= 1;
6471 fn_jpeg_start_decompress (&mgr
->cinfo
);
6472 width
= img
->width
= mgr
->cinfo
.output_width
;
6473 height
= img
->height
= mgr
->cinfo
.output_height
;
6475 if (!check_image_size (f
, width
, height
))
6477 mgr
->failure_code
= MY_JPEG_INVALID_IMAGE_SIZE
;
6478 sys_longjmp (mgr
->setjmp_buffer
, 1);
6481 /* Create X image and pixmap. */
6482 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6484 mgr
->failure_code
= MY_JPEG_CANNOT_CREATE_X
;
6485 sys_longjmp (mgr
->setjmp_buffer
, 1);
6488 /* Allocate colors. When color quantization is used,
6489 mgr->cinfo.actual_number_of_colors has been set with the number of
6490 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6491 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6492 No more than 255 colors will be generated. */
6496 if (mgr
->cinfo
.out_color_components
> 2)
6497 ir
= 0, ig
= 1, ib
= 2;
6498 else if (mgr
->cinfo
.out_color_components
> 1)
6499 ir
= 0, ig
= 1, ib
= 0;
6501 ir
= 0, ig
= 0, ib
= 0;
6503 /* Use the color table mechanism because it handles colors that
6504 cannot be allocated nicely. Such colors will be replaced with
6505 a default color, and we don't have to care about which colors
6506 can be freed safely, and which can't. */
6507 init_color_table ();
6508 colors
= alloca (mgr
->cinfo
.actual_number_of_colors
* sizeof *colors
);
6510 for (i
= 0; i
< mgr
->cinfo
.actual_number_of_colors
; ++i
)
6512 /* Multiply RGB values with 255 because X expects RGB values
6513 in the range 0..0xffff. */
6514 int r
= mgr
->cinfo
.colormap
[ir
][i
] << 8;
6515 int g
= mgr
->cinfo
.colormap
[ig
][i
] << 8;
6516 int b
= mgr
->cinfo
.colormap
[ib
][i
] << 8;
6517 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6520 #ifdef COLOR_TABLE_SUPPORT
6521 /* Remember those colors actually allocated. */
6522 img
->colors
= colors_in_color_table (&img
->ncolors
);
6523 free_color_table ();
6524 #endif /* COLOR_TABLE_SUPPORT */
6528 row_stride
= width
* mgr
->cinfo
.output_components
;
6529 buffer
= mgr
->cinfo
.mem
->alloc_sarray ((j_common_ptr
) &mgr
->cinfo
,
6530 JPOOL_IMAGE
, row_stride
, 1);
6531 for (y
= 0; y
< height
; ++y
)
6533 fn_jpeg_read_scanlines (&mgr
->cinfo
, buffer
, 1);
6534 for (x
= 0; x
< mgr
->cinfo
.output_width
; ++x
)
6535 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6539 fn_jpeg_finish_decompress (&mgr
->cinfo
);
6540 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6544 /* Maybe fill in the background field while we have ximg handy. */
6545 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6546 /* Casting avoids a GCC warning. */
6547 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6549 /* Put the image into the pixmap. */
6550 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6551 x_destroy_x_image (ximg
);
6556 jpeg_load (struct frame
*f
, struct image
*img
)
6558 struct my_jpeg_error_mgr mgr
;
6559 return jpeg_load_body (f
, img
, &mgr
);
6562 #else /* HAVE_JPEG */
6566 jpeg_load (struct frame
*f
, struct image
*img
)
6568 return ns_load_image (f
, img
,
6569 image_spec_value (img
->spec
, QCfile
, NULL
),
6570 image_spec_value (img
->spec
, QCdata
, NULL
));
6572 #endif /* HAVE_NS */
6574 #endif /* !HAVE_JPEG */
6578 /***********************************************************************
6580 ***********************************************************************/
6582 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6584 static int tiff_image_p (Lisp_Object object
);
6585 static int tiff_load (struct frame
*f
, struct image
*img
);
6587 /* The symbol `tiff' identifying images of this type. */
6589 static Lisp_Object Qtiff
;
6591 /* Indices of image specification fields in tiff_format, below. */
6593 enum tiff_keyword_index
6602 TIFF_HEURISTIC_MASK
,
6609 /* Vector of image_keyword structures describing the format
6610 of valid user-defined image specifications. */
6612 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6614 {":type", IMAGE_SYMBOL_VALUE
, 1},
6615 {":data", IMAGE_STRING_VALUE
, 0},
6616 {":file", IMAGE_STRING_VALUE
, 0},
6617 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6618 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6619 {":relief", IMAGE_INTEGER_VALUE
, 0},
6620 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6621 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6622 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6623 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6624 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6627 /* Structure describing the image type `tiff'. */
6629 static struct image_type tiff_type
=
6638 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6641 tiff_image_p (Lisp_Object object
)
6643 struct image_keyword fmt
[TIFF_LAST
];
6644 memcpy (fmt
, tiff_format
, sizeof fmt
);
6646 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6649 /* Must specify either the :data or :file keyword. */
6650 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6653 #endif /* HAVE_TIFF || HAVE_NS */
6661 /* TIFF library details. */
6662 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6663 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6664 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6665 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6666 TIFFReadWriteProc
, TIFFReadWriteProc
,
6667 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6668 TIFFMapFileProc
, TIFFUnmapFileProc
));
6669 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6670 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6671 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6672 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6675 init_tiff_functions (Lisp_Object libraries
)
6679 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6682 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6683 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6684 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6685 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6686 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6687 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6688 LOAD_IMGLIB_FN (library
, TIFFClose
);
6689 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6695 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6696 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6697 #define fn_TIFFOpen TIFFOpen
6698 #define fn_TIFFClientOpen TIFFClientOpen
6699 #define fn_TIFFGetField TIFFGetField
6700 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6701 #define fn_TIFFClose TIFFClose
6702 #define fn_TIFFSetDirectory TIFFSetDirectory
6703 #endif /* HAVE_NTGUI */
6706 /* Reading from a memory buffer for TIFF images Based on the PNG
6707 memory source, but we have to provide a lot of extra functions.
6710 We really only need to implement read and seek, but I am not
6711 convinced that the TIFF library is smart enough not to destroy
6712 itself if we only hand it the function pointers we need to
6717 unsigned char *bytes
;
6724 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6726 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6728 size
= min (size
, src
->len
- src
->index
);
6729 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6735 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6741 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6743 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6748 case SEEK_SET
: /* Go from beginning of source. */
6752 case SEEK_END
: /* Go from end of source. */
6753 idx
= src
->len
+ off
;
6756 case SEEK_CUR
: /* Go from current position. */
6757 idx
= src
->index
+ off
;
6760 default: /* Invalid `whence'. */
6764 if (idx
> src
->len
|| idx
< 0)
6772 tiff_close_memory (thandle_t data
)
6779 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6781 /* It is already _IN_ memory. */
6786 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6788 /* We don't need to do this. */
6792 tiff_size_of_memory (thandle_t data
)
6794 return ((tiff_memory_source
*) data
)->len
;
6797 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6798 compiler error compiling tiff_handler, see Bugzilla bug #17406
6799 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6800 this function as external works around that problem. */
6801 #if defined (__MINGW32__) && __GNUC__ == 3
6802 # define MINGW_STATIC
6804 # define MINGW_STATIC static
6808 tiff_handler (const char *, const char *, const char *, va_list)
6809 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6811 tiff_handler (const char *log_format
, const char *title
,
6812 const char *format
, va_list ap
)
6814 /* doprnt is not suitable here, as TIFF handlers are called from
6815 libtiff and are passed arbitrary printf directives. Instead, use
6816 vsnprintf, taking care to be portable to nonstandard environments
6817 where vsnprintf returns -1 on buffer overflow. Since it's just a
6818 log entry, it's OK to truncate it. */
6820 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6821 add_to_log (log_format
, build_string (title
),
6822 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6826 static void tiff_error_handler (const char *, const char *, va_list)
6827 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6829 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6831 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6835 static void tiff_warning_handler (const char *, const char *, va_list)
6836 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6838 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6840 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6844 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6848 tiff_load (struct frame
*f
, struct image
*img
)
6850 Lisp_Object file
, specified_file
;
6851 Lisp_Object specified_data
;
6853 int width
, height
, x
, y
, count
;
6857 tiff_memory_source memsrc
;
6860 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6861 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6863 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6864 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6866 if (NILP (specified_data
))
6868 /* Read from a file */
6869 file
= x_find_image_file (specified_file
);
6870 if (!STRINGP (file
))
6872 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6876 /* Try to open the image file. */
6877 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6880 image_error ("Cannot open `%s'", file
, Qnil
);
6886 if (!STRINGP (specified_data
))
6888 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6892 /* Memory source! */
6893 memsrc
.bytes
= SDATA (specified_data
);
6894 memsrc
.len
= SBYTES (specified_data
);
6897 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6898 tiff_read_from_memory
,
6899 tiff_write_from_memory
,
6900 tiff_seek_in_memory
,
6902 tiff_size_of_memory
,
6908 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6913 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6914 if (INTEGERP (image
))
6916 EMACS_INT ino
= XFASTINT (image
);
6917 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
6918 && fn_TIFFSetDirectory (tiff
, ino
)))
6920 image_error ("Invalid image number `%s' in image `%s'",
6922 fn_TIFFClose (tiff
);
6927 /* Get width and height of the image, and allocate a raster buffer
6928 of width x height 32-bit values. */
6929 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6930 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6932 if (!check_image_size (f
, width
, height
))
6934 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6935 fn_TIFFClose (tiff
);
6939 /* Create the X image and pixmap. */
6940 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
6941 && x_create_x_image_and_pixmap (f
, width
, height
, 0,
6942 &ximg
, &img
->pixmap
)))
6944 fn_TIFFClose (tiff
);
6948 buf
= xmalloc (sizeof *buf
* width
* height
);
6950 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6952 /* Count the number of images in the file. */
6953 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
6957 img
->lisp_data
= Fcons (Qcount
,
6958 Fcons (make_number (count
),
6961 fn_TIFFClose (tiff
);
6964 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6969 /* Initialize the color table. */
6970 init_color_table ();
6972 /* Process the pixel raster. Origin is in the lower-left corner. */
6973 for (y
= 0; y
< height
; ++y
)
6975 uint32
*row
= buf
+ y
* width
;
6977 for (x
= 0; x
< width
; ++x
)
6979 uint32 abgr
= row
[x
];
6980 int r
= TIFFGetR (abgr
) << 8;
6981 int g
= TIFFGetG (abgr
) << 8;
6982 int b
= TIFFGetB (abgr
) << 8;
6983 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6987 #ifdef COLOR_TABLE_SUPPORT
6988 /* Remember the colors allocated for the image. Free the color table. */
6989 img
->colors
= colors_in_color_table (&img
->ncolors
);
6990 free_color_table ();
6991 #endif /* COLOR_TABLE_SUPPORT */
6994 img
->height
= height
;
6996 /* Maybe fill in the background field while we have ximg handy. */
6997 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6998 /* Casting avoids a GCC warning on W32. */
6999 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7001 /* Put the image into the pixmap, then free the X image and its buffer. */
7002 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7003 x_destroy_x_image (ximg
);
7009 #else /* HAVE_TIFF */
7013 tiff_load (struct frame
*f
, struct image
*img
)
7015 return ns_load_image (f
, img
,
7016 image_spec_value (img
->spec
, QCfile
, NULL
),
7017 image_spec_value (img
->spec
, QCdata
, NULL
));
7019 #endif /* HAVE_NS */
7021 #endif /* !HAVE_TIFF */
7025 /***********************************************************************
7027 ***********************************************************************/
7029 #if defined (HAVE_GIF) || defined (HAVE_NS)
7031 static int gif_image_p (Lisp_Object object
);
7032 static int gif_load (struct frame
*f
, struct image
*img
);
7033 static void gif_clear_image (struct frame
*f
, struct image
*img
);
7035 /* The symbol `gif' identifying images of this type. */
7037 static Lisp_Object Qgif
;
7039 /* Indices of image specification fields in gif_format, below. */
7041 enum gif_keyword_index
7057 /* Vector of image_keyword structures describing the format
7058 of valid user-defined image specifications. */
7060 static const struct image_keyword gif_format
[GIF_LAST
] =
7062 {":type", IMAGE_SYMBOL_VALUE
, 1},
7063 {":data", IMAGE_STRING_VALUE
, 0},
7064 {":file", IMAGE_STRING_VALUE
, 0},
7065 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7066 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7067 {":relief", IMAGE_INTEGER_VALUE
, 0},
7068 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7069 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7070 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7071 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7072 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7075 /* Structure describing the image type `gif'. */
7077 static struct image_type gif_type
=
7086 /* Free X resources of GIF image IMG which is used on frame F. */
7089 gif_clear_image (struct frame
*f
, struct image
*img
)
7091 img
->lisp_data
= Qnil
;
7092 x_clear_image (f
, img
);
7095 /* Return non-zero if OBJECT is a valid GIF image specification. */
7098 gif_image_p (Lisp_Object object
)
7100 struct image_keyword fmt
[GIF_LAST
];
7101 memcpy (fmt
, gif_format
, sizeof fmt
);
7103 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7106 /* Must specify either the :data or :file keyword. */
7107 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7110 #endif /* HAVE_GIF */
7114 #if defined (HAVE_NTGUI)
7115 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7116 Undefine before redefining to avoid a preprocessor warning. */
7120 /* avoid conflict with QuickdrawText.h */
7121 #define DrawText gif_DrawText
7122 #include <gif_lib.h>
7125 #else /* HAVE_NTGUI */
7127 #include <gif_lib.h>
7129 #endif /* HAVE_NTGUI */
7134 /* GIF library details. */
7135 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7136 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7137 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7138 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7141 init_gif_functions (Lisp_Object libraries
)
7145 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7148 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7149 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7150 LOAD_IMGLIB_FN (library
, DGifOpen
);
7151 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7157 #define fn_DGifCloseFile DGifCloseFile
7158 #define fn_DGifSlurp DGifSlurp
7159 #define fn_DGifOpen DGifOpen
7160 #define fn_DGifOpenFileName DGifOpenFileName
7162 #endif /* HAVE_NTGUI */
7164 /* Reading a GIF image from memory
7165 Based on the PNG memory stuff to a certain extent. */
7169 unsigned char *bytes
;
7175 /* Make the current memory source available to gif_read_from_memory.
7176 It's done this way because not all versions of libungif support
7177 a UserData field in the GifFileType structure. */
7178 static gif_memory_source
*current_gif_memory_src
;
7181 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7183 gif_memory_source
*src
= current_gif_memory_src
;
7185 if (len
> src
->len
- src
->index
)
7188 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7194 /* Load GIF image IMG for use on frame F. Value is non-zero if
7197 static const int interlace_start
[] = {0, 4, 2, 1};
7198 static const int interlace_increment
[] = {8, 8, 4, 2};
7200 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7203 gif_load (struct frame
*f
, struct image
*img
)
7206 int rc
, width
, height
, x
, y
, i
, j
;
7208 ColorMapObject
*gif_color_map
;
7209 unsigned long pixel_colors
[256];
7211 gif_memory_source memsrc
;
7212 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7213 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7214 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7215 unsigned long bgcolor
= 0;
7218 if (NILP (specified_data
))
7220 file
= x_find_image_file (specified_file
);
7221 if (!STRINGP (file
))
7223 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7227 /* Open the GIF file. */
7228 gif
= fn_DGifOpenFileName (SSDATA (file
));
7231 image_error ("Cannot open `%s'", file
, Qnil
);
7237 if (!STRINGP (specified_data
))
7239 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7243 /* Read from memory! */
7244 current_gif_memory_src
= &memsrc
;
7245 memsrc
.bytes
= SDATA (specified_data
);
7246 memsrc
.len
= SBYTES (specified_data
);
7249 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7252 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7257 /* Before reading entire contents, check the declared image size. */
7258 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7260 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7261 fn_DGifCloseFile (gif
);
7265 /* Read entire contents. */
7266 rc
= fn_DGifSlurp (gif
);
7267 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7269 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7270 fn_DGifCloseFile (gif
);
7274 /* Which sub-image are we to display? */
7276 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7277 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7278 if (idx
< 0 || idx
>= gif
->ImageCount
)
7280 image_error ("Invalid image number `%s' in image `%s'",
7281 image_number
, img
->spec
);
7282 fn_DGifCloseFile (gif
);
7287 width
= img
->width
= gif
->SWidth
;
7288 height
= img
->height
= gif
->SHeight
;
7290 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Top
;
7291 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Left
;
7292 img
->corners
[BOT_CORNER
]
7293 = img
->corners
[TOP_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Height
;
7294 img
->corners
[RIGHT_CORNER
]
7295 = img
->corners
[LEFT_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Width
;
7297 if (!check_image_size (f
, width
, height
))
7299 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7300 fn_DGifCloseFile (gif
);
7304 /* Create the X image and pixmap. */
7305 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7307 fn_DGifCloseFile (gif
);
7311 /* Clear the part of the screen image not covered by the image.
7312 Full animated GIF support requires more here (see the gif89 spec,
7313 disposal methods). Let's simply assume that the part not covered
7314 by a sub-image is in the frame's background color. */
7315 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7316 for (x
= 0; x
< width
; ++x
)
7317 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7319 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7320 for (x
= 0; x
< width
; ++x
)
7321 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7323 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7325 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7326 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7327 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7328 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7331 /* Read the GIF image into the X image. */
7333 /* FIXME: With the current implementation, loading an animated gif
7334 is quadratic in the number of animation frames, since each frame
7335 is a separate struct image. We must provide a way for a single
7336 gif_load call to construct and save all animation frames. */
7338 init_color_table ();
7339 if (STRINGP (specified_bg
))
7340 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7341 FRAME_BACKGROUND_PIXEL (f
));
7342 for (j
= 0; j
<= idx
; ++j
)
7344 /* We use a local variable `raster' here because RasterBits is a
7345 char *, which invites problems with bytes >= 0x80. */
7346 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7347 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7348 int transparency_color_index
= -1;
7350 int subimg_width
= subimage
->ImageDesc
.Width
;
7351 int subimg_height
= subimage
->ImageDesc
.Height
;
7352 int subimg_top
= subimage
->ImageDesc
.Top
;
7353 int subimg_left
= subimage
->ImageDesc
.Left
;
7355 /* Find the Graphic Control Extension block for this sub-image.
7356 Extract the disposal method and transparency color. */
7357 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7359 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7361 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7362 && extblock
->ByteCount
== 4
7363 && extblock
->Bytes
[0] & 1)
7365 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7366 to background". Treat any other value like 2. */
7367 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7368 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7373 /* We can't "keep in place" the first subimage. */
7377 /* For disposal == 0, the spec says "No disposal specified. The
7378 decoder is not required to take any action." In practice, it
7379 seems we need to treat this like "keep in place", see e.g.
7380 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7384 /* Allocate subimage colors. */
7385 memset (pixel_colors
, 0, sizeof pixel_colors
);
7386 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7388 gif_color_map
= gif
->SColorMap
;
7391 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7393 if (transparency_color_index
== i
)
7394 pixel_colors
[i
] = STRINGP (specified_bg
)
7395 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7398 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7399 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7400 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7401 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7405 /* Apply the pixel values. */
7406 if (gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7410 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7412 y
++, row
+= interlace_increment
[pass
])
7414 if (row
>= subimg_height
)
7416 row
= interlace_start
[++pass
];
7417 while (row
>= subimg_height
)
7418 row
= interlace_start
[++pass
];
7421 for (x
= 0; x
< subimg_width
; x
++)
7423 int c
= raster
[y
* subimg_width
+ x
];
7424 if (transparency_color_index
!= c
|| disposal
!= 1)
7425 XPutPixel (ximg
, x
+ subimg_left
, row
+ subimg_top
,
7432 for (y
= 0; y
< subimg_height
; ++y
)
7433 for (x
= 0; x
< subimg_width
; ++x
)
7435 int c
= raster
[y
* subimg_width
+ x
];
7436 if (transparency_color_index
!= c
|| disposal
!= 1)
7437 XPutPixel (ximg
, x
+ subimg_left
, y
+ subimg_top
,
7443 #ifdef COLOR_TABLE_SUPPORT
7444 img
->colors
= colors_in_color_table (&img
->ncolors
);
7445 free_color_table ();
7446 #endif /* COLOR_TABLE_SUPPORT */
7448 /* Save GIF image extension data for `image-metadata'.
7449 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7450 img
->lisp_data
= Qnil
;
7451 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7454 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7455 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7456 /* Append (... FUNCTION "BYTES") */
7459 = Fcons (make_number (ext
->Function
),
7460 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7462 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7463 && ext
->ByteCount
== 4)
7465 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7466 delay
|= ext
->Bytes
[1];
7469 img
->lisp_data
= Fcons (Qextension_data
,
7470 Fcons (img
->lisp_data
, Qnil
));
7474 Fcons (make_float (delay
/ 100.0),
7478 if (gif
->ImageCount
> 1)
7479 img
->lisp_data
= Fcons (Qcount
,
7480 Fcons (make_number (gif
->ImageCount
),
7483 fn_DGifCloseFile (gif
);
7485 /* Maybe fill in the background field while we have ximg handy. */
7486 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7487 /* Casting avoids a GCC warning. */
7488 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7490 /* Put the image into the pixmap, then free the X image and its buffer. */
7491 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7492 x_destroy_x_image (ximg
);
7497 #else /* !HAVE_GIF */
7501 gif_load (struct frame
*f
, struct image
*img
)
7503 return ns_load_image (f
, img
,
7504 image_spec_value (img
->spec
, QCfile
, NULL
),
7505 image_spec_value (img
->spec
, QCdata
, NULL
));
7507 #endif /* HAVE_NS */
7509 #endif /* HAVE_GIF */
7512 /***********************************************************************
7514 ***********************************************************************/
7515 #if defined (HAVE_IMAGEMAGICK)
7517 static Lisp_Object Qimagemagick
;
7519 static int imagemagick_image_p (Lisp_Object
);
7520 static int imagemagick_load (struct frame
*, struct image
*);
7521 static void imagemagick_clear_image (struct frame
*, struct image
*);
7523 /* Indices of image specification fields in imagemagick_format. */
7525 enum imagemagick_keyword_index
7533 IMAGEMAGICK_ALGORITHM
,
7534 IMAGEMAGICK_HEURISTIC_MASK
,
7536 IMAGEMAGICK_BACKGROUND
,
7539 IMAGEMAGICK_ROTATION
,
7544 /* Vector of image_keyword structures describing the format
7545 of valid user-defined image specifications. */
7547 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7549 {":type", IMAGE_SYMBOL_VALUE
, 1},
7550 {":data", IMAGE_STRING_VALUE
, 0},
7551 {":file", IMAGE_STRING_VALUE
, 0},
7552 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7553 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7554 {":relief", IMAGE_INTEGER_VALUE
, 0},
7555 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7556 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7557 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7558 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7559 {":height", IMAGE_INTEGER_VALUE
, 0},
7560 {":width", IMAGE_INTEGER_VALUE
, 0},
7561 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7562 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7565 /* Structure describing the image type for any image handled via
7568 static struct image_type imagemagick_type
=
7571 imagemagick_image_p
,
7573 imagemagick_clear_image
,
7577 /* Free X resources of imagemagick image IMG which is used on frame F. */
7580 imagemagick_clear_image (struct frame
*f
,
7583 x_clear_image (f
, img
);
7586 /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do
7587 this by calling parse_image_spec and supplying the keywords that
7588 identify the IMAGEMAGICK format. */
7591 imagemagick_image_p (Lisp_Object object
)
7593 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7594 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7596 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7599 /* Must specify either the :data or :file keyword. */
7600 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7603 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7604 Therefore rename the function so it doesn't collide with ImageMagick. */
7605 #define DrawRectangle DrawRectangleGif
7606 #include <wand/MagickWand.h>
7608 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7609 Emacs seems to work fine with the hidden version, so unhide it. */
7610 #include <magick/version.h>
7611 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7612 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7613 MagickPixelPacket
*);
7616 /* Log ImageMagick error message.
7617 Useful when a ImageMagick function returns the status `MagickFalse'. */
7620 imagemagick_error (MagickWand
*wand
)
7623 ExceptionType severity
;
7625 description
= MagickGetException (wand
, &severity
);
7626 image_error ("ImageMagick error: %s",
7627 build_string (description
),
7629 description
= (char *) MagickRelinquishMemory (description
);
7632 /* Helper function for imagemagick_load, which does the actual loading
7633 given contents and size, apart from frame and image structures,
7634 passed from imagemagick_load. Uses librimagemagick to do most of
7635 the image processing.
7637 F is a pointer to the Emacs frame; IMG to the image structure to
7638 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
7639 be parsed; SIZE is the number of bytes of data; and FILENAME is
7640 either the file name or the image data.
7642 Return non-zero if successful. */
7645 imagemagick_load_image (struct frame
*f
, struct image
*img
,
7646 unsigned char *contents
, unsigned int size
,
7649 size_t width
, height
;
7650 MagickBooleanType status
;
7653 MagickWand
*image_wand
;
7654 MagickWand
*ping_wand
;
7655 PixelIterator
*iterator
;
7656 PixelWand
**pixels
, *bg_wand
= NULL
;
7657 MagickPixelPacket pixel
;
7662 int desired_width
, desired_height
;
7666 /* Handle image index for image types who can contain more than one image.
7667 Interface :index is same as for GIF. First we "ping" the image to see how
7668 many sub-images it contains. Pinging is faster than loading the image to
7669 find out things about it. */
7671 /* Initialize the imagemagick environment. */
7672 MagickWandGenesis ();
7673 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7674 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7675 ping_wand
= NewMagickWand ();
7676 /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */
7679 ? MagickPingImage (ping_wand
, filename
)
7680 : MagickPingImageBlob (ping_wand
, contents
, size
);
7682 if (status
== MagickFalse
)
7684 imagemagick_error (ping_wand
);
7685 DestroyMagickWand (ping_wand
);
7689 if (ino
< 0 || ino
>= MagickGetNumberImages (ping_wand
))
7691 image_error ("Invalid image number `%s' in image `%s'",
7693 DestroyMagickWand (ping_wand
);
7697 if (MagickGetNumberImages (ping_wand
) > 1)
7700 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7703 DestroyMagickWand (ping_wand
);
7705 /* Now we know how many images are inside the file. If it's not a
7706 bundle, the number is one. Load the image data. */
7708 image_wand
= NewMagickWand ();
7711 ? MagickReadImage (image_wand
, filename
)
7712 : MagickReadImageBlob (image_wand
, contents
, size
))
7715 imagemagick_error (image_wand
);
7716 goto imagemagick_error
;
7719 /* Retrieve the frame's background color, for use later. */
7722 Lisp_Object specified_bg
;
7724 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7725 if (!STRINGP (specified_bg
)
7726 || !x_defined_color (f
, SSDATA (specified_bg
), &bgcolor
, 0))
7729 bgcolor
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
7730 x_query_color (f
, &bgcolor
);
7732 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &bgcolor
, 1);
7736 bg_wand
= NewPixelWand ();
7737 PixelSetRed (bg_wand
, (double) bgcolor
.red
/ 65535);
7738 PixelSetGreen (bg_wand
, (double) bgcolor
.green
/ 65535);
7739 PixelSetBlue (bg_wand
, (double) bgcolor
.blue
/ 65535);
7742 /* If width and/or height is set in the display spec assume we want
7743 to scale to those values. If either h or w is unspecified, the
7744 unspecified should be calculated from the specified to preserve
7746 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7747 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7748 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7749 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7751 height
= MagickGetImageHeight (image_wand
);
7752 width
= MagickGetImageWidth (image_wand
);
7754 if (desired_width
!= -1 && desired_height
== -1)
7755 /* w known, calculate h. */
7756 desired_height
= (double) desired_width
/ width
* height
;
7757 if (desired_width
== -1 && desired_height
!= -1)
7758 /* h known, calculate w. */
7759 desired_width
= (double) desired_height
/ height
* width
;
7760 if (desired_width
!= -1 && desired_height
!= -1)
7762 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7763 if (status
== MagickFalse
)
7765 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7766 imagemagick_error (image_wand
);
7767 goto imagemagick_error
;
7771 /* crop behaves similar to image slicing in Emacs but is more memory
7773 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7775 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7777 /* After some testing, it seems MagickCropImage is the fastest crop
7778 function in ImageMagick. This crop function seems to do less copying
7779 than the alternatives, but it still reads the entire image into memory
7780 before cropping, which is apparently difficult to avoid when using
7782 size_t crop_width
= XINT (XCAR (crop
));
7784 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7786 size_t crop_height
= XINT (XCAR (crop
));
7788 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7790 ssize_t crop_x
= XINT (XCAR (crop
));
7792 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7794 ssize_t crop_y
= XINT (XCAR (crop
));
7795 MagickCropImage (image_wand
, crop_width
, crop_height
,
7802 /* Furthermore :rotation. we need background color and angle for
7805 TODO background handling for rotation specified_bg =
7806 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7808 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7811 rotation
= extract_float (value
);
7812 status
= MagickRotateImage (image_wand
, bg_wand
, rotation
);
7813 if (status
== MagickFalse
)
7815 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7816 imagemagick_error (image_wand
);
7817 goto imagemagick_error
;
7821 /* Finally we are done manipulating the image. Figure out the
7822 resulting width/height and transfer ownership to Emacs. */
7823 height
= MagickGetImageHeight (image_wand
);
7824 width
= MagickGetImageWidth (image_wand
);
7826 /* Set the canvas background color to the frame or specified
7827 background, and flatten the image. Note: as of ImageMagick
7828 6.6.0, SVG image transparency is not handled properly
7829 (e.g. etc/images/splash.svg shows a white background always). */
7831 MagickWand
*new_wand
;
7832 MagickSetImageBackgroundColor (image_wand
, bg_wand
);
7833 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
7834 new_wand
= MagickMergeImageLayers (image_wand
, MergeLayer
);
7836 new_wand
= MagickFlattenImages (image_wand
);
7838 DestroyMagickWand (image_wand
);
7839 image_wand
= new_wand
;
7842 if (! (width
<= INT_MAX
&& height
<= INT_MAX
7843 && check_image_size (f
, width
, height
)))
7845 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7846 goto imagemagick_error
;
7849 /* We can now get a valid pixel buffer from the imagemagick file, if all
7852 init_color_table ();
7854 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7855 if (imagemagick_render_type
!= 0)
7857 /* Magicexportimage is normally faster than pixelpushing. This
7858 method is also well tested. Some aspects of this method are
7859 ad-hoc and needs to be more researched. */
7860 int imagedepth
= 24; /*MagickGetImageDepth(image_wand);*/
7861 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP"; /*"RGBP";*/
7862 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7863 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7864 &ximg
, &img
->pixmap
))
7866 #ifdef COLOR_TABLE_SUPPORT
7867 free_color_table ();
7869 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7870 goto imagemagick_error
;
7873 /* Oddly, the below code doesn't seem to work:*/
7874 /* switch(ximg->bitmap_unit){ */
7876 /* pixelwidth=CharPixel; */
7879 /* pixelwidth=ShortPixel; */
7882 /* pixelwidth=LongPixel; */
7886 Here im just guessing the format of the bitmap.
7887 happens to work fine for:
7890 seems about 3 times as fast as pixel pushing(not carefully measured)
7892 pixelwidth
= CharPixel
; /*??? TODO figure out*/
7893 MagickExportImagePixels (image_wand
, 0, 0, width
, height
,
7894 exportdepth
, pixelwidth
, ximg
->data
);
7897 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
7899 size_t image_height
;
7901 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7902 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7903 &ximg
, &img
->pixmap
))
7905 #ifdef COLOR_TABLE_SUPPORT
7906 free_color_table ();
7908 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7909 goto imagemagick_error
;
7912 /* Copy imagemagick image to x with primitive yet robust pixel
7913 pusher loop. This has been tested a lot with many different
7916 /* Copy pixels from the imagemagick image structure to the x image map. */
7917 iterator
= NewPixelIterator (image_wand
);
7918 if (iterator
== (PixelIterator
*) NULL
)
7920 #ifdef COLOR_TABLE_SUPPORT
7921 free_color_table ();
7923 x_destroy_x_image (ximg
);
7924 image_error ("Imagemagick pixel iterator creation failed",
7926 goto imagemagick_error
;
7929 image_height
= MagickGetImageHeight (image_wand
);
7930 for (y
= 0; y
< image_height
; y
++)
7932 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7933 if (pixels
== (PixelWand
**) NULL
)
7935 for (x
= 0; x
< (long) width
; x
++)
7937 PixelGetMagickColor (pixels
[x
], &pixel
);
7938 XPutPixel (ximg
, x
, y
,
7939 lookup_rgb_color (f
,
7945 DestroyPixelIterator (iterator
);
7948 #ifdef COLOR_TABLE_SUPPORT
7949 /* Remember colors allocated for this image. */
7950 img
->colors
= colors_in_color_table (&img
->ncolors
);
7951 free_color_table ();
7952 #endif /* COLOR_TABLE_SUPPORT */
7955 img
->height
= height
;
7957 /* Put the image into the pixmap, then free the X image and its
7959 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7960 x_destroy_x_image (ximg
);
7962 /* Final cleanup. image_wand should be the only resource left. */
7963 DestroyMagickWand (image_wand
);
7964 if (bg_wand
) DestroyPixelWand (bg_wand
);
7966 /* `MagickWandTerminus' terminates the imagemagick environment. */
7967 MagickWandTerminus ();
7972 DestroyMagickWand (image_wand
);
7973 if (bg_wand
) DestroyPixelWand (bg_wand
);
7975 MagickWandTerminus ();
7976 /* TODO more cleanup. */
7977 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7982 /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if
7983 successful. this function will go into the imagemagick_type structure, and
7984 the prototype thus needs to be compatible with that structure. */
7987 imagemagick_load (struct frame
*f
, struct image
*img
)
7990 Lisp_Object file_name
;
7992 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7993 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7994 if (STRINGP (file_name
))
7998 file
= x_find_image_file (file_name
);
7999 if (!STRINGP (file
))
8001 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8004 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
8006 /* Else its not a file, its a lisp object. Load the image from a
8007 lisp object rather than a file. */
8012 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8013 if (!STRINGP (data
))
8015 image_error ("Invalid image data `%s'", data
, Qnil
);
8018 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
8019 SBYTES (data
), NULL
);
8025 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
8026 doc
: /* Return a list of image types supported by ImageMagick.
8027 Each entry in this list is a symbol named after an ImageMagick format
8028 tag. See the ImageMagick manual for a list of ImageMagick formats and
8029 their descriptions (http://www.imagemagick.org/script/formats.php).
8030 You can also try the shell command: `identify -list format'.
8032 Note that ImageMagick recognizes many file-types that Emacs does not
8033 recognize as images, such as C. See `imagemagick-types-enable'
8034 and `imagemagick-types-inhibit'. */)
8037 Lisp_Object typelist
= Qnil
;
8042 Lisp_Object Qimagemagicktype
;
8044 GetExceptionInfo(&ex
);
8045 imtypes
= GetMagickList ("*", &numf
, &ex
);
8046 DestroyExceptionInfo(&ex
);
8048 for (i
= 0; i
< numf
; i
++)
8050 Qimagemagicktype
= intern (imtypes
[i
]);
8051 typelist
= Fcons (Qimagemagicktype
, typelist
);
8053 return Fnreverse (typelist
);
8056 #endif /* defined (HAVE_IMAGEMAGICK) */
8060 /***********************************************************************
8062 ***********************************************************************/
8064 #if defined (HAVE_RSVG)
8066 /* Function prototypes. */
8068 static int svg_image_p (Lisp_Object object
);
8069 static int svg_load (struct frame
*f
, struct image
*img
);
8071 static int svg_load_image (struct frame
*, struct image
*,
8072 unsigned char *, ptrdiff_t);
8074 /* The symbol `svg' identifying images of this type. */
8076 static Lisp_Object Qsvg
;
8078 /* Indices of image specification fields in svg_format, below. */
8080 enum svg_keyword_index
8095 /* Vector of image_keyword structures describing the format
8096 of valid user-defined image specifications. */
8098 static const struct image_keyword svg_format
[SVG_LAST
] =
8100 {":type", IMAGE_SYMBOL_VALUE
, 1},
8101 {":data", IMAGE_STRING_VALUE
, 0},
8102 {":file", IMAGE_STRING_VALUE
, 0},
8103 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8104 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8105 {":relief", IMAGE_INTEGER_VALUE
, 0},
8106 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8107 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8108 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8109 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8112 /* Structure describing the image type `svg'. Its the same type of
8113 structure defined for all image formats, handled by emacs image
8114 functions. See struct image_type in dispextern.h. */
8116 static struct image_type svg_type
=
8118 /* An identifier showing that this is an image structure for the SVG format. */
8120 /* Handle to a function that can be used to identify a SVG file. */
8122 /* Handle to function used to load a SVG file. */
8124 /* Handle to function to free sresources for SVG. */
8126 /* An internal field to link to the next image type in a list of
8127 image types, will be filled in when registering the format. */
8132 /* Return non-zero if OBJECT is a valid SVG image specification. Do
8133 this by calling parse_image_spec and supplying the keywords that
8134 identify the SVG format. */
8137 svg_image_p (Lisp_Object object
)
8139 struct image_keyword fmt
[SVG_LAST
];
8140 memcpy (fmt
, svg_format
, sizeof fmt
);
8142 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8145 /* Must specify either the :data or :file keyword. */
8146 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8149 #include <librsvg/rsvg.h>
8153 /* SVG library functions. */
8154 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
);
8155 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
);
8156 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
);
8157 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
);
8158 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
);
8160 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
);
8161 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
);
8162 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
);
8163 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
);
8164 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
);
8165 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
);
8166 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
);
8167 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
);
8169 DEF_IMGLIB_FN (void, g_type_init
);
8170 DEF_IMGLIB_FN (void, g_object_unref
);
8171 DEF_IMGLIB_FN (void, g_error_free
);
8173 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8176 init_svg_functions (Lisp_Object libraries
)
8178 HMODULE library
, gdklib
, glib
, gobject
;
8180 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
8181 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
8182 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
8183 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
8186 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8187 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8188 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8189 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8190 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8192 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8193 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8194 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8195 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8196 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8197 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8198 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8199 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8201 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8202 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8203 LOAD_IMGLIB_FN (glib
, g_error_free
);
8209 /* The following aliases for library functions allow dynamic loading
8210 to be used on some platforms. */
8211 #define fn_rsvg_handle_new rsvg_handle_new
8212 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8213 #define fn_rsvg_handle_write rsvg_handle_write
8214 #define fn_rsvg_handle_close rsvg_handle_close
8215 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8217 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8218 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8219 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8220 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8221 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8222 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8223 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8224 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8226 #define fn_g_type_init g_type_init
8227 #define fn_g_object_unref g_object_unref
8228 #define fn_g_error_free g_error_free
8229 #endif /* !HAVE_NTGUI */
8231 /* Load SVG image IMG for use on frame F. Value is non-zero if
8232 successful. this function will go into the svg_type structure, and
8233 the prototype thus needs to be compatible with that structure. */
8236 svg_load (struct frame
*f
, struct image
*img
)
8239 Lisp_Object file_name
;
8241 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8242 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8243 if (STRINGP (file_name
))
8246 unsigned char *contents
;
8249 file
= x_find_image_file (file_name
);
8250 if (!STRINGP (file
))
8252 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8256 /* Read the entire file into memory. */
8257 contents
= slurp_file (SSDATA (file
), &size
);
8258 if (contents
== NULL
)
8260 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8263 /* If the file was slurped into memory properly, parse it. */
8264 success_p
= svg_load_image (f
, img
, contents
, size
);
8267 /* Else its not a file, its a lisp object. Load the image from a
8268 lisp object rather than a file. */
8273 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8274 if (!STRINGP (data
))
8276 image_error ("Invalid image data `%s'", data
, Qnil
);
8279 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8285 /* svg_load_image is a helper function for svg_load, which does the
8286 actual loading given contents and size, apart from frame and image
8287 structures, passed from svg_load.
8289 Uses librsvg to do most of the image processing.
8291 Returns non-zero when successful. */
8293 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8294 struct image
*img
, /* Pointer to emacs image structure. */
8295 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8296 ptrdiff_t size
) /* Size of data in bytes. */
8298 RsvgHandle
*rsvg_handle
;
8299 RsvgDimensionData dimension_data
;
8304 const guint8
*pixels
;
8307 Lisp_Object specified_bg
;
8312 /* g_type_init is a glib function that must be called prior to using
8313 gnome type library functions. */
8315 /* Make a handle to a new rsvg object. */
8316 rsvg_handle
= fn_rsvg_handle_new ();
8318 /* Parse the contents argument and fill in the rsvg_handle. */
8319 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8320 if (err
) goto rsvg_error
;
8322 /* The parsing is complete, rsvg_handle is ready to used, close it
8323 for further writes. */
8324 fn_rsvg_handle_close (rsvg_handle
, &err
);
8325 if (err
) goto rsvg_error
;
8327 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8328 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8330 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8334 /* We can now get a valid pixel buffer from the svg file, if all
8336 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8337 if (!pixbuf
) goto rsvg_error
;
8338 fn_g_object_unref (rsvg_handle
);
8340 /* Extract some meta data from the svg handle. */
8341 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8342 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8343 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8344 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8346 /* Validate the svg meta data. */
8347 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8348 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8349 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8350 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8352 /* Try to create a x pixmap to hold the svg pixmap. */
8353 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8355 fn_g_object_unref (pixbuf
);
8359 init_color_table ();
8361 /* Handle alpha channel by combining the image with a background
8363 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8364 if (!STRINGP (specified_bg
)
8365 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8368 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8369 x_query_color (f
, &background
);
8371 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8375 /* SVG pixmaps specify transparency in the last byte, so right
8376 shift 8 bits to get rid of it, since emacs doesn't support
8378 background
.red
>>= 8;
8379 background
.green
>>= 8;
8380 background
.blue
>>= 8;
8382 /* This loop handles opacity values, since Emacs assumes
8383 non-transparent images. Each pixel must be "flattened" by
8384 calculating the resulting color, given the transparency of the
8385 pixel, and the image background color. */
8386 for (y
= 0; y
< height
; ++y
)
8388 for (x
= 0; x
< width
; ++x
)
8398 opacity
= *pixels
++;
8400 red
= ((red
* opacity
)
8401 + (background
.red
* ((1 << 8) - opacity
)));
8402 green
= ((green
* opacity
)
8403 + (background
.green
* ((1 << 8) - opacity
)));
8404 blue
= ((blue
* opacity
)
8405 + (background
.blue
* ((1 << 8) - opacity
)));
8407 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8410 pixels
+= rowstride
- 4 * width
;
8413 #ifdef COLOR_TABLE_SUPPORT
8414 /* Remember colors allocated for this image. */
8415 img
->colors
= colors_in_color_table (&img
->ncolors
);
8416 free_color_table ();
8417 #endif /* COLOR_TABLE_SUPPORT */
8419 fn_g_object_unref (pixbuf
);
8422 img
->height
= height
;
8424 /* Maybe fill in the background field while we have ximg handy.
8425 Casting avoids a GCC warning. */
8426 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8428 /* Put the image into the pixmap, then free the X image and its
8430 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8431 x_destroy_x_image (ximg
);
8436 fn_g_object_unref (rsvg_handle
);
8437 /* FIXME: Use error->message so the user knows what is the actual
8438 problem with the image. */
8439 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8440 fn_g_error_free (err
);
8444 #endif /* defined (HAVE_RSVG) */
8449 /***********************************************************************
8451 ***********************************************************************/
8453 #ifdef HAVE_X_WINDOWS
8454 #define HAVE_GHOSTSCRIPT 1
8455 #endif /* HAVE_X_WINDOWS */
8457 #ifdef HAVE_GHOSTSCRIPT
8459 static int gs_image_p (Lisp_Object object
);
8460 static int gs_load (struct frame
*f
, struct image
*img
);
8461 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8463 /* Keyword symbols. */
8465 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8467 /* Indices of image specification fields in gs_format, below. */
8469 enum gs_keyword_index
8487 /* Vector of image_keyword structures describing the format
8488 of valid user-defined image specifications. */
8490 static const struct image_keyword gs_format
[GS_LAST
] =
8492 {":type", IMAGE_SYMBOL_VALUE
, 1},
8493 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8494 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8495 {":file", IMAGE_STRING_VALUE
, 1},
8496 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8497 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8498 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8499 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8500 {":relief", IMAGE_INTEGER_VALUE
, 0},
8501 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8502 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8503 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8504 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8507 /* Structure describing the image type `ghostscript'. */
8509 static struct image_type gs_type
=
8519 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8522 gs_clear_image (struct frame
*f
, struct image
*img
)
8524 x_clear_image (f
, img
);
8528 /* Return non-zero if OBJECT is a valid Ghostscript image
8532 gs_image_p (Lisp_Object object
)
8534 struct image_keyword fmt
[GS_LAST
];
8538 memcpy (fmt
, gs_format
, sizeof fmt
);
8540 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8543 /* Bounding box must be a list or vector containing 4 integers. */
8544 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8547 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8548 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8553 else if (VECTORP (tem
))
8555 if (ASIZE (tem
) != 4)
8557 for (i
= 0; i
< 4; ++i
)
8558 if (!INTEGERP (AREF (tem
, i
)))
8568 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8572 gs_load (struct frame
*f
, struct image
*img
)
8574 uprintmax_t printnum1
, printnum2
;
8575 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
8576 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8578 double in_width
, in_height
;
8579 Lisp_Object pixel_colors
= Qnil
;
8581 /* Compute pixel size of pixmap needed from the given size in the
8582 image specification. Sizes in the specification are in pt. 1 pt
8583 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8585 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8586 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8587 in_width
*= FRAME_X_DISPLAY_INFO (f
)->resx
;
8588 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8589 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8590 in_height
*= FRAME_X_DISPLAY_INFO (f
)->resy
;
8592 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
8593 && check_image_size (f
, in_width
, in_height
)))
8595 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8598 img
->width
= in_width
;
8599 img
->height
= in_height
;
8601 /* Create the pixmap. */
8602 eassert (img
->pixmap
== NO_PIXMAP
);
8604 if (x_check_image_size (0, img
->width
, img
->height
))
8606 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8608 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8609 img
->width
, img
->height
,
8610 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8616 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8620 /* Call the loader to fill the pixmap. It returns a process object
8621 if successful. We do not record_unwind_protect here because
8622 other places in redisplay like calling window scroll functions
8623 don't either. Let the Lisp loader use `unwind-protect' instead. */
8624 printnum1
= FRAME_X_WINDOW (f
);
8625 printnum2
= img
->pixmap
;
8626 window_and_pixmap_id
8627 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8629 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
8630 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
8632 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8634 XSETFRAME (frame
, f
);
8635 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8637 loader
= intern ("gs-load-image");
8639 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
8640 make_number (img
->width
),
8641 make_number (img
->height
),
8642 window_and_pixmap_id
,
8644 return PROCESSP (img
->lisp_data
);
8648 /* Kill the Ghostscript process that was started to fill PIXMAP on
8649 frame F. Called from XTread_socket when receiving an event
8650 telling Emacs that Ghostscript has finished drawing. */
8653 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8655 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8660 /* Find the image containing PIXMAP. */
8661 for (i
= 0; i
< c
->used
; ++i
)
8662 if (c
->images
[i
]->pixmap
== pixmap
)
8665 /* Should someone in between have cleared the image cache, for
8666 instance, give up. */
8670 /* Kill the GS process. We should have found PIXMAP in the image
8671 cache and its image should contain a process object. */
8673 eassert (PROCESSP (img
->lisp_data
));
8674 Fkill_process (img
->lisp_data
, Qnil
);
8675 img
->lisp_data
= Qnil
;
8677 #if defined (HAVE_X_WINDOWS)
8679 /* On displays with a mutable colormap, figure out the colors
8680 allocated for the image by looking at the pixels of an XImage for
8682 class = FRAME_X_VISUAL (f
)->class;
8683 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8689 /* Try to get an XImage for img->pixmep. */
8690 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8691 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8696 /* Initialize the color table. */
8697 init_color_table ();
8699 /* For each pixel of the image, look its color up in the
8700 color table. After having done so, the color table will
8701 contain an entry for each color used by the image. */
8702 for (y
= 0; y
< img
->height
; ++y
)
8703 for (x
= 0; x
< img
->width
; ++x
)
8705 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8706 lookup_pixel_color (f
, pixel
);
8709 /* Record colors in the image. Free color table and XImage. */
8710 #ifdef COLOR_TABLE_SUPPORT
8711 img
->colors
= colors_in_color_table (&img
->ncolors
);
8712 free_color_table ();
8714 XDestroyImage (ximg
);
8716 #if 0 /* This doesn't seem to be the case. If we free the colors
8717 here, we get a BadAccess later in x_clear_image when
8718 freeing the colors. */
8719 /* We have allocated colors once, but Ghostscript has also
8720 allocated colors on behalf of us. So, to get the
8721 reference counts right, free them once. */
8723 x_free_colors (f
, img
->colors
, img
->ncolors
);
8727 image_error ("Cannot get X image of `%s'; colors will not be freed",
8732 #endif /* HAVE_X_WINDOWS */
8734 /* Now that we have the pixmap, compute mask and transform the
8735 image if requested. */
8737 postprocess_image (f
, img
);
8741 #endif /* HAVE_GHOSTSCRIPT */
8744 /***********************************************************************
8746 ***********************************************************************/
8750 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8751 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8754 return valid_image_p (spec
) ? Qt
: Qnil
;
8758 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8763 if (valid_image_p (spec
))
8764 id
= lookup_image (SELECTED_FRAME (), spec
);
8767 return make_number (id
);
8770 #endif /* GLYPH_DEBUG */
8773 /***********************************************************************
8775 ***********************************************************************/
8778 /* Image types that rely on external libraries are loaded dynamically
8779 if the library is available. */
8780 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8781 define_image_type (image_type, init_lib_fn (libraries))
8783 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8784 define_image_type (image_type, 1)
8785 #endif /* HAVE_NTGUI */
8787 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8788 doc
: /* Initialize image library implementing image type TYPE.
8789 Return non-nil if TYPE is a supported image type.
8791 Image types pbm and xbm are prebuilt; other types are loaded here.
8792 Libraries to load are specified in alist LIBRARIES (usually, the value
8793 of `dynamic-library-alist', which see). */)
8794 (Lisp_Object type
, Lisp_Object libraries
)
8797 /* Don't try to reload the library. */
8798 Lisp_Object tested
= Fassq (type
, Vlibrary_cache
);
8800 return XCDR (tested
);
8803 /* Types pbm and xbm are built-in and always available. */
8804 if (EQ (type
, Qpbm
) || EQ (type
, Qxbm
))
8807 #if defined (HAVE_XPM) || defined (HAVE_NS)
8808 if (EQ (type
, Qxpm
))
8809 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8812 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8813 if (EQ (type
, Qjpeg
))
8814 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8817 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8818 if (EQ (type
, Qtiff
))
8819 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8822 #if defined (HAVE_GIF) || defined (HAVE_NS)
8823 if (EQ (type
, Qgif
))
8824 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8827 #if defined (HAVE_PNG) || defined (HAVE_NS)
8828 if (EQ (type
, Qpng
))
8829 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8832 #if defined (HAVE_RSVG)
8833 if (EQ (type
, Qsvg
))
8834 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8837 #if defined (HAVE_IMAGEMAGICK)
8838 if (EQ (type
, Qimagemagick
))
8839 return CHECK_LIB_AVAILABLE (&imagemagick_type
, init_imagemagick_functions
,
8843 #ifdef HAVE_GHOSTSCRIPT
8844 if (EQ (type
, Qpostscript
))
8845 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8848 /* If the type is not recognized, avoid testing it ever again. */
8849 CACHE_IMAGE_TYPE (type
, Qnil
);
8854 syms_of_image (void)
8856 /* Initialize this only once, since that's what we do with Vimage_types
8857 and they are supposed to be in sync. Initializing here gives correct
8858 operation on GNU/Linux of calling dump-emacs after loading some images. */
8861 /* Must be defined now because we're going to update it below, while
8862 defining the supported image types. */
8863 DEFVAR_LISP ("image-types", Vimage_types
,
8864 doc
: /* List of potentially supported image types.
8865 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8866 To check whether it is really supported, use `image-type-available-p'. */);
8867 Vimage_types
= Qnil
;
8869 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8870 doc
: /* Maximum size of images.
8871 Emacs will not load an image into memory if its pixel width or
8872 pixel height exceeds this limit.
8874 If the value is an integer, it directly specifies the maximum
8875 image height and width, measured in pixels. If it is a floating
8876 point number, it specifies the maximum image height and width
8877 as a ratio to the frame height and width. If the value is
8878 non-numeric, there is no explicit limit on the size of images. */);
8879 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8881 DEFSYM (Qpbm
, "pbm");
8882 ADD_IMAGE_TYPE (Qpbm
);
8884 DEFSYM (Qxbm
, "xbm");
8885 ADD_IMAGE_TYPE (Qxbm
);
8887 define_image_type (&xbm_type
, 1);
8888 define_image_type (&pbm_type
, 1);
8890 DEFSYM (Qcount
, "count");
8891 DEFSYM (Qextension_data
, "extension-data");
8892 DEFSYM (Qdelay
, "delay");
8894 DEFSYM (QCascent
, ":ascent");
8895 DEFSYM (QCmargin
, ":margin");
8896 DEFSYM (QCrelief
, ":relief");
8897 DEFSYM (QCconversion
, ":conversion");
8898 DEFSYM (QCcolor_symbols
, ":color-symbols");
8899 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
8900 DEFSYM (QCindex
, ":index");
8901 DEFSYM (QCgeometry
, ":geometry");
8902 DEFSYM (QCcrop
, ":crop");
8903 DEFSYM (QCrotation
, ":rotation");
8904 DEFSYM (QCmatrix
, ":matrix");
8905 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
8906 DEFSYM (QCmask
, ":mask");
8908 DEFSYM (Qlaplace
, "laplace");
8909 DEFSYM (Qemboss
, "emboss");
8910 DEFSYM (Qedge_detection
, "edge-detection");
8911 DEFSYM (Qheuristic
, "heuristic");
8913 DEFSYM (Qpostscript
, "postscript");
8914 #ifdef HAVE_GHOSTSCRIPT
8915 ADD_IMAGE_TYPE (Qpostscript
);
8916 DEFSYM (QCloader
, ":loader");
8917 DEFSYM (QCbounding_box
, ":bounding-box");
8918 DEFSYM (QCpt_width
, ":pt-width");
8919 DEFSYM (QCpt_height
, ":pt-height");
8920 #endif /* HAVE_GHOSTSCRIPT */
8923 DEFSYM (Qlibpng_version
, "libpng-version");
8924 Fset (Qlibpng_version
,
8926 make_number (PNG_LIBPNG_VER
)
8933 #if defined (HAVE_XPM) || defined (HAVE_NS)
8934 DEFSYM (Qxpm
, "xpm");
8935 ADD_IMAGE_TYPE (Qxpm
);
8938 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8939 DEFSYM (Qjpeg
, "jpeg");
8940 ADD_IMAGE_TYPE (Qjpeg
);
8943 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8944 DEFSYM (Qtiff
, "tiff");
8945 ADD_IMAGE_TYPE (Qtiff
);
8948 #if defined (HAVE_GIF) || defined (HAVE_NS)
8949 DEFSYM (Qgif
, "gif");
8950 ADD_IMAGE_TYPE (Qgif
);
8953 #if defined (HAVE_PNG) || defined (HAVE_NS)
8954 DEFSYM (Qpng
, "png");
8955 ADD_IMAGE_TYPE (Qpng
);
8958 #if defined (HAVE_IMAGEMAGICK)
8959 DEFSYM (Qimagemagick
, "imagemagick");
8960 ADD_IMAGE_TYPE (Qimagemagick
);
8963 #if defined (HAVE_RSVG)
8964 DEFSYM (Qsvg
, "svg");
8965 ADD_IMAGE_TYPE (Qsvg
);
8967 /* Other libraries used directly by svg code. */
8968 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
8969 DEFSYM (Qglib
, "glib");
8970 DEFSYM (Qgobject
, "gobject");
8971 #endif /* HAVE_NTGUI */
8972 #endif /* HAVE_RSVG */
8974 defsubr (&Sinit_image_library
);
8975 #ifdef HAVE_IMAGEMAGICK
8976 defsubr (&Simagemagick_types
);
8978 defsubr (&Sclear_image_cache
);
8979 defsubr (&Simage_flush
);
8980 defsubr (&Simage_size
);
8981 defsubr (&Simage_mask_p
);
8982 defsubr (&Simage_metadata
);
8986 defsubr (&Slookup_image
);
8989 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8990 doc
: /* Non-nil means always draw a cross over disabled images.
8991 Disabled images are those having a `:conversion disabled' property.
8992 A cross is always drawn on black & white displays. */);
8993 cross_disabled_images
= 0;
8995 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8996 doc
: /* List of directories to search for window system bitmap files. */);
8997 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8999 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
9000 doc
: /* Maximum time after which images are removed from the cache.
9001 When an image has not been displayed this many seconds, Emacs
9002 automatically removes it from the image cache. If the cache contains
9003 a large number of images, the actual eviction time may be shorter.
9004 The value can also be nil, meaning the cache is never cleared.
9006 The function `clear-image-cache' disregards this variable. */);
9007 Vimage_cache_eviction_delay
= make_number (300);
9008 #ifdef HAVE_IMAGEMAGICK
9009 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type
,
9010 doc
: /* Integer indicating which ImageMagick rendering method to use.
9012 0 -- the default method (pixel pushing)
9013 1 -- a newer method ("MagickExportImagePixels") that may perform
9014 better (speed etc) in some cases, but has not been as thoroughly
9015 tested with Emacs as the default method. This method requires
9016 ImageMagick version 6.4.6 (approximately) or later.
9018 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9019 imagemagick_render_type
= 0;