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 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
594 The initialized data segment is read-only. */
595 struct image_type
*p
= xmalloc (sizeof *p
);
597 p
->next
= image_types
;
602 CACHE_IMAGE_TYPE (*type
->type
, success
);
607 /* Look up image type SYMBOL, and return a pointer to its image_type
608 structure. Value is null if SYMBOL is not a known image type. */
610 static inline struct image_type
*
611 lookup_image_type (Lisp_Object symbol
)
613 struct image_type
*type
;
615 /* We must initialize the image-type if it hasn't been already. */
616 if (NILP (Finit_image_library (symbol
, Vdynamic_library_alist
)))
617 return 0; /* unimplemented */
619 for (type
= image_types
; type
; type
= type
->next
)
620 if (EQ (symbol
, *type
->type
))
627 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
628 valid image specification is a list whose car is the symbol
629 `image', and whose rest is a property list. The property list must
630 contain a value for key `:type'. That value must be the name of a
631 supported image type. The rest of the property list depends on the
635 valid_image_p (Lisp_Object object
)
643 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
644 if (EQ (XCAR (tem
), QCtype
))
647 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
649 struct image_type
*type
;
650 type
= lookup_image_type (XCAR (tem
));
652 valid_p
= type
->valid_p (object
);
663 /* Log error message with format string FORMAT and argument ARG.
664 Signaling an error, e.g. when an image cannot be loaded, is not a
665 good idea because this would interrupt redisplay, and the error
666 message display would lead to another redisplay. This function
667 therefore simply displays a message. */
670 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
672 add_to_log (format
, arg1
, arg2
);
677 /***********************************************************************
679 ***********************************************************************/
681 enum image_value_type
683 IMAGE_DONT_CHECK_VALUE_TYPE
,
685 IMAGE_STRING_OR_NIL_VALUE
,
687 IMAGE_POSITIVE_INTEGER_VALUE
,
688 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
,
689 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
692 IMAGE_FUNCTION_VALUE
,
697 /* Structure used when parsing image specifications. */
701 /* Name of keyword. */
704 /* The type of value allowed. */
705 enum image_value_type type
;
707 /* Non-zero means key must be present. */
710 /* Used to recognize duplicate keywords in a property list. */
713 /* The value that was found. */
718 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
720 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
723 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
724 has the format (image KEYWORD VALUE ...). One of the keyword/
725 value pairs must be `:type TYPE'. KEYWORDS is a vector of
726 image_keywords structures of size NKEYWORDS describing other
727 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
730 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
731 int nkeywords
, Lisp_Object type
)
740 while (CONSP (plist
))
742 Lisp_Object key
, value
;
744 /* First element of a pair must be a symbol. */
746 plist
= XCDR (plist
);
750 /* There must follow a value. */
753 value
= XCAR (plist
);
754 plist
= XCDR (plist
);
756 /* Find key in KEYWORDS. Error if not found. */
757 for (i
= 0; i
< nkeywords
; ++i
)
758 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
764 /* Record that we recognized the keyword. If a keywords
765 was found more than once, it's an error. */
766 keywords
[i
].value
= value
;
767 if (keywords
[i
].count
> 1)
771 /* Check type of value against allowed type. */
772 switch (keywords
[i
].type
)
774 case IMAGE_STRING_VALUE
:
775 if (!STRINGP (value
))
779 case IMAGE_STRING_OR_NIL_VALUE
:
780 if (!STRINGP (value
) && !NILP (value
))
784 case IMAGE_SYMBOL_VALUE
:
785 if (!SYMBOLP (value
))
789 case IMAGE_POSITIVE_INTEGER_VALUE
:
790 if (! RANGED_INTEGERP (1, value
, INT_MAX
))
794 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
:
795 if (RANGED_INTEGERP (0, value
, INT_MAX
))
798 && RANGED_INTEGERP (0, XCAR (value
), INT_MAX
)
799 && RANGED_INTEGERP (0, XCDR (value
), INT_MAX
))
803 case IMAGE_ASCENT_VALUE
:
804 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
806 else if (RANGED_INTEGERP (0, value
, 100))
810 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
811 /* Unlike the other integer-related cases, this one does not
812 verify that VALUE fits in 'int'. This is because callers
814 if (!INTEGERP (value
) || XINT (value
) < 0)
818 case IMAGE_DONT_CHECK_VALUE_TYPE
:
821 case IMAGE_FUNCTION_VALUE
:
822 value
= indirect_function (value
);
823 if (!NILP (Ffunctionp (value
)))
827 case IMAGE_NUMBER_VALUE
:
828 if (!INTEGERP (value
) && !FLOATP (value
))
832 case IMAGE_INTEGER_VALUE
:
833 if (! TYPE_RANGED_INTEGERP (int, value
))
837 case IMAGE_BOOL_VALUE
:
838 if (!NILP (value
) && !EQ (value
, Qt
))
847 if (EQ (key
, QCtype
) && !EQ (type
, value
))
851 /* Check that all mandatory fields are present. */
852 for (i
= 0; i
< nkeywords
; ++i
)
853 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
860 /* Return the value of KEY in image specification SPEC. Value is nil
861 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
862 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
865 image_spec_value (Lisp_Object spec
, Lisp_Object key
, int *found
)
869 eassert (valid_image_p (spec
));
871 for (tail
= XCDR (spec
);
872 CONSP (tail
) && CONSP (XCDR (tail
));
873 tail
= XCDR (XCDR (tail
)))
875 if (EQ (XCAR (tail
), key
))
879 return XCAR (XCDR (tail
));
889 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
890 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
891 PIXELS non-nil means return the size in pixels, otherwise return the
892 size in canonical character units.
893 FRAME is the frame on which the image will be displayed. FRAME nil
894 or omitted means use the selected frame. */)
895 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
900 if (valid_image_p (spec
))
902 struct frame
*f
= check_x_frame (frame
);
903 ptrdiff_t id
= lookup_image (f
, spec
);
904 struct image
*img
= IMAGE_FROM_ID (f
, id
);
905 int width
= img
->width
+ 2 * img
->hmargin
;
906 int height
= img
->height
+ 2 * img
->vmargin
;
909 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
910 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
912 size
= Fcons (make_number (width
), make_number (height
));
915 error ("Invalid image specification");
921 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
922 doc
: /* Return t if image SPEC has a mask bitmap.
923 FRAME is the frame on which the image will be displayed. FRAME nil
924 or omitted means use the selected frame. */)
925 (Lisp_Object spec
, Lisp_Object frame
)
930 if (valid_image_p (spec
))
932 struct frame
*f
= check_x_frame (frame
);
933 ptrdiff_t id
= lookup_image (f
, spec
);
934 struct image
*img
= IMAGE_FROM_ID (f
, id
);
939 error ("Invalid image specification");
944 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
945 doc
: /* Return metadata for image SPEC.
946 FRAME is the frame on which the image will be displayed. FRAME nil
947 or omitted means use the selected frame. */)
948 (Lisp_Object spec
, Lisp_Object frame
)
953 if (valid_image_p (spec
))
955 struct frame
*f
= check_x_frame (frame
);
956 ptrdiff_t id
= lookup_image (f
, spec
);
957 struct image
*img
= IMAGE_FROM_ID (f
, id
);
958 ext
= img
->lisp_data
;
965 /***********************************************************************
966 Image type independent image structures
967 ***********************************************************************/
969 static void free_image (struct frame
*f
, struct image
*img
);
971 #define MAX_IMAGE_SIZE 10.0
972 /* Allocate and return a new image structure for image specification
973 SPEC. SPEC has a hash value of HASH. */
975 static struct image
*
976 make_image (Lisp_Object spec
, EMACS_UINT hash
)
978 struct image
*img
= xzalloc (sizeof *img
);
979 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
981 eassert (valid_image_p (spec
));
982 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
983 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
984 eassert (img
->type
!= NULL
);
986 img
->lisp_data
= Qnil
;
987 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
989 img
->corners
[BOT_CORNER
] = -1; /* Full image */
994 /* Free image IMG which was used on frame F, including its resources. */
997 free_image (struct frame
*f
, struct image
*img
)
1001 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1003 /* Remove IMG from the hash table of its cache. */
1005 img
->prev
->next
= img
->next
;
1007 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1010 img
->next
->prev
= img
->prev
;
1012 c
->images
[img
->id
] = NULL
;
1014 /* Free resources, then free IMG. */
1015 img
->type
->free (f
, img
);
1020 /* Return 1 if the given widths and heights are valid for display;
1021 otherwise, return 0. */
1024 check_image_size (struct frame
*f
, int width
, int height
)
1028 if (width
<= 0 || height
<= 0)
1031 if (INTEGERP (Vmax_image_size
))
1032 return (width
<= XINT (Vmax_image_size
)
1033 && height
<= XINT (Vmax_image_size
));
1034 else if (FLOATP (Vmax_image_size
))
1038 w
= FRAME_PIXEL_WIDTH (f
);
1039 h
= FRAME_PIXEL_HEIGHT (f
);
1042 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1043 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1044 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1050 /* Prepare image IMG for display on frame F. Must be called before
1051 drawing an image. */
1054 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1056 /* We're about to display IMG, so set its timestamp to `now'. */
1057 img
->timestamp
= current_emacs_time ();
1059 /* If IMG doesn't have a pixmap yet, load it now, using the image
1060 type dependent loader function. */
1061 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1062 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1067 /* Value is the number of pixels for the ascent of image IMG when
1068 drawn in face FACE. */
1071 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1076 if (slice
->height
== img
->height
)
1077 height
= img
->height
+ img
->vmargin
;
1078 else if (slice
->y
== 0)
1079 height
= slice
->height
+ img
->vmargin
;
1081 height
= slice
->height
;
1083 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1088 /* W32 specific version. Why?. ++kfs */
1089 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1090 - FONT_BASE (face
->font
)) / 2;
1092 /* This expression is arranged so that if the image can't be
1093 exactly centered, it will be moved slightly up. This is
1094 because a typical font is `top-heavy' (due to the presence
1095 uppercase letters), so the image placement should err towards
1096 being top-heavy too. It also just generally looks better. */
1097 ascent
= (height
+ FONT_BASE (face
->font
)
1098 - FONT_DESCENT (face
->font
) + 1) / 2;
1099 #endif /* HAVE_NTGUI */
1102 ascent
= height
/ 2;
1105 ascent
= height
* (img
->ascent
/ 100.0);
1111 /* Image background colors. */
1113 /* Find the "best" corner color of a bitmap.
1114 On W32, XIMG is assumed to a device context with the bitmap selected. */
1116 static RGB_PIXEL_COLOR
1117 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1118 unsigned long width
, unsigned long height
)
1120 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1123 if (corners
&& corners
[BOT_CORNER
] >= 0)
1125 /* Get the colors at the corner_pixels of ximg. */
1126 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1127 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1128 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1129 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1133 /* Get the colors at the corner_pixels of ximg. */
1134 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1135 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1136 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1137 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1139 /* Choose the most frequently found color as background. */
1140 for (i
= best_count
= 0; i
< 4; ++i
)
1144 for (j
= n
= 0; j
< 4; ++j
)
1145 if (corner_pixels
[i
] == corner_pixels
[j
])
1149 best
= corner_pixels
[i
], best_count
= n
;
1155 /* Portability macros */
1159 #define Destroy_Image(img_dc, prev) \
1160 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1162 #define Free_Pixmap(display, pixmap) \
1163 DeleteObject (pixmap)
1165 #elif defined (HAVE_NS)
1167 #define Destroy_Image(ximg, dummy) \
1168 ns_release_object (ximg)
1170 #define Free_Pixmap(display, pixmap) \
1171 ns_release_object (pixmap)
1175 #define Destroy_Image(ximg, dummy) \
1176 XDestroyImage (ximg)
1178 #define Free_Pixmap(display, pixmap) \
1179 XFreePixmap (display, pixmap)
1181 #endif /* !HAVE_NTGUI && !HAVE_NS */
1184 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1185 it is guessed heuristically. If non-zero, XIMG is an existing
1186 XImage object (or device context with the image selected on W32) to
1187 use for the heuristic. */
1190 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1192 if (! img
->background_valid
)
1193 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1195 int free_ximg
= !ximg
;
1198 #endif /* HAVE_NTGUI */
1203 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1204 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1206 HDC frame_dc
= get_frame_dc (f
);
1207 ximg
= CreateCompatibleDC (frame_dc
);
1208 release_frame_dc (f
, frame_dc
);
1209 prev
= SelectObject (ximg
, img
->pixmap
);
1210 #endif /* !HAVE_NTGUI */
1213 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1216 Destroy_Image (ximg
, prev
);
1218 img
->background_valid
= 1;
1221 return img
->background
;
1224 /* Return the `background_transparent' field of IMG. If IMG doesn't
1225 have one yet, it is guessed heuristically. If non-zero, MASK is an
1226 existing XImage object to use for the heuristic. */
1229 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1231 if (! img
->background_transparent_valid
)
1232 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1236 int free_mask
= !mask
;
1239 #endif /* HAVE_NTGUI */
1244 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1245 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1247 HDC frame_dc
= get_frame_dc (f
);
1248 mask
= CreateCompatibleDC (frame_dc
);
1249 release_frame_dc (f
, frame_dc
);
1250 prev
= SelectObject (mask
, img
->mask
);
1251 #endif /* HAVE_NTGUI */
1254 img
->background_transparent
1255 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1258 Destroy_Image (mask
, prev
);
1261 img
->background_transparent
= 0;
1263 img
->background_transparent_valid
= 1;
1266 return img
->background_transparent
;
1270 /***********************************************************************
1271 Helper functions for X image types
1272 ***********************************************************************/
1274 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1276 static void x_clear_image (struct frame
*f
, struct image
*img
);
1277 static unsigned long x_alloc_image_color (struct frame
*f
,
1279 Lisp_Object color_name
,
1280 unsigned long dflt
);
1283 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1284 free the pixmap if any. MASK_P non-zero means clear the mask
1285 pixmap if any. COLORS_P non-zero means free colors allocated for
1286 the image, if any. */
1289 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1292 if (pixmap_p
&& img
->pixmap
)
1294 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1295 img
->pixmap
= NO_PIXMAP
;
1296 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1297 img
->background_valid
= 0;
1300 if (mask_p
&& img
->mask
)
1302 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1303 img
->mask
= NO_PIXMAP
;
1304 img
->background_transparent_valid
= 0;
1307 if (colors_p
&& img
->ncolors
)
1309 /* W32_TODO: color table support. */
1310 #ifdef HAVE_X_WINDOWS
1311 x_free_colors (f
, img
->colors
, img
->ncolors
);
1312 #endif /* HAVE_X_WINDOWS */
1313 xfree (img
->colors
);
1320 /* Free X resources of image IMG which is used on frame F. */
1323 x_clear_image (struct frame
*f
, struct image
*img
)
1326 x_clear_image_1 (f
, img
, 1, 1, 1);
1331 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1332 cannot be allocated, use DFLT. Add a newly allocated color to
1333 IMG->colors, so that it can be freed again. Value is the pixel
1336 static unsigned long
1337 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1341 unsigned long result
;
1343 eassert (STRINGP (color_name
));
1345 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1346 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1349 /* This isn't called frequently so we get away with simply
1350 reallocating the color vector to the needed size, here. */
1351 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1352 img
->colors
= xrealloc (img
->colors
, ncolors
* sizeof *img
->colors
);
1353 img
->colors
[ncolors
- 1] = color
.pixel
;
1354 img
->ncolors
= ncolors
;
1355 result
= color
.pixel
;
1365 /***********************************************************************
1367 ***********************************************************************/
1369 static void cache_image (struct frame
*f
, struct image
*img
);
1370 static void postprocess_image (struct frame
*, struct image
*);
1372 /* Return a new, initialized image cache that is allocated from the
1373 heap. Call free_image_cache to free an image cache. */
1375 struct image_cache
*
1376 make_image_cache (void)
1378 struct image_cache
*c
= xzalloc (sizeof *c
);
1382 c
->images
= xmalloc (size
* sizeof *c
->images
);
1384 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1385 c
->buckets
= xzalloc (size
);
1390 /* Find an image matching SPEC in the cache, and return it. If no
1391 image is found, return NULL. */
1392 static struct image
*
1393 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1396 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1397 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1399 if (!c
) return NULL
;
1401 /* If the image spec does not specify a background color, the cached
1402 image must have the same background color as the current frame.
1403 The foreground color must also match, for the sake of monochrome
1406 In fact, we could ignore the foreground color matching condition
1407 for color images, or if the image spec specifies :foreground;
1408 similarly we could ignore the background color matching condition
1409 for formats that don't use transparency (such as jpeg), or if the
1410 image spec specifies :background. However, the extra memory
1411 usage is probably negligible in practice, so we don't bother. */
1413 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1414 if (img
->hash
== hash
1415 && !NILP (Fequal (img
->spec
, spec
))
1416 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1417 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1423 /* Search frame F for an image with spec SPEC, and free it. */
1426 uncache_image (struct frame
*f
, Lisp_Object spec
)
1428 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1431 free_image (f
, img
);
1432 /* As display glyphs may still be referring to the image ID, we
1433 must garbage the frame (Bug#6426). */
1434 SET_FRAME_GARBAGED (f
);
1439 /* Free image cache of frame F. Be aware that X frames share images
1443 free_image_cache (struct frame
*f
)
1445 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1450 /* Cache should not be referenced by any frame when freed. */
1451 eassert (c
->refcount
== 0);
1453 for (i
= 0; i
< c
->used
; ++i
)
1454 free_image (f
, c
->images
[i
]);
1458 FRAME_IMAGE_CACHE (f
) = NULL
;
1463 /* Clear image cache of frame F. FILTER=t means free all images.
1464 FILTER=nil means clear only images that haven't been
1465 displayed for some time.
1466 Else, only free the images which have FILTER in their `dependencies'.
1467 Should be called from time to time to reduce the number of loaded images.
1468 If image-cache-eviction-delay is non-nil, this frees images in the cache
1469 which weren't displayed for at least that many seconds. */
1472 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1474 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1478 ptrdiff_t i
, nfreed
= 0;
1480 /* Block input so that we won't be interrupted by a SIGIO
1481 while being in an inconsistent state. */
1486 /* Filter image cache. */
1487 for (i
= 0; i
< c
->used
; ++i
)
1489 struct image
*img
= c
->images
[i
];
1490 if (img
&& (EQ (Qt
, filter
)
1491 || !NILP (Fmember (filter
, img
->dependencies
))))
1493 free_image (f
, img
);
1498 else if (INTEGERP (Vimage_cache_eviction_delay
))
1500 /* Free cache based on timestamp. */
1503 ptrdiff_t nimages
= 0;
1505 for (i
= 0; i
< c
->used
; ++i
)
1509 /* If the number of cached images has grown unusually large,
1510 decrease the cache eviction delay (Bug#6230). */
1511 delay
= XINT (Vimage_cache_eviction_delay
);
1513 delay
= 1600 * delay
/ nimages
/ nimages
;
1514 delay
= max (delay
, 1);
1516 t
= current_emacs_time ();
1517 old
= sub_emacs_time (t
, EMACS_TIME_FROM_DOUBLE (delay
));
1519 for (i
= 0; i
< c
->used
; ++i
)
1521 struct image
*img
= c
->images
[i
];
1522 if (img
&& EMACS_TIME_LT (img
->timestamp
, old
))
1524 free_image (f
, img
);
1530 /* We may be clearing the image cache because, for example,
1531 Emacs was iconified for a longer period of time. In that
1532 case, current matrices may still contain references to
1533 images freed above. So, clear these matrices. */
1536 Lisp_Object tail
, frame
;
1538 FOR_EACH_FRAME (tail
, frame
)
1540 struct frame
*fr
= XFRAME (frame
);
1541 if (FRAME_IMAGE_CACHE (fr
) == c
)
1542 clear_current_matrices (fr
);
1545 ++windows_or_buffers_changed
;
1553 clear_image_caches (Lisp_Object filter
)
1555 /* FIXME: We want to do
1556 * struct terminal *t;
1557 * for (t = terminal_list; t; t = t->next_terminal)
1558 * clear_image_cache (t, filter); */
1559 Lisp_Object tail
, frame
;
1560 FOR_EACH_FRAME (tail
, frame
)
1561 if (FRAME_WINDOW_P (XFRAME (frame
)))
1562 clear_image_cache (XFRAME (frame
), filter
);
1565 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1567 doc
: /* Clear the image cache.
1568 FILTER nil or a frame means clear all images in the selected frame.
1569 FILTER t means clear the image caches of all frames.
1570 Anything else, means only clear those images which refer to FILTER,
1571 which is then usually a filename. */)
1572 (Lisp_Object filter
)
1574 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1575 clear_image_caches (filter
);
1577 clear_image_cache (check_x_frame (filter
), Qt
);
1583 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1585 doc
: /* Fush the image with specification SPEC on frame FRAME.
1586 This removes the image from the Emacs image cache. If SPEC specifies
1587 an image file, the next redisplay of this image will read from the
1588 current contents of that file.
1590 FRAME nil or omitted means use the selected frame.
1591 FRAME t means refresh the image on all frames. */)
1592 (Lisp_Object spec
, Lisp_Object frame
)
1594 if (!valid_image_p (spec
))
1595 error ("Invalid image specification");
1600 FOR_EACH_FRAME (tail
, frame
)
1602 struct frame
*f
= XFRAME (frame
);
1603 if (FRAME_WINDOW_P (f
))
1604 uncache_image (f
, spec
);
1608 uncache_image (check_x_frame (frame
), spec
);
1614 /* Compute masks and transform image IMG on frame F, as specified
1615 by the image's specification, */
1618 postprocess_image (struct frame
*f
, struct image
*img
)
1620 /* Manipulation of the image's mask. */
1623 Lisp_Object conversion
, spec
;
1628 /* `:heuristic-mask t'
1630 means build a mask heuristically.
1631 `:heuristic-mask (R G B)'
1632 `:mask (heuristic (R G B))'
1633 means build a mask from color (R G B) in the
1636 means remove a mask, if any. */
1638 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1640 x_build_heuristic_mask (f
, img
, mask
);
1645 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1647 if (EQ (mask
, Qheuristic
))
1648 x_build_heuristic_mask (f
, img
, Qt
);
1649 else if (CONSP (mask
)
1650 && EQ (XCAR (mask
), Qheuristic
))
1652 if (CONSP (XCDR (mask
)))
1653 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1655 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1657 else if (NILP (mask
) && found_p
&& img
->mask
)
1659 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1660 img
->mask
= NO_PIXMAP
;
1665 /* Should we apply an image transformation algorithm? */
1666 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1667 if (EQ (conversion
, Qdisabled
))
1668 x_disable_image (f
, img
);
1669 else if (EQ (conversion
, Qlaplace
))
1671 else if (EQ (conversion
, Qemboss
))
1673 else if (CONSP (conversion
)
1674 && EQ (XCAR (conversion
), Qedge_detection
))
1677 tem
= XCDR (conversion
);
1679 x_edge_detection (f
, img
,
1680 Fplist_get (tem
, QCmatrix
),
1681 Fplist_get (tem
, QCcolor_adjustment
));
1687 /* Return the id of image with Lisp specification SPEC on frame F.
1688 SPEC must be a valid Lisp image specification (see valid_image_p). */
1691 lookup_image (struct frame
*f
, Lisp_Object spec
)
1696 /* F must be a window-system frame, and SPEC must be a valid image
1698 eassert (FRAME_WINDOW_P (f
));
1699 eassert (valid_image_p (spec
));
1701 /* Look up SPEC in the hash table of the image cache. */
1702 hash
= sxhash (spec
, 0);
1703 img
= search_image_cache (f
, spec
, hash
);
1704 if (img
&& img
->load_failed_p
)
1706 free_image (f
, img
);
1710 /* If not found, create a new image and cache it. */
1714 img
= make_image (spec
, hash
);
1715 cache_image (f
, img
);
1716 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1717 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1718 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1720 /* If we can't load the image, and we don't have a width and
1721 height, use some arbitrary width and height so that we can
1722 draw a rectangle for it. */
1723 if (img
->load_failed_p
)
1727 value
= image_spec_value (spec
, QCwidth
, NULL
);
1728 img
->width
= (INTEGERP (value
)
1729 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1730 value
= image_spec_value (spec
, QCheight
, NULL
);
1731 img
->height
= (INTEGERP (value
)
1732 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1736 /* Handle image type independent image attributes
1737 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1738 `:background COLOR'. */
1739 Lisp_Object ascent
, margin
, relief
, bg
;
1742 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1743 if (INTEGERP (ascent
))
1744 img
->ascent
= XFASTINT (ascent
);
1745 else if (EQ (ascent
, Qcenter
))
1746 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1748 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1749 if (INTEGERP (margin
))
1750 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1751 else if (CONSP (margin
))
1753 img
->hmargin
= XFASTINT (XCAR (margin
));
1754 img
->vmargin
= XFASTINT (XCDR (margin
));
1757 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1758 relief_bound
= INT_MAX
- max (img
->hmargin
, img
->vmargin
);
1759 if (RANGED_INTEGERP (- relief_bound
, relief
, relief_bound
))
1761 img
->relief
= XINT (relief
);
1762 img
->hmargin
+= eabs (img
->relief
);
1763 img
->vmargin
+= eabs (img
->relief
);
1766 if (! img
->background_valid
)
1768 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1772 = x_alloc_image_color (f
, img
, bg
,
1773 FRAME_BACKGROUND_PIXEL (f
));
1774 img
->background_valid
= 1;
1778 /* Do image transformations and compute masks, unless we
1779 don't have the image yet. */
1780 if (!EQ (*img
->type
->type
, Qpostscript
))
1781 postprocess_image (f
, img
);
1787 /* We're using IMG, so set its timestamp to `now'. */
1788 img
->timestamp
= current_emacs_time ();
1790 /* Value is the image id. */
1795 /* Cache image IMG in the image cache of frame F. */
1798 cache_image (struct frame
*f
, struct image
*img
)
1800 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1803 /* Find a free slot in c->images. */
1804 for (i
= 0; i
< c
->used
; ++i
)
1805 if (c
->images
[i
] == NULL
)
1808 /* If no free slot found, maybe enlarge c->images. */
1809 if (i
== c
->used
&& c
->used
== c
->size
)
1810 c
->images
= xpalloc (c
->images
, &c
->size
, 1, -1, sizeof *c
->images
);
1812 /* Add IMG to c->images, and assign IMG an id. */
1818 /* Add IMG to the cache's hash table. */
1819 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1820 img
->next
= c
->buckets
[i
];
1822 img
->next
->prev
= img
;
1824 c
->buckets
[i
] = img
;
1828 /* Call FN on every image in the image cache of frame F. Used to mark
1829 Lisp Objects in the image cache. */
1831 /* Mark Lisp objects in image IMG. */
1834 mark_image (struct image
*img
)
1836 mark_object (img
->spec
);
1837 mark_object (img
->dependencies
);
1839 if (!NILP (img
->lisp_data
))
1840 mark_object (img
->lisp_data
);
1845 mark_image_cache (struct image_cache
*c
)
1850 for (i
= 0; i
< c
->used
; ++i
)
1852 mark_image (c
->images
[i
]);
1858 /***********************************************************************
1859 X / NS / W32 support code
1860 ***********************************************************************/
1864 /* Macro for defining functions that will be loaded from image DLLs. */
1865 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1867 /* Macro for loading those image functions from the library. */
1868 #define LOAD_IMGLIB_FN(lib,func) { \
1869 fn_##func = (void *) GetProcAddress (lib, #func); \
1870 if (!fn_##func) return 0; \
1873 #endif /* HAVE_NTGUI */
1875 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1876 XImagePtr
*, Pixmap
*);
1877 static void x_destroy_x_image (XImagePtr
);
1878 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1880 /* Return nonzero if XIMG's size WIDTH x HEIGHT doesn't break the
1882 WIDTH and HEIGHT must both be positive.
1883 If XIMG is null, assume it is a bitmap. */
1885 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1887 #ifdef HAVE_X_WINDOWS
1888 /* Respect Xlib's limits: it cannot deal with images that have more
1889 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1890 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1893 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1894 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1897 int bitmap_pad
, depth
, bytes_per_line
;
1900 bitmap_pad
= ximg
->bitmap_pad
;
1901 depth
= ximg
->depth
;
1902 bytes_per_line
= ximg
->bytes_per_line
;
1908 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1910 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1911 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1913 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1914 For now, assume that every image size is allowed on these systems. */
1919 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1920 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1921 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1922 via xmalloc. Print error messages via image_error if an error
1923 occurs. Value is non-zero if successful.
1925 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1926 should indicate the bit depth of the image. */
1929 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1930 XImagePtr
*ximg
, Pixmap
*pixmap
)
1932 #ifdef HAVE_X_WINDOWS
1933 Display
*display
= FRAME_X_DISPLAY (f
);
1934 Window window
= FRAME_X_WINDOW (f
);
1935 Screen
*screen
= FRAME_X_SCREEN (f
);
1937 eassert (interrupt_input_blocked
);
1940 depth
= DefaultDepthOfScreen (screen
);
1941 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1942 depth
, ZPixmap
, 0, NULL
, width
, height
,
1943 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1946 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1950 if (! x_check_image_size (*ximg
, width
, height
))
1952 x_destroy_x_image (*ximg
);
1954 image_error ("Image too large (%dx%d)",
1955 make_number (width
), make_number (height
));
1959 /* Allocate image raster. */
1960 (*ximg
)->data
= xmalloc ((*ximg
)->bytes_per_line
* height
);
1962 /* Allocate a pixmap of the same size. */
1963 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1964 if (*pixmap
== NO_PIXMAP
)
1966 x_destroy_x_image (*ximg
);
1968 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1973 #endif /* HAVE_X_WINDOWS */
1977 BITMAPINFOHEADER
*header
;
1979 int scanline_width_bits
;
1981 int palette_colors
= 0;
1986 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1987 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1989 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
1993 scanline_width_bits
= width
* depth
;
1994 remainder
= scanline_width_bits
% 32;
1997 scanline_width_bits
+= 32 - remainder
;
1999 /* Bitmaps with a depth less than 16 need a palette. */
2000 /* BITMAPINFO structure already contains the first RGBQUAD. */
2002 palette_colors
= 1 << (depth
- 1);
2004 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2006 header
= &(*ximg
)->info
.bmiHeader
;
2007 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2008 header
->biSize
= sizeof (*header
);
2009 header
->biWidth
= width
;
2010 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2011 header
->biPlanes
= 1;
2012 header
->biBitCount
= depth
;
2013 header
->biCompression
= BI_RGB
;
2014 header
->biClrUsed
= palette_colors
;
2016 /* TODO: fill in palette. */
2019 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2020 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2021 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2022 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2023 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2024 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2025 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2026 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2029 hdc
= get_frame_dc (f
);
2031 /* Create a DIBSection and raster array for the bitmap,
2032 and store its handle in *pixmap. */
2033 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2034 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2035 /* casting avoids a GCC warning */
2036 (void **)&((*ximg
)->data
), NULL
, 0);
2038 /* Realize display palette and garbage all frames. */
2039 release_frame_dc (f
, hdc
);
2041 if (*pixmap
== NULL
)
2043 DWORD err
= GetLastError ();
2044 Lisp_Object errcode
;
2045 /* All system errors are < 10000, so the following is safe. */
2046 XSETINT (errcode
, err
);
2047 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2048 x_destroy_x_image (*ximg
);
2054 #endif /* HAVE_NTGUI */
2057 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2061 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2070 /* Destroy XImage XIMG. Free XIMG->data. */
2073 x_destroy_x_image (XImagePtr ximg
)
2075 eassert (interrupt_input_blocked
);
2078 #ifdef HAVE_X_WINDOWS
2081 XDestroyImage (ximg
);
2082 #endif /* HAVE_X_WINDOWS */
2084 /* Data will be freed by DestroyObject. */
2087 #endif /* HAVE_NTGUI */
2089 ns_release_object (ximg
);
2090 #endif /* HAVE_NS */
2095 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2096 are width and height of both the image and pixmap. */
2099 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2101 #ifdef HAVE_X_WINDOWS
2104 eassert (interrupt_input_blocked
);
2105 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2106 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2107 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2108 #endif /* HAVE_X_WINDOWS */
2111 #if 0 /* I don't think this is necessary looking at where it is used. */
2112 HDC hdc
= get_frame_dc (f
);
2113 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2114 release_frame_dc (f
, hdc
);
2116 #endif /* HAVE_NTGUI */
2119 eassert (ximg
== pixmap
);
2120 ns_retain_object (ximg
);
2125 /***********************************************************************
2127 ***********************************************************************/
2129 /* Find image file FILE. Look in data-directory/images, then
2130 x-bitmap-file-path. Value is the encoded full name of the file
2131 found, or nil if not found. */
2134 x_find_image_file (Lisp_Object file
)
2136 Lisp_Object file_found
, search_path
;
2139 /* TODO I think this should use something like image-load-path
2140 instead. Unfortunately, that can contain non-string elements. */
2141 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2143 Vx_bitmap_file_path
);
2145 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2146 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2152 file_found
= ENCODE_FILE (file_found
);
2160 /* Read FILE into memory. Value is a pointer to a buffer allocated
2161 with xmalloc holding FILE's contents. Value is null if an error
2162 occurred. *SIZE is set to the size of the file. */
2164 static unsigned char *
2165 slurp_file (char *file
, ptrdiff_t *size
)
2168 unsigned char *buf
= NULL
;
2171 if (stat (file
, &st
) == 0
2172 && (fp
= fopen (file
, "rb")) != NULL
2173 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
2174 && (buf
= xmalloc (st
.st_size
),
2175 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2196 /***********************************************************************
2198 ***********************************************************************/
2200 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2201 static int xbm_load (struct frame
*f
, struct image
*img
);
2202 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2203 unsigned char *, unsigned char *);
2204 static int xbm_image_p (Lisp_Object object
);
2205 static int xbm_read_bitmap_data (struct frame
*f
,
2206 unsigned char *, unsigned char *,
2207 int *, int *, char **, int);
2208 static int xbm_file_p (Lisp_Object
);
2211 /* Indices of image specification fields in xbm_format, below. */
2213 enum xbm_keyword_index
2231 /* Vector of image_keyword structures describing the format
2232 of valid XBM image specifications. */
2234 static const struct image_keyword xbm_format
[XBM_LAST
] =
2236 {":type", IMAGE_SYMBOL_VALUE
, 1},
2237 {":file", IMAGE_STRING_VALUE
, 0},
2238 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2239 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2240 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2241 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2242 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2243 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2244 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
2245 {":relief", IMAGE_INTEGER_VALUE
, 0},
2246 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2247 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2248 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2251 /* Structure describing the image type XBM. */
2253 static struct image_type xbm_type
=
2262 /* Tokens returned from xbm_scan. */
2271 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2272 A valid specification is a list starting with the symbol `image'
2273 The rest of the list is a property list which must contain an
2276 If the specification specifies a file to load, it must contain
2277 an entry `:file FILENAME' where FILENAME is a string.
2279 If the specification is for a bitmap loaded from memory it must
2280 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2281 WIDTH and HEIGHT are integers > 0. DATA may be:
2283 1. a string large enough to hold the bitmap data, i.e. it must
2284 have a size >= (WIDTH + 7) / 8 * HEIGHT
2286 2. a bool-vector of size >= WIDTH * HEIGHT
2288 3. a vector of strings or bool-vectors, one for each line of the
2291 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2292 may not be specified in this case because they are defined in the
2295 Both the file and data forms may contain the additional entries
2296 `:background COLOR' and `:foreground COLOR'. If not present,
2297 foreground and background of the frame on which the image is
2298 displayed is used. */
2301 xbm_image_p (Lisp_Object object
)
2303 struct image_keyword kw
[XBM_LAST
];
2305 memcpy (kw
, xbm_format
, sizeof kw
);
2306 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2309 eassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2311 if (kw
[XBM_FILE
].count
)
2313 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2316 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2318 /* In-memory XBM file. */
2319 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2327 /* Entries for `:width', `:height' and `:data' must be present. */
2328 if (!kw
[XBM_WIDTH
].count
2329 || !kw
[XBM_HEIGHT
].count
2330 || !kw
[XBM_DATA
].count
)
2333 data
= kw
[XBM_DATA
].value
;
2334 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2335 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2337 /* Check type of data, and width and height against contents of
2343 /* Number of elements of the vector must be >= height. */
2344 if (ASIZE (data
) < height
)
2347 /* Each string or bool-vector in data must be large enough
2348 for one line of the image. */
2349 for (i
= 0; i
< height
; ++i
)
2351 Lisp_Object elt
= AREF (data
, i
);
2356 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2359 else if (BOOL_VECTOR_P (elt
))
2361 if (XBOOL_VECTOR (elt
)->size
< width
)
2368 else if (STRINGP (data
))
2371 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2374 else if (BOOL_VECTOR_P (data
))
2376 if (XBOOL_VECTOR (data
)->size
/ height
< width
)
2387 /* Scan a bitmap file. FP is the stream to read from. Value is
2388 either an enumerator from enum xbm_token, or a character for a
2389 single-character token, or 0 at end of file. If scanning an
2390 identifier, store the lexeme of the identifier in SVAL. If
2391 scanning a number, store its value in *IVAL. */
2394 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2400 /* Skip white space. */
2401 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
2406 else if (c_isdigit (c
))
2408 int value
= 0, digit
;
2410 if (c
== '0' && *s
< end
)
2413 if (c
== 'x' || c
== 'X')
2420 else if (c
>= 'a' && c
<= 'f')
2421 digit
= c
- 'a' + 10;
2422 else if (c
>= 'A' && c
<= 'F')
2423 digit
= c
- 'A' + 10;
2426 value
= 16 * value
+ digit
;
2429 else if (c_isdigit (c
))
2433 && (c
= *(*s
)++, c_isdigit (c
)))
2434 value
= 8 * value
+ c
- '0';
2441 && (c
= *(*s
)++, c_isdigit (c
)))
2442 value
= 10 * value
+ c
- '0';
2450 else if (c_isalpha (c
) || c
== '_')
2454 && (c
= *(*s
)++, (c_isalnum (c
) || c
== '_')))
2461 else if (c
== '/' && **s
== '*')
2463 /* C-style comment. */
2465 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2479 /* Create a Windows bitmap from X bitmap data. */
2481 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2483 static unsigned char swap_nibble
[16]
2484 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2485 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2486 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2487 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2489 unsigned char *bits
, *p
;
2492 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2493 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2494 bits
= alloca (height
* w2
);
2495 memset (bits
, 0, height
* w2
);
2496 for (i
= 0; i
< height
; i
++)
2499 for (j
= 0; j
< w1
; j
++)
2501 /* Bitswap XBM bytes to match how Windows does things. */
2502 unsigned char c
= *data
++;
2503 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2504 | (swap_nibble
[(c
>>4) & 0xf]));
2507 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2513 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2514 COLORREF foreground
, COLORREF background
)
2516 HDC hdc
, old_img_dc
, new_img_dc
;
2517 HGDIOBJ old_prev
, new_prev
;
2520 hdc
= get_frame_dc (f
);
2521 old_img_dc
= CreateCompatibleDC (hdc
);
2522 new_img_dc
= CreateCompatibleDC (hdc
);
2523 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2524 release_frame_dc (f
, hdc
);
2525 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2526 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2527 /* Windows convention for mono bitmaps is black = background,
2528 white = foreground. */
2529 SetTextColor (new_img_dc
, background
);
2530 SetBkColor (new_img_dc
, foreground
);
2532 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2535 SelectObject (old_img_dc
, old_prev
);
2536 SelectObject (new_img_dc
, new_prev
);
2537 DeleteDC (old_img_dc
);
2538 DeleteDC (new_img_dc
);
2539 DeleteObject (img
->pixmap
);
2540 if (new_pixmap
== 0)
2541 fprintf (stderr
, "Failed to convert image to color.\n");
2543 img
->pixmap
= new_pixmap
;
2546 #define XBM_BIT_SHUFFLE(b) (~(b))
2550 #define XBM_BIT_SHUFFLE(b) (b)
2552 #endif /* HAVE_NTGUI */
2556 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2557 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2558 int non_default_colors
)
2562 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2564 /* If colors were specified, transfer the bitmap to a color one. */
2565 if (non_default_colors
)
2566 convert_mono_to_color_image (f
, img
, fg
, bg
);
2568 #elif defined (HAVE_NS)
2569 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2573 (x_check_image_size (0, img
->width
, img
->height
)
2574 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2577 img
->width
, img
->height
,
2579 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2581 #endif /* !HAVE_NTGUI && !HAVE_NS */
2586 /* Replacement for XReadBitmapFileData which isn't available under old
2587 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2588 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2589 the image. Return in *DATA the bitmap data allocated with xmalloc.
2590 Value is non-zero if successful. DATA null means just test if
2591 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2592 is non-zero, inhibit the call to image_error when the image size is
2593 invalid (the bitmap remains unread). */
2596 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2597 int *width
, int *height
, char **data
,
2598 int inhibit_image_error
)
2600 unsigned char *s
= contents
;
2601 char buffer
[BUFSIZ
];
2604 int bytes_per_line
, i
, nbytes
;
2610 LA1 = xbm_scan (&s, end, buffer, &value)
2612 #define expect(TOKEN) \
2613 if (LA1 != (TOKEN)) \
2618 #define expect_ident(IDENT) \
2619 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2624 *width
= *height
= -1;
2627 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2629 /* Parse defines for width, height and hot-spots. */
2633 expect_ident ("define");
2634 expect (XBM_TK_IDENT
);
2636 if (LA1
== XBM_TK_NUMBER
)
2638 char *q
= strrchr (buffer
, '_');
2639 q
= q
? q
+ 1 : buffer
;
2640 if (strcmp (q
, "width") == 0)
2642 else if (strcmp (q
, "height") == 0)
2645 expect (XBM_TK_NUMBER
);
2648 if (!check_image_size (f
, *width
, *height
))
2650 if (!inhibit_image_error
)
2651 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2654 else if (data
== NULL
)
2657 /* Parse bits. Must start with `static'. */
2658 expect_ident ("static");
2659 if (LA1
== XBM_TK_IDENT
)
2661 if (strcmp (buffer
, "unsigned") == 0)
2664 expect_ident ("char");
2666 else if (strcmp (buffer
, "short") == 0)
2670 if (*width
% 16 && *width
% 16 < 9)
2673 else if (strcmp (buffer
, "char") == 0)
2681 expect (XBM_TK_IDENT
);
2687 if (! x_check_image_size (0, *width
, *height
))
2689 if (!inhibit_image_error
)
2690 image_error ("Image too large (%dx%d)",
2691 make_number (*width
), make_number (*height
));
2694 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2695 nbytes
= bytes_per_line
* *height
;
2696 p
= *data
= xmalloc (nbytes
);
2700 for (i
= 0; i
< nbytes
; i
+= 2)
2703 expect (XBM_TK_NUMBER
);
2705 *p
++ = XBM_BIT_SHUFFLE (val
);
2706 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2707 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2709 if (LA1
== ',' || LA1
== '}')
2717 for (i
= 0; i
< nbytes
; ++i
)
2720 expect (XBM_TK_NUMBER
);
2722 *p
++ = XBM_BIT_SHUFFLE (val
);
2724 if (LA1
== ',' || LA1
== '}')
2749 /* Load XBM image IMG which will be displayed on frame F from buffer
2750 CONTENTS. END is the end of the buffer. Value is non-zero if
2754 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2761 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2765 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2766 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2767 int non_default_colors
= 0;
2770 eassert (img
->width
> 0 && img
->height
> 0);
2772 /* Get foreground and background colors, maybe allocate colors. */
2773 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2776 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2777 non_default_colors
= 1;
2779 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2782 background
= x_alloc_image_color (f
, img
, value
, background
);
2783 img
->background
= background
;
2784 img
->background_valid
= 1;
2785 non_default_colors
= 1;
2788 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2789 foreground
, background
,
2790 non_default_colors
);
2793 if (img
->pixmap
== NO_PIXMAP
)
2795 x_clear_image (f
, img
);
2796 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2802 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2808 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2811 xbm_file_p (Lisp_Object data
)
2814 return (STRINGP (data
)
2815 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2816 (SDATA (data
) + SBYTES (data
)),
2821 /* Fill image IMG which is used on frame F with pixmap data. Value is
2822 non-zero if successful. */
2825 xbm_load (struct frame
*f
, struct image
*img
)
2828 Lisp_Object file_name
;
2830 eassert (xbm_image_p (img
->spec
));
2832 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2833 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2834 if (STRINGP (file_name
))
2837 unsigned char *contents
;
2840 file
= x_find_image_file (file_name
);
2841 if (!STRINGP (file
))
2843 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2847 contents
= slurp_file (SSDATA (file
), &size
);
2848 if (contents
== NULL
)
2850 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2854 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2859 struct image_keyword fmt
[XBM_LAST
];
2861 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2862 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2863 int non_default_colors
= 0;
2866 int in_memory_file_p
= 0;
2868 /* See if data looks like an in-memory XBM file. */
2869 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2870 in_memory_file_p
= xbm_file_p (data
);
2872 /* Parse the image specification. */
2873 memcpy (fmt
, xbm_format
, sizeof fmt
);
2874 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2878 /* Get specified width, and height. */
2879 if (!in_memory_file_p
)
2881 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2882 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2883 eassert (img
->width
> 0 && img
->height
> 0);
2884 if (!check_image_size (f
, img
->width
, img
->height
))
2886 image_error ("Invalid image size (see `max-image-size')",
2892 /* Get foreground and background colors, maybe allocate colors. */
2893 if (fmt
[XBM_FOREGROUND
].count
2894 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2896 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2898 non_default_colors
= 1;
2901 if (fmt
[XBM_BACKGROUND
].count
2902 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2904 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2906 non_default_colors
= 1;
2909 if (in_memory_file_p
)
2910 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2919 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2921 p
= bits
= alloca (nbytes
* img
->height
);
2922 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2924 Lisp_Object line
= AREF (data
, i
);
2926 memcpy (p
, SDATA (line
), nbytes
);
2928 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2931 else if (STRINGP (data
))
2932 bits
= SSDATA (data
);
2934 bits
= (char *) XBOOL_VECTOR (data
)->data
;
2940 /* Windows mono bitmaps are reversed compared with X. */
2941 invertedBits
= bits
;
2942 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2944 bits
= alloca (nbytes
);
2945 for (i
= 0; i
< nbytes
; i
++)
2946 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2949 /* Create the pixmap. */
2951 if (x_check_image_size (0, img
->width
, img
->height
))
2952 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2953 foreground
, background
,
2954 non_default_colors
);
2956 img
->pixmap
= NO_PIXMAP
;
2962 image_error ("Unable to create pixmap for XBM image `%s'",
2964 x_clear_image (f
, img
);
2974 /***********************************************************************
2976 ***********************************************************************/
2978 #if defined (HAVE_XPM) || defined (HAVE_NS)
2980 static int xpm_image_p (Lisp_Object object
);
2981 static int xpm_load (struct frame
*f
, struct image
*img
);
2982 static int xpm_valid_color_symbols_p (Lisp_Object
);
2984 #endif /* HAVE_XPM || HAVE_NS */
2988 /* Indicate to xpm.h that we don't have Xlib. */
2990 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
2991 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
2992 #define XColor xpm_XColor
2993 #define XImage xpm_XImage
2994 #define Display xpm_Display
2995 #define PIXEL_ALREADY_TYPEDEFED
2996 #include "X11/xpm.h"
3001 #undef PIXEL_ALREADY_TYPEDEFED
3003 #include "X11/xpm.h"
3004 #endif /* HAVE_NTGUI */
3005 #endif /* HAVE_XPM */
3007 #if defined (HAVE_XPM) || defined (HAVE_NS)
3008 /* The symbol `xpm' identifying XPM-format images. */
3010 static Lisp_Object Qxpm
;
3012 /* Indices of image specification fields in xpm_format, below. */
3014 enum xpm_keyword_index
3030 /* Vector of image_keyword structures describing the format
3031 of valid XPM image specifications. */
3033 static const struct image_keyword xpm_format
[XPM_LAST
] =
3035 {":type", IMAGE_SYMBOL_VALUE
, 1},
3036 {":file", IMAGE_STRING_VALUE
, 0},
3037 {":data", IMAGE_STRING_VALUE
, 0},
3038 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3039 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
3040 {":relief", IMAGE_INTEGER_VALUE
, 0},
3041 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3042 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3043 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3044 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3045 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3048 /* Structure describing the image type XPM. */
3050 static struct image_type xpm_type
=
3059 #ifdef HAVE_X_WINDOWS
3061 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3062 functions for allocating image colors. Our own functions handle
3063 color allocation failures more gracefully than the ones on the XPM
3066 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3067 #define ALLOC_XPM_COLORS
3069 #endif /* HAVE_X_WINDOWS */
3071 #ifdef ALLOC_XPM_COLORS
3073 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3074 static void xpm_free_color_cache (void);
3075 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3076 static int xpm_color_bucket (char *);
3077 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3080 /* An entry in a hash table used to cache color definitions of named
3081 colors. This cache is necessary to speed up XPM image loading in
3082 case we do color allocations ourselves. Without it, we would need
3083 a call to XParseColor per pixel in the image. */
3085 struct xpm_cached_color
3087 /* Next in collision chain. */
3088 struct xpm_cached_color
*next
;
3090 /* Color definition (RGB and pixel color). */
3097 /* The hash table used for the color cache, and its bucket vector
3100 #define XPM_COLOR_CACHE_BUCKETS 1001
3101 static struct xpm_cached_color
**xpm_color_cache
;
3103 /* Initialize the color cache. */
3106 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3108 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3109 xpm_color_cache
= xzalloc (nbytes
);
3110 init_color_table ();
3112 if (attrs
->valuemask
& XpmColorSymbols
)
3117 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3118 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3119 attrs
->colorsymbols
[i
].value
, &color
))
3121 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3123 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3128 /* Free the color cache. */
3131 xpm_free_color_cache (void)
3133 struct xpm_cached_color
*p
, *next
;
3136 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3137 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3143 xfree (xpm_color_cache
);
3144 xpm_color_cache
= NULL
;
3145 free_color_table ();
3148 /* Return the bucket index for color named COLOR_NAME in the color
3152 xpm_color_bucket (char *color_name
)
3154 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3155 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3159 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3160 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3163 static struct xpm_cached_color
*
3164 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3167 struct xpm_cached_color
*p
;
3170 bucket
= xpm_color_bucket (color_name
);
3172 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3173 p
= xmalloc (nbytes
);
3174 strcpy (p
->name
, color_name
);
3176 p
->next
= xpm_color_cache
[bucket
];
3177 xpm_color_cache
[bucket
] = p
;
3181 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3182 return the cached definition in *COLOR. Otherwise, make a new
3183 entry in the cache and allocate the color. Value is zero if color
3184 allocation failed. */
3187 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3189 struct xpm_cached_color
*p
;
3190 int h
= xpm_color_bucket (color_name
);
3192 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3193 if (strcmp (p
->name
, color_name
) == 0)
3198 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3201 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3203 p
= xpm_cache_color (f
, color_name
, color
, h
);
3205 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3206 with transparency, and it's useful. */
3207 else if (strcmp ("opaque", color_name
) == 0)
3209 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3210 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3211 p
= xpm_cache_color (f
, color_name
, color
, h
);
3218 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3219 CLOSURE is a pointer to the frame on which we allocate the
3220 color. Return in *COLOR the allocated color. Value is non-zero
3224 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3227 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3231 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3232 is a pointer to the frame on which we allocate the color. Value is
3233 non-zero if successful. */
3236 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3241 #endif /* ALLOC_XPM_COLORS */
3246 /* XPM library details. */
3248 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3249 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3250 xpm_XImage
**, XpmAttributes
*));
3251 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3252 xpm_XImage
**, XpmAttributes
*));
3253 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3256 init_xpm_functions (Lisp_Object libraries
)
3260 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3263 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3264 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3265 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3266 LOAD_IMGLIB_FN (library
, XImageFree
);
3270 #endif /* HAVE_NTGUI */
3273 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3274 for XPM images. Such a list must consist of conses whose car and
3278 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3280 while (CONSP (color_symbols
))
3282 Lisp_Object sym
= XCAR (color_symbols
);
3284 || !STRINGP (XCAR (sym
))
3285 || !STRINGP (XCDR (sym
)))
3287 color_symbols
= XCDR (color_symbols
);
3290 return NILP (color_symbols
);
3294 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3297 xpm_image_p (Lisp_Object object
)
3299 struct image_keyword fmt
[XPM_LAST
];
3300 memcpy (fmt
, xpm_format
, sizeof fmt
);
3301 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3302 /* Either `:file' or `:data' must be present. */
3303 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3304 /* Either no `:color-symbols' or it's a list of conses
3305 whose car and cdr are strings. */
3306 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3307 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3310 #endif /* HAVE_XPM || HAVE_NS */
3312 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3314 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3316 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3319 XpmAttributes attrs
;
3320 Pixmap bitmap
, mask
;
3322 memset (&attrs
, 0, sizeof attrs
);
3324 attrs
.visual
= FRAME_X_VISUAL (f
);
3325 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3326 attrs
.valuemask
|= XpmVisual
;
3327 attrs
.valuemask
|= XpmColormap
;
3329 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3330 (char **) bits
, &bitmap
, &mask
, &attrs
);
3331 if (rc
!= XpmSuccess
)
3333 XpmFreeAttributes (&attrs
);
3337 id
= x_allocate_bitmap_record (f
);
3338 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3339 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3340 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3341 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3342 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3343 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3344 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3345 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3347 XpmFreeAttributes (&attrs
);
3350 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3352 /* Load image IMG which will be displayed on frame F. Value is
3353 non-zero if successful. */
3358 xpm_load (struct frame
*f
, struct image
*img
)
3361 XpmAttributes attrs
;
3362 Lisp_Object specified_file
, color_symbols
;
3365 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3366 #endif /* HAVE_NTGUI */
3368 /* Configure the XPM lib. Use the visual of frame F. Allocate
3369 close colors. Return colors allocated. */
3370 memset (&attrs
, 0, sizeof attrs
);
3373 attrs
.visual
= FRAME_X_VISUAL (f
);
3374 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3375 attrs
.valuemask
|= XpmVisual
;
3376 attrs
.valuemask
|= XpmColormap
;
3377 #endif /* HAVE_NTGUI */
3379 #ifdef ALLOC_XPM_COLORS
3380 /* Allocate colors with our own functions which handle
3381 failing color allocation more gracefully. */
3382 attrs
.color_closure
= f
;
3383 attrs
.alloc_color
= xpm_alloc_color
;
3384 attrs
.free_colors
= xpm_free_colors
;
3385 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3386 #else /* not ALLOC_XPM_COLORS */
3387 /* Let the XPM lib allocate colors. */
3388 attrs
.valuemask
|= XpmReturnAllocPixels
;
3389 #ifdef XpmAllocCloseColors
3390 attrs
.alloc_close_colors
= 1;
3391 attrs
.valuemask
|= XpmAllocCloseColors
;
3392 #else /* not XpmAllocCloseColors */
3393 attrs
.closeness
= 600;
3394 attrs
.valuemask
|= XpmCloseness
;
3395 #endif /* not XpmAllocCloseColors */
3396 #endif /* ALLOC_XPM_COLORS */
3398 /* If image specification contains symbolic color definitions, add
3399 these to `attrs'. */
3400 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3401 if (CONSP (color_symbols
))
3404 XpmColorSymbol
*xpm_syms
;
3407 attrs
.valuemask
|= XpmColorSymbols
;
3409 /* Count number of symbols. */
3410 attrs
.numsymbols
= 0;
3411 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3414 /* Allocate an XpmColorSymbol array. */
3415 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3416 xpm_syms
= alloca (size
);
3417 memset (xpm_syms
, 0, size
);
3418 attrs
.colorsymbols
= xpm_syms
;
3420 /* Fill the color symbol array. */
3421 for (tail
= color_symbols
, i
= 0;
3423 ++i
, tail
= XCDR (tail
))
3427 char *empty_string
= (char *) "";
3429 if (!CONSP (XCAR (tail
)))
3431 xpm_syms
[i
].name
= empty_string
;
3432 xpm_syms
[i
].value
= empty_string
;
3435 name
= XCAR (XCAR (tail
));
3436 color
= XCDR (XCAR (tail
));
3439 xpm_syms
[i
].name
= alloca (SCHARS (name
) + 1);
3440 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3443 xpm_syms
[i
].name
= empty_string
;
3444 if (STRINGP (color
))
3446 xpm_syms
[i
].value
= alloca (SCHARS (color
) + 1);
3447 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3450 xpm_syms
[i
].value
= empty_string
;
3454 /* Create a pixmap for the image, either from a file, or from a
3455 string buffer containing data in the same format as an XPM file. */
3456 #ifdef ALLOC_XPM_COLORS
3457 xpm_init_color_cache (f
, &attrs
);
3460 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3464 HDC frame_dc
= get_frame_dc (f
);
3465 hdc
= CreateCompatibleDC (frame_dc
);
3466 release_frame_dc (f
, frame_dc
);
3468 #endif /* HAVE_NTGUI */
3470 if (STRINGP (specified_file
))
3472 Lisp_Object file
= x_find_image_file (specified_file
);
3473 if (!STRINGP (file
))
3475 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3476 #ifdef ALLOC_XPM_COLORS
3477 xpm_free_color_cache ();
3483 /* XpmReadFileToPixmap is not available in the Windows port of
3484 libxpm. But XpmReadFileToImage almost does what we want. */
3485 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3486 &xpm_image
, &xpm_mask
,
3489 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3490 SSDATA (file
), &img
->pixmap
, &img
->mask
,
3492 #endif /* HAVE_NTGUI */
3496 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3497 if (!STRINGP (buffer
))
3499 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3500 #ifdef ALLOC_XPM_COLORS
3501 xpm_free_color_cache ();
3506 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3507 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3508 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3509 &xpm_image
, &xpm_mask
,
3512 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3514 &img
->pixmap
, &img
->mask
,
3516 #endif /* HAVE_NTGUI */
3519 if (rc
== XpmSuccess
)
3521 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3522 img
->colors
= colors_in_color_table (&img
->ncolors
);
3523 #else /* not ALLOC_XPM_COLORS */
3527 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3528 plus some duplicate attributes. */
3529 if (xpm_image
&& xpm_image
->bitmap
)
3531 img
->pixmap
= xpm_image
->bitmap
;
3532 /* XImageFree in libXpm frees XImage struct without destroying
3533 the bitmap, which is what we want. */
3534 fn_XImageFree (xpm_image
);
3536 if (xpm_mask
&& xpm_mask
->bitmap
)
3538 /* The mask appears to be inverted compared with what we expect.
3539 TODO: invert our expectations. See other places where we
3540 have to invert bits because our idea of masks is backwards. */
3542 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3544 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3545 SelectObject (hdc
, old_obj
);
3547 img
->mask
= xpm_mask
->bitmap
;
3548 fn_XImageFree (xpm_mask
);
3553 #endif /* HAVE_NTGUI */
3555 /* Remember allocated colors. */
3556 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3557 img
->ncolors
= attrs
.nalloc_pixels
;
3558 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3560 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3561 #ifdef DEBUG_X_COLORS
3562 register_color (img
->colors
[i
]);
3565 #endif /* not ALLOC_XPM_COLORS */
3567 img
->width
= attrs
.width
;
3568 img
->height
= attrs
.height
;
3569 eassert (img
->width
> 0 && img
->height
> 0);
3571 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3573 fn_XpmFreeAttributes (&attrs
);
3575 XpmFreeAttributes (&attrs
);
3576 #endif /* HAVE_NTGUI */
3582 #endif /* HAVE_NTGUI */
3587 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3590 case XpmFileInvalid
:
3591 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3595 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3598 case XpmColorFailed
:
3599 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3603 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3608 #ifdef ALLOC_XPM_COLORS
3609 xpm_free_color_cache ();
3611 return rc
== XpmSuccess
;
3614 #endif /* HAVE_XPM */
3616 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3618 /* XPM support functions for NS where libxpm is not available.
3619 Only XPM version 3 (without any extensions) is supported. */
3621 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3623 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3624 const unsigned char *, int);
3625 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3627 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3628 const unsigned char *, int);
3630 /* Tokens returned from xpm_scan. */
3639 /* Scan an XPM data and return a character (< 256) or a token defined
3640 by enum xpm_token above. *S and END are the start (inclusive) and
3641 the end (exclusive) addresses of the data, respectively. Advance
3642 *S while scanning. If token is either XPM_TK_IDENT or
3643 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3644 length of the corresponding token, respectively. */
3647 xpm_scan (const unsigned char **s
,
3648 const unsigned char *end
,
3649 const unsigned char **beg
,
3656 /* Skip white-space. */
3657 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
3660 /* gnus-pointer.xpm uses '-' in its identifier.
3661 sb-dir-plus.xpm uses '+' in its identifier. */
3662 if (c_isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3666 && (c
= **s
, c_isalnum (c
)
3667 || c
== '_' || c
== '-' || c
== '+'))
3670 return XPM_TK_IDENT
;
3675 while (*s
< end
&& **s
!= '"')
3680 return XPM_TK_STRING
;
3684 if (*s
< end
&& **s
== '*')
3686 /* C-style comment. */
3690 while (*s
< end
&& *(*s
)++ != '*')
3693 while (*s
< end
&& **s
!= '/');
3707 /* Functions for color table lookup in XPM data. A key is a string
3708 specifying the color of each pixel in XPM data. A value is either
3709 an integer that specifies a pixel color, Qt that specifies
3710 transparency, or Qnil for the unspecified color. If the length of
3711 the key string is one, a vector is used as a table. Otherwise, a
3712 hash table is used. */
3715 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3716 const unsigned char *,
3719 Lisp_Object (**get_func
) (Lisp_Object
,
3720 const unsigned char *,
3723 *put_func
= xpm_put_color_table_v
;
3724 *get_func
= xpm_get_color_table_v
;
3725 return Fmake_vector (make_number (256), Qnil
);
3729 xpm_put_color_table_v (Lisp_Object color_table
,
3730 const unsigned char *chars_start
,
3734 ASET (color_table
, *chars_start
, color
);
3738 xpm_get_color_table_v (Lisp_Object color_table
,
3739 const unsigned char *chars_start
,
3742 return AREF (color_table
, *chars_start
);
3746 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3747 const unsigned char *,
3750 Lisp_Object (**get_func
) (Lisp_Object
,
3751 const unsigned char *,
3754 *put_func
= xpm_put_color_table_h
;
3755 *get_func
= xpm_get_color_table_h
;
3756 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3757 make_float (DEFAULT_REHASH_SIZE
),
3758 make_float (DEFAULT_REHASH_THRESHOLD
),
3763 xpm_put_color_table_h (Lisp_Object color_table
,
3764 const unsigned char *chars_start
,
3768 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3769 EMACS_UINT hash_code
;
3770 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3772 hash_lookup (table
, chars
, &hash_code
);
3773 hash_put (table
, chars
, color
, hash_code
);
3777 xpm_get_color_table_h (Lisp_Object color_table
,
3778 const unsigned char *chars_start
,
3781 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3783 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3785 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3788 enum xpm_color_key
{
3796 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3799 xpm_str_to_color_key (const char *s
)
3804 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3806 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3812 xpm_load_image (struct frame
*f
,
3814 const unsigned char *contents
,
3815 const unsigned char *end
)
3817 const unsigned char *s
= contents
, *beg
, *str
;
3818 unsigned char buffer
[BUFSIZ
];
3819 int width
, height
, x
, y
;
3820 int num_colors
, chars_per_pixel
;
3823 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3824 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3825 Lisp_Object frame
, color_symbols
, color_table
;
3826 int best_key
, have_mask
= 0;
3827 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3830 LA1 = xpm_scan (&s, end, &beg, &len)
3832 #define expect(TOKEN) \
3833 if (LA1 != (TOKEN)) \
3838 #define expect_ident(IDENT) \
3839 if (LA1 == XPM_TK_IDENT \
3840 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3845 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3849 expect_ident ("static");
3850 expect_ident ("char");
3852 expect (XPM_TK_IDENT
);
3857 expect (XPM_TK_STRING
);
3860 memcpy (buffer
, beg
, len
);
3862 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3863 &num_colors
, &chars_per_pixel
) != 4
3864 || width
<= 0 || height
<= 0
3865 || num_colors
<= 0 || chars_per_pixel
<= 0)
3868 if (!check_image_size (f
, width
, height
))
3870 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3874 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3875 &ximg
, &img
->pixmap
)
3877 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3878 &mask_img
, &img
->mask
)
3882 image_error ("Image too large", Qnil
, Qnil
);
3888 XSETFRAME (frame
, f
);
3889 if (!NILP (Fxw_display_color_p (frame
)))
3890 best_key
= XPM_COLOR_KEY_C
;
3891 else if (!NILP (Fx_display_grayscale_p (frame
)))
3892 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3893 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3895 best_key
= XPM_COLOR_KEY_M
;
3897 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3898 if (chars_per_pixel
== 1)
3899 color_table
= xpm_make_color_table_v (&put_color_table
,
3902 color_table
= xpm_make_color_table_h (&put_color_table
,
3905 while (num_colors
-- > 0)
3907 char *color
, *max_color
;
3908 int key
, next_key
, max_key
= 0;
3909 Lisp_Object symbol_color
= Qnil
, color_val
;
3912 expect (XPM_TK_STRING
);
3913 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3915 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3916 buffer
[len
- chars_per_pixel
] = '\0';
3918 str
= strtok (buffer
, " \t");
3921 key
= xpm_str_to_color_key (str
);
3926 color
= strtok (NULL
, " \t");
3930 while ((str
= strtok (NULL
, " \t")) != NULL
)
3932 next_key
= xpm_str_to_color_key (str
);
3935 color
[strlen (color
)] = ' ';
3938 if (key
== XPM_COLOR_KEY_S
)
3940 if (NILP (symbol_color
))
3941 symbol_color
= build_string (color
);
3943 else if (max_key
< key
&& key
<= best_key
)
3953 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3955 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3957 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3959 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
3961 else if (x_defined_color (f
, SSDATA (XCDR (specified_color
)),
3963 color_val
= make_number (cdef
.pixel
);
3966 if (NILP (color_val
) && max_key
> 0)
3968 if (xstrcasecmp (max_color
, "None") == 0)
3970 else if (x_defined_color (f
, max_color
, &cdef
, 0))
3971 color_val
= make_number (cdef
.pixel
);
3973 if (!NILP (color_val
))
3974 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
3979 for (y
= 0; y
< height
; y
++)
3981 expect (XPM_TK_STRING
);
3983 if (len
< width
* chars_per_pixel
)
3985 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
3987 Lisp_Object color_val
=
3988 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
3990 XPutPixel (ximg
, x
, y
,
3991 (INTEGERP (color_val
) ? XINT (color_val
)
3992 : FRAME_FOREGROUND_PIXEL (f
)));
3994 XPutPixel (mask_img
, x
, y
,
3995 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
3996 : (have_mask
= 1, PIX_MASK_RETAIN
)));
3998 if (EQ (color_val
, Qt
))
3999 ns_set_alpha (ximg
, x
, y
, 0);
4007 img
->height
= height
;
4009 /* Maybe fill in the background field while we have ximg handy. */
4010 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4011 IMAGE_BACKGROUND (img
, f
, ximg
);
4013 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4014 x_destroy_x_image (ximg
);
4018 /* Fill in the background_transparent field while we have the
4020 image_background_transparent (img
, f
, mask_img
);
4022 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4023 x_destroy_x_image (mask_img
);
4027 x_destroy_x_image (mask_img
);
4028 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4029 img
->mask
= NO_PIXMAP
;
4035 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4036 x_destroy_x_image (ximg
);
4037 x_destroy_x_image (mask_img
);
4038 x_clear_image (f
, img
);
4047 xpm_load (struct frame
*f
,
4051 Lisp_Object file_name
;
4053 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4054 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4055 if (STRINGP (file_name
))
4058 unsigned char *contents
;
4061 file
= x_find_image_file (file_name
);
4062 if (!STRINGP (file
))
4064 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4068 contents
= slurp_file (SSDATA (file
), &size
);
4069 if (contents
== NULL
)
4071 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4075 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4082 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4083 if (!STRINGP (data
))
4085 image_error ("Invalid image data `%s'", data
, Qnil
);
4088 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4089 SDATA (data
) + SBYTES (data
));
4095 #endif /* HAVE_NS && !HAVE_XPM */
4099 /***********************************************************************
4101 ***********************************************************************/
4103 #ifdef COLOR_TABLE_SUPPORT
4105 /* An entry in the color table mapping an RGB color to a pixel color. */
4110 unsigned long pixel
;
4112 /* Next in color table collision list. */
4113 struct ct_color
*next
;
4116 /* The bucket vector size to use. Must be prime. */
4120 /* Value is a hash of the RGB color given by R, G, and B. */
4122 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4124 /* The color hash table. */
4126 static struct ct_color
**ct_table
;
4128 /* Number of entries in the color table. */
4130 static int ct_colors_allocated
;
4133 ct_colors_allocated_max
=
4135 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4138 /* Initialize the color table. */
4141 init_color_table (void)
4143 int size
= CT_SIZE
* sizeof (*ct_table
);
4144 ct_table
= xzalloc (size
);
4145 ct_colors_allocated
= 0;
4149 /* Free memory associated with the color table. */
4152 free_color_table (void)
4155 struct ct_color
*p
, *next
;
4157 for (i
= 0; i
< CT_SIZE
; ++i
)
4158 for (p
= ct_table
[i
]; p
; p
= next
)
4169 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4170 entry for that color already is in the color table, return the
4171 pixel color of that entry. Otherwise, allocate a new color for R,
4172 G, B, and make an entry in the color table. */
4174 static unsigned long
4175 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4177 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4178 int i
= hash
% CT_SIZE
;
4180 Display_Info
*dpyinfo
;
4182 /* Handle TrueColor visuals specially, which improves performance by
4183 two orders of magnitude. Freeing colors on TrueColor visuals is
4184 a nop, and pixel colors specify RGB values directly. See also
4185 the Xlib spec, chapter 3.1. */
4186 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4187 if (dpyinfo
->red_bits
> 0)
4189 unsigned long pr
, pg
, pb
;
4191 /* Apply gamma-correction like normal color allocation does. */
4195 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4196 gamma_correct (f
, &color
);
4197 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4200 /* Scale down RGB values to the visual's bits per RGB, and shift
4201 them to the right position in the pixel color. Note that the
4202 original RGB values are 16-bit values, as usual in X. */
4203 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4204 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4205 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4207 /* Assemble the pixel color. */
4208 return pr
| pg
| pb
;
4211 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4212 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4218 #ifdef HAVE_X_WINDOWS
4226 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4227 return FRAME_FOREGROUND_PIXEL (f
);
4229 #ifdef HAVE_X_WINDOWS
4234 cmap
= FRAME_X_COLORMAP (f
);
4235 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4238 ++ct_colors_allocated
;
4239 p
= xmalloc (sizeof *p
);
4243 p
->pixel
= color
.pixel
;
4244 p
->next
= ct_table
[i
];
4248 return FRAME_FOREGROUND_PIXEL (f
);
4252 color
= PALETTERGB (r
, g
, b
);
4254 color
= RGB_TO_ULONG (r
, g
, b
);
4255 #endif /* HAVE_NTGUI */
4256 ++ct_colors_allocated
;
4257 p
= xmalloc (sizeof *p
);
4262 p
->next
= ct_table
[i
];
4264 #endif /* HAVE_X_WINDOWS */
4272 /* Look up pixel color PIXEL which is used on frame F in the color
4273 table. If not already present, allocate it. Value is PIXEL. */
4275 static unsigned long
4276 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4278 int i
= pixel
% CT_SIZE
;
4281 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4282 if (p
->pixel
== pixel
)
4291 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4292 return FRAME_FOREGROUND_PIXEL (f
);
4294 #ifdef HAVE_X_WINDOWS
4295 cmap
= FRAME_X_COLORMAP (f
);
4296 color
.pixel
= pixel
;
4297 x_query_color (f
, &color
);
4298 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4301 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4302 color
.pixel
= pixel
;
4303 XQueryColor (NULL
, cmap
, &color
);
4304 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4306 #endif /* HAVE_X_WINDOWS */
4310 ++ct_colors_allocated
;
4312 p
= xmalloc (sizeof *p
);
4317 p
->next
= ct_table
[i
];
4321 return FRAME_FOREGROUND_PIXEL (f
);
4327 /* Value is a vector of all pixel colors contained in the color table,
4328 allocated via xmalloc. Set *N to the number of colors. */
4330 static unsigned long *
4331 colors_in_color_table (int *n
)
4335 unsigned long *colors
;
4337 if (ct_colors_allocated
== 0)
4344 colors
= xmalloc (ct_colors_allocated
* sizeof *colors
);
4345 *n
= ct_colors_allocated
;
4347 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4348 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4349 colors
[j
++] = p
->pixel
;
4355 #else /* COLOR_TABLE_SUPPORT */
4357 static unsigned long
4358 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4360 unsigned long pixel
;
4363 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4364 #endif /* HAVE_NTGUI */
4367 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4368 #endif /* HAVE_NS */
4373 init_color_table (void)
4376 #endif /* COLOR_TABLE_SUPPORT */
4379 /***********************************************************************
4381 ***********************************************************************/
4383 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4384 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4385 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4388 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4389 #endif /* HAVE_NTGUI */
4391 /* Edge detection matrices for different edge-detection
4394 static int emboss_matrix
[9] = {
4396 2, -1, 0, /* y - 1 */
4398 0, 1, -2 /* y + 1 */
4401 static int laplace_matrix
[9] = {
4403 1, 0, 0, /* y - 1 */
4405 0, 0, -1 /* y + 1 */
4408 /* Value is the intensity of the color whose red/green/blue values
4411 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4414 /* On frame F, return an array of XColor structures describing image
4415 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4416 non-zero means also fill the red/green/blue members of the XColor
4417 structures. Value is a pointer to the array of XColors structures,
4418 allocated with xmalloc; it must be freed by the caller. */
4421 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4425 XImagePtr_or_DC ximg
;
4429 #endif /* HAVE_NTGUI */
4431 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4432 memory_full (SIZE_MAX
);
4433 colors
= xmalloc (sizeof *colors
* img
->width
* img
->height
);
4436 /* Get the X image IMG->pixmap. */
4437 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4438 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4440 /* Load the image into a memory device context. */
4441 hdc
= get_frame_dc (f
);
4442 ximg
= CreateCompatibleDC (hdc
);
4443 release_frame_dc (f
, hdc
);
4444 prev
= SelectObject (ximg
, img
->pixmap
);
4445 #endif /* HAVE_NTGUI */
4447 /* Fill the `pixel' members of the XColor array. I wished there
4448 were an easy and portable way to circumvent XGetPixel. */
4450 for (y
= 0; y
< img
->height
; ++y
)
4452 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4454 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4455 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4457 x_query_colors (f
, row
, img
->width
);
4461 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4463 /* W32_TODO: palette support needed here? */
4464 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4467 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4468 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4469 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4472 #endif /* HAVE_X_WINDOWS */
4475 Destroy_Image (ximg
, prev
);
4482 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4483 created with CreateDIBSection, with the pointer to the bit values
4484 stored in ximg->data. */
4487 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4489 int width
= ximg
->info
.bmiHeader
.biWidth
;
4490 unsigned char * pixel
;
4492 /* True color images. */
4493 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4495 int rowbytes
= width
* 3;
4496 /* Ensure scanlines are aligned on 4 byte boundaries. */
4498 rowbytes
+= 4 - (rowbytes
% 4);
4500 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4501 /* Windows bitmaps are in BGR order. */
4502 *pixel
= GetBValue (color
);
4503 *(pixel
+ 1) = GetGValue (color
);
4504 *(pixel
+ 2) = GetRValue (color
);
4506 /* Monochrome images. */
4507 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4509 int rowbytes
= width
/ 8;
4510 /* Ensure scanlines are aligned on 4 byte boundaries. */
4512 rowbytes
+= 4 - (rowbytes
% 4);
4513 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4514 /* Filter out palette info. */
4515 if (color
& 0x00ffffff)
4516 *pixel
= *pixel
| (1 << x
% 8);
4518 *pixel
= *pixel
& ~(1 << x
% 8);
4521 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4524 #endif /* HAVE_NTGUI */
4526 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4527 RGB members are set. F is the frame on which this all happens.
4528 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4531 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4534 XImagePtr oimg
= NULL
;
4538 init_color_table ();
4540 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4543 for (y
= 0; y
< img
->height
; ++y
)
4544 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4546 unsigned long pixel
;
4547 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4548 XPutPixel (oimg
, x
, y
, pixel
);
4552 x_clear_image_1 (f
, img
, 1, 0, 1);
4554 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4555 x_destroy_x_image (oimg
);
4556 img
->pixmap
= pixmap
;
4557 #ifdef COLOR_TABLE_SUPPORT
4558 img
->colors
= colors_in_color_table (&img
->ncolors
);
4559 free_color_table ();
4560 #endif /* COLOR_TABLE_SUPPORT */
4564 /* On frame F, perform edge-detection on image IMG.
4566 MATRIX is a nine-element array specifying the transformation
4567 matrix. See emboss_matrix for an example.
4569 COLOR_ADJUST is a color adjustment added to each pixel of the
4573 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4575 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4579 for (i
= sum
= 0; i
< 9; ++i
)
4580 sum
+= eabs (matrix
[i
]);
4582 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4584 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4585 memory_full (SIZE_MAX
);
4586 new = xmalloc (sizeof *new * img
->width
* img
->height
);
4588 for (y
= 0; y
< img
->height
; ++y
)
4590 p
= COLOR (new, 0, y
);
4591 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4592 p
= COLOR (new, img
->width
- 1, y
);
4593 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4596 for (x
= 1; x
< img
->width
- 1; ++x
)
4598 p
= COLOR (new, x
, 0);
4599 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4600 p
= COLOR (new, x
, img
->height
- 1);
4601 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4604 for (y
= 1; y
< img
->height
- 1; ++y
)
4606 p
= COLOR (new, 1, y
);
4608 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4610 int r
, g
, b
, yy
, xx
;
4613 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4614 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4617 XColor
*t
= COLOR (colors
, xx
, yy
);
4618 r
+= matrix
[i
] * t
->red
;
4619 g
+= matrix
[i
] * t
->green
;
4620 b
+= matrix
[i
] * t
->blue
;
4623 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4624 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4625 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4626 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4631 x_from_xcolors (f
, img
, new);
4637 /* Perform the pre-defined `emboss' edge-detection on image IMG
4641 x_emboss (struct frame
*f
, struct image
*img
)
4643 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4647 /* Transform image IMG which is used on frame F with a Laplace
4648 edge-detection algorithm. The result is an image that can be used
4649 to draw disabled buttons, for example. */
4652 x_laplace (struct frame
*f
, struct image
*img
)
4654 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4658 /* Perform edge-detection on image IMG on frame F, with specified
4659 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4661 MATRIX must be either
4663 - a list of at least 9 numbers in row-major form
4664 - a vector of at least 9 numbers
4666 COLOR_ADJUST nil means use a default; otherwise it must be a
4670 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4671 Lisp_Object color_adjust
)
4679 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4680 ++i
, matrix
= XCDR (matrix
))
4681 trans
[i
] = XFLOATINT (XCAR (matrix
));
4683 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4685 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4686 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4689 if (NILP (color_adjust
))
4690 color_adjust
= make_number (0xffff / 2);
4692 if (i
== 9 && NUMBERP (color_adjust
))
4693 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4697 /* Transform image IMG on frame F so that it looks disabled. */
4700 x_disable_image (struct frame
*f
, struct image
*img
)
4702 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4704 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4706 int n_planes
= dpyinfo
->n_planes
;
4707 #endif /* HAVE_NTGUI */
4711 /* Color (or grayscale). Convert to gray, and equalize. Just
4712 drawing such images with a stipple can look very odd, so
4713 we're using this method instead. */
4714 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4716 const int h
= 15000;
4717 const int l
= 30000;
4719 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4723 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4724 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4725 p
->red
= p
->green
= p
->blue
= i2
;
4728 x_from_xcolors (f
, img
, colors
);
4731 /* Draw a cross over the disabled image, if we must or if we
4733 if (n_planes
< 2 || cross_disabled_images
)
4736 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4738 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4740 Display
*dpy
= FRAME_X_DISPLAY (f
);
4741 GC gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4742 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4743 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4744 img
->width
- 1, img
->height
- 1);
4745 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4751 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4752 XSetForeground (dpy
, gc
, MaskForeground (f
));
4753 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4754 img
->width
- 1, img
->height
- 1);
4755 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4759 #endif /* !HAVE_NS */
4764 hdc
= get_frame_dc (f
);
4765 bmpdc
= CreateCompatibleDC (hdc
);
4766 release_frame_dc (f
, hdc
);
4768 prev
= SelectObject (bmpdc
, img
->pixmap
);
4770 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4771 MoveToEx (bmpdc
, 0, 0, NULL
);
4772 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4773 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4774 LineTo (bmpdc
, img
->width
- 1, 0);
4778 SelectObject (bmpdc
, img
->mask
);
4779 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4780 MoveToEx (bmpdc
, 0, 0, NULL
);
4781 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4782 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4783 LineTo (bmpdc
, img
->width
- 1, 0);
4785 SelectObject (bmpdc
, prev
);
4787 #endif /* HAVE_NTGUI */
4792 /* Build a mask for image IMG which is used on frame F. FILE is the
4793 name of an image file, for error messages. HOW determines how to
4794 determine the background color of IMG. If it is a list '(R G B)',
4795 with R, G, and B being integers >= 0, take that as the color of the
4796 background. Otherwise, determine the background color of IMG
4797 heuristically. Value is non-zero if successful. */
4800 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4802 XImagePtr_or_DC ximg
;
4810 #endif /* HAVE_NTGUI */
4811 int x
, y
, rc
, use_img_background
;
4812 unsigned long bg
= 0;
4816 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4817 img
->mask
= NO_PIXMAP
;
4818 img
->background_transparent_valid
= 0;
4823 /* Create an image and pixmap serving as mask. */
4824 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4825 &mask_img
, &img
->mask
);
4828 #endif /* !HAVE_NS */
4830 /* Get the X image of IMG->pixmap. */
4831 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4832 img
->width
, img
->height
,
4835 /* Create the bit array serving as mask. */
4836 row_width
= (img
->width
+ 7) / 8;
4837 mask_img
= xzalloc (row_width
* img
->height
);
4839 /* Create a memory device context for IMG->pixmap. */
4840 frame_dc
= get_frame_dc (f
);
4841 ximg
= CreateCompatibleDC (frame_dc
);
4842 release_frame_dc (f
, frame_dc
);
4843 prev
= SelectObject (ximg
, img
->pixmap
);
4844 #endif /* HAVE_NTGUI */
4846 /* Determine the background color of ximg. If HOW is `(R G B)'
4847 take that as color. Otherwise, use the image's background color. */
4848 use_img_background
= 1;
4854 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4856 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4860 if (i
== 3 && NILP (how
))
4862 char color_name
[30];
4863 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4866 0x00ffffff & /* Filter out palette info. */
4867 #endif /* HAVE_NTGUI */
4868 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4869 use_img_background
= 0;
4873 if (use_img_background
)
4874 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4876 /* Set all bits in mask_img to 1 whose color in ximg is different
4877 from the background color bg. */
4879 for (y
= 0; y
< img
->height
; ++y
)
4880 for (x
= 0; x
< img
->width
; ++x
)
4882 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4883 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4885 if (XGetPixel (ximg
, x
, y
) == bg
)
4886 ns_set_alpha (ximg
, x
, y
, 0);
4887 #endif /* HAVE_NS */
4889 /* Fill in the background_transparent field while we have the mask handy. */
4890 image_background_transparent (img
, f
, mask_img
);
4892 /* Put mask_img into img->mask. */
4893 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4894 x_destroy_x_image (mask_img
);
4895 #endif /* !HAVE_NS */
4897 for (y
= 0; y
< img
->height
; ++y
)
4898 for (x
= 0; x
< img
->width
; ++x
)
4900 COLORREF p
= GetPixel (ximg
, x
, y
);
4902 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4905 /* Create the mask image. */
4906 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4908 /* Fill in the background_transparent field while we have the mask handy. */
4909 SelectObject (ximg
, img
->mask
);
4910 image_background_transparent (img
, f
, ximg
);
4912 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4914 #endif /* HAVE_NTGUI */
4916 Destroy_Image (ximg
, prev
);
4922 /***********************************************************************
4923 PBM (mono, gray, color)
4924 ***********************************************************************/
4926 static int pbm_image_p (Lisp_Object object
);
4927 static int pbm_load (struct frame
*f
, struct image
*img
);
4928 static int pbm_scan_number (unsigned char **, unsigned char *);
4930 /* The symbol `pbm' identifying images of this type. */
4932 static Lisp_Object Qpbm
;
4934 /* Indices of image specification fields in gs_format, below. */
4936 enum pbm_keyword_index
4952 /* Vector of image_keyword structures describing the format
4953 of valid user-defined image specifications. */
4955 static const struct image_keyword pbm_format
[PBM_LAST
] =
4957 {":type", IMAGE_SYMBOL_VALUE
, 1},
4958 {":file", IMAGE_STRING_VALUE
, 0},
4959 {":data", IMAGE_STRING_VALUE
, 0},
4960 {":ascent", IMAGE_ASCENT_VALUE
, 0},
4961 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
4962 {":relief", IMAGE_INTEGER_VALUE
, 0},
4963 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4964 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4965 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
4966 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
4967 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
4970 /* Structure describing the image type `pbm'. */
4972 static struct image_type pbm_type
=
4982 /* Return non-zero if OBJECT is a valid PBM image specification. */
4985 pbm_image_p (Lisp_Object object
)
4987 struct image_keyword fmt
[PBM_LAST
];
4989 memcpy (fmt
, pbm_format
, sizeof fmt
);
4991 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
4994 /* Must specify either :data or :file. */
4995 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
4999 /* Scan a decimal number from *S and return it. Advance *S while
5000 reading the number. END is the end of the string. Value is -1 at
5004 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5006 int c
= 0, val
= -1;
5010 /* Skip white-space. */
5011 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
5016 /* Skip comment to end of line. */
5017 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5020 else if (c_isdigit (c
))
5022 /* Read decimal number. */
5024 while (*s
< end
&& (c
= *(*s
)++, c_isdigit (c
)))
5025 val
= 10 * val
+ c
- '0';
5037 #if 0 /* Unused. ++kfs */
5039 /* Read FILE into memory. Value is a pointer to a buffer allocated
5040 with xmalloc holding FILE's contents. Value is null if an error
5041 occurred. *SIZE is set to the size of the file. */
5044 pbm_read_file (Lisp_Object file
, int *size
)
5050 if (stat (SDATA (file
), &st
) == 0
5051 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5052 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
5053 && (buf
= xmalloc (st
.st_size
),
5054 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5073 #endif /* HAVE_NTGUI */
5075 /* Load PBM image IMG for use on frame F. */
5078 pbm_load (struct frame
*f
, struct image
*img
)
5081 int width
, height
, max_color_idx
= 0;
5083 Lisp_Object file
, specified_file
;
5084 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5085 unsigned char *contents
= NULL
;
5086 unsigned char *end
, *p
;
5089 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5091 if (STRINGP (specified_file
))
5093 file
= x_find_image_file (specified_file
);
5094 if (!STRINGP (file
))
5096 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5100 contents
= slurp_file (SSDATA (file
), &size
);
5101 if (contents
== NULL
)
5103 image_error ("Error reading `%s'", file
, Qnil
);
5108 end
= contents
+ size
;
5113 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5114 if (!STRINGP (data
))
5116 image_error ("Invalid image data `%s'", data
, Qnil
);
5120 end
= p
+ SBYTES (data
);
5123 /* Check magic number. */
5124 if (end
- p
< 2 || *p
++ != 'P')
5126 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5135 raw_p
= 0, type
= PBM_MONO
;
5139 raw_p
= 0, type
= PBM_GRAY
;
5143 raw_p
= 0, type
= PBM_COLOR
;
5147 raw_p
= 1, type
= PBM_MONO
;
5151 raw_p
= 1, type
= PBM_GRAY
;
5155 raw_p
= 1, type
= PBM_COLOR
;
5159 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5163 /* Read width, height, maximum color-component. Characters
5164 starting with `#' up to the end of a line are ignored. */
5165 width
= pbm_scan_number (&p
, end
);
5166 height
= pbm_scan_number (&p
, end
);
5168 if (type
!= PBM_MONO
)
5170 max_color_idx
= pbm_scan_number (&p
, end
);
5171 if (max_color_idx
> 65535 || max_color_idx
< 0)
5173 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5178 if (!check_image_size (f
, width
, height
))
5180 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5184 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5185 &ximg
, &img
->pixmap
))
5188 /* Initialize the color hash table. */
5189 init_color_table ();
5191 if (type
== PBM_MONO
)
5194 struct image_keyword fmt
[PBM_LAST
];
5195 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5196 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5198 /* Parse the image specification. */
5199 memcpy (fmt
, pbm_format
, sizeof fmt
);
5200 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5202 /* Get foreground and background colors, maybe allocate colors. */
5203 if (fmt
[PBM_FOREGROUND
].count
5204 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5205 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5206 if (fmt
[PBM_BACKGROUND
].count
5207 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5209 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5210 img
->background
= bg
;
5211 img
->background_valid
= 1;
5214 for (y
= 0; y
< height
; ++y
)
5215 for (x
= 0; x
< width
; ++x
)
5223 x_destroy_x_image (ximg
);
5224 x_clear_image (f
, img
);
5225 image_error ("Invalid image size in image `%s'",
5235 g
= pbm_scan_number (&p
, end
);
5237 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5242 int expected_size
= height
* width
;
5243 if (max_color_idx
> 255)
5245 if (type
== PBM_COLOR
)
5248 if (raw_p
&& p
+ expected_size
> end
)
5250 x_destroy_x_image (ximg
);
5251 x_clear_image (f
, img
);
5252 image_error ("Invalid image size in image `%s'",
5257 for (y
= 0; y
< height
; ++y
)
5258 for (x
= 0; x
< width
; ++x
)
5262 if (type
== PBM_GRAY
&& raw_p
)
5265 if (max_color_idx
> 255)
5266 r
= g
= b
= r
* 256 + *p
++;
5268 else if (type
== PBM_GRAY
)
5269 r
= g
= b
= pbm_scan_number (&p
, end
);
5273 if (max_color_idx
> 255)
5276 if (max_color_idx
> 255)
5279 if (max_color_idx
> 255)
5284 r
= pbm_scan_number (&p
, end
);
5285 g
= pbm_scan_number (&p
, end
);
5286 b
= pbm_scan_number (&p
, end
);
5289 if (r
< 0 || g
< 0 || b
< 0)
5291 x_destroy_x_image (ximg
);
5292 image_error ("Invalid pixel value in image `%s'",
5297 /* RGB values are now in the range 0..max_color_idx.
5298 Scale this to the range 0..0xffff supported by X. */
5299 r
= (double) r
* 65535 / max_color_idx
;
5300 g
= (double) g
* 65535 / max_color_idx
;
5301 b
= (double) b
* 65535 / max_color_idx
;
5302 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5306 #ifdef COLOR_TABLE_SUPPORT
5307 /* Store in IMG->colors the colors allocated for the image, and
5308 free the color table. */
5309 img
->colors
= colors_in_color_table (&img
->ncolors
);
5310 free_color_table ();
5311 #endif /* COLOR_TABLE_SUPPORT */
5314 img
->height
= height
;
5316 /* Maybe fill in the background field while we have ximg handy. */
5318 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5319 /* Casting avoids a GCC warning. */
5320 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5322 /* Put the image into a pixmap. */
5323 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5324 x_destroy_x_image (ximg
);
5326 /* X and W32 versions did it here, MAC version above. ++kfs
5328 img->height = height; */
5335 /***********************************************************************
5337 ***********************************************************************/
5339 #if defined (HAVE_PNG) || defined (HAVE_NS)
5341 /* Function prototypes. */
5343 static int png_image_p (Lisp_Object object
);
5344 static int png_load (struct frame
*f
, struct image
*img
);
5346 /* The symbol `png' identifying images of this type. */
5348 static Lisp_Object Qpng
;
5350 /* Indices of image specification fields in png_format, below. */
5352 enum png_keyword_index
5367 /* Vector of image_keyword structures describing the format
5368 of valid user-defined image specifications. */
5370 static const struct image_keyword png_format
[PNG_LAST
] =
5372 {":type", IMAGE_SYMBOL_VALUE
, 1},
5373 {":data", IMAGE_STRING_VALUE
, 0},
5374 {":file", IMAGE_STRING_VALUE
, 0},
5375 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5376 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5377 {":relief", IMAGE_INTEGER_VALUE
, 0},
5378 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5379 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5380 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5381 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5384 /* Structure describing the image type `png'. */
5386 static struct image_type png_type
=
5395 /* Return non-zero if OBJECT is a valid PNG image specification. */
5398 png_image_p (Lisp_Object object
)
5400 struct image_keyword fmt
[PNG_LAST
];
5401 memcpy (fmt
, png_format
, sizeof fmt
);
5403 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5406 /* Must specify either the :data or :file keyword. */
5407 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5410 #endif /* HAVE_PNG || HAVE_NS */
5416 /* PNG library details. */
5418 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5419 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5420 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5421 png_error_ptr
, png_error_ptr
));
5422 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5423 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5424 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5425 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5426 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5427 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5428 png_uint_32
*, png_uint_32
*,
5429 int *, int *, int *, int *, int *));
5430 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5431 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5432 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5433 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5434 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5436 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5437 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5438 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5439 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5440 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5441 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5442 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5444 #if (PNG_LIBPNG_VER >= 10500)
5445 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5446 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5447 #endif /* libpng version >= 1.5 */
5450 init_png_functions (Lisp_Object libraries
)
5454 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5457 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5458 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5459 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5460 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5461 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5462 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5463 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5464 LOAD_IMGLIB_FN (library
, png_read_info
);
5465 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5466 LOAD_IMGLIB_FN (library
, png_get_valid
);
5467 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5468 LOAD_IMGLIB_FN (library
, png_set_expand
);
5469 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5470 LOAD_IMGLIB_FN (library
, png_set_background
);
5471 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5472 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5473 LOAD_IMGLIB_FN (library
, png_get_channels
);
5474 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5475 LOAD_IMGLIB_FN (library
, png_read_image
);
5476 LOAD_IMGLIB_FN (library
, png_read_end
);
5477 LOAD_IMGLIB_FN (library
, png_error
);
5479 #if (PNG_LIBPNG_VER >= 10500)
5480 LOAD_IMGLIB_FN (library
, png_longjmp
);
5481 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5482 #endif /* libpng version >= 1.5 */
5488 #define fn_png_get_io_ptr png_get_io_ptr
5489 #define fn_png_sig_cmp png_sig_cmp
5490 #define fn_png_create_read_struct png_create_read_struct
5491 #define fn_png_create_info_struct png_create_info_struct
5492 #define fn_png_destroy_read_struct png_destroy_read_struct
5493 #define fn_png_set_read_fn png_set_read_fn
5494 #define fn_png_set_sig_bytes png_set_sig_bytes
5495 #define fn_png_read_info png_read_info
5496 #define fn_png_get_IHDR png_get_IHDR
5497 #define fn_png_get_valid png_get_valid
5498 #define fn_png_set_strip_16 png_set_strip_16
5499 #define fn_png_set_expand png_set_expand
5500 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5501 #define fn_png_set_background png_set_background
5502 #define fn_png_get_bKGD png_get_bKGD
5503 #define fn_png_read_update_info png_read_update_info
5504 #define fn_png_get_channels png_get_channels
5505 #define fn_png_get_rowbytes png_get_rowbytes
5506 #define fn_png_read_image png_read_image
5507 #define fn_png_read_end png_read_end
5508 #define fn_png_error png_error
5510 #if (PNG_LIBPNG_VER >= 10500)
5511 #define fn_png_longjmp png_longjmp
5512 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5513 #endif /* libpng version >= 1.5 */
5515 #endif /* HAVE_NTGUI */
5517 /* Possibly inefficient/inexact substitutes for _setjmp and _longjmp.
5518 Do not use sys_setjmp, as PNG supports only jmp_buf. The _longjmp
5519 substitute may munge the signal mask, but that should be OK here.
5520 MinGW (MS-Windows) uses _setjmp and defines setjmp to _setjmp in
5521 the system header setjmp.h; don't mess up that. */
5522 #ifndef HAVE__SETJMP
5523 # define _setjmp(j) setjmp (j)
5524 # define _longjmp longjmp
5527 #if (PNG_LIBPNG_VER < 10500)
5528 #define PNG_LONGJMP(ptr) (_longjmp ((ptr)->jmpbuf, 1))
5529 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5531 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5532 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5533 #define PNG_JMPBUF(ptr) \
5534 (*fn_png_set_longjmp_fn ((ptr), _longjmp, sizeof (jmp_buf)))
5537 /* Error and warning handlers installed when the PNG library
5540 static _Noreturn
void
5541 my_png_error (png_struct
*png_ptr
, const char *msg
)
5543 eassert (png_ptr
!= NULL
);
5544 /* Avoid compiler warning about deprecated direct access to
5545 png_ptr's fields in libpng versions 1.4.x. */
5546 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5547 PNG_LONGJMP (png_ptr
);
5552 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5554 eassert (png_ptr
!= NULL
);
5555 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5558 /* Memory source for PNG decoding. */
5560 struct png_memory_storage
5562 unsigned char *bytes
; /* The data */
5563 ptrdiff_t len
; /* How big is it? */
5564 ptrdiff_t index
; /* Where are we? */
5568 /* Function set as reader function when reading PNG image from memory.
5569 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5570 bytes from the input to DATA. */
5573 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5575 struct png_memory_storage
*tbr
5576 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5578 if (length
> tbr
->len
- tbr
->index
)
5579 fn_png_error (png_ptr
, "Read error");
5581 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5582 tbr
->index
= tbr
->index
+ length
;
5586 /* Function set as reader function when reading PNG image from a file.
5587 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5588 bytes from the input to DATA. */
5591 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5593 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5595 if (fread (data
, 1, length
, fp
) < length
)
5596 fn_png_error (png_ptr
, "Read error");
5600 /* Load PNG image IMG for use on frame F. Value is non-zero if
5603 struct png_load_context
5605 /* These are members so that longjmp doesn't munge local variables. */
5606 png_struct
*png_ptr
;
5615 png_load_body (struct frame
*f
, struct image
*img
, struct png_load_context
*c
)
5617 Lisp_Object file
, specified_file
;
5618 Lisp_Object specified_data
;
5621 XImagePtr ximg
, mask_img
= NULL
;
5622 png_struct
*png_ptr
;
5623 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5626 png_byte
*pixels
= NULL
;
5627 png_byte
**rows
= NULL
;
5628 png_uint_32 width
, height
;
5629 int bit_depth
, color_type
, interlace_type
;
5631 png_uint_32 row_bytes
;
5633 struct png_memory_storage tbr
; /* Data to be read */
5635 /* Find out what file to load. */
5636 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5637 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5639 if (NILP (specified_data
))
5641 file
= x_find_image_file (specified_file
);
5642 if (!STRINGP (file
))
5644 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5648 /* Open the image file. */
5649 fp
= fopen (SSDATA (file
), "rb");
5652 image_error ("Cannot open image file `%s'", file
, Qnil
);
5656 /* Check PNG signature. */
5657 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5658 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5660 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5667 if (!STRINGP (specified_data
))
5669 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5673 /* Read from memory. */
5674 tbr
.bytes
= SDATA (specified_data
);
5675 tbr
.len
= SBYTES (specified_data
);
5678 /* Check PNG signature. */
5679 if (tbr
.len
< sizeof sig
5680 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5682 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5686 /* Need to skip past the signature. */
5687 tbr
.bytes
+= sizeof (sig
);
5690 /* Initialize read and info structs for PNG lib. */
5691 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5696 info_ptr
= fn_png_create_info_struct (png_ptr
);
5697 end_info
= fn_png_create_info_struct (png_ptr
);
5700 c
->png_ptr
= png_ptr
;
5701 c
->info_ptr
= info_ptr
;
5702 c
->end_info
= end_info
;
5707 if (! (info_ptr
&& end_info
))
5709 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5714 if (fp
) fclose (fp
);
5718 /* Set error jump-back. We come back here when the PNG library
5719 detects an error. */
5720 if (_setjmp (PNG_JMPBUF (png_ptr
)))
5724 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5732 /* Silence a bogus diagnostic; see GCC bug 54561. */
5733 IF_LINT (fp
= c
->fp
);
5735 /* Read image info. */
5736 if (!NILP (specified_data
))
5737 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5739 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5741 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5742 fn_png_read_info (png_ptr
, info_ptr
);
5743 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5744 &interlace_type
, NULL
, NULL
);
5746 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5747 && check_image_size (f
, width
, height
)))
5749 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5753 /* Create the X image and pixmap now, so that the work below can be
5754 omitted if the image is too large for X. */
5755 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5759 /* If image contains simply transparency data, we prefer to
5760 construct a clipping mask. */
5761 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5766 /* This function is easier to write if we only have to handle
5767 one data format: RGB or RGBA with 8 bits per channel. Let's
5768 transform other formats into that format. */
5770 /* Strip more than 8 bits per channel. */
5771 if (bit_depth
== 16)
5772 fn_png_set_strip_16 (png_ptr
);
5774 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5776 fn_png_set_expand (png_ptr
);
5778 /* Convert grayscale images to RGB. */
5779 if (color_type
== PNG_COLOR_TYPE_GRAY
5780 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5781 fn_png_set_gray_to_rgb (png_ptr
);
5783 /* Handle alpha channel by combining the image with a background
5784 color. Do this only if a real alpha channel is supplied. For
5785 simple transparency, we prefer a clipping mask. */
5788 /* png_color_16 *image_bg; */
5789 Lisp_Object specified_bg
5790 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5791 int shift
= (bit_depth
== 16) ? 0 : 8;
5793 if (STRINGP (specified_bg
))
5794 /* The user specified `:background', use that. */
5797 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5799 png_color_16 user_bg
;
5801 memset (&user_bg
, 0, sizeof user_bg
);
5802 user_bg
.red
= color
.red
>> shift
;
5803 user_bg
.green
= color
.green
>> shift
;
5804 user_bg
.blue
= color
.blue
>> shift
;
5806 fn_png_set_background (png_ptr
, &user_bg
,
5807 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5812 /* We use the current frame background, ignoring any default
5813 background color set by the image. */
5814 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5816 png_color_16 frame_background
;
5818 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5819 x_query_color (f
, &color
);
5821 memset (&frame_background
, 0, sizeof frame_background
);
5822 frame_background
.red
= color
.red
>> shift
;
5823 frame_background
.green
= color
.green
>> shift
;
5824 frame_background
.blue
= color
.blue
>> shift
;
5825 #endif /* HAVE_X_WINDOWS */
5827 fn_png_set_background (png_ptr
, &frame_background
,
5828 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5832 /* Update info structure. */
5833 fn_png_read_update_info (png_ptr
, info_ptr
);
5835 /* Get number of channels. Valid values are 1 for grayscale images
5836 and images with a palette, 2 for grayscale images with transparency
5837 information (alpha channel), 3 for RGB images, and 4 for RGB
5838 images with alpha channel, i.e. RGBA. If conversions above were
5839 sufficient we should only have 3 or 4 channels here. */
5840 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5841 eassert (channels
== 3 || channels
== 4);
5843 /* Number of bytes needed for one row of the image. */
5844 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5846 /* Allocate memory for the image. */
5847 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5848 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5849 memory_full (SIZE_MAX
);
5850 c
->pixels
= pixels
= xmalloc (sizeof *pixels
* row_bytes
* height
);
5851 c
->rows
= rows
= xmalloc (height
* sizeof *rows
);
5852 for (i
= 0; i
< height
; ++i
)
5853 rows
[i
] = pixels
+ i
* row_bytes
;
5855 /* Read the entire image. */
5856 fn_png_read_image (png_ptr
, rows
);
5857 fn_png_read_end (png_ptr
, info_ptr
);
5864 /* Create an image and pixmap serving as mask if the PNG image
5865 contains an alpha channel. */
5868 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5869 &mask_img
, &img
->mask
))
5871 x_destroy_x_image (ximg
);
5872 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5873 img
->pixmap
= NO_PIXMAP
;
5877 /* Fill the X image and mask from PNG data. */
5878 init_color_table ();
5880 for (y
= 0; y
< height
; ++y
)
5882 png_byte
*p
= rows
[y
];
5884 for (x
= 0; x
< width
; ++x
)
5891 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5892 /* An alpha channel, aka mask channel, associates variable
5893 transparency with an image. Where other image formats
5894 support binary transparency---fully transparent or fully
5895 opaque---PNG allows up to 254 levels of partial transparency.
5896 The PNG library implements partial transparency by combining
5897 the image with a specified background color.
5899 I'm not sure how to handle this here nicely: because the
5900 background on which the image is displayed may change, for
5901 real alpha channel support, it would be necessary to create
5902 a new image for each possible background.
5904 What I'm doing now is that a mask is created if we have
5905 boolean transparency information. Otherwise I'm using
5906 the frame's background color to combine the image with. */
5911 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5917 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5918 /* Set IMG's background color from the PNG image, unless the user
5922 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5924 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5925 img
->background_valid
= 1;
5929 #ifdef COLOR_TABLE_SUPPORT
5930 /* Remember colors allocated for this image. */
5931 img
->colors
= colors_in_color_table (&img
->ncolors
);
5932 free_color_table ();
5933 #endif /* COLOR_TABLE_SUPPORT */
5936 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5941 img
->height
= height
;
5943 /* Maybe fill in the background field while we have ximg handy.
5944 Casting avoids a GCC warning. */
5945 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5947 /* Put the image into the pixmap, then free the X image and its buffer. */
5948 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5949 x_destroy_x_image (ximg
);
5951 /* Same for the mask. */
5954 /* Fill in the background_transparent field while we have the
5955 mask handy. Casting avoids a GCC warning. */
5956 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5958 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5959 x_destroy_x_image (mask_img
);
5966 png_load (struct frame
*f
, struct image
*img
)
5968 struct png_load_context c
;
5969 return png_load_body (f
, img
, &c
);
5972 #else /* HAVE_PNG */
5976 png_load (struct frame
*f
, struct image
*img
)
5978 return ns_load_image (f
, img
,
5979 image_spec_value (img
->spec
, QCfile
, NULL
),
5980 image_spec_value (img
->spec
, QCdata
, NULL
));
5982 #endif /* HAVE_NS */
5985 #endif /* !HAVE_PNG */
5989 /***********************************************************************
5991 ***********************************************************************/
5993 #if defined (HAVE_JPEG) || defined (HAVE_NS)
5995 static int jpeg_image_p (Lisp_Object object
);
5996 static int jpeg_load (struct frame
*f
, struct image
*img
);
5998 /* The symbol `jpeg' identifying images of this type. */
6000 static Lisp_Object Qjpeg
;
6002 /* Indices of image specification fields in gs_format, below. */
6004 enum jpeg_keyword_index
6013 JPEG_HEURISTIC_MASK
,
6019 /* Vector of image_keyword structures describing the format
6020 of valid user-defined image specifications. */
6022 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
6024 {":type", IMAGE_SYMBOL_VALUE
, 1},
6025 {":data", IMAGE_STRING_VALUE
, 0},
6026 {":file", IMAGE_STRING_VALUE
, 0},
6027 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6028 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6029 {":relief", IMAGE_INTEGER_VALUE
, 0},
6030 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6031 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6032 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6033 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6036 /* Structure describing the image type `jpeg'. */
6038 static struct image_type jpeg_type
=
6047 /* Return non-zero if OBJECT is a valid JPEG image specification. */
6050 jpeg_image_p (Lisp_Object object
)
6052 struct image_keyword fmt
[JPEG_LAST
];
6054 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6056 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6059 /* Must specify either the :data or :file keyword. */
6060 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6063 #endif /* HAVE_JPEG || HAVE_NS */
6067 /* Work around a warning about HAVE_STDLIB_H being redefined in
6069 #ifdef HAVE_STDLIB_H
6070 #undef HAVE_STDLIB_H
6071 #endif /* HAVE_STLIB_H */
6073 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6074 /* In older releases of the jpeg library, jpeglib.h will define boolean
6075 differently depending on __WIN32__, so make sure it is defined. */
6079 #include <jpeglib.h>
6082 #ifdef HAVE_STLIB_H_1
6083 #define HAVE_STDLIB_H 1
6088 /* JPEG library details. */
6089 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6090 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6091 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6092 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6093 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6094 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6095 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6096 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6099 init_jpeg_functions (Lisp_Object libraries
)
6103 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6106 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6107 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6108 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6109 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6110 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6111 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6112 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6113 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6117 /* Wrapper since we can't directly assign the function pointer
6118 to another function pointer that was declared more completely easily. */
6120 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6122 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6127 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6128 #define fn_jpeg_start_decompress jpeg_start_decompress
6129 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6130 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6131 #define fn_jpeg_read_header jpeg_read_header
6132 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6133 #define fn_jpeg_std_error jpeg_std_error
6134 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6136 #endif /* HAVE_NTGUI */
6138 struct my_jpeg_error_mgr
6140 struct jpeg_error_mgr pub
;
6141 sys_jmp_buf setjmp_buffer
;
6143 /* The remaining members are so that longjmp doesn't munge local
6145 struct jpeg_decompress_struct cinfo
;
6149 MY_JPEG_INVALID_IMAGE_SIZE
,
6150 MY_JPEG_CANNOT_CREATE_X
6158 static _Noreturn
void
6159 my_error_exit (j_common_ptr cinfo
)
6161 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6162 mgr
->failure_code
= MY_JPEG_ERROR_EXIT
;
6163 sys_longjmp (mgr
->setjmp_buffer
, 1);
6167 /* Init source method for JPEG data source manager. Called by
6168 jpeg_read_header() before any data is actually read. See
6169 libjpeg.doc from the JPEG lib distribution. */
6172 our_common_init_source (j_decompress_ptr cinfo
)
6177 /* Method to terminate data source. Called by
6178 jpeg_finish_decompress() after all data has been processed. */
6181 our_common_term_source (j_decompress_ptr cinfo
)
6186 /* Fill input buffer method for JPEG data source manager. Called
6187 whenever more data is needed. We read the whole image in one step,
6188 so this only adds a fake end of input marker at the end. */
6190 static JOCTET our_memory_buffer
[2];
6193 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6195 /* Insert a fake EOI marker. */
6196 struct jpeg_source_mgr
*src
= cinfo
->src
;
6198 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6199 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6201 src
->next_input_byte
= our_memory_buffer
;
6202 src
->bytes_in_buffer
= 2;
6207 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6208 is the JPEG data source manager. */
6211 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6213 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6217 if (num_bytes
> src
->bytes_in_buffer
)
6218 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6220 src
->bytes_in_buffer
-= num_bytes
;
6221 src
->next_input_byte
+= num_bytes
;
6226 /* Set up the JPEG lib for reading an image from DATA which contains
6227 LEN bytes. CINFO is the decompression info structure created for
6228 reading the image. */
6231 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, ptrdiff_t len
)
6233 struct jpeg_source_mgr
*src
;
6235 if (cinfo
->src
== NULL
)
6237 /* First time for this JPEG object? */
6238 cinfo
->src
= (struct jpeg_source_mgr
*)
6239 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6240 sizeof (struct jpeg_source_mgr
));
6241 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6242 src
->next_input_byte
= data
;
6245 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6246 src
->init_source
= our_common_init_source
;
6247 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6248 src
->skip_input_data
= our_memory_skip_input_data
;
6249 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6250 src
->term_source
= our_common_term_source
;
6251 src
->bytes_in_buffer
= len
;
6252 src
->next_input_byte
= data
;
6256 struct jpeg_stdio_mgr
6258 struct jpeg_source_mgr mgr
;
6265 /* Size of buffer to read JPEG from file.
6266 Not too big, as we want to use alloc_small. */
6267 #define JPEG_STDIO_BUFFER_SIZE 8192
6270 /* Fill input buffer method for JPEG data source manager. Called
6271 whenever more data is needed. The data is read from a FILE *. */
6274 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6276 struct jpeg_stdio_mgr
*src
;
6278 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6283 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6285 src
->mgr
.bytes_in_buffer
= bytes
;
6288 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6290 src
->buffer
[0] = (JOCTET
) 0xFF;
6291 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6292 src
->mgr
.bytes_in_buffer
= 2;
6294 src
->mgr
.next_input_byte
= src
->buffer
;
6301 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6302 is the JPEG data source manager. */
6305 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6307 struct jpeg_stdio_mgr
*src
;
6308 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6310 while (num_bytes
> 0 && !src
->finished
)
6312 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6314 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6315 src
->mgr
.next_input_byte
+= num_bytes
;
6320 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6321 src
->mgr
.bytes_in_buffer
= 0;
6322 src
->mgr
.next_input_byte
= NULL
;
6324 our_stdio_fill_input_buffer (cinfo
);
6330 /* Set up the JPEG lib for reading an image from a FILE *.
6331 CINFO is the decompression info structure created for
6332 reading the image. */
6335 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6337 struct jpeg_stdio_mgr
*src
;
6339 if (cinfo
->src
!= NULL
)
6340 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6343 /* First time for this JPEG object? */
6344 cinfo
->src
= (struct jpeg_source_mgr
*)
6345 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6346 sizeof (struct jpeg_stdio_mgr
));
6347 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6348 src
->buffer
= (JOCTET
*)
6349 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6350 JPEG_STDIO_BUFFER_SIZE
);
6355 src
->mgr
.init_source
= our_common_init_source
;
6356 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6357 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6358 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6359 src
->mgr
.term_source
= our_common_term_source
;
6360 src
->mgr
.bytes_in_buffer
= 0;
6361 src
->mgr
.next_input_byte
= NULL
;
6365 /* Load image IMG for use on frame F. Patterned after example.c
6366 from the JPEG lib. */
6369 jpeg_load_body (struct frame
*f
, struct image
*img
,
6370 struct my_jpeg_error_mgr
*mgr
)
6372 Lisp_Object file
, specified_file
;
6373 Lisp_Object specified_data
;
6376 int row_stride
, x
, y
;
6377 XImagePtr ximg
= NULL
;
6378 unsigned long *colors
;
6381 /* Open the JPEG file. */
6382 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6383 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6385 if (NILP (specified_data
))
6387 file
= x_find_image_file (specified_file
);
6388 if (!STRINGP (file
))
6390 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6394 fp
= fopen (SSDATA (file
), "rb");
6397 image_error ("Cannot open `%s'", file
, Qnil
);
6401 else if (!STRINGP (specified_data
))
6403 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6407 IF_LINT (mgr
->fp
= fp
);
6409 /* Customize libjpeg's error handling to call my_error_exit when an
6410 error is detected. This function will perform a longjmp. */
6411 mgr
->cinfo
.err
= fn_jpeg_std_error (&mgr
->pub
);
6412 mgr
->pub
.error_exit
= my_error_exit
;
6413 if (sys_setjmp (mgr
->setjmp_buffer
))
6415 switch (mgr
->failure_code
)
6417 case MY_JPEG_ERROR_EXIT
:
6419 char buf
[JMSG_LENGTH_MAX
];
6420 mgr
->cinfo
.err
->format_message ((j_common_ptr
) &mgr
->cinfo
, buf
);
6421 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6422 build_string (buf
));
6426 case MY_JPEG_INVALID_IMAGE_SIZE
:
6427 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6430 case MY_JPEG_CANNOT_CREATE_X
:
6434 /* Close the input file and destroy the JPEG object. */
6437 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6439 /* If we already have an XImage, free that. */
6440 x_destroy_x_image (ximg
);
6442 /* Free pixmap and colors. */
6443 x_clear_image (f
, img
);
6447 /* Silence a bogus diagnostic; see GCC bug 54561. */
6448 IF_LINT (fp
= mgr
->fp
);
6450 /* Create the JPEG decompression object. Let it read from fp.
6451 Read the JPEG image header. */
6452 fn_jpeg_CreateDecompress (&mgr
->cinfo
, JPEG_LIB_VERSION
, sizeof *&mgr
->cinfo
);
6454 if (NILP (specified_data
))
6455 jpeg_file_src (&mgr
->cinfo
, fp
);
6457 jpeg_memory_src (&mgr
->cinfo
, SDATA (specified_data
),
6458 SBYTES (specified_data
));
6460 fn_jpeg_read_header (&mgr
->cinfo
, 1);
6462 /* Customize decompression so that color quantization will be used.
6463 Start decompression. */
6464 mgr
->cinfo
.quantize_colors
= 1;
6465 fn_jpeg_start_decompress (&mgr
->cinfo
);
6466 width
= img
->width
= mgr
->cinfo
.output_width
;
6467 height
= img
->height
= mgr
->cinfo
.output_height
;
6469 if (!check_image_size (f
, width
, height
))
6471 mgr
->failure_code
= MY_JPEG_INVALID_IMAGE_SIZE
;
6472 sys_longjmp (mgr
->setjmp_buffer
, 1);
6475 /* Create X image and pixmap. */
6476 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6478 mgr
->failure_code
= MY_JPEG_CANNOT_CREATE_X
;
6479 sys_longjmp (mgr
->setjmp_buffer
, 1);
6482 /* Allocate colors. When color quantization is used,
6483 mgr->cinfo.actual_number_of_colors has been set with the number of
6484 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6485 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6486 No more than 255 colors will be generated. */
6490 if (mgr
->cinfo
.out_color_components
> 2)
6491 ir
= 0, ig
= 1, ib
= 2;
6492 else if (mgr
->cinfo
.out_color_components
> 1)
6493 ir
= 0, ig
= 1, ib
= 0;
6495 ir
= 0, ig
= 0, ib
= 0;
6497 /* Use the color table mechanism because it handles colors that
6498 cannot be allocated nicely. Such colors will be replaced with
6499 a default color, and we don't have to care about which colors
6500 can be freed safely, and which can't. */
6501 init_color_table ();
6502 colors
= alloca (mgr
->cinfo
.actual_number_of_colors
* sizeof *colors
);
6504 for (i
= 0; i
< mgr
->cinfo
.actual_number_of_colors
; ++i
)
6506 /* Multiply RGB values with 255 because X expects RGB values
6507 in the range 0..0xffff. */
6508 int r
= mgr
->cinfo
.colormap
[ir
][i
] << 8;
6509 int g
= mgr
->cinfo
.colormap
[ig
][i
] << 8;
6510 int b
= mgr
->cinfo
.colormap
[ib
][i
] << 8;
6511 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6514 #ifdef COLOR_TABLE_SUPPORT
6515 /* Remember those colors actually allocated. */
6516 img
->colors
= colors_in_color_table (&img
->ncolors
);
6517 free_color_table ();
6518 #endif /* COLOR_TABLE_SUPPORT */
6522 row_stride
= width
* mgr
->cinfo
.output_components
;
6523 buffer
= mgr
->cinfo
.mem
->alloc_sarray ((j_common_ptr
) &mgr
->cinfo
,
6524 JPOOL_IMAGE
, row_stride
, 1);
6525 for (y
= 0; y
< height
; ++y
)
6527 fn_jpeg_read_scanlines (&mgr
->cinfo
, buffer
, 1);
6528 for (x
= 0; x
< mgr
->cinfo
.output_width
; ++x
)
6529 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6533 fn_jpeg_finish_decompress (&mgr
->cinfo
);
6534 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6538 /* Maybe fill in the background field while we have ximg handy. */
6539 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6540 /* Casting avoids a GCC warning. */
6541 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6543 /* Put the image into the pixmap. */
6544 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6545 x_destroy_x_image (ximg
);
6550 jpeg_load (struct frame
*f
, struct image
*img
)
6552 struct my_jpeg_error_mgr mgr
;
6553 return jpeg_load_body (f
, img
, &mgr
);
6556 #else /* HAVE_JPEG */
6560 jpeg_load (struct frame
*f
, struct image
*img
)
6562 return ns_load_image (f
, img
,
6563 image_spec_value (img
->spec
, QCfile
, NULL
),
6564 image_spec_value (img
->spec
, QCdata
, NULL
));
6566 #endif /* HAVE_NS */
6568 #endif /* !HAVE_JPEG */
6572 /***********************************************************************
6574 ***********************************************************************/
6576 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6578 static int tiff_image_p (Lisp_Object object
);
6579 static int tiff_load (struct frame
*f
, struct image
*img
);
6581 /* The symbol `tiff' identifying images of this type. */
6583 static Lisp_Object Qtiff
;
6585 /* Indices of image specification fields in tiff_format, below. */
6587 enum tiff_keyword_index
6596 TIFF_HEURISTIC_MASK
,
6603 /* Vector of image_keyword structures describing the format
6604 of valid user-defined image specifications. */
6606 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6608 {":type", IMAGE_SYMBOL_VALUE
, 1},
6609 {":data", IMAGE_STRING_VALUE
, 0},
6610 {":file", IMAGE_STRING_VALUE
, 0},
6611 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6612 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6613 {":relief", IMAGE_INTEGER_VALUE
, 0},
6614 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6615 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6616 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6617 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6618 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6621 /* Structure describing the image type `tiff'. */
6623 static struct image_type tiff_type
=
6632 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6635 tiff_image_p (Lisp_Object object
)
6637 struct image_keyword fmt
[TIFF_LAST
];
6638 memcpy (fmt
, tiff_format
, sizeof fmt
);
6640 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6643 /* Must specify either the :data or :file keyword. */
6644 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6647 #endif /* HAVE_TIFF || HAVE_NS */
6655 /* TIFF library details. */
6656 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6657 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6658 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6659 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6660 TIFFReadWriteProc
, TIFFReadWriteProc
,
6661 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6662 TIFFMapFileProc
, TIFFUnmapFileProc
));
6663 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6664 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6665 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6666 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6669 init_tiff_functions (Lisp_Object libraries
)
6673 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6676 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6677 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6678 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6679 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6680 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6681 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6682 LOAD_IMGLIB_FN (library
, TIFFClose
);
6683 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6689 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6690 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6691 #define fn_TIFFOpen TIFFOpen
6692 #define fn_TIFFClientOpen TIFFClientOpen
6693 #define fn_TIFFGetField TIFFGetField
6694 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6695 #define fn_TIFFClose TIFFClose
6696 #define fn_TIFFSetDirectory TIFFSetDirectory
6697 #endif /* HAVE_NTGUI */
6700 /* Reading from a memory buffer for TIFF images Based on the PNG
6701 memory source, but we have to provide a lot of extra functions.
6704 We really only need to implement read and seek, but I am not
6705 convinced that the TIFF library is smart enough not to destroy
6706 itself if we only hand it the function pointers we need to
6711 unsigned char *bytes
;
6718 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6720 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6722 size
= min (size
, src
->len
- src
->index
);
6723 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6729 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6735 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6737 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6742 case SEEK_SET
: /* Go from beginning of source. */
6746 case SEEK_END
: /* Go from end of source. */
6747 idx
= src
->len
+ off
;
6750 case SEEK_CUR
: /* Go from current position. */
6751 idx
= src
->index
+ off
;
6754 default: /* Invalid `whence'. */
6758 if (idx
> src
->len
|| idx
< 0)
6766 tiff_close_memory (thandle_t data
)
6773 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6775 /* It is already _IN_ memory. */
6780 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6782 /* We don't need to do this. */
6786 tiff_size_of_memory (thandle_t data
)
6788 return ((tiff_memory_source
*) data
)->len
;
6791 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6792 compiler error compiling tiff_handler, see Bugzilla bug #17406
6793 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6794 this function as external works around that problem. */
6795 #if defined (__MINGW32__) && __GNUC__ == 3
6796 # define MINGW_STATIC
6798 # define MINGW_STATIC static
6802 tiff_handler (const char *, const char *, const char *, va_list)
6803 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6805 tiff_handler (const char *log_format
, const char *title
,
6806 const char *format
, va_list ap
)
6808 /* doprnt is not suitable here, as TIFF handlers are called from
6809 libtiff and are passed arbitrary printf directives. Instead, use
6810 vsnprintf, taking care to be portable to nonstandard environments
6811 where vsnprintf returns -1 on buffer overflow. Since it's just a
6812 log entry, it's OK to truncate it. */
6814 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6815 add_to_log (log_format
, build_string (title
),
6816 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6820 static void tiff_error_handler (const char *, const char *, va_list)
6821 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6823 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6825 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6829 static void tiff_warning_handler (const char *, const char *, va_list)
6830 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6832 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6834 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6838 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6842 tiff_load (struct frame
*f
, struct image
*img
)
6844 Lisp_Object file
, specified_file
;
6845 Lisp_Object specified_data
;
6847 int width
, height
, x
, y
, count
;
6851 tiff_memory_source memsrc
;
6854 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6855 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6857 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6858 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6860 if (NILP (specified_data
))
6862 /* Read from a file */
6863 file
= x_find_image_file (specified_file
);
6864 if (!STRINGP (file
))
6866 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6870 /* Try to open the image file. */
6871 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6874 image_error ("Cannot open `%s'", file
, Qnil
);
6880 if (!STRINGP (specified_data
))
6882 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6886 /* Memory source! */
6887 memsrc
.bytes
= SDATA (specified_data
);
6888 memsrc
.len
= SBYTES (specified_data
);
6891 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6892 tiff_read_from_memory
,
6893 tiff_write_from_memory
,
6894 tiff_seek_in_memory
,
6896 tiff_size_of_memory
,
6902 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6907 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6908 if (INTEGERP (image
))
6910 EMACS_INT ino
= XFASTINT (image
);
6911 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
6912 && fn_TIFFSetDirectory (tiff
, ino
)))
6914 image_error ("Invalid image number `%s' in image `%s'",
6916 fn_TIFFClose (tiff
);
6921 /* Get width and height of the image, and allocate a raster buffer
6922 of width x height 32-bit values. */
6923 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6924 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6926 if (!check_image_size (f
, width
, height
))
6928 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6929 fn_TIFFClose (tiff
);
6933 /* Create the X image and pixmap. */
6934 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
6935 && x_create_x_image_and_pixmap (f
, width
, height
, 0,
6936 &ximg
, &img
->pixmap
)))
6938 fn_TIFFClose (tiff
);
6942 buf
= xmalloc (sizeof *buf
* width
* height
);
6944 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6946 /* Count the number of images in the file. */
6947 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
6951 img
->lisp_data
= Fcons (Qcount
,
6952 Fcons (make_number (count
),
6955 fn_TIFFClose (tiff
);
6958 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6963 /* Initialize the color table. */
6964 init_color_table ();
6966 /* Process the pixel raster. Origin is in the lower-left corner. */
6967 for (y
= 0; y
< height
; ++y
)
6969 uint32
*row
= buf
+ y
* width
;
6971 for (x
= 0; x
< width
; ++x
)
6973 uint32 abgr
= row
[x
];
6974 int r
= TIFFGetR (abgr
) << 8;
6975 int g
= TIFFGetG (abgr
) << 8;
6976 int b
= TIFFGetB (abgr
) << 8;
6977 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6981 #ifdef COLOR_TABLE_SUPPORT
6982 /* Remember the colors allocated for the image. Free the color table. */
6983 img
->colors
= colors_in_color_table (&img
->ncolors
);
6984 free_color_table ();
6985 #endif /* COLOR_TABLE_SUPPORT */
6988 img
->height
= height
;
6990 /* Maybe fill in the background field while we have ximg handy. */
6991 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6992 /* Casting avoids a GCC warning on W32. */
6993 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6995 /* Put the image into the pixmap, then free the X image and its buffer. */
6996 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6997 x_destroy_x_image (ximg
);
7003 #else /* HAVE_TIFF */
7007 tiff_load (struct frame
*f
, struct image
*img
)
7009 return ns_load_image (f
, img
,
7010 image_spec_value (img
->spec
, QCfile
, NULL
),
7011 image_spec_value (img
->spec
, QCdata
, NULL
));
7013 #endif /* HAVE_NS */
7015 #endif /* !HAVE_TIFF */
7019 /***********************************************************************
7021 ***********************************************************************/
7023 #if defined (HAVE_GIF) || defined (HAVE_NS)
7025 static int gif_image_p (Lisp_Object object
);
7026 static int gif_load (struct frame
*f
, struct image
*img
);
7027 static void gif_clear_image (struct frame
*f
, struct image
*img
);
7029 /* The symbol `gif' identifying images of this type. */
7031 static Lisp_Object Qgif
;
7033 /* Indices of image specification fields in gif_format, below. */
7035 enum gif_keyword_index
7051 /* Vector of image_keyword structures describing the format
7052 of valid user-defined image specifications. */
7054 static const struct image_keyword gif_format
[GIF_LAST
] =
7056 {":type", IMAGE_SYMBOL_VALUE
, 1},
7057 {":data", IMAGE_STRING_VALUE
, 0},
7058 {":file", IMAGE_STRING_VALUE
, 0},
7059 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7060 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7061 {":relief", IMAGE_INTEGER_VALUE
, 0},
7062 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7063 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7064 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7065 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7066 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7069 /* Structure describing the image type `gif'. */
7071 static struct image_type gif_type
=
7080 /* Free X resources of GIF image IMG which is used on frame F. */
7083 gif_clear_image (struct frame
*f
, struct image
*img
)
7085 img
->lisp_data
= Qnil
;
7086 x_clear_image (f
, img
);
7089 /* Return non-zero if OBJECT is a valid GIF image specification. */
7092 gif_image_p (Lisp_Object object
)
7094 struct image_keyword fmt
[GIF_LAST
];
7095 memcpy (fmt
, gif_format
, sizeof fmt
);
7097 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7100 /* Must specify either the :data or :file keyword. */
7101 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7104 #endif /* HAVE_GIF */
7108 #if defined (HAVE_NTGUI)
7109 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7110 Undefine before redefining to avoid a preprocessor warning. */
7114 /* avoid conflict with QuickdrawText.h */
7115 #define DrawText gif_DrawText
7116 #include <gif_lib.h>
7119 #else /* HAVE_NTGUI */
7121 #include <gif_lib.h>
7123 #endif /* HAVE_NTGUI */
7128 /* GIF library details. */
7129 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7130 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7131 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7132 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7135 init_gif_functions (Lisp_Object libraries
)
7139 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7142 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7143 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7144 LOAD_IMGLIB_FN (library
, DGifOpen
);
7145 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7151 #define fn_DGifCloseFile DGifCloseFile
7152 #define fn_DGifSlurp DGifSlurp
7153 #define fn_DGifOpen DGifOpen
7154 #define fn_DGifOpenFileName DGifOpenFileName
7156 #endif /* HAVE_NTGUI */
7158 /* Reading a GIF image from memory
7159 Based on the PNG memory stuff to a certain extent. */
7163 unsigned char *bytes
;
7169 /* Make the current memory source available to gif_read_from_memory.
7170 It's done this way because not all versions of libungif support
7171 a UserData field in the GifFileType structure. */
7172 static gif_memory_source
*current_gif_memory_src
;
7175 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7177 gif_memory_source
*src
= current_gif_memory_src
;
7179 if (len
> src
->len
- src
->index
)
7182 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7188 /* Load GIF image IMG for use on frame F. Value is non-zero if
7191 static const int interlace_start
[] = {0, 4, 2, 1};
7192 static const int interlace_increment
[] = {8, 8, 4, 2};
7194 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7197 gif_load (struct frame
*f
, struct image
*img
)
7200 int rc
, width
, height
, x
, y
, i
, j
;
7202 ColorMapObject
*gif_color_map
;
7203 unsigned long pixel_colors
[256];
7205 gif_memory_source memsrc
;
7206 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7207 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7208 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7209 unsigned long bgcolor
= 0;
7212 if (NILP (specified_data
))
7214 file
= x_find_image_file (specified_file
);
7215 if (!STRINGP (file
))
7217 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7221 /* Open the GIF file. */
7222 gif
= fn_DGifOpenFileName (SSDATA (file
));
7225 image_error ("Cannot open `%s'", file
, Qnil
);
7231 if (!STRINGP (specified_data
))
7233 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7237 /* Read from memory! */
7238 current_gif_memory_src
= &memsrc
;
7239 memsrc
.bytes
= SDATA (specified_data
);
7240 memsrc
.len
= SBYTES (specified_data
);
7243 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7246 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7251 /* Before reading entire contents, check the declared image size. */
7252 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7254 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7255 fn_DGifCloseFile (gif
);
7259 /* Read entire contents. */
7260 rc
= fn_DGifSlurp (gif
);
7261 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7263 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7264 fn_DGifCloseFile (gif
);
7268 /* Which sub-image are we to display? */
7270 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7271 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7272 if (idx
< 0 || idx
>= gif
->ImageCount
)
7274 image_error ("Invalid image number `%s' in image `%s'",
7275 image_number
, img
->spec
);
7276 fn_DGifCloseFile (gif
);
7281 width
= img
->width
= gif
->SWidth
;
7282 height
= img
->height
= gif
->SHeight
;
7284 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Top
;
7285 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Left
;
7286 img
->corners
[BOT_CORNER
]
7287 = img
->corners
[TOP_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Height
;
7288 img
->corners
[RIGHT_CORNER
]
7289 = img
->corners
[LEFT_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Width
;
7291 if (!check_image_size (f
, width
, height
))
7293 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7294 fn_DGifCloseFile (gif
);
7298 /* Create the X image and pixmap. */
7299 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7301 fn_DGifCloseFile (gif
);
7305 /* Clear the part of the screen image not covered by the image.
7306 Full animated GIF support requires more here (see the gif89 spec,
7307 disposal methods). Let's simply assume that the part not covered
7308 by a sub-image is in the frame's background color. */
7309 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7310 for (x
= 0; x
< width
; ++x
)
7311 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7313 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7314 for (x
= 0; x
< width
; ++x
)
7315 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7317 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7319 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7320 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7321 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7322 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7325 /* Read the GIF image into the X image. */
7327 /* FIXME: With the current implementation, loading an animated gif
7328 is quadratic in the number of animation frames, since each frame
7329 is a separate struct image. We must provide a way for a single
7330 gif_load call to construct and save all animation frames. */
7332 init_color_table ();
7333 if (STRINGP (specified_bg
))
7334 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7335 FRAME_BACKGROUND_PIXEL (f
));
7336 for (j
= 0; j
<= idx
; ++j
)
7338 /* We use a local variable `raster' here because RasterBits is a
7339 char *, which invites problems with bytes >= 0x80. */
7340 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7341 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7342 int transparency_color_index
= -1;
7344 int subimg_width
= subimage
->ImageDesc
.Width
;
7345 int subimg_height
= subimage
->ImageDesc
.Height
;
7346 int subimg_top
= subimage
->ImageDesc
.Top
;
7347 int subimg_left
= subimage
->ImageDesc
.Left
;
7349 /* Find the Graphic Control Extension block for this sub-image.
7350 Extract the disposal method and transparency color. */
7351 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7353 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7355 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7356 && extblock
->ByteCount
== 4
7357 && extblock
->Bytes
[0] & 1)
7359 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7360 to background". Treat any other value like 2. */
7361 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7362 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7367 /* We can't "keep in place" the first subimage. */
7371 /* For disposal == 0, the spec says "No disposal specified. The
7372 decoder is not required to take any action." In practice, it
7373 seems we need to treat this like "keep in place", see e.g.
7374 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7378 /* Allocate subimage colors. */
7379 memset (pixel_colors
, 0, sizeof pixel_colors
);
7380 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7382 gif_color_map
= gif
->SColorMap
;
7385 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7387 if (transparency_color_index
== i
)
7388 pixel_colors
[i
] = STRINGP (specified_bg
)
7389 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7392 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7393 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7394 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7395 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7399 /* Apply the pixel values. */
7400 if (gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7404 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7406 y
++, row
+= interlace_increment
[pass
])
7408 if (row
>= subimg_height
)
7410 row
= interlace_start
[++pass
];
7411 while (row
>= subimg_height
)
7412 row
= interlace_start
[++pass
];
7415 for (x
= 0; x
< subimg_width
; x
++)
7417 int c
= raster
[y
* subimg_width
+ x
];
7418 if (transparency_color_index
!= c
|| disposal
!= 1)
7419 XPutPixel (ximg
, x
+ subimg_left
, row
+ subimg_top
,
7426 for (y
= 0; y
< subimg_height
; ++y
)
7427 for (x
= 0; x
< subimg_width
; ++x
)
7429 int c
= raster
[y
* subimg_width
+ x
];
7430 if (transparency_color_index
!= c
|| disposal
!= 1)
7431 XPutPixel (ximg
, x
+ subimg_left
, y
+ subimg_top
,
7437 #ifdef COLOR_TABLE_SUPPORT
7438 img
->colors
= colors_in_color_table (&img
->ncolors
);
7439 free_color_table ();
7440 #endif /* COLOR_TABLE_SUPPORT */
7442 /* Save GIF image extension data for `image-metadata'.
7443 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7444 img
->lisp_data
= Qnil
;
7445 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7448 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7449 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7450 /* Append (... FUNCTION "BYTES") */
7453 = Fcons (make_number (ext
->Function
),
7454 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7456 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7457 && ext
->ByteCount
== 4)
7459 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7460 delay
|= ext
->Bytes
[1];
7463 img
->lisp_data
= Fcons (Qextension_data
,
7464 Fcons (img
->lisp_data
, Qnil
));
7468 Fcons (make_float (delay
/ 100.0),
7472 if (gif
->ImageCount
> 1)
7473 img
->lisp_data
= Fcons (Qcount
,
7474 Fcons (make_number (gif
->ImageCount
),
7477 fn_DGifCloseFile (gif
);
7479 /* Maybe fill in the background field while we have ximg handy. */
7480 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7481 /* Casting avoids a GCC warning. */
7482 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7484 /* Put the image into the pixmap, then free the X image and its buffer. */
7485 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7486 x_destroy_x_image (ximg
);
7491 #else /* !HAVE_GIF */
7495 gif_load (struct frame
*f
, struct image
*img
)
7497 return ns_load_image (f
, img
,
7498 image_spec_value (img
->spec
, QCfile
, NULL
),
7499 image_spec_value (img
->spec
, QCdata
, NULL
));
7501 #endif /* HAVE_NS */
7503 #endif /* HAVE_GIF */
7506 /***********************************************************************
7508 ***********************************************************************/
7509 #if defined (HAVE_IMAGEMAGICK)
7511 static Lisp_Object Qimagemagick
;
7513 static int imagemagick_image_p (Lisp_Object
);
7514 static int imagemagick_load (struct frame
*, struct image
*);
7515 static void imagemagick_clear_image (struct frame
*, struct image
*);
7517 /* Indices of image specification fields in imagemagick_format. */
7519 enum imagemagick_keyword_index
7527 IMAGEMAGICK_ALGORITHM
,
7528 IMAGEMAGICK_HEURISTIC_MASK
,
7530 IMAGEMAGICK_BACKGROUND
,
7533 IMAGEMAGICK_ROTATION
,
7538 /* Vector of image_keyword structures describing the format
7539 of valid user-defined image specifications. */
7541 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7543 {":type", IMAGE_SYMBOL_VALUE
, 1},
7544 {":data", IMAGE_STRING_VALUE
, 0},
7545 {":file", IMAGE_STRING_VALUE
, 0},
7546 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7547 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7548 {":relief", IMAGE_INTEGER_VALUE
, 0},
7549 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7550 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7551 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7552 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7553 {":height", IMAGE_INTEGER_VALUE
, 0},
7554 {":width", IMAGE_INTEGER_VALUE
, 0},
7555 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7556 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7559 /* Structure describing the image type for any image handled via
7562 static struct image_type imagemagick_type
=
7565 imagemagick_image_p
,
7567 imagemagick_clear_image
,
7571 /* Free X resources of imagemagick image IMG which is used on frame F. */
7574 imagemagick_clear_image (struct frame
*f
,
7577 x_clear_image (f
, img
);
7580 /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do
7581 this by calling parse_image_spec and supplying the keywords that
7582 identify the IMAGEMAGICK format. */
7585 imagemagick_image_p (Lisp_Object object
)
7587 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7588 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7590 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7593 /* Must specify either the :data or :file keyword. */
7594 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7597 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7598 Therefore rename the function so it doesn't collide with ImageMagick. */
7599 #define DrawRectangle DrawRectangleGif
7600 #include <wand/MagickWand.h>
7602 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7603 Emacs seems to work fine with the hidden version, so unhide it. */
7604 #include <magick/version.h>
7605 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7606 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7607 MagickPixelPacket
*);
7610 /* Log ImageMagick error message.
7611 Useful when a ImageMagick function returns the status `MagickFalse'. */
7614 imagemagick_error (MagickWand
*wand
)
7617 ExceptionType severity
;
7619 description
= MagickGetException (wand
, &severity
);
7620 image_error ("ImageMagick error: %s",
7621 build_string (description
),
7623 description
= (char *) MagickRelinquishMemory (description
);
7626 /* Helper function for imagemagick_load, which does the actual loading
7627 given contents and size, apart from frame and image structures,
7628 passed from imagemagick_load. Uses librimagemagick to do most of
7629 the image processing.
7631 F is a pointer to the Emacs frame; IMG to the image structure to
7632 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
7633 be parsed; SIZE is the number of bytes of data; and FILENAME is
7634 either the file name or the image data.
7636 Return non-zero if successful. */
7639 imagemagick_load_image (struct frame
*f
, struct image
*img
,
7640 unsigned char *contents
, unsigned int size
,
7643 size_t width
, height
;
7644 MagickBooleanType status
;
7647 MagickWand
*image_wand
;
7648 MagickWand
*ping_wand
;
7649 PixelIterator
*iterator
;
7650 PixelWand
**pixels
, *bg_wand
= NULL
;
7651 MagickPixelPacket pixel
;
7656 int desired_width
, desired_height
;
7660 /* Handle image index for image types who can contain more than one image.
7661 Interface :index is same as for GIF. First we "ping" the image to see how
7662 many sub-images it contains. Pinging is faster than loading the image to
7663 find out things about it. */
7665 /* Initialize the imagemagick environment. */
7666 MagickWandGenesis ();
7667 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7668 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7669 ping_wand
= NewMagickWand ();
7670 /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */
7673 ? MagickPingImage (ping_wand
, filename
)
7674 : MagickPingImageBlob (ping_wand
, contents
, size
);
7676 if (status
== MagickFalse
)
7678 imagemagick_error (ping_wand
);
7679 DestroyMagickWand (ping_wand
);
7683 if (ino
< 0 || ino
>= MagickGetNumberImages (ping_wand
))
7685 image_error ("Invalid image number `%s' in image `%s'",
7687 DestroyMagickWand (ping_wand
);
7691 if (MagickGetNumberImages (ping_wand
) > 1)
7694 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7697 DestroyMagickWand (ping_wand
);
7699 /* Now we know how many images are inside the file. If it's not a
7700 bundle, the number is one. Load the image data. */
7702 image_wand
= NewMagickWand ();
7705 ? MagickReadImage (image_wand
, filename
)
7706 : MagickReadImageBlob (image_wand
, contents
, size
))
7709 imagemagick_error (image_wand
);
7710 goto imagemagick_error
;
7713 /* Retrieve the frame's background color, for use later. */
7716 Lisp_Object specified_bg
;
7718 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7719 if (!STRINGP (specified_bg
)
7720 || !x_defined_color (f
, SSDATA (specified_bg
), &bgcolor
, 0))
7723 bgcolor
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
7724 x_query_color (f
, &bgcolor
);
7726 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &bgcolor
, 1);
7730 bg_wand
= NewPixelWand ();
7731 PixelSetRed (bg_wand
, (double) bgcolor
.red
/ 65535);
7732 PixelSetGreen (bg_wand
, (double) bgcolor
.green
/ 65535);
7733 PixelSetBlue (bg_wand
, (double) bgcolor
.blue
/ 65535);
7736 /* If width and/or height is set in the display spec assume we want
7737 to scale to those values. If either h or w is unspecified, the
7738 unspecified should be calculated from the specified to preserve
7740 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7741 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7742 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7743 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7745 height
= MagickGetImageHeight (image_wand
);
7746 width
= MagickGetImageWidth (image_wand
);
7748 if (desired_width
!= -1 && desired_height
== -1)
7749 /* w known, calculate h. */
7750 desired_height
= (double) desired_width
/ width
* height
;
7751 if (desired_width
== -1 && desired_height
!= -1)
7752 /* h known, calculate w. */
7753 desired_width
= (double) desired_height
/ height
* width
;
7754 if (desired_width
!= -1 && desired_height
!= -1)
7756 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7757 if (status
== MagickFalse
)
7759 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7760 imagemagick_error (image_wand
);
7761 goto imagemagick_error
;
7765 /* crop behaves similar to image slicing in Emacs but is more memory
7767 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7769 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7771 /* After some testing, it seems MagickCropImage is the fastest crop
7772 function in ImageMagick. This crop function seems to do less copying
7773 than the alternatives, but it still reads the entire image into memory
7774 before cropping, which is apparently difficult to avoid when using
7776 size_t crop_width
= XINT (XCAR (crop
));
7778 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7780 size_t crop_height
= XINT (XCAR (crop
));
7782 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7784 ssize_t crop_x
= XINT (XCAR (crop
));
7786 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7788 ssize_t crop_y
= XINT (XCAR (crop
));
7789 MagickCropImage (image_wand
, crop_width
, crop_height
,
7796 /* Furthermore :rotation. we need background color and angle for
7799 TODO background handling for rotation specified_bg =
7800 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7802 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7805 rotation
= extract_float (value
);
7806 status
= MagickRotateImage (image_wand
, bg_wand
, rotation
);
7807 if (status
== MagickFalse
)
7809 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7810 imagemagick_error (image_wand
);
7811 goto imagemagick_error
;
7815 /* Finally we are done manipulating the image. Figure out the
7816 resulting width/height and transfer ownership to Emacs. */
7817 height
= MagickGetImageHeight (image_wand
);
7818 width
= MagickGetImageWidth (image_wand
);
7820 /* Set the canvas background color to the frame or specified
7821 background, and flatten the image. Note: as of ImageMagick
7822 6.6.0, SVG image transparency is not handled properly
7823 (e.g. etc/images/splash.svg shows a white background always). */
7825 MagickWand
*new_wand
;
7826 MagickSetImageBackgroundColor (image_wand
, bg_wand
);
7827 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
7828 new_wand
= MagickMergeImageLayers (image_wand
, MergeLayer
);
7830 new_wand
= MagickFlattenImages (image_wand
);
7832 DestroyMagickWand (image_wand
);
7833 image_wand
= new_wand
;
7836 if (! (width
<= INT_MAX
&& height
<= INT_MAX
7837 && check_image_size (f
, width
, height
)))
7839 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7840 goto imagemagick_error
;
7843 /* We can now get a valid pixel buffer from the imagemagick file, if all
7846 init_color_table ();
7848 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7849 if (imagemagick_render_type
!= 0)
7851 /* Magicexportimage is normally faster than pixelpushing. This
7852 method is also well tested. Some aspects of this method are
7853 ad-hoc and needs to be more researched. */
7854 int imagedepth
= 24; /*MagickGetImageDepth(image_wand);*/
7855 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP"; /*"RGBP";*/
7856 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7857 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7858 &ximg
, &img
->pixmap
))
7860 #ifdef COLOR_TABLE_SUPPORT
7861 free_color_table ();
7863 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7864 goto imagemagick_error
;
7867 /* Oddly, the below code doesn't seem to work:*/
7868 /* switch(ximg->bitmap_unit){ */
7870 /* pixelwidth=CharPixel; */
7873 /* pixelwidth=ShortPixel; */
7876 /* pixelwidth=LongPixel; */
7880 Here im just guessing the format of the bitmap.
7881 happens to work fine for:
7884 seems about 3 times as fast as pixel pushing(not carefully measured)
7886 pixelwidth
= CharPixel
; /*??? TODO figure out*/
7887 MagickExportImagePixels (image_wand
, 0, 0, width
, height
,
7888 exportdepth
, pixelwidth
, ximg
->data
);
7891 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
7893 size_t image_height
;
7895 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7896 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7897 &ximg
, &img
->pixmap
))
7899 #ifdef COLOR_TABLE_SUPPORT
7900 free_color_table ();
7902 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7903 goto imagemagick_error
;
7906 /* Copy imagemagick image to x with primitive yet robust pixel
7907 pusher loop. This has been tested a lot with many different
7910 /* Copy pixels from the imagemagick image structure to the x image map. */
7911 iterator
= NewPixelIterator (image_wand
);
7912 if (iterator
== (PixelIterator
*) NULL
)
7914 #ifdef COLOR_TABLE_SUPPORT
7915 free_color_table ();
7917 x_destroy_x_image (ximg
);
7918 image_error ("Imagemagick pixel iterator creation failed",
7920 goto imagemagick_error
;
7923 image_height
= MagickGetImageHeight (image_wand
);
7924 for (y
= 0; y
< image_height
; y
++)
7926 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7927 if (pixels
== (PixelWand
**) NULL
)
7929 for (x
= 0; x
< (long) width
; x
++)
7931 PixelGetMagickColor (pixels
[x
], &pixel
);
7932 XPutPixel (ximg
, x
, y
,
7933 lookup_rgb_color (f
,
7939 DestroyPixelIterator (iterator
);
7942 #ifdef COLOR_TABLE_SUPPORT
7943 /* Remember colors allocated for this image. */
7944 img
->colors
= colors_in_color_table (&img
->ncolors
);
7945 free_color_table ();
7946 #endif /* COLOR_TABLE_SUPPORT */
7949 img
->height
= height
;
7951 /* Put the image into the pixmap, then free the X image and its
7953 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7954 x_destroy_x_image (ximg
);
7956 /* Final cleanup. image_wand should be the only resource left. */
7957 DestroyMagickWand (image_wand
);
7958 if (bg_wand
) DestroyPixelWand (bg_wand
);
7960 /* `MagickWandTerminus' terminates the imagemagick environment. */
7961 MagickWandTerminus ();
7966 DestroyMagickWand (image_wand
);
7967 if (bg_wand
) DestroyPixelWand (bg_wand
);
7969 MagickWandTerminus ();
7970 /* TODO more cleanup. */
7971 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7976 /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if
7977 successful. this function will go into the imagemagick_type structure, and
7978 the prototype thus needs to be compatible with that structure. */
7981 imagemagick_load (struct frame
*f
, struct image
*img
)
7984 Lisp_Object file_name
;
7986 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7987 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7988 if (STRINGP (file_name
))
7992 file
= x_find_image_file (file_name
);
7993 if (!STRINGP (file
))
7995 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7998 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
8000 /* Else its not a file, its a lisp object. Load the image from a
8001 lisp object rather than a file. */
8006 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8007 if (!STRINGP (data
))
8009 image_error ("Invalid image data `%s'", data
, Qnil
);
8012 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
8013 SBYTES (data
), NULL
);
8019 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
8020 doc
: /* Return a list of image types supported by ImageMagick.
8021 Each entry in this list is a symbol named after an ImageMagick format
8022 tag. See the ImageMagick manual for a list of ImageMagick formats and
8023 their descriptions (http://www.imagemagick.org/script/formats.php).
8024 You can also try the shell command: `identify -list format'.
8026 Note that ImageMagick recognizes many file-types that Emacs does not
8027 recognize as images, such as C. See `imagemagick-types-enable'
8028 and `imagemagick-types-inhibit'. */)
8031 Lisp_Object typelist
= Qnil
;
8036 Lisp_Object Qimagemagicktype
;
8038 GetExceptionInfo(&ex
);
8039 imtypes
= GetMagickList ("*", &numf
, &ex
);
8040 DestroyExceptionInfo(&ex
);
8042 for (i
= 0; i
< numf
; i
++)
8044 Qimagemagicktype
= intern (imtypes
[i
]);
8045 typelist
= Fcons (Qimagemagicktype
, typelist
);
8047 return Fnreverse (typelist
);
8050 #endif /* defined (HAVE_IMAGEMAGICK) */
8054 /***********************************************************************
8056 ***********************************************************************/
8058 #if defined (HAVE_RSVG)
8060 /* Function prototypes. */
8062 static int svg_image_p (Lisp_Object object
);
8063 static int svg_load (struct frame
*f
, struct image
*img
);
8065 static int svg_load_image (struct frame
*, struct image
*,
8066 unsigned char *, ptrdiff_t);
8068 /* The symbol `svg' identifying images of this type. */
8070 static Lisp_Object Qsvg
;
8072 /* Indices of image specification fields in svg_format, below. */
8074 enum svg_keyword_index
8089 /* Vector of image_keyword structures describing the format
8090 of valid user-defined image specifications. */
8092 static const struct image_keyword svg_format
[SVG_LAST
] =
8094 {":type", IMAGE_SYMBOL_VALUE
, 1},
8095 {":data", IMAGE_STRING_VALUE
, 0},
8096 {":file", IMAGE_STRING_VALUE
, 0},
8097 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8098 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8099 {":relief", IMAGE_INTEGER_VALUE
, 0},
8100 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8101 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8102 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8103 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8106 /* Structure describing the image type `svg'. Its the same type of
8107 structure defined for all image formats, handled by emacs image
8108 functions. See struct image_type in dispextern.h. */
8110 static struct image_type svg_type
=
8112 /* An identifier showing that this is an image structure for the SVG format. */
8114 /* Handle to a function that can be used to identify a SVG file. */
8116 /* Handle to function used to load a SVG file. */
8118 /* Handle to function to free sresources for SVG. */
8120 /* An internal field to link to the next image type in a list of
8121 image types, will be filled in when registering the format. */
8126 /* Return non-zero if OBJECT is a valid SVG image specification. Do
8127 this by calling parse_image_spec and supplying the keywords that
8128 identify the SVG format. */
8131 svg_image_p (Lisp_Object object
)
8133 struct image_keyword fmt
[SVG_LAST
];
8134 memcpy (fmt
, svg_format
, sizeof fmt
);
8136 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8139 /* Must specify either the :data or :file keyword. */
8140 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8143 #include <librsvg/rsvg.h>
8147 /* SVG library functions. */
8148 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
);
8149 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
);
8150 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
);
8151 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
);
8152 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
);
8154 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
);
8155 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
);
8156 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
);
8157 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
);
8158 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
);
8159 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
);
8160 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
);
8161 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
);
8163 DEF_IMGLIB_FN (void, g_type_init
);
8164 DEF_IMGLIB_FN (void, g_object_unref
);
8165 DEF_IMGLIB_FN (void, g_error_free
);
8167 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8170 init_svg_functions (Lisp_Object libraries
)
8172 HMODULE library
, gdklib
, glib
, gobject
;
8174 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
8175 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
8176 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
8177 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
8180 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8181 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8182 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8183 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8184 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8186 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8187 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8188 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8189 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8190 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8191 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8192 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8193 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8195 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8196 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8197 LOAD_IMGLIB_FN (glib
, g_error_free
);
8203 /* The following aliases for library functions allow dynamic loading
8204 to be used on some platforms. */
8205 #define fn_rsvg_handle_new rsvg_handle_new
8206 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8207 #define fn_rsvg_handle_write rsvg_handle_write
8208 #define fn_rsvg_handle_close rsvg_handle_close
8209 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8211 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8212 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8213 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8214 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8215 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8216 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8217 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8218 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8220 #define fn_g_type_init g_type_init
8221 #define fn_g_object_unref g_object_unref
8222 #define fn_g_error_free g_error_free
8223 #endif /* !HAVE_NTGUI */
8225 /* Load SVG image IMG for use on frame F. Value is non-zero if
8226 successful. this function will go into the svg_type structure, and
8227 the prototype thus needs to be compatible with that structure. */
8230 svg_load (struct frame
*f
, struct image
*img
)
8233 Lisp_Object file_name
;
8235 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8236 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8237 if (STRINGP (file_name
))
8240 unsigned char *contents
;
8243 file
= x_find_image_file (file_name
);
8244 if (!STRINGP (file
))
8246 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8250 /* Read the entire file into memory. */
8251 contents
= slurp_file (SSDATA (file
), &size
);
8252 if (contents
== NULL
)
8254 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8257 /* If the file was slurped into memory properly, parse it. */
8258 success_p
= svg_load_image (f
, img
, contents
, size
);
8261 /* Else its not a file, its a lisp object. Load the image from a
8262 lisp object rather than a file. */
8267 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8268 if (!STRINGP (data
))
8270 image_error ("Invalid image data `%s'", data
, Qnil
);
8273 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8279 /* svg_load_image is a helper function for svg_load, which does the
8280 actual loading given contents and size, apart from frame and image
8281 structures, passed from svg_load.
8283 Uses librsvg to do most of the image processing.
8285 Returns non-zero when successful. */
8287 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8288 struct image
*img
, /* Pointer to emacs image structure. */
8289 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8290 ptrdiff_t size
) /* Size of data in bytes. */
8292 RsvgHandle
*rsvg_handle
;
8293 RsvgDimensionData dimension_data
;
8298 const guint8
*pixels
;
8301 Lisp_Object specified_bg
;
8306 /* g_type_init is a glib function that must be called prior to using
8307 gnome type library functions. */
8309 /* Make a handle to a new rsvg object. */
8310 rsvg_handle
= fn_rsvg_handle_new ();
8312 /* Parse the contents argument and fill in the rsvg_handle. */
8313 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8314 if (err
) goto rsvg_error
;
8316 /* The parsing is complete, rsvg_handle is ready to used, close it
8317 for further writes. */
8318 fn_rsvg_handle_close (rsvg_handle
, &err
);
8319 if (err
) goto rsvg_error
;
8321 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8322 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8324 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8328 /* We can now get a valid pixel buffer from the svg file, if all
8330 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8331 if (!pixbuf
) goto rsvg_error
;
8332 fn_g_object_unref (rsvg_handle
);
8334 /* Extract some meta data from the svg handle. */
8335 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8336 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8337 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8338 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8340 /* Validate the svg meta data. */
8341 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8342 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8343 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8344 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8346 /* Try to create a x pixmap to hold the svg pixmap. */
8347 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8349 fn_g_object_unref (pixbuf
);
8353 init_color_table ();
8355 /* Handle alpha channel by combining the image with a background
8357 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8358 if (!STRINGP (specified_bg
)
8359 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8362 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8363 x_query_color (f
, &background
);
8365 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8369 /* SVG pixmaps specify transparency in the last byte, so right
8370 shift 8 bits to get rid of it, since emacs doesn't support
8372 background
.red
>>= 8;
8373 background
.green
>>= 8;
8374 background
.blue
>>= 8;
8376 /* This loop handles opacity values, since Emacs assumes
8377 non-transparent images. Each pixel must be "flattened" by
8378 calculating the resulting color, given the transparency of the
8379 pixel, and the image background color. */
8380 for (y
= 0; y
< height
; ++y
)
8382 for (x
= 0; x
< width
; ++x
)
8392 opacity
= *pixels
++;
8394 red
= ((red
* opacity
)
8395 + (background
.red
* ((1 << 8) - opacity
)));
8396 green
= ((green
* opacity
)
8397 + (background
.green
* ((1 << 8) - opacity
)));
8398 blue
= ((blue
* opacity
)
8399 + (background
.blue
* ((1 << 8) - opacity
)));
8401 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8404 pixels
+= rowstride
- 4 * width
;
8407 #ifdef COLOR_TABLE_SUPPORT
8408 /* Remember colors allocated for this image. */
8409 img
->colors
= colors_in_color_table (&img
->ncolors
);
8410 free_color_table ();
8411 #endif /* COLOR_TABLE_SUPPORT */
8413 fn_g_object_unref (pixbuf
);
8416 img
->height
= height
;
8418 /* Maybe fill in the background field while we have ximg handy.
8419 Casting avoids a GCC warning. */
8420 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8422 /* Put the image into the pixmap, then free the X image and its
8424 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8425 x_destroy_x_image (ximg
);
8430 fn_g_object_unref (rsvg_handle
);
8431 /* FIXME: Use error->message so the user knows what is the actual
8432 problem with the image. */
8433 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8434 fn_g_error_free (err
);
8438 #endif /* defined (HAVE_RSVG) */
8443 /***********************************************************************
8445 ***********************************************************************/
8447 #ifdef HAVE_X_WINDOWS
8448 #define HAVE_GHOSTSCRIPT 1
8449 #endif /* HAVE_X_WINDOWS */
8451 #ifdef HAVE_GHOSTSCRIPT
8453 static int gs_image_p (Lisp_Object object
);
8454 static int gs_load (struct frame
*f
, struct image
*img
);
8455 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8457 /* Keyword symbols. */
8459 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8461 /* Indices of image specification fields in gs_format, below. */
8463 enum gs_keyword_index
8481 /* Vector of image_keyword structures describing the format
8482 of valid user-defined image specifications. */
8484 static const struct image_keyword gs_format
[GS_LAST
] =
8486 {":type", IMAGE_SYMBOL_VALUE
, 1},
8487 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8488 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8489 {":file", IMAGE_STRING_VALUE
, 1},
8490 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8491 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8492 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8493 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8494 {":relief", IMAGE_INTEGER_VALUE
, 0},
8495 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8496 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8497 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8498 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8501 /* Structure describing the image type `ghostscript'. */
8503 static struct image_type gs_type
=
8513 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8516 gs_clear_image (struct frame
*f
, struct image
*img
)
8518 x_clear_image (f
, img
);
8522 /* Return non-zero if OBJECT is a valid Ghostscript image
8526 gs_image_p (Lisp_Object object
)
8528 struct image_keyword fmt
[GS_LAST
];
8532 memcpy (fmt
, gs_format
, sizeof fmt
);
8534 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8537 /* Bounding box must be a list or vector containing 4 integers. */
8538 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8541 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8542 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8547 else if (VECTORP (tem
))
8549 if (ASIZE (tem
) != 4)
8551 for (i
= 0; i
< 4; ++i
)
8552 if (!INTEGERP (AREF (tem
, i
)))
8562 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8566 gs_load (struct frame
*f
, struct image
*img
)
8568 uprintmax_t printnum1
, printnum2
;
8569 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
8570 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8572 double in_width
, in_height
;
8573 Lisp_Object pixel_colors
= Qnil
;
8575 /* Compute pixel size of pixmap needed from the given size in the
8576 image specification. Sizes in the specification are in pt. 1 pt
8577 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8579 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8580 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8581 in_width
*= FRAME_X_DISPLAY_INFO (f
)->resx
;
8582 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8583 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8584 in_height
*= FRAME_X_DISPLAY_INFO (f
)->resy
;
8586 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
8587 && check_image_size (f
, in_width
, in_height
)))
8589 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8592 img
->width
= in_width
;
8593 img
->height
= in_height
;
8595 /* Create the pixmap. */
8596 eassert (img
->pixmap
== NO_PIXMAP
);
8598 if (x_check_image_size (0, img
->width
, img
->height
))
8600 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8602 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8603 img
->width
, img
->height
,
8604 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8610 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8614 /* Call the loader to fill the pixmap. It returns a process object
8615 if successful. We do not record_unwind_protect here because
8616 other places in redisplay like calling window scroll functions
8617 don't either. Let the Lisp loader use `unwind-protect' instead. */
8618 printnum1
= FRAME_X_WINDOW (f
);
8619 printnum2
= img
->pixmap
;
8620 window_and_pixmap_id
8621 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8623 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
8624 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
8626 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8628 XSETFRAME (frame
, f
);
8629 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8631 loader
= intern ("gs-load-image");
8633 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
8634 make_number (img
->width
),
8635 make_number (img
->height
),
8636 window_and_pixmap_id
,
8638 return PROCESSP (img
->lisp_data
);
8642 /* Kill the Ghostscript process that was started to fill PIXMAP on
8643 frame F. Called from XTread_socket when receiving an event
8644 telling Emacs that Ghostscript has finished drawing. */
8647 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8649 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8654 /* Find the image containing PIXMAP. */
8655 for (i
= 0; i
< c
->used
; ++i
)
8656 if (c
->images
[i
]->pixmap
== pixmap
)
8659 /* Should someone in between have cleared the image cache, for
8660 instance, give up. */
8664 /* Kill the GS process. We should have found PIXMAP in the image
8665 cache and its image should contain a process object. */
8667 eassert (PROCESSP (img
->lisp_data
));
8668 Fkill_process (img
->lisp_data
, Qnil
);
8669 img
->lisp_data
= Qnil
;
8671 #if defined (HAVE_X_WINDOWS)
8673 /* On displays with a mutable colormap, figure out the colors
8674 allocated for the image by looking at the pixels of an XImage for
8676 class = FRAME_X_VISUAL (f
)->class;
8677 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8683 /* Try to get an XImage for img->pixmep. */
8684 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8685 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8690 /* Initialize the color table. */
8691 init_color_table ();
8693 /* For each pixel of the image, look its color up in the
8694 color table. After having done so, the color table will
8695 contain an entry for each color used by the image. */
8696 for (y
= 0; y
< img
->height
; ++y
)
8697 for (x
= 0; x
< img
->width
; ++x
)
8699 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8700 lookup_pixel_color (f
, pixel
);
8703 /* Record colors in the image. Free color table and XImage. */
8704 #ifdef COLOR_TABLE_SUPPORT
8705 img
->colors
= colors_in_color_table (&img
->ncolors
);
8706 free_color_table ();
8708 XDestroyImage (ximg
);
8710 #if 0 /* This doesn't seem to be the case. If we free the colors
8711 here, we get a BadAccess later in x_clear_image when
8712 freeing the colors. */
8713 /* We have allocated colors once, but Ghostscript has also
8714 allocated colors on behalf of us. So, to get the
8715 reference counts right, free them once. */
8717 x_free_colors (f
, img
->colors
, img
->ncolors
);
8721 image_error ("Cannot get X image of `%s'; colors will not be freed",
8726 #endif /* HAVE_X_WINDOWS */
8728 /* Now that we have the pixmap, compute mask and transform the
8729 image if requested. */
8731 postprocess_image (f
, img
);
8735 #endif /* HAVE_GHOSTSCRIPT */
8738 /***********************************************************************
8740 ***********************************************************************/
8744 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8745 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8748 return valid_image_p (spec
) ? Qt
: Qnil
;
8752 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8757 if (valid_image_p (spec
))
8758 id
= lookup_image (SELECTED_FRAME (), spec
);
8761 return make_number (id
);
8764 #endif /* GLYPH_DEBUG */
8767 /***********************************************************************
8769 ***********************************************************************/
8772 /* Image types that rely on external libraries are loaded dynamically
8773 if the library is available. */
8774 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8775 define_image_type (image_type, init_lib_fn (libraries))
8777 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8778 define_image_type (image_type, 1)
8779 #endif /* HAVE_NTGUI */
8781 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8782 doc
: /* Initialize image library implementing image type TYPE.
8783 Return non-nil if TYPE is a supported image type.
8785 Image types pbm and xbm are prebuilt; other types are loaded here.
8786 Libraries to load are specified in alist LIBRARIES (usually, the value
8787 of `dynamic-library-alist', which see). */)
8788 (Lisp_Object type
, Lisp_Object libraries
)
8791 /* Don't try to reload the library. */
8792 Lisp_Object tested
= Fassq (type
, Vlibrary_cache
);
8794 return XCDR (tested
);
8797 /* Types pbm and xbm are built-in and always available. */
8798 if (EQ (type
, Qpbm
) || EQ (type
, Qxbm
))
8801 #if defined (HAVE_XPM) || defined (HAVE_NS)
8802 if (EQ (type
, Qxpm
))
8803 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8806 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8807 if (EQ (type
, Qjpeg
))
8808 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8811 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8812 if (EQ (type
, Qtiff
))
8813 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8816 #if defined (HAVE_GIF) || defined (HAVE_NS)
8817 if (EQ (type
, Qgif
))
8818 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8821 #if defined (HAVE_PNG) || defined (HAVE_NS)
8822 if (EQ (type
, Qpng
))
8823 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8826 #if defined (HAVE_RSVG)
8827 if (EQ (type
, Qsvg
))
8828 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8831 #if defined (HAVE_IMAGEMAGICK)
8832 if (EQ (type
, Qimagemagick
))
8833 return CHECK_LIB_AVAILABLE (&imagemagick_type
, init_imagemagick_functions
,
8837 #ifdef HAVE_GHOSTSCRIPT
8838 if (EQ (type
, Qpostscript
))
8839 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8842 /* If the type is not recognized, avoid testing it ever again. */
8843 CACHE_IMAGE_TYPE (type
, Qnil
);
8848 syms_of_image (void)
8850 /* Initialize this only once, since that's what we do with Vimage_types
8851 and they are supposed to be in sync. Initializing here gives correct
8852 operation on GNU/Linux of calling dump-emacs after loading some images. */
8855 /* Must be defined now because we're going to update it below, while
8856 defining the supported image types. */
8857 DEFVAR_LISP ("image-types", Vimage_types
,
8858 doc
: /* List of potentially supported image types.
8859 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8860 To check whether it is really supported, use `image-type-available-p'. */);
8861 Vimage_types
= Qnil
;
8863 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8864 doc
: /* Maximum size of images.
8865 Emacs will not load an image into memory if its pixel width or
8866 pixel height exceeds this limit.
8868 If the value is an integer, it directly specifies the maximum
8869 image height and width, measured in pixels. If it is a floating
8870 point number, it specifies the maximum image height and width
8871 as a ratio to the frame height and width. If the value is
8872 non-numeric, there is no explicit limit on the size of images. */);
8873 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8875 DEFSYM (Qpbm
, "pbm");
8876 ADD_IMAGE_TYPE (Qpbm
);
8878 DEFSYM (Qxbm
, "xbm");
8879 ADD_IMAGE_TYPE (Qxbm
);
8881 define_image_type (&xbm_type
, 1);
8882 define_image_type (&pbm_type
, 1);
8884 DEFSYM (Qcount
, "count");
8885 DEFSYM (Qextension_data
, "extension-data");
8886 DEFSYM (Qdelay
, "delay");
8888 DEFSYM (QCascent
, ":ascent");
8889 DEFSYM (QCmargin
, ":margin");
8890 DEFSYM (QCrelief
, ":relief");
8891 DEFSYM (QCconversion
, ":conversion");
8892 DEFSYM (QCcolor_symbols
, ":color-symbols");
8893 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
8894 DEFSYM (QCindex
, ":index");
8895 DEFSYM (QCgeometry
, ":geometry");
8896 DEFSYM (QCcrop
, ":crop");
8897 DEFSYM (QCrotation
, ":rotation");
8898 DEFSYM (QCmatrix
, ":matrix");
8899 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
8900 DEFSYM (QCmask
, ":mask");
8902 DEFSYM (Qlaplace
, "laplace");
8903 DEFSYM (Qemboss
, "emboss");
8904 DEFSYM (Qedge_detection
, "edge-detection");
8905 DEFSYM (Qheuristic
, "heuristic");
8907 DEFSYM (Qpostscript
, "postscript");
8908 #ifdef HAVE_GHOSTSCRIPT
8909 ADD_IMAGE_TYPE (Qpostscript
);
8910 DEFSYM (QCloader
, ":loader");
8911 DEFSYM (QCbounding_box
, ":bounding-box");
8912 DEFSYM (QCpt_width
, ":pt-width");
8913 DEFSYM (QCpt_height
, ":pt-height");
8914 #endif /* HAVE_GHOSTSCRIPT */
8917 DEFSYM (Qlibpng_version
, "libpng-version");
8918 Fset (Qlibpng_version
,
8920 make_number (PNG_LIBPNG_VER
)
8927 #if defined (HAVE_XPM) || defined (HAVE_NS)
8928 DEFSYM (Qxpm
, "xpm");
8929 ADD_IMAGE_TYPE (Qxpm
);
8932 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8933 DEFSYM (Qjpeg
, "jpeg");
8934 ADD_IMAGE_TYPE (Qjpeg
);
8937 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8938 DEFSYM (Qtiff
, "tiff");
8939 ADD_IMAGE_TYPE (Qtiff
);
8942 #if defined (HAVE_GIF) || defined (HAVE_NS)
8943 DEFSYM (Qgif
, "gif");
8944 ADD_IMAGE_TYPE (Qgif
);
8947 #if defined (HAVE_PNG) || defined (HAVE_NS)
8948 DEFSYM (Qpng
, "png");
8949 ADD_IMAGE_TYPE (Qpng
);
8952 #if defined (HAVE_IMAGEMAGICK)
8953 DEFSYM (Qimagemagick
, "imagemagick");
8954 ADD_IMAGE_TYPE (Qimagemagick
);
8957 #if defined (HAVE_RSVG)
8958 DEFSYM (Qsvg
, "svg");
8959 ADD_IMAGE_TYPE (Qsvg
);
8961 /* Other libraries used directly by svg code. */
8962 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
8963 DEFSYM (Qglib
, "glib");
8964 DEFSYM (Qgobject
, "gobject");
8965 #endif /* HAVE_NTGUI */
8966 #endif /* HAVE_RSVG */
8968 defsubr (&Sinit_image_library
);
8969 #ifdef HAVE_IMAGEMAGICK
8970 defsubr (&Simagemagick_types
);
8972 defsubr (&Sclear_image_cache
);
8973 defsubr (&Simage_flush
);
8974 defsubr (&Simage_size
);
8975 defsubr (&Simage_mask_p
);
8976 defsubr (&Simage_metadata
);
8980 defsubr (&Slookup_image
);
8983 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8984 doc
: /* Non-nil means always draw a cross over disabled images.
8985 Disabled images are those having a `:conversion disabled' property.
8986 A cross is always drawn on black & white displays. */);
8987 cross_disabled_images
= 0;
8989 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8990 doc
: /* List of directories to search for window system bitmap files. */);
8991 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8993 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
8994 doc
: /* Maximum time after which images are removed from the cache.
8995 When an image has not been displayed this many seconds, Emacs
8996 automatically removes it from the image cache. If the cache contains
8997 a large number of images, the actual eviction time may be shorter.
8998 The value can also be nil, meaning the cache is never cleared.
9000 The function `clear-image-cache' disregards this variable. */);
9001 Vimage_cache_eviction_delay
= make_number (300);
9002 #ifdef HAVE_IMAGEMAGICK
9003 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type
,
9004 doc
: /* Integer indicating which ImageMagick rendering method to use.
9006 0 -- the default method (pixel pushing)
9007 1 -- a newer method ("MagickExportImagePixels") that may perform
9008 better (speed etc) in some cases, but has not been as thoroughly
9009 tested with Emacs as the default method. This method requires
9010 ImageMagick version 6.4.6 (approximately) or later.
9012 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9013 imagemagick_render_type
= 0;