1 /* Functions for image support on window system.
3 Copyright (C) 1989, 1992-2014 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>
38 #include "dispextern.h"
39 #include "blockinput.h"
42 #include "character.h"
44 #include "termhooks.h"
47 #ifdef HAVE_SYS_STAT_H
49 #endif /* HAVE_SYS_STAT_H */
51 #ifdef HAVE_SYS_TYPES_H
52 #include <sys/types.h>
53 #endif /* HAVE_SYS_TYPES_H */
55 #ifdef HAVE_WINDOW_SYSTEM
57 #endif /* HAVE_WINDOW_SYSTEM */
60 #define COLOR_TABLE_SUPPORT 1
62 typedef struct x_bitmap_record Bitmap_Record
;
63 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
64 #define NO_PIXMAP None
66 #define PIX_MASK_RETAIN 0
67 #define PIX_MASK_DRAW 1
68 #endif /* HAVE_X_WINDOWS */
72 /* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */
77 /* W32_TODO : Color tables on W32. */
78 #undef COLOR_TABLE_SUPPORT
80 typedef struct w32_bitmap_record Bitmap_Record
;
81 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
84 #define PIX_MASK_RETAIN 0
85 #define PIX_MASK_DRAW 1
87 #define x_defined_color w32_defined_color
88 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
90 /* Versions of libpng, libgif, and libjpeg that we were compiled with,
91 or -1 if no PNG/GIF support was compiled in. This is tested by
92 w32-win.el to correctly set up the alist used to search for the
93 respective image libraries. */
94 Lisp_Object Qlibpng_version
, Qlibgif_version
, Qlibjpeg_version
;
96 #endif /* HAVE_NTGUI */
99 #undef COLOR_TABLE_SUPPORT
101 typedef struct ns_bitmap_record Bitmap_Record
;
103 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
106 #define PIX_MASK_RETAIN 0
107 #define PIX_MASK_DRAW 1
109 #define x_defined_color(f, name, color_def, alloc) \
110 ns_defined_color (f, name, color_def, alloc, 0)
111 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
115 /* The symbol `postscript' identifying images of this type. */
117 static Lisp_Object Qpostscript
;
119 static void x_disable_image (struct frame
*, struct image
*);
120 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
123 static void init_color_table (void);
124 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
125 #ifdef COLOR_TABLE_SUPPORT
126 static void free_color_table (void);
127 static unsigned long *colors_in_color_table (int *n
);
130 static Lisp_Object QCmax_width
, QCmax_height
;
132 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
133 id, which is just an int that this section returns. Bitmaps are
134 reference counted so they can be shared among frames.
136 Bitmap indices are guaranteed to be > 0, so a negative number can
137 be used to indicate no bitmap.
139 If you use x_create_bitmap_from_data, then you must keep track of
140 the bitmaps yourself. That is, creating a bitmap from the same
141 data more than once will not be caught. */
144 /* Use with images created by ns_image_for_XPM. */
146 XGetPixel (XImagePtr ximage
, int x
, int y
)
148 return ns_get_pixel (ximage
, x
, y
);
151 /* Use with images created by ns_image_for_XPM; alpha set to 1;
152 pixel is assumed to be in RGB form. */
154 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
156 ns_put_pixel (ximage
, x
, y
, pixel
);
161 /* Functions to access the contents of a bitmap, given an id. */
164 x_bitmap_height (struct frame
*f
, ptrdiff_t id
)
166 return FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
170 x_bitmap_width (struct frame
*f
, ptrdiff_t id
)
172 return FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
175 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
177 x_bitmap_pixmap (struct frame
*f
, ptrdiff_t id
)
179 /* HAVE_NTGUI needs the explicit cast here. */
180 return (ptrdiff_t) FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
184 #ifdef HAVE_X_WINDOWS
186 x_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
188 return FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
192 /* Allocate a new bitmap record. Returns index of new record. */
195 x_allocate_bitmap_record (struct frame
*f
)
197 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
200 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
201 return ++dpyinfo
->bitmaps_last
;
203 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
204 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
208 xpalloc (dpyinfo
->bitmaps
, &dpyinfo
->bitmaps_size
,
209 10, -1, sizeof *dpyinfo
->bitmaps
);
210 return ++dpyinfo
->bitmaps_last
;
213 /* Add one reference to the reference count of the bitmap with id ID. */
216 x_reference_bitmap (struct frame
*f
, ptrdiff_t id
)
218 ++FRAME_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
221 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
224 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
226 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
229 #ifdef HAVE_X_WINDOWS
231 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
232 bits
, width
, height
);
235 #endif /* HAVE_X_WINDOWS */
239 bitmap
= CreateBitmap (width
, height
,
240 FRAME_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
241 FRAME_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
245 #endif /* HAVE_NTGUI */
248 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
253 id
= x_allocate_bitmap_record (f
);
256 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
257 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
260 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
261 dpyinfo
->bitmaps
[id
- 1].height
= height
;
262 dpyinfo
->bitmaps
[id
- 1].width
= width
;
263 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
265 #ifdef HAVE_X_WINDOWS
266 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
267 dpyinfo
->bitmaps
[id
- 1].have_mask
= false;
268 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
269 #endif /* HAVE_X_WINDOWS */
272 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
273 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
274 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
275 #endif /* HAVE_NTGUI */
280 /* Create bitmap from file FILE for frame F. */
283 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
285 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
288 return -1; /* W32_TODO : bitmap support */
289 #endif /* HAVE_NTGUI */
293 void *bitmap
= ns_image_from_file (file
);
299 id
= x_allocate_bitmap_record (f
);
300 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
301 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
302 dpyinfo
->bitmaps
[id
- 1].file
= xlispstrdup (file
);
303 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
304 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
305 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
309 #ifdef HAVE_X_WINDOWS
310 unsigned int width
, height
;
312 int xhot
, yhot
, result
;
317 /* Look for an existing bitmap with the same name. */
318 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
320 if (dpyinfo
->bitmaps
[id
].refcount
321 && dpyinfo
->bitmaps
[id
].file
322 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
324 ++dpyinfo
->bitmaps
[id
].refcount
;
329 /* Search bitmap-file-path for the file, if appropriate. */
330 if (openp (Vx_bitmap_file_path
, file
, Qnil
, &found
,
331 make_number (R_OK
), false)
335 filename
= SSDATA (found
);
337 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
338 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
339 if (result
!= BitmapSuccess
)
342 id
= x_allocate_bitmap_record (f
);
343 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
344 dpyinfo
->bitmaps
[id
- 1].have_mask
= false;
345 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
346 dpyinfo
->bitmaps
[id
- 1].file
= xlispstrdup (file
);
347 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
348 dpyinfo
->bitmaps
[id
- 1].height
= height
;
349 dpyinfo
->bitmaps
[id
- 1].width
= width
;
352 #endif /* HAVE_X_WINDOWS */
358 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
360 #ifdef HAVE_X_WINDOWS
361 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
363 XFreePixmap (dpyinfo
->display
, bm
->mask
);
364 #endif /* HAVE_X_WINDOWS */
367 DeleteObject (bm
->pixmap
);
368 #endif /* HAVE_NTGUI */
371 ns_release_object (bm
->img
);
381 /* Remove reference to bitmap with id number ID. */
384 x_destroy_bitmap (struct frame
*f
, ptrdiff_t id
)
386 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
390 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
392 if (--bm
->refcount
== 0)
395 free_bitmap_record (dpyinfo
, bm
);
401 /* Free all the bitmaps for the display specified by DPYINFO. */
404 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
407 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
409 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
410 if (bm
->refcount
> 0)
411 free_bitmap_record (dpyinfo
, bm
);
413 dpyinfo
->bitmaps_last
= 0;
416 static bool x_create_x_image_and_pixmap (struct frame
*, int, int, int,
417 XImagePtr
*, Pixmap
*);
418 static void x_destroy_x_image (XImagePtr ximg
);
421 static XImagePtr_or_DC
image_get_x_image_or_dc (struct frame
*, struct image
*,
423 static void image_unget_x_image_or_dc (struct image
*, bool, XImagePtr_or_DC
,
426 static XImagePtr
image_get_x_image (struct frame
*, struct image
*, bool);
427 static void image_unget_x_image (struct image
*, bool, XImagePtr
);
428 #define image_get_x_image_or_dc(f, img, mask_p, dummy) \
429 image_get_x_image (f, img, mask_p)
430 #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \
431 image_unget_x_image (img, mask_p, ximg)
434 #ifdef HAVE_X_WINDOWS
436 static void image_sync_to_pixmaps (struct frame
*, struct image
*);
438 /* Useful functions defined in the section
439 `Image type independent image structures' below. */
441 static unsigned long four_corners_best (XImagePtr ximg
,
444 unsigned long height
);
447 /* Create a mask of a bitmap. Note is this not a perfect mask.
448 It's nicer with some borders in this context */
451 x_create_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
454 XImagePtr ximg
, mask_img
;
455 unsigned long width
, height
;
458 unsigned long x
, y
, xp
, xm
, yp
, ym
;
461 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
466 pixmap
= x_bitmap_pixmap (f
, id
);
467 width
= x_bitmap_width (f
, id
);
468 height
= x_bitmap_height (f
, id
);
471 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
480 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
485 XDestroyImage (ximg
);
489 bg
= four_corners_best (ximg
, NULL
, width
, height
);
491 for (y
= 0; y
< ximg
->height
; ++y
)
493 for (x
= 0; x
< ximg
->width
; ++x
)
495 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
496 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
497 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
498 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
499 if (XGetPixel (ximg
, x
, y
) == bg
500 && XGetPixel (ximg
, x
, yp
) == bg
501 && XGetPixel (ximg
, x
, ym
) == bg
502 && XGetPixel (ximg
, xp
, y
) == bg
503 && XGetPixel (ximg
, xp
, yp
) == bg
504 && XGetPixel (ximg
, xp
, ym
) == bg
505 && XGetPixel (ximg
, xm
, y
) == bg
506 && XGetPixel (ximg
, xm
, yp
) == bg
507 && XGetPixel (ximg
, xm
, ym
) == bg
)
508 XPutPixel (mask_img
, x
, y
, 0);
510 XPutPixel (mask_img
, x
, y
, 1);
514 eassert (input_blocked_p ());
515 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
516 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
518 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
520 dpyinfo
->bitmaps
[id
- 1].have_mask
= true;
521 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
523 XDestroyImage (ximg
);
524 x_destroy_x_image (mask_img
);
527 #endif /* HAVE_X_WINDOWS */
530 /***********************************************************************
532 ***********************************************************************/
534 /* List of supported image types. Use define_image_type to add new
535 types. Use lookup_image_type to find a type for a given symbol. */
537 static struct image_type
*image_types
;
539 /* The symbol `xbm' which is used as the type symbol for XBM images. */
541 static Lisp_Object Qxbm
;
545 Lisp_Object QCascent
, QCmargin
, QCrelief
;
546 Lisp_Object QCconversion
;
547 static Lisp_Object QCheuristic_mask
;
548 static Lisp_Object QCcolor_symbols
;
549 static Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
;
550 static Lisp_Object QCcrop
, QCrotation
;
554 static Lisp_Object Qcount
, Qextension_data
, Qdelay
;
555 static Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
557 /* Forward function prototypes. */
559 static struct image_type
*lookup_image_type (Lisp_Object
);
560 static void x_laplace (struct frame
*, struct image
*);
561 static void x_emboss (struct frame
*, struct image
*);
562 static void x_build_heuristic_mask (struct frame
*, struct image
*,
565 #define CACHE_IMAGE_TYPE(type, status) \
566 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
568 #define CACHE_IMAGE_TYPE(type, status)
571 #define ADD_IMAGE_TYPE(type) \
572 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
574 /* Define a new image type from TYPE. This adds a copy of TYPE to
575 image_types and caches the loading status of TYPE. */
577 static struct image_type
*
578 define_image_type (struct image_type
*type
)
580 struct image_type
*p
= NULL
;
581 Lisp_Object target_type
= *type
->type
;
586 for (p
= image_types
; p
; p
= p
->next
)
587 if (EQ (*p
->type
, target_type
))
592 #if defined HAVE_NTGUI && defined WINDOWSNT
593 /* If we failed to load the library before, don't try again. */
594 Lisp_Object tested
= Fassq (target_type
, Vlibrary_cache
);
595 if (CONSP (tested
) && NILP (XCDR (tested
)))
600 type_valid
= type
->init ();
601 CACHE_IMAGE_TYPE (target_type
, type_valid
? Qt
: Qnil
);
607 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
608 The initialized data segment is read-only. */
609 p
= xmalloc (sizeof *p
);
611 p
->next
= image_types
;
621 /* Value is true if OBJECT is a valid Lisp image specification. A
622 valid image specification is a list whose car is the symbol
623 `image', and whose rest is a property list. The property list must
624 contain a value for key `:type'. That value must be the name of a
625 supported image type. The rest of the property list depends on the
629 valid_image_p (Lisp_Object object
)
637 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
638 if (EQ (XCAR (tem
), QCtype
))
641 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
643 struct image_type
*type
;
644 type
= lookup_image_type (XCAR (tem
));
646 valid_p
= type
->valid_p (object
);
657 /* Log error message with format string FORMAT and argument ARG.
658 Signaling an error, e.g. when an image cannot be loaded, is not a
659 good idea because this would interrupt redisplay, and the error
660 message display would lead to another redisplay. This function
661 therefore simply displays a message. */
664 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
666 add_to_log (format
, arg1
, arg2
);
671 /***********************************************************************
673 ***********************************************************************/
675 enum image_value_type
677 IMAGE_DONT_CHECK_VALUE_TYPE
,
679 IMAGE_STRING_OR_NIL_VALUE
,
681 IMAGE_POSITIVE_INTEGER_VALUE
,
682 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
,
683 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
686 IMAGE_FUNCTION_VALUE
,
691 /* Structure used when parsing image specifications. */
695 /* Name of keyword. */
698 /* The type of value allowed. */
699 enum image_value_type type
;
701 /* True means key must be present. */
704 /* Used to recognize duplicate keywords in a property list. */
707 /* The value that was found. */
712 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
713 has the format (image KEYWORD VALUE ...). One of the keyword/
714 value pairs must be `:type TYPE'. KEYWORDS is a vector of
715 image_keywords structures of size NKEYWORDS describing other
716 allowed keyword/value pairs. Value is true if SPEC is valid. */
719 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
720 int nkeywords
, Lisp_Object type
)
729 while (CONSP (plist
))
731 Lisp_Object key
, value
;
733 /* First element of a pair must be a symbol. */
735 plist
= XCDR (plist
);
739 /* There must follow a value. */
742 value
= XCAR (plist
);
743 plist
= XCDR (plist
);
745 /* Find key in KEYWORDS. Error if not found. */
746 for (i
= 0; i
< nkeywords
; ++i
)
747 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
753 /* Record that we recognized the keyword. If a keywords
754 was found more than once, it's an error. */
755 keywords
[i
].value
= value
;
756 if (keywords
[i
].count
> 1)
760 /* Check type of value against allowed type. */
761 switch (keywords
[i
].type
)
763 case IMAGE_STRING_VALUE
:
764 if (!STRINGP (value
))
768 case IMAGE_STRING_OR_NIL_VALUE
:
769 if (!STRINGP (value
) && !NILP (value
))
773 case IMAGE_SYMBOL_VALUE
:
774 if (!SYMBOLP (value
))
778 case IMAGE_POSITIVE_INTEGER_VALUE
:
779 if (! RANGED_INTEGERP (1, value
, INT_MAX
))
783 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
:
784 if (RANGED_INTEGERP (0, value
, INT_MAX
))
787 && RANGED_INTEGERP (0, XCAR (value
), INT_MAX
)
788 && RANGED_INTEGERP (0, XCDR (value
), INT_MAX
))
792 case IMAGE_ASCENT_VALUE
:
793 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
795 else if (RANGED_INTEGERP (0, value
, 100))
799 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
800 /* Unlike the other integer-related cases, this one does not
801 verify that VALUE fits in 'int'. This is because callers
803 if (!INTEGERP (value
) || XINT (value
) < 0)
807 case IMAGE_DONT_CHECK_VALUE_TYPE
:
810 case IMAGE_FUNCTION_VALUE
:
811 value
= indirect_function (value
);
812 if (!NILP (Ffunctionp (value
)))
816 case IMAGE_NUMBER_VALUE
:
817 if (!INTEGERP (value
) && !FLOATP (value
))
821 case IMAGE_INTEGER_VALUE
:
822 if (! TYPE_RANGED_INTEGERP (int, value
))
826 case IMAGE_BOOL_VALUE
:
827 if (!NILP (value
) && !EQ (value
, Qt
))
836 if (EQ (key
, QCtype
) && !EQ (type
, value
))
840 /* Check that all mandatory fields are present. */
841 for (i
= 0; i
< nkeywords
; ++i
)
842 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
849 /* Return the value of KEY in image specification SPEC. Value is nil
850 if KEY is not present in SPEC. Set *FOUND depending on whether KEY
851 was found in SPEC. */
854 image_spec_value (Lisp_Object spec
, Lisp_Object key
, bool *found
)
858 eassert (valid_image_p (spec
));
860 for (tail
= XCDR (spec
);
861 CONSP (tail
) && CONSP (XCDR (tail
));
862 tail
= XCDR (XCDR (tail
)))
864 if (EQ (XCAR (tail
), key
))
868 return XCAR (XCDR (tail
));
878 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
879 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
880 PIXELS non-nil means return the size in pixels, otherwise return the
881 size in canonical character units.
882 FRAME is the frame on which the image will be displayed. FRAME nil
883 or omitted means use the selected frame. */)
884 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
889 if (valid_image_p (spec
))
891 struct frame
*f
= decode_window_system_frame (frame
);
892 ptrdiff_t id
= lookup_image (f
, spec
);
893 struct image
*img
= IMAGE_FROM_ID (f
, id
);
894 int width
= img
->width
+ 2 * img
->hmargin
;
895 int height
= img
->height
+ 2 * img
->vmargin
;
898 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
899 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
901 size
= Fcons (make_number (width
), make_number (height
));
904 error ("Invalid image specification");
910 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
911 doc
: /* Return t if image SPEC has a mask bitmap.
912 FRAME is the frame on which the image will be displayed. FRAME nil
913 or omitted means use the selected frame. */)
914 (Lisp_Object spec
, Lisp_Object frame
)
919 if (valid_image_p (spec
))
921 struct frame
*f
= decode_window_system_frame (frame
);
922 ptrdiff_t id
= lookup_image (f
, spec
);
923 struct image
*img
= IMAGE_FROM_ID (f
, id
);
928 error ("Invalid image specification");
933 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
934 doc
: /* Return metadata for image SPEC.
935 FRAME is the frame on which the image will be displayed. FRAME nil
936 or omitted means use the selected frame. */)
937 (Lisp_Object spec
, Lisp_Object frame
)
942 if (valid_image_p (spec
))
944 struct frame
*f
= decode_window_system_frame (frame
);
945 ptrdiff_t id
= lookup_image (f
, spec
);
946 struct image
*img
= IMAGE_FROM_ID (f
, id
);
947 ext
= img
->lisp_data
;
954 /***********************************************************************
955 Image type independent image structures
956 ***********************************************************************/
958 #define MAX_IMAGE_SIZE 10.0
959 /* Allocate and return a new image structure for image specification
960 SPEC. SPEC has a hash value of HASH. */
962 static struct image
*
963 make_image (Lisp_Object spec
, EMACS_UINT hash
)
965 struct image
*img
= xzalloc (sizeof *img
);
966 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
968 eassert (valid_image_p (spec
));
969 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
970 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
971 eassert (img
->type
!= NULL
);
973 img
->lisp_data
= Qnil
;
974 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
976 img
->corners
[BOT_CORNER
] = -1; /* Full image */
981 /* Free image IMG which was used on frame F, including its resources. */
984 free_image (struct frame
*f
, struct image
*img
)
988 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
990 /* Remove IMG from the hash table of its cache. */
992 img
->prev
->next
= img
->next
;
994 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
997 img
->next
->prev
= img
->prev
;
999 c
->images
[img
->id
] = NULL
;
1001 /* Free resources, then free IMG. */
1002 img
->type
->free (f
, img
);
1007 /* Return true if the given widths and heights are valid for display. */
1010 check_image_size (struct frame
*f
, int width
, int height
)
1014 if (width
<= 0 || height
<= 0)
1017 if (INTEGERP (Vmax_image_size
))
1018 return (width
<= XINT (Vmax_image_size
)
1019 && height
<= XINT (Vmax_image_size
));
1020 else if (FLOATP (Vmax_image_size
))
1024 w
= FRAME_PIXEL_WIDTH (f
);
1025 h
= FRAME_PIXEL_HEIGHT (f
);
1028 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1029 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1030 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1036 /* Prepare image IMG for display on frame F. Must be called before
1037 drawing an image. */
1040 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1042 /* We're about to display IMG, so set its timestamp to `now'. */
1043 img
->timestamp
= current_timespec ();
1045 /* If IMG doesn't have a pixmap yet, load it now, using the image
1046 type dependent loader function. */
1047 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1048 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1050 #ifdef HAVE_X_WINDOWS
1051 if (!img
->load_failed_p
)
1054 image_sync_to_pixmaps (f
, img
);
1061 /* Value is the number of pixels for the ascent of image IMG when
1062 drawn in face FACE. */
1065 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1070 if (slice
->height
== img
->height
)
1071 height
= img
->height
+ img
->vmargin
;
1072 else if (slice
->y
== 0)
1073 height
= slice
->height
+ img
->vmargin
;
1075 height
= slice
->height
;
1077 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1082 /* W32 specific version. Why?. ++kfs */
1083 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1084 - FONT_BASE (face
->font
)) / 2;
1086 /* This expression is arranged so that if the image can't be
1087 exactly centered, it will be moved slightly up. This is
1088 because a typical font is `top-heavy' (due to the presence
1089 uppercase letters), so the image placement should err towards
1090 being top-heavy too. It also just generally looks better. */
1091 ascent
= (height
+ FONT_BASE (face
->font
)
1092 - FONT_DESCENT (face
->font
) + 1) / 2;
1093 #endif /* HAVE_NTGUI */
1096 ascent
= height
/ 2;
1099 ascent
= height
* (img
->ascent
/ 100.0);
1105 /* Image background colors. */
1107 /* Find the "best" corner color of a bitmap.
1108 On W32, XIMG is assumed to a device context with the bitmap selected. */
1110 static RGB_PIXEL_COLOR
1111 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1112 unsigned long width
, unsigned long height
)
1114 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1117 if (corners
&& corners
[BOT_CORNER
] >= 0)
1119 /* Get the colors at the corner_pixels of ximg. */
1120 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1121 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1122 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1123 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1127 /* Get the colors at the corner_pixels of ximg. */
1128 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1129 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1130 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1131 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1133 /* Choose the most frequently found color as background. */
1134 for (i
= best_count
= 0; i
< 4; ++i
)
1138 for (j
= n
= 0; j
< 4; ++j
)
1139 if (corner_pixels
[i
] == corner_pixels
[j
])
1143 best
= corner_pixels
[i
], best_count
= n
;
1149 /* Portability macros */
1153 #define Free_Pixmap(display, pixmap) \
1154 DeleteObject (pixmap)
1156 #elif defined (HAVE_NS)
1158 #define Free_Pixmap(display, pixmap) \
1159 ns_release_object (pixmap)
1163 #define Free_Pixmap(display, pixmap) \
1164 XFreePixmap (display, pixmap)
1166 #endif /* !HAVE_NTGUI && !HAVE_NS */
1169 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1170 it is guessed heuristically. If non-zero, XIMG is an existing
1171 XImage object (or device context with the image selected on W32) to
1172 use for the heuristic. */
1175 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1177 if (! img
->background_valid
)
1178 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1180 bool free_ximg
= !ximg
;
1183 #endif /* HAVE_NTGUI */
1186 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
1188 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1191 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
1193 img
->background_valid
= 1;
1196 return img
->background
;
1199 /* Return the `background_transparent' field of IMG. If IMG doesn't
1200 have one yet, it is guessed heuristically. If non-zero, MASK is an
1201 existing XImage object to use for the heuristic. */
1204 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1206 if (! img
->background_transparent_valid
)
1207 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1211 bool free_mask
= !mask
;
1214 #endif /* HAVE_NTGUI */
1217 mask
= image_get_x_image_or_dc (f
, img
, 1, &prev
);
1219 img
->background_transparent
1220 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1223 image_unget_x_image_or_dc (img
, 1, mask
, prev
);
1226 img
->background_transparent
= 0;
1228 img
->background_transparent_valid
= 1;
1231 return img
->background_transparent
;
1235 /***********************************************************************
1236 Helper functions for X image types
1237 ***********************************************************************/
1239 /* Clear X resources of image IMG on frame F according to FLAGS.
1240 FLAGS is bitwise-or of the following masks:
1241 CLEAR_IMAGE_PIXMAP free the pixmap if any.
1242 CLEAR_IMAGE_MASK means clear the mask pixmap if any.
1243 CLEAR_IMAGE_COLORS means free colors allocated for the image, if
1246 #define CLEAR_IMAGE_PIXMAP (1 << 0)
1247 #define CLEAR_IMAGE_MASK (1 << 1)
1248 #define CLEAR_IMAGE_COLORS (1 << 2)
1251 x_clear_image_1 (struct frame
*f
, struct image
*img
, int flags
)
1253 if (flags
& CLEAR_IMAGE_PIXMAP
)
1257 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1258 img
->pixmap
= NO_PIXMAP
;
1259 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1260 img
->background_valid
= 0;
1262 #ifdef HAVE_X_WINDOWS
1265 x_destroy_x_image (img
->ximg
);
1267 img
->background_valid
= 0;
1272 if (flags
& CLEAR_IMAGE_MASK
)
1276 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1277 img
->mask
= NO_PIXMAP
;
1278 img
->background_transparent_valid
= 0;
1280 #ifdef HAVE_X_WINDOWS
1283 x_destroy_x_image (img
->mask_img
);
1284 img
->mask_img
= NULL
;
1285 img
->background_transparent_valid
= 0;
1290 if ((flags
& CLEAR_IMAGE_COLORS
) && img
->ncolors
)
1292 /* W32_TODO: color table support. */
1293 #ifdef HAVE_X_WINDOWS
1294 x_free_colors (f
, img
->colors
, img
->ncolors
);
1295 #endif /* HAVE_X_WINDOWS */
1296 xfree (img
->colors
);
1303 /* Free X resources of image IMG which is used on frame F. */
1306 x_clear_image (struct frame
*f
, struct image
*img
)
1309 x_clear_image_1 (f
, img
,
1310 CLEAR_IMAGE_PIXMAP
| CLEAR_IMAGE_MASK
| CLEAR_IMAGE_COLORS
);
1315 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1316 cannot be allocated, use DFLT. Add a newly allocated color to
1317 IMG->colors, so that it can be freed again. Value is the pixel
1320 static unsigned long
1321 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1325 unsigned long result
;
1327 eassert (STRINGP (color_name
));
1329 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1330 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1333 /* This isn't called frequently so we get away with simply
1334 reallocating the color vector to the needed size, here. */
1335 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1336 img
->colors
= xrealloc (img
->colors
, ncolors
* sizeof *img
->colors
);
1337 img
->colors
[ncolors
- 1] = color
.pixel
;
1338 img
->ncolors
= ncolors
;
1339 result
= color
.pixel
;
1349 /***********************************************************************
1351 ***********************************************************************/
1353 static void cache_image (struct frame
*f
, struct image
*img
);
1355 /* Return a new, initialized image cache that is allocated from the
1356 heap. Call free_image_cache to free an image cache. */
1358 struct image_cache
*
1359 make_image_cache (void)
1361 struct image_cache
*c
= xmalloc (sizeof *c
);
1364 c
->used
= c
->refcount
= 0;
1365 c
->images
= xmalloc (c
->size
* sizeof *c
->images
);
1366 c
->buckets
= xzalloc (IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
);
1371 /* Find an image matching SPEC in the cache, and return it. If no
1372 image is found, return NULL. */
1373 static struct image
*
1374 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1377 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1378 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1380 if (!c
) return NULL
;
1382 /* If the image spec does not specify a background color, the cached
1383 image must have the same background color as the current frame.
1384 The foreground color must also match, for the sake of monochrome
1387 In fact, we could ignore the foreground color matching condition
1388 for color images, or if the image spec specifies :foreground;
1389 similarly we could ignore the background color matching condition
1390 for formats that don't use transparency (such as jpeg), or if the
1391 image spec specifies :background. However, the extra memory
1392 usage is probably negligible in practice, so we don't bother. */
1394 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1395 if (img
->hash
== hash
1396 && !NILP (Fequal (img
->spec
, spec
))
1397 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1398 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1404 /* Search frame F for an image with spec SPEC, and free it. */
1407 uncache_image (struct frame
*f
, Lisp_Object spec
)
1409 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1412 free_image (f
, img
);
1413 /* As display glyphs may still be referring to the image ID, we
1414 must garbage the frame (Bug#6426). */
1415 SET_FRAME_GARBAGED (f
);
1420 /* Free image cache of frame F. Be aware that X frames share images
1424 free_image_cache (struct frame
*f
)
1426 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1431 /* Cache should not be referenced by any frame when freed. */
1432 eassert (c
->refcount
== 0);
1434 for (i
= 0; i
< c
->used
; ++i
)
1435 free_image (f
, c
->images
[i
]);
1439 FRAME_IMAGE_CACHE (f
) = NULL
;
1444 /* Clear image cache of frame F. FILTER=t means free all images.
1445 FILTER=nil means clear only images that haven't been
1446 displayed for some time.
1447 Else, only free the images which have FILTER in their `dependencies'.
1448 Should be called from time to time to reduce the number of loaded images.
1449 If image-cache-eviction-delay is non-nil, this frees images in the cache
1450 which weren't displayed for at least that many seconds. */
1453 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1455 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1459 ptrdiff_t i
, nfreed
= 0;
1461 /* Block input so that we won't be interrupted by a SIGIO
1462 while being in an inconsistent state. */
1467 /* Filter image cache. */
1468 for (i
= 0; i
< c
->used
; ++i
)
1470 struct image
*img
= c
->images
[i
];
1471 if (img
&& (EQ (Qt
, filter
)
1472 || !NILP (Fmember (filter
, img
->dependencies
))))
1474 free_image (f
, img
);
1479 else if (INTEGERP (Vimage_cache_eviction_delay
))
1481 /* Free cache based on timestamp. */
1482 struct timespec old
, t
;
1484 ptrdiff_t nimages
= 0;
1486 for (i
= 0; i
< c
->used
; ++i
)
1490 /* If the number of cached images has grown unusually large,
1491 decrease the cache eviction delay (Bug#6230). */
1492 delay
= XINT (Vimage_cache_eviction_delay
);
1494 delay
= 1600 * delay
/ nimages
/ nimages
;
1495 delay
= max (delay
, 1);
1497 t
= current_timespec ();
1498 old
= timespec_sub (t
, dtotimespec (delay
));
1500 for (i
= 0; i
< c
->used
; ++i
)
1502 struct image
*img
= c
->images
[i
];
1503 if (img
&& timespec_cmp (img
->timestamp
, old
) < 0)
1505 free_image (f
, img
);
1511 /* We may be clearing the image cache because, for example,
1512 Emacs was iconified for a longer period of time. In that
1513 case, current matrices may still contain references to
1514 images freed above. So, clear these matrices. */
1517 Lisp_Object tail
, frame
;
1519 FOR_EACH_FRAME (tail
, frame
)
1521 struct frame
*fr
= XFRAME (frame
);
1522 if (FRAME_IMAGE_CACHE (fr
) == c
)
1523 clear_current_matrices (fr
);
1526 windows_or_buffers_changed
= 19;
1534 clear_image_caches (Lisp_Object filter
)
1536 /* FIXME: We want to do
1537 * struct terminal *t;
1538 * for (t = terminal_list; t; t = t->next_terminal)
1539 * clear_image_cache (t, filter); */
1540 Lisp_Object tail
, frame
;
1541 FOR_EACH_FRAME (tail
, frame
)
1542 if (FRAME_WINDOW_P (XFRAME (frame
)))
1543 clear_image_cache (XFRAME (frame
), filter
);
1546 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1548 doc
: /* Clear the image cache.
1549 FILTER nil or a frame means clear all images in the selected frame.
1550 FILTER t means clear the image caches of all frames.
1551 Anything else, means only clear those images which refer to FILTER,
1552 which is then usually a filename. */)
1553 (Lisp_Object filter
)
1555 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1556 clear_image_caches (filter
);
1558 clear_image_cache (decode_window_system_frame (filter
), Qt
);
1564 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1566 doc
: /* Flush the image with specification SPEC on frame FRAME.
1567 This removes the image from the Emacs image cache. If SPEC specifies
1568 an image file, the next redisplay of this image will read from the
1569 current contents of that file.
1571 FRAME nil or omitted means use the selected frame.
1572 FRAME t means refresh the image on all frames. */)
1573 (Lisp_Object spec
, Lisp_Object frame
)
1575 if (!valid_image_p (spec
))
1576 error ("Invalid image specification");
1581 FOR_EACH_FRAME (tail
, frame
)
1583 struct frame
*f
= XFRAME (frame
);
1584 if (FRAME_WINDOW_P (f
))
1585 uncache_image (f
, spec
);
1589 uncache_image (decode_window_system_frame (frame
), spec
);
1595 /* Compute masks and transform image IMG on frame F, as specified
1596 by the image's specification, */
1599 postprocess_image (struct frame
*f
, struct image
*img
)
1601 /* Manipulation of the image's mask. */
1604 Lisp_Object conversion
, spec
;
1609 /* `:heuristic-mask t'
1611 means build a mask heuristically.
1612 `:heuristic-mask (R G B)'
1613 `:mask (heuristic (R G B))'
1614 means build a mask from color (R G B) in the
1617 means remove a mask, if any. */
1619 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1621 x_build_heuristic_mask (f
, img
, mask
);
1626 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1628 if (EQ (mask
, Qheuristic
))
1629 x_build_heuristic_mask (f
, img
, Qt
);
1630 else if (CONSP (mask
)
1631 && EQ (XCAR (mask
), Qheuristic
))
1633 if (CONSP (XCDR (mask
)))
1634 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1636 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1638 else if (NILP (mask
) && found_p
&& img
->mask
)
1639 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
1643 /* Should we apply an image transformation algorithm? */
1644 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1645 if (EQ (conversion
, Qdisabled
))
1646 x_disable_image (f
, img
);
1647 else if (EQ (conversion
, Qlaplace
))
1649 else if (EQ (conversion
, Qemboss
))
1651 else if (CONSP (conversion
)
1652 && EQ (XCAR (conversion
), Qedge_detection
))
1655 tem
= XCDR (conversion
);
1657 x_edge_detection (f
, img
,
1658 Fplist_get (tem
, QCmatrix
),
1659 Fplist_get (tem
, QCcolor_adjustment
));
1665 /* Return the id of image with Lisp specification SPEC on frame F.
1666 SPEC must be a valid Lisp image specification (see valid_image_p). */
1669 lookup_image (struct frame
*f
, Lisp_Object spec
)
1674 /* F must be a window-system frame, and SPEC must be a valid image
1676 eassert (FRAME_WINDOW_P (f
));
1677 eassert (valid_image_p (spec
));
1679 /* Look up SPEC in the hash table of the image cache. */
1680 hash
= sxhash (spec
, 0);
1681 img
= search_image_cache (f
, spec
, hash
);
1682 if (img
&& img
->load_failed_p
)
1684 free_image (f
, img
);
1688 /* If not found, create a new image and cache it. */
1692 img
= make_image (spec
, hash
);
1693 cache_image (f
, img
);
1694 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1695 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1696 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1698 /* If we can't load the image, and we don't have a width and
1699 height, use some arbitrary width and height so that we can
1700 draw a rectangle for it. */
1701 if (img
->load_failed_p
)
1705 value
= image_spec_value (spec
, QCwidth
, NULL
);
1706 img
->width
= (INTEGERP (value
)
1707 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1708 value
= image_spec_value (spec
, QCheight
, NULL
);
1709 img
->height
= (INTEGERP (value
)
1710 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1714 /* Handle image type independent image attributes
1715 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1716 `:background COLOR'. */
1717 Lisp_Object ascent
, margin
, relief
, bg
;
1720 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1721 if (INTEGERP (ascent
))
1722 img
->ascent
= XFASTINT (ascent
);
1723 else if (EQ (ascent
, Qcenter
))
1724 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1726 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1727 if (INTEGERP (margin
))
1728 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1729 else if (CONSP (margin
))
1731 img
->hmargin
= XFASTINT (XCAR (margin
));
1732 img
->vmargin
= XFASTINT (XCDR (margin
));
1735 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1736 relief_bound
= INT_MAX
- max (img
->hmargin
, img
->vmargin
);
1737 if (RANGED_INTEGERP (- relief_bound
, relief
, relief_bound
))
1739 img
->relief
= XINT (relief
);
1740 img
->hmargin
+= eabs (img
->relief
);
1741 img
->vmargin
+= eabs (img
->relief
);
1744 if (! img
->background_valid
)
1746 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1750 = x_alloc_image_color (f
, img
, bg
,
1751 FRAME_BACKGROUND_PIXEL (f
));
1752 img
->background_valid
= 1;
1756 /* Do image transformations and compute masks, unless we
1757 don't have the image yet. */
1758 if (!EQ (*img
->type
->type
, Qpostscript
))
1759 postprocess_image (f
, img
);
1765 /* We're using IMG, so set its timestamp to `now'. */
1766 img
->timestamp
= current_timespec ();
1768 /* Value is the image id. */
1773 /* Cache image IMG in the image cache of frame F. */
1776 cache_image (struct frame
*f
, struct image
*img
)
1778 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1781 /* Find a free slot in c->images. */
1782 for (i
= 0; i
< c
->used
; ++i
)
1783 if (c
->images
[i
] == NULL
)
1786 /* If no free slot found, maybe enlarge c->images. */
1787 if (i
== c
->used
&& c
->used
== c
->size
)
1788 c
->images
= xpalloc (c
->images
, &c
->size
, 1, -1, sizeof *c
->images
);
1790 /* Add IMG to c->images, and assign IMG an id. */
1796 /* Add IMG to the cache's hash table. */
1797 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1798 img
->next
= c
->buckets
[i
];
1800 img
->next
->prev
= img
;
1802 c
->buckets
[i
] = img
;
1806 /* Call FN on every image in the image cache of frame F. Used to mark
1807 Lisp Objects in the image cache. */
1809 /* Mark Lisp objects in image IMG. */
1812 mark_image (struct image
*img
)
1814 mark_object (img
->spec
);
1815 mark_object (img
->dependencies
);
1817 if (!NILP (img
->lisp_data
))
1818 mark_object (img
->lisp_data
);
1823 mark_image_cache (struct image_cache
*c
)
1828 for (i
= 0; i
< c
->used
; ++i
)
1830 mark_image (c
->images
[i
]);
1836 /***********************************************************************
1837 X / NS / W32 support code
1838 ***********************************************************************/
1842 /* Macro for defining functions that will be loaded from image DLLs. */
1843 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1845 /* Macro for loading those image functions from the library. */
1846 #define LOAD_IMGLIB_FN(lib,func) { \
1847 fn_##func = (void *) GetProcAddress (lib, #func); \
1848 if (!fn_##func) return 0; \
1851 #endif /* WINDOWSNT */
1853 /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the
1855 WIDTH and HEIGHT must both be positive.
1856 If XIMG is null, assume it is a bitmap. */
1858 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1860 #ifdef HAVE_X_WINDOWS
1861 /* Respect Xlib's limits: it cannot deal with images that have more
1862 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1863 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1866 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1867 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1870 int bitmap_pad
, depth
, bytes_per_line
;
1873 bitmap_pad
= ximg
->bitmap_pad
;
1874 depth
= ximg
->depth
;
1875 bytes_per_line
= ximg
->bytes_per_line
;
1881 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1883 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1884 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1886 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1887 For now, assume that every image size is allowed on these systems. */
1892 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1893 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1894 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1895 via xmalloc. Print error messages via image_error if an error
1896 occurs. Value is true if successful.
1898 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1899 should indicate the bit depth of the image. */
1902 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1903 XImagePtr
*ximg
, Pixmap
*pixmap
)
1905 #ifdef HAVE_X_WINDOWS
1906 Display
*display
= FRAME_X_DISPLAY (f
);
1907 Window window
= FRAME_X_WINDOW (f
);
1908 Screen
*screen
= FRAME_X_SCREEN (f
);
1910 eassert (input_blocked_p ());
1913 depth
= DefaultDepthOfScreen (screen
);
1914 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1915 depth
, ZPixmap
, 0, NULL
, width
, height
,
1916 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1919 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1923 if (! x_check_image_size (*ximg
, width
, height
))
1925 x_destroy_x_image (*ximg
);
1927 image_error ("Image too large (%dx%d)",
1928 make_number (width
), make_number (height
));
1932 /* Allocate image raster. */
1933 (*ximg
)->data
= xmalloc ((*ximg
)->bytes_per_line
* height
);
1935 /* Allocate a pixmap of the same size. */
1936 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1937 if (*pixmap
== NO_PIXMAP
)
1939 x_destroy_x_image (*ximg
);
1941 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1946 #endif /* HAVE_X_WINDOWS */
1950 BITMAPINFOHEADER
*header
;
1952 int scanline_width_bits
;
1954 int palette_colors
= 0;
1959 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1960 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1962 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
1966 scanline_width_bits
= width
* depth
;
1967 remainder
= scanline_width_bits
% 32;
1970 scanline_width_bits
+= 32 - remainder
;
1972 /* Bitmaps with a depth less than 16 need a palette. */
1973 /* BITMAPINFO structure already contains the first RGBQUAD. */
1975 palette_colors
= 1 << (depth
- 1);
1977 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
1979 header
= &(*ximg
)->info
.bmiHeader
;
1980 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
1981 header
->biSize
= sizeof (*header
);
1982 header
->biWidth
= width
;
1983 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
1984 header
->biPlanes
= 1;
1985 header
->biBitCount
= depth
;
1986 header
->biCompression
= BI_RGB
;
1987 header
->biClrUsed
= palette_colors
;
1989 /* TODO: fill in palette. */
1992 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
1993 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
1994 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
1995 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
1996 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
1997 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
1998 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
1999 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2002 hdc
= get_frame_dc (f
);
2004 /* Create a DIBSection and raster array for the bitmap,
2005 and store its handle in *pixmap. */
2006 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2007 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2008 /* casting avoids a GCC warning */
2009 (void **)&((*ximg
)->data
), NULL
, 0);
2011 /* Realize display palette and garbage all frames. */
2012 release_frame_dc (f
, hdc
);
2014 if (*pixmap
== NULL
)
2016 DWORD err
= GetLastError ();
2017 Lisp_Object errcode
;
2018 /* All system errors are < 10000, so the following is safe. */
2019 XSETINT (errcode
, err
);
2020 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2021 x_destroy_x_image (*ximg
);
2027 #endif /* HAVE_NTGUI */
2030 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2034 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2043 /* Destroy XImage XIMG. Free XIMG->data. */
2046 x_destroy_x_image (XImagePtr ximg
)
2048 eassert (input_blocked_p ());
2051 #ifdef HAVE_X_WINDOWS
2054 XDestroyImage (ximg
);
2055 #endif /* HAVE_X_WINDOWS */
2057 /* Data will be freed by DestroyObject. */
2060 #endif /* HAVE_NTGUI */
2062 ns_release_object (ximg
);
2063 #endif /* HAVE_NS */
2068 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2069 are width and height of both the image and pixmap. */
2072 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2074 #ifdef HAVE_X_WINDOWS
2077 eassert (input_blocked_p ());
2078 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2079 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2080 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2081 #endif /* HAVE_X_WINDOWS */
2084 #if 0 /* I don't think this is necessary looking at where it is used. */
2085 HDC hdc
= get_frame_dc (f
);
2086 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2087 release_frame_dc (f
, hdc
);
2089 #endif /* HAVE_NTGUI */
2092 eassert (ximg
== pixmap
);
2093 ns_retain_object (ximg
);
2097 /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches
2098 with image_put_x_image. */
2101 image_create_x_image_and_pixmap (struct frame
*f
, struct image
*img
,
2102 int width
, int height
, int depth
,
2103 XImagePtr
*ximg
, bool mask_p
)
2105 eassert ((!mask_p
? img
->pixmap
: img
->mask
) == NO_PIXMAP
);
2107 return x_create_x_image_and_pixmap (f
, width
, height
, depth
, ximg
,
2108 !mask_p
? &img
->pixmap
: &img
->mask
);
2111 /* Put X image XIMG into image IMG on frame F, as a mask if and only
2112 if MASK_P. On X, this simply records XIMG on a member of IMG, so
2113 it can be put into the pixmap afterwards via image_sync_to_pixmaps.
2114 On the other platforms, it puts XIMG into the pixmap, then frees
2115 the X image and its buffer. */
2118 image_put_x_image (struct frame
*f
, struct image
*img
, XImagePtr ximg
,
2121 #ifdef HAVE_X_WINDOWS
2124 eassert (img
->ximg
== NULL
);
2129 eassert (img
->mask_img
== NULL
);
2130 img
->mask_img
= ximg
;
2133 x_put_x_image (f
, ximg
, !mask_p
? img
->pixmap
: img
->mask
,
2134 img
->width
, img
->height
);
2135 x_destroy_x_image (ximg
);
2139 #ifdef HAVE_X_WINDOWS
2140 /* Put the X images recorded in IMG on frame F into pixmaps, then free
2141 the X images and their buffers. */
2144 image_sync_to_pixmaps (struct frame
*f
, struct image
*img
)
2148 x_put_x_image (f
, img
->ximg
, img
->pixmap
, img
->width
, img
->height
);
2149 x_destroy_x_image (img
->ximg
);
2154 x_put_x_image (f
, img
->mask_img
, img
->mask
, img
->width
, img
->height
);
2155 x_destroy_x_image (img
->mask_img
);
2156 img
->mask_img
= NULL
;
2162 /* Create a memory device context for IMG on frame F. It stores the
2163 currently selected GDI object into *PREV for future restoration by
2164 image_unget_x_image_or_dc. */
2166 static XImagePtr_or_DC
2167 image_get_x_image_or_dc (struct frame
*f
, struct image
*img
, bool mask_p
,
2170 HDC frame_dc
= get_frame_dc (f
);
2171 XImagePtr_or_DC ximg
= CreateCompatibleDC (frame_dc
);
2173 release_frame_dc (f
, frame_dc
);
2174 *prev
= SelectObject (ximg
, !mask_p
? img
->pixmap
: img
->mask
);
2180 image_unget_x_image_or_dc (struct image
*img
, bool mask_p
,
2181 XImagePtr_or_DC ximg
, HGDIOBJ prev
)
2183 SelectObject (ximg
, prev
);
2186 #else /* !HAVE_NTGUI */
2187 /* Get the X image for IMG on frame F. The resulting X image data
2188 should be treated as read-only at least on X. */
2191 image_get_x_image (struct frame
*f
, struct image
*img
, bool mask_p
)
2193 #ifdef HAVE_X_WINDOWS
2194 XImagePtr ximg_in_img
= !mask_p
? img
->ximg
: img
->mask_img
;
2199 return XGetImage (FRAME_X_DISPLAY (f
), !mask_p
? img
->pixmap
: img
->mask
,
2200 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
2201 #elif defined (HAVE_NS)
2202 XImagePtr pixmap
= !mask_p
? img
->pixmap
: img
->mask
;
2204 ns_retain_object (pixmap
);
2210 image_unget_x_image (struct image
*img
, bool mask_p
, XImagePtr ximg
)
2212 #ifdef HAVE_X_WINDOWS
2213 XImagePtr ximg_in_img
= !mask_p
? img
->ximg
: img
->mask_img
;
2216 eassert (ximg
== ximg_in_img
);
2218 XDestroyImage (ximg
);
2219 #elif defined (HAVE_NS)
2220 ns_release_object (ximg
);
2223 #endif /* !HAVE_NTGUI */
2226 /***********************************************************************
2228 ***********************************************************************/
2230 /* Find image file FILE. Look in data-directory/images, then
2231 x-bitmap-file-path. Value is the encoded full name of the file
2232 found, or nil if not found. */
2235 x_find_image_file (Lisp_Object file
)
2237 Lisp_Object file_found
, search_path
;
2240 /* TODO I think this should use something like image-load-path
2241 instead. Unfortunately, that can contain non-string elements. */
2242 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2244 Vx_bitmap_file_path
);
2246 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2247 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
, false);
2253 file_found
= ENCODE_FILE (file_found
);
2262 /* Read FILE into memory. Value is a pointer to a buffer allocated
2263 with xmalloc holding FILE's contents. Value is null if an error
2264 occurred. *SIZE is set to the size of the file. */
2266 static unsigned char *
2267 slurp_file (char *file
, ptrdiff_t *size
)
2269 FILE *fp
= emacs_fopen (file
, "rb");
2270 unsigned char *buf
= NULL
;
2275 ptrdiff_t count
= SPECPDL_INDEX ();
2276 record_unwind_protect_ptr (fclose_unwind
, fp
);
2278 if (fstat (fileno (fp
), &st
) == 0
2279 && 0 <= st
.st_size
&& st
.st_size
< min (PTRDIFF_MAX
, SIZE_MAX
))
2281 /* Report an error if we read past the purported EOF.
2282 This can happen if the file grows as we read it. */
2283 ptrdiff_t buflen
= st
.st_size
;
2284 buf
= xmalloc (buflen
+ 1);
2285 if (fread (buf
, 1, buflen
+ 1, fp
) == buflen
)
2294 unbind_to (count
, Qnil
);
2302 /***********************************************************************
2304 ***********************************************************************/
2306 static bool xbm_load (struct frame
*f
, struct image
*img
);
2307 static bool xbm_image_p (Lisp_Object object
);
2308 static bool xbm_file_p (Lisp_Object
);
2311 /* Indices of image specification fields in xbm_format, below. */
2313 enum xbm_keyword_index
2331 /* Vector of image_keyword structures describing the format
2332 of valid XBM image specifications. */
2334 static const struct image_keyword xbm_format
[XBM_LAST
] =
2336 {":type", IMAGE_SYMBOL_VALUE
, 1},
2337 {":file", IMAGE_STRING_VALUE
, 0},
2338 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2339 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2340 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2341 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2342 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2343 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2344 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
2345 {":relief", IMAGE_INTEGER_VALUE
, 0},
2346 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2347 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2348 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2351 /* Structure describing the image type XBM. */
2353 static struct image_type xbm_type
=
2363 /* Tokens returned from xbm_scan. */
2372 /* Return true if OBJECT is a valid XBM-type image specification.
2373 A valid specification is a list starting with the symbol `image'
2374 The rest of the list is a property list which must contain an
2377 If the specification specifies a file to load, it must contain
2378 an entry `:file FILENAME' where FILENAME is a string.
2380 If the specification is for a bitmap loaded from memory it must
2381 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2382 WIDTH and HEIGHT are integers > 0. DATA may be:
2384 1. a string large enough to hold the bitmap data, i.e. it must
2385 have a size >= (WIDTH + 7) / 8 * HEIGHT
2387 2. a bool-vector of size >= WIDTH * HEIGHT
2389 3. a vector of strings or bool-vectors, one for each line of the
2392 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2393 may not be specified in this case because they are defined in the
2396 Both the file and data forms may contain the additional entries
2397 `:background COLOR' and `:foreground COLOR'. If not present,
2398 foreground and background of the frame on which the image is
2399 displayed is used. */
2402 xbm_image_p (Lisp_Object object
)
2404 struct image_keyword kw
[XBM_LAST
];
2406 memcpy (kw
, xbm_format
, sizeof kw
);
2407 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2410 eassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2412 if (kw
[XBM_FILE
].count
)
2414 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2417 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2419 /* In-memory XBM file. */
2420 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2428 /* Entries for `:width', `:height' and `:data' must be present. */
2429 if (!kw
[XBM_WIDTH
].count
2430 || !kw
[XBM_HEIGHT
].count
2431 || !kw
[XBM_DATA
].count
)
2434 data
= kw
[XBM_DATA
].value
;
2435 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2436 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2438 /* Check type of data, and width and height against contents of
2444 /* Number of elements of the vector must be >= height. */
2445 if (ASIZE (data
) < height
)
2448 /* Each string or bool-vector in data must be large enough
2449 for one line of the image. */
2450 for (i
= 0; i
< height
; ++i
)
2452 Lisp_Object elt
= AREF (data
, i
);
2457 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2460 else if (BOOL_VECTOR_P (elt
))
2462 if (bool_vector_size (elt
) < width
)
2469 else if (STRINGP (data
))
2472 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2475 else if (BOOL_VECTOR_P (data
))
2477 if (bool_vector_size (data
) / height
< width
)
2488 /* Scan a bitmap file. FP is the stream to read from. Value is
2489 either an enumerator from enum xbm_token, or a character for a
2490 single-character token, or 0 at end of file. If scanning an
2491 identifier, store the lexeme of the identifier in SVAL. If
2492 scanning a number, store its value in *IVAL. */
2495 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2501 /* Skip white space. */
2502 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
2507 else if (c_isdigit (c
))
2509 int value
= 0, digit
;
2511 if (c
== '0' && *s
< end
)
2514 if (c
== 'x' || c
== 'X')
2521 else if (c
>= 'a' && c
<= 'f')
2522 digit
= c
- 'a' + 10;
2523 else if (c
>= 'A' && c
<= 'F')
2524 digit
= c
- 'A' + 10;
2527 value
= 16 * value
+ digit
;
2530 else if (c_isdigit (c
))
2534 && (c
= *(*s
)++, c_isdigit (c
)))
2535 value
= 8 * value
+ c
- '0';
2542 && (c
= *(*s
)++, c_isdigit (c
)))
2543 value
= 10 * value
+ c
- '0';
2551 else if (c_isalpha (c
) || c
== '_')
2555 && (c
= *(*s
)++, (c_isalnum (c
) || c
== '_')))
2562 else if (c
== '/' && **s
== '*')
2564 /* C-style comment. */
2566 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2580 /* Create a Windows bitmap from X bitmap data. */
2582 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2584 static unsigned char swap_nibble
[16]
2585 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2586 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2587 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2588 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2590 unsigned char *bits
, *p
;
2593 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2594 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2595 bits
= alloca (height
* w2
);
2596 memset (bits
, 0, height
* w2
);
2597 for (i
= 0; i
< height
; i
++)
2600 for (j
= 0; j
< w1
; j
++)
2602 /* Bitswap XBM bytes to match how Windows does things. */
2603 unsigned char c
= *data
++;
2604 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2605 | (swap_nibble
[(c
>>4) & 0xf]));
2608 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2614 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2615 COLORREF foreground
, COLORREF background
)
2617 HDC hdc
, old_img_dc
, new_img_dc
;
2618 HGDIOBJ old_prev
, new_prev
;
2621 hdc
= get_frame_dc (f
);
2622 old_img_dc
= CreateCompatibleDC (hdc
);
2623 new_img_dc
= CreateCompatibleDC (hdc
);
2624 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2625 release_frame_dc (f
, hdc
);
2626 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2627 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2628 /* Windows convention for mono bitmaps is black = background,
2629 white = foreground. */
2630 SetTextColor (new_img_dc
, background
);
2631 SetBkColor (new_img_dc
, foreground
);
2633 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2636 SelectObject (old_img_dc
, old_prev
);
2637 SelectObject (new_img_dc
, new_prev
);
2638 DeleteDC (old_img_dc
);
2639 DeleteDC (new_img_dc
);
2640 DeleteObject (img
->pixmap
);
2641 if (new_pixmap
== 0)
2642 fprintf (stderr
, "Failed to convert image to color.\n");
2644 img
->pixmap
= new_pixmap
;
2647 #define XBM_BIT_SHUFFLE(b) (~(b))
2651 #define XBM_BIT_SHUFFLE(b) (b)
2653 #endif /* HAVE_NTGUI */
2657 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2658 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2659 bool non_default_colors
)
2663 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2665 /* If colors were specified, transfer the bitmap to a color one. */
2666 if (non_default_colors
)
2667 convert_mono_to_color_image (f
, img
, fg
, bg
);
2669 #elif defined (HAVE_NS)
2670 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2674 (x_check_image_size (0, img
->width
, img
->height
)
2675 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2678 img
->width
, img
->height
,
2680 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2682 #endif /* !HAVE_NTGUI && !HAVE_NS */
2687 /* Replacement for XReadBitmapFileData which isn't available under old
2688 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2689 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2690 the image. Return in *DATA the bitmap data allocated with xmalloc.
2691 Value is true if successful. DATA null means just test if
2692 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR,
2693 inhibit the call to image_error when the image size is invalid (the
2694 bitmap remains unread). */
2697 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2698 int *width
, int *height
, char **data
,
2699 bool inhibit_image_error
)
2701 unsigned char *s
= contents
;
2702 char buffer
[BUFSIZ
];
2705 int bytes_per_line
, i
, nbytes
;
2711 LA1 = xbm_scan (&s, end, buffer, &value)
2713 #define expect(TOKEN) \
2716 if (LA1 != (TOKEN)) \
2722 #define expect_ident(IDENT) \
2723 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2728 *width
= *height
= -1;
2731 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2733 /* Parse defines for width, height and hot-spots. */
2737 expect_ident ("define");
2738 expect (XBM_TK_IDENT
);
2740 if (LA1
== XBM_TK_NUMBER
)
2742 char *q
= strrchr (buffer
, '_');
2743 q
= q
? q
+ 1 : buffer
;
2744 if (strcmp (q
, "width") == 0)
2746 else if (strcmp (q
, "height") == 0)
2749 expect (XBM_TK_NUMBER
);
2752 if (!check_image_size (f
, *width
, *height
))
2754 if (!inhibit_image_error
)
2755 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2758 else if (data
== NULL
)
2761 /* Parse bits. Must start with `static'. */
2762 expect_ident ("static");
2763 if (LA1
== XBM_TK_IDENT
)
2765 if (strcmp (buffer
, "unsigned") == 0)
2768 expect_ident ("char");
2770 else if (strcmp (buffer
, "short") == 0)
2774 if (*width
% 16 && *width
% 16 < 9)
2777 else if (strcmp (buffer
, "char") == 0)
2785 expect (XBM_TK_IDENT
);
2791 if (! x_check_image_size (0, *width
, *height
))
2793 if (!inhibit_image_error
)
2794 image_error ("Image too large (%dx%d)",
2795 make_number (*width
), make_number (*height
));
2798 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2799 nbytes
= bytes_per_line
* *height
;
2800 p
= *data
= xmalloc (nbytes
);
2804 for (i
= 0; i
< nbytes
; i
+= 2)
2807 expect (XBM_TK_NUMBER
);
2809 *p
++ = XBM_BIT_SHUFFLE (val
);
2810 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2811 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2813 if (LA1
== ',' || LA1
== '}')
2821 for (i
= 0; i
< nbytes
; ++i
)
2824 expect (XBM_TK_NUMBER
);
2826 *p
++ = XBM_BIT_SHUFFLE (val
);
2828 if (LA1
== ',' || LA1
== '}')
2853 /* Load XBM image IMG which will be displayed on frame F from buffer
2854 CONTENTS. END is the end of the buffer. Value is true if
2858 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2865 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2869 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2870 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2871 bool non_default_colors
= 0;
2874 eassert (img
->width
> 0 && img
->height
> 0);
2876 /* Get foreground and background colors, maybe allocate colors. */
2877 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2880 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2881 non_default_colors
= 1;
2883 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2886 background
= x_alloc_image_color (f
, img
, value
, background
);
2887 img
->background
= background
;
2888 img
->background_valid
= 1;
2889 non_default_colors
= 1;
2892 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2893 foreground
, background
,
2894 non_default_colors
);
2897 if (img
->pixmap
== NO_PIXMAP
)
2899 x_clear_image (f
, img
);
2900 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2906 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2912 /* Value is true if DATA looks like an in-memory XBM file. */
2915 xbm_file_p (Lisp_Object data
)
2918 return (STRINGP (data
)
2919 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2920 (SDATA (data
) + SBYTES (data
)),
2925 /* Fill image IMG which is used on frame F with pixmap data. Value is
2926 true if successful. */
2929 xbm_load (struct frame
*f
, struct image
*img
)
2932 Lisp_Object file_name
;
2934 eassert (xbm_image_p (img
->spec
));
2936 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2937 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2938 if (STRINGP (file_name
))
2941 unsigned char *contents
;
2944 file
= x_find_image_file (file_name
);
2945 if (!STRINGP (file
))
2947 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2951 contents
= slurp_file (SSDATA (file
), &size
);
2952 if (contents
== NULL
)
2954 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2958 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2963 struct image_keyword fmt
[XBM_LAST
];
2965 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2966 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2967 bool non_default_colors
= 0;
2970 bool in_memory_file_p
= 0;
2972 /* See if data looks like an in-memory XBM file. */
2973 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2974 in_memory_file_p
= xbm_file_p (data
);
2976 /* Parse the image specification. */
2977 memcpy (fmt
, xbm_format
, sizeof fmt
);
2978 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2981 /* Get specified width, and height. */
2982 if (!in_memory_file_p
)
2984 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2985 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2986 eassert (img
->width
> 0 && img
->height
> 0);
2987 if (!check_image_size (f
, img
->width
, img
->height
))
2989 image_error ("Invalid image size (see `max-image-size')",
2995 /* Get foreground and background colors, maybe allocate colors. */
2996 if (fmt
[XBM_FOREGROUND
].count
2997 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2999 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
3001 non_default_colors
= 1;
3004 if (fmt
[XBM_BACKGROUND
].count
3005 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
3007 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
3009 non_default_colors
= 1;
3012 if (in_memory_file_p
)
3013 success_p
= xbm_load_image (f
, img
, SDATA (data
),
3022 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
3024 p
= bits
= alloca (nbytes
* img
->height
);
3025 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
3027 Lisp_Object line
= AREF (data
, i
);
3029 memcpy (p
, SDATA (line
), nbytes
);
3031 memcpy (p
, bool_vector_data (line
), nbytes
);
3034 else if (STRINGP (data
))
3035 bits
= SSDATA (data
);
3037 bits
= (char *) bool_vector_data (data
);
3043 /* Windows mono bitmaps are reversed compared with X. */
3044 invertedBits
= bits
;
3045 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
3047 bits
= alloca (nbytes
);
3048 for (i
= 0; i
< nbytes
; i
++)
3049 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
3052 /* Create the pixmap. */
3054 if (x_check_image_size (0, img
->width
, img
->height
))
3055 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
3056 foreground
, background
,
3057 non_default_colors
);
3059 img
->pixmap
= NO_PIXMAP
;
3065 image_error ("Unable to create pixmap for XBM image `%s'",
3067 x_clear_image (f
, img
);
3077 /***********************************************************************
3079 ***********************************************************************/
3081 #if defined (HAVE_XPM) || defined (HAVE_NS)
3083 static bool xpm_image_p (Lisp_Object object
);
3084 static bool xpm_load (struct frame
*f
, struct image
*img
);
3086 #endif /* HAVE_XPM || HAVE_NS */
3090 /* Indicate to xpm.h that we don't have Xlib. */
3092 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3093 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3094 #define XColor xpm_XColor
3095 #define XImage xpm_XImage
3096 #define Display xpm_Display
3097 #define PIXEL_ALREADY_TYPEDEFED
3098 #include "X11/xpm.h"
3103 #undef PIXEL_ALREADY_TYPEDEFED
3105 #include "X11/xpm.h"
3106 #endif /* HAVE_NTGUI */
3107 #endif /* HAVE_XPM */
3109 #if defined (HAVE_XPM) || defined (HAVE_NS)
3110 /* The symbol `xpm' identifying XPM-format images. */
3112 static Lisp_Object Qxpm
;
3114 /* Indices of image specification fields in xpm_format, below. */
3116 enum xpm_keyword_index
3132 /* Vector of image_keyword structures describing the format
3133 of valid XPM image specifications. */
3135 static const struct image_keyword xpm_format
[XPM_LAST
] =
3137 {":type", IMAGE_SYMBOL_VALUE
, 1},
3138 {":file", IMAGE_STRING_VALUE
, 0},
3139 {":data", IMAGE_STRING_VALUE
, 0},
3140 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3141 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
3142 {":relief", IMAGE_INTEGER_VALUE
, 0},
3143 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3144 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3145 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3146 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3147 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3150 #if defined HAVE_NTGUI && defined WINDOWSNT
3151 static bool init_xpm_functions (void);
3153 #define init_xpm_functions NULL
3156 /* Structure describing the image type XPM. */
3158 static struct image_type xpm_type
=
3168 #ifdef HAVE_X_WINDOWS
3170 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3171 functions for allocating image colors. Our own functions handle
3172 color allocation failures more gracefully than the ones on the XPM
3175 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3176 #define ALLOC_XPM_COLORS
3178 #endif /* HAVE_X_WINDOWS */
3180 #ifdef ALLOC_XPM_COLORS
3182 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3185 /* An entry in a hash table used to cache color definitions of named
3186 colors. This cache is necessary to speed up XPM image loading in
3187 case we do color allocations ourselves. Without it, we would need
3188 a call to XParseColor per pixel in the image. */
3190 struct xpm_cached_color
3192 /* Next in collision chain. */
3193 struct xpm_cached_color
*next
;
3195 /* Color definition (RGB and pixel color). */
3199 char name
[FLEXIBLE_ARRAY_MEMBER
];
3202 /* The hash table used for the color cache, and its bucket vector
3205 #define XPM_COLOR_CACHE_BUCKETS 1001
3206 static struct xpm_cached_color
**xpm_color_cache
;
3208 /* Initialize the color cache. */
3211 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3213 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3214 xpm_color_cache
= xzalloc (nbytes
);
3215 init_color_table ();
3217 if (attrs
->valuemask
& XpmColorSymbols
)
3222 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3223 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3224 attrs
->colorsymbols
[i
].value
, &color
))
3226 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3228 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3233 /* Free the color cache. */
3236 xpm_free_color_cache (void)
3238 struct xpm_cached_color
*p
, *next
;
3241 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3242 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3248 xfree (xpm_color_cache
);
3249 xpm_color_cache
= NULL
;
3250 free_color_table ();
3253 /* Return the bucket index for color named COLOR_NAME in the color
3257 xpm_color_bucket (char *color_name
)
3259 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3260 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3264 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3265 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3268 static struct xpm_cached_color
*
3269 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3272 struct xpm_cached_color
*p
;
3275 bucket
= xpm_color_bucket (color_name
);
3277 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3278 p
= xmalloc (nbytes
);
3279 strcpy (p
->name
, color_name
);
3281 p
->next
= xpm_color_cache
[bucket
];
3282 xpm_color_cache
[bucket
] = p
;
3286 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3287 return the cached definition in *COLOR. Otherwise, make a new
3288 entry in the cache and allocate the color. Value is false if color
3289 allocation failed. */
3292 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3294 struct xpm_cached_color
*p
;
3295 int h
= xpm_color_bucket (color_name
);
3297 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3298 if (strcmp (p
->name
, color_name
) == 0)
3303 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3306 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3308 p
= xpm_cache_color (f
, color_name
, color
, h
);
3310 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3311 with transparency, and it's useful. */
3312 else if (strcmp ("opaque", color_name
) == 0)
3314 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3315 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3316 p
= xpm_cache_color (f
, color_name
, color
, h
);
3323 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3324 CLOSURE is a pointer to the frame on which we allocate the
3325 color. Return in *COLOR the allocated color. Value is non-zero
3329 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3332 return xpm_lookup_color (closure
, color_name
, color
);
3336 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3337 is a pointer to the frame on which we allocate the color. Value is
3338 non-zero if successful. */
3341 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3346 #endif /* ALLOC_XPM_COLORS */
3351 /* XPM library details. */
3353 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3354 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3355 xpm_XImage
**, XpmAttributes
*));
3356 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3357 xpm_XImage
**, XpmAttributes
*));
3358 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3361 init_xpm_functions (void)
3365 if (!(library
= w32_delayed_load (Qxpm
)))
3368 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3369 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3370 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3371 LOAD_IMGLIB_FN (library
, XImageFree
);
3375 #endif /* WINDOWSNT */
3377 #if defined HAVE_NTGUI && !defined WINDOWSNT
3378 /* Glue for code below */
3379 #define fn_XpmReadFileToImage XpmReadFileToImage
3380 #define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer
3381 #define fn_XImageFree XImageFree
3382 #define fn_XpmFreeAttributes XpmFreeAttributes
3383 #endif /* HAVE_NTGUI && !WINDOWSNT */
3385 /* Value is true if COLOR_SYMBOLS is a valid color symbols list
3386 for XPM images. Such a list must consist of conses whose car and
3390 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3392 while (CONSP (color_symbols
))
3394 Lisp_Object sym
= XCAR (color_symbols
);
3396 || !STRINGP (XCAR (sym
))
3397 || !STRINGP (XCDR (sym
)))
3399 color_symbols
= XCDR (color_symbols
);
3402 return NILP (color_symbols
);
3406 /* Value is true if OBJECT is a valid XPM image specification. */
3409 xpm_image_p (Lisp_Object object
)
3411 struct image_keyword fmt
[XPM_LAST
];
3412 memcpy (fmt
, xpm_format
, sizeof fmt
);
3413 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3414 /* Either `:file' or `:data' must be present. */
3415 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3416 /* Either no `:color-symbols' or it's a list of conses
3417 whose car and cdr are strings. */
3418 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3419 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3422 #endif /* HAVE_XPM || HAVE_NS */
3424 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3426 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3428 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
3431 XpmAttributes attrs
;
3432 Pixmap bitmap
, mask
;
3434 memset (&attrs
, 0, sizeof attrs
);
3436 attrs
.visual
= FRAME_X_VISUAL (f
);
3437 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3438 attrs
.valuemask
|= XpmVisual
;
3439 attrs
.valuemask
|= XpmColormap
;
3441 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3442 (char **) bits
, &bitmap
, &mask
, &attrs
);
3443 if (rc
!= XpmSuccess
)
3445 XpmFreeAttributes (&attrs
);
3449 id
= x_allocate_bitmap_record (f
);
3450 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3451 dpyinfo
->bitmaps
[id
- 1].have_mask
= true;
3452 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3453 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3454 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3455 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3456 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3457 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3459 XpmFreeAttributes (&attrs
);
3462 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3464 /* Load image IMG which will be displayed on frame F. Value is
3465 true if successful. */
3470 xpm_load (struct frame
*f
, struct image
*img
)
3473 XpmAttributes attrs
;
3474 Lisp_Object specified_file
, color_symbols
;
3477 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3478 #endif /* HAVE_NTGUI */
3480 /* Configure the XPM lib. Use the visual of frame F. Allocate
3481 close colors. Return colors allocated. */
3482 memset (&attrs
, 0, sizeof attrs
);
3485 attrs
.visual
= FRAME_X_VISUAL (f
);
3486 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3487 attrs
.valuemask
|= XpmVisual
;
3488 attrs
.valuemask
|= XpmColormap
;
3489 #endif /* HAVE_NTGUI */
3491 #ifdef ALLOC_XPM_COLORS
3492 /* Allocate colors with our own functions which handle
3493 failing color allocation more gracefully. */
3494 attrs
.color_closure
= f
;
3495 attrs
.alloc_color
= xpm_alloc_color
;
3496 attrs
.free_colors
= xpm_free_colors
;
3497 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3498 #else /* not ALLOC_XPM_COLORS */
3499 /* Let the XPM lib allocate colors. */
3500 attrs
.valuemask
|= XpmReturnAllocPixels
;
3501 #ifdef XpmAllocCloseColors
3502 attrs
.alloc_close_colors
= 1;
3503 attrs
.valuemask
|= XpmAllocCloseColors
;
3504 #else /* not XpmAllocCloseColors */
3505 attrs
.closeness
= 600;
3506 attrs
.valuemask
|= XpmCloseness
;
3507 #endif /* not XpmAllocCloseColors */
3508 #endif /* ALLOC_XPM_COLORS */
3510 /* If image specification contains symbolic color definitions, add
3511 these to `attrs'. */
3512 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3513 if (CONSP (color_symbols
))
3516 XpmColorSymbol
*xpm_syms
;
3519 attrs
.valuemask
|= XpmColorSymbols
;
3521 /* Count number of symbols. */
3522 attrs
.numsymbols
= 0;
3523 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3526 /* Allocate an XpmColorSymbol array. */
3527 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3528 xpm_syms
= alloca (size
);
3529 memset (xpm_syms
, 0, size
);
3530 attrs
.colorsymbols
= xpm_syms
;
3532 /* Fill the color symbol array. */
3533 for (tail
= color_symbols
, i
= 0;
3535 ++i
, tail
= XCDR (tail
))
3539 char *empty_string
= (char *) "";
3541 if (!CONSP (XCAR (tail
)))
3543 xpm_syms
[i
].name
= empty_string
;
3544 xpm_syms
[i
].value
= empty_string
;
3547 name
= XCAR (XCAR (tail
));
3548 color
= XCDR (XCAR (tail
));
3551 xpm_syms
[i
].name
= alloca (SCHARS (name
) + 1);
3552 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3555 xpm_syms
[i
].name
= empty_string
;
3556 if (STRINGP (color
))
3558 xpm_syms
[i
].value
= alloca (SCHARS (color
) + 1);
3559 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3562 xpm_syms
[i
].value
= empty_string
;
3566 /* Create a pixmap for the image, either from a file, or from a
3567 string buffer containing data in the same format as an XPM file. */
3568 #ifdef ALLOC_XPM_COLORS
3569 xpm_init_color_cache (f
, &attrs
);
3572 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3576 HDC frame_dc
= get_frame_dc (f
);
3577 hdc
= CreateCompatibleDC (frame_dc
);
3578 release_frame_dc (f
, frame_dc
);
3580 #endif /* HAVE_NTGUI */
3582 if (STRINGP (specified_file
))
3584 Lisp_Object file
= x_find_image_file (specified_file
);
3585 if (!STRINGP (file
))
3587 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3588 #ifdef ALLOC_XPM_COLORS
3589 xpm_free_color_cache ();
3596 /* FILE is encoded in UTF-8, but image libraries on Windows
3597 support neither UTF-8 nor UTF-16 encoded file names. So we
3598 need to re-encode it in ANSI. */
3599 file
= ansi_encode_filename (file
);
3601 /* XpmReadFileToPixmap is not available in the Windows port of
3602 libxpm. But XpmReadFileToImage almost does what we want. */
3603 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3604 &xpm_image
, &xpm_mask
,
3607 rc
= XpmReadFileToImage (FRAME_X_DISPLAY (f
), SSDATA (file
),
3608 &img
->ximg
, &img
->mask_img
,
3610 #endif /* HAVE_NTGUI */
3614 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3615 if (!STRINGP (buffer
))
3617 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3618 #ifdef ALLOC_XPM_COLORS
3619 xpm_free_color_cache ();
3624 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3625 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3626 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3627 &xpm_image
, &xpm_mask
,
3630 rc
= XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f
), SSDATA (buffer
),
3631 &img
->ximg
, &img
->mask_img
,
3633 #endif /* HAVE_NTGUI */
3636 #ifdef HAVE_X_WINDOWS
3637 if (rc
== XpmSuccess
)
3639 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3640 img
->ximg
->width
, img
->ximg
->height
,
3642 if (img
->pixmap
== NO_PIXMAP
)
3644 x_clear_image (f
, img
);
3647 else if (img
->mask_img
)
3649 img
->mask
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3650 img
->mask_img
->width
,
3651 img
->mask_img
->height
,
3652 img
->mask_img
->depth
);
3653 if (img
->mask
== NO_PIXMAP
)
3655 x_clear_image (f
, img
);
3662 if (rc
== XpmSuccess
)
3664 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3665 img
->colors
= colors_in_color_table (&img
->ncolors
);
3666 #else /* not ALLOC_XPM_COLORS */
3670 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3671 plus some duplicate attributes. */
3672 if (xpm_image
&& xpm_image
->bitmap
)
3674 img
->pixmap
= xpm_image
->bitmap
;
3675 /* XImageFree in libXpm frees XImage struct without destroying
3676 the bitmap, which is what we want. */
3677 fn_XImageFree (xpm_image
);
3679 if (xpm_mask
&& xpm_mask
->bitmap
)
3681 /* The mask appears to be inverted compared with what we expect.
3682 TODO: invert our expectations. See other places where we
3683 have to invert bits because our idea of masks is backwards. */
3685 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3687 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3688 SelectObject (hdc
, old_obj
);
3690 img
->mask
= xpm_mask
->bitmap
;
3691 fn_XImageFree (xpm_mask
);
3696 #endif /* HAVE_NTGUI */
3698 /* Remember allocated colors. */
3699 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3700 img
->ncolors
= attrs
.nalloc_pixels
;
3701 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3703 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3704 #ifdef DEBUG_X_COLORS
3705 register_color (img
->colors
[i
]);
3708 #endif /* not ALLOC_XPM_COLORS */
3710 img
->width
= attrs
.width
;
3711 img
->height
= attrs
.height
;
3712 eassert (img
->width
> 0 && img
->height
> 0);
3714 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3716 fn_XpmFreeAttributes (&attrs
);
3718 XpmFreeAttributes (&attrs
);
3719 #endif /* HAVE_NTGUI */
3721 #ifdef HAVE_X_WINDOWS
3722 /* Maybe fill in the background field while we have ximg handy. */
3723 IMAGE_BACKGROUND (img
, f
, img
->ximg
);
3725 /* Fill in the background_transparent field while we have the
3727 image_background_transparent (img
, f
, img
->mask_img
);
3734 #endif /* HAVE_NTGUI */
3739 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3742 case XpmFileInvalid
:
3743 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3747 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3750 case XpmColorFailed
:
3751 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3755 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3760 #ifdef ALLOC_XPM_COLORS
3761 xpm_free_color_cache ();
3763 return rc
== XpmSuccess
;
3766 #endif /* HAVE_XPM */
3768 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3770 /* XPM support functions for NS where libxpm is not available.
3771 Only XPM version 3 (without any extensions) is supported. */
3773 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3775 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3776 const unsigned char *, int);
3777 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3779 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3780 const unsigned char *, int);
3782 /* Tokens returned from xpm_scan. */
3791 /* Scan an XPM data and return a character (< 256) or a token defined
3792 by enum xpm_token above. *S and END are the start (inclusive) and
3793 the end (exclusive) addresses of the data, respectively. Advance
3794 *S while scanning. If token is either XPM_TK_IDENT or
3795 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3796 length of the corresponding token, respectively. */
3799 xpm_scan (const unsigned char **s
,
3800 const unsigned char *end
,
3801 const unsigned char **beg
,
3808 /* Skip white-space. */
3809 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
3812 /* gnus-pointer.xpm uses '-' in its identifier.
3813 sb-dir-plus.xpm uses '+' in its identifier. */
3814 if (c_isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3818 && (c
= **s
, c_isalnum (c
)
3819 || c
== '_' || c
== '-' || c
== '+'))
3822 return XPM_TK_IDENT
;
3827 while (*s
< end
&& **s
!= '"')
3832 return XPM_TK_STRING
;
3836 if (*s
< end
&& **s
== '*')
3838 /* C-style comment. */
3842 while (*s
< end
&& *(*s
)++ != '*')
3845 while (*s
< end
&& **s
!= '/');
3859 /* Functions for color table lookup in XPM data. A key is a string
3860 specifying the color of each pixel in XPM data. A value is either
3861 an integer that specifies a pixel color, Qt that specifies
3862 transparency, or Qnil for the unspecified color. If the length of
3863 the key string is one, a vector is used as a table. Otherwise, a
3864 hash table is used. */
3867 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3868 const unsigned char *,
3871 Lisp_Object (**get_func
) (Lisp_Object
,
3872 const unsigned char *,
3875 *put_func
= xpm_put_color_table_v
;
3876 *get_func
= xpm_get_color_table_v
;
3877 return Fmake_vector (make_number (256), Qnil
);
3881 xpm_put_color_table_v (Lisp_Object color_table
,
3882 const unsigned char *chars_start
,
3886 ASET (color_table
, *chars_start
, color
);
3890 xpm_get_color_table_v (Lisp_Object color_table
,
3891 const unsigned char *chars_start
,
3894 return AREF (color_table
, *chars_start
);
3898 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3899 const unsigned char *,
3902 Lisp_Object (**get_func
) (Lisp_Object
,
3903 const unsigned char *,
3906 *put_func
= xpm_put_color_table_h
;
3907 *get_func
= xpm_get_color_table_h
;
3908 return make_hash_table (hashtest_equal
, make_number (DEFAULT_HASH_SIZE
),
3909 make_float (DEFAULT_REHASH_SIZE
),
3910 make_float (DEFAULT_REHASH_THRESHOLD
),
3915 xpm_put_color_table_h (Lisp_Object color_table
,
3916 const unsigned char *chars_start
,
3920 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3921 EMACS_UINT hash_code
;
3922 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3924 hash_lookup (table
, chars
, &hash_code
);
3925 hash_put (table
, chars
, color
, hash_code
);
3929 xpm_get_color_table_h (Lisp_Object color_table
,
3930 const unsigned char *chars_start
,
3933 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3935 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3937 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3940 enum xpm_color_key
{
3948 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3951 xpm_str_to_color_key (const char *s
)
3956 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3958 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3964 xpm_load_image (struct frame
*f
,
3966 const unsigned char *contents
,
3967 const unsigned char *end
)
3969 const unsigned char *s
= contents
, *beg
, *str
;
3970 unsigned char buffer
[BUFSIZ
];
3971 int width
, height
, x
, y
;
3972 int num_colors
, chars_per_pixel
;
3975 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3976 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3977 Lisp_Object frame
, color_symbols
, color_table
;
3979 bool have_mask
= false;
3980 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3983 LA1 = xpm_scan (&s, end, &beg, &len)
3985 #define expect(TOKEN) \
3988 if (LA1 != (TOKEN)) \
3994 #define expect_ident(IDENT) \
3995 if (LA1 == XPM_TK_IDENT \
3996 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
4001 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
4005 expect_ident ("static");
4006 expect_ident ("char");
4008 expect (XPM_TK_IDENT
);
4013 expect (XPM_TK_STRING
);
4016 memcpy (buffer
, beg
, len
);
4018 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
4019 &num_colors
, &chars_per_pixel
) != 4
4020 || width
<= 0 || height
<= 0
4021 || num_colors
<= 0 || chars_per_pixel
<= 0)
4024 if (!check_image_size (f
, width
, height
))
4026 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
4030 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0)
4032 || !image_create_x_image_and_pixmap (f
, img
, width
, height
, 1,
4037 image_error ("Image too large", Qnil
, Qnil
);
4043 XSETFRAME (frame
, f
);
4044 if (!NILP (Fxw_display_color_p (frame
)))
4045 best_key
= XPM_COLOR_KEY_C
;
4046 else if (!NILP (Fx_display_grayscale_p (frame
)))
4047 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
4048 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
4050 best_key
= XPM_COLOR_KEY_M
;
4052 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
4053 if (chars_per_pixel
== 1)
4054 color_table
= xpm_make_color_table_v (&put_color_table
,
4057 color_table
= xpm_make_color_table_h (&put_color_table
,
4060 while (num_colors
-- > 0)
4062 char *color
, *max_color
;
4063 int key
, next_key
, max_key
= 0;
4064 Lisp_Object symbol_color
= Qnil
, color_val
;
4067 expect (XPM_TK_STRING
);
4068 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
4070 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
4071 buffer
[len
- chars_per_pixel
] = '\0';
4073 str
= strtok (buffer
, " \t");
4076 key
= xpm_str_to_color_key (str
);
4081 color
= strtok (NULL
, " \t");
4085 while ((str
= strtok (NULL
, " \t")) != NULL
)
4087 next_key
= xpm_str_to_color_key (str
);
4090 color
[strlen (color
)] = ' ';
4093 if (key
== XPM_COLOR_KEY_S
)
4095 if (NILP (symbol_color
))
4096 symbol_color
= build_string (color
);
4098 else if (max_key
< key
&& key
<= best_key
)
4108 if (!NILP (color_symbols
) && !NILP (symbol_color
))
4110 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
4112 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
4114 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
4116 else if (x_defined_color (f
, SSDATA (XCDR (specified_color
)),
4118 color_val
= make_number (cdef
.pixel
);
4121 if (NILP (color_val
) && max_key
> 0)
4123 if (xstrcasecmp (max_color
, "None") == 0)
4125 else if (x_defined_color (f
, max_color
, &cdef
, 0))
4126 color_val
= make_number (cdef
.pixel
);
4128 if (!NILP (color_val
))
4129 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
4134 for (y
= 0; y
< height
; y
++)
4136 expect (XPM_TK_STRING
);
4138 if (len
< width
* chars_per_pixel
)
4140 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
4142 Lisp_Object color_val
=
4143 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4145 XPutPixel (ximg
, x
, y
,
4146 (INTEGERP (color_val
) ? XINT (color_val
)
4147 : FRAME_FOREGROUND_PIXEL (f
)));
4149 XPutPixel (mask_img
, x
, y
,
4150 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4151 : (have_mask
= true, PIX_MASK_RETAIN
)));
4153 if (EQ (color_val
, Qt
))
4154 ns_set_alpha (ximg
, x
, y
, 0);
4162 img
->height
= height
;
4164 /* Maybe fill in the background field while we have ximg handy. */
4165 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4166 IMAGE_BACKGROUND (img
, f
, ximg
);
4168 image_put_x_image (f
, img
, ximg
, 0);
4172 /* Fill in the background_transparent field while we have the
4174 image_background_transparent (img
, f
, mask_img
);
4176 image_put_x_image (f
, img
, mask_img
, 1);
4180 x_destroy_x_image (mask_img
);
4181 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
4187 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4188 x_destroy_x_image (ximg
);
4189 x_destroy_x_image (mask_img
);
4190 x_clear_image (f
, img
);
4199 xpm_load (struct frame
*f
,
4203 Lisp_Object file_name
;
4205 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4206 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4207 if (STRINGP (file_name
))
4210 unsigned char *contents
;
4213 file
= x_find_image_file (file_name
);
4214 if (!STRINGP (file
))
4216 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4220 contents
= slurp_file (SSDATA (file
), &size
);
4221 if (contents
== NULL
)
4223 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4227 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4234 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4235 if (!STRINGP (data
))
4237 image_error ("Invalid image data `%s'", data
, Qnil
);
4240 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4241 SDATA (data
) + SBYTES (data
));
4247 #endif /* HAVE_NS && !HAVE_XPM */
4251 /***********************************************************************
4253 ***********************************************************************/
4255 #ifdef COLOR_TABLE_SUPPORT
4257 /* An entry in the color table mapping an RGB color to a pixel color. */
4262 unsigned long pixel
;
4264 /* Next in color table collision list. */
4265 struct ct_color
*next
;
4268 /* The bucket vector size to use. Must be prime. */
4272 /* Value is a hash of the RGB color given by R, G, and B. */
4274 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4276 /* The color hash table. */
4278 static struct ct_color
**ct_table
;
4280 /* Number of entries in the color table. */
4282 static int ct_colors_allocated
;
4285 ct_colors_allocated_max
=
4287 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4290 /* Initialize the color table. */
4293 init_color_table (void)
4295 int size
= CT_SIZE
* sizeof (*ct_table
);
4296 ct_table
= xzalloc (size
);
4297 ct_colors_allocated
= 0;
4301 /* Free memory associated with the color table. */
4304 free_color_table (void)
4307 struct ct_color
*p
, *next
;
4309 for (i
= 0; i
< CT_SIZE
; ++i
)
4310 for (p
= ct_table
[i
]; p
; p
= next
)
4321 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4322 entry for that color already is in the color table, return the
4323 pixel color of that entry. Otherwise, allocate a new color for R,
4324 G, B, and make an entry in the color table. */
4326 static unsigned long
4327 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4329 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4330 int i
= hash
% CT_SIZE
;
4332 Display_Info
*dpyinfo
;
4334 /* Handle TrueColor visuals specially, which improves performance by
4335 two orders of magnitude. Freeing colors on TrueColor visuals is
4336 a nop, and pixel colors specify RGB values directly. See also
4337 the Xlib spec, chapter 3.1. */
4338 dpyinfo
= FRAME_DISPLAY_INFO (f
);
4339 if (dpyinfo
->red_bits
> 0)
4341 unsigned long pr
, pg
, pb
;
4343 /* Apply gamma-correction like normal color allocation does. */
4347 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4348 gamma_correct (f
, &color
);
4349 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4352 /* Scale down RGB values to the visual's bits per RGB, and shift
4353 them to the right position in the pixel color. Note that the
4354 original RGB values are 16-bit values, as usual in X. */
4355 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4356 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4357 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4359 /* Assemble the pixel color. */
4360 return pr
| pg
| pb
;
4363 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4364 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4370 #ifdef HAVE_X_WINDOWS
4378 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4379 return FRAME_FOREGROUND_PIXEL (f
);
4381 #ifdef HAVE_X_WINDOWS
4386 cmap
= FRAME_X_COLORMAP (f
);
4387 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4390 ++ct_colors_allocated
;
4391 p
= xmalloc (sizeof *p
);
4395 p
->pixel
= color
.pixel
;
4396 p
->next
= ct_table
[i
];
4400 return FRAME_FOREGROUND_PIXEL (f
);
4404 color
= PALETTERGB (r
, g
, b
);
4406 color
= RGB_TO_ULONG (r
, g
, b
);
4407 #endif /* HAVE_NTGUI */
4408 ++ct_colors_allocated
;
4409 p
= xmalloc (sizeof *p
);
4414 p
->next
= ct_table
[i
];
4416 #endif /* HAVE_X_WINDOWS */
4424 /* Look up pixel color PIXEL which is used on frame F in the color
4425 table. If not already present, allocate it. Value is PIXEL. */
4427 static unsigned long
4428 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4430 int i
= pixel
% CT_SIZE
;
4433 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4434 if (p
->pixel
== pixel
)
4443 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4444 return FRAME_FOREGROUND_PIXEL (f
);
4446 #ifdef HAVE_X_WINDOWS
4447 cmap
= FRAME_X_COLORMAP (f
);
4448 color
.pixel
= pixel
;
4449 x_query_color (f
, &color
);
4450 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4453 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4454 color
.pixel
= pixel
;
4455 XQueryColor (NULL
, cmap
, &color
);
4456 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4458 #endif /* HAVE_X_WINDOWS */
4462 ++ct_colors_allocated
;
4464 p
= xmalloc (sizeof *p
);
4469 p
->next
= ct_table
[i
];
4473 return FRAME_FOREGROUND_PIXEL (f
);
4479 /* Value is a vector of all pixel colors contained in the color table,
4480 allocated via xmalloc. Set *N to the number of colors. */
4482 static unsigned long *
4483 colors_in_color_table (int *n
)
4487 unsigned long *colors
;
4489 if (ct_colors_allocated
== 0)
4496 colors
= xmalloc (ct_colors_allocated
* sizeof *colors
);
4497 *n
= ct_colors_allocated
;
4499 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4500 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4501 colors
[j
++] = p
->pixel
;
4507 #else /* COLOR_TABLE_SUPPORT */
4509 static unsigned long
4510 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4512 unsigned long pixel
;
4515 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4516 #endif /* HAVE_NTGUI */
4519 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4520 #endif /* HAVE_NS */
4525 init_color_table (void)
4528 #endif /* COLOR_TABLE_SUPPORT */
4531 /***********************************************************************
4533 ***********************************************************************/
4535 /* Edge detection matrices for different edge-detection
4538 static int emboss_matrix
[9] = {
4540 2, -1, 0, /* y - 1 */
4542 0, 1, -2 /* y + 1 */
4545 static int laplace_matrix
[9] = {
4547 1, 0, 0, /* y - 1 */
4549 0, 0, -1 /* y + 1 */
4552 /* Value is the intensity of the color whose red/green/blue values
4555 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4558 /* On frame F, return an array of XColor structures describing image
4559 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4560 means also fill the red/green/blue members of the XColor
4561 structures. Value is a pointer to the array of XColors structures,
4562 allocated with xmalloc; it must be freed by the caller. */
4565 x_to_xcolors (struct frame
*f
, struct image
*img
, bool rgb_p
)
4569 XImagePtr_or_DC ximg
;
4572 #endif /* HAVE_NTGUI */
4574 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4575 memory_full (SIZE_MAX
);
4576 colors
= xmalloc (sizeof *colors
* img
->width
* img
->height
);
4578 /* Get the X image or create a memory device context for IMG. */
4579 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
4581 /* Fill the `pixel' members of the XColor array. I wished there
4582 were an easy and portable way to circumvent XGetPixel. */
4584 for (y
= 0; y
< img
->height
; ++y
)
4586 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4588 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4589 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4591 x_query_colors (f
, row
, img
->width
);
4595 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4597 /* W32_TODO: palette support needed here? */
4598 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4601 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4602 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4603 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4606 #endif /* HAVE_X_WINDOWS */
4609 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
4616 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4617 created with CreateDIBSection, with the pointer to the bit values
4618 stored in ximg->data. */
4621 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4623 int width
= ximg
->info
.bmiHeader
.biWidth
;
4624 unsigned char * pixel
;
4626 /* True color images. */
4627 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4629 int rowbytes
= width
* 3;
4630 /* Ensure scanlines are aligned on 4 byte boundaries. */
4632 rowbytes
+= 4 - (rowbytes
% 4);
4634 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4635 /* Windows bitmaps are in BGR order. */
4636 *pixel
= GetBValue (color
);
4637 *(pixel
+ 1) = GetGValue (color
);
4638 *(pixel
+ 2) = GetRValue (color
);
4640 /* Monochrome images. */
4641 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4643 int rowbytes
= width
/ 8;
4644 /* Ensure scanlines are aligned on 4 byte boundaries. */
4646 rowbytes
+= 4 - (rowbytes
% 4);
4647 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4648 /* Filter out palette info. */
4649 if (color
& 0x00ffffff)
4650 *pixel
= *pixel
| (1 << x
% 8);
4652 *pixel
= *pixel
& ~(1 << x
% 8);
4655 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4658 #endif /* HAVE_NTGUI */
4660 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4661 RGB members are set. F is the frame on which this all happens.
4662 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4665 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4668 XImagePtr oimg
= NULL
;
4671 init_color_table ();
4673 x_clear_image_1 (f
, img
, CLEAR_IMAGE_PIXMAP
| CLEAR_IMAGE_COLORS
);
4674 image_create_x_image_and_pixmap (f
, img
, img
->width
, img
->height
, 0,
4677 for (y
= 0; y
< img
->height
; ++y
)
4678 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4680 unsigned long pixel
;
4681 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4682 XPutPixel (oimg
, x
, y
, pixel
);
4687 image_put_x_image (f
, img
, oimg
, 0);
4688 #ifdef COLOR_TABLE_SUPPORT
4689 img
->colors
= colors_in_color_table (&img
->ncolors
);
4690 free_color_table ();
4691 #endif /* COLOR_TABLE_SUPPORT */
4695 /* On frame F, perform edge-detection on image IMG.
4697 MATRIX is a nine-element array specifying the transformation
4698 matrix. See emboss_matrix for an example.
4700 COLOR_ADJUST is a color adjustment added to each pixel of the
4704 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4706 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4710 for (i
= sum
= 0; i
< 9; ++i
)
4711 sum
+= eabs (matrix
[i
]);
4713 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4715 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4716 memory_full (SIZE_MAX
);
4717 new = xmalloc (sizeof *new * img
->width
* img
->height
);
4719 for (y
= 0; y
< img
->height
; ++y
)
4721 p
= COLOR (new, 0, y
);
4722 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4723 p
= COLOR (new, img
->width
- 1, y
);
4724 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4727 for (x
= 1; x
< img
->width
- 1; ++x
)
4729 p
= COLOR (new, x
, 0);
4730 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4731 p
= COLOR (new, x
, img
->height
- 1);
4732 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4735 for (y
= 1; y
< img
->height
- 1; ++y
)
4737 p
= COLOR (new, 1, y
);
4739 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4741 int r
, g
, b
, yy
, xx
;
4744 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4745 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4748 XColor
*t
= COLOR (colors
, xx
, yy
);
4749 r
+= matrix
[i
] * t
->red
;
4750 g
+= matrix
[i
] * t
->green
;
4751 b
+= matrix
[i
] * t
->blue
;
4754 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4755 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4756 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4757 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4762 x_from_xcolors (f
, img
, new);
4768 /* Perform the pre-defined `emboss' edge-detection on image IMG
4772 x_emboss (struct frame
*f
, struct image
*img
)
4774 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4778 /* Transform image IMG which is used on frame F with a Laplace
4779 edge-detection algorithm. The result is an image that can be used
4780 to draw disabled buttons, for example. */
4783 x_laplace (struct frame
*f
, struct image
*img
)
4785 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4789 /* Perform edge-detection on image IMG on frame F, with specified
4790 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4792 MATRIX must be either
4794 - a list of at least 9 numbers in row-major form
4795 - a vector of at least 9 numbers
4797 COLOR_ADJUST nil means use a default; otherwise it must be a
4801 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4802 Lisp_Object color_adjust
)
4810 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4811 ++i
, matrix
= XCDR (matrix
))
4812 trans
[i
] = XFLOATINT (XCAR (matrix
));
4814 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4816 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4817 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4820 if (NILP (color_adjust
))
4821 color_adjust
= make_number (0xffff / 2);
4823 if (i
== 9 && NUMBERP (color_adjust
))
4824 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4828 /* Transform image IMG on frame F so that it looks disabled. */
4831 x_disable_image (struct frame
*f
, struct image
*img
)
4833 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
4835 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4837 int n_planes
= dpyinfo
->n_planes
;
4838 #endif /* HAVE_NTGUI */
4842 /* Color (or grayscale). Convert to gray, and equalize. Just
4843 drawing such images with a stipple can look very odd, so
4844 we're using this method instead. */
4845 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4847 const int h
= 15000;
4848 const int l
= 30000;
4850 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4854 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4855 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4856 p
->red
= p
->green
= p
->blue
= i2
;
4859 x_from_xcolors (f
, img
, colors
);
4862 /* Draw a cross over the disabled image, if we must or if we
4864 if (n_planes
< 2 || cross_disabled_images
)
4867 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4869 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4871 Display
*dpy
= FRAME_X_DISPLAY (f
);
4874 image_sync_to_pixmaps (f
, img
);
4875 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4876 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4877 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4878 img
->width
- 1, img
->height
- 1);
4879 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4885 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4886 XSetForeground (dpy
, gc
, MaskForeground (f
));
4887 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4888 img
->width
- 1, img
->height
- 1);
4889 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4893 #endif /* !HAVE_NS */
4898 hdc
= get_frame_dc (f
);
4899 bmpdc
= CreateCompatibleDC (hdc
);
4900 release_frame_dc (f
, hdc
);
4902 prev
= SelectObject (bmpdc
, img
->pixmap
);
4904 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4905 MoveToEx (bmpdc
, 0, 0, NULL
);
4906 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4907 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4908 LineTo (bmpdc
, img
->width
- 1, 0);
4912 SelectObject (bmpdc
, img
->mask
);
4913 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4914 MoveToEx (bmpdc
, 0, 0, NULL
);
4915 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4916 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4917 LineTo (bmpdc
, img
->width
- 1, 0);
4919 SelectObject (bmpdc
, prev
);
4921 #endif /* HAVE_NTGUI */
4926 /* Build a mask for image IMG which is used on frame F. FILE is the
4927 name of an image file, for error messages. HOW determines how to
4928 determine the background color of IMG. If it is a list '(R G B)',
4929 with R, G, and B being integers >= 0, take that as the color of the
4930 background. Otherwise, determine the background color of IMG
4934 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4936 XImagePtr_or_DC ximg
;
4943 #endif /* HAVE_NTGUI */
4945 bool use_img_background
;
4946 unsigned long bg
= 0;
4949 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
4953 /* Create an image and pixmap serving as mask. */
4954 if (! image_create_x_image_and_pixmap (f
, img
, img
->width
, img
->height
, 1,
4957 #endif /* !HAVE_NS */
4959 /* Create the bit array serving as mask. */
4960 row_width
= (img
->width
+ 7) / 8;
4961 mask_img
= xzalloc (row_width
* img
->height
);
4962 #endif /* HAVE_NTGUI */
4964 /* Get the X image or create a memory device context for IMG. */
4965 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
4967 /* Determine the background color of ximg. If HOW is `(R G B)'
4968 take that as color. Otherwise, use the image's background color. */
4969 use_img_background
= 1;
4975 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4977 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4981 if (i
== 3 && NILP (how
))
4983 char color_name
[30];
4984 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4987 0x00ffffff & /* Filter out palette info. */
4988 #endif /* HAVE_NTGUI */
4989 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4990 use_img_background
= 0;
4994 if (use_img_background
)
4995 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4997 /* Set all bits in mask_img to 1 whose color in ximg is different
4998 from the background color bg. */
5000 for (y
= 0; y
< img
->height
; ++y
)
5001 for (x
= 0; x
< img
->width
; ++x
)
5003 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
5004 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
5006 if (XGetPixel (ximg
, x
, y
) == bg
)
5007 ns_set_alpha (ximg
, x
, y
, 0);
5008 #endif /* HAVE_NS */
5010 /* Fill in the background_transparent field while we have the mask handy. */
5011 image_background_transparent (img
, f
, mask_img
);
5013 /* Put mask_img into the image. */
5014 image_put_x_image (f
, img
, mask_img
, 1);
5015 #endif /* !HAVE_NS */
5017 for (y
= 0; y
< img
->height
; ++y
)
5018 for (x
= 0; x
< img
->width
; ++x
)
5020 COLORREF p
= GetPixel (ximg
, x
, y
);
5022 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
5025 /* Create the mask image. */
5026 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
5028 /* Fill in the background_transparent field while we have the mask handy. */
5029 SelectObject (ximg
, img
->mask
);
5030 image_background_transparent (img
, f
, ximg
);
5032 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
5034 #endif /* HAVE_NTGUI */
5036 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
5040 /***********************************************************************
5041 PBM (mono, gray, color)
5042 ***********************************************************************/
5044 static bool pbm_image_p (Lisp_Object object
);
5045 static bool pbm_load (struct frame
*f
, struct image
*img
);
5047 /* The symbol `pbm' identifying images of this type. */
5049 static Lisp_Object Qpbm
;
5051 /* Indices of image specification fields in gs_format, below. */
5053 enum pbm_keyword_index
5069 /* Vector of image_keyword structures describing the format
5070 of valid user-defined image specifications. */
5072 static const struct image_keyword pbm_format
[PBM_LAST
] =
5074 {":type", IMAGE_SYMBOL_VALUE
, 1},
5075 {":file", IMAGE_STRING_VALUE
, 0},
5076 {":data", IMAGE_STRING_VALUE
, 0},
5077 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5078 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5079 {":relief", IMAGE_INTEGER_VALUE
, 0},
5080 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5081 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5082 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5083 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
5084 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5087 /* Structure describing the image type `pbm'. */
5089 static struct image_type pbm_type
=
5100 /* Return true if OBJECT is a valid PBM image specification. */
5103 pbm_image_p (Lisp_Object object
)
5105 struct image_keyword fmt
[PBM_LAST
];
5107 memcpy (fmt
, pbm_format
, sizeof fmt
);
5109 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
5112 /* Must specify either :data or :file. */
5113 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5117 /* Get next char skipping comments in Netpbm header. Returns -1 at
5121 pbm_next_char (unsigned char **s
, unsigned char *end
)
5125 while (*s
< end
&& (c
= *(*s
)++, c
== '#'))
5127 /* Skip to the next line break. */
5128 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n' && c
!= '\r'))
5138 /* Scan a decimal number from *S and return it. Advance *S while
5139 reading the number. END is the end of the string. Value is -1 at
5143 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5145 int c
= 0, val
= -1;
5147 /* Skip white-space. */
5148 while ((c
= pbm_next_char (s
, end
)) != -1 && c_isspace (c
))
5153 /* Read decimal number. */
5155 while ((c
= pbm_next_char (s
, end
)) != -1 && c_isdigit (c
))
5156 val
= 10 * val
+ c
- '0';
5163 /* Load PBM image IMG for use on frame F. */
5166 pbm_load (struct frame
*f
, struct image
*img
)
5170 int width
, height
, max_color_idx
= 0;
5172 Lisp_Object file
, specified_file
;
5173 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5174 unsigned char *contents
= NULL
;
5175 unsigned char *end
, *p
;
5178 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5180 if (STRINGP (specified_file
))
5182 file
= x_find_image_file (specified_file
);
5183 if (!STRINGP (file
))
5185 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5189 contents
= slurp_file (SSDATA (file
), &size
);
5190 if (contents
== NULL
)
5192 image_error ("Error reading `%s'", file
, Qnil
);
5197 end
= contents
+ size
;
5202 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5203 if (!STRINGP (data
))
5205 image_error ("Invalid image data `%s'", data
, Qnil
);
5209 end
= p
+ SBYTES (data
);
5212 /* Check magic number. */
5213 if (end
- p
< 2 || *p
++ != 'P')
5215 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5224 raw_p
= 0, type
= PBM_MONO
;
5228 raw_p
= 0, type
= PBM_GRAY
;
5232 raw_p
= 0, type
= PBM_COLOR
;
5236 raw_p
= 1, type
= PBM_MONO
;
5240 raw_p
= 1, type
= PBM_GRAY
;
5244 raw_p
= 1, type
= PBM_COLOR
;
5248 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5252 /* Read width, height, maximum color-component. Characters
5253 starting with `#' up to the end of a line are ignored. */
5254 width
= pbm_scan_number (&p
, end
);
5255 height
= pbm_scan_number (&p
, end
);
5257 if (type
!= PBM_MONO
)
5259 max_color_idx
= pbm_scan_number (&p
, end
);
5260 if (max_color_idx
> 65535 || max_color_idx
< 0)
5262 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5267 if (!check_image_size (f
, width
, height
))
5269 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5273 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
5276 /* Initialize the color hash table. */
5277 init_color_table ();
5279 if (type
== PBM_MONO
)
5282 struct image_keyword fmt
[PBM_LAST
];
5283 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5284 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5286 /* Parse the image specification. */
5287 memcpy (fmt
, pbm_format
, sizeof fmt
);
5288 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5290 /* Get foreground and background colors, maybe allocate colors. */
5291 if (fmt
[PBM_FOREGROUND
].count
5292 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5293 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5294 if (fmt
[PBM_BACKGROUND
].count
5295 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5297 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5298 img
->background
= bg
;
5299 img
->background_valid
= 1;
5302 for (y
= 0; y
< height
; ++y
)
5303 for (x
= 0; x
< width
; ++x
)
5311 x_destroy_x_image (ximg
);
5312 x_clear_image (f
, img
);
5313 image_error ("Invalid image size in image `%s'",
5323 g
= pbm_scan_number (&p
, end
);
5325 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5330 int expected_size
= height
* width
;
5331 if (max_color_idx
> 255)
5333 if (type
== PBM_COLOR
)
5336 if (raw_p
&& p
+ expected_size
> end
)
5338 x_destroy_x_image (ximg
);
5339 x_clear_image (f
, img
);
5340 image_error ("Invalid image size in image `%s'",
5345 for (y
= 0; y
< height
; ++y
)
5346 for (x
= 0; x
< width
; ++x
)
5350 if (type
== PBM_GRAY
&& raw_p
)
5353 if (max_color_idx
> 255)
5354 r
= g
= b
= r
* 256 + *p
++;
5356 else if (type
== PBM_GRAY
)
5357 r
= g
= b
= pbm_scan_number (&p
, end
);
5361 if (max_color_idx
> 255)
5364 if (max_color_idx
> 255)
5367 if (max_color_idx
> 255)
5372 r
= pbm_scan_number (&p
, end
);
5373 g
= pbm_scan_number (&p
, end
);
5374 b
= pbm_scan_number (&p
, end
);
5377 if (r
< 0 || g
< 0 || b
< 0)
5379 x_destroy_x_image (ximg
);
5380 image_error ("Invalid pixel value in image `%s'",
5385 /* RGB values are now in the range 0..max_color_idx.
5386 Scale this to the range 0..0xffff supported by X. */
5387 r
= (double) r
* 65535 / max_color_idx
;
5388 g
= (double) g
* 65535 / max_color_idx
;
5389 b
= (double) b
* 65535 / max_color_idx
;
5390 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5394 #ifdef COLOR_TABLE_SUPPORT
5395 /* Store in IMG->colors the colors allocated for the image, and
5396 free the color table. */
5397 img
->colors
= colors_in_color_table (&img
->ncolors
);
5398 free_color_table ();
5399 #endif /* COLOR_TABLE_SUPPORT */
5402 img
->height
= height
;
5404 /* Maybe fill in the background field while we have ximg handy. */
5406 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5407 /* Casting avoids a GCC warning. */
5408 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5410 /* Put ximg into the image. */
5411 image_put_x_image (f
, img
, ximg
, 0);
5413 /* X and W32 versions did it here, MAC version above. ++kfs
5415 img->height = height; */
5422 /***********************************************************************
5424 ***********************************************************************/
5426 #if defined (HAVE_PNG) || defined (HAVE_NS)
5428 /* Function prototypes. */
5430 static bool png_image_p (Lisp_Object object
);
5431 static bool png_load (struct frame
*f
, struct image
*img
);
5433 /* The symbol `png' identifying images of this type. */
5435 static Lisp_Object Qpng
;
5437 /* Indices of image specification fields in png_format, below. */
5439 enum png_keyword_index
5454 /* Vector of image_keyword structures describing the format
5455 of valid user-defined image specifications. */
5457 static const struct image_keyword png_format
[PNG_LAST
] =
5459 {":type", IMAGE_SYMBOL_VALUE
, 1},
5460 {":data", IMAGE_STRING_VALUE
, 0},
5461 {":file", IMAGE_STRING_VALUE
, 0},
5462 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5463 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5464 {":relief", IMAGE_INTEGER_VALUE
, 0},
5465 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5466 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5467 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5468 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5471 #if defined HAVE_NTGUI && defined WINDOWSNT
5472 static bool init_png_functions (void);
5474 #define init_png_functions NULL
5477 /* Structure describing the image type `png'. */
5479 static struct image_type png_type
=
5489 /* Return true if OBJECT is a valid PNG image specification. */
5492 png_image_p (Lisp_Object object
)
5494 struct image_keyword fmt
[PNG_LAST
];
5495 memcpy (fmt
, png_format
, sizeof fmt
);
5497 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5500 /* Must specify either the :data or :file keyword. */
5501 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5504 #endif /* HAVE_PNG || HAVE_NS */
5510 /* PNG library details. */
5512 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5513 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5514 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5515 png_error_ptr
, png_error_ptr
));
5516 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5517 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5518 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5519 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5520 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5521 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5522 png_uint_32
*, png_uint_32
*,
5523 int *, int *, int *, int *, int *));
5524 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5525 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5526 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5527 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5528 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5530 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5531 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5532 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5533 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5534 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5535 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5536 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5538 #if (PNG_LIBPNG_VER >= 10500)
5539 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int)) PNG_NORETURN
;
5540 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5541 #endif /* libpng version >= 1.5 */
5544 init_png_functions (void)
5548 if (!(library
= w32_delayed_load (Qpng
)))
5551 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5552 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5553 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5554 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5555 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5556 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5557 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5558 LOAD_IMGLIB_FN (library
, png_read_info
);
5559 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5560 LOAD_IMGLIB_FN (library
, png_get_valid
);
5561 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5562 LOAD_IMGLIB_FN (library
, png_set_expand
);
5563 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5564 LOAD_IMGLIB_FN (library
, png_set_background
);
5565 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5566 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5567 LOAD_IMGLIB_FN (library
, png_get_channels
);
5568 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5569 LOAD_IMGLIB_FN (library
, png_read_image
);
5570 LOAD_IMGLIB_FN (library
, png_read_end
);
5571 LOAD_IMGLIB_FN (library
, png_error
);
5573 #if (PNG_LIBPNG_VER >= 10500)
5574 LOAD_IMGLIB_FN (library
, png_longjmp
);
5575 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5576 #endif /* libpng version >= 1.5 */
5582 #define fn_png_get_io_ptr png_get_io_ptr
5583 #define fn_png_sig_cmp png_sig_cmp
5584 #define fn_png_create_read_struct png_create_read_struct
5585 #define fn_png_create_info_struct png_create_info_struct
5586 #define fn_png_destroy_read_struct png_destroy_read_struct
5587 #define fn_png_set_read_fn png_set_read_fn
5588 #define fn_png_set_sig_bytes png_set_sig_bytes
5589 #define fn_png_read_info png_read_info
5590 #define fn_png_get_IHDR png_get_IHDR
5591 #define fn_png_get_valid png_get_valid
5592 #define fn_png_set_strip_16 png_set_strip_16
5593 #define fn_png_set_expand png_set_expand
5594 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5595 #define fn_png_set_background png_set_background
5596 #define fn_png_get_bKGD png_get_bKGD
5597 #define fn_png_read_update_info png_read_update_info
5598 #define fn_png_get_channels png_get_channels
5599 #define fn_png_get_rowbytes png_get_rowbytes
5600 #define fn_png_read_image png_read_image
5601 #define fn_png_read_end png_read_end
5602 #define fn_png_error png_error
5604 #if (PNG_LIBPNG_VER >= 10500)
5605 #define fn_png_longjmp png_longjmp
5606 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5607 #endif /* libpng version >= 1.5 */
5609 #endif /* WINDOWSNT */
5611 /* Fast implementations of setjmp and longjmp. Although setjmp and longjmp
5612 will do, POSIX _setjmp and _longjmp (if available) are often faster.
5613 Do not use sys_setjmp, as PNG supports only jmp_buf.
5614 It's OK if the longjmp substitute restores the signal mask. */
5616 # define FAST_SETJMP(j) _setjmp (j)
5617 # define FAST_LONGJMP _longjmp
5619 # define FAST_SETJMP(j) setjmp (j)
5620 # define FAST_LONGJMP longjmp
5623 #if PNG_LIBPNG_VER < 10500
5624 #define PNG_LONGJMP(ptr) FAST_LONGJMP ((ptr)->jmpbuf, 1)
5625 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5627 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5628 #define PNG_LONGJMP(ptr) fn_png_longjmp (ptr, 1)
5629 #define PNG_JMPBUF(ptr) \
5630 (*fn_png_set_longjmp_fn (ptr, FAST_LONGJMP, sizeof (jmp_buf)))
5633 /* Error and warning handlers installed when the PNG library
5636 static _Noreturn
void
5637 my_png_error (png_struct
*png_ptr
, const char *msg
)
5639 eassert (png_ptr
!= NULL
);
5640 /* Avoid compiler warning about deprecated direct access to
5641 png_ptr's fields in libpng versions 1.4.x. */
5642 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5643 PNG_LONGJMP (png_ptr
);
5648 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5650 eassert (png_ptr
!= NULL
);
5651 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5654 /* Memory source for PNG decoding. */
5656 struct png_memory_storage
5658 unsigned char *bytes
; /* The data */
5659 ptrdiff_t len
; /* How big is it? */
5660 ptrdiff_t index
; /* Where are we? */
5664 /* Function set as reader function when reading PNG image from memory.
5665 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5666 bytes from the input to DATA. */
5669 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5671 struct png_memory_storage
*tbr
= fn_png_get_io_ptr (png_ptr
);
5673 if (length
> tbr
->len
- tbr
->index
)
5674 fn_png_error (png_ptr
, "Read error");
5676 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5677 tbr
->index
= tbr
->index
+ length
;
5681 /* Function set as reader function when reading PNG image from a file.
5682 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5683 bytes from the input to DATA. */
5686 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5688 FILE *fp
= fn_png_get_io_ptr (png_ptr
);
5690 if (fread (data
, 1, length
, fp
) < length
)
5691 fn_png_error (png_ptr
, "Read error");
5695 /* Load PNG image IMG for use on frame F. Value is true if
5698 struct png_load_context
5700 /* These are members so that longjmp doesn't munge local variables. */
5701 png_struct
*png_ptr
;
5710 png_load_body (struct frame
*f
, struct image
*img
, struct png_load_context
*c
)
5712 Lisp_Object file
, specified_file
;
5713 Lisp_Object specified_data
;
5716 XImagePtr ximg
, mask_img
= NULL
;
5717 png_struct
*png_ptr
;
5718 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5721 png_byte
*pixels
= NULL
;
5722 png_byte
**rows
= NULL
;
5723 png_uint_32 width
, height
;
5724 int bit_depth
, color_type
, interlace_type
;
5726 png_uint_32 row_bytes
;
5728 struct png_memory_storage tbr
; /* Data to be read */
5730 /* Find out what file to load. */
5731 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5732 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5734 if (NILP (specified_data
))
5736 file
= x_find_image_file (specified_file
);
5737 if (!STRINGP (file
))
5739 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5743 /* Open the image file. */
5744 fp
= emacs_fopen (SSDATA (file
), "rb");
5747 image_error ("Cannot open image file `%s'", file
, Qnil
);
5751 /* Check PNG signature. */
5752 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5753 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5756 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5762 if (!STRINGP (specified_data
))
5764 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5768 /* Read from memory. */
5769 tbr
.bytes
= SDATA (specified_data
);
5770 tbr
.len
= SBYTES (specified_data
);
5773 /* Check PNG signature. */
5774 if (tbr
.len
< sizeof sig
5775 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5777 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5781 /* Need to skip past the signature. */
5782 tbr
.bytes
+= sizeof (sig
);
5785 /* Initialize read and info structs for PNG lib. */
5786 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5791 info_ptr
= fn_png_create_info_struct (png_ptr
);
5792 end_info
= fn_png_create_info_struct (png_ptr
);
5795 c
->png_ptr
= png_ptr
;
5796 c
->info_ptr
= info_ptr
;
5797 c
->end_info
= end_info
;
5802 if (! (info_ptr
&& end_info
))
5804 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5809 if (fp
) fclose (fp
);
5813 /* Set error jump-back. We come back here when the PNG library
5814 detects an error. */
5815 if (FAST_SETJMP (PNG_JMPBUF (png_ptr
)))
5819 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5827 /* Silence a bogus diagnostic; see GCC bug 54561. */
5828 IF_LINT (fp
= c
->fp
);
5830 /* Read image info. */
5831 if (!NILP (specified_data
))
5832 fn_png_set_read_fn (png_ptr
, &tbr
, png_read_from_memory
);
5834 fn_png_set_read_fn (png_ptr
, fp
, png_read_from_file
);
5836 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5837 fn_png_read_info (png_ptr
, info_ptr
);
5838 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5839 &interlace_type
, NULL
, NULL
);
5841 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5842 && check_image_size (f
, width
, height
)))
5844 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5848 /* Create the X image and pixmap now, so that the work below can be
5849 omitted if the image is too large for X. */
5850 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
5853 /* If image contains simply transparency data, we prefer to
5854 construct a clipping mask. */
5855 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5860 /* This function is easier to write if we only have to handle
5861 one data format: RGB or RGBA with 8 bits per channel. Let's
5862 transform other formats into that format. */
5864 /* Strip more than 8 bits per channel. */
5865 if (bit_depth
== 16)
5866 fn_png_set_strip_16 (png_ptr
);
5868 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5870 fn_png_set_expand (png_ptr
);
5872 /* Convert grayscale images to RGB. */
5873 if (color_type
== PNG_COLOR_TYPE_GRAY
5874 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5875 fn_png_set_gray_to_rgb (png_ptr
);
5877 /* Handle alpha channel by combining the image with a background
5878 color. Do this only if a real alpha channel is supplied. For
5879 simple transparency, we prefer a clipping mask. */
5882 /* png_color_16 *image_bg; */
5883 Lisp_Object specified_bg
5884 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5885 int shift
= (bit_depth
== 16) ? 0 : 8;
5887 if (STRINGP (specified_bg
))
5888 /* The user specified `:background', use that. */
5891 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5893 png_color_16 user_bg
;
5895 memset (&user_bg
, 0, sizeof user_bg
);
5896 user_bg
.red
= color
.red
>> shift
;
5897 user_bg
.green
= color
.green
>> shift
;
5898 user_bg
.blue
= color
.blue
>> shift
;
5900 fn_png_set_background (png_ptr
, &user_bg
,
5901 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5906 /* We use the current frame background, ignoring any default
5907 background color set by the image. */
5908 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5910 png_color_16 frame_background
;
5912 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5913 x_query_color (f
, &color
);
5915 memset (&frame_background
, 0, sizeof frame_background
);
5916 frame_background
.red
= color
.red
>> shift
;
5917 frame_background
.green
= color
.green
>> shift
;
5918 frame_background
.blue
= color
.blue
>> shift
;
5919 #endif /* HAVE_X_WINDOWS */
5921 fn_png_set_background (png_ptr
, &frame_background
,
5922 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5926 /* Update info structure. */
5927 fn_png_read_update_info (png_ptr
, info_ptr
);
5929 /* Get number of channels. Valid values are 1 for grayscale images
5930 and images with a palette, 2 for grayscale images with transparency
5931 information (alpha channel), 3 for RGB images, and 4 for RGB
5932 images with alpha channel, i.e. RGBA. If conversions above were
5933 sufficient we should only have 3 or 4 channels here. */
5934 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5935 eassert (channels
== 3 || channels
== 4);
5937 /* Number of bytes needed for one row of the image. */
5938 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5940 /* Allocate memory for the image. */
5941 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5942 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5943 memory_full (SIZE_MAX
);
5944 c
->pixels
= pixels
= xmalloc (sizeof *pixels
* row_bytes
* height
);
5945 c
->rows
= rows
= xmalloc (height
* sizeof *rows
);
5946 for (i
= 0; i
< height
; ++i
)
5947 rows
[i
] = pixels
+ i
* row_bytes
;
5949 /* Read the entire image. */
5950 fn_png_read_image (png_ptr
, rows
);
5951 fn_png_read_end (png_ptr
, info_ptr
);
5958 /* Create an image and pixmap serving as mask if the PNG image
5959 contains an alpha channel. */
5962 && !image_create_x_image_and_pixmap (f
, img
, width
, height
, 1,
5965 x_destroy_x_image (ximg
);
5966 x_clear_image_1 (f
, img
, CLEAR_IMAGE_PIXMAP
);
5970 /* Fill the X image and mask from PNG data. */
5971 init_color_table ();
5973 for (y
= 0; y
< height
; ++y
)
5975 png_byte
*p
= rows
[y
];
5977 for (x
= 0; x
< width
; ++x
)
5984 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5985 /* An alpha channel, aka mask channel, associates variable
5986 transparency with an image. Where other image formats
5987 support binary transparency---fully transparent or fully
5988 opaque---PNG allows up to 254 levels of partial transparency.
5989 The PNG library implements partial transparency by combining
5990 the image with a specified background color.
5992 I'm not sure how to handle this here nicely: because the
5993 background on which the image is displayed may change, for
5994 real alpha channel support, it would be necessary to create
5995 a new image for each possible background.
5997 What I'm doing now is that a mask is created if we have
5998 boolean transparency information. Otherwise I'm using
5999 the frame's background color to combine the image with. */
6004 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
6010 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6011 /* Set IMG's background color from the PNG image, unless the user
6015 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
6017 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
6018 img
->background_valid
= 1;
6022 #ifdef COLOR_TABLE_SUPPORT
6023 /* Remember colors allocated for this image. */
6024 img
->colors
= colors_in_color_table (&img
->ncolors
);
6025 free_color_table ();
6026 #endif /* COLOR_TABLE_SUPPORT */
6029 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
6034 img
->height
= height
;
6036 /* Maybe fill in the background field while we have ximg handy.
6037 Casting avoids a GCC warning. */
6038 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6040 /* Put ximg into the image. */
6041 image_put_x_image (f
, img
, ximg
, 0);
6043 /* Same for the mask. */
6046 /* Fill in the background_transparent field while we have the
6047 mask handy. Casting avoids a GCC warning. */
6048 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
6050 image_put_x_image (f
, img
, mask_img
, 1);
6057 png_load (struct frame
*f
, struct image
*img
)
6059 struct png_load_context c
;
6060 return png_load_body (f
, img
, &c
);
6063 #else /* HAVE_PNG */
6067 png_load (struct frame
*f
, struct image
*img
)
6069 return ns_load_image (f
, img
,
6070 image_spec_value (img
->spec
, QCfile
, NULL
),
6071 image_spec_value (img
->spec
, QCdata
, NULL
));
6073 #endif /* HAVE_NS */
6076 #endif /* !HAVE_PNG */
6080 /***********************************************************************
6082 ***********************************************************************/
6084 #if defined (HAVE_JPEG) || defined (HAVE_NS)
6086 static bool jpeg_image_p (Lisp_Object object
);
6087 static bool jpeg_load (struct frame
*f
, struct image
*img
);
6089 /* The symbol `jpeg' identifying images of this type. */
6091 static Lisp_Object Qjpeg
;
6093 /* Indices of image specification fields in gs_format, below. */
6095 enum jpeg_keyword_index
6104 JPEG_HEURISTIC_MASK
,
6110 /* Vector of image_keyword structures describing the format
6111 of valid user-defined image specifications. */
6113 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
6115 {":type", IMAGE_SYMBOL_VALUE
, 1},
6116 {":data", IMAGE_STRING_VALUE
, 0},
6117 {":file", IMAGE_STRING_VALUE
, 0},
6118 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6119 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6120 {":relief", IMAGE_INTEGER_VALUE
, 0},
6121 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6122 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6123 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6124 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6127 #if defined HAVE_NTGUI && defined WINDOWSNT
6128 static bool init_jpeg_functions (void);
6130 #define init_jpeg_functions NULL
6133 /* Structure describing the image type `jpeg'. */
6135 static struct image_type jpeg_type
=
6141 init_jpeg_functions
,
6145 /* Return true if OBJECT is a valid JPEG image specification. */
6148 jpeg_image_p (Lisp_Object object
)
6150 struct image_keyword fmt
[JPEG_LAST
];
6152 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6154 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6157 /* Must specify either the :data or :file keyword. */
6158 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6161 #endif /* HAVE_JPEG || HAVE_NS */
6165 /* Work around a warning about HAVE_STDLIB_H being redefined in
6167 #ifdef HAVE_STDLIB_H
6168 #undef HAVE_STDLIB_H
6169 #endif /* HAVE_STLIB_H */
6171 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6172 /* In older releases of the jpeg library, jpeglib.h will define boolean
6173 differently depending on __WIN32__, so make sure it is defined. */
6177 /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types.
6178 Some versions of jpeglib try to detect whether rpcndr.h is loaded,
6179 using the Windows boolean type instead of the jpeglib boolean type
6180 if so. Cygwin jpeglib, however, doesn't try to detect whether its
6181 headers are included along with windows.h, so under Cygwin, jpeglib
6182 attempts to define a conflicting boolean type. Worse, forcing
6183 Cygwin jpeglib headers to use the Windows boolean type doesn't work
6184 because it created an ABI incompatibility between the
6185 already-compiled jpeg library and the header interface definition.
6187 The best we can do is to define jpeglib's boolean type to a
6188 different name. This name, jpeg_boolean, remains in effect through
6189 the rest of image.c.
6191 #if defined CYGWIN && defined HAVE_NTGUI
6192 #define boolean jpeg_boolean
6194 #include <jpeglib.h>
6199 /* JPEG library details. */
6200 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6201 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6202 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6203 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6204 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6205 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6206 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6207 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6210 init_jpeg_functions (void)
6214 if (!(library
= w32_delayed_load (Qjpeg
)))
6217 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6218 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6219 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6220 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6221 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6222 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6223 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6224 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6228 /* Wrapper since we can't directly assign the function pointer
6229 to another function pointer that was declared more completely easily. */
6231 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6233 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6238 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6239 #define fn_jpeg_start_decompress jpeg_start_decompress
6240 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6241 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6242 #define fn_jpeg_read_header jpeg_read_header
6243 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6244 #define fn_jpeg_std_error jpeg_std_error
6245 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6247 #endif /* WINDOWSNT */
6249 struct my_jpeg_error_mgr
6251 struct jpeg_error_mgr pub
;
6252 sys_jmp_buf setjmp_buffer
;
6254 /* The remaining members are so that longjmp doesn't munge local
6256 struct jpeg_decompress_struct cinfo
;
6260 MY_JPEG_INVALID_IMAGE_SIZE
,
6261 MY_JPEG_CANNOT_CREATE_X
6269 static _Noreturn
void
6270 my_error_exit (j_common_ptr cinfo
)
6272 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6273 mgr
->failure_code
= MY_JPEG_ERROR_EXIT
;
6274 sys_longjmp (mgr
->setjmp_buffer
, 1);
6278 /* Init source method for JPEG data source manager. Called by
6279 jpeg_read_header() before any data is actually read. See
6280 libjpeg.doc from the JPEG lib distribution. */
6283 our_common_init_source (j_decompress_ptr cinfo
)
6288 /* Method to terminate data source. Called by
6289 jpeg_finish_decompress() after all data has been processed. */
6292 our_common_term_source (j_decompress_ptr cinfo
)
6297 /* Fill input buffer method for JPEG data source manager. Called
6298 whenever more data is needed. We read the whole image in one step,
6299 so this only adds a fake end of input marker at the end. */
6301 static JOCTET our_memory_buffer
[2];
6304 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6306 /* Insert a fake EOI marker. */
6307 struct jpeg_source_mgr
*src
= cinfo
->src
;
6309 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6310 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6312 src
->next_input_byte
= our_memory_buffer
;
6313 src
->bytes_in_buffer
= 2;
6318 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6319 is the JPEG data source manager. */
6322 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6324 struct jpeg_source_mgr
*src
= cinfo
->src
;
6328 if (num_bytes
> src
->bytes_in_buffer
)
6329 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6331 src
->bytes_in_buffer
-= num_bytes
;
6332 src
->next_input_byte
+= num_bytes
;
6337 /* Set up the JPEG lib for reading an image from DATA which contains
6338 LEN bytes. CINFO is the decompression info structure created for
6339 reading the image. */
6342 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, ptrdiff_t len
)
6344 struct jpeg_source_mgr
*src
= cinfo
->src
;
6348 /* First time for this JPEG object? */
6349 src
= cinfo
->mem
->alloc_small ((j_common_ptr
) cinfo
,
6350 JPOOL_PERMANENT
, sizeof *src
);
6352 src
->next_input_byte
= data
;
6355 src
->init_source
= our_common_init_source
;
6356 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6357 src
->skip_input_data
= our_memory_skip_input_data
;
6358 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6359 src
->term_source
= our_common_term_source
;
6360 src
->bytes_in_buffer
= len
;
6361 src
->next_input_byte
= data
;
6365 struct jpeg_stdio_mgr
6367 struct jpeg_source_mgr mgr
;
6374 /* Size of buffer to read JPEG from file.
6375 Not too big, as we want to use alloc_small. */
6376 #define JPEG_STDIO_BUFFER_SIZE 8192
6379 /* Fill input buffer method for JPEG data source manager. Called
6380 whenever more data is needed. The data is read from a FILE *. */
6383 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6385 struct jpeg_stdio_mgr
*src
;
6387 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6392 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6394 src
->mgr
.bytes_in_buffer
= bytes
;
6397 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6399 src
->buffer
[0] = (JOCTET
) 0xFF;
6400 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6401 src
->mgr
.bytes_in_buffer
= 2;
6403 src
->mgr
.next_input_byte
= src
->buffer
;
6410 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6411 is the JPEG data source manager. */
6414 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6416 struct jpeg_stdio_mgr
*src
;
6417 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6419 while (num_bytes
> 0 && !src
->finished
)
6421 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6423 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6424 src
->mgr
.next_input_byte
+= num_bytes
;
6429 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6430 src
->mgr
.bytes_in_buffer
= 0;
6431 src
->mgr
.next_input_byte
= NULL
;
6433 our_stdio_fill_input_buffer (cinfo
);
6439 /* Set up the JPEG lib for reading an image from a FILE *.
6440 CINFO is the decompression info structure created for
6441 reading the image. */
6444 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6446 struct jpeg_stdio_mgr
*src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6450 /* First time for this JPEG object? */
6451 src
= cinfo
->mem
->alloc_small ((j_common_ptr
) cinfo
,
6452 JPOOL_PERMANENT
, sizeof *src
);
6453 cinfo
->src
= (struct jpeg_source_mgr
*) src
;
6454 src
->buffer
= cinfo
->mem
->alloc_small ((j_common_ptr
) cinfo
,
6456 JPEG_STDIO_BUFFER_SIZE
);
6461 src
->mgr
.init_source
= our_common_init_source
;
6462 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6463 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6464 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6465 src
->mgr
.term_source
= our_common_term_source
;
6466 src
->mgr
.bytes_in_buffer
= 0;
6467 src
->mgr
.next_input_byte
= NULL
;
6471 /* Load image IMG for use on frame F. Patterned after example.c
6472 from the JPEG lib. */
6475 jpeg_load_body (struct frame
*f
, struct image
*img
,
6476 struct my_jpeg_error_mgr
*mgr
)
6478 Lisp_Object file
, specified_file
;
6479 Lisp_Object specified_data
;
6482 int row_stride
, x
, y
;
6483 XImagePtr ximg
= NULL
;
6484 unsigned long *colors
;
6487 /* Open the JPEG file. */
6488 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6489 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6491 if (NILP (specified_data
))
6493 file
= x_find_image_file (specified_file
);
6494 if (!STRINGP (file
))
6496 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6500 fp
= emacs_fopen (SSDATA (file
), "rb");
6503 image_error ("Cannot open `%s'", file
, Qnil
);
6507 else if (!STRINGP (specified_data
))
6509 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6513 IF_LINT (mgr
->fp
= fp
);
6515 /* Customize libjpeg's error handling to call my_error_exit when an
6516 error is detected. This function will perform a longjmp. */
6517 mgr
->cinfo
.err
= fn_jpeg_std_error (&mgr
->pub
);
6518 mgr
->pub
.error_exit
= my_error_exit
;
6519 if (sys_setjmp (mgr
->setjmp_buffer
))
6521 switch (mgr
->failure_code
)
6523 case MY_JPEG_ERROR_EXIT
:
6525 char buf
[JMSG_LENGTH_MAX
];
6526 mgr
->cinfo
.err
->format_message ((j_common_ptr
) &mgr
->cinfo
, buf
);
6527 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6528 build_string (buf
));
6532 case MY_JPEG_INVALID_IMAGE_SIZE
:
6533 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6536 case MY_JPEG_CANNOT_CREATE_X
:
6540 /* Close the input file and destroy the JPEG object. */
6543 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6545 /* If we already have an XImage, free that. */
6546 x_destroy_x_image (ximg
);
6548 /* Free pixmap and colors. */
6549 x_clear_image (f
, img
);
6553 /* Silence a bogus diagnostic; see GCC bug 54561. */
6554 IF_LINT (fp
= mgr
->fp
);
6556 /* Create the JPEG decompression object. Let it read from fp.
6557 Read the JPEG image header. */
6558 fn_jpeg_CreateDecompress (&mgr
->cinfo
, JPEG_LIB_VERSION
, sizeof *&mgr
->cinfo
);
6560 if (NILP (specified_data
))
6561 jpeg_file_src (&mgr
->cinfo
, fp
);
6563 jpeg_memory_src (&mgr
->cinfo
, SDATA (specified_data
),
6564 SBYTES (specified_data
));
6566 fn_jpeg_read_header (&mgr
->cinfo
, 1);
6568 /* Customize decompression so that color quantization will be used.
6569 Start decompression. */
6570 mgr
->cinfo
.quantize_colors
= 1;
6571 fn_jpeg_start_decompress (&mgr
->cinfo
);
6572 width
= img
->width
= mgr
->cinfo
.output_width
;
6573 height
= img
->height
= mgr
->cinfo
.output_height
;
6575 if (!check_image_size (f
, width
, height
))
6577 mgr
->failure_code
= MY_JPEG_INVALID_IMAGE_SIZE
;
6578 sys_longjmp (mgr
->setjmp_buffer
, 1);
6581 /* Create X image and pixmap. */
6582 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
6584 mgr
->failure_code
= MY_JPEG_CANNOT_CREATE_X
;
6585 sys_longjmp (mgr
->setjmp_buffer
, 1);
6588 /* Allocate colors. When color quantization is used,
6589 mgr->cinfo.actual_number_of_colors has been set with the number of
6590 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6591 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6592 No more than 255 colors will be generated. */
6596 if (mgr
->cinfo
.out_color_components
> 2)
6597 ir
= 0, ig
= 1, ib
= 2;
6598 else if (mgr
->cinfo
.out_color_components
> 1)
6599 ir
= 0, ig
= 1, ib
= 0;
6601 ir
= 0, ig
= 0, ib
= 0;
6603 /* Use the color table mechanism because it handles colors that
6604 cannot be allocated nicely. Such colors will be replaced with
6605 a default color, and we don't have to care about which colors
6606 can be freed safely, and which can't. */
6607 init_color_table ();
6608 colors
= alloca (mgr
->cinfo
.actual_number_of_colors
* sizeof *colors
);
6610 for (i
= 0; i
< mgr
->cinfo
.actual_number_of_colors
; ++i
)
6612 /* Multiply RGB values with 255 because X expects RGB values
6613 in the range 0..0xffff. */
6614 int r
= mgr
->cinfo
.colormap
[ir
][i
] << 8;
6615 int g
= mgr
->cinfo
.colormap
[ig
][i
] << 8;
6616 int b
= mgr
->cinfo
.colormap
[ib
][i
] << 8;
6617 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6620 #ifdef COLOR_TABLE_SUPPORT
6621 /* Remember those colors actually allocated. */
6622 img
->colors
= colors_in_color_table (&img
->ncolors
);
6623 free_color_table ();
6624 #endif /* COLOR_TABLE_SUPPORT */
6628 row_stride
= width
* mgr
->cinfo
.output_components
;
6629 buffer
= mgr
->cinfo
.mem
->alloc_sarray ((j_common_ptr
) &mgr
->cinfo
,
6630 JPOOL_IMAGE
, row_stride
, 1);
6631 for (y
= 0; y
< height
; ++y
)
6633 fn_jpeg_read_scanlines (&mgr
->cinfo
, buffer
, 1);
6634 for (x
= 0; x
< mgr
->cinfo
.output_width
; ++x
)
6635 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6639 fn_jpeg_finish_decompress (&mgr
->cinfo
);
6640 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6644 /* Maybe fill in the background field while we have ximg handy. */
6645 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6646 /* Casting avoids a GCC warning. */
6647 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6649 /* Put ximg into the image. */
6650 image_put_x_image (f
, img
, ximg
, 0);
6655 jpeg_load (struct frame
*f
, struct image
*img
)
6657 struct my_jpeg_error_mgr mgr
;
6658 return jpeg_load_body (f
, img
, &mgr
);
6661 #else /* HAVE_JPEG */
6665 jpeg_load (struct frame
*f
, struct image
*img
)
6667 return ns_load_image (f
, img
,
6668 image_spec_value (img
->spec
, QCfile
, NULL
),
6669 image_spec_value (img
->spec
, QCdata
, NULL
));
6671 #endif /* HAVE_NS */
6673 #endif /* !HAVE_JPEG */
6677 /***********************************************************************
6679 ***********************************************************************/
6681 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6683 static bool tiff_image_p (Lisp_Object object
);
6684 static bool tiff_load (struct frame
*f
, struct image
*img
);
6686 /* The symbol `tiff' identifying images of this type. */
6688 static Lisp_Object Qtiff
;
6690 /* Indices of image specification fields in tiff_format, below. */
6692 enum tiff_keyword_index
6701 TIFF_HEURISTIC_MASK
,
6708 /* Vector of image_keyword structures describing the format
6709 of valid user-defined image specifications. */
6711 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6713 {":type", IMAGE_SYMBOL_VALUE
, 1},
6714 {":data", IMAGE_STRING_VALUE
, 0},
6715 {":file", IMAGE_STRING_VALUE
, 0},
6716 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6717 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6718 {":relief", IMAGE_INTEGER_VALUE
, 0},
6719 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6720 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6721 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6722 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6723 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6726 #if defined HAVE_NTGUI && defined WINDOWSNT
6727 static bool init_tiff_functions (void);
6729 #define init_tiff_functions NULL
6732 /* Structure describing the image type `tiff'. */
6734 static struct image_type tiff_type
=
6740 init_tiff_functions
,
6744 /* Return true if OBJECT is a valid TIFF image specification. */
6747 tiff_image_p (Lisp_Object object
)
6749 struct image_keyword fmt
[TIFF_LAST
];
6750 memcpy (fmt
, tiff_format
, sizeof fmt
);
6752 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6755 /* Must specify either the :data or :file keyword. */
6756 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6759 #endif /* HAVE_TIFF || HAVE_NS */
6767 /* TIFF library details. */
6768 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6769 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6770 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6771 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6772 TIFFReadWriteProc
, TIFFReadWriteProc
,
6773 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6774 TIFFMapFileProc
, TIFFUnmapFileProc
));
6775 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6776 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6777 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6778 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6781 init_tiff_functions (void)
6785 if (!(library
= w32_delayed_load (Qtiff
)))
6788 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6789 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6790 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6791 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6792 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6793 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6794 LOAD_IMGLIB_FN (library
, TIFFClose
);
6795 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6801 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6802 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6803 #define fn_TIFFOpen TIFFOpen
6804 #define fn_TIFFClientOpen TIFFClientOpen
6805 #define fn_TIFFGetField TIFFGetField
6806 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6807 #define fn_TIFFClose TIFFClose
6808 #define fn_TIFFSetDirectory TIFFSetDirectory
6809 #endif /* WINDOWSNT */
6812 /* Reading from a memory buffer for TIFF images Based on the PNG
6813 memory source, but we have to provide a lot of extra functions.
6816 We really only need to implement read and seek, but I am not
6817 convinced that the TIFF library is smart enough not to destroy
6818 itself if we only hand it the function pointers we need to
6823 unsigned char *bytes
;
6830 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6832 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6834 size
= min (size
, src
->len
- src
->index
);
6835 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6841 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6847 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6849 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6854 case SEEK_SET
: /* Go from beginning of source. */
6858 case SEEK_END
: /* Go from end of source. */
6859 idx
= src
->len
+ off
;
6862 case SEEK_CUR
: /* Go from current position. */
6863 idx
= src
->index
+ off
;
6866 default: /* Invalid `whence'. */
6870 if (idx
> src
->len
|| idx
< 0)
6878 tiff_close_memory (thandle_t data
)
6885 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6887 /* It is already _IN_ memory. */
6892 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6894 /* We don't need to do this. */
6898 tiff_size_of_memory (thandle_t data
)
6900 return ((tiff_memory_source
*) data
)->len
;
6903 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6904 compiler error compiling tiff_handler, see Bugzilla bug #17406
6905 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6906 this function as external works around that problem. */
6907 #if defined (__MINGW32__) && __GNUC__ == 3
6908 # define MINGW_STATIC
6910 # define MINGW_STATIC static
6914 tiff_handler (const char *, const char *, const char *, va_list)
6915 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6917 tiff_handler (const char *log_format
, const char *title
,
6918 const char *format
, va_list ap
)
6920 /* doprnt is not suitable here, as TIFF handlers are called from
6921 libtiff and are passed arbitrary printf directives. Instead, use
6922 vsnprintf, taking care to be portable to nonstandard environments
6923 where vsnprintf returns -1 on buffer overflow. Since it's just a
6924 log entry, it's OK to truncate it. */
6926 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6927 add_to_log (log_format
, build_string (title
),
6928 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6932 static void tiff_error_handler (const char *, const char *, va_list)
6933 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6935 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6937 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6941 static void tiff_warning_handler (const char *, const char *, va_list)
6942 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6944 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6946 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6950 /* Load TIFF image IMG for use on frame F. Value is true if
6954 tiff_load (struct frame
*f
, struct image
*img
)
6956 Lisp_Object file
, specified_file
;
6957 Lisp_Object specified_data
;
6959 int width
, height
, x
, y
, count
;
6963 tiff_memory_source memsrc
;
6966 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6967 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6969 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6970 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6972 if (NILP (specified_data
))
6974 /* Read from a file */
6975 file
= x_find_image_file (specified_file
);
6976 if (!STRINGP (file
))
6978 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6982 file
= ansi_encode_filename (file
);
6985 /* Try to open the image file. */
6986 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6989 image_error ("Cannot open `%s'", file
, Qnil
);
6995 if (!STRINGP (specified_data
))
6997 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7001 /* Memory source! */
7002 memsrc
.bytes
= SDATA (specified_data
);
7003 memsrc
.len
= SBYTES (specified_data
);
7006 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
7007 tiff_read_from_memory
,
7008 tiff_write_from_memory
,
7009 tiff_seek_in_memory
,
7011 tiff_size_of_memory
,
7017 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
7022 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7023 if (INTEGERP (image
))
7025 EMACS_INT ino
= XFASTINT (image
);
7026 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
7027 && fn_TIFFSetDirectory (tiff
, ino
)))
7029 image_error ("Invalid image number `%s' in image `%s'",
7031 fn_TIFFClose (tiff
);
7036 /* Get width and height of the image, and allocate a raster buffer
7037 of width x height 32-bit values. */
7038 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
7039 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
7041 if (!check_image_size (f
, width
, height
))
7043 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7044 fn_TIFFClose (tiff
);
7048 /* Create the X image and pixmap. */
7049 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
7050 && image_create_x_image_and_pixmap (f
, img
, width
, height
, 0,
7053 fn_TIFFClose (tiff
);
7057 buf
= xmalloc (sizeof *buf
* width
* height
);
7059 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
7061 /* Count the number of images in the file. */
7062 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
7066 img
->lisp_data
= Fcons (Qcount
,
7067 Fcons (make_number (count
),
7070 fn_TIFFClose (tiff
);
7073 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
7078 /* Initialize the color table. */
7079 init_color_table ();
7081 /* Process the pixel raster. Origin is in the lower-left corner. */
7082 for (y
= 0; y
< height
; ++y
)
7084 uint32
*row
= buf
+ y
* width
;
7086 for (x
= 0; x
< width
; ++x
)
7088 uint32 abgr
= row
[x
];
7089 int r
= TIFFGetR (abgr
) << 8;
7090 int g
= TIFFGetG (abgr
) << 8;
7091 int b
= TIFFGetB (abgr
) << 8;
7092 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
7096 #ifdef COLOR_TABLE_SUPPORT
7097 /* Remember the colors allocated for the image. Free the color table. */
7098 img
->colors
= colors_in_color_table (&img
->ncolors
);
7099 free_color_table ();
7100 #endif /* COLOR_TABLE_SUPPORT */
7103 img
->height
= height
;
7105 /* Maybe fill in the background field while we have ximg handy. */
7106 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7107 /* Casting avoids a GCC warning on W32. */
7108 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7110 /* Put ximg into the image. */
7111 image_put_x_image (f
, img
, ximg
, 0);
7117 #else /* HAVE_TIFF */
7121 tiff_load (struct frame
*f
, struct image
*img
)
7123 return ns_load_image (f
, img
,
7124 image_spec_value (img
->spec
, QCfile
, NULL
),
7125 image_spec_value (img
->spec
, QCdata
, NULL
));
7127 #endif /* HAVE_NS */
7129 #endif /* !HAVE_TIFF */
7133 /***********************************************************************
7135 ***********************************************************************/
7137 #if defined (HAVE_GIF) || defined (HAVE_NS)
7139 static bool gif_image_p (Lisp_Object object
);
7140 static bool gif_load (struct frame
*f
, struct image
*img
);
7141 static void gif_clear_image (struct frame
*f
, struct image
*img
);
7143 /* The symbol `gif' identifying images of this type. */
7145 static Lisp_Object Qgif
;
7147 /* Indices of image specification fields in gif_format, below. */
7149 enum gif_keyword_index
7165 /* Vector of image_keyword structures describing the format
7166 of valid user-defined image specifications. */
7168 static const struct image_keyword gif_format
[GIF_LAST
] =
7170 {":type", IMAGE_SYMBOL_VALUE
, 1},
7171 {":data", IMAGE_STRING_VALUE
, 0},
7172 {":file", IMAGE_STRING_VALUE
, 0},
7173 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7174 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7175 {":relief", IMAGE_INTEGER_VALUE
, 0},
7176 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7177 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7178 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7179 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7180 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7183 #if defined HAVE_NTGUI && defined WINDOWSNT
7184 static bool init_gif_functions (void);
7186 #define init_gif_functions NULL
7189 /* Structure describing the image type `gif'. */
7191 static struct image_type gif_type
=
7201 /* Free X resources of GIF image IMG which is used on frame F. */
7204 gif_clear_image (struct frame
*f
, struct image
*img
)
7206 img
->lisp_data
= Qnil
;
7207 x_clear_image (f
, img
);
7210 /* Return true if OBJECT is a valid GIF image specification. */
7213 gif_image_p (Lisp_Object object
)
7215 struct image_keyword fmt
[GIF_LAST
];
7216 memcpy (fmt
, gif_format
, sizeof fmt
);
7218 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7221 /* Must specify either the :data or :file keyword. */
7222 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7225 #endif /* HAVE_GIF */
7229 #if defined (HAVE_NTGUI)
7231 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7232 Undefine before redefining to avoid a preprocessor warning. */
7236 /* avoid conflict with QuickdrawText.h */
7237 #define DrawText gif_DrawText
7238 #include <gif_lib.h>
7241 /* Giflib before 5.0 didn't define these macros (used only if HAVE_NTGUI). */
7242 #ifndef GIFLIB_MINOR
7243 #define GIFLIB_MINOR 0
7245 #ifndef GIFLIB_RELEASE
7246 #define GIFLIB_RELEASE 0
7249 #else /* HAVE_NTGUI */
7251 #include <gif_lib.h>
7253 #endif /* HAVE_NTGUI */
7255 /* Giflib before 5.0 didn't define these macros. */
7256 #ifndef GIFLIB_MAJOR
7257 #define GIFLIB_MAJOR 4
7262 /* GIF library details. */
7263 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7264 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7265 #if GIFLIB_MAJOR < 5
7266 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7267 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7269 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
, int *));
7270 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *, int *));
7271 DEF_IMGLIB_FN (char *, GifErrorString
, (int));
7275 init_gif_functions (void)
7279 if (!(library
= w32_delayed_load (Qgif
)))
7282 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7283 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7284 LOAD_IMGLIB_FN (library
, DGifOpen
);
7285 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7286 #if GIFLIB_MAJOR >= 5
7287 LOAD_IMGLIB_FN (library
, GifErrorString
);
7294 #define fn_DGifCloseFile DGifCloseFile
7295 #define fn_DGifSlurp DGifSlurp
7296 #define fn_DGifOpen DGifOpen
7297 #define fn_DGifOpenFileName DGifOpenFileName
7298 #if 5 <= GIFLIB_MAJOR
7299 # define fn_GifErrorString GifErrorString
7302 #endif /* WINDOWSNT */
7304 /* Reading a GIF image from memory
7305 Based on the PNG memory stuff to a certain extent. */
7309 unsigned char *bytes
;
7315 /* Make the current memory source available to gif_read_from_memory.
7316 It's done this way because not all versions of libungif support
7317 a UserData field in the GifFileType structure. */
7318 static gif_memory_source
*current_gif_memory_src
;
7321 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7323 gif_memory_source
*src
= current_gif_memory_src
;
7325 if (len
> src
->len
- src
->index
)
7328 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7334 /* Load GIF image IMG for use on frame F. Value is true if
7337 static const int interlace_start
[] = {0, 4, 2, 1};
7338 static const int interlace_increment
[] = {8, 8, 4, 2};
7340 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7343 gif_load (struct frame
*f
, struct image
*img
)
7346 int rc
, width
, height
, x
, y
, i
, j
;
7348 ColorMapObject
*gif_color_map
;
7349 unsigned long pixel_colors
[256];
7351 gif_memory_source memsrc
;
7352 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7353 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7354 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7355 unsigned long bgcolor
= 0;
7357 #if GIFLIB_MAJOR >= 5
7361 if (NILP (specified_data
))
7363 file
= x_find_image_file (specified_file
);
7364 if (!STRINGP (file
))
7366 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7370 file
= ansi_encode_filename (file
);
7373 /* Open the GIF file. */
7374 #if GIFLIB_MAJOR < 5
7375 gif
= fn_DGifOpenFileName (SSDATA (file
));
7378 image_error ("Cannot open `%s'", file
, Qnil
);
7382 gif
= fn_DGifOpenFileName (SSDATA (file
), &gif_err
);
7385 image_error ("Cannot open `%s': %s",
7386 file
, build_string (fn_GifErrorString (gif_err
)));
7393 if (!STRINGP (specified_data
))
7395 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7399 /* Read from memory! */
7400 current_gif_memory_src
= &memsrc
;
7401 memsrc
.bytes
= SDATA (specified_data
);
7402 memsrc
.len
= SBYTES (specified_data
);
7405 #if GIFLIB_MAJOR < 5
7406 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7409 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7413 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
, &gif_err
);
7416 image_error ("Cannot open memory source `%s': %s",
7417 img
->spec
, build_string (fn_GifErrorString (gif_err
)));
7423 /* Before reading entire contents, check the declared image size. */
7424 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7426 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7427 fn_DGifCloseFile (gif
);
7431 /* Read entire contents. */
7432 rc
= fn_DGifSlurp (gif
);
7433 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7435 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7436 fn_DGifCloseFile (gif
);
7440 /* Which sub-image are we to display? */
7442 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7443 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7444 if (idx
< 0 || idx
>= gif
->ImageCount
)
7446 image_error ("Invalid image number `%s' in image `%s'",
7447 image_number
, img
->spec
);
7448 fn_DGifCloseFile (gif
);
7453 width
= img
->width
= gif
->SWidth
;
7454 height
= img
->height
= gif
->SHeight
;
7456 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Top
;
7457 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Left
;
7458 img
->corners
[BOT_CORNER
]
7459 = img
->corners
[TOP_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Height
;
7460 img
->corners
[RIGHT_CORNER
]
7461 = img
->corners
[LEFT_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Width
;
7463 if (!check_image_size (f
, width
, height
))
7465 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7466 fn_DGifCloseFile (gif
);
7470 /* Check that the selected subimages fit. It's not clear whether
7471 the GIF spec requires this, but Emacs can crash if they don't fit. */
7472 for (j
= 0; j
<= idx
; ++j
)
7474 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7475 int subimg_width
= subimage
->ImageDesc
.Width
;
7476 int subimg_height
= subimage
->ImageDesc
.Height
;
7477 int subimg_top
= subimage
->ImageDesc
.Top
;
7478 int subimg_left
= subimage
->ImageDesc
.Left
;
7479 if (! (0 <= subimg_width
&& 0 <= subimg_height
7480 && 0 <= subimg_top
&& subimg_top
<= height
- subimg_height
7481 && 0 <= subimg_left
&& subimg_left
<= width
- subimg_width
))
7483 image_error ("Subimage does not fit in image", Qnil
, Qnil
);
7484 fn_DGifCloseFile (gif
);
7489 /* Create the X image and pixmap. */
7490 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
7492 fn_DGifCloseFile (gif
);
7496 /* Clear the part of the screen image not covered by the image.
7497 Full animated GIF support requires more here (see the gif89 spec,
7498 disposal methods). Let's simply assume that the part not covered
7499 by a sub-image is in the frame's background color. */
7500 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7501 for (x
= 0; x
< width
; ++x
)
7502 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7504 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7505 for (x
= 0; x
< width
; ++x
)
7506 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7508 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7510 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7511 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7512 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7513 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7516 /* Read the GIF image into the X image. */
7518 /* FIXME: With the current implementation, loading an animated gif
7519 is quadratic in the number of animation frames, since each frame
7520 is a separate struct image. We must provide a way for a single
7521 gif_load call to construct and save all animation frames. */
7523 init_color_table ();
7524 if (STRINGP (specified_bg
))
7525 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7526 FRAME_BACKGROUND_PIXEL (f
));
7527 for (j
= 0; j
<= idx
; ++j
)
7529 /* We use a local variable `raster' here because RasterBits is a
7530 char *, which invites problems with bytes >= 0x80. */
7531 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7532 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7533 int transparency_color_index
= -1;
7535 int subimg_width
= subimage
->ImageDesc
.Width
;
7536 int subimg_height
= subimage
->ImageDesc
.Height
;
7537 int subimg_top
= subimage
->ImageDesc
.Top
;
7538 int subimg_left
= subimage
->ImageDesc
.Left
;
7540 /* Find the Graphic Control Extension block for this sub-image.
7541 Extract the disposal method and transparency color. */
7542 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7544 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7546 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7547 && extblock
->ByteCount
== 4
7548 && extblock
->Bytes
[0] & 1)
7550 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7551 to background". Treat any other value like 2. */
7552 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7553 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7558 /* We can't "keep in place" the first subimage. */
7562 /* For disposal == 0, the spec says "No disposal specified. The
7563 decoder is not required to take any action." In practice, it
7564 seems we need to treat this like "keep in place", see e.g.
7565 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7569 /* Allocate subimage colors. */
7570 memset (pixel_colors
, 0, sizeof pixel_colors
);
7571 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7573 gif_color_map
= gif
->SColorMap
;
7576 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7578 if (transparency_color_index
== i
)
7579 pixel_colors
[i
] = STRINGP (specified_bg
)
7580 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7583 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7584 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7585 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7586 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7590 /* Apply the pixel values. */
7591 if (GIFLIB_MAJOR
< 5 && gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7595 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7597 y
++, row
+= interlace_increment
[pass
])
7599 while (subimg_height
<= row
)
7600 row
= interlace_start
[++pass
];
7602 for (x
= 0; x
< subimg_width
; x
++)
7604 int c
= raster
[y
* subimg_width
+ x
];
7605 if (transparency_color_index
!= c
|| disposal
!= 1)
7606 XPutPixel (ximg
, x
+ subimg_left
, row
+ subimg_top
,
7613 for (y
= 0; y
< subimg_height
; ++y
)
7614 for (x
= 0; x
< subimg_width
; ++x
)
7616 int c
= raster
[y
* subimg_width
+ x
];
7617 if (transparency_color_index
!= c
|| disposal
!= 1)
7618 XPutPixel (ximg
, x
+ subimg_left
, y
+ subimg_top
,
7624 #ifdef COLOR_TABLE_SUPPORT
7625 img
->colors
= colors_in_color_table (&img
->ncolors
);
7626 free_color_table ();
7627 #endif /* COLOR_TABLE_SUPPORT */
7629 /* Save GIF image extension data for `image-metadata'.
7630 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7631 img
->lisp_data
= Qnil
;
7632 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7635 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7636 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7637 /* Append (... FUNCTION "BYTES") */
7640 = Fcons (make_number (ext
->Function
),
7641 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7643 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7644 && ext
->ByteCount
== 4)
7646 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7647 delay
|= ext
->Bytes
[1];
7650 img
->lisp_data
= list2 (Qextension_data
, img
->lisp_data
);
7654 Fcons (make_float (delay
/ 100.0),
7658 if (gif
->ImageCount
> 1)
7659 img
->lisp_data
= Fcons (Qcount
,
7660 Fcons (make_number (gif
->ImageCount
),
7663 fn_DGifCloseFile (gif
);
7665 /* Maybe fill in the background field while we have ximg handy. */
7666 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7667 /* Casting avoids a GCC warning. */
7668 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7670 /* Put ximg into the image. */
7671 image_put_x_image (f
, img
, ximg
, 0);
7676 #else /* !HAVE_GIF */
7680 gif_load (struct frame
*f
, struct image
*img
)
7682 return ns_load_image (f
, img
,
7683 image_spec_value (img
->spec
, QCfile
, NULL
),
7684 image_spec_value (img
->spec
, QCdata
, NULL
));
7686 #endif /* HAVE_NS */
7688 #endif /* HAVE_GIF */
7691 #ifdef HAVE_IMAGEMAGICK
7693 /***********************************************************************
7695 ***********************************************************************/
7697 /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER,
7698 safely rounded and clipped to int range. */
7701 scale_image_size (int size
, size_t divisor
, size_t multiplier
)
7706 double scaled
= s
* multiplier
/ divisor
+ 0.5;
7707 if (scaled
< INT_MAX
)
7713 /* Compute the desired size of an image with native size WIDTH x HEIGHT.
7714 Use SPEC to deduce the size. Store the desired size into
7715 *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */
7717 compute_image_size (size_t width
, size_t height
,
7719 int *d_width
, int *d_height
)
7722 int desired_width
, desired_height
;
7724 /* If width and/or height is set in the display spec assume we want
7725 to scale to those values. If either h or w is unspecified, the
7726 unspecified should be calculated from the specified to preserve
7728 value
= image_spec_value (spec
, QCwidth
, NULL
);
7729 desired_width
= NATNUMP (value
) ? min (XFASTINT (value
), INT_MAX
) : -1;
7730 value
= image_spec_value (spec
, QCheight
, NULL
);
7731 desired_height
= NATNUMP (value
) ? min (XFASTINT (value
), INT_MAX
) : -1;
7733 if (desired_width
== -1)
7735 value
= image_spec_value (spec
, QCmax_width
, NULL
);
7736 if (NATNUMP (value
))
7738 int max_width
= min (XFASTINT (value
), INT_MAX
);
7739 if (max_width
< width
)
7741 /* The image is wider than :max-width. */
7742 desired_width
= max_width
;
7743 if (desired_height
== -1)
7745 desired_height
= scale_image_size (desired_width
,
7747 value
= image_spec_value (spec
, QCmax_height
, NULL
);
7748 if (NATNUMP (value
))
7750 int max_height
= min (XFASTINT (value
), INT_MAX
);
7751 if (max_height
< desired_height
)
7753 desired_height
= max_height
;
7754 desired_width
= scale_image_size (desired_height
,
7763 if (desired_height
== -1)
7765 value
= image_spec_value (spec
, QCmax_height
, NULL
);
7766 if (NATNUMP (value
))
7768 int max_height
= min (XFASTINT (value
), INT_MAX
);
7769 if (max_height
< height
)
7770 desired_height
= max_height
;
7774 if (desired_width
!= -1 && desired_height
== -1)
7775 /* w known, calculate h. */
7776 desired_height
= scale_image_size (desired_width
, width
, height
);
7778 if (desired_width
== -1 && desired_height
!= -1)
7779 /* h known, calculate w. */
7780 desired_width
= scale_image_size (desired_height
, height
, width
);
7782 *d_width
= desired_width
;
7783 *d_height
= desired_height
;
7786 static Lisp_Object Qimagemagick
;
7788 static bool imagemagick_image_p (Lisp_Object
);
7789 static bool imagemagick_load (struct frame
*, struct image
*);
7790 static void imagemagick_clear_image (struct frame
*, struct image
*);
7792 /* Indices of image specification fields in imagemagick_format. */
7794 enum imagemagick_keyword_index
7802 IMAGEMAGICK_ALGORITHM
,
7803 IMAGEMAGICK_HEURISTIC_MASK
,
7805 IMAGEMAGICK_BACKGROUND
,
7808 IMAGEMAGICK_MAX_HEIGHT
,
7809 IMAGEMAGICK_MAX_WIDTH
,
7811 IMAGEMAGICK_ROTATION
,
7816 /* Vector of image_keyword structures describing the format
7817 of valid user-defined image specifications. */
7819 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7821 {":type", IMAGE_SYMBOL_VALUE
, 1},
7822 {":data", IMAGE_STRING_VALUE
, 0},
7823 {":file", IMAGE_STRING_VALUE
, 0},
7824 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7825 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7826 {":relief", IMAGE_INTEGER_VALUE
, 0},
7827 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7828 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7829 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7830 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7831 {":height", IMAGE_INTEGER_VALUE
, 0},
7832 {":width", IMAGE_INTEGER_VALUE
, 0},
7833 {":max-height", IMAGE_INTEGER_VALUE
, 0},
7834 {":max-width", IMAGE_INTEGER_VALUE
, 0},
7835 {":format", IMAGE_SYMBOL_VALUE
, 0},
7836 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7837 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7840 #if defined HAVE_NTGUI && defined WINDOWSNT
7841 static bool init_imagemagick_functions (void);
7843 #define init_imagemagick_functions NULL
7846 /* Structure describing the image type for any image handled via
7849 static struct image_type imagemagick_type
=
7852 imagemagick_image_p
,
7854 imagemagick_clear_image
,
7855 init_imagemagick_functions
,
7859 /* Free X resources of imagemagick image IMG which is used on frame F. */
7862 imagemagick_clear_image (struct frame
*f
,
7865 x_clear_image (f
, img
);
7868 /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do
7869 this by calling parse_image_spec and supplying the keywords that
7870 identify the IMAGEMAGICK format. */
7873 imagemagick_image_p (Lisp_Object object
)
7875 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7876 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7878 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7881 /* Must specify either the :data or :file keyword. */
7882 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7885 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7886 Therefore rename the function so it doesn't collide with ImageMagick. */
7887 #define DrawRectangle DrawRectangleGif
7888 #include <wand/MagickWand.h>
7890 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7891 Emacs seems to work fine with the hidden version, so unhide it. */
7892 #include <magick/version.h>
7893 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7894 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7895 MagickPixelPacket
*);
7898 /* Log ImageMagick error message.
7899 Useful when a ImageMagick function returns the status `MagickFalse'. */
7902 imagemagick_error (MagickWand
*wand
)
7905 ExceptionType severity
;
7907 description
= MagickGetException (wand
, &severity
);
7908 image_error ("ImageMagick error: %s",
7909 build_string (description
),
7911 MagickRelinquishMemory (description
);
7914 /* Possibly give ImageMagick some extra help to determine the image
7915 type by supplying a "dummy" filename based on the Content-Type. */
7918 imagemagick_filename_hint (Lisp_Object spec
, char hint_buffer
[MaxTextExtent
])
7920 Lisp_Object symbol
= intern ("image-format-suffixes");
7921 Lisp_Object val
= find_symbol_value (symbol
);
7927 format
= image_spec_value (spec
, intern (":format"), NULL
);
7928 val
= Fcar_safe (Fcdr_safe (Fassq (format
, val
)));
7929 if (! STRINGP (val
))
7932 /* It's OK to truncate the hint if it has MaxTextExtent or more bytes,
7933 as ImageMagick would ignore the extra bytes anyway. */
7934 snprintf (hint_buffer
, MaxTextExtent
, "/tmp/foo.%s", SSDATA (val
));
7938 /* Animated images (e.g., GIF89a) are composed from one "master image"
7939 (which is the first one, and then there's a number of images that
7940 follow. If following images have non-transparent colors, these are
7941 composed "on top" of the master image. So, in general, one has to
7942 compute ann the preceding images to be able to display a particular
7945 Computing all the preceding images is too slow, so we maintain a
7946 cache of previously computed images. We have to maintain a cache
7947 separate from the image cache, because the images may be scaled
7950 struct animation_cache
7954 struct timespec update_time
;
7955 struct animation_cache
*next
;
7956 char signature
[FLEXIBLE_ARRAY_MEMBER
];
7959 static struct animation_cache
*animation_cache
= NULL
;
7961 static struct animation_cache
*
7962 imagemagick_create_cache (char *signature
)
7964 struct animation_cache
*cache
7965 = xmalloc (offsetof (struct animation_cache
, signature
)
7966 + strlen (signature
) + 1);
7970 strcpy (cache
->signature
, signature
);
7974 /* Discard cached images that haven't been used for a minute. */
7976 imagemagick_prune_animation_cache (void)
7978 struct animation_cache
**pcache
= &animation_cache
;
7979 struct timespec old
= timespec_sub (current_timespec (),
7980 make_timespec (60, 0));
7984 struct animation_cache
*cache
= *pcache
;
7985 if (timespec_cmp (old
, cache
->update_time
) <= 0)
7986 pcache
= &cache
->next
;
7990 DestroyMagickWand (cache
->wand
);
7991 *pcache
= cache
->next
;
7997 static struct animation_cache
*
7998 imagemagick_get_animation_cache (MagickWand
*wand
)
8000 char *signature
= MagickGetImageSignature (wand
);
8001 struct animation_cache
*cache
;
8002 struct animation_cache
**pcache
= &animation_cache
;
8004 imagemagick_prune_animation_cache ();
8011 *pcache
= cache
= imagemagick_create_cache (signature
);
8014 if (strcmp (signature
, cache
->signature
) == 0)
8016 pcache
= &cache
->next
;
8019 DestroyString (signature
);
8020 cache
->update_time
= current_timespec ();
8025 imagemagick_compute_animated_image (MagickWand
*super_wand
, int ino
)
8028 MagickWand
*composite_wand
;
8029 size_t dest_width
, dest_height
;
8030 struct animation_cache
*cache
= imagemagick_get_animation_cache (super_wand
);
8032 MagickSetIteratorIndex (super_wand
, 0);
8034 if (ino
== 0 || cache
->wand
== NULL
|| cache
->index
> ino
)
8036 composite_wand
= MagickGetImage (super_wand
);
8038 DestroyMagickWand (cache
->wand
);
8041 composite_wand
= cache
->wand
;
8043 dest_width
= MagickGetImageWidth (composite_wand
);
8044 dest_height
= MagickGetImageHeight (composite_wand
);
8046 for (i
= max (1, cache
->index
+ 1); i
<= ino
; i
++)
8048 MagickWand
*sub_wand
;
8049 PixelIterator
*source_iterator
, *dest_iterator
;
8050 PixelWand
**source
, **dest
;
8051 size_t source_width
, source_height
;
8052 ssize_t source_left
, source_top
;
8053 MagickPixelPacket pixel
;
8054 DisposeType dispose
;
8055 ptrdiff_t lines
= 0;
8057 MagickSetIteratorIndex (super_wand
, i
);
8058 sub_wand
= MagickGetImage (super_wand
);
8060 MagickGetImagePage (sub_wand
, &source_width
, &source_height
,
8061 &source_left
, &source_top
);
8063 /* This flag says how to handle transparent pixels. */
8064 dispose
= MagickGetImageDispose (sub_wand
);
8066 source_iterator
= NewPixelIterator (sub_wand
);
8067 if (! source_iterator
)
8069 DestroyMagickWand (composite_wand
);
8070 DestroyMagickWand (sub_wand
);
8072 image_error ("Imagemagick pixel iterator creation failed",
8077 dest_iterator
= NewPixelIterator (composite_wand
);
8078 if (! dest_iterator
)
8080 DestroyMagickWand (composite_wand
);
8081 DestroyMagickWand (sub_wand
);
8082 DestroyPixelIterator (source_iterator
);
8084 image_error ("Imagemagick pixel iterator creation failed",
8089 /* The sub-image may not start at origin, so move the destination
8090 iterator to where the sub-image should start. */
8093 PixelSetIteratorRow (dest_iterator
, source_top
);
8097 while ((source
= PixelGetNextIteratorRow (source_iterator
, &source_width
))
8102 /* Sanity check. This shouldn't happen, but apparently
8103 does in some pictures. */
8104 if (++lines
>= dest_height
)
8107 dest
= PixelGetNextIteratorRow (dest_iterator
, &dest_width
);
8108 for (x
= 0; x
< source_width
; x
++)
8110 /* Sanity check. This shouldn't happen, but apparently
8111 also does in some pictures. */
8112 if (x
+ source_left
> dest_width
- 1)
8114 /* Normally we only copy over non-transparent pixels,
8115 but if the disposal method is "Background", then we
8116 copy over all pixels. */
8117 if (dispose
== BackgroundDispose
||
8118 PixelGetAlpha (source
[x
]))
8120 PixelGetMagickColor (source
[x
], &pixel
);
8121 PixelSetMagickColor (dest
[x
+ source_left
], &pixel
);
8124 PixelSyncIterator (dest_iterator
);
8127 DestroyPixelIterator (source_iterator
);
8128 DestroyPixelIterator (dest_iterator
);
8129 DestroyMagickWand (sub_wand
);
8132 /* Cache a copy for the next iteration. The current wand will be
8133 destroyed by the caller. */
8134 cache
->wand
= CloneMagickWand (composite_wand
);
8137 return composite_wand
;
8141 /* Helper function for imagemagick_load, which does the actual loading
8142 given contents and size, apart from frame and image structures,
8143 passed from imagemagick_load. Uses librimagemagick to do most of
8144 the image processing.
8146 F is a pointer to the Emacs frame; IMG to the image structure to
8147 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
8148 be parsed; SIZE is the number of bytes of data; and FILENAME is
8149 either the file name or the image data.
8151 Return true if successful. */
8154 imagemagick_load_image (struct frame
*f
, struct image
*img
,
8155 unsigned char *contents
, unsigned int size
,
8158 size_t width
, height
;
8159 MagickBooleanType status
;
8162 MagickWand
*image_wand
;
8163 PixelIterator
*iterator
;
8164 PixelWand
**pixels
, *bg_wand
= NULL
;
8165 MagickPixelPacket pixel
;
8170 int desired_width
, desired_height
;
8173 char hint_buffer
[MaxTextExtent
];
8174 char *filename_hint
= NULL
;
8176 /* Handle image index for image types who can contain more than one image.
8177 Interface :index is same as for GIF. First we "ping" the image to see how
8178 many sub-images it contains. Pinging is faster than loading the image to
8179 find out things about it. */
8181 /* Initialize the imagemagick environment. */
8182 MagickWandGenesis ();
8183 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
8184 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
8185 image_wand
= NewMagickWand ();
8188 status
= MagickReadImage (image_wand
, filename
);
8191 filename_hint
= imagemagick_filename_hint (img
->spec
, hint_buffer
);
8192 MagickSetFilename (image_wand
, filename_hint
);
8193 status
= MagickReadImageBlob (image_wand
, contents
, size
);
8196 if (status
== MagickFalse
)
8198 imagemagick_error (image_wand
);
8199 DestroyMagickWand (image_wand
);
8203 if (ino
< 0 || ino
>= MagickGetNumberImages (image_wand
))
8205 image_error ("Invalid image number `%s' in image `%s'",
8207 DestroyMagickWand (image_wand
);
8211 if (MagickGetNumberImages (image_wand
) > 1)
8214 Fcons (make_number (MagickGetNumberImages (image_wand
)),
8217 /* If we have an animated image, get the new wand based on the
8219 if (MagickGetNumberImages (image_wand
) > 1)
8221 MagickWand
*super_wand
= image_wand
;
8222 image_wand
= imagemagick_compute_animated_image (super_wand
, ino
);
8224 image_wand
= super_wand
;
8226 DestroyMagickWand (super_wand
);
8229 /* Retrieve the frame's background color, for use later. */
8232 Lisp_Object specified_bg
;
8234 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8235 if (!STRINGP (specified_bg
)
8236 || !x_defined_color (f
, SSDATA (specified_bg
), &bgcolor
, 0))
8239 bgcolor
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8240 x_query_color (f
, &bgcolor
);
8242 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &bgcolor
, 1);
8246 bg_wand
= NewPixelWand ();
8247 PixelSetRed (bg_wand
, (double) bgcolor
.red
/ 65535);
8248 PixelSetGreen (bg_wand
, (double) bgcolor
.green
/ 65535);
8249 PixelSetBlue (bg_wand
, (double) bgcolor
.blue
/ 65535);
8252 compute_image_size (MagickGetImageWidth (image_wand
),
8253 MagickGetImageHeight (image_wand
),
8254 img
->spec
, &desired_width
, &desired_height
);
8256 if (desired_width
!= -1 && desired_height
!= -1)
8258 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
8259 if (status
== MagickFalse
)
8261 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
8262 imagemagick_error (image_wand
);
8263 goto imagemagick_error
;
8267 /* crop behaves similar to image slicing in Emacs but is more memory
8269 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
8271 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
8273 /* After some testing, it seems MagickCropImage is the fastest crop
8274 function in ImageMagick. This crop function seems to do less copying
8275 than the alternatives, but it still reads the entire image into memory
8276 before cropping, which is apparently difficult to avoid when using
8278 size_t crop_width
= XINT (XCAR (crop
));
8280 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
8282 size_t crop_height
= XINT (XCAR (crop
));
8284 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
8286 ssize_t crop_x
= XINT (XCAR (crop
));
8288 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
8290 ssize_t crop_y
= XINT (XCAR (crop
));
8291 MagickCropImage (image_wand
, crop_width
, crop_height
,
8298 /* Furthermore :rotation. we need background color and angle for
8301 TODO background handling for rotation specified_bg =
8302 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
8304 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
8307 rotation
= extract_float (value
);
8308 status
= MagickRotateImage (image_wand
, bg_wand
, rotation
);
8309 if (status
== MagickFalse
)
8311 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
8312 imagemagick_error (image_wand
);
8313 goto imagemagick_error
;
8317 /* Set the canvas background color to the frame or specified
8318 background, and flatten the image. Note: as of ImageMagick
8319 6.6.0, SVG image transparency is not handled properly
8320 (e.g. etc/images/splash.svg shows a white background always). */
8322 MagickWand
*new_wand
;
8323 MagickSetImageBackgroundColor (image_wand
, bg_wand
);
8324 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
8325 new_wand
= MagickMergeImageLayers (image_wand
, MergeLayer
);
8327 new_wand
= MagickFlattenImages (image_wand
);
8329 DestroyMagickWand (image_wand
);
8330 image_wand
= new_wand
;
8333 /* Finally we are done manipulating the image. Figure out the
8334 resulting width/height and transfer ownership to Emacs. */
8335 height
= MagickGetImageHeight (image_wand
);
8336 width
= MagickGetImageWidth (image_wand
);
8338 if (! (width
<= INT_MAX
&& height
<= INT_MAX
8339 && check_image_size (f
, width
, height
)))
8341 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8342 goto imagemagick_error
;
8345 /* We can now get a valid pixel buffer from the imagemagick file, if all
8348 init_color_table ();
8350 #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS)
8351 if (imagemagick_render_type
!= 0)
8353 /* Magicexportimage is normally faster than pixelpushing. This
8354 method is also well tested. Some aspects of this method are
8355 ad-hoc and needs to be more researched. */
8356 int imagedepth
= 24; /*MagickGetImageDepth(image_wand);*/
8357 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP"; /*"RGBP";*/
8358 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8359 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, imagedepth
,
8362 #ifdef COLOR_TABLE_SUPPORT
8363 free_color_table ();
8365 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
8366 goto imagemagick_error
;
8369 /* Oddly, the below code doesn't seem to work:*/
8370 /* switch(ximg->bitmap_unit){ */
8372 /* pixelwidth=CharPixel; */
8375 /* pixelwidth=ShortPixel; */
8378 /* pixelwidth=LongPixel; */
8382 Here im just guessing the format of the bitmap.
8383 happens to work fine for:
8386 seems about 3 times as fast as pixel pushing(not carefully measured)
8388 pixelwidth
= CharPixel
; /*??? TODO figure out*/
8389 MagickExportImagePixels (image_wand
, 0, 0, width
, height
,
8390 exportdepth
, pixelwidth
, ximg
->data
);
8393 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
8395 size_t image_height
;
8397 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8398 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0,
8401 #ifdef COLOR_TABLE_SUPPORT
8402 free_color_table ();
8404 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
8405 goto imagemagick_error
;
8408 /* Copy imagemagick image to x with primitive yet robust pixel
8409 pusher loop. This has been tested a lot with many different
8412 /* Copy pixels from the imagemagick image structure to the x image map. */
8413 iterator
= NewPixelIterator (image_wand
);
8416 #ifdef COLOR_TABLE_SUPPORT
8417 free_color_table ();
8419 x_destroy_x_image (ximg
);
8420 image_error ("Imagemagick pixel iterator creation failed",
8422 goto imagemagick_error
;
8425 image_height
= MagickGetImageHeight (image_wand
);
8426 for (y
= 0; y
< image_height
; y
++)
8428 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
8431 for (x
= 0; x
< (long) width
; x
++)
8433 PixelGetMagickColor (pixels
[x
], &pixel
);
8434 XPutPixel (ximg
, x
, y
,
8435 lookup_rgb_color (f
,
8441 DestroyPixelIterator (iterator
);
8444 #ifdef COLOR_TABLE_SUPPORT
8445 /* Remember colors allocated for this image. */
8446 img
->colors
= colors_in_color_table (&img
->ncolors
);
8447 free_color_table ();
8448 #endif /* COLOR_TABLE_SUPPORT */
8451 img
->height
= height
;
8453 /* Put ximg into the image. */
8454 image_put_x_image (f
, img
, ximg
, 0);
8456 /* Final cleanup. image_wand should be the only resource left. */
8457 DestroyMagickWand (image_wand
);
8458 if (bg_wand
) DestroyPixelWand (bg_wand
);
8460 /* `MagickWandTerminus' terminates the imagemagick environment. */
8461 MagickWandTerminus ();
8466 DestroyMagickWand (image_wand
);
8467 if (bg_wand
) DestroyPixelWand (bg_wand
);
8469 MagickWandTerminus ();
8470 /* TODO more cleanup. */
8471 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
8476 /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if
8477 successful. this function will go into the imagemagick_type structure, and
8478 the prototype thus needs to be compatible with that structure. */
8481 imagemagick_load (struct frame
*f
, struct image
*img
)
8484 Lisp_Object file_name
;
8486 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8487 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8488 if (STRINGP (file_name
))
8492 file
= x_find_image_file (file_name
);
8493 if (!STRINGP (file
))
8495 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8499 file
= ansi_encode_filename (file
);
8501 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
8503 /* Else its not a file, its a lisp object. Load the image from a
8504 lisp object rather than a file. */
8509 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8510 if (!STRINGP (data
))
8512 image_error ("Invalid image data `%s'", data
, Qnil
);
8515 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
8516 SBYTES (data
), NULL
);
8522 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
8523 doc
: /* Return a list of image types supported by ImageMagick.
8524 Each entry in this list is a symbol named after an ImageMagick format
8525 tag. See the ImageMagick manual for a list of ImageMagick formats and
8526 their descriptions (http://www.imagemagick.org/script/formats.php).
8527 You can also try the shell command: `identify -list format'.
8529 Note that ImageMagick recognizes many file-types that Emacs does not
8530 recognize as images, such as C. See `imagemagick-types-enable'
8531 and `imagemagick-types-inhibit'. */)
8534 Lisp_Object typelist
= Qnil
;
8539 Lisp_Object Qimagemagicktype
;
8541 GetExceptionInfo(&ex
);
8542 imtypes
= GetMagickList ("*", &numf
, &ex
);
8543 DestroyExceptionInfo(&ex
);
8545 for (i
= 0; i
< numf
; i
++)
8547 Qimagemagicktype
= intern (imtypes
[i
]);
8548 typelist
= Fcons (Qimagemagicktype
, typelist
);
8549 imtypes
[i
] = MagickRelinquishMemory (imtypes
[i
]);
8552 MagickRelinquishMemory (imtypes
);
8553 return Fnreverse (typelist
);
8556 #endif /* defined (HAVE_IMAGEMAGICK) */
8560 /***********************************************************************
8562 ***********************************************************************/
8564 #if defined (HAVE_RSVG)
8566 /* Function prototypes. */
8568 static bool svg_image_p (Lisp_Object object
);
8569 static bool svg_load (struct frame
*f
, struct image
*img
);
8571 static bool svg_load_image (struct frame
*, struct image
*,
8572 unsigned char *, ptrdiff_t);
8574 /* The symbol `svg' identifying images of this type. */
8576 static Lisp_Object Qsvg
;
8578 /* Indices of image specification fields in svg_format, below. */
8580 enum svg_keyword_index
8595 /* Vector of image_keyword structures describing the format
8596 of valid user-defined image specifications. */
8598 static const struct image_keyword svg_format
[SVG_LAST
] =
8600 {":type", IMAGE_SYMBOL_VALUE
, 1},
8601 {":data", IMAGE_STRING_VALUE
, 0},
8602 {":file", IMAGE_STRING_VALUE
, 0},
8603 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8604 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8605 {":relief", IMAGE_INTEGER_VALUE
, 0},
8606 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8607 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8608 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8609 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8612 #if defined HAVE_NTGUI && defined WINDOWSNT
8613 static bool init_svg_functions (void);
8615 #define init_svg_functions NULL
8618 /* Structure describing the image type `svg'. Its the same type of
8619 structure defined for all image formats, handled by emacs image
8620 functions. See struct image_type in dispextern.h. */
8622 static struct image_type svg_type
=
8633 /* Return true if OBJECT is a valid SVG image specification. Do
8634 this by calling parse_image_spec and supplying the keywords that
8635 identify the SVG format. */
8638 svg_image_p (Lisp_Object object
)
8640 struct image_keyword fmt
[SVG_LAST
];
8641 memcpy (fmt
, svg_format
, sizeof fmt
);
8643 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8646 /* Must specify either the :data or :file keyword. */
8647 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8650 #include <librsvg/rsvg.h>
8654 /* SVG library functions. */
8655 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
, (void));
8656 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
, (RsvgHandle
*, RsvgDimensionData
*));
8657 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
, (RsvgHandle
*, const guchar
*, gsize
, GError
**));
8658 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
, (RsvgHandle
*, GError
**));
8659 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
, (RsvgHandle
*));
8660 DEF_IMGLIB_FN (void *, rsvg_handle_set_size_callback
, (RsvgHandle
*, RsvgSizeFunc
, gpointer
, GDestroyNotify
));
8662 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
, (const GdkPixbuf
*));
8663 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
, (const GdkPixbuf
*));
8664 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
, (const GdkPixbuf
*));
8665 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
, (const GdkPixbuf
*));
8666 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
, (const GdkPixbuf
*));
8667 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
, (const GdkPixbuf
*));
8668 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
, (const GdkPixbuf
*));
8669 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
, (const GdkPixbuf
*));
8671 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8672 DEF_IMGLIB_FN (void, g_type_init
, (void));
8674 DEF_IMGLIB_FN (void, g_object_unref
, (gpointer
));
8675 DEF_IMGLIB_FN (void, g_error_free
, (GError
*));
8677 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8680 init_svg_functions (void)
8682 HMODULE library
, gdklib
, glib
, gobject
;
8684 if (!(glib
= w32_delayed_load (Qglib
))
8685 || !(gobject
= w32_delayed_load (Qgobject
))
8686 || !(gdklib
= w32_delayed_load (Qgdk_pixbuf
))
8687 || !(library
= w32_delayed_load (Qsvg
)))
8690 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8691 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8692 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8693 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8694 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8696 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8697 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8698 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8699 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8700 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8701 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8702 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8703 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8705 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8706 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8708 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8709 LOAD_IMGLIB_FN (glib
, g_error_free
);
8715 /* The following aliases for library functions allow dynamic loading
8716 to be used on some platforms. */
8717 #define fn_rsvg_handle_new rsvg_handle_new
8718 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8719 #define fn_rsvg_handle_write rsvg_handle_write
8720 #define fn_rsvg_handle_close rsvg_handle_close
8721 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8723 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8724 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8725 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8726 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8727 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8728 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8729 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8730 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8732 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8733 #define fn_g_type_init g_type_init
8735 #define fn_g_object_unref g_object_unref
8736 #define fn_g_error_free g_error_free
8737 #endif /* !WINDOWSNT */
8739 /* Load SVG image IMG for use on frame F. Value is true if
8743 svg_load (struct frame
*f
, struct image
*img
)
8746 Lisp_Object file_name
;
8748 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8749 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8750 if (STRINGP (file_name
))
8753 unsigned char *contents
;
8756 file
= x_find_image_file (file_name
);
8757 if (!STRINGP (file
))
8759 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8763 /* Read the entire file into memory. */
8764 contents
= slurp_file (SSDATA (file
), &size
);
8765 if (contents
== NULL
)
8767 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8770 /* If the file was slurped into memory properly, parse it. */
8771 success_p
= svg_load_image (f
, img
, contents
, size
);
8774 /* Else its not a file, its a lisp object. Load the image from a
8775 lisp object rather than a file. */
8780 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8781 if (!STRINGP (data
))
8783 image_error ("Invalid image data `%s'", data
, Qnil
);
8786 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8792 /* svg_load_image is a helper function for svg_load, which does the
8793 actual loading given contents and size, apart from frame and image
8794 structures, passed from svg_load.
8796 Uses librsvg to do most of the image processing.
8798 Returns true when successful. */
8800 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8801 struct image
*img
, /* Pointer to emacs image structure. */
8802 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8803 ptrdiff_t size
) /* Size of data in bytes. */
8805 RsvgHandle
*rsvg_handle
;
8806 RsvgDimensionData dimension_data
;
8811 const guint8
*pixels
;
8814 Lisp_Object specified_bg
;
8819 #if ! GLIB_CHECK_VERSION (2, 36, 0)
8820 /* g_type_init is a glib function that must be called prior to
8821 using gnome type library functions (obsolete since 2.36.0). */
8825 /* Make a handle to a new rsvg object. */
8826 rsvg_handle
= fn_rsvg_handle_new ();
8828 /* Parse the contents argument and fill in the rsvg_handle. */
8829 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8830 if (err
) goto rsvg_error
;
8832 /* The parsing is complete, rsvg_handle is ready to used, close it
8833 for further writes. */
8834 fn_rsvg_handle_close (rsvg_handle
, &err
);
8835 if (err
) goto rsvg_error
;
8837 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8838 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8840 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8844 /* We can now get a valid pixel buffer from the svg file, if all
8846 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8847 if (!pixbuf
) goto rsvg_error
;
8848 fn_g_object_unref (rsvg_handle
);
8850 /* Extract some meta data from the svg handle. */
8851 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8852 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8853 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8854 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8856 /* Validate the svg meta data. */
8857 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8858 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8859 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8860 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8862 /* Try to create a x pixmap to hold the svg pixmap. */
8863 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
8865 fn_g_object_unref (pixbuf
);
8869 init_color_table ();
8871 /* Handle alpha channel by combining the image with a background
8873 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8874 if (!STRINGP (specified_bg
)
8875 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8878 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8879 x_query_color (f
, &background
);
8881 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8885 /* SVG pixmaps specify transparency in the last byte, so right
8886 shift 8 bits to get rid of it, since emacs doesn't support
8888 background
.red
>>= 8;
8889 background
.green
>>= 8;
8890 background
.blue
>>= 8;
8892 /* This loop handles opacity values, since Emacs assumes
8893 non-transparent images. Each pixel must be "flattened" by
8894 calculating the resulting color, given the transparency of the
8895 pixel, and the image background color. */
8896 for (y
= 0; y
< height
; ++y
)
8898 for (x
= 0; x
< width
; ++x
)
8908 opacity
= *pixels
++;
8910 red
= ((red
* opacity
)
8911 + (background
.red
* ((1 << 8) - opacity
)));
8912 green
= ((green
* opacity
)
8913 + (background
.green
* ((1 << 8) - opacity
)));
8914 blue
= ((blue
* opacity
)
8915 + (background
.blue
* ((1 << 8) - opacity
)));
8917 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8920 pixels
+= rowstride
- 4 * width
;
8923 #ifdef COLOR_TABLE_SUPPORT
8924 /* Remember colors allocated for this image. */
8925 img
->colors
= colors_in_color_table (&img
->ncolors
);
8926 free_color_table ();
8927 #endif /* COLOR_TABLE_SUPPORT */
8929 fn_g_object_unref (pixbuf
);
8932 img
->height
= height
;
8934 /* Maybe fill in the background field while we have ximg handy.
8935 Casting avoids a GCC warning. */
8936 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8938 /* Put ximg into the image. */
8939 image_put_x_image (f
, img
, ximg
, 0);
8944 fn_g_object_unref (rsvg_handle
);
8945 /* FIXME: Use error->message so the user knows what is the actual
8946 problem with the image. */
8947 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8948 fn_g_error_free (err
);
8952 #endif /* defined (HAVE_RSVG) */
8957 /***********************************************************************
8959 ***********************************************************************/
8961 #ifdef HAVE_X_WINDOWS
8962 #define HAVE_GHOSTSCRIPT 1
8963 #endif /* HAVE_X_WINDOWS */
8965 #ifdef HAVE_GHOSTSCRIPT
8967 static bool gs_image_p (Lisp_Object object
);
8968 static bool gs_load (struct frame
*f
, struct image
*img
);
8969 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8971 /* Keyword symbols. */
8973 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8975 /* Indices of image specification fields in gs_format, below. */
8977 enum gs_keyword_index
8995 /* Vector of image_keyword structures describing the format
8996 of valid user-defined image specifications. */
8998 static const struct image_keyword gs_format
[GS_LAST
] =
9000 {":type", IMAGE_SYMBOL_VALUE
, 1},
9001 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
9002 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
9003 {":file", IMAGE_STRING_VALUE
, 1},
9004 {":loader", IMAGE_FUNCTION_VALUE
, 0},
9005 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
9006 {":ascent", IMAGE_ASCENT_VALUE
, 0},
9007 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
9008 {":relief", IMAGE_INTEGER_VALUE
, 0},
9009 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9010 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9011 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9012 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
9015 /* Structure describing the image type `ghostscript'. */
9017 static struct image_type gs_type
=
9028 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9031 gs_clear_image (struct frame
*f
, struct image
*img
)
9033 x_clear_image (f
, img
);
9037 /* Return true if OBJECT is a valid Ghostscript image
9041 gs_image_p (Lisp_Object object
)
9043 struct image_keyword fmt
[GS_LAST
];
9047 memcpy (fmt
, gs_format
, sizeof fmt
);
9049 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
9052 /* Bounding box must be a list or vector containing 4 integers. */
9053 tem
= fmt
[GS_BOUNDING_BOX
].value
;
9056 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
9057 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
9062 else if (VECTORP (tem
))
9064 if (ASIZE (tem
) != 4)
9066 for (i
= 0; i
< 4; ++i
)
9067 if (!INTEGERP (AREF (tem
, i
)))
9077 /* Load Ghostscript image IMG for use on frame F. Value is true
9081 gs_load (struct frame
*f
, struct image
*img
)
9083 uprintmax_t printnum1
, printnum2
;
9084 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
9085 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
9087 double in_width
, in_height
;
9088 Lisp_Object pixel_colors
= Qnil
;
9090 /* Compute pixel size of pixmap needed from the given size in the
9091 image specification. Sizes in the specification are in pt. 1 pt
9092 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9094 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
9095 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
9096 in_width
*= FRAME_RES_X (f
);
9097 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
9098 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
9099 in_height
*= FRAME_RES_Y (f
);
9101 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
9102 && check_image_size (f
, in_width
, in_height
)))
9104 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
9107 img
->width
= in_width
;
9108 img
->height
= in_height
;
9110 /* Create the pixmap. */
9111 eassert (img
->pixmap
== NO_PIXMAP
);
9113 if (x_check_image_size (0, img
->width
, img
->height
))
9115 /* Only W32 version did BLOCK_INPUT here. ++kfs */
9117 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
9118 img
->width
, img
->height
,
9119 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
9125 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
9129 /* Call the loader to fill the pixmap. It returns a process object
9130 if successful. We do not record_unwind_protect here because
9131 other places in redisplay like calling window scroll functions
9132 don't either. Let the Lisp loader use `unwind-protect' instead. */
9133 printnum1
= FRAME_X_WINDOW (f
);
9134 printnum2
= img
->pixmap
;
9135 window_and_pixmap_id
9136 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
9138 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
9139 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
9141 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
9143 XSETFRAME (frame
, f
);
9144 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
9146 loader
= intern ("gs-load-image");
9148 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
9149 make_number (img
->width
),
9150 make_number (img
->height
),
9151 window_and_pixmap_id
,
9153 return PROCESSP (img
->lisp_data
);
9157 /* Kill the Ghostscript process that was started to fill PIXMAP on
9158 frame F. Called from XTread_socket when receiving an event
9159 telling Emacs that Ghostscript has finished drawing. */
9162 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
9164 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
9169 /* Find the image containing PIXMAP. */
9170 for (i
= 0; i
< c
->used
; ++i
)
9171 if (c
->images
[i
]->pixmap
== pixmap
)
9174 /* Should someone in between have cleared the image cache, for
9175 instance, give up. */
9179 /* Kill the GS process. We should have found PIXMAP in the image
9180 cache and its image should contain a process object. */
9182 eassert (PROCESSP (img
->lisp_data
));
9183 Fkill_process (img
->lisp_data
, Qnil
);
9184 img
->lisp_data
= Qnil
;
9186 #if defined (HAVE_X_WINDOWS)
9188 /* On displays with a mutable colormap, figure out the colors
9189 allocated for the image by looking at the pixels of an XImage for
9191 class = FRAME_X_VISUAL (f
)->class;
9192 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
9198 /* Try to get an XImage for img->pixmep. */
9199 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
9200 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
9205 /* Initialize the color table. */
9206 init_color_table ();
9208 /* For each pixel of the image, look its color up in the
9209 color table. After having done so, the color table will
9210 contain an entry for each color used by the image. */
9211 for (y
= 0; y
< img
->height
; ++y
)
9212 for (x
= 0; x
< img
->width
; ++x
)
9214 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
9215 lookup_pixel_color (f
, pixel
);
9218 /* Record colors in the image. Free color table and XImage. */
9219 #ifdef COLOR_TABLE_SUPPORT
9220 img
->colors
= colors_in_color_table (&img
->ncolors
);
9221 free_color_table ();
9223 XDestroyImage (ximg
);
9225 #if 0 /* This doesn't seem to be the case. If we free the colors
9226 here, we get a BadAccess later in x_clear_image when
9227 freeing the colors. */
9228 /* We have allocated colors once, but Ghostscript has also
9229 allocated colors on behalf of us. So, to get the
9230 reference counts right, free them once. */
9232 x_free_colors (f
, img
->colors
, img
->ncolors
);
9236 image_error ("Cannot get X image of `%s'; colors will not be freed",
9241 #endif /* HAVE_X_WINDOWS */
9243 /* Now that we have the pixmap, compute mask and transform the
9244 image if requested. */
9246 postprocess_image (f
, img
);
9250 #endif /* HAVE_GHOSTSCRIPT */
9253 /***********************************************************************
9255 ***********************************************************************/
9259 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
9260 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
9263 return valid_image_p (spec
) ? Qt
: Qnil
;
9267 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
9272 if (valid_image_p (spec
))
9273 id
= lookup_image (SELECTED_FRAME (), spec
);
9276 return make_number (id
);
9279 #endif /* GLYPH_DEBUG */
9282 /***********************************************************************
9284 ***********************************************************************/
9286 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 1, 1, 0,
9287 doc
: /* Initialize image library implementing image type TYPE.
9288 Return non-nil if TYPE is a supported image type.
9290 If image libraries are loaded dynamically (currently only the case on
9291 MS-Windows), load the library for TYPE if it is not yet loaded, using
9292 the library file(s) specified by `dynamic-library-alist'. */)
9295 return lookup_image_type (type
) ? Qt
: Qnil
;
9298 /* Look up image type TYPE, and return a pointer to its image_type
9299 structure. Return 0 if TYPE is not a known image type. */
9301 static struct image_type
*
9302 lookup_image_type (Lisp_Object type
)
9304 /* Types pbm and xbm are built-in and always available. */
9305 if (EQ (type
, Qpbm
))
9306 return define_image_type (&pbm_type
);
9308 if (EQ (type
, Qxbm
))
9309 return define_image_type (&xbm_type
);
9311 #if defined (HAVE_XPM) || defined (HAVE_NS)
9312 if (EQ (type
, Qxpm
))
9313 return define_image_type (&xpm_type
);
9316 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9317 if (EQ (type
, Qjpeg
))
9318 return define_image_type (&jpeg_type
);
9321 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9322 if (EQ (type
, Qtiff
))
9323 return define_image_type (&tiff_type
);
9326 #if defined (HAVE_GIF) || defined (HAVE_NS)
9327 if (EQ (type
, Qgif
))
9328 return define_image_type (&gif_type
);
9331 #if defined (HAVE_PNG) || defined (HAVE_NS)
9332 if (EQ (type
, Qpng
))
9333 return define_image_type (&png_type
);
9336 #if defined (HAVE_RSVG)
9337 if (EQ (type
, Qsvg
))
9338 return define_image_type (&svg_type
);
9341 #if defined (HAVE_IMAGEMAGICK)
9342 if (EQ (type
, Qimagemagick
))
9343 return define_image_type (&imagemagick_type
);
9346 #ifdef HAVE_GHOSTSCRIPT
9347 if (EQ (type
, Qpostscript
))
9348 return define_image_type (&gs_type
);
9354 /* Reset image_types before dumping.
9355 Called from Fdump_emacs. */
9358 reset_image_types (void)
9362 struct image_type
*next
= image_types
->next
;
9363 xfree (image_types
);
9369 syms_of_image (void)
9371 /* Initialize this only once; it will be reset before dumping. */
9374 /* Must be defined now because we're going to update it below, while
9375 defining the supported image types. */
9376 DEFVAR_LISP ("image-types", Vimage_types
,
9377 doc
: /* List of potentially supported image types.
9378 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
9379 To check whether it is really supported, use `image-type-available-p'. */);
9380 Vimage_types
= Qnil
;
9382 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
9383 doc
: /* Maximum size of images.
9384 Emacs will not load an image into memory if its pixel width or
9385 pixel height exceeds this limit.
9387 If the value is an integer, it directly specifies the maximum
9388 image height and width, measured in pixels. If it is a floating
9389 point number, it specifies the maximum image height and width
9390 as a ratio to the frame height and width. If the value is
9391 non-numeric, there is no explicit limit on the size of images. */);
9392 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
9394 DEFSYM (Qcount
, "count");
9395 DEFSYM (Qextension_data
, "extension-data");
9396 DEFSYM (Qdelay
, "delay");
9398 DEFSYM (QCascent
, ":ascent");
9399 DEFSYM (QCmargin
, ":margin");
9400 DEFSYM (QCrelief
, ":relief");
9401 DEFSYM (QCconversion
, ":conversion");
9402 DEFSYM (QCcolor_symbols
, ":color-symbols");
9403 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
9404 DEFSYM (QCindex
, ":index");
9405 DEFSYM (QCgeometry
, ":geometry");
9406 DEFSYM (QCcrop
, ":crop");
9407 DEFSYM (QCrotation
, ":rotation");
9408 DEFSYM (QCmatrix
, ":matrix");
9409 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
9410 DEFSYM (QCmask
, ":mask");
9412 DEFSYM (Qlaplace
, "laplace");
9413 DEFSYM (Qemboss
, "emboss");
9414 DEFSYM (Qedge_detection
, "edge-detection");
9415 DEFSYM (Qheuristic
, "heuristic");
9417 DEFSYM (Qpostscript
, "postscript");
9418 DEFSYM (QCmax_width
, ":max-width");
9419 DEFSYM (QCmax_height
, ":max-height");
9420 #ifdef HAVE_GHOSTSCRIPT
9421 ADD_IMAGE_TYPE (Qpostscript
);
9422 DEFSYM (QCloader
, ":loader");
9423 DEFSYM (QCbounding_box
, ":bounding-box");
9424 DEFSYM (QCpt_width
, ":pt-width");
9425 DEFSYM (QCpt_height
, ":pt-height");
9426 #endif /* HAVE_GHOSTSCRIPT */
9429 DEFSYM (Qlibpng_version
, "libpng-version");
9430 Fset (Qlibpng_version
,
9432 make_number (PNG_LIBPNG_VER
)
9437 DEFSYM (Qlibgif_version
, "libgif-version");
9438 Fset (Qlibgif_version
,
9440 make_number (GIFLIB_MAJOR
* 10000
9441 + GIFLIB_MINOR
* 100
9447 DEFSYM (Qlibjpeg_version
, "libjpeg-version");
9448 Fset (Qlibjpeg_version
,
9450 make_number (JPEG_LIB_VERSION
)
9457 DEFSYM (Qpbm
, "pbm");
9458 ADD_IMAGE_TYPE (Qpbm
);
9460 DEFSYM (Qxbm
, "xbm");
9461 ADD_IMAGE_TYPE (Qxbm
);
9463 #if defined (HAVE_XPM) || defined (HAVE_NS)
9464 DEFSYM (Qxpm
, "xpm");
9465 ADD_IMAGE_TYPE (Qxpm
);
9468 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9469 DEFSYM (Qjpeg
, "jpeg");
9470 ADD_IMAGE_TYPE (Qjpeg
);
9473 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9474 DEFSYM (Qtiff
, "tiff");
9475 ADD_IMAGE_TYPE (Qtiff
);
9478 #if defined (HAVE_GIF) || defined (HAVE_NS)
9479 DEFSYM (Qgif
, "gif");
9480 ADD_IMAGE_TYPE (Qgif
);
9483 #if defined (HAVE_PNG) || defined (HAVE_NS)
9484 DEFSYM (Qpng
, "png");
9485 ADD_IMAGE_TYPE (Qpng
);
9488 #if defined (HAVE_IMAGEMAGICK)
9489 DEFSYM (Qimagemagick
, "imagemagick");
9490 ADD_IMAGE_TYPE (Qimagemagick
);
9493 #if defined (HAVE_RSVG)
9494 DEFSYM (Qsvg
, "svg");
9495 ADD_IMAGE_TYPE (Qsvg
);
9497 /* Other libraries used directly by svg code. */
9498 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
9499 DEFSYM (Qglib
, "glib");
9500 DEFSYM (Qgobject
, "gobject");
9501 #endif /* HAVE_NTGUI */
9502 #endif /* HAVE_RSVG */
9504 defsubr (&Sinit_image_library
);
9505 #ifdef HAVE_IMAGEMAGICK
9506 defsubr (&Simagemagick_types
);
9508 defsubr (&Sclear_image_cache
);
9509 defsubr (&Simage_flush
);
9510 defsubr (&Simage_size
);
9511 defsubr (&Simage_mask_p
);
9512 defsubr (&Simage_metadata
);
9516 defsubr (&Slookup_image
);
9519 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
9520 doc
: /* Non-nil means always draw a cross over disabled images.
9521 Disabled images are those having a `:conversion disabled' property.
9522 A cross is always drawn on black & white displays. */);
9523 cross_disabled_images
= 0;
9525 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
9526 doc
: /* List of directories to search for window system bitmap files. */);
9527 Vx_bitmap_file_path
= decode_env_path (0, PATH_BITMAPS
, 0);
9529 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
9530 doc
: /* Maximum time after which images are removed from the cache.
9531 When an image has not been displayed this many seconds, Emacs
9532 automatically removes it from the image cache. If the cache contains
9533 a large number of images, the actual eviction time may be shorter.
9534 The value can also be nil, meaning the cache is never cleared.
9536 The function `clear-image-cache' disregards this variable. */);
9537 Vimage_cache_eviction_delay
= make_number (300);
9538 #ifdef HAVE_IMAGEMAGICK
9539 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type
,
9540 doc
: /* Integer indicating which ImageMagick rendering method to use.
9542 0 -- the default method (pixel pushing)
9543 1 -- a newer method ("MagickExportImagePixels") that may perform
9544 better (speed etc) in some cases, but has not been as thoroughly
9545 tested with Emacs as the default method. This method requires
9546 ImageMagick version 6.4.6 (approximately) or later.
9548 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9549 imagemagick_render_type
= 0;