1 /* Functions for image support on window system.
3 Copyright (C) 1989, 1992-2013 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #if defined HAVE_LIBPNG_PNG_H
26 # include <libpng/png.h>
36 /* This makes the fields of a Display accessible, in Xlib header files. */
38 #define XLIB_ILLEGAL_ACCESS
43 #include "dispextern.h"
44 #include "blockinput.h"
47 #include "character.h"
49 #include "termhooks.h"
52 #ifdef HAVE_SYS_STAT_H
54 #endif /* HAVE_SYS_STAT_H */
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif /* HAVE_SYS_TYPES_H */
60 #ifdef HAVE_WINDOW_SYSTEM
62 #endif /* HAVE_WINDOW_SYSTEM */
65 #define COLOR_TABLE_SUPPORT 1
67 typedef struct x_bitmap_record Bitmap_Record
;
68 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
69 #define NO_PIXMAP None
71 #define PIX_MASK_RETAIN 0
72 #define PIX_MASK_DRAW 1
73 #endif /* HAVE_X_WINDOWS */
77 /* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */
82 /* W32_TODO : Color tables on W32. */
83 #undef COLOR_TABLE_SUPPORT
85 typedef struct w32_bitmap_record Bitmap_Record
;
86 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
89 #define PIX_MASK_RETAIN 0
90 #define PIX_MASK_DRAW 1
92 #define x_defined_color w32_defined_color
93 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
95 /* Version of libpng that we were compiled with, or -1 if no PNG
96 support was compiled in. This is tested by w32-win.el to correctly
97 set up the alist used to search for PNG libraries. */
98 Lisp_Object Qlibpng_version
;
99 #endif /* HAVE_NTGUI */
102 #undef COLOR_TABLE_SUPPORT
104 typedef struct ns_bitmap_record Bitmap_Record
;
106 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
109 #define PIX_MASK_RETAIN 0
110 #define PIX_MASK_DRAW 1
112 #define x_defined_color(f, name, color_def, alloc) \
113 ns_defined_color (f, name, color_def, alloc, 0)
114 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
118 /* The symbol `postscript' identifying images of this type. */
120 static Lisp_Object Qpostscript
;
122 static void x_disable_image (struct frame
*, struct image
*);
123 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
126 static void init_color_table (void);
127 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
128 #ifdef COLOR_TABLE_SUPPORT
129 static void free_color_table (void);
130 static unsigned long *colors_in_color_table (int *n
);
133 static Lisp_Object QCmax_width
, QCmax_height
;
135 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
136 id, which is just an int that this section returns. Bitmaps are
137 reference counted so they can be shared among frames.
139 Bitmap indices are guaranteed to be > 0, so a negative number can
140 be used to indicate no bitmap.
142 If you use x_create_bitmap_from_data, then you must keep track of
143 the bitmaps yourself. That is, creating a bitmap from the same
144 data more than once will not be caught. */
147 /* Use with images created by ns_image_for_XPM. */
149 XGetPixel (XImagePtr ximage
, int x
, int y
)
151 return ns_get_pixel (ximage
, x
, y
);
154 /* Use with images created by ns_image_for_XPM; alpha set to 1;
155 pixel is assumed to be in RGB form. */
157 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
159 ns_put_pixel (ximage
, x
, y
, pixel
);
164 /* Functions to access the contents of a bitmap, given an id. */
167 x_bitmap_height (FRAME_PTR f
, ptrdiff_t id
)
169 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
173 x_bitmap_width (FRAME_PTR f
, ptrdiff_t id
)
175 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
178 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
180 x_bitmap_pixmap (FRAME_PTR f
, ptrdiff_t id
)
182 /* HAVE_NTGUI needs the explicit cast here. */
183 return (ptrdiff_t) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
187 #ifdef HAVE_X_WINDOWS
189 x_bitmap_mask (FRAME_PTR f
, ptrdiff_t id
)
191 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
195 /* Allocate a new bitmap record. Returns index of new record. */
198 x_allocate_bitmap_record (FRAME_PTR f
)
200 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
203 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
204 return ++dpyinfo
->bitmaps_last
;
206 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
207 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
211 xpalloc (dpyinfo
->bitmaps
, &dpyinfo
->bitmaps_size
,
212 10, -1, sizeof *dpyinfo
->bitmaps
);
213 return ++dpyinfo
->bitmaps_last
;
216 /* Add one reference to the reference count of the bitmap with id ID. */
219 x_reference_bitmap (FRAME_PTR f
, ptrdiff_t id
)
221 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
224 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
227 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
229 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
232 #ifdef HAVE_X_WINDOWS
234 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
235 bits
, width
, height
);
238 #endif /* HAVE_X_WINDOWS */
242 bitmap
= CreateBitmap (width
, height
,
243 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
244 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
248 #endif /* HAVE_NTGUI */
251 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
256 id
= x_allocate_bitmap_record (f
);
259 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
260 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
263 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
264 dpyinfo
->bitmaps
[id
- 1].height
= height
;
265 dpyinfo
->bitmaps
[id
- 1].width
= width
;
266 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
268 #ifdef HAVE_X_WINDOWS
269 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
270 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
271 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
272 #endif /* HAVE_X_WINDOWS */
275 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
276 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
277 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
278 #endif /* HAVE_NTGUI */
283 /* Create bitmap from file FILE for frame F. */
286 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
288 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
291 return -1; /* W32_TODO : bitmap support */
292 #endif /* HAVE_NTGUI */
296 void *bitmap
= ns_image_from_file (file
);
302 id
= x_allocate_bitmap_record (f
);
303 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
304 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
305 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
306 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
307 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
308 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
309 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
313 #ifdef HAVE_X_WINDOWS
314 unsigned int width
, height
;
316 int xhot
, yhot
, result
;
321 /* Look for an existing bitmap with the same name. */
322 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
324 if (dpyinfo
->bitmaps
[id
].refcount
325 && dpyinfo
->bitmaps
[id
].file
326 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
328 ++dpyinfo
->bitmaps
[id
].refcount
;
333 /* Search bitmap-file-path for the file, if appropriate. */
334 if (openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, make_number (R_OK
)) < 0)
337 filename
= SSDATA (found
);
339 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
340 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
341 if (result
!= BitmapSuccess
)
344 id
= x_allocate_bitmap_record (f
);
345 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
346 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
347 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
348 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
349 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
350 dpyinfo
->bitmaps
[id
- 1].height
= height
;
351 dpyinfo
->bitmaps
[id
- 1].width
= width
;
352 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
355 #endif /* HAVE_X_WINDOWS */
361 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
363 #ifdef HAVE_X_WINDOWS
364 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
366 XFreePixmap (dpyinfo
->display
, bm
->mask
);
367 #endif /* HAVE_X_WINDOWS */
370 DeleteObject (bm
->pixmap
);
371 #endif /* HAVE_NTGUI */
374 ns_release_object (bm
->img
);
384 /* Remove reference to bitmap with id number ID. */
387 x_destroy_bitmap (FRAME_PTR f
, ptrdiff_t id
)
389 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
393 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
395 if (--bm
->refcount
== 0)
398 free_bitmap_record (dpyinfo
, bm
);
404 /* Free all the bitmaps for the display specified by DPYINFO. */
407 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
410 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
412 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
413 if (bm
->refcount
> 0)
414 free_bitmap_record (dpyinfo
, bm
);
416 dpyinfo
->bitmaps_last
= 0;
419 static bool x_create_x_image_and_pixmap (struct frame
*, int, int, int,
420 XImagePtr
*, Pixmap
*);
421 static void x_destroy_x_image (XImagePtr ximg
);
424 static XImagePtr_or_DC
image_get_x_image_or_dc (struct frame
*, struct image
*,
426 static void image_unget_x_image_or_dc (struct image
*, bool, XImagePtr_or_DC
,
429 static XImagePtr
image_get_x_image (struct frame
*, struct image
*, bool);
430 static void image_unget_x_image (struct image
*, bool, XImagePtr
);
431 #define image_get_x_image_or_dc(f, img, mask_p, dummy) \
432 image_get_x_image (f, img, mask_p)
433 #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \
434 image_unget_x_image (img, mask_p, ximg)
437 #ifdef HAVE_X_WINDOWS
439 static void image_sync_to_pixmaps (struct frame
*, struct image
*);
441 /* Useful functions defined in the section
442 `Image type independent image structures' below. */
444 static unsigned long four_corners_best (XImagePtr ximg
,
447 unsigned long height
);
450 /* Create a mask of a bitmap. Note is this not a perfect mask.
451 It's nicer with some borders in this context */
454 x_create_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
457 XImagePtr ximg
, mask_img
;
458 unsigned long width
, height
;
461 unsigned long x
, y
, xp
, xm
, yp
, ym
;
464 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
469 pixmap
= x_bitmap_pixmap (f
, id
);
470 width
= x_bitmap_width (f
, id
);
471 height
= x_bitmap_height (f
, id
);
474 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
483 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
488 XDestroyImage (ximg
);
492 bg
= four_corners_best (ximg
, NULL
, width
, height
);
494 for (y
= 0; y
< ximg
->height
; ++y
)
496 for (x
= 0; x
< ximg
->width
; ++x
)
498 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
499 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
500 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
501 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
502 if (XGetPixel (ximg
, x
, y
) == bg
503 && XGetPixel (ximg
, x
, yp
) == bg
504 && XGetPixel (ximg
, x
, ym
) == bg
505 && XGetPixel (ximg
, xp
, y
) == bg
506 && XGetPixel (ximg
, xp
, yp
) == bg
507 && XGetPixel (ximg
, xp
, ym
) == bg
508 && XGetPixel (ximg
, xm
, y
) == bg
509 && XGetPixel (ximg
, xm
, yp
) == bg
510 && XGetPixel (ximg
, xm
, ym
) == bg
)
511 XPutPixel (mask_img
, x
, y
, 0);
513 XPutPixel (mask_img
, x
, y
, 1);
517 eassert (input_blocked_p ());
518 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
519 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
521 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
523 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
524 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
526 XDestroyImage (ximg
);
527 x_destroy_x_image (mask_img
);
530 #endif /* HAVE_X_WINDOWS */
533 /***********************************************************************
535 ***********************************************************************/
537 /* List of supported image types. Use define_image_type to add new
538 types. Use lookup_image_type to find a type for a given symbol. */
540 static struct image_type
*image_types
;
542 /* The symbol `xbm' which is used as the type symbol for XBM images. */
544 static Lisp_Object Qxbm
;
548 Lisp_Object QCascent
, QCmargin
, QCrelief
;
549 Lisp_Object QCconversion
;
550 static Lisp_Object QCheuristic_mask
;
551 static Lisp_Object QCcolor_symbols
;
552 static Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
;
553 static Lisp_Object QCcrop
, QCrotation
;
557 static Lisp_Object Qcount
, Qextension_data
, Qdelay
;
558 static Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
560 /* Forward function prototypes. */
562 static struct image_type
*lookup_image_type (Lisp_Object
);
563 static void x_laplace (struct frame
*, struct image
*);
564 static void x_emboss (struct frame
*, struct image
*);
565 static void x_build_heuristic_mask (struct frame
*, struct image
*,
568 extern Lisp_Object Vlibrary_cache
;
569 #define CACHE_IMAGE_TYPE(type, status) \
570 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
572 #define CACHE_IMAGE_TYPE(type, status)
575 #define ADD_IMAGE_TYPE(type) \
576 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
578 /* Define a new image type from TYPE. This adds a copy of TYPE to
579 image_types and caches the loading status of TYPE. */
581 static struct image_type
*
582 define_image_type (struct image_type
*type
)
584 struct image_type
*p
= NULL
;
585 Lisp_Object target_type
= *type
->type
;
590 for (p
= image_types
; p
; p
= p
->next
)
591 if (EQ (*p
->type
, target_type
))
596 #if defined HAVE_NTGUI && defined WINDOWSNT
597 /* If we failed to load the library before, don't try again. */
598 Lisp_Object tested
= Fassq (target_type
, Vlibrary_cache
);
599 if (CONSP (tested
) && NILP (XCDR (tested
)))
604 type_valid
= type
->init ();
605 CACHE_IMAGE_TYPE (target_type
, type_valid
? Qt
: Qnil
);
611 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
612 The initialized data segment is read-only. */
613 p
= xmalloc (sizeof *p
);
615 p
->next
= image_types
;
625 /* Value is true if OBJECT is a valid Lisp image specification. A
626 valid image specification is a list whose car is the symbol
627 `image', and whose rest is a property list. The property list must
628 contain a value for key `:type'. That value must be the name of a
629 supported image type. The rest of the property list depends on the
633 valid_image_p (Lisp_Object object
)
641 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
642 if (EQ (XCAR (tem
), QCtype
))
645 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
647 struct image_type
*type
;
648 type
= lookup_image_type (XCAR (tem
));
650 valid_p
= type
->valid_p (object
);
661 /* Log error message with format string FORMAT and argument ARG.
662 Signaling an error, e.g. when an image cannot be loaded, is not a
663 good idea because this would interrupt redisplay, and the error
664 message display would lead to another redisplay. This function
665 therefore simply displays a message. */
668 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
670 add_to_log (format
, arg1
, arg2
);
675 /***********************************************************************
677 ***********************************************************************/
679 enum image_value_type
681 IMAGE_DONT_CHECK_VALUE_TYPE
,
683 IMAGE_STRING_OR_NIL_VALUE
,
685 IMAGE_POSITIVE_INTEGER_VALUE
,
686 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
,
687 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
690 IMAGE_FUNCTION_VALUE
,
695 /* Structure used when parsing image specifications. */
699 /* Name of keyword. */
702 /* The type of value allowed. */
703 enum image_value_type type
;
705 /* True means key must be present. */
708 /* Used to recognize duplicate keywords in a property list. */
711 /* The value that was found. */
716 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
717 has the format (image KEYWORD VALUE ...). One of the keyword/
718 value pairs must be `:type TYPE'. KEYWORDS is a vector of
719 image_keywords structures of size NKEYWORDS describing other
720 allowed keyword/value pairs. Value is true if SPEC is valid. */
723 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
724 int nkeywords
, Lisp_Object type
)
733 while (CONSP (plist
))
735 Lisp_Object key
, value
;
737 /* First element of a pair must be a symbol. */
739 plist
= XCDR (plist
);
743 /* There must follow a value. */
746 value
= XCAR (plist
);
747 plist
= XCDR (plist
);
749 /* Find key in KEYWORDS. Error if not found. */
750 for (i
= 0; i
< nkeywords
; ++i
)
751 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
757 /* Record that we recognized the keyword. If a keywords
758 was found more than once, it's an error. */
759 keywords
[i
].value
= value
;
760 if (keywords
[i
].count
> 1)
764 /* Check type of value against allowed type. */
765 switch (keywords
[i
].type
)
767 case IMAGE_STRING_VALUE
:
768 if (!STRINGP (value
))
772 case IMAGE_STRING_OR_NIL_VALUE
:
773 if (!STRINGP (value
) && !NILP (value
))
777 case IMAGE_SYMBOL_VALUE
:
778 if (!SYMBOLP (value
))
782 case IMAGE_POSITIVE_INTEGER_VALUE
:
783 if (! RANGED_INTEGERP (1, value
, INT_MAX
))
787 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
:
788 if (RANGED_INTEGERP (0, value
, INT_MAX
))
791 && RANGED_INTEGERP (0, XCAR (value
), INT_MAX
)
792 && RANGED_INTEGERP (0, XCDR (value
), INT_MAX
))
796 case IMAGE_ASCENT_VALUE
:
797 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
799 else if (RANGED_INTEGERP (0, value
, 100))
803 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
804 /* Unlike the other integer-related cases, this one does not
805 verify that VALUE fits in 'int'. This is because callers
807 if (!INTEGERP (value
) || XINT (value
) < 0)
811 case IMAGE_DONT_CHECK_VALUE_TYPE
:
814 case IMAGE_FUNCTION_VALUE
:
815 value
= indirect_function (value
);
816 if (!NILP (Ffunctionp (value
)))
820 case IMAGE_NUMBER_VALUE
:
821 if (!INTEGERP (value
) && !FLOATP (value
))
825 case IMAGE_INTEGER_VALUE
:
826 if (! TYPE_RANGED_INTEGERP (int, value
))
830 case IMAGE_BOOL_VALUE
:
831 if (!NILP (value
) && !EQ (value
, Qt
))
840 if (EQ (key
, QCtype
) && !EQ (type
, value
))
844 /* Check that all mandatory fields are present. */
845 for (i
= 0; i
< nkeywords
; ++i
)
846 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
853 /* Return the value of KEY in image specification SPEC. Value is nil
854 if KEY is not present in SPEC. Set *FOUND depending on whether KEY
855 was found in SPEC. */
858 image_spec_value (Lisp_Object spec
, Lisp_Object key
, bool *found
)
862 eassert (valid_image_p (spec
));
864 for (tail
= XCDR (spec
);
865 CONSP (tail
) && CONSP (XCDR (tail
));
866 tail
= XCDR (XCDR (tail
)))
868 if (EQ (XCAR (tail
), key
))
872 return XCAR (XCDR (tail
));
882 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
883 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
884 PIXELS non-nil means return the size in pixels, otherwise return the
885 size in canonical character units.
886 FRAME is the frame on which the image will be displayed. FRAME nil
887 or omitted means use the selected frame. */)
888 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
893 if (valid_image_p (spec
))
895 struct frame
*f
= decode_window_system_frame (frame
);
896 ptrdiff_t id
= lookup_image (f
, spec
);
897 struct image
*img
= IMAGE_FROM_ID (f
, id
);
898 int width
= img
->width
+ 2 * img
->hmargin
;
899 int height
= img
->height
+ 2 * img
->vmargin
;
902 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
903 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
905 size
= Fcons (make_number (width
), make_number (height
));
908 error ("Invalid image specification");
914 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
915 doc
: /* Return t if image SPEC has a mask bitmap.
916 FRAME is the frame on which the image will be displayed. FRAME nil
917 or omitted means use the selected frame. */)
918 (Lisp_Object spec
, Lisp_Object frame
)
923 if (valid_image_p (spec
))
925 struct frame
*f
= decode_window_system_frame (frame
);
926 ptrdiff_t id
= lookup_image (f
, spec
);
927 struct image
*img
= IMAGE_FROM_ID (f
, id
);
932 error ("Invalid image specification");
937 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
938 doc
: /* Return metadata for image SPEC.
939 FRAME is the frame on which the image will be displayed. FRAME nil
940 or omitted means use the selected frame. */)
941 (Lisp_Object spec
, Lisp_Object frame
)
946 if (valid_image_p (spec
))
948 struct frame
*f
= decode_window_system_frame (frame
);
949 ptrdiff_t id
= lookup_image (f
, spec
);
950 struct image
*img
= IMAGE_FROM_ID (f
, id
);
951 ext
= img
->lisp_data
;
958 /***********************************************************************
959 Image type independent image structures
960 ***********************************************************************/
962 #define MAX_IMAGE_SIZE 10.0
963 /* Allocate and return a new image structure for image specification
964 SPEC. SPEC has a hash value of HASH. */
966 static struct image
*
967 make_image (Lisp_Object spec
, EMACS_UINT hash
)
969 struct image
*img
= xzalloc (sizeof *img
);
970 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
972 eassert (valid_image_p (spec
));
973 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
974 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
975 eassert (img
->type
!= NULL
);
977 img
->lisp_data
= Qnil
;
978 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
980 img
->corners
[BOT_CORNER
] = -1; /* Full image */
985 /* Free image IMG which was used on frame F, including its resources. */
988 free_image (struct frame
*f
, struct image
*img
)
992 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
994 /* Remove IMG from the hash table of its cache. */
996 img
->prev
->next
= img
->next
;
998 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1001 img
->next
->prev
= img
->prev
;
1003 c
->images
[img
->id
] = NULL
;
1005 /* Free resources, then free IMG. */
1006 img
->type
->free (f
, img
);
1011 /* Return true if the given widths and heights are valid for display. */
1014 check_image_size (struct frame
*f
, int width
, int height
)
1018 if (width
<= 0 || height
<= 0)
1021 if (INTEGERP (Vmax_image_size
))
1022 return (width
<= XINT (Vmax_image_size
)
1023 && height
<= XINT (Vmax_image_size
));
1024 else if (FLOATP (Vmax_image_size
))
1028 w
= FRAME_PIXEL_WIDTH (f
);
1029 h
= FRAME_PIXEL_HEIGHT (f
);
1032 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1033 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1034 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1040 /* Prepare image IMG for display on frame F. Must be called before
1041 drawing an image. */
1044 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1046 /* We're about to display IMG, so set its timestamp to `now'. */
1047 img
->timestamp
= current_emacs_time ();
1049 /* If IMG doesn't have a pixmap yet, load it now, using the image
1050 type dependent loader function. */
1051 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1052 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1054 #ifdef HAVE_X_WINDOWS
1055 if (!img
->load_failed_p
)
1058 image_sync_to_pixmaps (f
, img
);
1065 /* Value is the number of pixels for the ascent of image IMG when
1066 drawn in face FACE. */
1069 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1074 if (slice
->height
== img
->height
)
1075 height
= img
->height
+ img
->vmargin
;
1076 else if (slice
->y
== 0)
1077 height
= slice
->height
+ img
->vmargin
;
1079 height
= slice
->height
;
1081 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1086 /* W32 specific version. Why?. ++kfs */
1087 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1088 - FONT_BASE (face
->font
)) / 2;
1090 /* This expression is arranged so that if the image can't be
1091 exactly centered, it will be moved slightly up. This is
1092 because a typical font is `top-heavy' (due to the presence
1093 uppercase letters), so the image placement should err towards
1094 being top-heavy too. It also just generally looks better. */
1095 ascent
= (height
+ FONT_BASE (face
->font
)
1096 - FONT_DESCENT (face
->font
) + 1) / 2;
1097 #endif /* HAVE_NTGUI */
1100 ascent
= height
/ 2;
1103 ascent
= height
* (img
->ascent
/ 100.0);
1109 /* Image background colors. */
1111 /* Find the "best" corner color of a bitmap.
1112 On W32, XIMG is assumed to a device context with the bitmap selected. */
1114 static RGB_PIXEL_COLOR
1115 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1116 unsigned long width
, unsigned long height
)
1118 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1121 if (corners
&& corners
[BOT_CORNER
] >= 0)
1123 /* Get the colors at the corner_pixels of ximg. */
1124 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1125 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1126 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1127 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1131 /* Get the colors at the corner_pixels of ximg. */
1132 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1133 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1134 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1135 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1137 /* Choose the most frequently found color as background. */
1138 for (i
= best_count
= 0; i
< 4; ++i
)
1142 for (j
= n
= 0; j
< 4; ++j
)
1143 if (corner_pixels
[i
] == corner_pixels
[j
])
1147 best
= corner_pixels
[i
], best_count
= n
;
1153 /* Portability macros */
1157 #define Free_Pixmap(display, pixmap) \
1158 DeleteObject (pixmap)
1160 #elif defined (HAVE_NS)
1162 #define Free_Pixmap(display, pixmap) \
1163 ns_release_object (pixmap)
1167 #define Free_Pixmap(display, pixmap) \
1168 XFreePixmap (display, pixmap)
1170 #endif /* !HAVE_NTGUI && !HAVE_NS */
1173 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1174 it is guessed heuristically. If non-zero, XIMG is an existing
1175 XImage object (or device context with the image selected on W32) to
1176 use for the heuristic. */
1179 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1181 if (! img
->background_valid
)
1182 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1184 bool free_ximg
= !ximg
;
1187 #endif /* HAVE_NTGUI */
1190 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
1192 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1195 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
1197 img
->background_valid
= 1;
1200 return img
->background
;
1203 /* Return the `background_transparent' field of IMG. If IMG doesn't
1204 have one yet, it is guessed heuristically. If non-zero, MASK is an
1205 existing XImage object to use for the heuristic. */
1208 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1210 if (! img
->background_transparent_valid
)
1211 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1215 bool free_mask
= !mask
;
1218 #endif /* HAVE_NTGUI */
1221 mask
= image_get_x_image_or_dc (f
, img
, 1, &prev
);
1223 img
->background_transparent
1224 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1227 image_unget_x_image_or_dc (img
, 1, mask
, prev
);
1230 img
->background_transparent
= 0;
1232 img
->background_transparent_valid
= 1;
1235 return img
->background_transparent
;
1239 /***********************************************************************
1240 Helper functions for X image types
1241 ***********************************************************************/
1243 /* Clear X resources of image IMG on frame F according to FLAGS.
1244 FLAGS is bitwise-or of the following masks:
1245 CLEAR_IMAGE_PIXMAP free the pixmap if any.
1246 CLEAR_IMAGE_MASK means clear the mask pixmap if any.
1247 CLEAR_IMAGE_COLORS means free colors allocated for the image, if
1250 #define CLEAR_IMAGE_PIXMAP (1 << 0)
1251 #define CLEAR_IMAGE_MASK (1 << 1)
1252 #define CLEAR_IMAGE_COLORS (1 << 2)
1255 x_clear_image_1 (struct frame
*f
, struct image
*img
, int flags
)
1257 if (flags
& CLEAR_IMAGE_PIXMAP
)
1261 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1262 img
->pixmap
= NO_PIXMAP
;
1263 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1264 img
->background_valid
= 0;
1266 #ifdef HAVE_X_WINDOWS
1269 x_destroy_x_image (img
->ximg
);
1271 img
->background_valid
= 0;
1276 if (flags
& CLEAR_IMAGE_MASK
)
1280 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1281 img
->mask
= NO_PIXMAP
;
1282 img
->background_transparent_valid
= 0;
1284 #ifdef HAVE_X_WINDOWS
1287 x_destroy_x_image (img
->mask_img
);
1288 img
->mask_img
= NULL
;
1289 img
->background_transparent_valid
= 0;
1294 if ((flags
& CLEAR_IMAGE_COLORS
) && img
->ncolors
)
1296 /* W32_TODO: color table support. */
1297 #ifdef HAVE_X_WINDOWS
1298 x_free_colors (f
, img
->colors
, img
->ncolors
);
1299 #endif /* HAVE_X_WINDOWS */
1300 xfree (img
->colors
);
1307 /* Free X resources of image IMG which is used on frame F. */
1310 x_clear_image (struct frame
*f
, struct image
*img
)
1313 x_clear_image_1 (f
, img
,
1314 CLEAR_IMAGE_PIXMAP
| CLEAR_IMAGE_MASK
| CLEAR_IMAGE_COLORS
);
1319 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1320 cannot be allocated, use DFLT. Add a newly allocated color to
1321 IMG->colors, so that it can be freed again. Value is the pixel
1324 static unsigned long
1325 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1329 unsigned long result
;
1331 eassert (STRINGP (color_name
));
1333 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1334 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1337 /* This isn't called frequently so we get away with simply
1338 reallocating the color vector to the needed size, here. */
1339 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1340 img
->colors
= xrealloc (img
->colors
, ncolors
* sizeof *img
->colors
);
1341 img
->colors
[ncolors
- 1] = color
.pixel
;
1342 img
->ncolors
= ncolors
;
1343 result
= color
.pixel
;
1353 /***********************************************************************
1355 ***********************************************************************/
1357 static void cache_image (struct frame
*f
, struct image
*img
);
1359 /* Return a new, initialized image cache that is allocated from the
1360 heap. Call free_image_cache to free an image cache. */
1362 struct image_cache
*
1363 make_image_cache (void)
1365 struct image_cache
*c
= xzalloc (sizeof *c
);
1369 c
->images
= xmalloc (size
* sizeof *c
->images
);
1371 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1372 c
->buckets
= xzalloc (size
);
1377 /* Find an image matching SPEC in the cache, and return it. If no
1378 image is found, return NULL. */
1379 static struct image
*
1380 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1383 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1384 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1386 if (!c
) return NULL
;
1388 /* If the image spec does not specify a background color, the cached
1389 image must have the same background color as the current frame.
1390 The foreground color must also match, for the sake of monochrome
1393 In fact, we could ignore the foreground color matching condition
1394 for color images, or if the image spec specifies :foreground;
1395 similarly we could ignore the background color matching condition
1396 for formats that don't use transparency (such as jpeg), or if the
1397 image spec specifies :background. However, the extra memory
1398 usage is probably negligible in practice, so we don't bother. */
1400 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1401 if (img
->hash
== hash
1402 && !NILP (Fequal (img
->spec
, spec
))
1403 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1404 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1410 /* Search frame F for an image with spec SPEC, and free it. */
1413 uncache_image (struct frame
*f
, Lisp_Object spec
)
1415 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1418 free_image (f
, img
);
1419 /* As display glyphs may still be referring to the image ID, we
1420 must garbage the frame (Bug#6426). */
1421 SET_FRAME_GARBAGED (f
);
1426 /* Free image cache of frame F. Be aware that X frames share images
1430 free_image_cache (struct frame
*f
)
1432 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1437 /* Cache should not be referenced by any frame when freed. */
1438 eassert (c
->refcount
== 0);
1440 for (i
= 0; i
< c
->used
; ++i
)
1441 free_image (f
, c
->images
[i
]);
1445 FRAME_IMAGE_CACHE (f
) = NULL
;
1450 /* Clear image cache of frame F. FILTER=t means free all images.
1451 FILTER=nil means clear only images that haven't been
1452 displayed for some time.
1453 Else, only free the images which have FILTER in their `dependencies'.
1454 Should be called from time to time to reduce the number of loaded images.
1455 If image-cache-eviction-delay is non-nil, this frees images in the cache
1456 which weren't displayed for at least that many seconds. */
1459 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1461 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1465 ptrdiff_t i
, nfreed
= 0;
1467 /* Block input so that we won't be interrupted by a SIGIO
1468 while being in an inconsistent state. */
1473 /* Filter image cache. */
1474 for (i
= 0; i
< c
->used
; ++i
)
1476 struct image
*img
= c
->images
[i
];
1477 if (img
&& (EQ (Qt
, filter
)
1478 || !NILP (Fmember (filter
, img
->dependencies
))))
1480 free_image (f
, img
);
1485 else if (INTEGERP (Vimage_cache_eviction_delay
))
1487 /* Free cache based on timestamp. */
1490 ptrdiff_t nimages
= 0;
1492 for (i
= 0; i
< c
->used
; ++i
)
1496 /* If the number of cached images has grown unusually large,
1497 decrease the cache eviction delay (Bug#6230). */
1498 delay
= XINT (Vimage_cache_eviction_delay
);
1500 delay
= 1600 * delay
/ nimages
/ nimages
;
1501 delay
= max (delay
, 1);
1503 t
= current_emacs_time ();
1504 old
= sub_emacs_time (t
, EMACS_TIME_FROM_DOUBLE (delay
));
1506 for (i
= 0; i
< c
->used
; ++i
)
1508 struct image
*img
= c
->images
[i
];
1509 if (img
&& EMACS_TIME_LT (img
->timestamp
, old
))
1511 free_image (f
, img
);
1517 /* We may be clearing the image cache because, for example,
1518 Emacs was iconified for a longer period of time. In that
1519 case, current matrices may still contain references to
1520 images freed above. So, clear these matrices. */
1523 Lisp_Object tail
, frame
;
1525 FOR_EACH_FRAME (tail
, frame
)
1527 struct frame
*fr
= XFRAME (frame
);
1528 if (FRAME_IMAGE_CACHE (fr
) == c
)
1529 clear_current_matrices (fr
);
1532 ++windows_or_buffers_changed
;
1540 clear_image_caches (Lisp_Object filter
)
1542 /* FIXME: We want to do
1543 * struct terminal *t;
1544 * for (t = terminal_list; t; t = t->next_terminal)
1545 * clear_image_cache (t, filter); */
1546 Lisp_Object tail
, frame
;
1547 FOR_EACH_FRAME (tail
, frame
)
1548 if (FRAME_WINDOW_P (XFRAME (frame
)))
1549 clear_image_cache (XFRAME (frame
), filter
);
1552 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1554 doc
: /* Clear the image cache.
1555 FILTER nil or a frame means clear all images in the selected frame.
1556 FILTER t means clear the image caches of all frames.
1557 Anything else, means only clear those images which refer to FILTER,
1558 which is then usually a filename. */)
1559 (Lisp_Object filter
)
1561 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1562 clear_image_caches (filter
);
1564 clear_image_cache (decode_window_system_frame (filter
), Qt
);
1570 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1572 doc
: /* Fush the image with specification SPEC on frame FRAME.
1573 This removes the image from the Emacs image cache. If SPEC specifies
1574 an image file, the next redisplay of this image will read from the
1575 current contents of that file.
1577 FRAME nil or omitted means use the selected frame.
1578 FRAME t means refresh the image on all frames. */)
1579 (Lisp_Object spec
, Lisp_Object frame
)
1581 if (!valid_image_p (spec
))
1582 error ("Invalid image specification");
1587 FOR_EACH_FRAME (tail
, frame
)
1589 struct frame
*f
= XFRAME (frame
);
1590 if (FRAME_WINDOW_P (f
))
1591 uncache_image (f
, spec
);
1595 uncache_image (decode_window_system_frame (frame
), spec
);
1601 /* Compute masks and transform image IMG on frame F, as specified
1602 by the image's specification, */
1605 postprocess_image (struct frame
*f
, struct image
*img
)
1607 /* Manipulation of the image's mask. */
1610 Lisp_Object conversion
, spec
;
1615 /* `:heuristic-mask t'
1617 means build a mask heuristically.
1618 `:heuristic-mask (R G B)'
1619 `:mask (heuristic (R G B))'
1620 means build a mask from color (R G B) in the
1623 means remove a mask, if any. */
1625 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1627 x_build_heuristic_mask (f
, img
, mask
);
1632 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1634 if (EQ (mask
, Qheuristic
))
1635 x_build_heuristic_mask (f
, img
, Qt
);
1636 else if (CONSP (mask
)
1637 && EQ (XCAR (mask
), Qheuristic
))
1639 if (CONSP (XCDR (mask
)))
1640 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1642 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1644 else if (NILP (mask
) && found_p
&& img
->mask
)
1645 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
1649 /* Should we apply an image transformation algorithm? */
1650 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1651 if (EQ (conversion
, Qdisabled
))
1652 x_disable_image (f
, img
);
1653 else if (EQ (conversion
, Qlaplace
))
1655 else if (EQ (conversion
, Qemboss
))
1657 else if (CONSP (conversion
)
1658 && EQ (XCAR (conversion
), Qedge_detection
))
1661 tem
= XCDR (conversion
);
1663 x_edge_detection (f
, img
,
1664 Fplist_get (tem
, QCmatrix
),
1665 Fplist_get (tem
, QCcolor_adjustment
));
1671 /* Return the id of image with Lisp specification SPEC on frame F.
1672 SPEC must be a valid Lisp image specification (see valid_image_p). */
1675 lookup_image (struct frame
*f
, Lisp_Object spec
)
1680 /* F must be a window-system frame, and SPEC must be a valid image
1682 eassert (FRAME_WINDOW_P (f
));
1683 eassert (valid_image_p (spec
));
1685 /* Look up SPEC in the hash table of the image cache. */
1686 hash
= sxhash (spec
, 0);
1687 img
= search_image_cache (f
, spec
, hash
);
1688 if (img
&& img
->load_failed_p
)
1690 free_image (f
, img
);
1694 /* If not found, create a new image and cache it. */
1698 img
= make_image (spec
, hash
);
1699 cache_image (f
, img
);
1700 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1701 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1702 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1704 /* If we can't load the image, and we don't have a width and
1705 height, use some arbitrary width and height so that we can
1706 draw a rectangle for it. */
1707 if (img
->load_failed_p
)
1711 value
= image_spec_value (spec
, QCwidth
, NULL
);
1712 img
->width
= (INTEGERP (value
)
1713 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1714 value
= image_spec_value (spec
, QCheight
, NULL
);
1715 img
->height
= (INTEGERP (value
)
1716 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1720 /* Handle image type independent image attributes
1721 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1722 `:background COLOR'. */
1723 Lisp_Object ascent
, margin
, relief
, bg
;
1726 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1727 if (INTEGERP (ascent
))
1728 img
->ascent
= XFASTINT (ascent
);
1729 else if (EQ (ascent
, Qcenter
))
1730 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1732 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1733 if (INTEGERP (margin
))
1734 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1735 else if (CONSP (margin
))
1737 img
->hmargin
= XFASTINT (XCAR (margin
));
1738 img
->vmargin
= XFASTINT (XCDR (margin
));
1741 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1742 relief_bound
= INT_MAX
- max (img
->hmargin
, img
->vmargin
);
1743 if (RANGED_INTEGERP (- relief_bound
, relief
, relief_bound
))
1745 img
->relief
= XINT (relief
);
1746 img
->hmargin
+= eabs (img
->relief
);
1747 img
->vmargin
+= eabs (img
->relief
);
1750 if (! img
->background_valid
)
1752 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1756 = x_alloc_image_color (f
, img
, bg
,
1757 FRAME_BACKGROUND_PIXEL (f
));
1758 img
->background_valid
= 1;
1762 /* Do image transformations and compute masks, unless we
1763 don't have the image yet. */
1764 if (!EQ (*img
->type
->type
, Qpostscript
))
1765 postprocess_image (f
, img
);
1771 /* We're using IMG, so set its timestamp to `now'. */
1772 img
->timestamp
= current_emacs_time ();
1774 /* Value is the image id. */
1779 /* Cache image IMG in the image cache of frame F. */
1782 cache_image (struct frame
*f
, struct image
*img
)
1784 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1787 /* Find a free slot in c->images. */
1788 for (i
= 0; i
< c
->used
; ++i
)
1789 if (c
->images
[i
] == NULL
)
1792 /* If no free slot found, maybe enlarge c->images. */
1793 if (i
== c
->used
&& c
->used
== c
->size
)
1794 c
->images
= xpalloc (c
->images
, &c
->size
, 1, -1, sizeof *c
->images
);
1796 /* Add IMG to c->images, and assign IMG an id. */
1802 /* Add IMG to the cache's hash table. */
1803 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1804 img
->next
= c
->buckets
[i
];
1806 img
->next
->prev
= img
;
1808 c
->buckets
[i
] = img
;
1812 /* Call FN on every image in the image cache of frame F. Used to mark
1813 Lisp Objects in the image cache. */
1815 /* Mark Lisp objects in image IMG. */
1818 mark_image (struct image
*img
)
1820 mark_object (img
->spec
);
1821 mark_object (img
->dependencies
);
1823 if (!NILP (img
->lisp_data
))
1824 mark_object (img
->lisp_data
);
1829 mark_image_cache (struct image_cache
*c
)
1834 for (i
= 0; i
< c
->used
; ++i
)
1836 mark_image (c
->images
[i
]);
1842 /***********************************************************************
1843 X / NS / W32 support code
1844 ***********************************************************************/
1848 /* Macro for defining functions that will be loaded from image DLLs. */
1849 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1851 /* Macro for loading those image functions from the library. */
1852 #define LOAD_IMGLIB_FN(lib,func) { \
1853 fn_##func = (void *) GetProcAddress (lib, #func); \
1854 if (!fn_##func) return 0; \
1857 #endif /* WINDOWSNT */
1859 /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the
1861 WIDTH and HEIGHT must both be positive.
1862 If XIMG is null, assume it is a bitmap. */
1864 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1866 #ifdef HAVE_X_WINDOWS
1867 /* Respect Xlib's limits: it cannot deal with images that have more
1868 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1869 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1872 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1873 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1876 int bitmap_pad
, depth
, bytes_per_line
;
1879 bitmap_pad
= ximg
->bitmap_pad
;
1880 depth
= ximg
->depth
;
1881 bytes_per_line
= ximg
->bytes_per_line
;
1887 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1889 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1890 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1892 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1893 For now, assume that every image size is allowed on these systems. */
1898 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1899 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1900 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1901 via xmalloc. Print error messages via image_error if an error
1902 occurs. Value is true if successful.
1904 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1905 should indicate the bit depth of the image. */
1908 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1909 XImagePtr
*ximg
, Pixmap
*pixmap
)
1911 #ifdef HAVE_X_WINDOWS
1912 Display
*display
= FRAME_X_DISPLAY (f
);
1913 Window window
= FRAME_X_WINDOW (f
);
1914 Screen
*screen
= FRAME_X_SCREEN (f
);
1916 eassert (input_blocked_p ());
1919 depth
= DefaultDepthOfScreen (screen
);
1920 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1921 depth
, ZPixmap
, 0, NULL
, width
, height
,
1922 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1925 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1929 if (! x_check_image_size (*ximg
, width
, height
))
1931 x_destroy_x_image (*ximg
);
1933 image_error ("Image too large (%dx%d)",
1934 make_number (width
), make_number (height
));
1938 /* Allocate image raster. */
1939 (*ximg
)->data
= xmalloc ((*ximg
)->bytes_per_line
* height
);
1941 /* Allocate a pixmap of the same size. */
1942 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1943 if (*pixmap
== NO_PIXMAP
)
1945 x_destroy_x_image (*ximg
);
1947 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1952 #endif /* HAVE_X_WINDOWS */
1956 BITMAPINFOHEADER
*header
;
1958 int scanline_width_bits
;
1960 int palette_colors
= 0;
1965 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1966 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1968 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
1972 scanline_width_bits
= width
* depth
;
1973 remainder
= scanline_width_bits
% 32;
1976 scanline_width_bits
+= 32 - remainder
;
1978 /* Bitmaps with a depth less than 16 need a palette. */
1979 /* BITMAPINFO structure already contains the first RGBQUAD. */
1981 palette_colors
= 1 << (depth
- 1);
1983 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
1985 header
= &(*ximg
)->info
.bmiHeader
;
1986 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
1987 header
->biSize
= sizeof (*header
);
1988 header
->biWidth
= width
;
1989 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
1990 header
->biPlanes
= 1;
1991 header
->biBitCount
= depth
;
1992 header
->biCompression
= BI_RGB
;
1993 header
->biClrUsed
= palette_colors
;
1995 /* TODO: fill in palette. */
1998 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
1999 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2000 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2001 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2002 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2003 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2004 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2005 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2008 hdc
= get_frame_dc (f
);
2010 /* Create a DIBSection and raster array for the bitmap,
2011 and store its handle in *pixmap. */
2012 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2013 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2014 /* casting avoids a GCC warning */
2015 (void **)&((*ximg
)->data
), NULL
, 0);
2017 /* Realize display palette and garbage all frames. */
2018 release_frame_dc (f
, hdc
);
2020 if (*pixmap
== NULL
)
2022 DWORD err
= GetLastError ();
2023 Lisp_Object errcode
;
2024 /* All system errors are < 10000, so the following is safe. */
2025 XSETINT (errcode
, err
);
2026 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2027 x_destroy_x_image (*ximg
);
2033 #endif /* HAVE_NTGUI */
2036 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2040 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2049 /* Destroy XImage XIMG. Free XIMG->data. */
2052 x_destroy_x_image (XImagePtr ximg
)
2054 eassert (input_blocked_p ());
2057 #ifdef HAVE_X_WINDOWS
2060 XDestroyImage (ximg
);
2061 #endif /* HAVE_X_WINDOWS */
2063 /* Data will be freed by DestroyObject. */
2066 #endif /* HAVE_NTGUI */
2068 ns_release_object (ximg
);
2069 #endif /* HAVE_NS */
2074 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2075 are width and height of both the image and pixmap. */
2078 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2080 #ifdef HAVE_X_WINDOWS
2083 eassert (input_blocked_p ());
2084 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2085 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2086 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2087 #endif /* HAVE_X_WINDOWS */
2090 #if 0 /* I don't think this is necessary looking at where it is used. */
2091 HDC hdc
= get_frame_dc (f
);
2092 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2093 release_frame_dc (f
, hdc
);
2095 #endif /* HAVE_NTGUI */
2098 eassert (ximg
== pixmap
);
2099 ns_retain_object (ximg
);
2103 /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches
2104 with image_put_x_image. */
2107 image_create_x_image_and_pixmap (struct frame
*f
, struct image
*img
,
2108 int width
, int height
, int depth
,
2109 XImagePtr
*ximg
, bool mask_p
)
2111 eassert ((!mask_p
? img
->pixmap
: img
->mask
) == NO_PIXMAP
);
2113 return x_create_x_image_and_pixmap (f
, width
, height
, depth
, ximg
,
2114 !mask_p
? &img
->pixmap
: &img
->mask
);
2117 /* Put X image XIMG into image IMG on frame F, as a mask if and only
2118 if MASK_P. On X, this simply records XIMG on a member of IMG, so
2119 it can be put into the pixmap afterwards via image_sync_to_pixmaps.
2120 On the other platforms, it puts XIMG into the pixmap, then frees
2121 the X image and its buffer. */
2124 image_put_x_image (struct frame
*f
, struct image
*img
, XImagePtr ximg
,
2127 #ifdef HAVE_X_WINDOWS
2130 eassert (img
->ximg
== NULL
);
2135 eassert (img
->mask_img
== NULL
);
2136 img
->mask_img
= ximg
;
2139 x_put_x_image (f
, ximg
, !mask_p
? img
->pixmap
: img
->mask
,
2140 img
->width
, img
->height
);
2141 x_destroy_x_image (ximg
);
2145 #ifdef HAVE_X_WINDOWS
2146 /* Put the X images recorded in IMG on frame F into pixmaps, then free
2147 the X images and their buffers. */
2150 image_sync_to_pixmaps (struct frame
*f
, struct image
*img
)
2154 x_put_x_image (f
, img
->ximg
, img
->pixmap
, img
->width
, img
->height
);
2155 x_destroy_x_image (img
->ximg
);
2160 x_put_x_image (f
, img
->mask_img
, img
->mask
, img
->width
, img
->height
);
2161 x_destroy_x_image (img
->mask_img
);
2162 img
->mask_img
= NULL
;
2168 /* Create a memory device context for IMG on frame F. It stores the
2169 currently selected GDI object into *PREV for future restoration by
2170 image_unget_x_image_or_dc. */
2172 static XImagePtr_or_DC
2173 image_get_x_image_or_dc (struct frame
*f
, struct image
*img
, bool mask_p
,
2176 HDC frame_dc
= get_frame_dc (f
);
2177 XImagePtr_or_DC ximg
= CreateCompatibleDC (frame_dc
);
2179 release_frame_dc (f
, frame_dc
);
2180 *prev
= SelectObject (ximg
, !mask_p
? img
->pixmap
: img
->mask
);
2186 image_unget_x_image_or_dc (struct image
*img
, bool mask_p
,
2187 XImagePtr_or_DC ximg
, HGDIOBJ prev
)
2189 SelectObject (ximg
, prev
);
2192 #else /* !HAVE_NTGUI */
2193 /* Get the X image for IMG on frame F. The resulting X image data
2194 should be treated as read-only at least on X. */
2197 image_get_x_image (struct frame
*f
, struct image
*img
, bool mask_p
)
2199 #ifdef HAVE_X_WINDOWS
2200 XImagePtr ximg_in_img
= !mask_p
? img
->ximg
: img
->mask_img
;
2205 return XGetImage (FRAME_X_DISPLAY (f
), !mask_p
? img
->pixmap
: img
->mask
,
2206 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
2207 #elif defined (HAVE_NS)
2208 XImagePtr pixmap
= !mask_p
? img
->pixmap
: img
->mask
;
2210 ns_retain_object (pixmap
);
2216 image_unget_x_image (struct image
*img
, bool mask_p
, XImagePtr ximg
)
2218 #ifdef HAVE_X_WINDOWS
2219 XImagePtr ximg_in_img
= !mask_p
? img
->ximg
: img
->mask_img
;
2222 eassert (ximg
== ximg_in_img
);
2224 XDestroyImage (ximg
);
2225 #elif defined (HAVE_NS)
2226 ns_release_object (ximg
);
2229 #endif /* !HAVE_NTGUI */
2232 /***********************************************************************
2234 ***********************************************************************/
2236 /* Find image file FILE. Look in data-directory/images, then
2237 x-bitmap-file-path. Value is the encoded full name of the file
2238 found, or nil if not found. */
2241 x_find_image_file (Lisp_Object file
)
2243 Lisp_Object file_found
, search_path
;
2246 /* TODO I think this should use something like image-load-path
2247 instead. Unfortunately, that can contain non-string elements. */
2248 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2250 Vx_bitmap_file_path
);
2252 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2253 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2259 file_found
= ENCODE_FILE (file_found
);
2268 /* Read FILE into memory. Value is a pointer to a buffer allocated
2269 with xmalloc holding FILE's contents. Value is null if an error
2270 occurred. *SIZE is set to the size of the file. */
2272 static unsigned char *
2273 slurp_file (char *file
, ptrdiff_t *size
)
2275 FILE *fp
= emacs_fopen (file
, "rb");
2276 unsigned char *buf
= NULL
;
2279 if (fp
&& fstat (fileno (fp
), &st
) == 0
2280 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
2281 && (buf
= xmalloc (st
.st_size
),
2282 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2303 /***********************************************************************
2305 ***********************************************************************/
2307 static bool xbm_load (struct frame
*f
, struct image
*img
);
2308 static bool xbm_image_p (Lisp_Object object
);
2309 static bool xbm_file_p (Lisp_Object
);
2312 /* Indices of image specification fields in xbm_format, below. */
2314 enum xbm_keyword_index
2332 /* Vector of image_keyword structures describing the format
2333 of valid XBM image specifications. */
2335 static const struct image_keyword xbm_format
[XBM_LAST
] =
2337 {":type", IMAGE_SYMBOL_VALUE
, 1},
2338 {":file", IMAGE_STRING_VALUE
, 0},
2339 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2340 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2341 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2342 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2343 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2344 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2345 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
2346 {":relief", IMAGE_INTEGER_VALUE
, 0},
2347 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2348 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2349 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2352 /* Structure describing the image type XBM. */
2354 static struct image_type xbm_type
=
2364 /* Tokens returned from xbm_scan. */
2373 /* Return true if OBJECT is a valid XBM-type image specification.
2374 A valid specification is a list starting with the symbol `image'
2375 The rest of the list is a property list which must contain an
2378 If the specification specifies a file to load, it must contain
2379 an entry `:file FILENAME' where FILENAME is a string.
2381 If the specification is for a bitmap loaded from memory it must
2382 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2383 WIDTH and HEIGHT are integers > 0. DATA may be:
2385 1. a string large enough to hold the bitmap data, i.e. it must
2386 have a size >= (WIDTH + 7) / 8 * HEIGHT
2388 2. a bool-vector of size >= WIDTH * HEIGHT
2390 3. a vector of strings or bool-vectors, one for each line of the
2393 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2394 may not be specified in this case because they are defined in the
2397 Both the file and data forms may contain the additional entries
2398 `:background COLOR' and `:foreground COLOR'. If not present,
2399 foreground and background of the frame on which the image is
2400 displayed is used. */
2403 xbm_image_p (Lisp_Object object
)
2405 struct image_keyword kw
[XBM_LAST
];
2407 memcpy (kw
, xbm_format
, sizeof kw
);
2408 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2411 eassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2413 if (kw
[XBM_FILE
].count
)
2415 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2418 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2420 /* In-memory XBM file. */
2421 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2429 /* Entries for `:width', `:height' and `:data' must be present. */
2430 if (!kw
[XBM_WIDTH
].count
2431 || !kw
[XBM_HEIGHT
].count
2432 || !kw
[XBM_DATA
].count
)
2435 data
= kw
[XBM_DATA
].value
;
2436 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2437 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2439 /* Check type of data, and width and height against contents of
2445 /* Number of elements of the vector must be >= height. */
2446 if (ASIZE (data
) < height
)
2449 /* Each string or bool-vector in data must be large enough
2450 for one line of the image. */
2451 for (i
= 0; i
< height
; ++i
)
2453 Lisp_Object elt
= AREF (data
, i
);
2458 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2461 else if (BOOL_VECTOR_P (elt
))
2463 if (XBOOL_VECTOR (elt
)->size
< width
)
2470 else if (STRINGP (data
))
2473 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2476 else if (BOOL_VECTOR_P (data
))
2478 if (XBOOL_VECTOR (data
)->size
/ height
< width
)
2489 /* Scan a bitmap file. FP is the stream to read from. Value is
2490 either an enumerator from enum xbm_token, or a character for a
2491 single-character token, or 0 at end of file. If scanning an
2492 identifier, store the lexeme of the identifier in SVAL. If
2493 scanning a number, store its value in *IVAL. */
2496 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2502 /* Skip white space. */
2503 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
2508 else if (c_isdigit (c
))
2510 int value
= 0, digit
;
2512 if (c
== '0' && *s
< end
)
2515 if (c
== 'x' || c
== 'X')
2522 else if (c
>= 'a' && c
<= 'f')
2523 digit
= c
- 'a' + 10;
2524 else if (c
>= 'A' && c
<= 'F')
2525 digit
= c
- 'A' + 10;
2528 value
= 16 * value
+ digit
;
2531 else if (c_isdigit (c
))
2535 && (c
= *(*s
)++, c_isdigit (c
)))
2536 value
= 8 * value
+ c
- '0';
2543 && (c
= *(*s
)++, c_isdigit (c
)))
2544 value
= 10 * value
+ c
- '0';
2552 else if (c_isalpha (c
) || c
== '_')
2556 && (c
= *(*s
)++, (c_isalnum (c
) || c
== '_')))
2563 else if (c
== '/' && **s
== '*')
2565 /* C-style comment. */
2567 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2581 /* Create a Windows bitmap from X bitmap data. */
2583 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2585 static unsigned char swap_nibble
[16]
2586 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2587 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2588 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2589 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2591 unsigned char *bits
, *p
;
2594 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2595 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2596 bits
= alloca (height
* w2
);
2597 memset (bits
, 0, height
* w2
);
2598 for (i
= 0; i
< height
; i
++)
2601 for (j
= 0; j
< w1
; j
++)
2603 /* Bitswap XBM bytes to match how Windows does things. */
2604 unsigned char c
= *data
++;
2605 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2606 | (swap_nibble
[(c
>>4) & 0xf]));
2609 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2615 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2616 COLORREF foreground
, COLORREF background
)
2618 HDC hdc
, old_img_dc
, new_img_dc
;
2619 HGDIOBJ old_prev
, new_prev
;
2622 hdc
= get_frame_dc (f
);
2623 old_img_dc
= CreateCompatibleDC (hdc
);
2624 new_img_dc
= CreateCompatibleDC (hdc
);
2625 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2626 release_frame_dc (f
, hdc
);
2627 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2628 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2629 /* Windows convention for mono bitmaps is black = background,
2630 white = foreground. */
2631 SetTextColor (new_img_dc
, background
);
2632 SetBkColor (new_img_dc
, foreground
);
2634 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2637 SelectObject (old_img_dc
, old_prev
);
2638 SelectObject (new_img_dc
, new_prev
);
2639 DeleteDC (old_img_dc
);
2640 DeleteDC (new_img_dc
);
2641 DeleteObject (img
->pixmap
);
2642 if (new_pixmap
== 0)
2643 fprintf (stderr
, "Failed to convert image to color.\n");
2645 img
->pixmap
= new_pixmap
;
2648 #define XBM_BIT_SHUFFLE(b) (~(b))
2652 #define XBM_BIT_SHUFFLE(b) (b)
2654 #endif /* HAVE_NTGUI */
2658 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2659 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2660 bool non_default_colors
)
2664 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2666 /* If colors were specified, transfer the bitmap to a color one. */
2667 if (non_default_colors
)
2668 convert_mono_to_color_image (f
, img
, fg
, bg
);
2670 #elif defined (HAVE_NS)
2671 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2675 (x_check_image_size (0, img
->width
, img
->height
)
2676 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2679 img
->width
, img
->height
,
2681 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2683 #endif /* !HAVE_NTGUI && !HAVE_NS */
2688 /* Replacement for XReadBitmapFileData which isn't available under old
2689 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2690 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2691 the image. Return in *DATA the bitmap data allocated with xmalloc.
2692 Value is true if successful. DATA null means just test if
2693 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR,
2694 inhibit the call to image_error when the image size is invalid (the
2695 bitmap remains unread). */
2698 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2699 int *width
, int *height
, char **data
,
2700 bool inhibit_image_error
)
2702 unsigned char *s
= contents
;
2703 char buffer
[BUFSIZ
];
2706 int bytes_per_line
, i
, nbytes
;
2712 LA1 = xbm_scan (&s, end, buffer, &value)
2714 #define expect(TOKEN) \
2715 if (LA1 != (TOKEN)) \
2720 #define expect_ident(IDENT) \
2721 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2726 *width
= *height
= -1;
2729 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2731 /* Parse defines for width, height and hot-spots. */
2735 expect_ident ("define");
2736 expect (XBM_TK_IDENT
);
2738 if (LA1
== XBM_TK_NUMBER
)
2740 char *q
= strrchr (buffer
, '_');
2741 q
= q
? q
+ 1 : buffer
;
2742 if (strcmp (q
, "width") == 0)
2744 else if (strcmp (q
, "height") == 0)
2747 expect (XBM_TK_NUMBER
);
2750 if (!check_image_size (f
, *width
, *height
))
2752 if (!inhibit_image_error
)
2753 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2756 else if (data
== NULL
)
2759 /* Parse bits. Must start with `static'. */
2760 expect_ident ("static");
2761 if (LA1
== XBM_TK_IDENT
)
2763 if (strcmp (buffer
, "unsigned") == 0)
2766 expect_ident ("char");
2768 else if (strcmp (buffer
, "short") == 0)
2772 if (*width
% 16 && *width
% 16 < 9)
2775 else if (strcmp (buffer
, "char") == 0)
2783 expect (XBM_TK_IDENT
);
2789 if (! x_check_image_size (0, *width
, *height
))
2791 if (!inhibit_image_error
)
2792 image_error ("Image too large (%dx%d)",
2793 make_number (*width
), make_number (*height
));
2796 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2797 nbytes
= bytes_per_line
* *height
;
2798 p
= *data
= xmalloc (nbytes
);
2802 for (i
= 0; i
< nbytes
; i
+= 2)
2805 expect (XBM_TK_NUMBER
);
2807 *p
++ = XBM_BIT_SHUFFLE (val
);
2808 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2809 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2811 if (LA1
== ',' || LA1
== '}')
2819 for (i
= 0; i
< nbytes
; ++i
)
2822 expect (XBM_TK_NUMBER
);
2824 *p
++ = XBM_BIT_SHUFFLE (val
);
2826 if (LA1
== ',' || LA1
== '}')
2851 /* Load XBM image IMG which will be displayed on frame F from buffer
2852 CONTENTS. END is the end of the buffer. Value is true if
2856 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2863 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2867 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2868 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2869 bool non_default_colors
= 0;
2872 eassert (img
->width
> 0 && img
->height
> 0);
2874 /* Get foreground and background colors, maybe allocate colors. */
2875 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2878 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2879 non_default_colors
= 1;
2881 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2884 background
= x_alloc_image_color (f
, img
, value
, background
);
2885 img
->background
= background
;
2886 img
->background_valid
= 1;
2887 non_default_colors
= 1;
2890 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2891 foreground
, background
,
2892 non_default_colors
);
2895 if (img
->pixmap
== NO_PIXMAP
)
2897 x_clear_image (f
, img
);
2898 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2904 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2910 /* Value is true if DATA looks like an in-memory XBM file. */
2913 xbm_file_p (Lisp_Object data
)
2916 return (STRINGP (data
)
2917 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2918 (SDATA (data
) + SBYTES (data
)),
2923 /* Fill image IMG which is used on frame F with pixmap data. Value is
2924 true if successful. */
2927 xbm_load (struct frame
*f
, struct image
*img
)
2930 Lisp_Object file_name
;
2932 eassert (xbm_image_p (img
->spec
));
2934 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2935 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2936 if (STRINGP (file_name
))
2939 unsigned char *contents
;
2942 file
= x_find_image_file (file_name
);
2943 if (!STRINGP (file
))
2945 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2949 contents
= slurp_file (SSDATA (file
), &size
);
2950 if (contents
== NULL
)
2952 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2956 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2961 struct image_keyword fmt
[XBM_LAST
];
2963 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2964 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2965 bool non_default_colors
= 0;
2968 bool in_memory_file_p
= 0;
2970 /* See if data looks like an in-memory XBM file. */
2971 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2972 in_memory_file_p
= xbm_file_p (data
);
2974 /* Parse the image specification. */
2975 memcpy (fmt
, xbm_format
, sizeof fmt
);
2976 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2979 /* Get specified width, and height. */
2980 if (!in_memory_file_p
)
2982 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2983 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2984 eassert (img
->width
> 0 && img
->height
> 0);
2985 if (!check_image_size (f
, img
->width
, img
->height
))
2987 image_error ("Invalid image size (see `max-image-size')",
2993 /* Get foreground and background colors, maybe allocate colors. */
2994 if (fmt
[XBM_FOREGROUND
].count
2995 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2997 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2999 non_default_colors
= 1;
3002 if (fmt
[XBM_BACKGROUND
].count
3003 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
3005 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
3007 non_default_colors
= 1;
3010 if (in_memory_file_p
)
3011 success_p
= xbm_load_image (f
, img
, SDATA (data
),
3020 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
3022 p
= bits
= alloca (nbytes
* img
->height
);
3023 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
3025 Lisp_Object line
= AREF (data
, i
);
3027 memcpy (p
, SDATA (line
), nbytes
);
3029 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
3032 else if (STRINGP (data
))
3033 bits
= SSDATA (data
);
3035 bits
= (char *) XBOOL_VECTOR (data
)->data
;
3041 /* Windows mono bitmaps are reversed compared with X. */
3042 invertedBits
= bits
;
3043 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
3045 bits
= alloca (nbytes
);
3046 for (i
= 0; i
< nbytes
; i
++)
3047 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
3050 /* Create the pixmap. */
3052 if (x_check_image_size (0, img
->width
, img
->height
))
3053 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
3054 foreground
, background
,
3055 non_default_colors
);
3057 img
->pixmap
= NO_PIXMAP
;
3063 image_error ("Unable to create pixmap for XBM image `%s'",
3065 x_clear_image (f
, img
);
3075 /***********************************************************************
3077 ***********************************************************************/
3079 #if defined (HAVE_XPM) || defined (HAVE_NS)
3081 static bool xpm_image_p (Lisp_Object object
);
3082 static bool xpm_load (struct frame
*f
, struct image
*img
);
3084 #endif /* HAVE_XPM || HAVE_NS */
3088 /* Indicate to xpm.h that we don't have Xlib. */
3090 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3091 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3092 #define XColor xpm_XColor
3093 #define XImage xpm_XImage
3094 #define Display xpm_Display
3095 #define PIXEL_ALREADY_TYPEDEFED
3096 #include "X11/xpm.h"
3101 #undef PIXEL_ALREADY_TYPEDEFED
3103 #include "X11/xpm.h"
3104 #endif /* HAVE_NTGUI */
3105 #endif /* HAVE_XPM */
3107 #if defined (HAVE_XPM) || defined (HAVE_NS)
3108 /* The symbol `xpm' identifying XPM-format images. */
3110 static Lisp_Object Qxpm
;
3112 /* Indices of image specification fields in xpm_format, below. */
3114 enum xpm_keyword_index
3130 /* Vector of image_keyword structures describing the format
3131 of valid XPM image specifications. */
3133 static const struct image_keyword xpm_format
[XPM_LAST
] =
3135 {":type", IMAGE_SYMBOL_VALUE
, 1},
3136 {":file", IMAGE_STRING_VALUE
, 0},
3137 {":data", IMAGE_STRING_VALUE
, 0},
3138 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3139 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
3140 {":relief", IMAGE_INTEGER_VALUE
, 0},
3141 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3142 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3143 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3144 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3145 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3148 #if defined HAVE_NTGUI && defined WINDOWSNT
3149 static bool init_xpm_functions (void);
3151 #define init_xpm_functions NULL
3154 /* Structure describing the image type XPM. */
3156 static struct image_type xpm_type
=
3166 #ifdef HAVE_X_WINDOWS
3168 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3169 functions for allocating image colors. Our own functions handle
3170 color allocation failures more gracefully than the ones on the XPM
3173 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3174 #define ALLOC_XPM_COLORS
3176 #endif /* HAVE_X_WINDOWS */
3178 #ifdef ALLOC_XPM_COLORS
3180 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3183 /* An entry in a hash table used to cache color definitions of named
3184 colors. This cache is necessary to speed up XPM image loading in
3185 case we do color allocations ourselves. Without it, we would need
3186 a call to XParseColor per pixel in the image. */
3188 struct xpm_cached_color
3190 /* Next in collision chain. */
3191 struct xpm_cached_color
*next
;
3193 /* Color definition (RGB and pixel color). */
3197 char name
[FLEXIBLE_ARRAY_MEMBER
];
3200 /* The hash table used for the color cache, and its bucket vector
3203 #define XPM_COLOR_CACHE_BUCKETS 1001
3204 static struct xpm_cached_color
**xpm_color_cache
;
3206 /* Initialize the color cache. */
3209 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3211 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3212 xpm_color_cache
= xzalloc (nbytes
);
3213 init_color_table ();
3215 if (attrs
->valuemask
& XpmColorSymbols
)
3220 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3221 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3222 attrs
->colorsymbols
[i
].value
, &color
))
3224 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3226 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3231 /* Free the color cache. */
3234 xpm_free_color_cache (void)
3236 struct xpm_cached_color
*p
, *next
;
3239 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3240 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3246 xfree (xpm_color_cache
);
3247 xpm_color_cache
= NULL
;
3248 free_color_table ();
3251 /* Return the bucket index for color named COLOR_NAME in the color
3255 xpm_color_bucket (char *color_name
)
3257 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3258 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3262 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3263 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3266 static struct xpm_cached_color
*
3267 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3270 struct xpm_cached_color
*p
;
3273 bucket
= xpm_color_bucket (color_name
);
3275 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3276 p
= xmalloc (nbytes
);
3277 strcpy (p
->name
, color_name
);
3279 p
->next
= xpm_color_cache
[bucket
];
3280 xpm_color_cache
[bucket
] = p
;
3284 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3285 return the cached definition in *COLOR. Otherwise, make a new
3286 entry in the cache and allocate the color. Value is false if color
3287 allocation failed. */
3290 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3292 struct xpm_cached_color
*p
;
3293 int h
= xpm_color_bucket (color_name
);
3295 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3296 if (strcmp (p
->name
, color_name
) == 0)
3301 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3304 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3306 p
= xpm_cache_color (f
, color_name
, color
, h
);
3308 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3309 with transparency, and it's useful. */
3310 else if (strcmp ("opaque", color_name
) == 0)
3312 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3313 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3314 p
= xpm_cache_color (f
, color_name
, color
, h
);
3321 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3322 CLOSURE is a pointer to the frame on which we allocate the
3323 color. Return in *COLOR the allocated color. Value is non-zero
3327 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3330 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3334 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3335 is a pointer to the frame on which we allocate the color. Value is
3336 non-zero if successful. */
3339 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3344 #endif /* ALLOC_XPM_COLORS */
3349 /* XPM library details. */
3351 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3352 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3353 xpm_XImage
**, XpmAttributes
*));
3354 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3355 xpm_XImage
**, XpmAttributes
*));
3356 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3359 init_xpm_functions (void)
3363 if (!(library
= w32_delayed_load (Qxpm
)))
3366 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3367 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3368 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3369 LOAD_IMGLIB_FN (library
, XImageFree
);
3373 #endif /* WINDOWSNT */
3375 #if defined HAVE_NTGUI && !defined WINDOWSNT
3376 /* Glue for code below */
3377 #define fn_XpmReadFileToImage XpmReadFileToImage
3378 #define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer
3379 #define fn_XImageFree XImageFree
3380 #define fn_XpmFreeAttributes XpmFreeAttributes
3381 #endif /* HAVE_NTGUI && !WINDOWSNT */
3383 /* Value is true if COLOR_SYMBOLS is a valid color symbols list
3384 for XPM images. Such a list must consist of conses whose car and
3388 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3390 while (CONSP (color_symbols
))
3392 Lisp_Object sym
= XCAR (color_symbols
);
3394 || !STRINGP (XCAR (sym
))
3395 || !STRINGP (XCDR (sym
)))
3397 color_symbols
= XCDR (color_symbols
);
3400 return NILP (color_symbols
);
3404 /* Value is true if OBJECT is a valid XPM image specification. */
3407 xpm_image_p (Lisp_Object object
)
3409 struct image_keyword fmt
[XPM_LAST
];
3410 memcpy (fmt
, xpm_format
, sizeof fmt
);
3411 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3412 /* Either `:file' or `:data' must be present. */
3413 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3414 /* Either no `:color-symbols' or it's a list of conses
3415 whose car and cdr are strings. */
3416 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3417 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3420 #endif /* HAVE_XPM || HAVE_NS */
3422 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3424 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3426 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3429 XpmAttributes attrs
;
3430 Pixmap bitmap
, mask
;
3432 memset (&attrs
, 0, sizeof attrs
);
3434 attrs
.visual
= FRAME_X_VISUAL (f
);
3435 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3436 attrs
.valuemask
|= XpmVisual
;
3437 attrs
.valuemask
|= XpmColormap
;
3439 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3440 (char **) bits
, &bitmap
, &mask
, &attrs
);
3441 if (rc
!= XpmSuccess
)
3443 XpmFreeAttributes (&attrs
);
3447 id
= x_allocate_bitmap_record (f
);
3448 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3449 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3450 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3451 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3452 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3453 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3454 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3455 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3457 XpmFreeAttributes (&attrs
);
3460 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3462 /* Load image IMG which will be displayed on frame F. Value is
3463 true if successful. */
3468 xpm_load (struct frame
*f
, struct image
*img
)
3471 XpmAttributes attrs
;
3472 Lisp_Object specified_file
, color_symbols
;
3475 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3476 #endif /* HAVE_NTGUI */
3478 /* Configure the XPM lib. Use the visual of frame F. Allocate
3479 close colors. Return colors allocated. */
3480 memset (&attrs
, 0, sizeof attrs
);
3483 attrs
.visual
= FRAME_X_VISUAL (f
);
3484 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3485 attrs
.valuemask
|= XpmVisual
;
3486 attrs
.valuemask
|= XpmColormap
;
3487 #endif /* HAVE_NTGUI */
3489 #ifdef ALLOC_XPM_COLORS
3490 /* Allocate colors with our own functions which handle
3491 failing color allocation more gracefully. */
3492 attrs
.color_closure
= f
;
3493 attrs
.alloc_color
= xpm_alloc_color
;
3494 attrs
.free_colors
= xpm_free_colors
;
3495 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3496 #else /* not ALLOC_XPM_COLORS */
3497 /* Let the XPM lib allocate colors. */
3498 attrs
.valuemask
|= XpmReturnAllocPixels
;
3499 #ifdef XpmAllocCloseColors
3500 attrs
.alloc_close_colors
= 1;
3501 attrs
.valuemask
|= XpmAllocCloseColors
;
3502 #else /* not XpmAllocCloseColors */
3503 attrs
.closeness
= 600;
3504 attrs
.valuemask
|= XpmCloseness
;
3505 #endif /* not XpmAllocCloseColors */
3506 #endif /* ALLOC_XPM_COLORS */
3508 /* If image specification contains symbolic color definitions, add
3509 these to `attrs'. */
3510 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3511 if (CONSP (color_symbols
))
3514 XpmColorSymbol
*xpm_syms
;
3517 attrs
.valuemask
|= XpmColorSymbols
;
3519 /* Count number of symbols. */
3520 attrs
.numsymbols
= 0;
3521 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3524 /* Allocate an XpmColorSymbol array. */
3525 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3526 xpm_syms
= alloca (size
);
3527 memset (xpm_syms
, 0, size
);
3528 attrs
.colorsymbols
= xpm_syms
;
3530 /* Fill the color symbol array. */
3531 for (tail
= color_symbols
, i
= 0;
3533 ++i
, tail
= XCDR (tail
))
3537 char *empty_string
= (char *) "";
3539 if (!CONSP (XCAR (tail
)))
3541 xpm_syms
[i
].name
= empty_string
;
3542 xpm_syms
[i
].value
= empty_string
;
3545 name
= XCAR (XCAR (tail
));
3546 color
= XCDR (XCAR (tail
));
3549 xpm_syms
[i
].name
= alloca (SCHARS (name
) + 1);
3550 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3553 xpm_syms
[i
].name
= empty_string
;
3554 if (STRINGP (color
))
3556 xpm_syms
[i
].value
= alloca (SCHARS (color
) + 1);
3557 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3560 xpm_syms
[i
].value
= empty_string
;
3564 /* Create a pixmap for the image, either from a file, or from a
3565 string buffer containing data in the same format as an XPM file. */
3566 #ifdef ALLOC_XPM_COLORS
3567 xpm_init_color_cache (f
, &attrs
);
3570 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3574 HDC frame_dc
= get_frame_dc (f
);
3575 hdc
= CreateCompatibleDC (frame_dc
);
3576 release_frame_dc (f
, frame_dc
);
3578 #endif /* HAVE_NTGUI */
3580 if (STRINGP (specified_file
))
3582 Lisp_Object file
= x_find_image_file (specified_file
);
3583 if (!STRINGP (file
))
3585 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3586 #ifdef ALLOC_XPM_COLORS
3587 xpm_free_color_cache ();
3593 /* XpmReadFileToPixmap is not available in the Windows port of
3594 libxpm. But XpmReadFileToImage almost does what we want. */
3595 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3596 &xpm_image
, &xpm_mask
,
3599 rc
= XpmReadFileToImage (FRAME_X_DISPLAY (f
), SSDATA (file
),
3600 &img
->ximg
, &img
->mask_img
,
3602 #endif /* HAVE_NTGUI */
3606 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3607 if (!STRINGP (buffer
))
3609 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3610 #ifdef ALLOC_XPM_COLORS
3611 xpm_free_color_cache ();
3616 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3617 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3618 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3619 &xpm_image
, &xpm_mask
,
3622 rc
= XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f
), SSDATA (buffer
),
3623 &img
->ximg
, &img
->mask_img
,
3625 #endif /* HAVE_NTGUI */
3628 #ifdef HAVE_X_WINDOWS
3629 if (rc
== XpmSuccess
)
3631 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3632 img
->ximg
->width
, img
->ximg
->height
,
3634 if (img
->pixmap
== NO_PIXMAP
)
3636 x_clear_image (f
, img
);
3639 else if (img
->mask_img
)
3641 img
->mask
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3642 img
->mask_img
->width
,
3643 img
->mask_img
->height
,
3644 img
->mask_img
->depth
);
3645 if (img
->mask
== NO_PIXMAP
)
3647 x_clear_image (f
, img
);
3654 if (rc
== XpmSuccess
)
3656 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3657 img
->colors
= colors_in_color_table (&img
->ncolors
);
3658 #else /* not ALLOC_XPM_COLORS */
3662 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3663 plus some duplicate attributes. */
3664 if (xpm_image
&& xpm_image
->bitmap
)
3666 img
->pixmap
= xpm_image
->bitmap
;
3667 /* XImageFree in libXpm frees XImage struct without destroying
3668 the bitmap, which is what we want. */
3669 fn_XImageFree (xpm_image
);
3671 if (xpm_mask
&& xpm_mask
->bitmap
)
3673 /* The mask appears to be inverted compared with what we expect.
3674 TODO: invert our expectations. See other places where we
3675 have to invert bits because our idea of masks is backwards. */
3677 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3679 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3680 SelectObject (hdc
, old_obj
);
3682 img
->mask
= xpm_mask
->bitmap
;
3683 fn_XImageFree (xpm_mask
);
3688 #endif /* HAVE_NTGUI */
3690 /* Remember allocated colors. */
3691 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3692 img
->ncolors
= attrs
.nalloc_pixels
;
3693 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3695 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3696 #ifdef DEBUG_X_COLORS
3697 register_color (img
->colors
[i
]);
3700 #endif /* not ALLOC_XPM_COLORS */
3702 img
->width
= attrs
.width
;
3703 img
->height
= attrs
.height
;
3704 eassert (img
->width
> 0 && img
->height
> 0);
3706 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3708 fn_XpmFreeAttributes (&attrs
);
3710 XpmFreeAttributes (&attrs
);
3711 #endif /* HAVE_NTGUI */
3713 #ifdef HAVE_X_WINDOWS
3714 /* Maybe fill in the background field while we have ximg handy. */
3715 IMAGE_BACKGROUND (img
, f
, img
->ximg
);
3717 /* Fill in the background_transparent field while we have the
3719 image_background_transparent (img
, f
, img
->mask_img
);
3726 #endif /* HAVE_NTGUI */
3731 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3734 case XpmFileInvalid
:
3735 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3739 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3742 case XpmColorFailed
:
3743 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3747 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3752 #ifdef ALLOC_XPM_COLORS
3753 xpm_free_color_cache ();
3755 return rc
== XpmSuccess
;
3758 #endif /* HAVE_XPM */
3760 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3762 /* XPM support functions for NS where libxpm is not available.
3763 Only XPM version 3 (without any extensions) is supported. */
3765 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3767 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3768 const unsigned char *, int);
3769 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3771 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3772 const unsigned char *, int);
3774 /* Tokens returned from xpm_scan. */
3783 /* Scan an XPM data and return a character (< 256) or a token defined
3784 by enum xpm_token above. *S and END are the start (inclusive) and
3785 the end (exclusive) addresses of the data, respectively. Advance
3786 *S while scanning. If token is either XPM_TK_IDENT or
3787 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3788 length of the corresponding token, respectively. */
3791 xpm_scan (const unsigned char **s
,
3792 const unsigned char *end
,
3793 const unsigned char **beg
,
3800 /* Skip white-space. */
3801 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
3804 /* gnus-pointer.xpm uses '-' in its identifier.
3805 sb-dir-plus.xpm uses '+' in its identifier. */
3806 if (c_isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3810 && (c
= **s
, c_isalnum (c
)
3811 || c
== '_' || c
== '-' || c
== '+'))
3814 return XPM_TK_IDENT
;
3819 while (*s
< end
&& **s
!= '"')
3824 return XPM_TK_STRING
;
3828 if (*s
< end
&& **s
== '*')
3830 /* C-style comment. */
3834 while (*s
< end
&& *(*s
)++ != '*')
3837 while (*s
< end
&& **s
!= '/');
3851 /* Functions for color table lookup in XPM data. A key is a string
3852 specifying the color of each pixel in XPM data. A value is either
3853 an integer that specifies a pixel color, Qt that specifies
3854 transparency, or Qnil for the unspecified color. If the length of
3855 the key string is one, a vector is used as a table. Otherwise, a
3856 hash table is used. */
3859 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3860 const unsigned char *,
3863 Lisp_Object (**get_func
) (Lisp_Object
,
3864 const unsigned char *,
3867 *put_func
= xpm_put_color_table_v
;
3868 *get_func
= xpm_get_color_table_v
;
3869 return Fmake_vector (make_number (256), Qnil
);
3873 xpm_put_color_table_v (Lisp_Object color_table
,
3874 const unsigned char *chars_start
,
3878 ASET (color_table
, *chars_start
, color
);
3882 xpm_get_color_table_v (Lisp_Object color_table
,
3883 const unsigned char *chars_start
,
3886 return AREF (color_table
, *chars_start
);
3890 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3891 const unsigned char *,
3894 Lisp_Object (**get_func
) (Lisp_Object
,
3895 const unsigned char *,
3898 *put_func
= xpm_put_color_table_h
;
3899 *get_func
= xpm_get_color_table_h
;
3900 return make_hash_table (hashtest_equal
, make_number (DEFAULT_HASH_SIZE
),
3901 make_float (DEFAULT_REHASH_SIZE
),
3902 make_float (DEFAULT_REHASH_THRESHOLD
),
3907 xpm_put_color_table_h (Lisp_Object color_table
,
3908 const unsigned char *chars_start
,
3912 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3913 EMACS_UINT hash_code
;
3914 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3916 hash_lookup (table
, chars
, &hash_code
);
3917 hash_put (table
, chars
, color
, hash_code
);
3921 xpm_get_color_table_h (Lisp_Object color_table
,
3922 const unsigned char *chars_start
,
3925 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3927 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3929 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3932 enum xpm_color_key
{
3940 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3943 xpm_str_to_color_key (const char *s
)
3948 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3950 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3956 xpm_load_image (struct frame
*f
,
3958 const unsigned char *contents
,
3959 const unsigned char *end
)
3961 const unsigned char *s
= contents
, *beg
, *str
;
3962 unsigned char buffer
[BUFSIZ
];
3963 int width
, height
, x
, y
;
3964 int num_colors
, chars_per_pixel
;
3967 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3968 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3969 Lisp_Object frame
, color_symbols
, color_table
;
3972 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3975 LA1 = xpm_scan (&s, end, &beg, &len)
3977 #define expect(TOKEN) \
3978 if (LA1 != (TOKEN)) \
3983 #define expect_ident(IDENT) \
3984 if (LA1 == XPM_TK_IDENT \
3985 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3990 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3994 expect_ident ("static");
3995 expect_ident ("char");
3997 expect (XPM_TK_IDENT
);
4002 expect (XPM_TK_STRING
);
4005 memcpy (buffer
, beg
, len
);
4007 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
4008 &num_colors
, &chars_per_pixel
) != 4
4009 || width
<= 0 || height
<= 0
4010 || num_colors
<= 0 || chars_per_pixel
<= 0)
4013 if (!check_image_size (f
, width
, height
))
4015 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
4019 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0)
4021 || !image_create_x_image_and_pixmap (f
, img
, width
, height
, 1,
4026 image_error ("Image too large", Qnil
, Qnil
);
4032 XSETFRAME (frame
, f
);
4033 if (!NILP (Fxw_display_color_p (frame
)))
4034 best_key
= XPM_COLOR_KEY_C
;
4035 else if (!NILP (Fx_display_grayscale_p (frame
)))
4036 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
4037 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
4039 best_key
= XPM_COLOR_KEY_M
;
4041 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
4042 if (chars_per_pixel
== 1)
4043 color_table
= xpm_make_color_table_v (&put_color_table
,
4046 color_table
= xpm_make_color_table_h (&put_color_table
,
4049 while (num_colors
-- > 0)
4051 char *color
, *max_color
;
4052 int key
, next_key
, max_key
= 0;
4053 Lisp_Object symbol_color
= Qnil
, color_val
;
4056 expect (XPM_TK_STRING
);
4057 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
4059 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
4060 buffer
[len
- chars_per_pixel
] = '\0';
4062 str
= strtok (buffer
, " \t");
4065 key
= xpm_str_to_color_key (str
);
4070 color
= strtok (NULL
, " \t");
4074 while ((str
= strtok (NULL
, " \t")) != NULL
)
4076 next_key
= xpm_str_to_color_key (str
);
4079 color
[strlen (color
)] = ' ';
4082 if (key
== XPM_COLOR_KEY_S
)
4084 if (NILP (symbol_color
))
4085 symbol_color
= build_string (color
);
4087 else if (max_key
< key
&& key
<= best_key
)
4097 if (!NILP (color_symbols
) && !NILP (symbol_color
))
4099 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
4101 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
4103 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
4105 else if (x_defined_color (f
, SSDATA (XCDR (specified_color
)),
4107 color_val
= make_number (cdef
.pixel
);
4110 if (NILP (color_val
) && max_key
> 0)
4112 if (xstrcasecmp (max_color
, "None") == 0)
4114 else if (x_defined_color (f
, max_color
, &cdef
, 0))
4115 color_val
= make_number (cdef
.pixel
);
4117 if (!NILP (color_val
))
4118 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
4123 for (y
= 0; y
< height
; y
++)
4125 expect (XPM_TK_STRING
);
4127 if (len
< width
* chars_per_pixel
)
4129 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
4131 Lisp_Object color_val
=
4132 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4134 XPutPixel (ximg
, x
, y
,
4135 (INTEGERP (color_val
) ? XINT (color_val
)
4136 : FRAME_FOREGROUND_PIXEL (f
)));
4138 XPutPixel (mask_img
, x
, y
,
4139 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4140 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4142 if (EQ (color_val
, Qt
))
4143 ns_set_alpha (ximg
, x
, y
, 0);
4151 img
->height
= height
;
4153 /* Maybe fill in the background field while we have ximg handy. */
4154 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4155 IMAGE_BACKGROUND (img
, f
, ximg
);
4157 image_put_x_image (f
, img
, ximg
, 0);
4161 /* Fill in the background_transparent field while we have the
4163 image_background_transparent (img
, f
, mask_img
);
4165 image_put_x_image (f
, img
, mask_img
, 1);
4169 x_destroy_x_image (mask_img
);
4170 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
4176 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4177 x_destroy_x_image (ximg
);
4178 x_destroy_x_image (mask_img
);
4179 x_clear_image (f
, img
);
4188 xpm_load (struct frame
*f
,
4192 Lisp_Object file_name
;
4194 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4195 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4196 if (STRINGP (file_name
))
4199 unsigned char *contents
;
4202 file
= x_find_image_file (file_name
);
4203 if (!STRINGP (file
))
4205 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4209 contents
= slurp_file (SSDATA (file
), &size
);
4210 if (contents
== NULL
)
4212 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4216 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4223 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4224 if (!STRINGP (data
))
4226 image_error ("Invalid image data `%s'", data
, Qnil
);
4229 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4230 SDATA (data
) + SBYTES (data
));
4236 #endif /* HAVE_NS && !HAVE_XPM */
4240 /***********************************************************************
4242 ***********************************************************************/
4244 #ifdef COLOR_TABLE_SUPPORT
4246 /* An entry in the color table mapping an RGB color to a pixel color. */
4251 unsigned long pixel
;
4253 /* Next in color table collision list. */
4254 struct ct_color
*next
;
4257 /* The bucket vector size to use. Must be prime. */
4261 /* Value is a hash of the RGB color given by R, G, and B. */
4263 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4265 /* The color hash table. */
4267 static struct ct_color
**ct_table
;
4269 /* Number of entries in the color table. */
4271 static int ct_colors_allocated
;
4274 ct_colors_allocated_max
=
4276 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4279 /* Initialize the color table. */
4282 init_color_table (void)
4284 int size
= CT_SIZE
* sizeof (*ct_table
);
4285 ct_table
= xzalloc (size
);
4286 ct_colors_allocated
= 0;
4290 /* Free memory associated with the color table. */
4293 free_color_table (void)
4296 struct ct_color
*p
, *next
;
4298 for (i
= 0; i
< CT_SIZE
; ++i
)
4299 for (p
= ct_table
[i
]; p
; p
= next
)
4310 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4311 entry for that color already is in the color table, return the
4312 pixel color of that entry. Otherwise, allocate a new color for R,
4313 G, B, and make an entry in the color table. */
4315 static unsigned long
4316 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4318 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4319 int i
= hash
% CT_SIZE
;
4321 Display_Info
*dpyinfo
;
4323 /* Handle TrueColor visuals specially, which improves performance by
4324 two orders of magnitude. Freeing colors on TrueColor visuals is
4325 a nop, and pixel colors specify RGB values directly. See also
4326 the Xlib spec, chapter 3.1. */
4327 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4328 if (dpyinfo
->red_bits
> 0)
4330 unsigned long pr
, pg
, pb
;
4332 /* Apply gamma-correction like normal color allocation does. */
4336 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4337 gamma_correct (f
, &color
);
4338 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4341 /* Scale down RGB values to the visual's bits per RGB, and shift
4342 them to the right position in the pixel color. Note that the
4343 original RGB values are 16-bit values, as usual in X. */
4344 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4345 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4346 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4348 /* Assemble the pixel color. */
4349 return pr
| pg
| pb
;
4352 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4353 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4359 #ifdef HAVE_X_WINDOWS
4367 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4368 return FRAME_FOREGROUND_PIXEL (f
);
4370 #ifdef HAVE_X_WINDOWS
4375 cmap
= FRAME_X_COLORMAP (f
);
4376 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4379 ++ct_colors_allocated
;
4380 p
= xmalloc (sizeof *p
);
4384 p
->pixel
= color
.pixel
;
4385 p
->next
= ct_table
[i
];
4389 return FRAME_FOREGROUND_PIXEL (f
);
4393 color
= PALETTERGB (r
, g
, b
);
4395 color
= RGB_TO_ULONG (r
, g
, b
);
4396 #endif /* HAVE_NTGUI */
4397 ++ct_colors_allocated
;
4398 p
= xmalloc (sizeof *p
);
4403 p
->next
= ct_table
[i
];
4405 #endif /* HAVE_X_WINDOWS */
4413 /* Look up pixel color PIXEL which is used on frame F in the color
4414 table. If not already present, allocate it. Value is PIXEL. */
4416 static unsigned long
4417 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4419 int i
= pixel
% CT_SIZE
;
4422 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4423 if (p
->pixel
== pixel
)
4432 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4433 return FRAME_FOREGROUND_PIXEL (f
);
4435 #ifdef HAVE_X_WINDOWS
4436 cmap
= FRAME_X_COLORMAP (f
);
4437 color
.pixel
= pixel
;
4438 x_query_color (f
, &color
);
4439 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4442 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4443 color
.pixel
= pixel
;
4444 XQueryColor (NULL
, cmap
, &color
);
4445 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4447 #endif /* HAVE_X_WINDOWS */
4451 ++ct_colors_allocated
;
4453 p
= xmalloc (sizeof *p
);
4458 p
->next
= ct_table
[i
];
4462 return FRAME_FOREGROUND_PIXEL (f
);
4468 /* Value is a vector of all pixel colors contained in the color table,
4469 allocated via xmalloc. Set *N to the number of colors. */
4471 static unsigned long *
4472 colors_in_color_table (int *n
)
4476 unsigned long *colors
;
4478 if (ct_colors_allocated
== 0)
4485 colors
= xmalloc (ct_colors_allocated
* sizeof *colors
);
4486 *n
= ct_colors_allocated
;
4488 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4489 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4490 colors
[j
++] = p
->pixel
;
4496 #else /* COLOR_TABLE_SUPPORT */
4498 static unsigned long
4499 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4501 unsigned long pixel
;
4504 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4505 #endif /* HAVE_NTGUI */
4508 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4509 #endif /* HAVE_NS */
4514 init_color_table (void)
4517 #endif /* COLOR_TABLE_SUPPORT */
4520 /***********************************************************************
4522 ***********************************************************************/
4524 /* Edge detection matrices for different edge-detection
4527 static int emboss_matrix
[9] = {
4529 2, -1, 0, /* y - 1 */
4531 0, 1, -2 /* y + 1 */
4534 static int laplace_matrix
[9] = {
4536 1, 0, 0, /* y - 1 */
4538 0, 0, -1 /* y + 1 */
4541 /* Value is the intensity of the color whose red/green/blue values
4544 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4547 /* On frame F, return an array of XColor structures describing image
4548 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4549 means also fill the red/green/blue members of the XColor
4550 structures. Value is a pointer to the array of XColors structures,
4551 allocated with xmalloc; it must be freed by the caller. */
4554 x_to_xcolors (struct frame
*f
, struct image
*img
, bool rgb_p
)
4558 XImagePtr_or_DC ximg
;
4561 #endif /* HAVE_NTGUI */
4563 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4564 memory_full (SIZE_MAX
);
4565 colors
= xmalloc (sizeof *colors
* img
->width
* img
->height
);
4567 /* Get the X image or create a memory device context for IMG. */
4568 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
4570 /* Fill the `pixel' members of the XColor array. I wished there
4571 were an easy and portable way to circumvent XGetPixel. */
4573 for (y
= 0; y
< img
->height
; ++y
)
4575 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4577 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4578 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4580 x_query_colors (f
, row
, img
->width
);
4584 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4586 /* W32_TODO: palette support needed here? */
4587 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4590 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4591 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4592 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4595 #endif /* HAVE_X_WINDOWS */
4598 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
4605 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4606 created with CreateDIBSection, with the pointer to the bit values
4607 stored in ximg->data. */
4610 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4612 int width
= ximg
->info
.bmiHeader
.biWidth
;
4613 unsigned char * pixel
;
4615 /* True color images. */
4616 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4618 int rowbytes
= width
* 3;
4619 /* Ensure scanlines are aligned on 4 byte boundaries. */
4621 rowbytes
+= 4 - (rowbytes
% 4);
4623 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4624 /* Windows bitmaps are in BGR order. */
4625 *pixel
= GetBValue (color
);
4626 *(pixel
+ 1) = GetGValue (color
);
4627 *(pixel
+ 2) = GetRValue (color
);
4629 /* Monochrome images. */
4630 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4632 int rowbytes
= width
/ 8;
4633 /* Ensure scanlines are aligned on 4 byte boundaries. */
4635 rowbytes
+= 4 - (rowbytes
% 4);
4636 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4637 /* Filter out palette info. */
4638 if (color
& 0x00ffffff)
4639 *pixel
= *pixel
| (1 << x
% 8);
4641 *pixel
= *pixel
& ~(1 << x
% 8);
4644 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4647 #endif /* HAVE_NTGUI */
4649 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4650 RGB members are set. F is the frame on which this all happens.
4651 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4654 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4657 XImagePtr oimg
= NULL
;
4660 init_color_table ();
4662 x_clear_image_1 (f
, img
, CLEAR_IMAGE_PIXMAP
| CLEAR_IMAGE_COLORS
);
4663 image_create_x_image_and_pixmap (f
, img
, img
->width
, img
->height
, 0,
4666 for (y
= 0; y
< img
->height
; ++y
)
4667 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4669 unsigned long pixel
;
4670 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4671 XPutPixel (oimg
, x
, y
, pixel
);
4676 image_put_x_image (f
, img
, oimg
, 0);
4677 #ifdef COLOR_TABLE_SUPPORT
4678 img
->colors
= colors_in_color_table (&img
->ncolors
);
4679 free_color_table ();
4680 #endif /* COLOR_TABLE_SUPPORT */
4684 /* On frame F, perform edge-detection on image IMG.
4686 MATRIX is a nine-element array specifying the transformation
4687 matrix. See emboss_matrix for an example.
4689 COLOR_ADJUST is a color adjustment added to each pixel of the
4693 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4695 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4699 for (i
= sum
= 0; i
< 9; ++i
)
4700 sum
+= eabs (matrix
[i
]);
4702 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4704 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4705 memory_full (SIZE_MAX
);
4706 new = xmalloc (sizeof *new * img
->width
* img
->height
);
4708 for (y
= 0; y
< img
->height
; ++y
)
4710 p
= COLOR (new, 0, y
);
4711 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4712 p
= COLOR (new, img
->width
- 1, y
);
4713 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4716 for (x
= 1; x
< img
->width
- 1; ++x
)
4718 p
= COLOR (new, x
, 0);
4719 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4720 p
= COLOR (new, x
, img
->height
- 1);
4721 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4724 for (y
= 1; y
< img
->height
- 1; ++y
)
4726 p
= COLOR (new, 1, y
);
4728 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4730 int r
, g
, b
, yy
, xx
;
4733 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4734 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4737 XColor
*t
= COLOR (colors
, xx
, yy
);
4738 r
+= matrix
[i
] * t
->red
;
4739 g
+= matrix
[i
] * t
->green
;
4740 b
+= matrix
[i
] * t
->blue
;
4743 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4744 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4745 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4746 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4751 x_from_xcolors (f
, img
, new);
4757 /* Perform the pre-defined `emboss' edge-detection on image IMG
4761 x_emboss (struct frame
*f
, struct image
*img
)
4763 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4767 /* Transform image IMG which is used on frame F with a Laplace
4768 edge-detection algorithm. The result is an image that can be used
4769 to draw disabled buttons, for example. */
4772 x_laplace (struct frame
*f
, struct image
*img
)
4774 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4778 /* Perform edge-detection on image IMG on frame F, with specified
4779 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4781 MATRIX must be either
4783 - a list of at least 9 numbers in row-major form
4784 - a vector of at least 9 numbers
4786 COLOR_ADJUST nil means use a default; otherwise it must be a
4790 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4791 Lisp_Object color_adjust
)
4799 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4800 ++i
, matrix
= XCDR (matrix
))
4801 trans
[i
] = XFLOATINT (XCAR (matrix
));
4803 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4805 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4806 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4809 if (NILP (color_adjust
))
4810 color_adjust
= make_number (0xffff / 2);
4812 if (i
== 9 && NUMBERP (color_adjust
))
4813 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4817 /* Transform image IMG on frame F so that it looks disabled. */
4820 x_disable_image (struct frame
*f
, struct image
*img
)
4822 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4824 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4826 int n_planes
= dpyinfo
->n_planes
;
4827 #endif /* HAVE_NTGUI */
4831 /* Color (or grayscale). Convert to gray, and equalize. Just
4832 drawing such images with a stipple can look very odd, so
4833 we're using this method instead. */
4834 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4836 const int h
= 15000;
4837 const int l
= 30000;
4839 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4843 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4844 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4845 p
->red
= p
->green
= p
->blue
= i2
;
4848 x_from_xcolors (f
, img
, colors
);
4851 /* Draw a cross over the disabled image, if we must or if we
4853 if (n_planes
< 2 || cross_disabled_images
)
4856 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4858 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4860 Display
*dpy
= FRAME_X_DISPLAY (f
);
4863 image_sync_to_pixmaps (f
, img
);
4864 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4865 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4866 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4867 img
->width
- 1, img
->height
- 1);
4868 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4874 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4875 XSetForeground (dpy
, gc
, MaskForeground (f
));
4876 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4877 img
->width
- 1, img
->height
- 1);
4878 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4882 #endif /* !HAVE_NS */
4887 hdc
= get_frame_dc (f
);
4888 bmpdc
= CreateCompatibleDC (hdc
);
4889 release_frame_dc (f
, hdc
);
4891 prev
= SelectObject (bmpdc
, img
->pixmap
);
4893 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4894 MoveToEx (bmpdc
, 0, 0, NULL
);
4895 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4896 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4897 LineTo (bmpdc
, img
->width
- 1, 0);
4901 SelectObject (bmpdc
, img
->mask
);
4902 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4903 MoveToEx (bmpdc
, 0, 0, NULL
);
4904 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4905 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4906 LineTo (bmpdc
, img
->width
- 1, 0);
4908 SelectObject (bmpdc
, prev
);
4910 #endif /* HAVE_NTGUI */
4915 /* Build a mask for image IMG which is used on frame F. FILE is the
4916 name of an image file, for error messages. HOW determines how to
4917 determine the background color of IMG. If it is a list '(R G B)',
4918 with R, G, and B being integers >= 0, take that as the color of the
4919 background. Otherwise, determine the background color of IMG
4923 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4925 XImagePtr_or_DC ximg
;
4932 #endif /* HAVE_NTGUI */
4934 bool rc
, use_img_background
;
4935 unsigned long bg
= 0;
4938 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
4942 /* Create an image and pixmap serving as mask. */
4943 rc
= image_create_x_image_and_pixmap (f
, img
, img
->width
, img
->height
, 1,
4947 #endif /* !HAVE_NS */
4949 /* Create the bit array serving as mask. */
4950 row_width
= (img
->width
+ 7) / 8;
4951 mask_img
= xzalloc (row_width
* img
->height
);
4952 #endif /* HAVE_NTGUI */
4954 /* Get the X image or create a memory device context for IMG. */
4955 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
4957 /* Determine the background color of ximg. If HOW is `(R G B)'
4958 take that as color. Otherwise, use the image's background color. */
4959 use_img_background
= 1;
4965 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4967 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4971 if (i
== 3 && NILP (how
))
4973 char color_name
[30];
4974 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4977 0x00ffffff & /* Filter out palette info. */
4978 #endif /* HAVE_NTGUI */
4979 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4980 use_img_background
= 0;
4984 if (use_img_background
)
4985 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4987 /* Set all bits in mask_img to 1 whose color in ximg is different
4988 from the background color bg. */
4990 for (y
= 0; y
< img
->height
; ++y
)
4991 for (x
= 0; x
< img
->width
; ++x
)
4993 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4994 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4996 if (XGetPixel (ximg
, x
, y
) == bg
)
4997 ns_set_alpha (ximg
, x
, y
, 0);
4998 #endif /* HAVE_NS */
5000 /* Fill in the background_transparent field while we have the mask handy. */
5001 image_background_transparent (img
, f
, mask_img
);
5003 /* Put mask_img into the image. */
5004 image_put_x_image (f
, img
, mask_img
, 1);
5005 #endif /* !HAVE_NS */
5007 for (y
= 0; y
< img
->height
; ++y
)
5008 for (x
= 0; x
< img
->width
; ++x
)
5010 COLORREF p
= GetPixel (ximg
, x
, y
);
5012 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
5015 /* Create the mask image. */
5016 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
5018 /* Fill in the background_transparent field while we have the mask handy. */
5019 SelectObject (ximg
, img
->mask
);
5020 image_background_transparent (img
, f
, ximg
);
5022 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
5024 #endif /* HAVE_NTGUI */
5026 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
5030 /***********************************************************************
5031 PBM (mono, gray, color)
5032 ***********************************************************************/
5034 static bool pbm_image_p (Lisp_Object object
);
5035 static bool pbm_load (struct frame
*f
, struct image
*img
);
5037 /* The symbol `pbm' identifying images of this type. */
5039 static Lisp_Object Qpbm
;
5041 /* Indices of image specification fields in gs_format, below. */
5043 enum pbm_keyword_index
5059 /* Vector of image_keyword structures describing the format
5060 of valid user-defined image specifications. */
5062 static const struct image_keyword pbm_format
[PBM_LAST
] =
5064 {":type", IMAGE_SYMBOL_VALUE
, 1},
5065 {":file", IMAGE_STRING_VALUE
, 0},
5066 {":data", IMAGE_STRING_VALUE
, 0},
5067 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5068 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5069 {":relief", IMAGE_INTEGER_VALUE
, 0},
5070 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5071 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5072 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5073 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
5074 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5077 /* Structure describing the image type `pbm'. */
5079 static struct image_type pbm_type
=
5090 /* Return true if OBJECT is a valid PBM image specification. */
5093 pbm_image_p (Lisp_Object object
)
5095 struct image_keyword fmt
[PBM_LAST
];
5097 memcpy (fmt
, pbm_format
, sizeof fmt
);
5099 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
5102 /* Must specify either :data or :file. */
5103 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5107 /* Scan a decimal number from *S and return it. Advance *S while
5108 reading the number. END is the end of the string. Value is -1 at
5112 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5114 int c
= 0, val
= -1;
5118 /* Skip white-space. */
5119 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
5124 /* Skip comment to end of line. */
5125 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5128 else if (c_isdigit (c
))
5130 /* Read decimal number. */
5132 while (*s
< end
&& (c
= *(*s
)++, c_isdigit (c
)))
5133 val
= 10 * val
+ c
- '0';
5144 /* Load PBM image IMG for use on frame F. */
5147 pbm_load (struct frame
*f
, struct image
*img
)
5151 int width
, height
, max_color_idx
= 0;
5153 Lisp_Object file
, specified_file
;
5154 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5155 unsigned char *contents
= NULL
;
5156 unsigned char *end
, *p
;
5159 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5161 if (STRINGP (specified_file
))
5163 file
= x_find_image_file (specified_file
);
5164 if (!STRINGP (file
))
5166 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5170 contents
= slurp_file (SSDATA (file
), &size
);
5171 if (contents
== NULL
)
5173 image_error ("Error reading `%s'", file
, Qnil
);
5178 end
= contents
+ size
;
5183 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5184 if (!STRINGP (data
))
5186 image_error ("Invalid image data `%s'", data
, Qnil
);
5190 end
= p
+ SBYTES (data
);
5193 /* Check magic number. */
5194 if (end
- p
< 2 || *p
++ != 'P')
5196 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5205 raw_p
= 0, type
= PBM_MONO
;
5209 raw_p
= 0, type
= PBM_GRAY
;
5213 raw_p
= 0, type
= PBM_COLOR
;
5217 raw_p
= 1, type
= PBM_MONO
;
5221 raw_p
= 1, type
= PBM_GRAY
;
5225 raw_p
= 1, type
= PBM_COLOR
;
5229 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5233 /* Read width, height, maximum color-component. Characters
5234 starting with `#' up to the end of a line are ignored. */
5235 width
= pbm_scan_number (&p
, end
);
5236 height
= pbm_scan_number (&p
, end
);
5238 if (type
!= PBM_MONO
)
5240 max_color_idx
= pbm_scan_number (&p
, end
);
5241 if (max_color_idx
> 65535 || max_color_idx
< 0)
5243 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5248 if (!check_image_size (f
, width
, height
))
5250 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5254 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
5257 /* Initialize the color hash table. */
5258 init_color_table ();
5260 if (type
== PBM_MONO
)
5263 struct image_keyword fmt
[PBM_LAST
];
5264 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5265 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5267 /* Parse the image specification. */
5268 memcpy (fmt
, pbm_format
, sizeof fmt
);
5269 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5271 /* Get foreground and background colors, maybe allocate colors. */
5272 if (fmt
[PBM_FOREGROUND
].count
5273 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5274 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5275 if (fmt
[PBM_BACKGROUND
].count
5276 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5278 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5279 img
->background
= bg
;
5280 img
->background_valid
= 1;
5283 for (y
= 0; y
< height
; ++y
)
5284 for (x
= 0; x
< width
; ++x
)
5292 x_destroy_x_image (ximg
);
5293 x_clear_image (f
, img
);
5294 image_error ("Invalid image size in image `%s'",
5304 g
= pbm_scan_number (&p
, end
);
5306 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5311 int expected_size
= height
* width
;
5312 if (max_color_idx
> 255)
5314 if (type
== PBM_COLOR
)
5317 if (raw_p
&& p
+ expected_size
> end
)
5319 x_destroy_x_image (ximg
);
5320 x_clear_image (f
, img
);
5321 image_error ("Invalid image size in image `%s'",
5326 for (y
= 0; y
< height
; ++y
)
5327 for (x
= 0; x
< width
; ++x
)
5331 if (type
== PBM_GRAY
&& raw_p
)
5334 if (max_color_idx
> 255)
5335 r
= g
= b
= r
* 256 + *p
++;
5337 else if (type
== PBM_GRAY
)
5338 r
= g
= b
= pbm_scan_number (&p
, end
);
5342 if (max_color_idx
> 255)
5345 if (max_color_idx
> 255)
5348 if (max_color_idx
> 255)
5353 r
= pbm_scan_number (&p
, end
);
5354 g
= pbm_scan_number (&p
, end
);
5355 b
= pbm_scan_number (&p
, end
);
5358 if (r
< 0 || g
< 0 || b
< 0)
5360 x_destroy_x_image (ximg
);
5361 image_error ("Invalid pixel value in image `%s'",
5366 /* RGB values are now in the range 0..max_color_idx.
5367 Scale this to the range 0..0xffff supported by X. */
5368 r
= (double) r
* 65535 / max_color_idx
;
5369 g
= (double) g
* 65535 / max_color_idx
;
5370 b
= (double) b
* 65535 / max_color_idx
;
5371 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5375 #ifdef COLOR_TABLE_SUPPORT
5376 /* Store in IMG->colors the colors allocated for the image, and
5377 free the color table. */
5378 img
->colors
= colors_in_color_table (&img
->ncolors
);
5379 free_color_table ();
5380 #endif /* COLOR_TABLE_SUPPORT */
5383 img
->height
= height
;
5385 /* Maybe fill in the background field while we have ximg handy. */
5387 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5388 /* Casting avoids a GCC warning. */
5389 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5391 /* Put ximg into the image. */
5392 image_put_x_image (f
, img
, ximg
, 0);
5394 /* X and W32 versions did it here, MAC version above. ++kfs
5396 img->height = height; */
5403 /***********************************************************************
5405 ***********************************************************************/
5407 #if defined (HAVE_PNG) || defined (HAVE_NS)
5409 /* Function prototypes. */
5411 static bool png_image_p (Lisp_Object object
);
5412 static bool png_load (struct frame
*f
, struct image
*img
);
5414 /* The symbol `png' identifying images of this type. */
5416 static Lisp_Object Qpng
;
5418 /* Indices of image specification fields in png_format, below. */
5420 enum png_keyword_index
5435 /* Vector of image_keyword structures describing the format
5436 of valid user-defined image specifications. */
5438 static const struct image_keyword png_format
[PNG_LAST
] =
5440 {":type", IMAGE_SYMBOL_VALUE
, 1},
5441 {":data", IMAGE_STRING_VALUE
, 0},
5442 {":file", IMAGE_STRING_VALUE
, 0},
5443 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5444 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5445 {":relief", IMAGE_INTEGER_VALUE
, 0},
5446 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5447 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5448 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5449 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5452 #if defined HAVE_NTGUI && defined WINDOWSNT
5453 static bool init_png_functions (void);
5455 #define init_png_functions NULL
5458 /* Structure describing the image type `png'. */
5460 static struct image_type png_type
=
5470 /* Return true if OBJECT is a valid PNG image specification. */
5473 png_image_p (Lisp_Object object
)
5475 struct image_keyword fmt
[PNG_LAST
];
5476 memcpy (fmt
, png_format
, sizeof fmt
);
5478 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5481 /* Must specify either the :data or :file keyword. */
5482 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5485 #endif /* HAVE_PNG || HAVE_NS */
5491 /* PNG library details. */
5493 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5494 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5495 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5496 png_error_ptr
, png_error_ptr
));
5497 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5498 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5499 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5500 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5501 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5502 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5503 png_uint_32
*, png_uint_32
*,
5504 int *, int *, int *, int *, int *));
5505 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5506 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5507 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5508 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5509 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5511 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5512 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5513 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5514 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5515 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5516 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5517 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5519 #if (PNG_LIBPNG_VER >= 10500)
5520 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5521 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5522 #endif /* libpng version >= 1.5 */
5525 init_png_functions (void)
5529 if (!(library
= w32_delayed_load (Qpng
)))
5532 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5533 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5534 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5535 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5536 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5537 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5538 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5539 LOAD_IMGLIB_FN (library
, png_read_info
);
5540 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5541 LOAD_IMGLIB_FN (library
, png_get_valid
);
5542 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5543 LOAD_IMGLIB_FN (library
, png_set_expand
);
5544 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5545 LOAD_IMGLIB_FN (library
, png_set_background
);
5546 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5547 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5548 LOAD_IMGLIB_FN (library
, png_get_channels
);
5549 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5550 LOAD_IMGLIB_FN (library
, png_read_image
);
5551 LOAD_IMGLIB_FN (library
, png_read_end
);
5552 LOAD_IMGLIB_FN (library
, png_error
);
5554 #if (PNG_LIBPNG_VER >= 10500)
5555 LOAD_IMGLIB_FN (library
, png_longjmp
);
5556 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5557 #endif /* libpng version >= 1.5 */
5563 #define fn_png_get_io_ptr png_get_io_ptr
5564 #define fn_png_sig_cmp png_sig_cmp
5565 #define fn_png_create_read_struct png_create_read_struct
5566 #define fn_png_create_info_struct png_create_info_struct
5567 #define fn_png_destroy_read_struct png_destroy_read_struct
5568 #define fn_png_set_read_fn png_set_read_fn
5569 #define fn_png_set_sig_bytes png_set_sig_bytes
5570 #define fn_png_read_info png_read_info
5571 #define fn_png_get_IHDR png_get_IHDR
5572 #define fn_png_get_valid png_get_valid
5573 #define fn_png_set_strip_16 png_set_strip_16
5574 #define fn_png_set_expand png_set_expand
5575 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5576 #define fn_png_set_background png_set_background
5577 #define fn_png_get_bKGD png_get_bKGD
5578 #define fn_png_read_update_info png_read_update_info
5579 #define fn_png_get_channels png_get_channels
5580 #define fn_png_get_rowbytes png_get_rowbytes
5581 #define fn_png_read_image png_read_image
5582 #define fn_png_read_end png_read_end
5583 #define fn_png_error png_error
5585 #if (PNG_LIBPNG_VER >= 10500)
5586 #define fn_png_longjmp png_longjmp
5587 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5588 #endif /* libpng version >= 1.5 */
5590 #endif /* WINDOWSNT */
5592 /* Possibly inefficient/inexact substitutes for _setjmp and _longjmp.
5593 Do not use sys_setjmp, as PNG supports only jmp_buf. The _longjmp
5594 substitute may munge the signal mask, but that should be OK here.
5595 MinGW (MS-Windows) uses _setjmp and defines setjmp to _setjmp in
5596 the system header setjmp.h; don't mess up that. */
5597 #ifndef HAVE__SETJMP
5598 # define _setjmp(j) setjmp (j)
5599 # define _longjmp longjmp
5602 #if (PNG_LIBPNG_VER < 10500)
5603 #define PNG_LONGJMP(ptr) (_longjmp ((ptr)->jmpbuf, 1))
5604 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5606 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5607 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5608 #define PNG_JMPBUF(ptr) \
5609 (*fn_png_set_longjmp_fn ((ptr), _longjmp, sizeof (jmp_buf)))
5612 /* Error and warning handlers installed when the PNG library
5615 static _Noreturn
void
5616 my_png_error (png_struct
*png_ptr
, const char *msg
)
5618 eassert (png_ptr
!= NULL
);
5619 /* Avoid compiler warning about deprecated direct access to
5620 png_ptr's fields in libpng versions 1.4.x. */
5621 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5622 PNG_LONGJMP (png_ptr
);
5627 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5629 eassert (png_ptr
!= NULL
);
5630 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5633 /* Memory source for PNG decoding. */
5635 struct png_memory_storage
5637 unsigned char *bytes
; /* The data */
5638 ptrdiff_t len
; /* How big is it? */
5639 ptrdiff_t index
; /* Where are we? */
5643 /* Function set as reader function when reading PNG image from memory.
5644 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5645 bytes from the input to DATA. */
5648 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5650 struct png_memory_storage
*tbr
5651 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5653 if (length
> tbr
->len
- tbr
->index
)
5654 fn_png_error (png_ptr
, "Read error");
5656 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5657 tbr
->index
= tbr
->index
+ length
;
5661 /* Function set as reader function when reading PNG image from a file.
5662 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5663 bytes from the input to DATA. */
5666 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5668 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5670 if (fread (data
, 1, length
, fp
) < length
)
5671 fn_png_error (png_ptr
, "Read error");
5675 /* Load PNG image IMG for use on frame F. Value is true if
5678 struct png_load_context
5680 /* These are members so that longjmp doesn't munge local variables. */
5681 png_struct
*png_ptr
;
5690 png_load_body (struct frame
*f
, struct image
*img
, struct png_load_context
*c
)
5692 Lisp_Object file
, specified_file
;
5693 Lisp_Object specified_data
;
5696 XImagePtr ximg
, mask_img
= NULL
;
5697 png_struct
*png_ptr
;
5698 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5701 png_byte
*pixels
= NULL
;
5702 png_byte
**rows
= NULL
;
5703 png_uint_32 width
, height
;
5704 int bit_depth
, color_type
, interlace_type
;
5706 png_uint_32 row_bytes
;
5708 struct png_memory_storage tbr
; /* Data to be read */
5710 /* Find out what file to load. */
5711 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5712 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5714 if (NILP (specified_data
))
5716 file
= x_find_image_file (specified_file
);
5717 if (!STRINGP (file
))
5719 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5723 /* Open the image file. */
5724 fp
= emacs_fopen (SSDATA (file
), "rb");
5727 image_error ("Cannot open image file `%s'", file
, Qnil
);
5731 /* Check PNG signature. */
5732 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5733 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5735 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5742 if (!STRINGP (specified_data
))
5744 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5748 /* Read from memory. */
5749 tbr
.bytes
= SDATA (specified_data
);
5750 tbr
.len
= SBYTES (specified_data
);
5753 /* Check PNG signature. */
5754 if (tbr
.len
< sizeof sig
5755 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5757 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5761 /* Need to skip past the signature. */
5762 tbr
.bytes
+= sizeof (sig
);
5765 /* Initialize read and info structs for PNG lib. */
5766 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5771 info_ptr
= fn_png_create_info_struct (png_ptr
);
5772 end_info
= fn_png_create_info_struct (png_ptr
);
5775 c
->png_ptr
= png_ptr
;
5776 c
->info_ptr
= info_ptr
;
5777 c
->end_info
= end_info
;
5782 if (! (info_ptr
&& end_info
))
5784 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5789 if (fp
) fclose (fp
);
5793 /* Set error jump-back. We come back here when the PNG library
5794 detects an error. */
5795 if (_setjmp (PNG_JMPBUF (png_ptr
)))
5799 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5807 /* Silence a bogus diagnostic; see GCC bug 54561. */
5808 IF_LINT (fp
= c
->fp
);
5810 /* Read image info. */
5811 if (!NILP (specified_data
))
5812 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5814 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5816 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5817 fn_png_read_info (png_ptr
, info_ptr
);
5818 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5819 &interlace_type
, NULL
, NULL
);
5821 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5822 && check_image_size (f
, width
, height
)))
5824 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5828 /* Create the X image and pixmap now, so that the work below can be
5829 omitted if the image is too large for X. */
5830 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
5833 /* If image contains simply transparency data, we prefer to
5834 construct a clipping mask. */
5835 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5840 /* This function is easier to write if we only have to handle
5841 one data format: RGB or RGBA with 8 bits per channel. Let's
5842 transform other formats into that format. */
5844 /* Strip more than 8 bits per channel. */
5845 if (bit_depth
== 16)
5846 fn_png_set_strip_16 (png_ptr
);
5848 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5850 fn_png_set_expand (png_ptr
);
5852 /* Convert grayscale images to RGB. */
5853 if (color_type
== PNG_COLOR_TYPE_GRAY
5854 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5855 fn_png_set_gray_to_rgb (png_ptr
);
5857 /* Handle alpha channel by combining the image with a background
5858 color. Do this only if a real alpha channel is supplied. For
5859 simple transparency, we prefer a clipping mask. */
5862 /* png_color_16 *image_bg; */
5863 Lisp_Object specified_bg
5864 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5865 int shift
= (bit_depth
== 16) ? 0 : 8;
5867 if (STRINGP (specified_bg
))
5868 /* The user specified `:background', use that. */
5871 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5873 png_color_16 user_bg
;
5875 memset (&user_bg
, 0, sizeof user_bg
);
5876 user_bg
.red
= color
.red
>> shift
;
5877 user_bg
.green
= color
.green
>> shift
;
5878 user_bg
.blue
= color
.blue
>> shift
;
5880 fn_png_set_background (png_ptr
, &user_bg
,
5881 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5886 /* We use the current frame background, ignoring any default
5887 background color set by the image. */
5888 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5890 png_color_16 frame_background
;
5892 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5893 x_query_color (f
, &color
);
5895 memset (&frame_background
, 0, sizeof frame_background
);
5896 frame_background
.red
= color
.red
>> shift
;
5897 frame_background
.green
= color
.green
>> shift
;
5898 frame_background
.blue
= color
.blue
>> shift
;
5899 #endif /* HAVE_X_WINDOWS */
5901 fn_png_set_background (png_ptr
, &frame_background
,
5902 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5906 /* Update info structure. */
5907 fn_png_read_update_info (png_ptr
, info_ptr
);
5909 /* Get number of channels. Valid values are 1 for grayscale images
5910 and images with a palette, 2 for grayscale images with transparency
5911 information (alpha channel), 3 for RGB images, and 4 for RGB
5912 images with alpha channel, i.e. RGBA. If conversions above were
5913 sufficient we should only have 3 or 4 channels here. */
5914 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5915 eassert (channels
== 3 || channels
== 4);
5917 /* Number of bytes needed for one row of the image. */
5918 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5920 /* Allocate memory for the image. */
5921 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5922 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5923 memory_full (SIZE_MAX
);
5924 c
->pixels
= pixels
= xmalloc (sizeof *pixels
* row_bytes
* height
);
5925 c
->rows
= rows
= xmalloc (height
* sizeof *rows
);
5926 for (i
= 0; i
< height
; ++i
)
5927 rows
[i
] = pixels
+ i
* row_bytes
;
5929 /* Read the entire image. */
5930 fn_png_read_image (png_ptr
, rows
);
5931 fn_png_read_end (png_ptr
, info_ptr
);
5938 /* Create an image and pixmap serving as mask if the PNG image
5939 contains an alpha channel. */
5942 && !image_create_x_image_and_pixmap (f
, img
, width
, height
, 1,
5945 x_destroy_x_image (ximg
);
5946 x_clear_image_1 (f
, img
, CLEAR_IMAGE_PIXMAP
);
5950 /* Fill the X image and mask from PNG data. */
5951 init_color_table ();
5953 for (y
= 0; y
< height
; ++y
)
5955 png_byte
*p
= rows
[y
];
5957 for (x
= 0; x
< width
; ++x
)
5964 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5965 /* An alpha channel, aka mask channel, associates variable
5966 transparency with an image. Where other image formats
5967 support binary transparency---fully transparent or fully
5968 opaque---PNG allows up to 254 levels of partial transparency.
5969 The PNG library implements partial transparency by combining
5970 the image with a specified background color.
5972 I'm not sure how to handle this here nicely: because the
5973 background on which the image is displayed may change, for
5974 real alpha channel support, it would be necessary to create
5975 a new image for each possible background.
5977 What I'm doing now is that a mask is created if we have
5978 boolean transparency information. Otherwise I'm using
5979 the frame's background color to combine the image with. */
5984 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5990 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5991 /* Set IMG's background color from the PNG image, unless the user
5995 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5997 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5998 img
->background_valid
= 1;
6002 #ifdef COLOR_TABLE_SUPPORT
6003 /* Remember colors allocated for this image. */
6004 img
->colors
= colors_in_color_table (&img
->ncolors
);
6005 free_color_table ();
6006 #endif /* COLOR_TABLE_SUPPORT */
6009 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
6014 img
->height
= height
;
6016 /* Maybe fill in the background field while we have ximg handy.
6017 Casting avoids a GCC warning. */
6018 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6020 /* Put ximg into the image. */
6021 image_put_x_image (f
, img
, ximg
, 0);
6023 /* Same for the mask. */
6026 /* Fill in the background_transparent field while we have the
6027 mask handy. Casting avoids a GCC warning. */
6028 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
6030 image_put_x_image (f
, img
, mask_img
, 1);
6037 png_load (struct frame
*f
, struct image
*img
)
6039 struct png_load_context c
;
6040 return png_load_body (f
, img
, &c
);
6043 #else /* HAVE_PNG */
6047 png_load (struct frame
*f
, struct image
*img
)
6049 return ns_load_image (f
, img
,
6050 image_spec_value (img
->spec
, QCfile
, NULL
),
6051 image_spec_value (img
->spec
, QCdata
, NULL
));
6053 #endif /* HAVE_NS */
6056 #endif /* !HAVE_PNG */
6060 /***********************************************************************
6062 ***********************************************************************/
6064 #if defined (HAVE_JPEG) || defined (HAVE_NS)
6066 static bool jpeg_image_p (Lisp_Object object
);
6067 static bool jpeg_load (struct frame
*f
, struct image
*img
);
6069 /* The symbol `jpeg' identifying images of this type. */
6071 static Lisp_Object Qjpeg
;
6073 /* Indices of image specification fields in gs_format, below. */
6075 enum jpeg_keyword_index
6084 JPEG_HEURISTIC_MASK
,
6090 /* Vector of image_keyword structures describing the format
6091 of valid user-defined image specifications. */
6093 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
6095 {":type", IMAGE_SYMBOL_VALUE
, 1},
6096 {":data", IMAGE_STRING_VALUE
, 0},
6097 {":file", IMAGE_STRING_VALUE
, 0},
6098 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6099 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6100 {":relief", IMAGE_INTEGER_VALUE
, 0},
6101 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6102 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6103 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6104 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6107 #if defined HAVE_NTGUI && defined WINDOWSNT
6108 static bool init_jpeg_functions (void);
6110 #define init_jpeg_functions NULL
6113 /* Structure describing the image type `jpeg'. */
6115 static struct image_type jpeg_type
=
6121 init_jpeg_functions
,
6125 /* Return true if OBJECT is a valid JPEG image specification. */
6128 jpeg_image_p (Lisp_Object object
)
6130 struct image_keyword fmt
[JPEG_LAST
];
6132 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6134 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6137 /* Must specify either the :data or :file keyword. */
6138 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6141 #endif /* HAVE_JPEG || HAVE_NS */
6145 /* Work around a warning about HAVE_STDLIB_H being redefined in
6147 #ifdef HAVE_STDLIB_H
6148 #undef HAVE_STDLIB_H
6149 #endif /* HAVE_STLIB_H */
6151 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6152 /* In older releases of the jpeg library, jpeglib.h will define boolean
6153 differently depending on __WIN32__, so make sure it is defined. */
6157 /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types.
6158 Some versions of jpeglib try to detect whether rpcndr.h is loaded,
6159 using the Windows boolean type instead of the jpeglib boolean type
6160 if so. Cygwin jpeglib, however, doesn't try to detect whether its
6161 headers are included along with windows.h, so under Cygwin, jpeglib
6162 attempts to define a conflicting boolean type. Worse, forcing
6163 Cygwin jpeglib headers to use the Windows boolean type doesn't work
6164 because it created an ABI incompatibility between the
6165 already-compiled jpeg library and the header interface definition.
6167 The best we can do is to define jpeglib's boolean type to a
6168 different name. This name, jpeg_boolean, remains in effect through
6169 the rest of image.c.
6171 #if defined CYGWIN && defined HAVE_NTGUI
6172 #define boolean jpeg_boolean
6174 #include <jpeglib.h>
6179 /* JPEG library details. */
6180 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6181 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6182 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6183 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6184 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6185 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6186 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6187 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6190 init_jpeg_functions (void)
6194 if (!(library
= w32_delayed_load (Qjpeg
)))
6197 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6198 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6199 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6200 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6201 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6202 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6203 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6204 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6208 /* Wrapper since we can't directly assign the function pointer
6209 to another function pointer that was declared more completely easily. */
6211 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6213 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6218 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6219 #define fn_jpeg_start_decompress jpeg_start_decompress
6220 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6221 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6222 #define fn_jpeg_read_header jpeg_read_header
6223 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6224 #define fn_jpeg_std_error jpeg_std_error
6225 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6227 #endif /* WINDOWSNT */
6229 struct my_jpeg_error_mgr
6231 struct jpeg_error_mgr pub
;
6232 sys_jmp_buf setjmp_buffer
;
6234 /* The remaining members are so that longjmp doesn't munge local
6236 struct jpeg_decompress_struct cinfo
;
6240 MY_JPEG_INVALID_IMAGE_SIZE
,
6241 MY_JPEG_CANNOT_CREATE_X
6249 static _Noreturn
void
6250 my_error_exit (j_common_ptr cinfo
)
6252 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6253 mgr
->failure_code
= MY_JPEG_ERROR_EXIT
;
6254 sys_longjmp (mgr
->setjmp_buffer
, 1);
6258 /* Init source method for JPEG data source manager. Called by
6259 jpeg_read_header() before any data is actually read. See
6260 libjpeg.doc from the JPEG lib distribution. */
6263 our_common_init_source (j_decompress_ptr cinfo
)
6268 /* Method to terminate data source. Called by
6269 jpeg_finish_decompress() after all data has been processed. */
6272 our_common_term_source (j_decompress_ptr cinfo
)
6277 /* Fill input buffer method for JPEG data source manager. Called
6278 whenever more data is needed. We read the whole image in one step,
6279 so this only adds a fake end of input marker at the end. */
6281 static JOCTET our_memory_buffer
[2];
6284 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6286 /* Insert a fake EOI marker. */
6287 struct jpeg_source_mgr
*src
= cinfo
->src
;
6289 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6290 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6292 src
->next_input_byte
= our_memory_buffer
;
6293 src
->bytes_in_buffer
= 2;
6298 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6299 is the JPEG data source manager. */
6302 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6304 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6308 if (num_bytes
> src
->bytes_in_buffer
)
6309 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6311 src
->bytes_in_buffer
-= num_bytes
;
6312 src
->next_input_byte
+= num_bytes
;
6317 /* Set up the JPEG lib for reading an image from DATA which contains
6318 LEN bytes. CINFO is the decompression info structure created for
6319 reading the image. */
6322 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, ptrdiff_t len
)
6324 struct jpeg_source_mgr
*src
;
6326 if (cinfo
->src
== NULL
)
6328 /* First time for this JPEG object? */
6329 cinfo
->src
= (struct jpeg_source_mgr
*)
6330 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6331 sizeof (struct jpeg_source_mgr
));
6332 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6333 src
->next_input_byte
= data
;
6336 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6337 src
->init_source
= our_common_init_source
;
6338 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6339 src
->skip_input_data
= our_memory_skip_input_data
;
6340 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6341 src
->term_source
= our_common_term_source
;
6342 src
->bytes_in_buffer
= len
;
6343 src
->next_input_byte
= data
;
6347 struct jpeg_stdio_mgr
6349 struct jpeg_source_mgr mgr
;
6356 /* Size of buffer to read JPEG from file.
6357 Not too big, as we want to use alloc_small. */
6358 #define JPEG_STDIO_BUFFER_SIZE 8192
6361 /* Fill input buffer method for JPEG data source manager. Called
6362 whenever more data is needed. The data is read from a FILE *. */
6365 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6367 struct jpeg_stdio_mgr
*src
;
6369 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6374 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6376 src
->mgr
.bytes_in_buffer
= bytes
;
6379 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6381 src
->buffer
[0] = (JOCTET
) 0xFF;
6382 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6383 src
->mgr
.bytes_in_buffer
= 2;
6385 src
->mgr
.next_input_byte
= src
->buffer
;
6392 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6393 is the JPEG data source manager. */
6396 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6398 struct jpeg_stdio_mgr
*src
;
6399 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6401 while (num_bytes
> 0 && !src
->finished
)
6403 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6405 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6406 src
->mgr
.next_input_byte
+= num_bytes
;
6411 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6412 src
->mgr
.bytes_in_buffer
= 0;
6413 src
->mgr
.next_input_byte
= NULL
;
6415 our_stdio_fill_input_buffer (cinfo
);
6421 /* Set up the JPEG lib for reading an image from a FILE *.
6422 CINFO is the decompression info structure created for
6423 reading the image. */
6426 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6428 struct jpeg_stdio_mgr
*src
;
6430 if (cinfo
->src
!= NULL
)
6431 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6434 /* First time for this JPEG object? */
6435 cinfo
->src
= (struct jpeg_source_mgr
*)
6436 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6437 sizeof (struct jpeg_stdio_mgr
));
6438 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6439 src
->buffer
= (JOCTET
*)
6440 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6441 JPEG_STDIO_BUFFER_SIZE
);
6446 src
->mgr
.init_source
= our_common_init_source
;
6447 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6448 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6449 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6450 src
->mgr
.term_source
= our_common_term_source
;
6451 src
->mgr
.bytes_in_buffer
= 0;
6452 src
->mgr
.next_input_byte
= NULL
;
6456 /* Load image IMG for use on frame F. Patterned after example.c
6457 from the JPEG lib. */
6460 jpeg_load_body (struct frame
*f
, struct image
*img
,
6461 struct my_jpeg_error_mgr
*mgr
)
6463 Lisp_Object file
, specified_file
;
6464 Lisp_Object specified_data
;
6467 int row_stride
, x
, y
;
6468 XImagePtr ximg
= NULL
;
6469 unsigned long *colors
;
6472 /* Open the JPEG file. */
6473 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6474 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6476 if (NILP (specified_data
))
6478 file
= x_find_image_file (specified_file
);
6479 if (!STRINGP (file
))
6481 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6485 fp
= emacs_fopen (SSDATA (file
), "rb");
6488 image_error ("Cannot open `%s'", file
, Qnil
);
6492 else if (!STRINGP (specified_data
))
6494 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6498 IF_LINT (mgr
->fp
= fp
);
6500 /* Customize libjpeg's error handling to call my_error_exit when an
6501 error is detected. This function will perform a longjmp. */
6502 mgr
->cinfo
.err
= fn_jpeg_std_error (&mgr
->pub
);
6503 mgr
->pub
.error_exit
= my_error_exit
;
6504 if (sys_setjmp (mgr
->setjmp_buffer
))
6506 switch (mgr
->failure_code
)
6508 case MY_JPEG_ERROR_EXIT
:
6510 char buf
[JMSG_LENGTH_MAX
];
6511 mgr
->cinfo
.err
->format_message ((j_common_ptr
) &mgr
->cinfo
, buf
);
6512 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6513 build_string (buf
));
6517 case MY_JPEG_INVALID_IMAGE_SIZE
:
6518 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6521 case MY_JPEG_CANNOT_CREATE_X
:
6525 /* Close the input file and destroy the JPEG object. */
6528 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6530 /* If we already have an XImage, free that. */
6531 x_destroy_x_image (ximg
);
6533 /* Free pixmap and colors. */
6534 x_clear_image (f
, img
);
6538 /* Silence a bogus diagnostic; see GCC bug 54561. */
6539 IF_LINT (fp
= mgr
->fp
);
6541 /* Create the JPEG decompression object. Let it read from fp.
6542 Read the JPEG image header. */
6543 fn_jpeg_CreateDecompress (&mgr
->cinfo
, JPEG_LIB_VERSION
, sizeof *&mgr
->cinfo
);
6545 if (NILP (specified_data
))
6546 jpeg_file_src (&mgr
->cinfo
, fp
);
6548 jpeg_memory_src (&mgr
->cinfo
, SDATA (specified_data
),
6549 SBYTES (specified_data
));
6551 fn_jpeg_read_header (&mgr
->cinfo
, 1);
6553 /* Customize decompression so that color quantization will be used.
6554 Start decompression. */
6555 mgr
->cinfo
.quantize_colors
= 1;
6556 fn_jpeg_start_decompress (&mgr
->cinfo
);
6557 width
= img
->width
= mgr
->cinfo
.output_width
;
6558 height
= img
->height
= mgr
->cinfo
.output_height
;
6560 if (!check_image_size (f
, width
, height
))
6562 mgr
->failure_code
= MY_JPEG_INVALID_IMAGE_SIZE
;
6563 sys_longjmp (mgr
->setjmp_buffer
, 1);
6566 /* Create X image and pixmap. */
6567 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
6569 mgr
->failure_code
= MY_JPEG_CANNOT_CREATE_X
;
6570 sys_longjmp (mgr
->setjmp_buffer
, 1);
6573 /* Allocate colors. When color quantization is used,
6574 mgr->cinfo.actual_number_of_colors has been set with the number of
6575 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6576 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6577 No more than 255 colors will be generated. */
6581 if (mgr
->cinfo
.out_color_components
> 2)
6582 ir
= 0, ig
= 1, ib
= 2;
6583 else if (mgr
->cinfo
.out_color_components
> 1)
6584 ir
= 0, ig
= 1, ib
= 0;
6586 ir
= 0, ig
= 0, ib
= 0;
6588 /* Use the color table mechanism because it handles colors that
6589 cannot be allocated nicely. Such colors will be replaced with
6590 a default color, and we don't have to care about which colors
6591 can be freed safely, and which can't. */
6592 init_color_table ();
6593 colors
= alloca (mgr
->cinfo
.actual_number_of_colors
* sizeof *colors
);
6595 for (i
= 0; i
< mgr
->cinfo
.actual_number_of_colors
; ++i
)
6597 /* Multiply RGB values with 255 because X expects RGB values
6598 in the range 0..0xffff. */
6599 int r
= mgr
->cinfo
.colormap
[ir
][i
] << 8;
6600 int g
= mgr
->cinfo
.colormap
[ig
][i
] << 8;
6601 int b
= mgr
->cinfo
.colormap
[ib
][i
] << 8;
6602 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6605 #ifdef COLOR_TABLE_SUPPORT
6606 /* Remember those colors actually allocated. */
6607 img
->colors
= colors_in_color_table (&img
->ncolors
);
6608 free_color_table ();
6609 #endif /* COLOR_TABLE_SUPPORT */
6613 row_stride
= width
* mgr
->cinfo
.output_components
;
6614 buffer
= mgr
->cinfo
.mem
->alloc_sarray ((j_common_ptr
) &mgr
->cinfo
,
6615 JPOOL_IMAGE
, row_stride
, 1);
6616 for (y
= 0; y
< height
; ++y
)
6618 fn_jpeg_read_scanlines (&mgr
->cinfo
, buffer
, 1);
6619 for (x
= 0; x
< mgr
->cinfo
.output_width
; ++x
)
6620 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6624 fn_jpeg_finish_decompress (&mgr
->cinfo
);
6625 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6629 /* Maybe fill in the background field while we have ximg handy. */
6630 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6631 /* Casting avoids a GCC warning. */
6632 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6634 /* Put ximg into the image. */
6635 image_put_x_image (f
, img
, ximg
, 0);
6640 jpeg_load (struct frame
*f
, struct image
*img
)
6642 struct my_jpeg_error_mgr mgr
;
6643 return jpeg_load_body (f
, img
, &mgr
);
6646 #else /* HAVE_JPEG */
6650 jpeg_load (struct frame
*f
, struct image
*img
)
6652 return ns_load_image (f
, img
,
6653 image_spec_value (img
->spec
, QCfile
, NULL
),
6654 image_spec_value (img
->spec
, QCdata
, NULL
));
6656 #endif /* HAVE_NS */
6658 #endif /* !HAVE_JPEG */
6662 /***********************************************************************
6664 ***********************************************************************/
6666 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6668 static bool tiff_image_p (Lisp_Object object
);
6669 static bool tiff_load (struct frame
*f
, struct image
*img
);
6671 /* The symbol `tiff' identifying images of this type. */
6673 static Lisp_Object Qtiff
;
6675 /* Indices of image specification fields in tiff_format, below. */
6677 enum tiff_keyword_index
6686 TIFF_HEURISTIC_MASK
,
6693 /* Vector of image_keyword structures describing the format
6694 of valid user-defined image specifications. */
6696 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6698 {":type", IMAGE_SYMBOL_VALUE
, 1},
6699 {":data", IMAGE_STRING_VALUE
, 0},
6700 {":file", IMAGE_STRING_VALUE
, 0},
6701 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6702 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6703 {":relief", IMAGE_INTEGER_VALUE
, 0},
6704 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6705 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6706 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6707 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6708 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6711 #if defined HAVE_NTGUI && defined WINDOWSNT
6712 static bool init_tiff_functions (void);
6714 #define init_tiff_functions NULL
6717 /* Structure describing the image type `tiff'. */
6719 static struct image_type tiff_type
=
6725 init_tiff_functions
,
6729 /* Return true if OBJECT is a valid TIFF image specification. */
6732 tiff_image_p (Lisp_Object object
)
6734 struct image_keyword fmt
[TIFF_LAST
];
6735 memcpy (fmt
, tiff_format
, sizeof fmt
);
6737 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6740 /* Must specify either the :data or :file keyword. */
6741 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6744 #endif /* HAVE_TIFF || HAVE_NS */
6752 /* TIFF library details. */
6753 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6754 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6755 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6756 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6757 TIFFReadWriteProc
, TIFFReadWriteProc
,
6758 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6759 TIFFMapFileProc
, TIFFUnmapFileProc
));
6760 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6761 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6762 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6763 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6766 init_tiff_functions (void)
6770 if (!(library
= w32_delayed_load (Qtiff
)))
6773 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6774 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6775 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6776 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6777 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6778 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6779 LOAD_IMGLIB_FN (library
, TIFFClose
);
6780 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6786 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6787 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6788 #define fn_TIFFOpen TIFFOpen
6789 #define fn_TIFFClientOpen TIFFClientOpen
6790 #define fn_TIFFGetField TIFFGetField
6791 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6792 #define fn_TIFFClose TIFFClose
6793 #define fn_TIFFSetDirectory TIFFSetDirectory
6794 #endif /* WINDOWSNT */
6797 /* Reading from a memory buffer for TIFF images Based on the PNG
6798 memory source, but we have to provide a lot of extra functions.
6801 We really only need to implement read and seek, but I am not
6802 convinced that the TIFF library is smart enough not to destroy
6803 itself if we only hand it the function pointers we need to
6808 unsigned char *bytes
;
6815 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6817 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6819 size
= min (size
, src
->len
- src
->index
);
6820 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6826 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6832 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6834 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6839 case SEEK_SET
: /* Go from beginning of source. */
6843 case SEEK_END
: /* Go from end of source. */
6844 idx
= src
->len
+ off
;
6847 case SEEK_CUR
: /* Go from current position. */
6848 idx
= src
->index
+ off
;
6851 default: /* Invalid `whence'. */
6855 if (idx
> src
->len
|| idx
< 0)
6863 tiff_close_memory (thandle_t data
)
6870 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6872 /* It is already _IN_ memory. */
6877 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6879 /* We don't need to do this. */
6883 tiff_size_of_memory (thandle_t data
)
6885 return ((tiff_memory_source
*) data
)->len
;
6888 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6889 compiler error compiling tiff_handler, see Bugzilla bug #17406
6890 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6891 this function as external works around that problem. */
6892 #if defined (__MINGW32__) && __GNUC__ == 3
6893 # define MINGW_STATIC
6895 # define MINGW_STATIC static
6899 tiff_handler (const char *, const char *, const char *, va_list)
6900 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6902 tiff_handler (const char *log_format
, const char *title
,
6903 const char *format
, va_list ap
)
6905 /* doprnt is not suitable here, as TIFF handlers are called from
6906 libtiff and are passed arbitrary printf directives. Instead, use
6907 vsnprintf, taking care to be portable to nonstandard environments
6908 where vsnprintf returns -1 on buffer overflow. Since it's just a
6909 log entry, it's OK to truncate it. */
6911 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6912 add_to_log (log_format
, build_string (title
),
6913 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6917 static void tiff_error_handler (const char *, const char *, va_list)
6918 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6920 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6922 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6926 static void tiff_warning_handler (const char *, const char *, va_list)
6927 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6929 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6931 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6935 /* Load TIFF image IMG for use on frame F. Value is true if
6939 tiff_load (struct frame
*f
, struct image
*img
)
6941 Lisp_Object file
, specified_file
;
6942 Lisp_Object specified_data
;
6944 int width
, height
, x
, y
, count
;
6948 tiff_memory_source memsrc
;
6951 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6952 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6954 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6955 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6957 if (NILP (specified_data
))
6959 /* Read from a file */
6960 file
= x_find_image_file (specified_file
);
6961 if (!STRINGP (file
))
6963 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6967 /* Try to open the image file. */
6968 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6971 image_error ("Cannot open `%s'", file
, Qnil
);
6977 if (!STRINGP (specified_data
))
6979 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6983 /* Memory source! */
6984 memsrc
.bytes
= SDATA (specified_data
);
6985 memsrc
.len
= SBYTES (specified_data
);
6988 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6989 tiff_read_from_memory
,
6990 tiff_write_from_memory
,
6991 tiff_seek_in_memory
,
6993 tiff_size_of_memory
,
6999 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
7004 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7005 if (INTEGERP (image
))
7007 EMACS_INT ino
= XFASTINT (image
);
7008 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
7009 && fn_TIFFSetDirectory (tiff
, ino
)))
7011 image_error ("Invalid image number `%s' in image `%s'",
7013 fn_TIFFClose (tiff
);
7018 /* Get width and height of the image, and allocate a raster buffer
7019 of width x height 32-bit values. */
7020 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
7021 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
7023 if (!check_image_size (f
, width
, height
))
7025 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7026 fn_TIFFClose (tiff
);
7030 /* Create the X image and pixmap. */
7031 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
7032 && image_create_x_image_and_pixmap (f
, img
, width
, height
, 0,
7035 fn_TIFFClose (tiff
);
7039 buf
= xmalloc (sizeof *buf
* width
* height
);
7041 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
7043 /* Count the number of images in the file. */
7044 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
7048 img
->lisp_data
= Fcons (Qcount
,
7049 Fcons (make_number (count
),
7052 fn_TIFFClose (tiff
);
7055 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
7060 /* Initialize the color table. */
7061 init_color_table ();
7063 /* Process the pixel raster. Origin is in the lower-left corner. */
7064 for (y
= 0; y
< height
; ++y
)
7066 uint32
*row
= buf
+ y
* width
;
7068 for (x
= 0; x
< width
; ++x
)
7070 uint32 abgr
= row
[x
];
7071 int r
= TIFFGetR (abgr
) << 8;
7072 int g
= TIFFGetG (abgr
) << 8;
7073 int b
= TIFFGetB (abgr
) << 8;
7074 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
7078 #ifdef COLOR_TABLE_SUPPORT
7079 /* Remember the colors allocated for the image. Free the color table. */
7080 img
->colors
= colors_in_color_table (&img
->ncolors
);
7081 free_color_table ();
7082 #endif /* COLOR_TABLE_SUPPORT */
7085 img
->height
= height
;
7087 /* Maybe fill in the background field while we have ximg handy. */
7088 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7089 /* Casting avoids a GCC warning on W32. */
7090 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7092 /* Put ximg into the image. */
7093 image_put_x_image (f
, img
, ximg
, 0);
7099 #else /* HAVE_TIFF */
7103 tiff_load (struct frame
*f
, struct image
*img
)
7105 return ns_load_image (f
, img
,
7106 image_spec_value (img
->spec
, QCfile
, NULL
),
7107 image_spec_value (img
->spec
, QCdata
, NULL
));
7109 #endif /* HAVE_NS */
7111 #endif /* !HAVE_TIFF */
7115 /***********************************************************************
7117 ***********************************************************************/
7119 #if defined (HAVE_GIF) || defined (HAVE_NS)
7121 static bool gif_image_p (Lisp_Object object
);
7122 static bool gif_load (struct frame
*f
, struct image
*img
);
7123 static void gif_clear_image (struct frame
*f
, struct image
*img
);
7125 /* The symbol `gif' identifying images of this type. */
7127 static Lisp_Object Qgif
;
7129 /* Indices of image specification fields in gif_format, below. */
7131 enum gif_keyword_index
7147 /* Vector of image_keyword structures describing the format
7148 of valid user-defined image specifications. */
7150 static const struct image_keyword gif_format
[GIF_LAST
] =
7152 {":type", IMAGE_SYMBOL_VALUE
, 1},
7153 {":data", IMAGE_STRING_VALUE
, 0},
7154 {":file", IMAGE_STRING_VALUE
, 0},
7155 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7156 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7157 {":relief", IMAGE_INTEGER_VALUE
, 0},
7158 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7159 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7160 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7161 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7162 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7165 #if defined HAVE_NTGUI && defined WINDOWSNT
7166 static bool init_gif_functions (void);
7168 #define init_gif_functions NULL
7171 /* Structure describing the image type `gif'. */
7173 static struct image_type gif_type
=
7183 /* Free X resources of GIF image IMG which is used on frame F. */
7186 gif_clear_image (struct frame
*f
, struct image
*img
)
7188 img
->lisp_data
= Qnil
;
7189 x_clear_image (f
, img
);
7192 /* Return true if OBJECT is a valid GIF image specification. */
7195 gif_image_p (Lisp_Object object
)
7197 struct image_keyword fmt
[GIF_LAST
];
7198 memcpy (fmt
, gif_format
, sizeof fmt
);
7200 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7203 /* Must specify either the :data or :file keyword. */
7204 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7207 #endif /* HAVE_GIF */
7211 #if defined (HAVE_NTGUI)
7212 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7213 Undefine before redefining to avoid a preprocessor warning. */
7217 /* avoid conflict with QuickdrawText.h */
7218 #define DrawText gif_DrawText
7219 #include <gif_lib.h>
7222 #else /* HAVE_NTGUI */
7224 #include <gif_lib.h>
7226 #endif /* HAVE_NTGUI */
7231 /* GIF library details. */
7232 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7233 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7234 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7235 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7238 init_gif_functions (void)
7242 if (!(library
= w32_delayed_load (Qgif
)))
7245 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7246 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7247 LOAD_IMGLIB_FN (library
, DGifOpen
);
7248 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7254 #define fn_DGifCloseFile DGifCloseFile
7255 #define fn_DGifSlurp DGifSlurp
7256 #define fn_DGifOpen DGifOpen
7257 #define fn_DGifOpenFileName DGifOpenFileName
7259 #endif /* WINDOWSNT */
7261 /* Reading a GIF image from memory
7262 Based on the PNG memory stuff to a certain extent. */
7266 unsigned char *bytes
;
7272 /* Make the current memory source available to gif_read_from_memory.
7273 It's done this way because not all versions of libungif support
7274 a UserData field in the GifFileType structure. */
7275 static gif_memory_source
*current_gif_memory_src
;
7278 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7280 gif_memory_source
*src
= current_gif_memory_src
;
7282 if (len
> src
->len
- src
->index
)
7285 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7291 /* Load GIF image IMG for use on frame F. Value is true if
7294 static const int interlace_start
[] = {0, 4, 2, 1};
7295 static const int interlace_increment
[] = {8, 8, 4, 2};
7297 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7300 gif_load (struct frame
*f
, struct image
*img
)
7303 int rc
, width
, height
, x
, y
, i
, j
;
7305 ColorMapObject
*gif_color_map
;
7306 unsigned long pixel_colors
[256];
7308 gif_memory_source memsrc
;
7309 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7310 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7311 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7312 unsigned long bgcolor
= 0;
7315 if (NILP (specified_data
))
7317 file
= x_find_image_file (specified_file
);
7318 if (!STRINGP (file
))
7320 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7324 /* Open the GIF file. */
7325 gif
= fn_DGifOpenFileName (SSDATA (file
));
7328 image_error ("Cannot open `%s'", file
, Qnil
);
7334 if (!STRINGP (specified_data
))
7336 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7340 /* Read from memory! */
7341 current_gif_memory_src
= &memsrc
;
7342 memsrc
.bytes
= SDATA (specified_data
);
7343 memsrc
.len
= SBYTES (specified_data
);
7346 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7349 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7354 /* Before reading entire contents, check the declared image size. */
7355 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7357 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7358 fn_DGifCloseFile (gif
);
7362 /* Read entire contents. */
7363 rc
= fn_DGifSlurp (gif
);
7364 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7366 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7367 fn_DGifCloseFile (gif
);
7371 /* Which sub-image are we to display? */
7373 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7374 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7375 if (idx
< 0 || idx
>= gif
->ImageCount
)
7377 image_error ("Invalid image number `%s' in image `%s'",
7378 image_number
, img
->spec
);
7379 fn_DGifCloseFile (gif
);
7384 width
= img
->width
= gif
->SWidth
;
7385 height
= img
->height
= gif
->SHeight
;
7387 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Top
;
7388 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Left
;
7389 img
->corners
[BOT_CORNER
]
7390 = img
->corners
[TOP_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Height
;
7391 img
->corners
[RIGHT_CORNER
]
7392 = img
->corners
[LEFT_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Width
;
7394 if (!check_image_size (f
, width
, height
))
7396 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7397 fn_DGifCloseFile (gif
);
7401 /* Check that the selected subimages fit. It's not clear whether
7402 the GIF spec requires this, but Emacs can crash if they don't fit. */
7403 for (j
= 0; j
<= idx
; ++j
)
7405 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7406 int subimg_width
= subimage
->ImageDesc
.Width
;
7407 int subimg_height
= subimage
->ImageDesc
.Height
;
7408 int subimg_top
= subimage
->ImageDesc
.Top
;
7409 int subimg_left
= subimage
->ImageDesc
.Left
;
7410 if (! (0 <= subimg_width
&& 0 <= subimg_height
7411 && 0 <= subimg_top
&& subimg_top
<= height
- subimg_height
7412 && 0 <= subimg_left
&& subimg_left
<= width
- subimg_width
))
7414 image_error ("Subimage does not fit in image", Qnil
, Qnil
);
7415 fn_DGifCloseFile (gif
);
7420 /* Create the X image and pixmap. */
7421 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
7423 fn_DGifCloseFile (gif
);
7427 /* Clear the part of the screen image not covered by the image.
7428 Full animated GIF support requires more here (see the gif89 spec,
7429 disposal methods). Let's simply assume that the part not covered
7430 by a sub-image is in the frame's background color. */
7431 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7432 for (x
= 0; x
< width
; ++x
)
7433 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7435 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7436 for (x
= 0; x
< width
; ++x
)
7437 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7439 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7441 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7442 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7443 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7444 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7447 /* Read the GIF image into the X image. */
7449 /* FIXME: With the current implementation, loading an animated gif
7450 is quadratic in the number of animation frames, since each frame
7451 is a separate struct image. We must provide a way for a single
7452 gif_load call to construct and save all animation frames. */
7454 init_color_table ();
7455 if (STRINGP (specified_bg
))
7456 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7457 FRAME_BACKGROUND_PIXEL (f
));
7458 for (j
= 0; j
<= idx
; ++j
)
7460 /* We use a local variable `raster' here because RasterBits is a
7461 char *, which invites problems with bytes >= 0x80. */
7462 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7463 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7464 int transparency_color_index
= -1;
7466 int subimg_width
= subimage
->ImageDesc
.Width
;
7467 int subimg_height
= subimage
->ImageDesc
.Height
;
7468 int subimg_top
= subimage
->ImageDesc
.Top
;
7469 int subimg_left
= subimage
->ImageDesc
.Left
;
7471 /* Find the Graphic Control Extension block for this sub-image.
7472 Extract the disposal method and transparency color. */
7473 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7475 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7477 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7478 && extblock
->ByteCount
== 4
7479 && extblock
->Bytes
[0] & 1)
7481 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7482 to background". Treat any other value like 2. */
7483 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7484 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7489 /* We can't "keep in place" the first subimage. */
7493 /* For disposal == 0, the spec says "No disposal specified. The
7494 decoder is not required to take any action." In practice, it
7495 seems we need to treat this like "keep in place", see e.g.
7496 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7500 /* Allocate subimage colors. */
7501 memset (pixel_colors
, 0, sizeof pixel_colors
);
7502 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7504 gif_color_map
= gif
->SColorMap
;
7507 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7509 if (transparency_color_index
== i
)
7510 pixel_colors
[i
] = STRINGP (specified_bg
)
7511 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7514 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7515 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7516 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7517 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7521 /* Apply the pixel values. */
7522 if (gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7526 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7528 y
++, row
+= interlace_increment
[pass
])
7530 while (subimg_height
<= row
)
7532 lint_assume (pass
< 3);
7533 row
= interlace_start
[++pass
];
7536 for (x
= 0; x
< subimg_width
; x
++)
7538 int c
= raster
[y
* subimg_width
+ x
];
7539 if (transparency_color_index
!= c
|| disposal
!= 1)
7540 XPutPixel (ximg
, x
+ subimg_left
, row
+ subimg_top
,
7547 for (y
= 0; y
< subimg_height
; ++y
)
7548 for (x
= 0; x
< subimg_width
; ++x
)
7550 int c
= raster
[y
* subimg_width
+ x
];
7551 if (transparency_color_index
!= c
|| disposal
!= 1)
7552 XPutPixel (ximg
, x
+ subimg_left
, y
+ subimg_top
,
7558 #ifdef COLOR_TABLE_SUPPORT
7559 img
->colors
= colors_in_color_table (&img
->ncolors
);
7560 free_color_table ();
7561 #endif /* COLOR_TABLE_SUPPORT */
7563 /* Save GIF image extension data for `image-metadata'.
7564 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7565 img
->lisp_data
= Qnil
;
7566 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7569 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7570 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7571 /* Append (... FUNCTION "BYTES") */
7574 = Fcons (make_number (ext
->Function
),
7575 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7577 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7578 && ext
->ByteCount
== 4)
7580 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7581 delay
|= ext
->Bytes
[1];
7584 img
->lisp_data
= Fcons (Qextension_data
,
7585 Fcons (img
->lisp_data
, Qnil
));
7589 Fcons (make_float (delay
/ 100.0),
7593 if (gif
->ImageCount
> 1)
7594 img
->lisp_data
= Fcons (Qcount
,
7595 Fcons (make_number (gif
->ImageCount
),
7598 fn_DGifCloseFile (gif
);
7600 /* Maybe fill in the background field while we have ximg handy. */
7601 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7602 /* Casting avoids a GCC warning. */
7603 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7605 /* Put ximg into the image. */
7606 image_put_x_image (f
, img
, ximg
, 0);
7611 #else /* !HAVE_GIF */
7615 gif_load (struct frame
*f
, struct image
*img
)
7617 return ns_load_image (f
, img
,
7618 image_spec_value (img
->spec
, QCfile
, NULL
),
7619 image_spec_value (img
->spec
, QCdata
, NULL
));
7621 #endif /* HAVE_NS */
7623 #endif /* HAVE_GIF */
7626 #ifdef HAVE_IMAGEMAGICK
7628 /***********************************************************************
7630 ***********************************************************************/
7632 /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER,
7633 safely rounded and clipped to int range. */
7636 scale_image_size (int size
, size_t divisor
, size_t multiplier
)
7641 double scaled
= s
* multiplier
/ divisor
+ 0.5;
7642 if (scaled
< INT_MAX
)
7648 /* Compute the desired size of an image with native size WIDTH x HEIGHT.
7649 Use SPEC to deduce the size. Store the desired size into
7650 *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */
7652 compute_image_size (size_t width
, size_t height
,
7654 int *d_width
, int *d_height
)
7657 int desired_width
, desired_height
;
7659 /* If width and/or height is set in the display spec assume we want
7660 to scale to those values. If either h or w is unspecified, the
7661 unspecified should be calculated from the specified to preserve
7663 value
= image_spec_value (spec
, QCwidth
, NULL
);
7664 desired_width
= NATNUMP (value
) ? min (XFASTINT (value
), INT_MAX
) : -1;
7665 value
= image_spec_value (spec
, QCheight
, NULL
);
7666 desired_height
= NATNUMP (value
) ? min (XFASTINT (value
), INT_MAX
) : -1;
7668 if (desired_width
== -1)
7670 value
= image_spec_value (spec
, QCmax_width
, NULL
);
7671 if (NATNUMP (value
))
7673 int max_width
= min (XFASTINT (value
), INT_MAX
);
7674 if (max_width
< width
)
7676 /* The image is wider than :max-width. */
7677 desired_width
= max_width
;
7678 if (desired_height
== -1)
7680 desired_height
= scale_image_size (desired_width
,
7682 value
= image_spec_value (spec
, QCmax_height
, NULL
);
7683 if (NATNUMP (value
))
7685 int max_height
= min (XFASTINT (value
), INT_MAX
);
7686 if (max_height
< desired_height
)
7688 desired_height
= max_height
;
7689 desired_width
= scale_image_size (desired_height
,
7698 if (desired_height
== -1)
7700 value
= image_spec_value (spec
, QCmax_height
, NULL
);
7701 if (NATNUMP (value
))
7703 int max_height
= min (XFASTINT (value
), INT_MAX
);
7704 if (max_height
< height
)
7705 desired_height
= max_height
;
7709 if (desired_width
!= -1 && desired_height
== -1)
7710 /* w known, calculate h. */
7711 desired_height
= scale_image_size (desired_width
, width
, height
);
7713 if (desired_width
== -1 && desired_height
!= -1)
7714 /* h known, calculate w. */
7715 desired_width
= scale_image_size (desired_height
, height
, width
);
7717 *d_width
= desired_width
;
7718 *d_height
= desired_height
;
7721 static Lisp_Object Qimagemagick
;
7723 static bool imagemagick_image_p (Lisp_Object
);
7724 static bool imagemagick_load (struct frame
*, struct image
*);
7725 static void imagemagick_clear_image (struct frame
*, struct image
*);
7727 /* Indices of image specification fields in imagemagick_format. */
7729 enum imagemagick_keyword_index
7737 IMAGEMAGICK_ALGORITHM
,
7738 IMAGEMAGICK_HEURISTIC_MASK
,
7740 IMAGEMAGICK_BACKGROUND
,
7743 IMAGEMAGICK_MAX_HEIGHT
,
7744 IMAGEMAGICK_MAX_WIDTH
,
7745 IMAGEMAGICK_ROTATION
,
7750 /* Vector of image_keyword structures describing the format
7751 of valid user-defined image specifications. */
7753 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7755 {":type", IMAGE_SYMBOL_VALUE
, 1},
7756 {":data", IMAGE_STRING_VALUE
, 0},
7757 {":file", IMAGE_STRING_VALUE
, 0},
7758 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7759 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7760 {":relief", IMAGE_INTEGER_VALUE
, 0},
7761 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7762 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7763 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7764 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7765 {":height", IMAGE_INTEGER_VALUE
, 0},
7766 {":width", IMAGE_INTEGER_VALUE
, 0},
7767 {":max-height", IMAGE_INTEGER_VALUE
, 0},
7768 {":max-width", IMAGE_INTEGER_VALUE
, 0},
7769 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7770 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7773 #if defined HAVE_NTGUI && defined WINDOWSNT
7774 static bool init_imagemagick_functions (void);
7776 #define init_imagemagick_functions NULL
7779 /* Structure describing the image type for any image handled via
7782 static struct image_type imagemagick_type
=
7785 imagemagick_image_p
,
7787 imagemagick_clear_image
,
7788 init_imagemagick_functions
,
7792 /* Free X resources of imagemagick image IMG which is used on frame F. */
7795 imagemagick_clear_image (struct frame
*f
,
7798 x_clear_image (f
, img
);
7801 /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do
7802 this by calling parse_image_spec and supplying the keywords that
7803 identify the IMAGEMAGICK format. */
7806 imagemagick_image_p (Lisp_Object object
)
7808 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7809 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7811 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7814 /* Must specify either the :data or :file keyword. */
7815 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7818 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7819 Therefore rename the function so it doesn't collide with ImageMagick. */
7820 #define DrawRectangle DrawRectangleGif
7821 #include <wand/MagickWand.h>
7823 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7824 Emacs seems to work fine with the hidden version, so unhide it. */
7825 #include <magick/version.h>
7826 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7827 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7828 MagickPixelPacket
*);
7831 /* Log ImageMagick error message.
7832 Useful when a ImageMagick function returns the status `MagickFalse'. */
7835 imagemagick_error (MagickWand
*wand
)
7838 ExceptionType severity
;
7840 description
= MagickGetException (wand
, &severity
);
7841 image_error ("ImageMagick error: %s",
7842 build_string (description
),
7844 description
= (char *) MagickRelinquishMemory (description
);
7847 /* Helper function for imagemagick_load, which does the actual loading
7848 given contents and size, apart from frame and image structures,
7849 passed from imagemagick_load. Uses librimagemagick to do most of
7850 the image processing.
7852 F is a pointer to the Emacs frame; IMG to the image structure to
7853 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
7854 be parsed; SIZE is the number of bytes of data; and FILENAME is
7855 either the file name or the image data.
7857 Return true if successful. */
7860 imagemagick_load_image (struct frame
*f
, struct image
*img
,
7861 unsigned char *contents
, unsigned int size
,
7864 size_t width
, height
;
7865 MagickBooleanType status
;
7868 MagickWand
*image_wand
;
7869 MagickWand
*ping_wand
;
7870 PixelIterator
*iterator
;
7871 PixelWand
**pixels
, *bg_wand
= NULL
;
7872 MagickPixelPacket pixel
;
7877 int desired_width
, desired_height
;
7881 /* Handle image index for image types who can contain more than one image.
7882 Interface :index is same as for GIF. First we "ping" the image to see how
7883 many sub-images it contains. Pinging is faster than loading the image to
7884 find out things about it. */
7886 /* Initialize the imagemagick environment. */
7887 MagickWandGenesis ();
7888 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7889 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7890 ping_wand
= NewMagickWand ();
7891 /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */
7894 ? MagickPingImage (ping_wand
, filename
)
7895 : MagickPingImageBlob (ping_wand
, contents
, size
);
7897 if (status
== MagickFalse
)
7899 imagemagick_error (ping_wand
);
7900 DestroyMagickWand (ping_wand
);
7904 if (ino
< 0 || ino
>= MagickGetNumberImages (ping_wand
))
7906 image_error ("Invalid image number `%s' in image `%s'",
7908 DestroyMagickWand (ping_wand
);
7912 if (MagickGetNumberImages (ping_wand
) > 1)
7915 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7918 DestroyMagickWand (ping_wand
);
7920 /* Now we know how many images are inside the file. If it's not a
7921 bundle, the number is one. Load the image data. */
7923 image_wand
= NewMagickWand ();
7926 ? MagickReadImage (image_wand
, filename
)
7927 : MagickReadImageBlob (image_wand
, contents
, size
))
7930 imagemagick_error (image_wand
);
7931 goto imagemagick_error
;
7934 /* Retrieve the frame's background color, for use later. */
7937 Lisp_Object specified_bg
;
7939 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7940 if (!STRINGP (specified_bg
)
7941 || !x_defined_color (f
, SSDATA (specified_bg
), &bgcolor
, 0))
7944 bgcolor
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
7945 x_query_color (f
, &bgcolor
);
7947 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &bgcolor
, 1);
7951 bg_wand
= NewPixelWand ();
7952 PixelSetRed (bg_wand
, (double) bgcolor
.red
/ 65535);
7953 PixelSetGreen (bg_wand
, (double) bgcolor
.green
/ 65535);
7954 PixelSetBlue (bg_wand
, (double) bgcolor
.blue
/ 65535);
7957 compute_image_size (MagickGetImageWidth (image_wand
),
7958 MagickGetImageHeight (image_wand
),
7959 img
->spec
, &desired_width
, &desired_height
);
7961 if (desired_width
!= -1 && desired_height
!= -1)
7963 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7964 if (status
== MagickFalse
)
7966 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7967 imagemagick_error (image_wand
);
7968 goto imagemagick_error
;
7972 /* crop behaves similar to image slicing in Emacs but is more memory
7974 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7976 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7978 /* After some testing, it seems MagickCropImage is the fastest crop
7979 function in ImageMagick. This crop function seems to do less copying
7980 than the alternatives, but it still reads the entire image into memory
7981 before cropping, which is apparently difficult to avoid when using
7983 size_t crop_width
= XINT (XCAR (crop
));
7985 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7987 size_t crop_height
= XINT (XCAR (crop
));
7989 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7991 ssize_t crop_x
= XINT (XCAR (crop
));
7993 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7995 ssize_t crop_y
= XINT (XCAR (crop
));
7996 MagickCropImage (image_wand
, crop_width
, crop_height
,
8003 /* Furthermore :rotation. we need background color and angle for
8006 TODO background handling for rotation specified_bg =
8007 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
8009 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
8012 rotation
= extract_float (value
);
8013 status
= MagickRotateImage (image_wand
, bg_wand
, rotation
);
8014 if (status
== MagickFalse
)
8016 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
8017 imagemagick_error (image_wand
);
8018 goto imagemagick_error
;
8022 /* Set the canvas background color to the frame or specified
8023 background, and flatten the image. Note: as of ImageMagick
8024 6.6.0, SVG image transparency is not handled properly
8025 (e.g. etc/images/splash.svg shows a white background always). */
8027 MagickWand
*new_wand
;
8028 MagickSetImageBackgroundColor (image_wand
, bg_wand
);
8029 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
8030 new_wand
= MagickMergeImageLayers (image_wand
, MergeLayer
);
8032 new_wand
= MagickFlattenImages (image_wand
);
8034 DestroyMagickWand (image_wand
);
8035 image_wand
= new_wand
;
8038 /* Finally we are done manipulating the image. Figure out the
8039 resulting width/height and transfer ownership to Emacs. */
8040 height
= MagickGetImageHeight (image_wand
);
8041 width
= MagickGetImageWidth (image_wand
);
8043 if (! (width
<= INT_MAX
&& height
<= INT_MAX
8044 && check_image_size (f
, width
, height
)))
8046 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8047 goto imagemagick_error
;
8050 /* We can now get a valid pixel buffer from the imagemagick file, if all
8053 init_color_table ();
8055 #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS)
8056 if (imagemagick_render_type
!= 0)
8058 /* Magicexportimage is normally faster than pixelpushing. This
8059 method is also well tested. Some aspects of this method are
8060 ad-hoc and needs to be more researched. */
8061 int imagedepth
= 24; /*MagickGetImageDepth(image_wand);*/
8062 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP"; /*"RGBP";*/
8063 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8064 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, imagedepth
,
8067 #ifdef COLOR_TABLE_SUPPORT
8068 free_color_table ();
8070 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
8071 goto imagemagick_error
;
8074 /* Oddly, the below code doesn't seem to work:*/
8075 /* switch(ximg->bitmap_unit){ */
8077 /* pixelwidth=CharPixel; */
8080 /* pixelwidth=ShortPixel; */
8083 /* pixelwidth=LongPixel; */
8087 Here im just guessing the format of the bitmap.
8088 happens to work fine for:
8091 seems about 3 times as fast as pixel pushing(not carefully measured)
8093 pixelwidth
= CharPixel
; /*??? TODO figure out*/
8094 MagickExportImagePixels (image_wand
, 0, 0, width
, height
,
8095 exportdepth
, pixelwidth
, ximg
->data
);
8098 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
8100 size_t image_height
;
8102 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8103 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0,
8106 #ifdef COLOR_TABLE_SUPPORT
8107 free_color_table ();
8109 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
8110 goto imagemagick_error
;
8113 /* Copy imagemagick image to x with primitive yet robust pixel
8114 pusher loop. This has been tested a lot with many different
8117 /* Copy pixels from the imagemagick image structure to the x image map. */
8118 iterator
= NewPixelIterator (image_wand
);
8119 if (iterator
== (PixelIterator
*) NULL
)
8121 #ifdef COLOR_TABLE_SUPPORT
8122 free_color_table ();
8124 x_destroy_x_image (ximg
);
8125 image_error ("Imagemagick pixel iterator creation failed",
8127 goto imagemagick_error
;
8130 image_height
= MagickGetImageHeight (image_wand
);
8131 for (y
= 0; y
< image_height
; y
++)
8133 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
8134 if (pixels
== (PixelWand
**) NULL
)
8136 for (x
= 0; x
< (long) width
; x
++)
8138 PixelGetMagickColor (pixels
[x
], &pixel
);
8139 XPutPixel (ximg
, x
, y
,
8140 lookup_rgb_color (f
,
8146 DestroyPixelIterator (iterator
);
8149 #ifdef COLOR_TABLE_SUPPORT
8150 /* Remember colors allocated for this image. */
8151 img
->colors
= colors_in_color_table (&img
->ncolors
);
8152 free_color_table ();
8153 #endif /* COLOR_TABLE_SUPPORT */
8156 img
->height
= height
;
8158 /* Put ximg into the image. */
8159 image_put_x_image (f
, img
, ximg
, 0);
8161 /* Final cleanup. image_wand should be the only resource left. */
8162 DestroyMagickWand (image_wand
);
8163 if (bg_wand
) DestroyPixelWand (bg_wand
);
8165 /* `MagickWandTerminus' terminates the imagemagick environment. */
8166 MagickWandTerminus ();
8171 DestroyMagickWand (image_wand
);
8172 if (bg_wand
) DestroyPixelWand (bg_wand
);
8174 MagickWandTerminus ();
8175 /* TODO more cleanup. */
8176 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
8181 /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if
8182 successful. this function will go into the imagemagick_type structure, and
8183 the prototype thus needs to be compatible with that structure. */
8186 imagemagick_load (struct frame
*f
, struct image
*img
)
8189 Lisp_Object file_name
;
8191 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8192 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8193 if (STRINGP (file_name
))
8197 file
= x_find_image_file (file_name
);
8198 if (!STRINGP (file
))
8200 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8203 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
8205 /* Else its not a file, its a lisp object. Load the image from a
8206 lisp object rather than a file. */
8211 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8212 if (!STRINGP (data
))
8214 image_error ("Invalid image data `%s'", data
, Qnil
);
8217 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
8218 SBYTES (data
), NULL
);
8224 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
8225 doc
: /* Return a list of image types supported by ImageMagick.
8226 Each entry in this list is a symbol named after an ImageMagick format
8227 tag. See the ImageMagick manual for a list of ImageMagick formats and
8228 their descriptions (http://www.imagemagick.org/script/formats.php).
8229 You can also try the shell command: `identify -list format'.
8231 Note that ImageMagick recognizes many file-types that Emacs does not
8232 recognize as images, such as C. See `imagemagick-types-enable'
8233 and `imagemagick-types-inhibit'. */)
8236 Lisp_Object typelist
= Qnil
;
8241 Lisp_Object Qimagemagicktype
;
8243 GetExceptionInfo(&ex
);
8244 imtypes
= GetMagickList ("*", &numf
, &ex
);
8245 DestroyExceptionInfo(&ex
);
8247 for (i
= 0; i
< numf
; i
++)
8249 Qimagemagicktype
= intern (imtypes
[i
]);
8250 typelist
= Fcons (Qimagemagicktype
, typelist
);
8252 return Fnreverse (typelist
);
8255 #endif /* defined (HAVE_IMAGEMAGICK) */
8259 /***********************************************************************
8261 ***********************************************************************/
8263 #if defined (HAVE_RSVG)
8265 /* Function prototypes. */
8267 static bool svg_image_p (Lisp_Object object
);
8268 static bool svg_load (struct frame
*f
, struct image
*img
);
8270 static bool svg_load_image (struct frame
*, struct image
*,
8271 unsigned char *, ptrdiff_t);
8273 /* The symbol `svg' identifying images of this type. */
8275 static Lisp_Object Qsvg
;
8277 /* Indices of image specification fields in svg_format, below. */
8279 enum svg_keyword_index
8294 /* Vector of image_keyword structures describing the format
8295 of valid user-defined image specifications. */
8297 static const struct image_keyword svg_format
[SVG_LAST
] =
8299 {":type", IMAGE_SYMBOL_VALUE
, 1},
8300 {":data", IMAGE_STRING_VALUE
, 0},
8301 {":file", IMAGE_STRING_VALUE
, 0},
8302 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8303 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8304 {":relief", IMAGE_INTEGER_VALUE
, 0},
8305 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8306 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8307 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8308 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8311 #if defined HAVE_NTGUI && defined WINDOWSNT
8312 static bool init_svg_functions (void);
8314 #define init_svg_functions NULL
8317 /* Structure describing the image type `svg'. Its the same type of
8318 structure defined for all image formats, handled by emacs image
8319 functions. See struct image_type in dispextern.h. */
8321 static struct image_type svg_type
=
8332 /* Return true if OBJECT is a valid SVG image specification. Do
8333 this by calling parse_image_spec and supplying the keywords that
8334 identify the SVG format. */
8337 svg_image_p (Lisp_Object object
)
8339 struct image_keyword fmt
[SVG_LAST
];
8340 memcpy (fmt
, svg_format
, sizeof fmt
);
8342 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8345 /* Must specify either the :data or :file keyword. */
8346 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8349 #include <librsvg/rsvg.h>
8353 /* SVG library functions. */
8354 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
, (void));
8355 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
, (RsvgHandle
*, RsvgDimensionData
*));
8356 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
, (RsvgHandle
*, const guchar
*, gsize
, GError
**));
8357 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
, (RsvgHandle
*, GError
**));
8358 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
, (RsvgHandle
*));
8359 DEF_IMGLIB_FN (void *, rsvg_handle_set_size_callback
, (RsvgHandle
*, RsvgSizeFunc
, gpointer
, GDestroyNotify
));
8361 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
, (const GdkPixbuf
*));
8362 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
, (const GdkPixbuf
*));
8363 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
, (const GdkPixbuf
*));
8364 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
, (const GdkPixbuf
*));
8365 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
, (const GdkPixbuf
*));
8366 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
, (const GdkPixbuf
*));
8367 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
, (const GdkPixbuf
*));
8368 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
, (const GdkPixbuf
*));
8370 DEF_IMGLIB_FN (void, g_type_init
, (void));
8371 DEF_IMGLIB_FN (void, g_object_unref
, (gpointer
));
8372 DEF_IMGLIB_FN (void, g_error_free
, (GError
*));
8374 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8377 init_svg_functions (void)
8379 HMODULE library
, gdklib
, glib
, gobject
;
8381 if (!(glib
= w32_delayed_load (Qglib
))
8382 || !(gobject
= w32_delayed_load (Qgobject
))
8383 || !(gdklib
= w32_delayed_load (Qgdk_pixbuf
))
8384 || !(library
= w32_delayed_load (Qsvg
)))
8387 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8388 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8389 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8390 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8391 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8393 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8394 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8395 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8396 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8397 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8398 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8399 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8400 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8402 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8403 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8404 LOAD_IMGLIB_FN (glib
, g_error_free
);
8410 /* The following aliases for library functions allow dynamic loading
8411 to be used on some platforms. */
8412 #define fn_rsvg_handle_new rsvg_handle_new
8413 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8414 #define fn_rsvg_handle_write rsvg_handle_write
8415 #define fn_rsvg_handle_close rsvg_handle_close
8416 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8418 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8419 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8420 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8421 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8422 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8423 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8424 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8425 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8427 #define fn_g_type_init g_type_init
8428 #define fn_g_object_unref g_object_unref
8429 #define fn_g_error_free g_error_free
8430 #endif /* !WINDOWSNT */
8432 /* Load SVG image IMG for use on frame F. Value is true if
8436 svg_load (struct frame
*f
, struct image
*img
)
8439 Lisp_Object file_name
;
8441 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8442 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8443 if (STRINGP (file_name
))
8446 unsigned char *contents
;
8449 file
= x_find_image_file (file_name
);
8450 if (!STRINGP (file
))
8452 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8456 /* Read the entire file into memory. */
8457 contents
= slurp_file (SSDATA (file
), &size
);
8458 if (contents
== NULL
)
8460 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8463 /* If the file was slurped into memory properly, parse it. */
8464 success_p
= svg_load_image (f
, img
, contents
, size
);
8467 /* Else its not a file, its a lisp object. Load the image from a
8468 lisp object rather than a file. */
8473 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8474 if (!STRINGP (data
))
8476 image_error ("Invalid image data `%s'", data
, Qnil
);
8479 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8485 /* svg_load_image is a helper function for svg_load, which does the
8486 actual loading given contents and size, apart from frame and image
8487 structures, passed from svg_load.
8489 Uses librsvg to do most of the image processing.
8491 Returns true when successful. */
8493 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8494 struct image
*img
, /* Pointer to emacs image structure. */
8495 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8496 ptrdiff_t size
) /* Size of data in bytes. */
8498 RsvgHandle
*rsvg_handle
;
8499 RsvgDimensionData dimension_data
;
8504 const guint8
*pixels
;
8507 Lisp_Object specified_bg
;
8512 /* g_type_init is a glib function that must be called prior to using
8513 gnome type library functions. */
8515 /* Make a handle to a new rsvg object. */
8516 rsvg_handle
= fn_rsvg_handle_new ();
8518 /* Parse the contents argument and fill in the rsvg_handle. */
8519 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8520 if (err
) goto rsvg_error
;
8522 /* The parsing is complete, rsvg_handle is ready to used, close it
8523 for further writes. */
8524 fn_rsvg_handle_close (rsvg_handle
, &err
);
8525 if (err
) goto rsvg_error
;
8527 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8528 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8530 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8534 /* We can now get a valid pixel buffer from the svg file, if all
8536 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8537 if (!pixbuf
) goto rsvg_error
;
8538 fn_g_object_unref (rsvg_handle
);
8540 /* Extract some meta data from the svg handle. */
8541 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8542 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8543 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8544 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8546 /* Validate the svg meta data. */
8547 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8548 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8549 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8550 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8552 /* Try to create a x pixmap to hold the svg pixmap. */
8553 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
8555 fn_g_object_unref (pixbuf
);
8559 init_color_table ();
8561 /* Handle alpha channel by combining the image with a background
8563 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8564 if (!STRINGP (specified_bg
)
8565 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8568 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8569 x_query_color (f
, &background
);
8571 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8575 /* SVG pixmaps specify transparency in the last byte, so right
8576 shift 8 bits to get rid of it, since emacs doesn't support
8578 background
.red
>>= 8;
8579 background
.green
>>= 8;
8580 background
.blue
>>= 8;
8582 /* This loop handles opacity values, since Emacs assumes
8583 non-transparent images. Each pixel must be "flattened" by
8584 calculating the resulting color, given the transparency of the
8585 pixel, and the image background color. */
8586 for (y
= 0; y
< height
; ++y
)
8588 for (x
= 0; x
< width
; ++x
)
8598 opacity
= *pixels
++;
8600 red
= ((red
* opacity
)
8601 + (background
.red
* ((1 << 8) - opacity
)));
8602 green
= ((green
* opacity
)
8603 + (background
.green
* ((1 << 8) - opacity
)));
8604 blue
= ((blue
* opacity
)
8605 + (background
.blue
* ((1 << 8) - opacity
)));
8607 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8610 pixels
+= rowstride
- 4 * width
;
8613 #ifdef COLOR_TABLE_SUPPORT
8614 /* Remember colors allocated for this image. */
8615 img
->colors
= colors_in_color_table (&img
->ncolors
);
8616 free_color_table ();
8617 #endif /* COLOR_TABLE_SUPPORT */
8619 fn_g_object_unref (pixbuf
);
8622 img
->height
= height
;
8624 /* Maybe fill in the background field while we have ximg handy.
8625 Casting avoids a GCC warning. */
8626 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8628 /* Put ximg into the image. */
8629 image_put_x_image (f
, img
, ximg
, 0);
8634 fn_g_object_unref (rsvg_handle
);
8635 /* FIXME: Use error->message so the user knows what is the actual
8636 problem with the image. */
8637 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8638 fn_g_error_free (err
);
8642 #endif /* defined (HAVE_RSVG) */
8647 /***********************************************************************
8649 ***********************************************************************/
8651 #ifdef HAVE_X_WINDOWS
8652 #define HAVE_GHOSTSCRIPT 1
8653 #endif /* HAVE_X_WINDOWS */
8655 #ifdef HAVE_GHOSTSCRIPT
8657 static bool gs_image_p (Lisp_Object object
);
8658 static bool gs_load (struct frame
*f
, struct image
*img
);
8659 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8661 /* Keyword symbols. */
8663 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8665 /* Indices of image specification fields in gs_format, below. */
8667 enum gs_keyword_index
8685 /* Vector of image_keyword structures describing the format
8686 of valid user-defined image specifications. */
8688 static const struct image_keyword gs_format
[GS_LAST
] =
8690 {":type", IMAGE_SYMBOL_VALUE
, 1},
8691 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8692 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8693 {":file", IMAGE_STRING_VALUE
, 1},
8694 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8695 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8696 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8697 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8698 {":relief", IMAGE_INTEGER_VALUE
, 0},
8699 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8700 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8701 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8702 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8705 /* Structure describing the image type `ghostscript'. */
8707 static struct image_type gs_type
=
8718 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8721 gs_clear_image (struct frame
*f
, struct image
*img
)
8723 x_clear_image (f
, img
);
8727 /* Return true if OBJECT is a valid Ghostscript image
8731 gs_image_p (Lisp_Object object
)
8733 struct image_keyword fmt
[GS_LAST
];
8737 memcpy (fmt
, gs_format
, sizeof fmt
);
8739 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8742 /* Bounding box must be a list or vector containing 4 integers. */
8743 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8746 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8747 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8752 else if (VECTORP (tem
))
8754 if (ASIZE (tem
) != 4)
8756 for (i
= 0; i
< 4; ++i
)
8757 if (!INTEGERP (AREF (tem
, i
)))
8767 /* Load Ghostscript image IMG for use on frame F. Value is true
8771 gs_load (struct frame
*f
, struct image
*img
)
8773 uprintmax_t printnum1
, printnum2
;
8774 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
8775 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8777 double in_width
, in_height
;
8778 Lisp_Object pixel_colors
= Qnil
;
8780 /* Compute pixel size of pixmap needed from the given size in the
8781 image specification. Sizes in the specification are in pt. 1 pt
8782 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8784 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8785 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8786 in_width
*= FRAME_RES_X (f
);
8787 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8788 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8789 in_height
*= FRAME_RES_Y (f
);
8791 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
8792 && check_image_size (f
, in_width
, in_height
)))
8794 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8797 img
->width
= in_width
;
8798 img
->height
= in_height
;
8800 /* Create the pixmap. */
8801 eassert (img
->pixmap
== NO_PIXMAP
);
8803 if (x_check_image_size (0, img
->width
, img
->height
))
8805 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8807 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8808 img
->width
, img
->height
,
8809 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8815 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8819 /* Call the loader to fill the pixmap. It returns a process object
8820 if successful. We do not record_unwind_protect here because
8821 other places in redisplay like calling window scroll functions
8822 don't either. Let the Lisp loader use `unwind-protect' instead. */
8823 printnum1
= FRAME_X_WINDOW (f
);
8824 printnum2
= img
->pixmap
;
8825 window_and_pixmap_id
8826 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8828 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
8829 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
8831 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8833 XSETFRAME (frame
, f
);
8834 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8836 loader
= intern ("gs-load-image");
8838 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
8839 make_number (img
->width
),
8840 make_number (img
->height
),
8841 window_and_pixmap_id
,
8843 return PROCESSP (img
->lisp_data
);
8847 /* Kill the Ghostscript process that was started to fill PIXMAP on
8848 frame F. Called from XTread_socket when receiving an event
8849 telling Emacs that Ghostscript has finished drawing. */
8852 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8854 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8859 /* Find the image containing PIXMAP. */
8860 for (i
= 0; i
< c
->used
; ++i
)
8861 if (c
->images
[i
]->pixmap
== pixmap
)
8864 /* Should someone in between have cleared the image cache, for
8865 instance, give up. */
8869 /* Kill the GS process. We should have found PIXMAP in the image
8870 cache and its image should contain a process object. */
8872 eassert (PROCESSP (img
->lisp_data
));
8873 Fkill_process (img
->lisp_data
, Qnil
);
8874 img
->lisp_data
= Qnil
;
8876 #if defined (HAVE_X_WINDOWS)
8878 /* On displays with a mutable colormap, figure out the colors
8879 allocated for the image by looking at the pixels of an XImage for
8881 class = FRAME_X_VISUAL (f
)->class;
8882 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8888 /* Try to get an XImage for img->pixmep. */
8889 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8890 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8895 /* Initialize the color table. */
8896 init_color_table ();
8898 /* For each pixel of the image, look its color up in the
8899 color table. After having done so, the color table will
8900 contain an entry for each color used by the image. */
8901 for (y
= 0; y
< img
->height
; ++y
)
8902 for (x
= 0; x
< img
->width
; ++x
)
8904 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8905 lookup_pixel_color (f
, pixel
);
8908 /* Record colors in the image. Free color table and XImage. */
8909 #ifdef COLOR_TABLE_SUPPORT
8910 img
->colors
= colors_in_color_table (&img
->ncolors
);
8911 free_color_table ();
8913 XDestroyImage (ximg
);
8915 #if 0 /* This doesn't seem to be the case. If we free the colors
8916 here, we get a BadAccess later in x_clear_image when
8917 freeing the colors. */
8918 /* We have allocated colors once, but Ghostscript has also
8919 allocated colors on behalf of us. So, to get the
8920 reference counts right, free them once. */
8922 x_free_colors (f
, img
->colors
, img
->ncolors
);
8926 image_error ("Cannot get X image of `%s'; colors will not be freed",
8931 #endif /* HAVE_X_WINDOWS */
8933 /* Now that we have the pixmap, compute mask and transform the
8934 image if requested. */
8936 postprocess_image (f
, img
);
8940 #endif /* HAVE_GHOSTSCRIPT */
8943 /***********************************************************************
8945 ***********************************************************************/
8949 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8950 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8953 return valid_image_p (spec
) ? Qt
: Qnil
;
8957 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8962 if (valid_image_p (spec
))
8963 id
= lookup_image (SELECTED_FRAME (), spec
);
8966 return make_number (id
);
8969 #endif /* GLYPH_DEBUG */
8972 /***********************************************************************
8974 ***********************************************************************/
8976 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 1, 1, 0,
8977 doc
: /* Initialize image library implementing image type TYPE.
8978 Return non-nil if TYPE is a supported image type.
8980 If image libraries are loaded dynamically (currently only the case on
8981 MS-Windows), load the library for TYPE if it is not yet loaded, using
8982 the library file(s) specified by `dynamic-library-alist'. */)
8985 return lookup_image_type (type
) ? Qt
: Qnil
;
8988 /* Look up image type TYPE, and return a pointer to its image_type
8989 structure. Return 0 if TYPE is not a known image type. */
8991 static struct image_type
*
8992 lookup_image_type (Lisp_Object type
)
8994 /* Types pbm and xbm are built-in and always available. */
8995 if (EQ (type
, Qpbm
))
8996 return define_image_type (&pbm_type
);
8998 if (EQ (type
, Qxbm
))
8999 return define_image_type (&xbm_type
);
9001 #if defined (HAVE_XPM) || defined (HAVE_NS)
9002 if (EQ (type
, Qxpm
))
9003 return define_image_type (&xpm_type
);
9006 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9007 if (EQ (type
, Qjpeg
))
9008 return define_image_type (&jpeg_type
);
9011 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9012 if (EQ (type
, Qtiff
))
9013 return define_image_type (&tiff_type
);
9016 #if defined (HAVE_GIF) || defined (HAVE_NS)
9017 if (EQ (type
, Qgif
))
9018 return define_image_type (&gif_type
);
9021 #if defined (HAVE_PNG) || defined (HAVE_NS)
9022 if (EQ (type
, Qpng
))
9023 return define_image_type (&png_type
);
9026 #if defined (HAVE_RSVG)
9027 if (EQ (type
, Qsvg
))
9028 return define_image_type (&svg_type
);
9031 #if defined (HAVE_IMAGEMAGICK)
9032 if (EQ (type
, Qimagemagick
))
9033 return define_image_type (&imagemagick_type
);
9036 #ifdef HAVE_GHOSTSCRIPT
9037 if (EQ (type
, Qpostscript
))
9038 return define_image_type (&gs_type
);
9044 /* Reset image_types before dumping.
9045 Called from Fdump_emacs. */
9048 reset_image_types (void)
9052 struct image_type
*next
= image_types
->next
;
9053 xfree (image_types
);
9059 syms_of_image (void)
9061 /* Initialize this only once; it will be reset before dumping. */
9064 /* Must be defined now because we're going to update it below, while
9065 defining the supported image types. */
9066 DEFVAR_LISP ("image-types", Vimage_types
,
9067 doc
: /* List of potentially supported image types.
9068 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
9069 To check whether it is really supported, use `image-type-available-p'. */);
9070 Vimage_types
= Qnil
;
9072 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
9073 doc
: /* Maximum size of images.
9074 Emacs will not load an image into memory if its pixel width or
9075 pixel height exceeds this limit.
9077 If the value is an integer, it directly specifies the maximum
9078 image height and width, measured in pixels. If it is a floating
9079 point number, it specifies the maximum image height and width
9080 as a ratio to the frame height and width. If the value is
9081 non-numeric, there is no explicit limit on the size of images. */);
9082 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
9084 DEFSYM (Qcount
, "count");
9085 DEFSYM (Qextension_data
, "extension-data");
9086 DEFSYM (Qdelay
, "delay");
9088 DEFSYM (QCascent
, ":ascent");
9089 DEFSYM (QCmargin
, ":margin");
9090 DEFSYM (QCrelief
, ":relief");
9091 DEFSYM (QCconversion
, ":conversion");
9092 DEFSYM (QCcolor_symbols
, ":color-symbols");
9093 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
9094 DEFSYM (QCindex
, ":index");
9095 DEFSYM (QCgeometry
, ":geometry");
9096 DEFSYM (QCcrop
, ":crop");
9097 DEFSYM (QCrotation
, ":rotation");
9098 DEFSYM (QCmatrix
, ":matrix");
9099 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
9100 DEFSYM (QCmask
, ":mask");
9102 DEFSYM (Qlaplace
, "laplace");
9103 DEFSYM (Qemboss
, "emboss");
9104 DEFSYM (Qedge_detection
, "edge-detection");
9105 DEFSYM (Qheuristic
, "heuristic");
9107 DEFSYM (Qpostscript
, "postscript");
9108 DEFSYM (QCmax_width
, ":max-width");
9109 DEFSYM (QCmax_height
, ":max-height");
9110 #ifdef HAVE_GHOSTSCRIPT
9111 ADD_IMAGE_TYPE (Qpostscript
);
9112 DEFSYM (QCloader
, ":loader");
9113 DEFSYM (QCbounding_box
, ":bounding-box");
9114 DEFSYM (QCpt_width
, ":pt-width");
9115 DEFSYM (QCpt_height
, ":pt-height");
9116 #endif /* HAVE_GHOSTSCRIPT */
9119 DEFSYM (Qlibpng_version
, "libpng-version");
9120 Fset (Qlibpng_version
,
9122 make_number (PNG_LIBPNG_VER
)
9129 DEFSYM (Qpbm
, "pbm");
9130 ADD_IMAGE_TYPE (Qpbm
);
9132 DEFSYM (Qxbm
, "xbm");
9133 ADD_IMAGE_TYPE (Qxbm
);
9135 #if defined (HAVE_XPM) || defined (HAVE_NS)
9136 DEFSYM (Qxpm
, "xpm");
9137 ADD_IMAGE_TYPE (Qxpm
);
9140 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9141 DEFSYM (Qjpeg
, "jpeg");
9142 ADD_IMAGE_TYPE (Qjpeg
);
9145 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9146 DEFSYM (Qtiff
, "tiff");
9147 ADD_IMAGE_TYPE (Qtiff
);
9150 #if defined (HAVE_GIF) || defined (HAVE_NS)
9151 DEFSYM (Qgif
, "gif");
9152 ADD_IMAGE_TYPE (Qgif
);
9155 #if defined (HAVE_PNG) || defined (HAVE_NS)
9156 DEFSYM (Qpng
, "png");
9157 ADD_IMAGE_TYPE (Qpng
);
9160 #if defined (HAVE_IMAGEMAGICK)
9161 DEFSYM (Qimagemagick
, "imagemagick");
9162 ADD_IMAGE_TYPE (Qimagemagick
);
9165 #if defined (HAVE_RSVG)
9166 DEFSYM (Qsvg
, "svg");
9167 ADD_IMAGE_TYPE (Qsvg
);
9169 /* Other libraries used directly by svg code. */
9170 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
9171 DEFSYM (Qglib
, "glib");
9172 DEFSYM (Qgobject
, "gobject");
9173 #endif /* HAVE_NTGUI */
9174 #endif /* HAVE_RSVG */
9176 defsubr (&Sinit_image_library
);
9177 #ifdef HAVE_IMAGEMAGICK
9178 defsubr (&Simagemagick_types
);
9180 defsubr (&Sclear_image_cache
);
9181 defsubr (&Simage_flush
);
9182 defsubr (&Simage_size
);
9183 defsubr (&Simage_mask_p
);
9184 defsubr (&Simage_metadata
);
9188 defsubr (&Slookup_image
);
9191 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
9192 doc
: /* Non-nil means always draw a cross over disabled images.
9193 Disabled images are those having a `:conversion disabled' property.
9194 A cross is always drawn on black & white displays. */);
9195 cross_disabled_images
= 0;
9197 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
9198 doc
: /* List of directories to search for window system bitmap files. */);
9199 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
9201 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
9202 doc
: /* Maximum time after which images are removed from the cache.
9203 When an image has not been displayed this many seconds, Emacs
9204 automatically removes it from the image cache. If the cache contains
9205 a large number of images, the actual eviction time may be shorter.
9206 The value can also be nil, meaning the cache is never cleared.
9208 The function `clear-image-cache' disregards this variable. */);
9209 Vimage_cache_eviction_delay
= make_number (300);
9210 #ifdef HAVE_IMAGEMAGICK
9211 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type
,
9212 doc
: /* Integer indicating which ImageMagick rendering method to use.
9214 0 -- the default method (pixel pushing)
9215 1 -- a newer method ("MagickExportImagePixels") that may perform
9216 better (speed etc) in some cases, but has not been as thoroughly
9217 tested with Emacs as the default method. This method requires
9218 ImageMagick version 6.4.6 (approximately) or later.
9220 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9221 imagemagick_render_type
= 0;