1 /* Functions for image support on window system.
2 Copyright (C) 1989, 1992-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #if defined HAVE_LIBPNG_PNG_H
27 # include <libpng/png.h>
35 /* This makes the fields of a Display accessible, in Xlib header files. */
37 #define XLIB_ILLEGAL_ACCESS
42 #include "dispextern.h"
43 #include "blockinput.h"
46 #include "character.h"
48 #include "termhooks.h"
53 #include <sys/types.h>
56 #define COLOR_TABLE_SUPPORT 1
58 typedef struct x_bitmap_record Bitmap_Record
;
59 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
60 #define NO_PIXMAP None
62 #define RGB_PIXEL_COLOR unsigned long
64 #define PIX_MASK_RETAIN 0
65 #define PIX_MASK_DRAW 1
66 #endif /* HAVE_X_WINDOWS */
73 /* W32_TODO : Color tables on W32. */
74 #undef COLOR_TABLE_SUPPORT
76 typedef struct w32_bitmap_record Bitmap_Record
;
77 #define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y)
80 #define RGB_PIXEL_COLOR COLORREF
82 #define PIX_MASK_RETAIN 0
83 #define PIX_MASK_DRAW 1
85 #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual
86 #define x_defined_color w32_defined_color
87 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
89 /* Functions from w32term.c that depend on XColor (so can't go in w32term.h
90 without modifying lots of files). */
91 extern void x_query_colors (struct frame
*f
, XColor
*colors
, int ncolors
);
92 extern void x_query_color (struct frame
*f
, XColor
*color
);
94 /* Version of libpng that we were compiled with, or -1 if no PNG
95 support was compiled in. This is tested by w32-win.el to correctly
96 set up the alist used to search for PNG libraries. */
97 Lisp_Object Qlibpng_version
;
98 #endif /* HAVE_NTGUI */
102 #include <sys/types.h>
103 #include <sys/stat.h>
105 #undef COLOR_TABLE_SUPPORT
107 typedef struct ns_bitmap_record Bitmap_Record
;
109 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
112 #define RGB_PIXEL_COLOR unsigned long
115 #define PIX_MASK_RETAIN 0
116 #define PIX_MASK_DRAW 1
118 #define FRAME_X_VISUAL FRAME_NS_DISPLAY_INFO(f)->visual
119 #define x_defined_color(f, name, color_def, alloc) \
120 ns_defined_color (f, name, color_def, alloc, 0)
121 #define FRAME_X_SCREEN(f) 0
122 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
126 /* The symbol `postscript' identifying images of this type. */
128 static Lisp_Object Qpostscript
;
130 static void x_disable_image (struct frame
*, struct image
*);
131 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
134 static void init_color_table (void);
135 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
136 #ifdef COLOR_TABLE_SUPPORT
137 static void free_color_table (void);
138 static unsigned long *colors_in_color_table (int *n
);
140 static Lisp_Object
Finit_image_library (Lisp_Object
, Lisp_Object
);
142 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
143 id, which is just an int that this section returns. Bitmaps are
144 reference counted so they can be shared among frames.
146 Bitmap indices are guaranteed to be > 0, so a negative number can
147 be used to indicate no bitmap.
149 If you use x_create_bitmap_from_data, then you must keep track of
150 the bitmaps yourself. That is, creating a bitmap from the same
151 data more than once will not be caught. */
155 XGetImage (Display
*display
, Pixmap pixmap
, int x
, int y
,
156 unsigned int width
, unsigned int height
,
157 unsigned long plane_mask
, int format
)
159 /* TODO: not sure what this function is supposed to do.. */
160 ns_retain_object (pixmap
);
164 /* use with imgs created by ns_image_for_XPM */
166 XGetPixel (XImagePtr ximage
, int x
, int y
)
168 return ns_get_pixel (ximage
, x
, y
);
171 /* use with imgs created by ns_image_for_XPM; alpha set to 1;
172 pixel is assumed to be in form RGB */
174 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
176 ns_put_pixel (ximage
, x
, y
, pixel
);
181 /* Functions to access the contents of a bitmap, given an id. */
184 x_bitmap_height (FRAME_PTR f
, ptrdiff_t id
)
186 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
190 x_bitmap_width (FRAME_PTR f
, ptrdiff_t id
)
192 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
195 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
197 x_bitmap_pixmap (FRAME_PTR f
, ptrdiff_t id
)
199 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
203 #ifdef HAVE_X_WINDOWS
205 x_bitmap_mask (FRAME_PTR f
, ptrdiff_t id
)
207 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
211 /* Allocate a new bitmap record. Returns index of new record. */
214 x_allocate_bitmap_record (FRAME_PTR f
)
216 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
219 if (dpyinfo
->bitmaps
== NULL
)
222 = (Bitmap_Record
*) xmalloc (10 * sizeof (Bitmap_Record
));
223 dpyinfo
->bitmaps_size
= 10;
224 dpyinfo
->bitmaps_last
= 1;
228 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
229 return ++dpyinfo
->bitmaps_last
;
231 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
232 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
235 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (Bitmap_Record
) / 2
236 < dpyinfo
->bitmaps_size
)
237 memory_full (SIZE_MAX
);
239 = (Bitmap_Record
*) xrealloc (dpyinfo
->bitmaps
,
240 (dpyinfo
->bitmaps_size
241 * (2 * sizeof (Bitmap_Record
))));
242 dpyinfo
->bitmaps_size
*= 2;
243 return ++dpyinfo
->bitmaps_last
;
246 /* Add one reference to the reference count of the bitmap with id ID. */
249 x_reference_bitmap (FRAME_PTR f
, ptrdiff_t id
)
251 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
254 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
257 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
259 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
262 #ifdef HAVE_X_WINDOWS
264 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
265 bits
, width
, height
);
268 #endif /* HAVE_X_WINDOWS */
272 bitmap
= CreateBitmap (width
, height
,
273 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
274 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
278 #endif /* HAVE_NTGUI */
281 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
286 id
= x_allocate_bitmap_record (f
);
289 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
290 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
293 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
294 dpyinfo
->bitmaps
[id
- 1].height
= height
;
295 dpyinfo
->bitmaps
[id
- 1].width
= width
;
296 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
298 #ifdef HAVE_X_WINDOWS
299 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
300 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
301 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
302 #endif /* HAVE_X_WINDOWS */
305 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
306 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
307 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
308 #endif /* HAVE_NTGUI */
313 /* Create bitmap from file FILE for frame F. */
316 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
318 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
321 return -1; /* W32_TODO : bitmap support */
322 #endif /* HAVE_NTGUI */
326 void *bitmap
= ns_image_from_file (file
);
332 id
= x_allocate_bitmap_record (f
);
333 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
334 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
335 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
336 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
337 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
338 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
339 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
343 #ifdef HAVE_X_WINDOWS
344 unsigned int width
, height
;
346 int xhot
, yhot
, result
;
352 /* Look for an existing bitmap with the same name. */
353 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
355 if (dpyinfo
->bitmaps
[id
].refcount
356 && dpyinfo
->bitmaps
[id
].file
357 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
359 ++dpyinfo
->bitmaps
[id
].refcount
;
364 /* Search bitmap-file-path for the file, if appropriate. */
365 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
370 filename
= SSDATA (found
);
372 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
373 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
374 if (result
!= BitmapSuccess
)
377 id
= x_allocate_bitmap_record (f
);
378 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
379 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
380 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
381 dpyinfo
->bitmaps
[id
- 1].file
= (char *) xmalloc (SBYTES (file
) + 1);
382 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
383 dpyinfo
->bitmaps
[id
- 1].height
= height
;
384 dpyinfo
->bitmaps
[id
- 1].width
= width
;
385 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
388 #endif /* HAVE_X_WINDOWS */
394 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
396 #ifdef HAVE_X_WINDOWS
397 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
399 XFreePixmap (dpyinfo
->display
, bm
->mask
);
400 #endif /* HAVE_X_WINDOWS */
403 DeleteObject (bm
->pixmap
);
404 #endif /* HAVE_NTGUI */
407 ns_release_object (bm
->img
);
417 /* Remove reference to bitmap with id number ID. */
420 x_destroy_bitmap (FRAME_PTR f
, ptrdiff_t id
)
422 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
426 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
428 if (--bm
->refcount
== 0)
431 free_bitmap_record (dpyinfo
, bm
);
437 /* Free all the bitmaps for the display specified by DPYINFO. */
440 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
443 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
445 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
446 if (bm
->refcount
> 0)
447 free_bitmap_record (dpyinfo
, bm
);
449 dpyinfo
->bitmaps_last
= 0;
453 #ifdef HAVE_X_WINDOWS
455 /* Useful functions defined in the section
456 `Image type independent image structures' below. */
458 static unsigned long four_corners_best (XImagePtr ximg
,
461 unsigned long height
);
463 static int x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
,
464 int depth
, XImagePtr
*ximg
,
467 static void x_destroy_x_image (XImagePtr ximg
);
470 /* Create a mask of a bitmap. Note is this not a perfect mask.
471 It's nicer with some borders in this context */
474 x_create_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
477 XImagePtr ximg
, mask_img
;
478 unsigned long width
, height
;
481 unsigned long x
, y
, xp
, xm
, yp
, ym
;
484 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
489 pixmap
= x_bitmap_pixmap (f
, id
);
490 width
= x_bitmap_width (f
, id
);
491 height
= x_bitmap_height (f
, id
);
494 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
503 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
508 XDestroyImage (ximg
);
512 bg
= four_corners_best (ximg
, NULL
, width
, height
);
514 for (y
= 0; y
< ximg
->height
; ++y
)
516 for (x
= 0; x
< ximg
->width
; ++x
)
518 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
519 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
520 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
521 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
522 if (XGetPixel (ximg
, x
, y
) == bg
523 && XGetPixel (ximg
, x
, yp
) == bg
524 && XGetPixel (ximg
, x
, ym
) == bg
525 && XGetPixel (ximg
, xp
, y
) == bg
526 && XGetPixel (ximg
, xp
, yp
) == bg
527 && XGetPixel (ximg
, xp
, ym
) == bg
528 && XGetPixel (ximg
, xm
, y
) == bg
529 && XGetPixel (ximg
, xm
, yp
) == bg
530 && XGetPixel (ximg
, xm
, ym
) == bg
)
531 XPutPixel (mask_img
, x
, y
, 0);
533 XPutPixel (mask_img
, x
, y
, 1);
537 xassert (interrupt_input_blocked
);
538 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
539 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
541 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
543 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
544 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
546 XDestroyImage (ximg
);
547 x_destroy_x_image (mask_img
);
552 #endif /* HAVE_X_WINDOWS */
555 /***********************************************************************
557 ***********************************************************************/
559 /* List of supported image types. Use define_image_type to add new
560 types. Use lookup_image_type to find a type for a given symbol. */
562 static struct image_type
*image_types
;
564 /* The symbol `xbm' which is used as the type symbol for XBM images. */
566 static Lisp_Object Qxbm
;
570 Lisp_Object QCascent
, QCmargin
, QCrelief
;
571 Lisp_Object QCconversion
;
572 static Lisp_Object QCheuristic_mask
;
573 static Lisp_Object QCcolor_symbols
;
574 static Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
;
575 static Lisp_Object QCcrop
, QCrotation
;
579 static Lisp_Object Qcount
, Qextension_data
, Qdelay
;
580 static Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
582 /* Function prototypes. */
584 static Lisp_Object
define_image_type (struct image_type
*type
, int loaded
);
585 static struct image_type
*lookup_image_type (Lisp_Object symbol
);
586 static void image_error (const char *format
, Lisp_Object
, Lisp_Object
);
587 static void x_laplace (struct frame
*, struct image
*);
588 static void x_emboss (struct frame
*, struct image
*);
589 static int x_build_heuristic_mask (struct frame
*, struct image
*,
592 #define CACHE_IMAGE_TYPE(type, status) \
593 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
595 #define CACHE_IMAGE_TYPE(type, status)
598 #define ADD_IMAGE_TYPE(type) \
599 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
601 /* Define a new image type from TYPE. This adds a copy of TYPE to
602 image_types and caches the loading status of TYPE. */
605 define_image_type (struct image_type
*type
, int loaded
)
613 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
614 The initialized data segment is read-only. */
615 struct image_type
*p
= (struct image_type
*) xmalloc (sizeof *p
);
616 memcpy (p
, type
, sizeof *p
);
617 p
->next
= image_types
;
622 CACHE_IMAGE_TYPE (*type
->type
, success
);
627 /* Look up image type SYMBOL, and return a pointer to its image_type
628 structure. Value is null if SYMBOL is not a known image type. */
630 static inline struct image_type
*
631 lookup_image_type (Lisp_Object symbol
)
633 struct image_type
*type
;
635 /* We must initialize the image-type if it hasn't been already. */
636 if (NILP (Finit_image_library (symbol
, Vdynamic_library_alist
)))
637 return 0; /* unimplemented */
639 for (type
= image_types
; type
; type
= type
->next
)
640 if (EQ (symbol
, *type
->type
))
647 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
648 valid image specification is a list whose car is the symbol
649 `image', and whose rest is a property list. The property list must
650 contain a value for key `:type'. That value must be the name of a
651 supported image type. The rest of the property list depends on the
655 valid_image_p (Lisp_Object object
)
663 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
664 if (EQ (XCAR (tem
), QCtype
))
667 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
669 struct image_type
*type
;
670 type
= lookup_image_type (XCAR (tem
));
672 valid_p
= type
->valid_p (object
);
683 /* Log error message with format string FORMAT and argument ARG.
684 Signaling an error, e.g. when an image cannot be loaded, is not a
685 good idea because this would interrupt redisplay, and the error
686 message display would lead to another redisplay. This function
687 therefore simply displays a message. */
690 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
692 add_to_log (format
, arg1
, arg2
);
697 /***********************************************************************
699 ***********************************************************************/
701 enum image_value_type
703 IMAGE_DONT_CHECK_VALUE_TYPE
,
705 IMAGE_STRING_OR_NIL_VALUE
,
707 IMAGE_POSITIVE_INTEGER_VALUE
,
708 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
,
709 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
712 IMAGE_FUNCTION_VALUE
,
717 /* Structure used when parsing image specifications. */
721 /* Name of keyword. */
724 /* The type of value allowed. */
725 enum image_value_type type
;
727 /* Non-zero means key must be present. */
730 /* Used to recognize duplicate keywords in a property list. */
733 /* The value that was found. */
738 static int parse_image_spec (Lisp_Object
, struct image_keyword
*,
740 static Lisp_Object
image_spec_value (Lisp_Object
, Lisp_Object
, int *);
743 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
744 has the format (image KEYWORD VALUE ...). One of the keyword/
745 value pairs must be `:type TYPE'. KEYWORDS is a vector of
746 image_keywords structures of size NKEYWORDS describing other
747 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
750 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
751 int nkeywords
, Lisp_Object type
)
760 while (CONSP (plist
))
762 Lisp_Object key
, value
;
764 /* First element of a pair must be a symbol. */
766 plist
= XCDR (plist
);
770 /* There must follow a value. */
773 value
= XCAR (plist
);
774 plist
= XCDR (plist
);
776 /* Find key in KEYWORDS. Error if not found. */
777 for (i
= 0; i
< nkeywords
; ++i
)
778 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
784 /* Record that we recognized the keyword. If a keywords
785 was found more than once, it's an error. */
786 keywords
[i
].value
= value
;
789 if (keywords
[i
].count
> 1)
792 /* Check type of value against allowed type. */
793 switch (keywords
[i
].type
)
795 case IMAGE_STRING_VALUE
:
796 if (!STRINGP (value
))
800 case IMAGE_STRING_OR_NIL_VALUE
:
801 if (!STRINGP (value
) && !NILP (value
))
805 case IMAGE_SYMBOL_VALUE
:
806 if (!SYMBOLP (value
))
810 case IMAGE_POSITIVE_INTEGER_VALUE
:
811 if (! RANGED_INTEGERP (1, value
, INT_MAX
))
815 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
:
816 if (RANGED_INTEGERP (1, value
, INT_MAX
))
819 && RANGED_INTEGERP (1, XCAR (value
), INT_MAX
)
820 && RANGED_INTEGERP (1, XCDR (value
), INT_MAX
))
824 case IMAGE_ASCENT_VALUE
:
825 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
827 else if (RANGED_INTEGERP (0, value
, 100))
831 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
832 /* Unlike the other integer-related cases, this one does not
833 verify that VALUE fits in 'int'. This is because callers
835 if (!INTEGERP (value
) || XINT (value
) < 0)
839 case IMAGE_DONT_CHECK_VALUE_TYPE
:
842 case IMAGE_FUNCTION_VALUE
:
843 value
= indirect_function (value
);
844 if (!NILP (Ffunctionp (value
)))
848 case IMAGE_NUMBER_VALUE
:
849 if (!INTEGERP (value
) && !FLOATP (value
))
853 case IMAGE_INTEGER_VALUE
:
854 if (! TYPE_RANGED_INTEGERP (int, value
))
858 case IMAGE_BOOL_VALUE
:
859 if (!NILP (value
) && !EQ (value
, Qt
))
868 if (EQ (key
, QCtype
) && !EQ (type
, value
))
872 /* Check that all mandatory fields are present. */
873 for (i
= 0; i
< nkeywords
; ++i
)
874 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
881 /* Return the value of KEY in image specification SPEC. Value is nil
882 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
883 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
886 image_spec_value (Lisp_Object spec
, Lisp_Object key
, int *found
)
890 xassert (valid_image_p (spec
));
892 for (tail
= XCDR (spec
);
893 CONSP (tail
) && CONSP (XCDR (tail
));
894 tail
= XCDR (XCDR (tail
)))
896 if (EQ (XCAR (tail
), key
))
900 return XCAR (XCDR (tail
));
910 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
911 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
912 PIXELS non-nil means return the size in pixels, otherwise return the
913 size in canonical character units.
914 FRAME is the frame on which the image will be displayed. FRAME nil
915 or omitted means use the selected frame. */)
916 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
921 if (valid_image_p (spec
))
923 struct frame
*f
= check_x_frame (frame
);
924 ptrdiff_t id
= lookup_image (f
, spec
);
925 struct image
*img
= IMAGE_FROM_ID (f
, id
);
926 int width
= img
->width
+ 2 * img
->hmargin
;
927 int height
= img
->height
+ 2 * img
->vmargin
;
930 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
931 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
933 size
= Fcons (make_number (width
), make_number (height
));
936 error ("Invalid image specification");
942 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
943 doc
: /* Return t if image SPEC has a mask bitmap.
944 FRAME is the frame on which the image will be displayed. FRAME nil
945 or omitted means use the selected frame. */)
946 (Lisp_Object spec
, Lisp_Object frame
)
951 if (valid_image_p (spec
))
953 struct frame
*f
= check_x_frame (frame
);
954 ptrdiff_t id
= lookup_image (f
, spec
);
955 struct image
*img
= IMAGE_FROM_ID (f
, id
);
960 error ("Invalid image specification");
965 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
966 doc
: /* Return metadata for image SPEC.
967 FRAME is the frame on which the image will be displayed. FRAME nil
968 or omitted means use the selected frame. */)
969 (Lisp_Object spec
, Lisp_Object frame
)
974 if (valid_image_p (spec
))
976 struct frame
*f
= check_x_frame (frame
);
977 ptrdiff_t id
= lookup_image (f
, spec
);
978 struct image
*img
= IMAGE_FROM_ID (f
, id
);
979 ext
= img
->lisp_data
;
986 /***********************************************************************
987 Image type independent image structures
988 ***********************************************************************/
990 static void free_image (struct frame
*f
, struct image
*img
);
992 #define MAX_IMAGE_SIZE 6.0
993 /* Allocate and return a new image structure for image specification
994 SPEC. SPEC has a hash value of HASH. */
996 static struct image
*
997 make_image (Lisp_Object spec
, EMACS_UINT hash
)
999 struct image
*img
= (struct image
*) xmalloc (sizeof *img
);
1000 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
1002 xassert (valid_image_p (spec
));
1003 memset (img
, 0, sizeof *img
);
1004 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
1005 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
1006 xassert (img
->type
!= NULL
);
1008 img
->lisp_data
= Qnil
;
1009 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
1011 img
->corners
[BOT_CORNER
] = -1; /* Full image */
1016 /* Free image IMG which was used on frame F, including its resources. */
1019 free_image (struct frame
*f
, struct image
*img
)
1023 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1025 /* Remove IMG from the hash table of its cache. */
1027 img
->prev
->next
= img
->next
;
1029 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1032 img
->next
->prev
= img
->prev
;
1034 c
->images
[img
->id
] = NULL
;
1036 /* Free resources, then free IMG. */
1037 img
->type
->free (f
, img
);
1042 /* Return 1 if the given widths and heights are valid for display;
1043 otherwise, return 0. */
1046 check_image_size (struct frame
*f
, int width
, int height
)
1050 if (width
<= 0 || height
<= 0)
1053 if (INTEGERP (Vmax_image_size
))
1054 return (width
<= XINT (Vmax_image_size
)
1055 && height
<= XINT (Vmax_image_size
));
1056 else if (FLOATP (Vmax_image_size
))
1060 w
= FRAME_PIXEL_WIDTH (f
);
1061 h
= FRAME_PIXEL_HEIGHT (f
);
1064 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1065 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1066 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1072 /* Prepare image IMG for display on frame F. Must be called before
1073 drawing an image. */
1076 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1080 /* We're about to display IMG, so set its timestamp to `now'. */
1082 img
->timestamp
= EMACS_SECS (t
);
1084 /* If IMG doesn't have a pixmap yet, load it now, using the image
1085 type dependent loader function. */
1086 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1087 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1092 /* Value is the number of pixels for the ascent of image IMG when
1093 drawn in face FACE. */
1096 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1101 if (slice
->height
== img
->height
)
1102 height
= img
->height
+ img
->vmargin
;
1103 else if (slice
->y
== 0)
1104 height
= slice
->height
+ img
->vmargin
;
1106 height
= slice
->height
;
1108 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1113 /* W32 specific version. Why?. ++kfs */
1114 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1115 - FONT_BASE (face
->font
)) / 2;
1117 /* This expression is arranged so that if the image can't be
1118 exactly centered, it will be moved slightly up. This is
1119 because a typical font is `top-heavy' (due to the presence
1120 uppercase letters), so the image placement should err towards
1121 being top-heavy too. It also just generally looks better. */
1122 ascent
= (height
+ FONT_BASE(face
->font
)
1123 - FONT_DESCENT(face
->font
) + 1) / 2;
1124 #endif /* HAVE_NTGUI */
1127 ascent
= height
/ 2;
1130 ascent
= height
* (img
->ascent
/ 100.0);
1136 /* Image background colors. */
1138 /* Find the "best" corner color of a bitmap.
1139 On W32, XIMG is assumed to a device context with the bitmap selected. */
1141 static RGB_PIXEL_COLOR
1142 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1143 unsigned long width
, unsigned long height
)
1145 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1148 if (corners
&& corners
[BOT_CORNER
] >= 0)
1150 /* Get the colors at the corner_pixels of ximg. */
1151 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1152 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1153 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1154 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1158 /* Get the colors at the corner_pixels of ximg. */
1159 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1160 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1161 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1162 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1164 /* Choose the most frequently found color as background. */
1165 for (i
= best_count
= 0; i
< 4; ++i
)
1169 for (j
= n
= 0; j
< 4; ++j
)
1170 if (corner_pixels
[i
] == corner_pixels
[j
])
1174 best
= corner_pixels
[i
], best_count
= n
;
1180 /* Portability macros */
1184 #define Destroy_Image(img_dc, prev) \
1185 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1187 #define Free_Pixmap(display, pixmap) \
1188 DeleteObject (pixmap)
1190 #elif defined (HAVE_NS)
1192 #define Destroy_Image(ximg, dummy) \
1193 ns_release_object (ximg)
1195 #define Free_Pixmap(display, pixmap) \
1196 ns_release_object (pixmap)
1200 #define Destroy_Image(ximg, dummy) \
1201 XDestroyImage (ximg)
1203 #define Free_Pixmap(display, pixmap) \
1204 XFreePixmap (display, pixmap)
1206 #endif /* !HAVE_NTGUI && !HAVE_NS */
1209 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1210 it is guessed heuristically. If non-zero, XIMG is an existing
1211 XImage object (or device context with the image selected on W32) to
1212 use for the heuristic. */
1215 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1217 if (! img
->background_valid
)
1218 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1220 int free_ximg
= !ximg
;
1223 #endif /* HAVE_NTGUI */
1228 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
1229 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1231 HDC frame_dc
= get_frame_dc (f
);
1232 ximg
= CreateCompatibleDC (frame_dc
);
1233 release_frame_dc (f
, frame_dc
);
1234 prev
= SelectObject (ximg
, img
->pixmap
);
1235 #endif /* !HAVE_NTGUI */
1238 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1241 Destroy_Image (ximg
, prev
);
1243 img
->background_valid
= 1;
1246 return img
->background
;
1249 /* Return the `background_transparent' field of IMG. If IMG doesn't
1250 have one yet, it is guessed heuristically. If non-zero, MASK is an
1251 existing XImage object to use for the heuristic. */
1254 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1256 if (! img
->background_transparent_valid
)
1257 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1261 int free_mask
= !mask
;
1264 #endif /* HAVE_NTGUI */
1269 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
1270 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
1272 HDC frame_dc
= get_frame_dc (f
);
1273 mask
= CreateCompatibleDC (frame_dc
);
1274 release_frame_dc (f
, frame_dc
);
1275 prev
= SelectObject (mask
, img
->mask
);
1276 #endif /* HAVE_NTGUI */
1279 img
->background_transparent
1280 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1283 Destroy_Image (mask
, prev
);
1286 img
->background_transparent
= 0;
1288 img
->background_transparent_valid
= 1;
1291 return img
->background_transparent
;
1295 /***********************************************************************
1296 Helper functions for X image types
1297 ***********************************************************************/
1299 static void x_clear_image_1 (struct frame
*, struct image
*, int,
1301 static void x_clear_image (struct frame
*f
, struct image
*img
);
1302 static unsigned long x_alloc_image_color (struct frame
*f
,
1304 Lisp_Object color_name
,
1305 unsigned long dflt
);
1308 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1309 free the pixmap if any. MASK_P non-zero means clear the mask
1310 pixmap if any. COLORS_P non-zero means free colors allocated for
1311 the image, if any. */
1314 x_clear_image_1 (struct frame
*f
, struct image
*img
, int pixmap_p
, int mask_p
,
1317 if (pixmap_p
&& img
->pixmap
)
1319 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1320 img
->pixmap
= NO_PIXMAP
;
1321 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1322 img
->background_valid
= 0;
1325 if (mask_p
&& img
->mask
)
1327 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1328 img
->mask
= NO_PIXMAP
;
1329 img
->background_transparent_valid
= 0;
1332 if (colors_p
&& img
->ncolors
)
1334 /* W32_TODO: color table support. */
1335 #ifdef HAVE_X_WINDOWS
1336 x_free_colors (f
, img
->colors
, img
->ncolors
);
1337 #endif /* HAVE_X_WINDOWS */
1338 xfree (img
->colors
);
1345 /* Free X resources of image IMG which is used on frame F. */
1348 x_clear_image (struct frame
*f
, struct image
*img
)
1351 x_clear_image_1 (f
, img
, 1, 1, 1);
1356 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1357 cannot be allocated, use DFLT. Add a newly allocated color to
1358 IMG->colors, so that it can be freed again. Value is the pixel
1361 static unsigned long
1362 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1366 unsigned long result
;
1368 xassert (STRINGP (color_name
));
1370 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1371 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1374 /* This isn't called frequently so we get away with simply
1375 reallocating the color vector to the needed size, here. */
1376 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1378 (unsigned long *) xrealloc (img
->colors
,
1379 ncolors
* sizeof *img
->colors
);
1380 img
->colors
[ncolors
- 1] = color
.pixel
;
1381 img
->ncolors
= ncolors
;
1382 result
= color
.pixel
;
1392 /***********************************************************************
1394 ***********************************************************************/
1396 static void cache_image (struct frame
*f
, struct image
*img
);
1397 static void postprocess_image (struct frame
*, struct image
*);
1399 /* Return a new, initialized image cache that is allocated from the
1400 heap. Call free_image_cache to free an image cache. */
1402 struct image_cache
*
1403 make_image_cache (void)
1405 struct image_cache
*c
= (struct image_cache
*) xmalloc (sizeof *c
);
1408 memset (c
, 0, sizeof *c
);
1410 c
->images
= (struct image
**) xmalloc (size
* sizeof *c
->images
);
1412 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1413 c
->buckets
= (struct image
**) xmalloc (size
);
1414 memset (c
->buckets
, 0, size
);
1419 /* Find an image matching SPEC in the cache, and return it. If no
1420 image is found, return NULL. */
1421 static struct image
*
1422 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1425 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1426 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1428 if (!c
) return NULL
;
1430 /* If the image spec does not specify a background color, the cached
1431 image must have the same background color as the current frame.
1432 The foreground color must also match, for the sake of monochrome
1435 In fact, we could ignore the foreground color matching condition
1436 for color images, or if the image spec specifies :foreground;
1437 similarly we could ignore the background color matching condition
1438 for formats that don't use transparency (such as jpeg), or if the
1439 image spec specifies :background. However, the extra memory
1440 usage is probably negligible in practice, so we don't bother. */
1442 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1443 if (img
->hash
== hash
1444 && !NILP (Fequal (img
->spec
, spec
))
1445 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1446 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1452 /* Search frame F for an image with spec SPEC, and free it. */
1455 uncache_image (struct frame
*f
, Lisp_Object spec
)
1457 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1460 free_image (f
, img
);
1461 /* As display glyphs may still be referring to the image ID, we
1462 must garbage the frame (Bug#6426). */
1463 SET_FRAME_GARBAGED (f
);
1468 /* Free image cache of frame F. Be aware that X frames share images
1472 free_image_cache (struct frame
*f
)
1474 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1479 /* Cache should not be referenced by any frame when freed. */
1480 xassert (c
->refcount
== 0);
1482 for (i
= 0; i
< c
->used
; ++i
)
1483 free_image (f
, c
->images
[i
]);
1487 FRAME_IMAGE_CACHE (f
) = NULL
;
1492 /* Clear image cache of frame F. FILTER=t means free all images.
1493 FILTER=nil means clear only images that haven't been
1494 displayed for some time.
1495 Else, only free the images which have FILTER in their `dependencies'.
1496 Should be called from time to time to reduce the number of loaded images.
1497 If image-cache-eviction-delay is non-nil, this frees images in the cache
1498 which weren't displayed for at least that many seconds. */
1501 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1503 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1507 ptrdiff_t i
, nfreed
= 0;
1509 /* Block input so that we won't be interrupted by a SIGIO
1510 while being in an inconsistent state. */
1515 /* Filter image cache. */
1516 for (i
= 0; i
< c
->used
; ++i
)
1518 struct image
*img
= c
->images
[i
];
1519 if (img
&& (EQ (Qt
, filter
)
1520 || !NILP (Fmember (filter
, img
->dependencies
))))
1522 free_image (f
, img
);
1527 else if (INTEGERP (Vimage_cache_eviction_delay
))
1529 /* Free cache based on timestamp. */
1532 ptrdiff_t nimages
= 0;
1534 for (i
= 0; i
< c
->used
; ++i
)
1538 /* If the number of cached images has grown unusually large,
1539 decrease the cache eviction delay (Bug#6230). */
1540 delay
= XINT (Vimage_cache_eviction_delay
);
1542 delay
= 1600 * delay
/ nimages
/ nimages
;
1543 delay
= max (delay
, 1);
1546 old
= EMACS_SECS (t
) - delay
;
1548 for (i
= 0; i
< c
->used
; ++i
)
1550 struct image
*img
= c
->images
[i
];
1551 if (img
&& img
->timestamp
< old
)
1553 free_image (f
, img
);
1559 /* We may be clearing the image cache because, for example,
1560 Emacs was iconified for a longer period of time. In that
1561 case, current matrices may still contain references to
1562 images freed above. So, clear these matrices. */
1565 Lisp_Object tail
, frame
;
1567 FOR_EACH_FRAME (tail
, frame
)
1569 struct frame
*fr
= XFRAME (frame
);
1570 if (FRAME_IMAGE_CACHE (fr
) == c
)
1571 clear_current_matrices (fr
);
1574 ++windows_or_buffers_changed
;
1582 clear_image_caches (Lisp_Object filter
)
1584 /* FIXME: We want to do
1585 * struct terminal *t;
1586 * for (t = terminal_list; t; t = t->next_terminal)
1587 * clear_image_cache (t, filter); */
1588 Lisp_Object tail
, frame
;
1589 FOR_EACH_FRAME (tail
, frame
)
1590 if (FRAME_WINDOW_P (XFRAME (frame
)))
1591 clear_image_cache (XFRAME (frame
), filter
);
1594 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1596 doc
: /* Clear the image cache.
1597 FILTER nil or a frame means clear all images in the selected frame.
1598 FILTER t means clear the image caches of all frames.
1599 Anything else, means only clear those images which refer to FILTER,
1600 which is then usually a filename. */)
1601 (Lisp_Object filter
)
1603 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1604 clear_image_caches (filter
);
1606 clear_image_cache (check_x_frame (filter
), Qt
);
1612 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1614 doc
: /* Fush the image with specification SPEC on frame FRAME.
1615 This removes the image from the Emacs image cache. If SPEC specifies
1616 an image file, the next redisplay of this image will read from the
1617 current contents of that file.
1619 FRAME nil or omitted means use the selected frame.
1620 FRAME t means refresh the image on all frames. */)
1621 (Lisp_Object spec
, Lisp_Object frame
)
1623 if (!valid_image_p (spec
))
1624 error ("Invalid image specification");
1629 FOR_EACH_FRAME (tail
, frame
)
1631 struct frame
*f
= XFRAME (frame
);
1632 if (FRAME_WINDOW_P (f
))
1633 uncache_image (f
, spec
);
1637 uncache_image (check_x_frame (frame
), spec
);
1643 /* Compute masks and transform image IMG on frame F, as specified
1644 by the image's specification, */
1647 postprocess_image (struct frame
*f
, struct image
*img
)
1649 /* Manipulation of the image's mask. */
1652 Lisp_Object conversion
, spec
;
1657 /* `:heuristic-mask t'
1659 means build a mask heuristically.
1660 `:heuristic-mask (R G B)'
1661 `:mask (heuristic (R G B))'
1662 means build a mask from color (R G B) in the
1665 means remove a mask, if any. */
1667 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1669 x_build_heuristic_mask (f
, img
, mask
);
1674 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1676 if (EQ (mask
, Qheuristic
))
1677 x_build_heuristic_mask (f
, img
, Qt
);
1678 else if (CONSP (mask
)
1679 && EQ (XCAR (mask
), Qheuristic
))
1681 if (CONSP (XCDR (mask
)))
1682 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1684 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1686 else if (NILP (mask
) && found_p
&& img
->mask
)
1688 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1689 img
->mask
= NO_PIXMAP
;
1694 /* Should we apply an image transformation algorithm? */
1695 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1696 if (EQ (conversion
, Qdisabled
))
1697 x_disable_image (f
, img
);
1698 else if (EQ (conversion
, Qlaplace
))
1700 else if (EQ (conversion
, Qemboss
))
1702 else if (CONSP (conversion
)
1703 && EQ (XCAR (conversion
), Qedge_detection
))
1706 tem
= XCDR (conversion
);
1708 x_edge_detection (f
, img
,
1709 Fplist_get (tem
, QCmatrix
),
1710 Fplist_get (tem
, QCcolor_adjustment
));
1716 /* Return the id of image with Lisp specification SPEC on frame F.
1717 SPEC must be a valid Lisp image specification (see valid_image_p). */
1720 lookup_image (struct frame
*f
, Lisp_Object spec
)
1726 /* F must be a window-system frame, and SPEC must be a valid image
1728 xassert (FRAME_WINDOW_P (f
));
1729 xassert (valid_image_p (spec
));
1731 /* Look up SPEC in the hash table of the image cache. */
1732 hash
= sxhash (spec
, 0);
1733 img
= search_image_cache (f
, spec
, hash
);
1734 if (img
&& img
->load_failed_p
)
1736 free_image (f
, img
);
1740 /* If not found, create a new image and cache it. */
1744 img
= make_image (spec
, hash
);
1745 cache_image (f
, img
);
1746 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
1747 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1748 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1750 /* If we can't load the image, and we don't have a width and
1751 height, use some arbitrary width and height so that we can
1752 draw a rectangle for it. */
1753 if (img
->load_failed_p
)
1757 value
= image_spec_value (spec
, QCwidth
, NULL
);
1758 img
->width
= (INTEGERP (value
)
1759 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1760 value
= image_spec_value (spec
, QCheight
, NULL
);
1761 img
->height
= (INTEGERP (value
)
1762 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1766 /* Handle image type independent image attributes
1767 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1768 `:background COLOR'. */
1769 Lisp_Object ascent
, margin
, relief
, bg
;
1771 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1772 if (INTEGERP (ascent
))
1773 img
->ascent
= XFASTINT (ascent
);
1774 else if (EQ (ascent
, Qcenter
))
1775 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1777 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1778 if (INTEGERP (margin
))
1779 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1780 else if (CONSP (margin
))
1782 img
->hmargin
= XFASTINT (XCAR (margin
));
1783 img
->vmargin
= XFASTINT (XCDR (margin
));
1786 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1787 if (INTEGERP (relief
))
1789 img
->relief
= XINT (relief
);
1790 img
->hmargin
+= eabs (img
->relief
);
1791 img
->vmargin
+= eabs (img
->relief
);
1794 if (! img
->background_valid
)
1796 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1800 = x_alloc_image_color (f
, img
, bg
,
1801 FRAME_BACKGROUND_PIXEL (f
));
1802 img
->background_valid
= 1;
1806 /* Do image transformations and compute masks, unless we
1807 don't have the image yet. */
1808 if (!EQ (*img
->type
->type
, Qpostscript
))
1809 postprocess_image (f
, img
);
1815 /* We're using IMG, so set its timestamp to `now'. */
1816 EMACS_GET_TIME (now
);
1817 img
->timestamp
= EMACS_SECS (now
);
1819 /* Value is the image id. */
1824 /* Cache image IMG in the image cache of frame F. */
1827 cache_image (struct frame
*f
, struct image
*img
)
1829 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1832 /* Find a free slot in c->images. */
1833 for (i
= 0; i
< c
->used
; ++i
)
1834 if (c
->images
[i
] == NULL
)
1837 /* If no free slot found, maybe enlarge c->images. */
1838 if (i
== c
->used
&& c
->used
== c
->size
)
1840 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *c
->images
/ 2 < c
->size
)
1841 memory_full (SIZE_MAX
);
1843 (struct image
**) xrealloc (c
->images
,
1844 c
->size
* (2 * sizeof *c
->images
));
1848 /* Add IMG to c->images, and assign IMG an id. */
1854 /* Add IMG to the cache's hash table. */
1855 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1856 img
->next
= c
->buckets
[i
];
1858 img
->next
->prev
= img
;
1860 c
->buckets
[i
] = img
;
1864 /* Call FN on every image in the image cache of frame F. Used to mark
1865 Lisp Objects in the image cache. */
1867 /* Mark Lisp objects in image IMG. */
1870 mark_image (struct image
*img
)
1872 mark_object (img
->spec
);
1873 mark_object (img
->dependencies
);
1875 if (!NILP (img
->lisp_data
))
1876 mark_object (img
->lisp_data
);
1881 mark_image_cache (struct image_cache
*c
)
1886 for (i
= 0; i
< c
->used
; ++i
)
1888 mark_image (c
->images
[i
]);
1894 /***********************************************************************
1895 X / NS / W32 support code
1896 ***********************************************************************/
1900 /* Macro for defining functions that will be loaded from image DLLs. */
1901 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1903 /* Macro for loading those image functions from the library. */
1904 #define LOAD_IMGLIB_FN(lib,func) { \
1905 fn_##func = (void *) GetProcAddress (lib, #func); \
1906 if (!fn_##func) return 0; \
1909 #endif /* HAVE_NTGUI */
1911 static int x_create_x_image_and_pixmap (struct frame
*, int, int, int,
1912 XImagePtr
*, Pixmap
*);
1913 static void x_destroy_x_image (XImagePtr
);
1914 static void x_put_x_image (struct frame
*, XImagePtr
, Pixmap
, int, int);
1916 /* Return nonzero if XIMG's size WIDTH x HEIGHT doesn't break the
1918 WIDTH and HEIGHT must both be positive.
1919 If XIMG is null, assume it is a bitmap. */
1921 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1923 #ifdef HAVE_X_WINDOWS
1924 /* Respect Xlib's limits: it cannot deal with images that have more
1925 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1926 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1929 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1930 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1933 int bitmap_pad
, depth
, bytes_per_line
;
1936 bitmap_pad
= ximg
->bitmap_pad
;
1937 depth
= ximg
->depth
;
1938 bytes_per_line
= ximg
->bytes_per_line
;
1944 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1946 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1947 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1949 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1950 For now, assume that every image size is allowed on these systems. */
1955 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1956 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1957 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1958 via xmalloc. Print error messages via image_error if an error
1959 occurs. Value is non-zero if successful.
1961 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1962 should indicate the bit depth of the image. */
1965 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1966 XImagePtr
*ximg
, Pixmap
*pixmap
)
1968 #ifdef HAVE_X_WINDOWS
1969 Display
*display
= FRAME_X_DISPLAY (f
);
1970 Window window
= FRAME_X_WINDOW (f
);
1971 Screen
*screen
= FRAME_X_SCREEN (f
);
1973 xassert (interrupt_input_blocked
);
1976 depth
= DefaultDepthOfScreen (screen
);
1977 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1978 depth
, ZPixmap
, 0, NULL
, width
, height
,
1979 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1982 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1986 if (! x_check_image_size (*ximg
, width
, height
))
1988 x_destroy_x_image (*ximg
);
1990 image_error ("Image too large (%dx%d)",
1991 make_number (width
), make_number (height
));
1995 /* Allocate image raster. */
1996 (*ximg
)->data
= (char *) xmalloc ((*ximg
)->bytes_per_line
* height
);
1998 /* Allocate a pixmap of the same size. */
1999 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
2000 if (*pixmap
== NO_PIXMAP
)
2002 x_destroy_x_image (*ximg
);
2004 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
2009 #endif /* HAVE_X_WINDOWS */
2013 BITMAPINFOHEADER
*header
;
2015 int scanline_width_bits
;
2017 int palette_colors
= 0;
2022 if (depth
!= 1 && depth
!= 4 && depth
!= 8
2023 && depth
!= 16 && depth
!= 24 && depth
!= 32)
2025 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
2029 scanline_width_bits
= width
* depth
;
2030 remainder
= scanline_width_bits
% 32;
2033 scanline_width_bits
+= 32 - remainder
;
2035 /* Bitmaps with a depth less than 16 need a palette. */
2036 /* BITMAPINFO structure already contains the first RGBQUAD. */
2038 palette_colors
= 1 << depth
- 1;
2040 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
2042 header
= &(*ximg
)->info
.bmiHeader
;
2043 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
2044 header
->biSize
= sizeof (*header
);
2045 header
->biWidth
= width
;
2046 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
2047 header
->biPlanes
= 1;
2048 header
->biBitCount
= depth
;
2049 header
->biCompression
= BI_RGB
;
2050 header
->biClrUsed
= palette_colors
;
2052 /* TODO: fill in palette. */
2055 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2056 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2057 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2058 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2059 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2060 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2061 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2062 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2065 hdc
= get_frame_dc (f
);
2067 /* Create a DIBSection and raster array for the bitmap,
2068 and store its handle in *pixmap. */
2069 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2070 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2071 /* casting avoids a GCC warning */
2072 (void **)&((*ximg
)->data
), NULL
, 0);
2074 /* Realize display palette and garbage all frames. */
2075 release_frame_dc (f
, hdc
);
2077 if (*pixmap
== NULL
)
2079 DWORD err
= GetLastError ();
2080 Lisp_Object errcode
;
2081 /* All system errors are < 10000, so the following is safe. */
2082 XSETINT (errcode
, err
);
2083 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2084 x_destroy_x_image (*ximg
);
2090 #endif /* HAVE_NTGUI */
2093 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2097 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2106 /* Destroy XImage XIMG. Free XIMG->data. */
2109 x_destroy_x_image (XImagePtr ximg
)
2111 xassert (interrupt_input_blocked
);
2114 #ifdef HAVE_X_WINDOWS
2117 XDestroyImage (ximg
);
2118 #endif /* HAVE_X_WINDOWS */
2120 /* Data will be freed by DestroyObject. */
2123 #endif /* HAVE_NTGUI */
2125 ns_release_object (ximg
);
2126 #endif /* HAVE_NS */
2131 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2132 are width and height of both the image and pixmap. */
2135 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2137 #ifdef HAVE_X_WINDOWS
2140 xassert (interrupt_input_blocked
);
2141 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2142 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2143 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2144 #endif /* HAVE_X_WINDOWS */
2147 #if 0 /* I don't think this is necessary looking at where it is used. */
2148 HDC hdc
= get_frame_dc (f
);
2149 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2150 release_frame_dc (f
, hdc
);
2152 #endif /* HAVE_NTGUI */
2155 xassert (ximg
== pixmap
);
2156 ns_retain_object (ximg
);
2161 /***********************************************************************
2163 ***********************************************************************/
2165 /* Find image file FILE. Look in data-directory/images, then
2166 x-bitmap-file-path. Value is the encoded full name of the file
2167 found, or nil if not found. */
2170 x_find_image_file (Lisp_Object file
)
2172 Lisp_Object file_found
, search_path
;
2175 /* TODO I think this should use something like image-load-path
2176 instead. Unfortunately, that can contain non-string elements. */
2177 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2179 Vx_bitmap_file_path
);
2181 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2182 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2188 file_found
= ENCODE_FILE (file_found
);
2196 /* Read FILE into memory. Value is a pointer to a buffer allocated
2197 with xmalloc holding FILE's contents. Value is null if an error
2198 occurred. *SIZE is set to the size of the file. */
2200 static unsigned char *
2201 slurp_file (char *file
, ptrdiff_t *size
)
2204 unsigned char *buf
= NULL
;
2207 if (stat (file
, &st
) == 0
2208 && (fp
= fopen (file
, "rb")) != NULL
2209 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
2210 && (buf
= (unsigned char *) xmalloc (st
.st_size
),
2211 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2232 /***********************************************************************
2234 ***********************************************************************/
2236 static int xbm_scan (unsigned char **, unsigned char *, char *, int *);
2237 static int xbm_load (struct frame
*f
, struct image
*img
);
2238 static int xbm_load_image (struct frame
*f
, struct image
*img
,
2239 unsigned char *, unsigned char *);
2240 static int xbm_image_p (Lisp_Object object
);
2241 static int xbm_read_bitmap_data (struct frame
*f
,
2242 unsigned char *, unsigned char *,
2243 int *, int *, char **, int);
2244 static int xbm_file_p (Lisp_Object
);
2247 /* Indices of image specification fields in xbm_format, below. */
2249 enum xbm_keyword_index
2267 /* Vector of image_keyword structures describing the format
2268 of valid XBM image specifications. */
2270 static const struct image_keyword xbm_format
[XBM_LAST
] =
2272 {":type", IMAGE_SYMBOL_VALUE
, 1},
2273 {":file", IMAGE_STRING_VALUE
, 0},
2274 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2275 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2276 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2277 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2278 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2279 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2280 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
2281 {":relief", IMAGE_INTEGER_VALUE
, 0},
2282 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2283 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2284 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2287 /* Structure describing the image type XBM. */
2289 static struct image_type xbm_type
=
2298 /* Tokens returned from xbm_scan. */
2307 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2308 A valid specification is a list starting with the symbol `image'
2309 The rest of the list is a property list which must contain an
2312 If the specification specifies a file to load, it must contain
2313 an entry `:file FILENAME' where FILENAME is a string.
2315 If the specification is for a bitmap loaded from memory it must
2316 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2317 WIDTH and HEIGHT are integers > 0. DATA may be:
2319 1. a string large enough to hold the bitmap data, i.e. it must
2320 have a size >= (WIDTH + 7) / 8 * HEIGHT
2322 2. a bool-vector of size >= WIDTH * HEIGHT
2324 3. a vector of strings or bool-vectors, one for each line of the
2327 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2328 may not be specified in this case because they are defined in the
2331 Both the file and data forms may contain the additional entries
2332 `:background COLOR' and `:foreground COLOR'. If not present,
2333 foreground and background of the frame on which the image is
2334 displayed is used. */
2337 xbm_image_p (Lisp_Object object
)
2339 struct image_keyword kw
[XBM_LAST
];
2341 memcpy (kw
, xbm_format
, sizeof kw
);
2342 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2345 xassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2347 if (kw
[XBM_FILE
].count
)
2349 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2352 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2354 /* In-memory XBM file. */
2355 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2363 /* Entries for `:width', `:height' and `:data' must be present. */
2364 if (!kw
[XBM_WIDTH
].count
2365 || !kw
[XBM_HEIGHT
].count
2366 || !kw
[XBM_DATA
].count
)
2369 data
= kw
[XBM_DATA
].value
;
2370 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2371 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2373 /* Check type of data, and width and height against contents of
2379 /* Number of elements of the vector must be >= height. */
2380 if (ASIZE (data
) < height
)
2383 /* Each string or bool-vector in data must be large enough
2384 for one line of the image. */
2385 for (i
= 0; i
< height
; ++i
)
2387 Lisp_Object elt
= XVECTOR (data
)->contents
[i
];
2392 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2395 else if (BOOL_VECTOR_P (elt
))
2397 if (XBOOL_VECTOR (elt
)->size
< width
)
2404 else if (STRINGP (data
))
2407 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2410 else if (BOOL_VECTOR_P (data
))
2412 if (XBOOL_VECTOR (data
)->size
/ height
< width
)
2423 /* Scan a bitmap file. FP is the stream to read from. Value is
2424 either an enumerator from enum xbm_token, or a character for a
2425 single-character token, or 0 at end of file. If scanning an
2426 identifier, store the lexeme of the identifier in SVAL. If
2427 scanning a number, store its value in *IVAL. */
2430 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2436 /* Skip white space. */
2437 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
2442 else if (isdigit (c
))
2444 int value
= 0, digit
;
2446 if (c
== '0' && *s
< end
)
2449 if (c
== 'x' || c
== 'X')
2456 else if (c
>= 'a' && c
<= 'f')
2457 digit
= c
- 'a' + 10;
2458 else if (c
>= 'A' && c
<= 'F')
2459 digit
= c
- 'A' + 10;
2462 value
= 16 * value
+ digit
;
2465 else if (isdigit (c
))
2469 && (c
= *(*s
)++, isdigit (c
)))
2470 value
= 8 * value
+ c
- '0';
2477 && (c
= *(*s
)++, isdigit (c
)))
2478 value
= 10 * value
+ c
- '0';
2486 else if (isalpha (c
) || c
== '_')
2490 && (c
= *(*s
)++, (isalnum (c
) || c
== '_')))
2497 else if (c
== '/' && **s
== '*')
2499 /* C-style comment. */
2501 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2515 /* Create a Windows bitmap from X bitmap data. */
2517 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2519 static unsigned char swap_nibble
[16]
2520 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2521 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2522 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2523 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2525 unsigned char *bits
, *p
;
2528 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2529 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2530 bits
= (unsigned char *) alloca (height
* w2
);
2531 memset (bits
, 0, height
* w2
);
2532 for (i
= 0; i
< height
; i
++)
2535 for (j
= 0; j
< w1
; j
++)
2537 /* Bitswap XBM bytes to match how Windows does things. */
2538 unsigned char c
= *data
++;
2539 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2540 | (swap_nibble
[(c
>>4) & 0xf]));
2543 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2549 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2550 COLORREF foreground
, COLORREF background
)
2552 HDC hdc
, old_img_dc
, new_img_dc
;
2553 HGDIOBJ old_prev
, new_prev
;
2556 hdc
= get_frame_dc (f
);
2557 old_img_dc
= CreateCompatibleDC (hdc
);
2558 new_img_dc
= CreateCompatibleDC (hdc
);
2559 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2560 release_frame_dc (f
, hdc
);
2561 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2562 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2563 /* Windows convention for mono bitmaps is black = background,
2564 white = foreground. */
2565 SetTextColor (new_img_dc
, background
);
2566 SetBkColor (new_img_dc
, foreground
);
2568 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2571 SelectObject (old_img_dc
, old_prev
);
2572 SelectObject (new_img_dc
, new_prev
);
2573 DeleteDC (old_img_dc
);
2574 DeleteDC (new_img_dc
);
2575 DeleteObject (img
->pixmap
);
2576 if (new_pixmap
== 0)
2577 fprintf (stderr
, "Failed to convert image to color.\n");
2579 img
->pixmap
= new_pixmap
;
2582 #define XBM_BIT_SHUFFLE(b) (~(b))
2586 #define XBM_BIT_SHUFFLE(b) (b)
2588 #endif /* HAVE_NTGUI */
2592 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2593 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2594 int non_default_colors
)
2598 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2600 /* If colors were specified, transfer the bitmap to a color one. */
2601 if (non_default_colors
)
2602 convert_mono_to_color_image (f
, img
, fg
, bg
);
2604 #elif defined (HAVE_NS)
2605 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2609 (x_check_image_size (0, img
->width
, img
->height
)
2610 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2613 img
->width
, img
->height
,
2615 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2617 #endif /* !HAVE_NTGUI && !HAVE_NS */
2622 /* Replacement for XReadBitmapFileData which isn't available under old
2623 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2624 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2625 the image. Return in *DATA the bitmap data allocated with xmalloc.
2626 Value is non-zero if successful. DATA null means just test if
2627 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR
2628 is non-zero, inhibit the call to image_error when the image size is
2629 invalid (the bitmap remains unread). */
2632 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2633 int *width
, int *height
, char **data
,
2634 int inhibit_image_error
)
2636 unsigned char *s
= contents
;
2637 char buffer
[BUFSIZ
];
2640 int bytes_per_line
, i
, nbytes
;
2646 LA1 = xbm_scan (&s, end, buffer, &value)
2648 #define expect(TOKEN) \
2649 if (LA1 != (TOKEN)) \
2654 #define expect_ident(IDENT) \
2655 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2660 *width
= *height
= -1;
2663 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2665 /* Parse defines for width, height and hot-spots. */
2669 expect_ident ("define");
2670 expect (XBM_TK_IDENT
);
2672 if (LA1
== XBM_TK_NUMBER
)
2674 char *q
= strrchr (buffer
, '_');
2675 q
= q
? q
+ 1 : buffer
;
2676 if (strcmp (q
, "width") == 0)
2678 else if (strcmp (q
, "height") == 0)
2681 expect (XBM_TK_NUMBER
);
2684 if (!check_image_size (f
, *width
, *height
))
2686 if (!inhibit_image_error
)
2687 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2690 else if (data
== NULL
)
2693 /* Parse bits. Must start with `static'. */
2694 expect_ident ("static");
2695 if (LA1
== XBM_TK_IDENT
)
2697 if (strcmp (buffer
, "unsigned") == 0)
2700 expect_ident ("char");
2702 else if (strcmp (buffer
, "short") == 0)
2706 if (*width
% 16 && *width
% 16 < 9)
2709 else if (strcmp (buffer
, "char") == 0)
2717 expect (XBM_TK_IDENT
);
2723 if (! x_check_image_size (0, *width
, *height
))
2725 if (!inhibit_image_error
)
2726 image_error ("Image too large (%dx%d)",
2727 make_number (*width
), make_number (*height
));
2730 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2731 nbytes
= bytes_per_line
* *height
;
2732 p
= *data
= (char *) xmalloc (nbytes
);
2736 for (i
= 0; i
< nbytes
; i
+= 2)
2739 expect (XBM_TK_NUMBER
);
2741 *p
++ = XBM_BIT_SHUFFLE (val
);
2742 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2743 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2745 if (LA1
== ',' || LA1
== '}')
2753 for (i
= 0; i
< nbytes
; ++i
)
2756 expect (XBM_TK_NUMBER
);
2758 *p
++ = XBM_BIT_SHUFFLE (val
);
2760 if (LA1
== ',' || LA1
== '}')
2785 /* Load XBM image IMG which will be displayed on frame F from buffer
2786 CONTENTS. END is the end of the buffer. Value is non-zero if
2790 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2797 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2801 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2802 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2803 int non_default_colors
= 0;
2806 xassert (img
->width
> 0 && img
->height
> 0);
2808 /* Get foreground and background colors, maybe allocate colors. */
2809 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2812 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2813 non_default_colors
= 1;
2815 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2818 background
= x_alloc_image_color (f
, img
, value
, background
);
2819 img
->background
= background
;
2820 img
->background_valid
= 1;
2821 non_default_colors
= 1;
2824 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2825 foreground
, background
,
2826 non_default_colors
);
2829 if (img
->pixmap
== NO_PIXMAP
)
2831 x_clear_image (f
, img
);
2832 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2838 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2844 /* Value is non-zero if DATA looks like an in-memory XBM file. */
2847 xbm_file_p (Lisp_Object data
)
2850 return (STRINGP (data
)
2851 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2852 (SDATA (data
) + SBYTES (data
)),
2857 /* Fill image IMG which is used on frame F with pixmap data. Value is
2858 non-zero if successful. */
2861 xbm_load (struct frame
*f
, struct image
*img
)
2864 Lisp_Object file_name
;
2866 xassert (xbm_image_p (img
->spec
));
2868 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2869 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2870 if (STRINGP (file_name
))
2873 unsigned char *contents
;
2876 file
= x_find_image_file (file_name
);
2877 if (!STRINGP (file
))
2879 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2883 contents
= slurp_file (SSDATA (file
), &size
);
2884 if (contents
== NULL
)
2886 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2890 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2895 struct image_keyword fmt
[XBM_LAST
];
2897 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2898 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2899 int non_default_colors
= 0;
2902 int in_memory_file_p
= 0;
2904 /* See if data looks like an in-memory XBM file. */
2905 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2906 in_memory_file_p
= xbm_file_p (data
);
2908 /* Parse the image specification. */
2909 memcpy (fmt
, xbm_format
, sizeof fmt
);
2910 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2914 /* Get specified width, and height. */
2915 if (!in_memory_file_p
)
2917 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2918 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2919 xassert (img
->width
> 0 && img
->height
> 0);
2920 if (!check_image_size (f
, img
->width
, img
->height
))
2922 image_error ("Invalid image size (see `max-image-size')",
2928 /* Get foreground and background colors, maybe allocate colors. */
2929 if (fmt
[XBM_FOREGROUND
].count
2930 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2932 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
2934 non_default_colors
= 1;
2937 if (fmt
[XBM_BACKGROUND
].count
2938 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
2940 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
2942 non_default_colors
= 1;
2945 if (in_memory_file_p
)
2946 success_p
= xbm_load_image (f
, img
, SDATA (data
),
2955 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2957 p
= bits
= (char *) alloca (nbytes
* img
->height
);
2958 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
2960 Lisp_Object line
= XVECTOR (data
)->contents
[i
];
2962 memcpy (p
, SDATA (line
), nbytes
);
2964 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
2967 else if (STRINGP (data
))
2968 bits
= SSDATA (data
);
2970 bits
= (char *) XBOOL_VECTOR (data
)->data
;
2976 /* Windows mono bitmaps are reversed compared with X. */
2977 invertedBits
= bits
;
2978 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
2980 bits
= (char *) alloca (nbytes
);
2981 for (i
= 0; i
< nbytes
; i
++)
2982 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
2985 /* Create the pixmap. */
2987 if (x_check_image_size (0, img
->width
, img
->height
))
2988 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
2989 foreground
, background
,
2990 non_default_colors
);
2992 img
->pixmap
= NO_PIXMAP
;
2998 image_error ("Unable to create pixmap for XBM image `%s'",
3000 x_clear_image (f
, img
);
3010 /***********************************************************************
3012 ***********************************************************************/
3014 #if defined (HAVE_XPM) || defined (HAVE_NS)
3016 static int xpm_image_p (Lisp_Object object
);
3017 static int xpm_load (struct frame
*f
, struct image
*img
);
3018 static int xpm_valid_color_symbols_p (Lisp_Object
);
3020 #endif /* HAVE_XPM || HAVE_NS */
3024 /* Indicate to xpm.h that we don't have Xlib. */
3026 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3027 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3028 #define XColor xpm_XColor
3029 #define XImage xpm_XImage
3030 #define Display xpm_Display
3031 #define PIXEL_ALREADY_TYPEDEFED
3032 #include "X11/xpm.h"
3037 #undef PIXEL_ALREADY_TYPEDEFED
3039 #include "X11/xpm.h"
3040 #endif /* HAVE_NTGUI */
3041 #endif /* HAVE_XPM */
3043 #if defined (HAVE_XPM) || defined (HAVE_NS)
3044 /* The symbol `xpm' identifying XPM-format images. */
3046 static Lisp_Object Qxpm
;
3048 /* Indices of image specification fields in xpm_format, below. */
3050 enum xpm_keyword_index
3066 /* Vector of image_keyword structures describing the format
3067 of valid XPM image specifications. */
3069 static const struct image_keyword xpm_format
[XPM_LAST
] =
3071 {":type", IMAGE_SYMBOL_VALUE
, 1},
3072 {":file", IMAGE_STRING_VALUE
, 0},
3073 {":data", IMAGE_STRING_VALUE
, 0},
3074 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3075 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
3076 {":relief", IMAGE_INTEGER_VALUE
, 0},
3077 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3078 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3079 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3080 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3081 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3084 /* Structure describing the image type XPM. */
3086 static struct image_type xpm_type
=
3095 #ifdef HAVE_X_WINDOWS
3097 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3098 functions for allocating image colors. Our own functions handle
3099 color allocation failures more gracefully than the ones on the XPM
3102 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3103 #define ALLOC_XPM_COLORS
3105 #endif /* HAVE_X_WINDOWS */
3107 #ifdef ALLOC_XPM_COLORS
3109 static void xpm_init_color_cache (struct frame
*, XpmAttributes
*);
3110 static void xpm_free_color_cache (void);
3111 static int xpm_lookup_color (struct frame
*, char *, XColor
*);
3112 static int xpm_color_bucket (char *);
3113 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3116 /* An entry in a hash table used to cache color definitions of named
3117 colors. This cache is necessary to speed up XPM image loading in
3118 case we do color allocations ourselves. Without it, we would need
3119 a call to XParseColor per pixel in the image. */
3121 struct xpm_cached_color
3123 /* Next in collision chain. */
3124 struct xpm_cached_color
*next
;
3126 /* Color definition (RGB and pixel color). */
3133 /* The hash table used for the color cache, and its bucket vector
3136 #define XPM_COLOR_CACHE_BUCKETS 1001
3137 static struct xpm_cached_color
**xpm_color_cache
;
3139 /* Initialize the color cache. */
3142 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3144 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3145 xpm_color_cache
= (struct xpm_cached_color
**) xmalloc (nbytes
);
3146 memset (xpm_color_cache
, 0, nbytes
);
3147 init_color_table ();
3149 if (attrs
->valuemask
& XpmColorSymbols
)
3154 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3155 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3156 attrs
->colorsymbols
[i
].value
, &color
))
3158 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3160 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3165 /* Free the color cache. */
3168 xpm_free_color_cache (void)
3170 struct xpm_cached_color
*p
, *next
;
3173 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3174 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3180 xfree (xpm_color_cache
);
3181 xpm_color_cache
= NULL
;
3182 free_color_table ();
3185 /* Return the bucket index for color named COLOR_NAME in the color
3189 xpm_color_bucket (char *color_name
)
3191 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3192 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3196 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3197 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3200 static struct xpm_cached_color
*
3201 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3204 struct xpm_cached_color
*p
;
3207 bucket
= xpm_color_bucket (color_name
);
3209 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3210 p
= (struct xpm_cached_color
*) xmalloc (nbytes
);
3211 strcpy (p
->name
, color_name
);
3213 p
->next
= xpm_color_cache
[bucket
];
3214 xpm_color_cache
[bucket
] = p
;
3218 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3219 return the cached definition in *COLOR. Otherwise, make a new
3220 entry in the cache and allocate the color. Value is zero if color
3221 allocation failed. */
3224 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3226 struct xpm_cached_color
*p
;
3227 int h
= xpm_color_bucket (color_name
);
3229 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3230 if (strcmp (p
->name
, color_name
) == 0)
3235 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3238 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3240 p
= xpm_cache_color (f
, color_name
, color
, h
);
3242 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3243 with transparency, and it's useful. */
3244 else if (strcmp ("opaque", color_name
) == 0)
3246 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3247 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3248 p
= xpm_cache_color (f
, color_name
, color
, h
);
3255 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3256 CLOSURE is a pointer to the frame on which we allocate the
3257 color. Return in *COLOR the allocated color. Value is non-zero
3261 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3264 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3268 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3269 is a pointer to the frame on which we allocate the color. Value is
3270 non-zero if successful. */
3273 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3278 #endif /* ALLOC_XPM_COLORS */
3283 /* XPM library details. */
3285 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3286 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3287 xpm_XImage
**, XpmAttributes
*));
3288 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3289 xpm_XImage
**, XpmAttributes
*));
3290 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3293 init_xpm_functions (Lisp_Object libraries
)
3297 if (!(library
= w32_delayed_load (libraries
, Qxpm
)))
3300 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3301 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3302 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3303 LOAD_IMGLIB_FN (library
, XImageFree
);
3307 #endif /* HAVE_NTGUI */
3310 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3311 for XPM images. Such a list must consist of conses whose car and
3315 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3317 while (CONSP (color_symbols
))
3319 Lisp_Object sym
= XCAR (color_symbols
);
3321 || !STRINGP (XCAR (sym
))
3322 || !STRINGP (XCDR (sym
)))
3324 color_symbols
= XCDR (color_symbols
);
3327 return NILP (color_symbols
);
3331 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3334 xpm_image_p (Lisp_Object object
)
3336 struct image_keyword fmt
[XPM_LAST
];
3337 memcpy (fmt
, xpm_format
, sizeof fmt
);
3338 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3339 /* Either `:file' or `:data' must be present. */
3340 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3341 /* Either no `:color-symbols' or it's a list of conses
3342 whose car and cdr are strings. */
3343 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3344 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3347 #endif /* HAVE_XPM || HAVE_NS */
3349 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3351 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3353 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3356 XpmAttributes attrs
;
3357 Pixmap bitmap
, mask
;
3359 memset (&attrs
, 0, sizeof attrs
);
3361 attrs
.visual
= FRAME_X_VISUAL (f
);
3362 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3363 attrs
.valuemask
|= XpmVisual
;
3364 attrs
.valuemask
|= XpmColormap
;
3366 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3367 (char **) bits
, &bitmap
, &mask
, &attrs
);
3368 if (rc
!= XpmSuccess
)
3370 XpmFreeAttributes (&attrs
);
3374 id
= x_allocate_bitmap_record (f
);
3375 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3376 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3377 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3378 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3379 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3380 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3381 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3382 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3384 XpmFreeAttributes (&attrs
);
3387 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3389 /* Load image IMG which will be displayed on frame F. Value is
3390 non-zero if successful. */
3395 xpm_load (struct frame
*f
, struct image
*img
)
3398 XpmAttributes attrs
;
3399 Lisp_Object specified_file
, color_symbols
;
3402 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3403 #endif /* HAVE_NTGUI */
3405 /* Configure the XPM lib. Use the visual of frame F. Allocate
3406 close colors. Return colors allocated. */
3407 memset (&attrs
, 0, sizeof attrs
);
3410 attrs
.visual
= FRAME_X_VISUAL (f
);
3411 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3412 attrs
.valuemask
|= XpmVisual
;
3413 attrs
.valuemask
|= XpmColormap
;
3414 #endif /* HAVE_NTGUI */
3416 #ifdef ALLOC_XPM_COLORS
3417 /* Allocate colors with our own functions which handle
3418 failing color allocation more gracefully. */
3419 attrs
.color_closure
= f
;
3420 attrs
.alloc_color
= xpm_alloc_color
;
3421 attrs
.free_colors
= xpm_free_colors
;
3422 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3423 #else /* not ALLOC_XPM_COLORS */
3424 /* Let the XPM lib allocate colors. */
3425 attrs
.valuemask
|= XpmReturnAllocPixels
;
3426 #ifdef XpmAllocCloseColors
3427 attrs
.alloc_close_colors
= 1;
3428 attrs
.valuemask
|= XpmAllocCloseColors
;
3429 #else /* not XpmAllocCloseColors */
3430 attrs
.closeness
= 600;
3431 attrs
.valuemask
|= XpmCloseness
;
3432 #endif /* not XpmAllocCloseColors */
3433 #endif /* ALLOC_XPM_COLORS */
3435 /* If image specification contains symbolic color definitions, add
3436 these to `attrs'. */
3437 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3438 if (CONSP (color_symbols
))
3441 XpmColorSymbol
*xpm_syms
;
3444 attrs
.valuemask
|= XpmColorSymbols
;
3446 /* Count number of symbols. */
3447 attrs
.numsymbols
= 0;
3448 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3451 /* Allocate an XpmColorSymbol array. */
3452 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3453 xpm_syms
= (XpmColorSymbol
*) alloca (size
);
3454 memset (xpm_syms
, 0, size
);
3455 attrs
.colorsymbols
= xpm_syms
;
3457 /* Fill the color symbol array. */
3458 for (tail
= color_symbols
, i
= 0;
3460 ++i
, tail
= XCDR (tail
))
3464 char *empty_string
= (char *) "";
3466 if (!CONSP (XCAR (tail
)))
3468 xpm_syms
[i
].name
= empty_string
;
3469 xpm_syms
[i
].value
= empty_string
;
3472 name
= XCAR (XCAR (tail
));
3473 color
= XCDR (XCAR (tail
));
3476 xpm_syms
[i
].name
= (char *) alloca (SCHARS (name
) + 1);
3477 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3480 xpm_syms
[i
].name
= empty_string
;
3481 if (STRINGP (color
))
3483 xpm_syms
[i
].value
= (char *) alloca (SCHARS (color
) + 1);
3484 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3487 xpm_syms
[i
].value
= empty_string
;
3491 /* Create a pixmap for the image, either from a file, or from a
3492 string buffer containing data in the same format as an XPM file. */
3493 #ifdef ALLOC_XPM_COLORS
3494 xpm_init_color_cache (f
, &attrs
);
3497 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3501 HDC frame_dc
= get_frame_dc (f
);
3502 hdc
= CreateCompatibleDC (frame_dc
);
3503 release_frame_dc (f
, frame_dc
);
3505 #endif /* HAVE_NTGUI */
3507 if (STRINGP (specified_file
))
3509 Lisp_Object file
= x_find_image_file (specified_file
);
3510 if (!STRINGP (file
))
3512 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3513 #ifdef ALLOC_XPM_COLORS
3514 xpm_free_color_cache ();
3520 /* XpmReadFileToPixmap is not available in the Windows port of
3521 libxpm. But XpmReadFileToImage almost does what we want. */
3522 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3523 &xpm_image
, &xpm_mask
,
3526 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3527 SSDATA (file
), &img
->pixmap
, &img
->mask
,
3529 #endif /* HAVE_NTGUI */
3533 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3534 if (!STRINGP (buffer
))
3536 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3537 #ifdef ALLOC_XPM_COLORS
3538 xpm_free_color_cache ();
3543 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3544 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3545 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3546 &xpm_image
, &xpm_mask
,
3549 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3551 &img
->pixmap
, &img
->mask
,
3553 #endif /* HAVE_NTGUI */
3556 if (rc
== XpmSuccess
)
3558 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3559 img
->colors
= colors_in_color_table (&img
->ncolors
);
3560 #else /* not ALLOC_XPM_COLORS */
3564 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3565 plus some duplicate attributes. */
3566 if (xpm_image
&& xpm_image
->bitmap
)
3568 img
->pixmap
= xpm_image
->bitmap
;
3569 /* XImageFree in libXpm frees XImage struct without destroying
3570 the bitmap, which is what we want. */
3571 fn_XImageFree (xpm_image
);
3573 if (xpm_mask
&& xpm_mask
->bitmap
)
3575 /* The mask appears to be inverted compared with what we expect.
3576 TODO: invert our expectations. See other places where we
3577 have to invert bits because our idea of masks is backwards. */
3579 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3581 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3582 SelectObject (hdc
, old_obj
);
3584 img
->mask
= xpm_mask
->bitmap
;
3585 fn_XImageFree (xpm_mask
);
3590 #endif /* HAVE_NTGUI */
3592 /* Remember allocated colors. */
3593 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3594 img
->ncolors
= attrs
.nalloc_pixels
;
3595 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3597 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3598 #ifdef DEBUG_X_COLORS
3599 register_color (img
->colors
[i
]);
3602 #endif /* not ALLOC_XPM_COLORS */
3604 img
->width
= attrs
.width
;
3605 img
->height
= attrs
.height
;
3606 xassert (img
->width
> 0 && img
->height
> 0);
3608 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3610 fn_XpmFreeAttributes (&attrs
);
3612 XpmFreeAttributes (&attrs
);
3613 #endif /* HAVE_NTGUI */
3619 #endif /* HAVE_NTGUI */
3624 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3627 case XpmFileInvalid
:
3628 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3632 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3635 case XpmColorFailed
:
3636 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3640 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3645 #ifdef ALLOC_XPM_COLORS
3646 xpm_free_color_cache ();
3648 return rc
== XpmSuccess
;
3651 #endif /* HAVE_XPM */
3653 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3655 /* XPM support functions for NS where libxpm is not available.
3656 Only XPM version 3 (without any extensions) is supported. */
3658 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3660 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3661 const unsigned char *, int);
3662 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3664 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3665 const unsigned char *, int);
3667 /* Tokens returned from xpm_scan. */
3676 /* Scan an XPM data and return a character (< 256) or a token defined
3677 by enum xpm_token above. *S and END are the start (inclusive) and
3678 the end (exclusive) addresses of the data, respectively. Advance
3679 *S while scanning. If token is either XPM_TK_IDENT or
3680 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3681 length of the corresponding token, respectively. */
3684 xpm_scan (const unsigned char **s
,
3685 const unsigned char *end
,
3686 const unsigned char **beg
,
3693 /* Skip white-space. */
3694 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
3697 /* gnus-pointer.xpm uses '-' in its identifier.
3698 sb-dir-plus.xpm uses '+' in its identifier. */
3699 if (isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3703 && (c
= **s
, isalnum (c
) || c
== '_' || c
== '-' || c
== '+'))
3706 return XPM_TK_IDENT
;
3711 while (*s
< end
&& **s
!= '"')
3716 return XPM_TK_STRING
;
3720 if (*s
< end
&& **s
== '*')
3722 /* C-style comment. */
3726 while (*s
< end
&& *(*s
)++ != '*')
3729 while (*s
< end
&& **s
!= '/');
3743 /* Functions for color table lookup in XPM data. A key is a string
3744 specifying the color of each pixel in XPM data. A value is either
3745 an integer that specifies a pixel color, Qt that specifies
3746 transparency, or Qnil for the unspecified color. If the length of
3747 the key string is one, a vector is used as a table. Otherwise, a
3748 hash table is used. */
3751 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3752 const unsigned char *,
3755 Lisp_Object (**get_func
) (Lisp_Object
,
3756 const unsigned char *,
3759 *put_func
= xpm_put_color_table_v
;
3760 *get_func
= xpm_get_color_table_v
;
3761 return Fmake_vector (make_number (256), Qnil
);
3765 xpm_put_color_table_v (Lisp_Object color_table
,
3766 const unsigned char *chars_start
,
3770 XVECTOR (color_table
)->contents
[*chars_start
] = color
;
3774 xpm_get_color_table_v (Lisp_Object color_table
,
3775 const unsigned char *chars_start
,
3778 return XVECTOR (color_table
)->contents
[*chars_start
];
3782 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3783 const unsigned char *,
3786 Lisp_Object (**get_func
) (Lisp_Object
,
3787 const unsigned char *,
3790 *put_func
= xpm_put_color_table_h
;
3791 *get_func
= xpm_get_color_table_h
;
3792 return make_hash_table (Qequal
, make_number (DEFAULT_HASH_SIZE
),
3793 make_float (DEFAULT_REHASH_SIZE
),
3794 make_float (DEFAULT_REHASH_THRESHOLD
),
3799 xpm_put_color_table_h (Lisp_Object color_table
,
3800 const unsigned char *chars_start
,
3804 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3805 EMACS_UINT hash_code
;
3806 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3808 hash_lookup (table
, chars
, &hash_code
);
3809 hash_put (table
, chars
, color
, hash_code
);
3813 xpm_get_color_table_h (Lisp_Object color_table
,
3814 const unsigned char *chars_start
,
3817 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3819 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3821 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3824 enum xpm_color_key
{
3832 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3835 xpm_str_to_color_key (const char *s
)
3840 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3842 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3848 xpm_load_image (struct frame
*f
,
3850 const unsigned char *contents
,
3851 const unsigned char *end
)
3853 const unsigned char *s
= contents
, *beg
, *str
;
3854 unsigned char buffer
[BUFSIZ
];
3855 int width
, height
, x
, y
;
3856 int num_colors
, chars_per_pixel
;
3859 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3860 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3861 Lisp_Object frame
, color_symbols
, color_table
;
3862 int best_key
, have_mask
= 0;
3863 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3866 LA1 = xpm_scan (&s, end, &beg, &len)
3868 #define expect(TOKEN) \
3869 if (LA1 != (TOKEN)) \
3874 #define expect_ident(IDENT) \
3875 if (LA1 == XPM_TK_IDENT \
3876 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3881 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3885 expect_ident ("static");
3886 expect_ident ("char");
3888 expect (XPM_TK_IDENT
);
3893 expect (XPM_TK_STRING
);
3896 memcpy (buffer
, beg
, len
);
3898 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
3899 &num_colors
, &chars_per_pixel
) != 4
3900 || width
<= 0 || height
<= 0
3901 || num_colors
<= 0 || chars_per_pixel
<= 0)
3904 if (!check_image_size (f
, width
, height
))
3906 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
3910 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
3911 &ximg
, &img
->pixmap
)
3913 || !x_create_x_image_and_pixmap (f
, width
, height
, 1,
3914 &mask_img
, &img
->mask
)
3918 image_error ("Image too large", Qnil
, Qnil
);
3924 XSETFRAME (frame
, f
);
3925 if (!NILP (Fxw_display_color_p (frame
)))
3926 best_key
= XPM_COLOR_KEY_C
;
3927 else if (!NILP (Fx_display_grayscale_p (frame
)))
3928 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
3929 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
3931 best_key
= XPM_COLOR_KEY_M
;
3933 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3934 if (chars_per_pixel
== 1)
3935 color_table
= xpm_make_color_table_v (&put_color_table
,
3938 color_table
= xpm_make_color_table_h (&put_color_table
,
3941 while (num_colors
-- > 0)
3943 char *color
, *max_color
;
3944 int key
, next_key
, max_key
= 0;
3945 Lisp_Object symbol_color
= Qnil
, color_val
;
3948 expect (XPM_TK_STRING
);
3949 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
3951 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
3952 buffer
[len
- chars_per_pixel
] = '\0';
3954 str
= strtok (buffer
, " \t");
3957 key
= xpm_str_to_color_key (str
);
3962 color
= strtok (NULL
, " \t");
3966 while ((str
= strtok (NULL
, " \t")) != NULL
)
3968 next_key
= xpm_str_to_color_key (str
);
3971 color
[strlen (color
)] = ' ';
3974 if (key
== XPM_COLOR_KEY_S
)
3976 if (NILP (symbol_color
))
3977 symbol_color
= build_string (color
);
3979 else if (max_key
< key
&& key
<= best_key
)
3989 if (!NILP (color_symbols
) && !NILP (symbol_color
))
3991 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
3993 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
3995 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
3997 else if (x_defined_color (f
, SDATA (XCDR (specified_color
)),
3999 color_val
= make_number (cdef
.pixel
);
4002 if (NILP (color_val
) && max_key
> 0)
4004 if (xstrcasecmp (max_color
, "None") == 0)
4006 else if (x_defined_color (f
, max_color
, &cdef
, 0))
4007 color_val
= make_number (cdef
.pixel
);
4009 if (!NILP (color_val
))
4010 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
4015 for (y
= 0; y
< height
; y
++)
4017 expect (XPM_TK_STRING
);
4019 if (len
< width
* chars_per_pixel
)
4021 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
4023 Lisp_Object color_val
=
4024 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4026 XPutPixel (ximg
, x
, y
,
4027 (INTEGERP (color_val
) ? XINT (color_val
)
4028 : FRAME_FOREGROUND_PIXEL (f
)));
4030 XPutPixel (mask_img
, x
, y
,
4031 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4032 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4034 if (EQ (color_val
, Qt
))
4035 ns_set_alpha (ximg
, x
, y
, 0);
4043 img
->height
= height
;
4045 /* Maybe fill in the background field while we have ximg handy. */
4046 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4047 IMAGE_BACKGROUND (img
, f
, ximg
);
4049 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
4050 x_destroy_x_image (ximg
);
4054 /* Fill in the background_transparent field while we have the
4056 image_background_transparent (img
, f
, mask_img
);
4058 x_put_x_image (f
, mask_img
, img
->mask
, width
, height
);
4059 x_destroy_x_image (mask_img
);
4063 x_destroy_x_image (mask_img
);
4064 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4065 img
->mask
= NO_PIXMAP
;
4071 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4073 x_destroy_x_image (ximg
);
4074 x_destroy_x_image (mask_img
);
4075 x_clear_image (f
, img
);
4084 xpm_load (struct frame
*f
,
4088 Lisp_Object file_name
;
4090 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4091 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4092 if (STRINGP (file_name
))
4095 unsigned char *contents
;
4098 file
= x_find_image_file (file_name
);
4099 if (!STRINGP (file
))
4101 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4105 contents
= slurp_file (SDATA (file
), &size
);
4106 if (contents
== NULL
)
4108 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4112 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4119 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4120 if (!STRINGP (data
))
4122 image_error ("Invalid image data `%s'", data
, Qnil
);
4125 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4126 SDATA (data
) + SBYTES (data
));
4132 #endif /* HAVE_NS && !HAVE_XPM */
4136 /***********************************************************************
4138 ***********************************************************************/
4140 #ifdef COLOR_TABLE_SUPPORT
4142 /* An entry in the color table mapping an RGB color to a pixel color. */
4147 unsigned long pixel
;
4149 /* Next in color table collision list. */
4150 struct ct_color
*next
;
4153 /* The bucket vector size to use. Must be prime. */
4157 /* Value is a hash of the RGB color given by R, G, and B. */
4159 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4161 /* The color hash table. */
4163 static struct ct_color
**ct_table
;
4165 /* Number of entries in the color table. */
4167 static int ct_colors_allocated
;
4170 ct_colors_allocated_max
=
4172 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4175 /* Initialize the color table. */
4178 init_color_table (void)
4180 int size
= CT_SIZE
* sizeof (*ct_table
);
4181 ct_table
= (struct ct_color
**) xmalloc (size
);
4182 memset (ct_table
, 0, size
);
4183 ct_colors_allocated
= 0;
4187 /* Free memory associated with the color table. */
4190 free_color_table (void)
4193 struct ct_color
*p
, *next
;
4195 for (i
= 0; i
< CT_SIZE
; ++i
)
4196 for (p
= ct_table
[i
]; p
; p
= next
)
4207 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4208 entry for that color already is in the color table, return the
4209 pixel color of that entry. Otherwise, allocate a new color for R,
4210 G, B, and make an entry in the color table. */
4212 static unsigned long
4213 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4215 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4216 int i
= hash
% CT_SIZE
;
4218 Display_Info
*dpyinfo
;
4220 /* Handle TrueColor visuals specially, which improves performance by
4221 two orders of magnitude. Freeing colors on TrueColor visuals is
4222 a nop, and pixel colors specify RGB values directly. See also
4223 the Xlib spec, chapter 3.1. */
4224 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4225 if (dpyinfo
->red_bits
> 0)
4227 unsigned long pr
, pg
, pb
;
4229 /* Apply gamma-correction like normal color allocation does. */
4233 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4234 gamma_correct (f
, &color
);
4235 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4238 /* Scale down RGB values to the visual's bits per RGB, and shift
4239 them to the right position in the pixel color. Note that the
4240 original RGB values are 16-bit values, as usual in X. */
4241 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4242 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4243 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4245 /* Assemble the pixel color. */
4246 return pr
| pg
| pb
;
4249 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4250 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4256 #ifdef HAVE_X_WINDOWS
4264 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4265 return FRAME_FOREGROUND_PIXEL (f
);
4267 #ifdef HAVE_X_WINDOWS
4272 cmap
= FRAME_X_COLORMAP (f
);
4273 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4276 ++ct_colors_allocated
;
4277 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4281 p
->pixel
= color
.pixel
;
4282 p
->next
= ct_table
[i
];
4286 return FRAME_FOREGROUND_PIXEL (f
);
4290 color
= PALETTERGB (r
, g
, b
);
4292 color
= RGB_TO_ULONG (r
, g
, b
);
4293 #endif /* HAVE_NTGUI */
4294 ++ct_colors_allocated
;
4295 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4300 p
->next
= ct_table
[i
];
4302 #endif /* HAVE_X_WINDOWS */
4310 /* Look up pixel color PIXEL which is used on frame F in the color
4311 table. If not already present, allocate it. Value is PIXEL. */
4313 static unsigned long
4314 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4316 int i
= pixel
% CT_SIZE
;
4319 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4320 if (p
->pixel
== pixel
)
4329 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4330 return FRAME_FOREGROUND_PIXEL (f
);
4332 #ifdef HAVE_X_WINDOWS
4333 cmap
= FRAME_X_COLORMAP (f
);
4334 color
.pixel
= pixel
;
4335 x_query_color (f
, &color
);
4336 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4339 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4340 color
.pixel
= pixel
;
4341 XQueryColor (NULL
, cmap
, &color
);
4342 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4344 #endif /* HAVE_X_WINDOWS */
4348 ++ct_colors_allocated
;
4350 p
= (struct ct_color
*) xmalloc (sizeof *p
);
4355 p
->next
= ct_table
[i
];
4359 return FRAME_FOREGROUND_PIXEL (f
);
4365 /* Value is a vector of all pixel colors contained in the color table,
4366 allocated via xmalloc. Set *N to the number of colors. */
4368 static unsigned long *
4369 colors_in_color_table (int *n
)
4373 unsigned long *colors
;
4375 if (ct_colors_allocated
== 0)
4382 colors
= (unsigned long *) xmalloc (ct_colors_allocated
4384 *n
= ct_colors_allocated
;
4386 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4387 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4388 colors
[j
++] = p
->pixel
;
4394 #else /* COLOR_TABLE_SUPPORT */
4396 static unsigned long
4397 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4399 unsigned long pixel
;
4402 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4403 #endif /* HAVE_NTGUI */
4406 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4407 #endif /* HAVE_NS */
4412 init_color_table (void)
4415 #endif /* COLOR_TABLE_SUPPORT */
4418 /***********************************************************************
4420 ***********************************************************************/
4422 static XColor
*x_to_xcolors (struct frame
*, struct image
*, int);
4423 static void x_from_xcolors (struct frame
*, struct image
*, XColor
*);
4424 static void x_detect_edges (struct frame
*, struct image
*, int[9], int);
4427 static void XPutPixel (XImagePtr
, int, int, COLORREF
);
4428 #endif /* HAVE_NTGUI */
4430 /* Edge detection matrices for different edge-detection
4433 static int emboss_matrix
[9] = {
4435 2, -1, 0, /* y - 1 */
4437 0, 1, -2 /* y + 1 */
4440 static int laplace_matrix
[9] = {
4442 1, 0, 0, /* y - 1 */
4444 0, 0, -1 /* y + 1 */
4447 /* Value is the intensity of the color whose red/green/blue values
4450 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4453 /* On frame F, return an array of XColor structures describing image
4454 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4455 non-zero means also fill the red/green/blue members of the XColor
4456 structures. Value is a pointer to the array of XColors structures,
4457 allocated with xmalloc; it must be freed by the caller. */
4460 x_to_xcolors (struct frame
*f
, struct image
*img
, int rgb_p
)
4464 XImagePtr_or_DC ximg
;
4468 #endif /* HAVE_NTGUI */
4470 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4471 memory_full (SIZE_MAX
);
4472 colors
= (XColor
*) xmalloc (sizeof *colors
* img
->width
* img
->height
);
4475 /* Get the X image IMG->pixmap. */
4476 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4477 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4479 /* Load the image into a memory device context. */
4480 hdc
= get_frame_dc (f
);
4481 ximg
= CreateCompatibleDC (hdc
);
4482 release_frame_dc (f
, hdc
);
4483 prev
= SelectObject (ximg
, img
->pixmap
);
4484 #endif /* HAVE_NTGUI */
4486 /* Fill the `pixel' members of the XColor array. I wished there
4487 were an easy and portable way to circumvent XGetPixel. */
4489 for (y
= 0; y
< img
->height
; ++y
)
4493 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4494 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4495 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4497 x_query_colors (f
, row
, img
->width
);
4501 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4503 /* W32_TODO: palette support needed here? */
4504 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4507 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4508 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4509 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4512 #endif /* HAVE_X_WINDOWS */
4515 Destroy_Image (ximg
, prev
);
4522 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4523 created with CreateDIBSection, with the pointer to the bit values
4524 stored in ximg->data. */
4527 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4529 int width
= ximg
->info
.bmiHeader
.biWidth
;
4530 unsigned char * pixel
;
4532 /* True color images. */
4533 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4535 int rowbytes
= width
* 3;
4536 /* Ensure scanlines are aligned on 4 byte boundaries. */
4538 rowbytes
+= 4 - (rowbytes
% 4);
4540 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4541 /* Windows bitmaps are in BGR order. */
4542 *pixel
= GetBValue (color
);
4543 *(pixel
+ 1) = GetGValue (color
);
4544 *(pixel
+ 2) = GetRValue (color
);
4546 /* Monochrome images. */
4547 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4549 int rowbytes
= width
/ 8;
4550 /* Ensure scanlines are aligned on 4 byte boundaries. */
4552 rowbytes
+= 4 - (rowbytes
% 4);
4553 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4554 /* Filter out palette info. */
4555 if (color
& 0x00ffffff)
4556 *pixel
= *pixel
| (1 << x
% 8);
4558 *pixel
= *pixel
& ~(1 << x
% 8);
4561 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4564 #endif /* HAVE_NTGUI */
4566 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4567 RGB members are set. F is the frame on which this all happens.
4568 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4571 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4574 XImagePtr oimg
= NULL
;
4578 init_color_table ();
4580 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
4583 for (y
= 0; y
< img
->height
; ++y
)
4584 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4586 unsigned long pixel
;
4587 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4588 XPutPixel (oimg
, x
, y
, pixel
);
4592 x_clear_image_1 (f
, img
, 1, 0, 1);
4594 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
4595 x_destroy_x_image (oimg
);
4596 img
->pixmap
= pixmap
;
4597 #ifdef COLOR_TABLE_SUPPORT
4598 img
->colors
= colors_in_color_table (&img
->ncolors
);
4599 free_color_table ();
4600 #endif /* COLOR_TABLE_SUPPORT */
4604 /* On frame F, perform edge-detection on image IMG.
4606 MATRIX is a nine-element array specifying the transformation
4607 matrix. See emboss_matrix for an example.
4609 COLOR_ADJUST is a color adjustment added to each pixel of the
4613 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4615 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4619 for (i
= sum
= 0; i
< 9; ++i
)
4620 sum
+= eabs (matrix
[i
]);
4622 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4624 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4625 memory_full (SIZE_MAX
);
4626 new = (XColor
*) xmalloc (sizeof *new * img
->width
* img
->height
);
4628 for (y
= 0; y
< img
->height
; ++y
)
4630 p
= COLOR (new, 0, y
);
4631 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4632 p
= COLOR (new, img
->width
- 1, y
);
4633 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4636 for (x
= 1; x
< img
->width
- 1; ++x
)
4638 p
= COLOR (new, x
, 0);
4639 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4640 p
= COLOR (new, x
, img
->height
- 1);
4641 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4644 for (y
= 1; y
< img
->height
- 1; ++y
)
4646 p
= COLOR (new, 1, y
);
4648 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4650 int r
, g
, b
, yy
, xx
;
4653 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4654 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4657 XColor
*t
= COLOR (colors
, xx
, yy
);
4658 r
+= matrix
[i
] * t
->red
;
4659 g
+= matrix
[i
] * t
->green
;
4660 b
+= matrix
[i
] * t
->blue
;
4663 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4664 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4665 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4666 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4671 x_from_xcolors (f
, img
, new);
4677 /* Perform the pre-defined `emboss' edge-detection on image IMG
4681 x_emboss (struct frame
*f
, struct image
*img
)
4683 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4687 /* Transform image IMG which is used on frame F with a Laplace
4688 edge-detection algorithm. The result is an image that can be used
4689 to draw disabled buttons, for example. */
4692 x_laplace (struct frame
*f
, struct image
*img
)
4694 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4698 /* Perform edge-detection on image IMG on frame F, with specified
4699 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4701 MATRIX must be either
4703 - a list of at least 9 numbers in row-major form
4704 - a vector of at least 9 numbers
4706 COLOR_ADJUST nil means use a default; otherwise it must be a
4710 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4711 Lisp_Object color_adjust
)
4719 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4720 ++i
, matrix
= XCDR (matrix
))
4721 trans
[i
] = XFLOATINT (XCAR (matrix
));
4723 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4725 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4726 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4729 if (NILP (color_adjust
))
4730 color_adjust
= make_number (0xffff / 2);
4732 if (i
== 9 && NUMBERP (color_adjust
))
4733 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4737 /* Transform image IMG on frame F so that it looks disabled. */
4740 x_disable_image (struct frame
*f
, struct image
*img
)
4742 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4744 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4746 int n_planes
= dpyinfo
->n_planes
;
4747 #endif /* HAVE_NTGUI */
4751 /* Color (or grayscale). Convert to gray, and equalize. Just
4752 drawing such images with a stipple can look very odd, so
4753 we're using this method instead. */
4754 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4756 const int h
= 15000;
4757 const int l
= 30000;
4759 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4763 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4764 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4765 p
->red
= p
->green
= p
->blue
= i2
;
4768 x_from_xcolors (f
, img
, colors
);
4771 /* Draw a cross over the disabled image, if we must or if we
4773 if (n_planes
< 2 || cross_disabled_images
)
4776 Display
*dpy
= FRAME_X_DISPLAY (f
);
4779 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4781 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4783 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4784 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4785 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4786 img
->width
- 1, img
->height
- 1);
4787 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4793 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4794 XSetForeground (dpy
, gc
, MaskForeground (f
));
4795 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4796 img
->width
- 1, img
->height
- 1);
4797 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4801 #endif /* !HAVE_NS */
4806 hdc
= get_frame_dc (f
);
4807 bmpdc
= CreateCompatibleDC (hdc
);
4808 release_frame_dc (f
, hdc
);
4810 prev
= SelectObject (bmpdc
, img
->pixmap
);
4812 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4813 MoveToEx (bmpdc
, 0, 0, NULL
);
4814 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4815 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4816 LineTo (bmpdc
, img
->width
- 1, 0);
4820 SelectObject (bmpdc
, img
->mask
);
4821 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4822 MoveToEx (bmpdc
, 0, 0, NULL
);
4823 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4824 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4825 LineTo (bmpdc
, img
->width
- 1, 0);
4827 SelectObject (bmpdc
, prev
);
4829 #endif /* HAVE_NTGUI */
4834 /* Build a mask for image IMG which is used on frame F. FILE is the
4835 name of an image file, for error messages. HOW determines how to
4836 determine the background color of IMG. If it is a list '(R G B)',
4837 with R, G, and B being integers >= 0, take that as the color of the
4838 background. Otherwise, determine the background color of IMG
4839 heuristically. Value is non-zero if successful. */
4842 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4844 XImagePtr_or_DC ximg
;
4852 #endif /* HAVE_NTGUI */
4853 int x
, y
, rc
, use_img_background
;
4854 unsigned long bg
= 0;
4858 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
4859 img
->mask
= NO_PIXMAP
;
4860 img
->background_transparent_valid
= 0;
4865 /* Create an image and pixmap serving as mask. */
4866 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
4867 &mask_img
, &img
->mask
);
4870 #endif /* !HAVE_NS */
4872 /* Get the X image of IMG->pixmap. */
4873 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
, 0, 0,
4874 img
->width
, img
->height
,
4877 /* Create the bit array serving as mask. */
4878 row_width
= (img
->width
+ 7) / 8;
4879 mask_img
= xmalloc (row_width
* img
->height
);
4880 memset (mask_img
, 0, row_width
* img
->height
);
4882 /* Create a memory device context for IMG->pixmap. */
4883 frame_dc
= get_frame_dc (f
);
4884 ximg
= CreateCompatibleDC (frame_dc
);
4885 release_frame_dc (f
, frame_dc
);
4886 prev
= SelectObject (ximg
, img
->pixmap
);
4887 #endif /* HAVE_NTGUI */
4889 /* Determine the background color of ximg. If HOW is `(R G B)'
4890 take that as color. Otherwise, use the image's background color. */
4891 use_img_background
= 1;
4897 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4899 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4903 if (i
== 3 && NILP (how
))
4905 char color_name
[30];
4906 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4909 0x00ffffff & /* Filter out palette info. */
4910 #endif /* HAVE_NTGUI */
4911 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4912 use_img_background
= 0;
4916 if (use_img_background
)
4917 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4919 /* Set all bits in mask_img to 1 whose color in ximg is different
4920 from the background color bg. */
4922 for (y
= 0; y
< img
->height
; ++y
)
4923 for (x
= 0; x
< img
->width
; ++x
)
4925 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4926 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4928 if (XGetPixel (ximg
, x
, y
) == bg
)
4929 ns_set_alpha (ximg
, x
, y
, 0);
4930 #endif /* HAVE_NS */
4932 /* Fill in the background_transparent field while we have the mask handy. */
4933 image_background_transparent (img
, f
, mask_img
);
4935 /* Put mask_img into img->mask. */
4936 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
4937 x_destroy_x_image (mask_img
);
4938 #endif /* !HAVE_NS */
4940 for (y
= 0; y
< img
->height
; ++y
)
4941 for (x
= 0; x
< img
->width
; ++x
)
4943 COLORREF p
= GetPixel (ximg
, x
, y
);
4945 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
4948 /* Create the mask image. */
4949 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
4951 /* Fill in the background_transparent field while we have the mask handy. */
4952 SelectObject (ximg
, img
->mask
);
4953 image_background_transparent (img
, f
, ximg
);
4955 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
4957 #endif /* HAVE_NTGUI */
4959 Destroy_Image (ximg
, prev
);
4965 /***********************************************************************
4966 PBM (mono, gray, color)
4967 ***********************************************************************/
4969 static int pbm_image_p (Lisp_Object object
);
4970 static int pbm_load (struct frame
*f
, struct image
*img
);
4971 static int pbm_scan_number (unsigned char **, unsigned char *);
4973 /* The symbol `pbm' identifying images of this type. */
4975 static Lisp_Object Qpbm
;
4977 /* Indices of image specification fields in gs_format, below. */
4979 enum pbm_keyword_index
4995 /* Vector of image_keyword structures describing the format
4996 of valid user-defined image specifications. */
4998 static const struct image_keyword pbm_format
[PBM_LAST
] =
5000 {":type", IMAGE_SYMBOL_VALUE
, 1},
5001 {":file", IMAGE_STRING_VALUE
, 0},
5002 {":data", IMAGE_STRING_VALUE
, 0},
5003 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5004 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5005 {":relief", IMAGE_INTEGER_VALUE
, 0},
5006 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5007 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5008 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5009 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
5010 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5013 /* Structure describing the image type `pbm'. */
5015 static struct image_type pbm_type
=
5025 /* Return non-zero if OBJECT is a valid PBM image specification. */
5028 pbm_image_p (Lisp_Object object
)
5030 struct image_keyword fmt
[PBM_LAST
];
5032 memcpy (fmt
, pbm_format
, sizeof fmt
);
5034 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
5037 /* Must specify either :data or :file. */
5038 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5042 /* Scan a decimal number from *S and return it. Advance *S while
5043 reading the number. END is the end of the string. Value is -1 at
5047 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5049 int c
= 0, val
= -1;
5053 /* Skip white-space. */
5054 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
5059 /* Skip comment to end of line. */
5060 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5063 else if (isdigit (c
))
5065 /* Read decimal number. */
5067 while (*s
< end
&& (c
= *(*s
)++, isdigit (c
)))
5068 val
= 10 * val
+ c
- '0';
5080 #if 0 /* Unused. ++kfs */
5082 /* Read FILE into memory. Value is a pointer to a buffer allocated
5083 with xmalloc holding FILE's contents. Value is null if an error
5084 occurred. *SIZE is set to the size of the file. */
5087 pbm_read_file (Lisp_Object file
, int *size
)
5093 if (stat (SDATA (file
), &st
) == 0
5094 && (fp
= fopen (SDATA (file
), "rb")) != NULL
5095 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
5096 && (buf
= (char *) xmalloc (st
.st_size
),
5097 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5116 #endif /* HAVE_NTGUI */
5118 /* Load PBM image IMG for use on frame F. */
5121 pbm_load (struct frame
*f
, struct image
*img
)
5124 int width
, height
, max_color_idx
= 0;
5126 Lisp_Object file
, specified_file
;
5127 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5128 unsigned char *contents
= NULL
;
5129 unsigned char *end
, *p
;
5132 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5134 if (STRINGP (specified_file
))
5136 file
= x_find_image_file (specified_file
);
5137 if (!STRINGP (file
))
5139 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5143 contents
= slurp_file (SSDATA (file
), &size
);
5144 if (contents
== NULL
)
5146 image_error ("Error reading `%s'", file
, Qnil
);
5151 end
= contents
+ size
;
5156 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5157 if (!STRINGP (data
))
5159 image_error ("Invalid image data `%s'", data
, Qnil
);
5163 end
= p
+ SBYTES (data
);
5166 /* Check magic number. */
5167 if (end
- p
< 2 || *p
++ != 'P')
5169 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5178 raw_p
= 0, type
= PBM_MONO
;
5182 raw_p
= 0, type
= PBM_GRAY
;
5186 raw_p
= 0, type
= PBM_COLOR
;
5190 raw_p
= 1, type
= PBM_MONO
;
5194 raw_p
= 1, type
= PBM_GRAY
;
5198 raw_p
= 1, type
= PBM_COLOR
;
5202 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5206 /* Read width, height, maximum color-component. Characters
5207 starting with `#' up to the end of a line are ignored. */
5208 width
= pbm_scan_number (&p
, end
);
5209 height
= pbm_scan_number (&p
, end
);
5211 if (type
!= PBM_MONO
)
5213 max_color_idx
= pbm_scan_number (&p
, end
);
5214 if (max_color_idx
> 65535 || max_color_idx
< 0)
5216 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5221 if (!check_image_size (f
, width
, height
))
5223 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5227 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
5228 &ximg
, &img
->pixmap
))
5231 /* Initialize the color hash table. */
5232 init_color_table ();
5234 if (type
== PBM_MONO
)
5237 struct image_keyword fmt
[PBM_LAST
];
5238 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5239 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5241 /* Parse the image specification. */
5242 memcpy (fmt
, pbm_format
, sizeof fmt
);
5243 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5245 /* Get foreground and background colors, maybe allocate colors. */
5246 if (fmt
[PBM_FOREGROUND
].count
5247 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5248 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5249 if (fmt
[PBM_BACKGROUND
].count
5250 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5252 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5253 img
->background
= bg
;
5254 img
->background_valid
= 1;
5257 for (y
= 0; y
< height
; ++y
)
5258 for (x
= 0; x
< width
; ++x
)
5266 x_destroy_x_image (ximg
);
5267 x_clear_image (f
, img
);
5268 image_error ("Invalid image size in image `%s'",
5278 g
= pbm_scan_number (&p
, end
);
5280 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5285 int expected_size
= height
* width
;
5286 if (max_color_idx
> 255)
5288 if (type
== PBM_COLOR
)
5291 if (raw_p
&& p
+ expected_size
> end
)
5293 x_destroy_x_image (ximg
);
5294 x_clear_image (f
, img
);
5295 image_error ("Invalid image size in image `%s'",
5300 for (y
= 0; y
< height
; ++y
)
5301 for (x
= 0; x
< width
; ++x
)
5305 if (type
== PBM_GRAY
&& raw_p
)
5308 if (max_color_idx
> 255)
5309 r
= g
= b
= r
* 256 + *p
++;
5311 else if (type
== PBM_GRAY
)
5312 r
= g
= b
= pbm_scan_number (&p
, end
);
5316 if (max_color_idx
> 255)
5319 if (max_color_idx
> 255)
5322 if (max_color_idx
> 255)
5327 r
= pbm_scan_number (&p
, end
);
5328 g
= pbm_scan_number (&p
, end
);
5329 b
= pbm_scan_number (&p
, end
);
5332 if (r
< 0 || g
< 0 || b
< 0)
5334 x_destroy_x_image (ximg
);
5335 image_error ("Invalid pixel value in image `%s'",
5340 /* RGB values are now in the range 0..max_color_idx.
5341 Scale this to the range 0..0xffff supported by X. */
5342 r
= (double) r
* 65535 / max_color_idx
;
5343 g
= (double) g
* 65535 / max_color_idx
;
5344 b
= (double) b
* 65535 / max_color_idx
;
5345 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5349 #ifdef COLOR_TABLE_SUPPORT
5350 /* Store in IMG->colors the colors allocated for the image, and
5351 free the color table. */
5352 img
->colors
= colors_in_color_table (&img
->ncolors
);
5353 free_color_table ();
5354 #endif /* COLOR_TABLE_SUPPORT */
5357 img
->height
= height
;
5359 /* Maybe fill in the background field while we have ximg handy. */
5361 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5362 /* Casting avoids a GCC warning. */
5363 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5365 /* Put the image into a pixmap. */
5366 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5367 x_destroy_x_image (ximg
);
5369 /* X and W32 versions did it here, MAC version above. ++kfs
5371 img->height = height; */
5378 /***********************************************************************
5380 ***********************************************************************/
5382 #if defined (HAVE_PNG) || defined (HAVE_NS)
5384 /* Function prototypes. */
5386 static int png_image_p (Lisp_Object object
);
5387 static int png_load (struct frame
*f
, struct image
*img
);
5389 /* The symbol `png' identifying images of this type. */
5391 static Lisp_Object Qpng
;
5393 /* Indices of image specification fields in png_format, below. */
5395 enum png_keyword_index
5410 /* Vector of image_keyword structures describing the format
5411 of valid user-defined image specifications. */
5413 static const struct image_keyword png_format
[PNG_LAST
] =
5415 {":type", IMAGE_SYMBOL_VALUE
, 1},
5416 {":data", IMAGE_STRING_VALUE
, 0},
5417 {":file", IMAGE_STRING_VALUE
, 0},
5418 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5419 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5420 {":relief", IMAGE_INTEGER_VALUE
, 0},
5421 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5422 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5423 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5424 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5427 /* Structure describing the image type `png'. */
5429 static struct image_type png_type
=
5438 /* Return non-zero if OBJECT is a valid PNG image specification. */
5441 png_image_p (Lisp_Object object
)
5443 struct image_keyword fmt
[PNG_LAST
];
5444 memcpy (fmt
, png_format
, sizeof fmt
);
5446 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5449 /* Must specify either the :data or :file keyword. */
5450 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5453 #endif /* HAVE_PNG || HAVE_NS */
5459 /* PNG library details. */
5461 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5462 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5463 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5464 png_error_ptr
, png_error_ptr
));
5465 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5466 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5467 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5468 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5469 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5470 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5471 png_uint_32
*, png_uint_32
*,
5472 int *, int *, int *, int *, int *));
5473 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5474 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5475 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5476 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5477 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5479 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5480 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5481 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5482 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5483 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5484 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5485 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5487 #if (PNG_LIBPNG_VER >= 10500)
5488 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5489 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5490 #endif /* libpng version >= 1.5 */
5493 init_png_functions (Lisp_Object libraries
)
5497 if (!(library
= w32_delayed_load (libraries
, Qpng
)))
5500 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5501 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5502 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5503 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5504 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5505 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5506 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5507 LOAD_IMGLIB_FN (library
, png_read_info
);
5508 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5509 LOAD_IMGLIB_FN (library
, png_get_valid
);
5510 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5511 LOAD_IMGLIB_FN (library
, png_set_expand
);
5512 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5513 LOAD_IMGLIB_FN (library
, png_set_background
);
5514 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5515 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5516 LOAD_IMGLIB_FN (library
, png_get_channels
);
5517 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5518 LOAD_IMGLIB_FN (library
, png_read_image
);
5519 LOAD_IMGLIB_FN (library
, png_read_end
);
5520 LOAD_IMGLIB_FN (library
, png_error
);
5522 #if (PNG_LIBPNG_VER >= 10500)
5523 LOAD_IMGLIB_FN (library
, png_longjmp
);
5524 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5525 #endif /* libpng version >= 1.5 */
5531 #define fn_png_get_io_ptr png_get_io_ptr
5532 #define fn_png_sig_cmp png_sig_cmp
5533 #define fn_png_create_read_struct png_create_read_struct
5534 #define fn_png_create_info_struct png_create_info_struct
5535 #define fn_png_destroy_read_struct png_destroy_read_struct
5536 #define fn_png_set_read_fn png_set_read_fn
5537 #define fn_png_set_sig_bytes png_set_sig_bytes
5538 #define fn_png_read_info png_read_info
5539 #define fn_png_get_IHDR png_get_IHDR
5540 #define fn_png_get_valid png_get_valid
5541 #define fn_png_set_strip_16 png_set_strip_16
5542 #define fn_png_set_expand png_set_expand
5543 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5544 #define fn_png_set_background png_set_background
5545 #define fn_png_get_bKGD png_get_bKGD
5546 #define fn_png_read_update_info png_read_update_info
5547 #define fn_png_get_channels png_get_channels
5548 #define fn_png_get_rowbytes png_get_rowbytes
5549 #define fn_png_read_image png_read_image
5550 #define fn_png_read_end png_read_end
5551 #define fn_png_error png_error
5553 #if (PNG_LIBPNG_VER >= 10500)
5554 #define fn_png_longjmp png_longjmp
5555 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5556 #endif /* libpng version >= 1.5 */
5558 #endif /* HAVE_NTGUI */
5561 #if (PNG_LIBPNG_VER < 10500)
5562 #define PNG_LONGJMP(ptr) (longjmp ((ptr)->jmpbuf, 1))
5563 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5565 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5566 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5567 #define PNG_JMPBUF(ptr) \
5568 (*fn_png_set_longjmp_fn((ptr), longjmp, sizeof (jmp_buf)))
5571 /* Error and warning handlers installed when the PNG library
5574 static void my_png_error (png_struct
*, const char *) NO_RETURN
;
5576 my_png_error (png_struct
*png_ptr
, const char *msg
)
5578 xassert (png_ptr
!= NULL
);
5579 /* Avoid compiler warning about deprecated direct access to
5580 png_ptr's fields in libpng versions 1.4.x. */
5581 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5582 PNG_LONGJMP (png_ptr
);
5587 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5589 xassert (png_ptr
!= NULL
);
5590 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5593 /* Memory source for PNG decoding. */
5595 struct png_memory_storage
5597 unsigned char *bytes
; /* The data */
5598 ptrdiff_t len
; /* How big is it? */
5599 ptrdiff_t index
; /* Where are we? */
5603 /* Function set as reader function when reading PNG image from memory.
5604 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5605 bytes from the input to DATA. */
5608 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5610 struct png_memory_storage
*tbr
5611 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5613 if (length
> tbr
->len
- tbr
->index
)
5614 fn_png_error (png_ptr
, "Read error");
5616 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5617 tbr
->index
= tbr
->index
+ length
;
5621 /* Function set as reader function when reading PNG image from a file.
5622 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5623 bytes from the input to DATA. */
5626 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5628 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5630 if (fread (data
, 1, length
, fp
) < length
)
5631 fn_png_error (png_ptr
, "Read error");
5635 /* Load PNG image IMG for use on frame F. Value is non-zero if
5639 png_load (struct frame
*f
, struct image
*img
)
5641 Lisp_Object file
, specified_file
;
5642 Lisp_Object specified_data
;
5645 XImagePtr ximg
, mask_img
= NULL
;
5646 png_struct
*png_ptr
= NULL
;
5647 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5648 FILE *volatile fp
= NULL
;
5650 png_byte
* volatile pixels
= NULL
;
5651 png_byte
** volatile rows
= NULL
;
5652 png_uint_32 width
, height
;
5653 int bit_depth
, color_type
, interlace_type
;
5655 png_uint_32 row_bytes
;
5657 struct png_memory_storage tbr
; /* Data to be read */
5659 /* Find out what file to load. */
5660 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5661 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5663 if (NILP (specified_data
))
5665 file
= x_find_image_file (specified_file
);
5666 if (!STRINGP (file
))
5668 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5672 /* Open the image file. */
5673 fp
= fopen (SSDATA (file
), "rb");
5676 image_error ("Cannot open image file `%s'", file
, Qnil
);
5680 /* Check PNG signature. */
5681 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5682 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5684 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5691 if (!STRINGP (specified_data
))
5693 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5697 /* Read from memory. */
5698 tbr
.bytes
= SDATA (specified_data
);
5699 tbr
.len
= SBYTES (specified_data
);
5702 /* Check PNG signature. */
5703 if (tbr
.len
< sizeof sig
5704 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5706 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5710 /* Need to skip past the signature. */
5711 tbr
.bytes
+= sizeof (sig
);
5714 /* Initialize read and info structs for PNG lib. */
5715 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5720 if (fp
) fclose (fp
);
5724 info_ptr
= fn_png_create_info_struct (png_ptr
);
5727 fn_png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
5728 if (fp
) fclose (fp
);
5732 end_info
= fn_png_create_info_struct (png_ptr
);
5735 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
5736 if (fp
) fclose (fp
);
5740 /* Set error jump-back. We come back here when the PNG library
5741 detects an error. */
5742 if (setjmp (PNG_JMPBUF (png_ptr
)))
5746 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5749 if (fp
) fclose (fp
);
5753 /* Read image info. */
5754 if (!NILP (specified_data
))
5755 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5757 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5759 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5760 fn_png_read_info (png_ptr
, info_ptr
);
5761 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5762 &interlace_type
, NULL
, NULL
);
5764 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5765 && check_image_size (f
, width
, height
)))
5767 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5771 /* Create the X image and pixmap now, so that the work below can be
5772 omitted if the image is too large for X. */
5773 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
5777 /* If image contains simply transparency data, we prefer to
5778 construct a clipping mask. */
5779 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5784 /* This function is easier to write if we only have to handle
5785 one data format: RGB or RGBA with 8 bits per channel. Let's
5786 transform other formats into that format. */
5788 /* Strip more than 8 bits per channel. */
5789 if (bit_depth
== 16)
5790 fn_png_set_strip_16 (png_ptr
);
5792 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5794 fn_png_set_expand (png_ptr
);
5796 /* Convert grayscale images to RGB. */
5797 if (color_type
== PNG_COLOR_TYPE_GRAY
5798 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5799 fn_png_set_gray_to_rgb (png_ptr
);
5801 /* Handle alpha channel by combining the image with a background
5802 color. Do this only if a real alpha channel is supplied. For
5803 simple transparency, we prefer a clipping mask. */
5806 /* png_color_16 *image_bg; */
5807 Lisp_Object specified_bg
5808 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5809 int shift
= (bit_depth
== 16) ? 0 : 8;
5811 if (STRINGP (specified_bg
))
5812 /* The user specified `:background', use that. */
5815 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5817 png_color_16 user_bg
;
5819 memset (&user_bg
, 0, sizeof user_bg
);
5820 user_bg
.red
= color
.red
>> shift
;
5821 user_bg
.green
= color
.green
>> shift
;
5822 user_bg
.blue
= color
.blue
>> shift
;
5824 fn_png_set_background (png_ptr
, &user_bg
,
5825 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5830 /* We use the current frame background, ignoring any default
5831 background color set by the image. */
5832 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5834 png_color_16 frame_background
;
5836 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5837 x_query_color (f
, &color
);
5839 memset (&frame_background
, 0, sizeof frame_background
);
5840 frame_background
.red
= color
.red
>> shift
;
5841 frame_background
.green
= color
.green
>> shift
;
5842 frame_background
.blue
= color
.blue
>> shift
;
5843 #endif /* HAVE_X_WINDOWS */
5845 fn_png_set_background (png_ptr
, &frame_background
,
5846 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5850 /* Update info structure. */
5851 fn_png_read_update_info (png_ptr
, info_ptr
);
5853 /* Get number of channels. Valid values are 1 for grayscale images
5854 and images with a palette, 2 for grayscale images with transparency
5855 information (alpha channel), 3 for RGB images, and 4 for RGB
5856 images with alpha channel, i.e. RGBA. If conversions above were
5857 sufficient we should only have 3 or 4 channels here. */
5858 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5859 xassert (channels
== 3 || channels
== 4);
5861 /* Number of bytes needed for one row of the image. */
5862 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5864 /* Allocate memory for the image. */
5865 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5866 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5867 memory_full (SIZE_MAX
);
5868 pixels
= (png_byte
*) xmalloc (sizeof *pixels
* row_bytes
* height
);
5869 rows
= (png_byte
**) xmalloc (height
* sizeof *rows
);
5870 for (i
= 0; i
< height
; ++i
)
5871 rows
[i
] = pixels
+ i
* row_bytes
;
5873 /* Read the entire image. */
5874 fn_png_read_image (png_ptr
, rows
);
5875 fn_png_read_end (png_ptr
, info_ptr
);
5882 /* Create an image and pixmap serving as mask if the PNG image
5883 contains an alpha channel. */
5886 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
5887 &mask_img
, &img
->mask
))
5889 x_destroy_x_image (ximg
);
5890 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5891 img
->pixmap
= NO_PIXMAP
;
5895 /* Fill the X image and mask from PNG data. */
5896 init_color_table ();
5898 for (y
= 0; y
< height
; ++y
)
5900 png_byte
*p
= rows
[y
];
5902 for (x
= 0; x
< width
; ++x
)
5909 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5910 /* An alpha channel, aka mask channel, associates variable
5911 transparency with an image. Where other image formats
5912 support binary transparency---fully transparent or fully
5913 opaque---PNG allows up to 254 levels of partial transparency.
5914 The PNG library implements partial transparency by combining
5915 the image with a specified background color.
5917 I'm not sure how to handle this here nicely: because the
5918 background on which the image is displayed may change, for
5919 real alpha channel support, it would be necessary to create
5920 a new image for each possible background.
5922 What I'm doing now is that a mask is created if we have
5923 boolean transparency information. Otherwise I'm using
5924 the frame's background color to combine the image with. */
5929 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5935 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5936 /* Set IMG's background color from the PNG image, unless the user
5940 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
5942 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
5943 img
->background_valid
= 1;
5947 #ifdef COLOR_TABLE_SUPPORT
5948 /* Remember colors allocated for this image. */
5949 img
->colors
= colors_in_color_table (&img
->ncolors
);
5950 free_color_table ();
5951 #endif /* COLOR_TABLE_SUPPORT */
5954 fn_png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
5959 img
->height
= height
;
5961 /* Maybe fill in the background field while we have ximg handy.
5962 Casting avoids a GCC warning. */
5963 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5965 /* Put the image into the pixmap, then free the X image and its buffer. */
5966 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
5967 x_destroy_x_image (ximg
);
5969 /* Same for the mask. */
5972 /* Fill in the background_transparent field while we have the
5973 mask handy. Casting avoids a GCC warning. */
5974 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
5976 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
5977 x_destroy_x_image (mask_img
);
5983 #else /* HAVE_PNG */
5987 png_load (struct frame
*f
, struct image
*img
)
5989 return ns_load_image(f
, img
,
5990 image_spec_value (img
->spec
, QCfile
, NULL
),
5991 image_spec_value (img
->spec
, QCdata
, NULL
));
5993 #endif /* HAVE_NS */
5996 #endif /* !HAVE_PNG */
6000 /***********************************************************************
6002 ***********************************************************************/
6004 #if defined (HAVE_JPEG) || defined (HAVE_NS)
6006 static int jpeg_image_p (Lisp_Object object
);
6007 static int jpeg_load (struct frame
*f
, struct image
*img
);
6009 /* The symbol `jpeg' identifying images of this type. */
6011 static Lisp_Object Qjpeg
;
6013 /* Indices of image specification fields in gs_format, below. */
6015 enum jpeg_keyword_index
6024 JPEG_HEURISTIC_MASK
,
6030 /* Vector of image_keyword structures describing the format
6031 of valid user-defined image specifications. */
6033 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
6035 {":type", IMAGE_SYMBOL_VALUE
, 1},
6036 {":data", IMAGE_STRING_VALUE
, 0},
6037 {":file", IMAGE_STRING_VALUE
, 0},
6038 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6039 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6040 {":relief", IMAGE_INTEGER_VALUE
, 0},
6041 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6042 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6043 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6044 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6047 /* Structure describing the image type `jpeg'. */
6049 static struct image_type jpeg_type
=
6058 /* Return non-zero if OBJECT is a valid JPEG image specification. */
6061 jpeg_image_p (Lisp_Object object
)
6063 struct image_keyword fmt
[JPEG_LAST
];
6065 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6067 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6070 /* Must specify either the :data or :file keyword. */
6071 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6074 #endif /* HAVE_JPEG || HAVE_NS */
6078 /* Work around a warning about HAVE_STDLIB_H being redefined in
6080 #ifdef HAVE_STDLIB_H
6081 #undef HAVE_STDLIB_H
6082 #endif /* HAVE_STLIB_H */
6084 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6085 /* In older releases of the jpeg library, jpeglib.h will define boolean
6086 differently depending on __WIN32__, so make sure it is defined. */
6090 #include <jpeglib.h>
6093 #ifdef HAVE_STLIB_H_1
6094 #define HAVE_STDLIB_H 1
6099 /* JPEG library details. */
6100 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6101 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6102 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6103 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6104 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6105 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6106 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6107 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6110 init_jpeg_functions (Lisp_Object libraries
)
6114 if (!(library
= w32_delayed_load (libraries
, Qjpeg
)))
6117 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6118 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6119 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6120 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6121 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6122 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6123 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6124 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6128 /* Wrapper since we can't directly assign the function pointer
6129 to another function pointer that was declared more completely easily. */
6131 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6133 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6138 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a)
6139 #define fn_jpeg_start_decompress jpeg_start_decompress
6140 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6141 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6142 #define fn_jpeg_read_header jpeg_read_header
6143 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6144 #define fn_jpeg_std_error jpeg_std_error
6145 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6147 #endif /* HAVE_NTGUI */
6149 struct my_jpeg_error_mgr
6151 struct jpeg_error_mgr pub
;
6152 jmp_buf setjmp_buffer
;
6156 static void my_error_exit (j_common_ptr
) NO_RETURN
;
6158 my_error_exit (j_common_ptr cinfo
)
6160 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6161 longjmp (mgr
->setjmp_buffer
, 1);
6165 /* Init source method for JPEG data source manager. Called by
6166 jpeg_read_header() before any data is actually read. See
6167 libjpeg.doc from the JPEG lib distribution. */
6170 our_common_init_source (j_decompress_ptr cinfo
)
6175 /* Method to terminate data source. Called by
6176 jpeg_finish_decompress() after all data has been processed. */
6179 our_common_term_source (j_decompress_ptr cinfo
)
6184 /* Fill input buffer method for JPEG data source manager. Called
6185 whenever more data is needed. We read the whole image in one step,
6186 so this only adds a fake end of input marker at the end. */
6188 static JOCTET our_memory_buffer
[2];
6191 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6193 /* Insert a fake EOI marker. */
6194 struct jpeg_source_mgr
*src
= cinfo
->src
;
6196 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6197 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6199 src
->next_input_byte
= our_memory_buffer
;
6200 src
->bytes_in_buffer
= 2;
6205 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6206 is the JPEG data source manager. */
6209 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6211 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6215 if (num_bytes
> src
->bytes_in_buffer
)
6216 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6218 src
->bytes_in_buffer
-= num_bytes
;
6219 src
->next_input_byte
+= num_bytes
;
6224 /* Set up the JPEG lib for reading an image from DATA which contains
6225 LEN bytes. CINFO is the decompression info structure created for
6226 reading the image. */
6229 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, unsigned int len
)
6231 struct jpeg_source_mgr
*src
;
6233 if (cinfo
->src
== NULL
)
6235 /* First time for this JPEG object? */
6236 cinfo
->src
= (struct jpeg_source_mgr
*)
6237 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6238 sizeof (struct jpeg_source_mgr
));
6239 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6240 src
->next_input_byte
= data
;
6243 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6244 src
->init_source
= our_common_init_source
;
6245 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6246 src
->skip_input_data
= our_memory_skip_input_data
;
6247 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6248 src
->term_source
= our_common_term_source
;
6249 src
->bytes_in_buffer
= len
;
6250 src
->next_input_byte
= data
;
6254 struct jpeg_stdio_mgr
6256 struct jpeg_source_mgr mgr
;
6263 /* Size of buffer to read JPEG from file.
6264 Not too big, as we want to use alloc_small. */
6265 #define JPEG_STDIO_BUFFER_SIZE 8192
6268 /* Fill input buffer method for JPEG data source manager. Called
6269 whenever more data is needed. The data is read from a FILE *. */
6272 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6274 struct jpeg_stdio_mgr
*src
;
6276 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6281 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6283 src
->mgr
.bytes_in_buffer
= bytes
;
6286 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6288 src
->buffer
[0] = (JOCTET
) 0xFF;
6289 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6290 src
->mgr
.bytes_in_buffer
= 2;
6292 src
->mgr
.next_input_byte
= src
->buffer
;
6299 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6300 is the JPEG data source manager. */
6303 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6305 struct jpeg_stdio_mgr
*src
;
6306 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6308 while (num_bytes
> 0 && !src
->finished
)
6310 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6312 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6313 src
->mgr
.next_input_byte
+= num_bytes
;
6318 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6319 src
->mgr
.bytes_in_buffer
= 0;
6320 src
->mgr
.next_input_byte
= NULL
;
6322 our_stdio_fill_input_buffer (cinfo
);
6328 /* Set up the JPEG lib for reading an image from a FILE *.
6329 CINFO is the decompression info structure created for
6330 reading the image. */
6333 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6335 struct jpeg_stdio_mgr
*src
;
6337 if (cinfo
->src
!= NULL
)
6338 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6341 /* First time for this JPEG object? */
6342 cinfo
->src
= (struct jpeg_source_mgr
*)
6343 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6344 sizeof (struct jpeg_stdio_mgr
));
6345 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6346 src
->buffer
= (JOCTET
*)
6347 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6348 JPEG_STDIO_BUFFER_SIZE
);
6353 src
->mgr
.init_source
= our_common_init_source
;
6354 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6355 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6356 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6357 src
->mgr
.term_source
= our_common_term_source
;
6358 src
->mgr
.bytes_in_buffer
= 0;
6359 src
->mgr
.next_input_byte
= NULL
;
6363 /* Load image IMG for use on frame F. Patterned after example.c
6364 from the JPEG lib. */
6367 jpeg_load (struct frame
*f
, struct image
*img
)
6369 struct jpeg_decompress_struct cinfo
;
6370 struct my_jpeg_error_mgr mgr
;
6371 Lisp_Object file
, specified_file
;
6372 Lisp_Object specified_data
;
6373 FILE * volatile fp
= NULL
;
6375 int row_stride
, x
, y
;
6376 XImagePtr ximg
= NULL
;
6378 unsigned long *colors
;
6381 /* Open the JPEG file. */
6382 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6383 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6385 if (NILP (specified_data
))
6387 file
= x_find_image_file (specified_file
);
6388 if (!STRINGP (file
))
6390 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6394 fp
= fopen (SSDATA (file
), "rb");
6397 image_error ("Cannot open `%s'", file
, Qnil
);
6401 else if (!STRINGP (specified_data
))
6403 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6407 /* Customize libjpeg's error handling to call my_error_exit when an
6408 error is detected. This function will perform a longjmp. */
6409 cinfo
.err
= fn_jpeg_std_error (&mgr
.pub
);
6410 mgr
.pub
.error_exit
= my_error_exit
;
6412 if ((rc
= setjmp (mgr
.setjmp_buffer
)) != 0)
6416 /* Called from my_error_exit. Display a JPEG error. */
6417 char buf
[JMSG_LENGTH_MAX
];
6418 cinfo
.err
->format_message ((j_common_ptr
) &cinfo
, buf
);
6419 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6420 build_string (buf
));
6423 /* Close the input file and destroy the JPEG object. */
6425 fclose ((FILE *) fp
);
6426 fn_jpeg_destroy_decompress (&cinfo
);
6428 /* If we already have an XImage, free that. */
6429 x_destroy_x_image (ximg
);
6431 /* Free pixmap and colors. */
6432 x_clear_image (f
, img
);
6436 /* Create the JPEG decompression object. Let it read from fp.
6437 Read the JPEG image header. */
6438 fn_jpeg_CreateDecompress (&cinfo
, JPEG_LIB_VERSION
, sizeof (cinfo
));
6440 if (NILP (specified_data
))
6441 jpeg_file_src (&cinfo
, (FILE *) fp
);
6443 jpeg_memory_src (&cinfo
, SDATA (specified_data
),
6444 SBYTES (specified_data
));
6446 fn_jpeg_read_header (&cinfo
, 1);
6448 /* Customize decompression so that color quantization will be used.
6449 Start decompression. */
6450 cinfo
.quantize_colors
= 1;
6451 fn_jpeg_start_decompress (&cinfo
);
6452 width
= img
->width
= cinfo
.output_width
;
6453 height
= img
->height
= cinfo
.output_height
;
6455 if (!check_image_size (f
, width
, height
))
6457 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6458 longjmp (mgr
.setjmp_buffer
, 2);
6461 /* Create X image and pixmap. */
6462 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
6463 longjmp (mgr
.setjmp_buffer
, 2);
6465 /* Allocate colors. When color quantization is used,
6466 cinfo.actual_number_of_colors has been set with the number of
6467 colors generated, and cinfo.colormap is a two-dimensional array
6468 of color indices in the range 0..cinfo.actual_number_of_colors.
6469 No more than 255 colors will be generated. */
6473 if (cinfo
.out_color_components
> 2)
6474 ir
= 0, ig
= 1, ib
= 2;
6475 else if (cinfo
.out_color_components
> 1)
6476 ir
= 0, ig
= 1, ib
= 0;
6478 ir
= 0, ig
= 0, ib
= 0;
6480 /* Use the color table mechanism because it handles colors that
6481 cannot be allocated nicely. Such colors will be replaced with
6482 a default color, and we don't have to care about which colors
6483 can be freed safely, and which can't. */
6484 init_color_table ();
6485 colors
= (unsigned long *) alloca (cinfo
.actual_number_of_colors
6488 for (i
= 0; i
< cinfo
.actual_number_of_colors
; ++i
)
6490 /* Multiply RGB values with 255 because X expects RGB values
6491 in the range 0..0xffff. */
6492 int r
= cinfo
.colormap
[ir
][i
] << 8;
6493 int g
= cinfo
.colormap
[ig
][i
] << 8;
6494 int b
= cinfo
.colormap
[ib
][i
] << 8;
6495 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6498 #ifdef COLOR_TABLE_SUPPORT
6499 /* Remember those colors actually allocated. */
6500 img
->colors
= colors_in_color_table (&img
->ncolors
);
6501 free_color_table ();
6502 #endif /* COLOR_TABLE_SUPPORT */
6506 row_stride
= width
* cinfo
.output_components
;
6507 buffer
= cinfo
.mem
->alloc_sarray ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
,
6509 for (y
= 0; y
< height
; ++y
)
6511 fn_jpeg_read_scanlines (&cinfo
, buffer
, 1);
6512 for (x
= 0; x
< cinfo
.output_width
; ++x
)
6513 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6517 fn_jpeg_finish_decompress (&cinfo
);
6518 fn_jpeg_destroy_decompress (&cinfo
);
6520 fclose ((FILE *) fp
);
6522 /* Maybe fill in the background field while we have ximg handy. */
6523 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6524 /* Casting avoids a GCC warning. */
6525 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6527 /* Put the image into the pixmap. */
6528 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6529 x_destroy_x_image (ximg
);
6533 #else /* HAVE_JPEG */
6537 jpeg_load (struct frame
*f
, struct image
*img
)
6539 return ns_load_image (f
, img
,
6540 image_spec_value (img
->spec
, QCfile
, NULL
),
6541 image_spec_value (img
->spec
, QCdata
, NULL
));
6543 #endif /* HAVE_NS */
6545 #endif /* !HAVE_JPEG */
6549 /***********************************************************************
6551 ***********************************************************************/
6553 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6555 static int tiff_image_p (Lisp_Object object
);
6556 static int tiff_load (struct frame
*f
, struct image
*img
);
6558 /* The symbol `tiff' identifying images of this type. */
6560 static Lisp_Object Qtiff
;
6562 /* Indices of image specification fields in tiff_format, below. */
6564 enum tiff_keyword_index
6573 TIFF_HEURISTIC_MASK
,
6580 /* Vector of image_keyword structures describing the format
6581 of valid user-defined image specifications. */
6583 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6585 {":type", IMAGE_SYMBOL_VALUE
, 1},
6586 {":data", IMAGE_STRING_VALUE
, 0},
6587 {":file", IMAGE_STRING_VALUE
, 0},
6588 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6589 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6590 {":relief", IMAGE_INTEGER_VALUE
, 0},
6591 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6592 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6593 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6594 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6595 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6598 /* Structure describing the image type `tiff'. */
6600 static struct image_type tiff_type
=
6609 /* Return non-zero if OBJECT is a valid TIFF image specification. */
6612 tiff_image_p (Lisp_Object object
)
6614 struct image_keyword fmt
[TIFF_LAST
];
6615 memcpy (fmt
, tiff_format
, sizeof fmt
);
6617 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6620 /* Must specify either the :data or :file keyword. */
6621 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6624 #endif /* HAVE_TIFF || HAVE_NS */
6632 /* TIFF library details. */
6633 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6634 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6635 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6636 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6637 TIFFReadWriteProc
, TIFFReadWriteProc
,
6638 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6639 TIFFMapFileProc
, TIFFUnmapFileProc
));
6640 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6641 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6642 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6643 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6646 init_tiff_functions (Lisp_Object libraries
)
6650 if (!(library
= w32_delayed_load (libraries
, Qtiff
)))
6653 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6654 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6655 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6656 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6657 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6658 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6659 LOAD_IMGLIB_FN (library
, TIFFClose
);
6660 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6666 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6667 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6668 #define fn_TIFFOpen TIFFOpen
6669 #define fn_TIFFClientOpen TIFFClientOpen
6670 #define fn_TIFFGetField TIFFGetField
6671 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6672 #define fn_TIFFClose TIFFClose
6673 #define fn_TIFFSetDirectory TIFFSetDirectory
6674 #endif /* HAVE_NTGUI */
6677 /* Reading from a memory buffer for TIFF images Based on the PNG
6678 memory source, but we have to provide a lot of extra functions.
6681 We really only need to implement read and seek, but I am not
6682 convinced that the TIFF library is smart enough not to destroy
6683 itself if we only hand it the function pointers we need to
6688 unsigned char *bytes
;
6695 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6697 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6699 size
= min (size
, src
->len
- src
->index
);
6700 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6706 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6712 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6714 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6719 case SEEK_SET
: /* Go from beginning of source. */
6723 case SEEK_END
: /* Go from end of source. */
6724 idx
= src
->len
+ off
;
6727 case SEEK_CUR
: /* Go from current position. */
6728 idx
= src
->index
+ off
;
6731 default: /* Invalid `whence'. */
6735 if (idx
> src
->len
|| idx
< 0)
6743 tiff_close_memory (thandle_t data
)
6750 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6752 /* It is already _IN_ memory. */
6757 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6759 /* We don't need to do this. */
6763 tiff_size_of_memory (thandle_t data
)
6765 return ((tiff_memory_source
*) data
)->len
;
6769 static void tiff_handler (const char *, const char *, const char *, va_list)
6770 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6772 tiff_handler (const char *log_format
, const char *title
,
6773 const char *format
, va_list ap
)
6775 /* doprnt is not suitable here, as TIFF handlers are called from
6776 libtiff and are passed arbitrary printf directives. Instead, use
6777 vsnprintf, taking care to be portable to nonstandard environments
6778 where vsnprintf returns -1 on buffer overflow. Since it's just a
6779 log entry, it's OK to truncate it. */
6781 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6782 add_to_log (log_format
, build_string (title
),
6783 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6786 static void tiff_error_handler (const char *, const char *, va_list)
6787 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6789 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6791 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6795 static void tiff_warning_handler (const char *, const char *, va_list)
6796 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6798 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6800 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6804 /* Load TIFF image IMG for use on frame F. Value is non-zero if
6808 tiff_load (struct frame
*f
, struct image
*img
)
6810 Lisp_Object file
, specified_file
;
6811 Lisp_Object specified_data
;
6813 int width
, height
, x
, y
, count
;
6817 tiff_memory_source memsrc
;
6820 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6821 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6823 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6824 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6826 if (NILP (specified_data
))
6828 /* Read from a file */
6829 file
= x_find_image_file (specified_file
);
6830 if (!STRINGP (file
))
6832 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6836 /* Try to open the image file. */
6837 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6840 image_error ("Cannot open `%s'", file
, Qnil
);
6846 if (!STRINGP (specified_data
))
6848 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6852 /* Memory source! */
6853 memsrc
.bytes
= SDATA (specified_data
);
6854 memsrc
.len
= SBYTES (specified_data
);
6857 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6858 tiff_read_from_memory
,
6859 tiff_write_from_memory
,
6860 tiff_seek_in_memory
,
6862 tiff_size_of_memory
,
6868 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
6873 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
6874 if (INTEGERP (image
))
6876 EMACS_INT ino
= XFASTINT (image
);
6877 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
6878 && fn_TIFFSetDirectory (tiff
, ino
)))
6880 image_error ("Invalid image number `%s' in image `%s'",
6882 fn_TIFFClose (tiff
);
6887 /* Get width and height of the image, and allocate a raster buffer
6888 of width x height 32-bit values. */
6889 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
6890 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
6892 if (!check_image_size (f
, width
, height
))
6894 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6895 fn_TIFFClose (tiff
);
6899 /* Create the X image and pixmap. */
6900 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
6901 && x_create_x_image_and_pixmap (f
, width
, height
, 0,
6902 &ximg
, &img
->pixmap
)))
6904 fn_TIFFClose (tiff
);
6908 buf
= (uint32
*) xmalloc (sizeof *buf
* width
* height
);
6910 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
6912 /* Count the number of images in the file. */
6913 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
6917 img
->lisp_data
= Fcons (Qcount
,
6918 Fcons (make_number (count
),
6921 fn_TIFFClose (tiff
);
6924 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
6929 /* Initialize the color table. */
6930 init_color_table ();
6932 /* Process the pixel raster. Origin is in the lower-left corner. */
6933 for (y
= 0; y
< height
; ++y
)
6935 uint32
*row
= buf
+ y
* width
;
6937 for (x
= 0; x
< width
; ++x
)
6939 uint32 abgr
= row
[x
];
6940 int r
= TIFFGetR (abgr
) << 8;
6941 int g
= TIFFGetG (abgr
) << 8;
6942 int b
= TIFFGetB (abgr
) << 8;
6943 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
6947 #ifdef COLOR_TABLE_SUPPORT
6948 /* Remember the colors allocated for the image. Free the color table. */
6949 img
->colors
= colors_in_color_table (&img
->ncolors
);
6950 free_color_table ();
6951 #endif /* COLOR_TABLE_SUPPORT */
6954 img
->height
= height
;
6956 /* Maybe fill in the background field while we have ximg handy. */
6957 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6958 /* Casting avoids a GCC warning on W32. */
6959 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6961 /* Put the image into the pixmap, then free the X image and its buffer. */
6962 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
6963 x_destroy_x_image (ximg
);
6969 #else /* HAVE_TIFF */
6973 tiff_load (struct frame
*f
, struct image
*img
)
6975 return ns_load_image (f
, img
,
6976 image_spec_value (img
->spec
, QCfile
, NULL
),
6977 image_spec_value (img
->spec
, QCdata
, NULL
));
6979 #endif /* HAVE_NS */
6981 #endif /* !HAVE_TIFF */
6985 /***********************************************************************
6987 ***********************************************************************/
6989 #if defined (HAVE_GIF) || defined (HAVE_NS)
6991 static int gif_image_p (Lisp_Object object
);
6992 static int gif_load (struct frame
*f
, struct image
*img
);
6993 static void gif_clear_image (struct frame
*f
, struct image
*img
);
6995 /* The symbol `gif' identifying images of this type. */
6997 static Lisp_Object Qgif
;
6999 /* Indices of image specification fields in gif_format, below. */
7001 enum gif_keyword_index
7017 /* Vector of image_keyword structures describing the format
7018 of valid user-defined image specifications. */
7020 static const struct image_keyword gif_format
[GIF_LAST
] =
7022 {":type", IMAGE_SYMBOL_VALUE
, 1},
7023 {":data", IMAGE_STRING_VALUE
, 0},
7024 {":file", IMAGE_STRING_VALUE
, 0},
7025 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7026 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7027 {":relief", IMAGE_INTEGER_VALUE
, 0},
7028 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7029 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7030 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7031 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7032 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7035 /* Structure describing the image type `gif'. */
7037 static struct image_type gif_type
=
7046 /* Free X resources of GIF image IMG which is used on frame F. */
7049 gif_clear_image (struct frame
*f
, struct image
*img
)
7051 img
->lisp_data
= Qnil
;
7052 x_clear_image (f
, img
);
7055 /* Return non-zero if OBJECT is a valid GIF image specification. */
7058 gif_image_p (Lisp_Object object
)
7060 struct image_keyword fmt
[GIF_LAST
];
7061 memcpy (fmt
, gif_format
, sizeof fmt
);
7063 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7066 /* Must specify either the :data or :file keyword. */
7067 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7070 #endif /* HAVE_GIF */
7074 #if defined (HAVE_NTGUI)
7075 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7076 Undefine before redefining to avoid a preprocessor warning. */
7080 /* avoid conflict with QuickdrawText.h */
7081 #define DrawText gif_DrawText
7082 #include <gif_lib.h>
7085 #else /* HAVE_NTGUI */
7087 #include <gif_lib.h>
7089 #endif /* HAVE_NTGUI */
7094 /* GIF library details. */
7095 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7096 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7097 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7098 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7101 init_gif_functions (Lisp_Object libraries
)
7105 if (!(library
= w32_delayed_load (libraries
, Qgif
)))
7108 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7109 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7110 LOAD_IMGLIB_FN (library
, DGifOpen
);
7111 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7117 #define fn_DGifCloseFile DGifCloseFile
7118 #define fn_DGifSlurp DGifSlurp
7119 #define fn_DGifOpen DGifOpen
7120 #define fn_DGifOpenFileName DGifOpenFileName
7122 #endif /* HAVE_NTGUI */
7124 /* Reading a GIF image from memory
7125 Based on the PNG memory stuff to a certain extent. */
7129 unsigned char *bytes
;
7135 /* Make the current memory source available to gif_read_from_memory.
7136 It's done this way because not all versions of libungif support
7137 a UserData field in the GifFileType structure. */
7138 static gif_memory_source
*current_gif_memory_src
;
7141 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7143 gif_memory_source
*src
= current_gif_memory_src
;
7145 if (len
> src
->len
- src
->index
)
7148 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7154 /* Load GIF image IMG for use on frame F. Value is non-zero if
7157 static const int interlace_start
[] = {0, 4, 2, 1};
7158 static const int interlace_increment
[] = {8, 8, 4, 2};
7160 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7163 gif_load (struct frame
*f
, struct image
*img
)
7166 int rc
, width
, height
, x
, y
, i
, j
;
7168 ColorMapObject
*gif_color_map
;
7169 unsigned long pixel_colors
[256];
7171 int image_height
, image_width
;
7172 gif_memory_source memsrc
;
7173 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7174 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7175 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7176 unsigned long bgcolor
= 0;
7179 if (NILP (specified_data
))
7181 file
= x_find_image_file (specified_file
);
7182 if (!STRINGP (file
))
7184 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7188 /* Open the GIF file. */
7189 gif
= fn_DGifOpenFileName (SSDATA (file
));
7192 image_error ("Cannot open `%s'", file
, Qnil
);
7198 if (!STRINGP (specified_data
))
7200 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7204 /* Read from memory! */
7205 current_gif_memory_src
= &memsrc
;
7206 memsrc
.bytes
= SDATA (specified_data
);
7207 memsrc
.len
= SBYTES (specified_data
);
7210 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7213 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7218 /* Before reading entire contents, check the declared image size. */
7219 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7221 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7222 fn_DGifCloseFile (gif
);
7226 /* Read entire contents. */
7227 rc
= fn_DGifSlurp (gif
);
7228 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7230 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7231 fn_DGifCloseFile (gif
);
7235 /* Which sub-image are we to display? */
7237 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7238 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7239 if (idx
< 0 || idx
>= gif
->ImageCount
)
7241 image_error ("Invalid image number `%s' in image `%s'",
7242 image_number
, img
->spec
);
7243 fn_DGifCloseFile (gif
);
7248 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[idx
].ImageDesc
.Top
;
7249 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[idx
].ImageDesc
.Left
;
7250 image_height
= gif
->SavedImages
[idx
].ImageDesc
.Height
;
7251 img
->corners
[BOT_CORNER
] = img
->corners
[TOP_CORNER
] + image_height
;
7252 image_width
= gif
->SavedImages
[idx
].ImageDesc
.Width
;
7253 img
->corners
[RIGHT_CORNER
] = img
->corners
[LEFT_CORNER
] + image_width
;
7255 width
= img
->width
= max (gif
->SWidth
,
7256 max (gif
->Image
.Left
+ gif
->Image
.Width
,
7257 img
->corners
[RIGHT_CORNER
]));
7258 height
= img
->height
= max (gif
->SHeight
,
7259 max (gif
->Image
.Top
+ gif
->Image
.Height
,
7260 img
->corners
[BOT_CORNER
]));
7262 if (!check_image_size (f
, width
, height
))
7264 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7265 fn_DGifCloseFile (gif
);
7269 /* Create the X image and pixmap. */
7270 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
7272 fn_DGifCloseFile (gif
);
7276 /* Clear the part of the screen image not covered by the image.
7277 Full animated GIF support requires more here (see the gif89 spec,
7278 disposal methods). Let's simply assume that the part not covered
7279 by a sub-image is in the frame's background color. */
7280 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7281 for (x
= 0; x
< width
; ++x
)
7282 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7284 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7285 for (x
= 0; x
< width
; ++x
)
7286 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7288 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7290 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7291 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7292 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7293 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7296 /* Read the GIF image into the X image. */
7298 /* FIXME: With the current implementation, loading an animated gif
7299 is quadratic in the number of animation frames, since each frame
7300 is a separate struct image. We must provide a way for a single
7301 gif_load call to construct and save all animation frames. */
7303 init_color_table ();
7304 if (STRINGP (specified_bg
))
7305 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7306 FRAME_BACKGROUND_PIXEL (f
));
7307 for (j
= 0; j
<= idx
; ++j
)
7309 /* We use a local variable `raster' here because RasterBits is a
7310 char *, which invites problems with bytes >= 0x80. */
7311 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7312 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7313 int transparency_color_index
= -1;
7316 /* Find the Graphic Control Extension block for this sub-image.
7317 Extract the disposal method and transparency color. */
7318 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7320 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7322 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7323 && extblock
->ByteCount
== 4
7324 && extblock
->Bytes
[0] & 1)
7326 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7327 to background". Treat any other value like 2. */
7328 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7329 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7334 /* We can't "keep in place" the first subimage. */
7338 /* Allocate subimage colors. */
7339 memset (pixel_colors
, 0, sizeof pixel_colors
);
7340 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7342 gif_color_map
= gif
->SColorMap
;
7345 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7347 if (transparency_color_index
== i
)
7348 pixel_colors
[i
] = STRINGP (specified_bg
)
7349 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7352 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7353 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7354 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7355 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7359 /* Apply the pixel values. */
7360 if (gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7364 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7366 y
++, row
+= interlace_increment
[pass
])
7368 if (row
>= image_height
)
7370 row
= interlace_start
[++pass
];
7371 while (row
>= image_height
)
7372 row
= interlace_start
[++pass
];
7375 for (x
= 0; x
< image_width
; x
++)
7377 int c
= raster
[y
* image_width
+ x
];
7378 if (transparency_color_index
!= c
|| disposal
!= 1)
7379 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7380 row
+ img
->corners
[TOP_CORNER
], pixel_colors
[c
]);
7386 for (y
= 0; y
< image_height
; ++y
)
7387 for (x
= 0; x
< image_width
; ++x
)
7389 int c
= raster
[y
* image_width
+ x
];
7390 if (transparency_color_index
!= c
|| disposal
!= 1)
7391 XPutPixel (ximg
, x
+ img
->corners
[LEFT_CORNER
],
7392 y
+ img
->corners
[TOP_CORNER
], pixel_colors
[c
]);
7397 #ifdef COLOR_TABLE_SUPPORT
7398 img
->colors
= colors_in_color_table (&img
->ncolors
);
7399 free_color_table ();
7400 #endif /* COLOR_TABLE_SUPPORT */
7402 /* Save GIF image extension data for `image-metadata'.
7403 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7404 img
->lisp_data
= Qnil
;
7405 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7408 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7409 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7410 /* Append (... FUNCTION "BYTES") */
7413 = Fcons (make_number (ext
->Function
),
7414 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7416 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7417 && ext
->ByteCount
== 4)
7419 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7420 delay
|= ext
->Bytes
[1];
7423 img
->lisp_data
= Fcons (Qextension_data
,
7424 Fcons (img
->lisp_data
, Qnil
));
7428 Fcons (make_float (delay
/ 100.0),
7432 if (gif
->ImageCount
> 1)
7433 img
->lisp_data
= Fcons (Qcount
,
7434 Fcons (make_number (gif
->ImageCount
),
7437 fn_DGifCloseFile (gif
);
7439 /* Maybe fill in the background field while we have ximg handy. */
7440 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7441 /* Casting avoids a GCC warning. */
7442 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7444 /* Put the image into the pixmap, then free the X image and its buffer. */
7445 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7446 x_destroy_x_image (ximg
);
7451 #else /* !HAVE_GIF */
7455 gif_load (struct frame
*f
, struct image
*img
)
7457 return ns_load_image (f
, img
,
7458 image_spec_value (img
->spec
, QCfile
, NULL
),
7459 image_spec_value (img
->spec
, QCdata
, NULL
));
7461 #endif /* HAVE_NS */
7463 #endif /* HAVE_GIF */
7466 /***********************************************************************
7468 ***********************************************************************/
7469 #if defined (HAVE_IMAGEMAGICK)
7471 static Lisp_Object Qimagemagick
;
7473 static int imagemagick_image_p (Lisp_Object
);
7474 static int imagemagick_load (struct frame
*, struct image
*);
7475 static void imagemagick_clear_image (struct frame
*, struct image
*);
7477 /* Indices of image specification fields in imagemagick_format. */
7479 enum imagemagick_keyword_index
7487 IMAGEMAGICK_ALGORITHM
,
7488 IMAGEMAGICK_HEURISTIC_MASK
,
7490 IMAGEMAGICK_BACKGROUND
,
7493 IMAGEMAGICK_ROTATION
,
7498 /* Vector of image_keyword structures describing the format
7499 of valid user-defined image specifications. */
7501 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7503 {":type", IMAGE_SYMBOL_VALUE
, 1},
7504 {":data", IMAGE_STRING_VALUE
, 0},
7505 {":file", IMAGE_STRING_VALUE
, 0},
7506 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7507 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7508 {":relief", IMAGE_INTEGER_VALUE
, 0},
7509 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7510 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7511 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7512 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7513 {":height", IMAGE_INTEGER_VALUE
, 0},
7514 {":width", IMAGE_INTEGER_VALUE
, 0},
7515 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7516 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7519 /* Structure describing the image type for any image handled via
7522 static struct image_type imagemagick_type
=
7525 imagemagick_image_p
,
7527 imagemagick_clear_image
,
7531 /* Free X resources of imagemagick image IMG which is used on frame F. */
7534 imagemagick_clear_image (struct frame
*f
,
7537 x_clear_image (f
, img
);
7540 /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do
7541 this by calling parse_image_spec and supplying the keywords that
7542 identify the IMAGEMAGICK format. */
7545 imagemagick_image_p (Lisp_Object object
)
7547 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7548 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7550 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7553 /* Must specify either the :data or :file keyword. */
7554 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7557 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7558 Therefore rename the function so it doesnt collide with ImageMagick. */
7559 #define DrawRectangle DrawRectangleGif
7560 #include <wand/MagickWand.h>
7562 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7563 Emacs seems to work fine with the hidden version, so unhide it. */
7564 #include <magick/version.h>
7565 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7566 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7567 MagickPixelPacket
*);
7570 /* Helper function for imagemagick_load, which does the actual loading
7571 given contents and size, apart from frame and image structures,
7572 passed from imagemagick_load. Uses librimagemagick to do most of
7573 the image processing.
7575 F is a pointer to the Emacs frame; IMG to the image structure to
7576 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
7577 be parsed; SIZE is the number of bytes of data; and FILENAME is
7578 either the file name or the image data.
7580 Return non-zero if successful. */
7583 imagemagick_load_image (struct frame
*f
, struct image
*img
,
7584 unsigned char *contents
, unsigned int size
,
7590 MagickBooleanType status
;
7596 MagickWand
*image_wand
;
7597 MagickWand
*ping_wand
;
7598 PixelIterator
*iterator
;
7600 MagickPixelPacket pixel
;
7605 int desired_width
, desired_height
;
7607 EMACS_INT imagemagick_rendermethod
;
7609 ImageInfo
*image_info
;
7610 ExceptionInfo
*exception
;
7614 /* Handle image index for image types who can contain more than one image.
7615 Interface :index is same as for GIF. First we "ping" the image to see how
7616 many sub-images it contains. Pinging is faster than loading the image to
7617 find out things about it. */
7619 /* Initialize the imagemagick environment. */
7620 MagickWandGenesis ();
7621 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7622 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7623 ping_wand
= NewMagickWand ();
7624 MagickSetResolution (ping_wand
, 2, 2);
7625 if (filename
!= NULL
)
7627 status
= MagickPingImage (ping_wand
, filename
);
7631 status
= MagickPingImageBlob (ping_wand
, contents
, size
);
7634 if (! (0 <= ino
&& ino
< MagickGetNumberImages (ping_wand
)))
7636 image_error ("Invalid image number `%s' in image `%s'",
7638 DestroyMagickWand (ping_wand
);
7642 if (MagickGetNumberImages(ping_wand
) > 1)
7645 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7648 DestroyMagickWand (ping_wand
);
7650 /* Now we know how many images are inside the file. If it's not a
7651 bundle, the number is one. */
7653 if (filename
!= NULL
)
7655 image_info
= CloneImageInfo ((ImageInfo
*) NULL
);
7656 (void) strcpy (image_info
->filename
, filename
);
7657 image_info
->number_scenes
= 1;
7658 image_info
->scene
= ino
;
7659 exception
= AcquireExceptionInfo ();
7661 im_image
= ReadImage (image_info
, exception
);
7662 DestroyExceptionInfo (exception
);
7664 if (im_image
== NULL
)
7665 goto imagemagick_no_wand
;
7666 image_wand
= NewMagickWandFromImage (im_image
);
7667 DestroyImage(im_image
);
7671 image_wand
= NewMagickWand ();
7672 if (MagickReadImageBlob (image_wand
, contents
, size
) == MagickFalse
)
7673 goto imagemagick_error
;
7676 /* If width and/or height is set in the display spec assume we want
7677 to scale to those values. If either h or w is unspecified, the
7678 unspecified should be calculated from the specified to preserve
7681 value
= image_spec_value (img
->spec
, QCwidth
, NULL
);
7682 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7683 value
= image_spec_value (img
->spec
, QCheight
, NULL
);
7684 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7686 height
= MagickGetImageHeight (image_wand
);
7687 width
= MagickGetImageWidth (image_wand
);
7689 if (desired_width
!= -1 && desired_height
== -1)
7690 /* w known, calculate h. */
7691 desired_height
= (double) desired_width
/ width
* height
;
7692 if (desired_width
== -1 && desired_height
!= -1)
7693 /* h known, calculate w. */
7694 desired_width
= (double) desired_height
/ height
* width
;
7695 if (desired_width
!= -1 && desired_height
!= -1)
7697 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7698 if (status
== MagickFalse
)
7700 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7701 goto imagemagick_error
;
7705 /* crop behaves similar to image slicing in Emacs but is more memory
7707 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7709 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7711 /* After some testing, it seems MagickCropImage is the fastest crop
7712 function in ImageMagick. This crop function seems to do less copying
7713 than the alternatives, but it still reads the entire image into memory
7714 before cropping, which is apparently difficult to avoid when using
7716 size_t crop_width
= XINT (XCAR (crop
));
7718 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7720 size_t crop_height
= XINT (XCAR (crop
));
7722 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7724 ssize_t crop_x
= XINT (XCAR (crop
));
7726 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7728 ssize_t crop_y
= XINT (XCAR (crop
));
7729 MagickCropImage (image_wand
, crop_width
, crop_height
,
7736 /* Furthermore :rotation. we need background color and angle for
7739 TODO background handling for rotation specified_bg =
7740 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7742 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7745 PixelWand
* background
= NewPixelWand ();
7746 PixelSetColor (background
, "#ffffff");/*TODO remove hardcode*/
7748 rotation
= extract_float (value
);
7750 status
= MagickRotateImage (image_wand
, background
, rotation
);
7751 DestroyPixelWand (background
);
7752 if (status
== MagickFalse
)
7754 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
7755 goto imagemagick_error
;
7759 /* Finally we are done manipulating the image. Figure out the
7760 resulting width/height and transfer ownerwship to Emacs. */
7761 height
= MagickGetImageHeight (image_wand
);
7762 width
= MagickGetImageWidth (image_wand
);
7764 if (! (width
<= INT_MAX
&& height
<= INT_MAX
7765 && check_image_size (f
, width
, height
)))
7767 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7768 goto imagemagick_error
;
7771 /* We can now get a valid pixel buffer from the imagemagick file, if all
7774 init_color_table ();
7775 imagemagick_rendermethod
= (INTEGERP (Vimagemagick_render_type
)
7776 ? XINT (Vimagemagick_render_type
) : 0);
7777 if (imagemagick_rendermethod
== 0)
7779 size_t image_height
;
7781 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7782 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7783 &ximg
, &img
->pixmap
))
7785 #ifdef COLOR_TABLE_SUPPORT
7786 free_color_table ();
7788 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7789 goto imagemagick_error
;
7792 /* Copy imagegmagick image to x with primitive yet robust pixel
7793 pusher loop. This has been tested a lot with many different
7796 /* Copy pixels from the imagemagick image structure to the x image map. */
7797 iterator
= NewPixelIterator (image_wand
);
7798 if (iterator
== (PixelIterator
*) NULL
)
7800 #ifdef COLOR_TABLE_SUPPORT
7801 free_color_table ();
7803 x_destroy_x_image (ximg
);
7804 image_error ("Imagemagick pixel iterator creation failed",
7806 goto imagemagick_error
;
7809 image_height
= MagickGetImageHeight (image_wand
);
7810 for (y
= 0; y
< image_height
; y
++)
7812 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
7813 if (pixels
== (PixelWand
**) NULL
)
7815 for (x
= 0; x
< (long) width
; x
++)
7817 PixelGetMagickColor (pixels
[x
], &pixel
);
7818 XPutPixel (ximg
, x
, y
,
7819 lookup_rgb_color (f
,
7825 DestroyPixelIterator (iterator
);
7828 if (imagemagick_rendermethod
== 1)
7830 /* Magicexportimage is normaly faster than pixelpushing. This
7831 method is also well tested. Some aspects of this method are
7832 ad-hoc and needs to be more researched. */
7833 int imagedepth
= 24;/*MagickGetImageDepth(image_wand);*/
7834 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP";/*"RGBP";*/
7835 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7836 if (!x_create_x_image_and_pixmap (f
, width
, height
, imagedepth
,
7837 &ximg
, &img
->pixmap
))
7839 #ifdef COLOR_TABLE_SUPPORT
7840 free_color_table ();
7842 image_error("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
7843 goto imagemagick_error
;
7847 /* Oddly, the below code doesnt seem to work:*/
7848 /* switch(ximg->bitmap_unit){ */
7850 /* pixelwidth=CharPixel; */
7853 /* pixelwidth=ShortPixel; */
7856 /* pixelwidth=LongPixel; */
7860 Here im just guessing the format of the bitmap.
7861 happens to work fine for:
7864 seems about 3 times as fast as pixel pushing(not carefully measured)
7866 pixelwidth
= CharPixel
;/*??? TODO figure out*/
7867 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
7868 MagickExportImagePixels (image_wand
,
7873 /*&(img->pixmap));*/
7876 image_error ("You dont have MagickExportImagePixels, upgrade ImageMagick!",
7882 #ifdef COLOR_TABLE_SUPPORT
7883 /* Remember colors allocated for this image. */
7884 img
->colors
= colors_in_color_table (&img
->ncolors
);
7885 free_color_table ();
7886 #endif /* COLOR_TABLE_SUPPORT */
7890 img
->height
= height
;
7892 /* Put the image into the pixmap, then free the X image and its
7894 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7895 x_destroy_x_image (ximg
);
7898 /* Final cleanup. image_wand should be the only resource left. */
7899 DestroyMagickWand (image_wand
);
7900 /* `MagickWandTerminus' terminates the imagemagick environment. */
7901 MagickWandTerminus ();
7906 DestroyMagickWand (image_wand
);
7907 imagemagick_no_wand
:
7908 MagickWandTerminus ();
7909 /* TODO more cleanup. */
7910 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
7915 /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if
7916 successful. this function will go into the imagemagick_type structure, and
7917 the prototype thus needs to be compatible with that structure. */
7920 imagemagick_load (struct frame
*f
, struct image
*img
)
7923 Lisp_Object file_name
;
7925 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7926 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
7927 if (STRINGP (file_name
))
7931 file
= x_find_image_file (file_name
);
7932 if (!STRINGP (file
))
7934 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
7937 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
7939 /* Else its not a file, its a lisp object. Load the image from a
7940 lisp object rather than a file. */
7945 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7946 if (!STRINGP (data
))
7948 image_error ("Invalid image data `%s'", data
, Qnil
);
7951 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
7952 SBYTES (data
), NULL
);
7958 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
7959 doc
: /* Return a list of image types supported by ImageMagick.
7960 Each entry in this list is a symbol named after an ImageMagick format
7961 tag. See the ImageMagick manual for a list of ImageMagick formats and
7962 their descriptions (http://www.imagemagick.org/script/formats.php).
7964 Note that ImageMagick recognizes many file-types that Emacs does not
7965 recognize as images, such as C. See `imagemagick-types-inhibit'. */)
7968 Lisp_Object typelist
= Qnil
;
7971 char **imtypes
= GetMagickList ("*", &numf
, &ex
);
7973 Lisp_Object Qimagemagicktype
;
7974 for (i
= 0; i
< numf
; i
++)
7976 Qimagemagicktype
= intern (imtypes
[i
]);
7977 typelist
= Fcons (Qimagemagicktype
, typelist
);
7982 #endif /* defined (HAVE_IMAGEMAGICK) */
7986 /***********************************************************************
7988 ***********************************************************************/
7990 #if defined (HAVE_RSVG)
7992 /* Function prototypes. */
7994 static int svg_image_p (Lisp_Object object
);
7995 static int svg_load (struct frame
*f
, struct image
*img
);
7997 static int svg_load_image (struct frame
*, struct image
*,
7998 unsigned char *, ptrdiff_t);
8000 /* The symbol `svg' identifying images of this type. */
8002 static Lisp_Object Qsvg
;
8004 /* Indices of image specification fields in svg_format, below. */
8006 enum svg_keyword_index
8021 /* Vector of image_keyword structures describing the format
8022 of valid user-defined image specifications. */
8024 static const struct image_keyword svg_format
[SVG_LAST
] =
8026 {":type", IMAGE_SYMBOL_VALUE
, 1},
8027 {":data", IMAGE_STRING_VALUE
, 0},
8028 {":file", IMAGE_STRING_VALUE
, 0},
8029 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8030 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
8031 {":relief", IMAGE_INTEGER_VALUE
, 0},
8032 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8033 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8034 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8035 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8038 /* Structure describing the image type `svg'. Its the same type of
8039 structure defined for all image formats, handled by emacs image
8040 functions. See struct image_type in dispextern.h. */
8042 static struct image_type svg_type
=
8044 /* An identifier showing that this is an image structure for the SVG format. */
8046 /* Handle to a function that can be used to identify a SVG file. */
8048 /* Handle to function used to load a SVG file. */
8050 /* Handle to function to free sresources for SVG. */
8052 /* An internal field to link to the next image type in a list of
8053 image types, will be filled in when registering the format. */
8058 /* Return non-zero if OBJECT is a valid SVG image specification. Do
8059 this by calling parse_image_spec and supplying the keywords that
8060 identify the SVG format. */
8063 svg_image_p (Lisp_Object object
)
8065 struct image_keyword fmt
[SVG_LAST
];
8066 memcpy (fmt
, svg_format
, sizeof fmt
);
8068 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8071 /* Must specify either the :data or :file keyword. */
8072 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8075 #include <librsvg/rsvg.h>
8079 /* SVG library functions. */
8080 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
);
8081 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
);
8082 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
);
8083 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
);
8084 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
);
8086 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
);
8087 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
);
8088 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
);
8089 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
);
8090 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
);
8091 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
);
8092 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
);
8093 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
);
8095 DEF_IMGLIB_FN (void, g_type_init
);
8096 DEF_IMGLIB_FN (void, g_object_unref
);
8097 DEF_IMGLIB_FN (void, g_error_free
);
8099 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8102 init_svg_functions (Lisp_Object libraries
)
8104 HMODULE library
, gdklib
, glib
, gobject
;
8106 if (!(glib
= w32_delayed_load (libraries
, Qglib
))
8107 || !(gobject
= w32_delayed_load (libraries
, Qgobject
))
8108 || !(gdklib
= w32_delayed_load (libraries
, Qgdk_pixbuf
))
8109 || !(library
= w32_delayed_load (libraries
, Qsvg
)))
8112 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8113 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8114 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8115 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8116 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8118 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8119 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8120 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8121 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8122 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8123 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8124 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8125 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8127 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8128 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8129 LOAD_IMGLIB_FN (glib
, g_error_free
);
8135 /* The following aliases for library functions allow dynamic loading
8136 to be used on some platforms. */
8137 #define fn_rsvg_handle_new rsvg_handle_new
8138 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8139 #define fn_rsvg_handle_write rsvg_handle_write
8140 #define fn_rsvg_handle_close rsvg_handle_close
8141 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8143 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8144 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8145 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8146 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8147 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8148 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8149 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8150 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8152 #define fn_g_type_init g_type_init
8153 #define fn_g_object_unref g_object_unref
8154 #define fn_g_error_free g_error_free
8155 #endif /* !HAVE_NTGUI */
8157 /* Load SVG image IMG for use on frame F. Value is non-zero if
8158 successful. this function will go into the svg_type structure, and
8159 the prototype thus needs to be compatible with that structure. */
8162 svg_load (struct frame
*f
, struct image
*img
)
8165 Lisp_Object file_name
;
8167 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8168 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8169 if (STRINGP (file_name
))
8172 unsigned char *contents
;
8175 file
= x_find_image_file (file_name
);
8176 if (!STRINGP (file
))
8178 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8182 /* Read the entire file into memory. */
8183 contents
= slurp_file (SSDATA (file
), &size
);
8184 if (contents
== NULL
)
8186 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8189 /* If the file was slurped into memory properly, parse it. */
8190 success_p
= svg_load_image (f
, img
, contents
, size
);
8193 /* Else its not a file, its a lisp object. Load the image from a
8194 lisp object rather than a file. */
8199 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8200 if (!STRINGP (data
))
8202 image_error ("Invalid image data `%s'", data
, Qnil
);
8205 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8211 /* svg_load_image is a helper function for svg_load, which does the
8212 actual loading given contents and size, apart from frame and image
8213 structures, passed from svg_load.
8215 Uses librsvg to do most of the image processing.
8217 Returns non-zero when successful. */
8219 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8220 struct image
*img
, /* Pointer to emacs image structure. */
8221 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8222 ptrdiff_t size
) /* Size of data in bytes. */
8224 RsvgHandle
*rsvg_handle
;
8225 RsvgDimensionData dimension_data
;
8230 const guint8
*pixels
;
8233 Lisp_Object specified_bg
;
8238 /* g_type_init is a glib function that must be called prior to using
8239 gnome type library functions. */
8241 /* Make a handle to a new rsvg object. */
8242 rsvg_handle
= fn_rsvg_handle_new ();
8244 /* Parse the contents argument and fill in the rsvg_handle. */
8245 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8246 if (err
) goto rsvg_error
;
8248 /* The parsing is complete, rsvg_handle is ready to used, close it
8249 for further writes. */
8250 fn_rsvg_handle_close (rsvg_handle
, &err
);
8251 if (err
) goto rsvg_error
;
8253 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8254 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8256 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8260 /* We can now get a valid pixel buffer from the svg file, if all
8262 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8263 if (!pixbuf
) goto rsvg_error
;
8264 fn_g_object_unref (rsvg_handle
);
8266 /* Extract some meta data from the svg handle. */
8267 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8268 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8269 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8270 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8272 /* Validate the svg meta data. */
8273 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8274 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8275 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8276 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8278 /* Try to create a x pixmap to hold the svg pixmap. */
8279 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8281 fn_g_object_unref (pixbuf
);
8285 init_color_table ();
8287 /* Handle alpha channel by combining the image with a background
8289 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8290 if (!STRINGP (specified_bg
)
8291 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8294 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8295 x_query_color (f
, &background
);
8297 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8301 /* SVG pixmaps specify transparency in the last byte, so right
8302 shift 8 bits to get rid of it, since emacs doesn't support
8304 background
.red
>>= 8;
8305 background
.green
>>= 8;
8306 background
.blue
>>= 8;
8308 /* This loop handles opacity values, since Emacs assumes
8309 non-transparent images. Each pixel must be "flattened" by
8310 calculating the resulting color, given the transparency of the
8311 pixel, and the image background color. */
8312 for (y
= 0; y
< height
; ++y
)
8314 for (x
= 0; x
< width
; ++x
)
8324 opacity
= *pixels
++;
8326 red
= ((red
* opacity
)
8327 + (background
.red
* ((1 << 8) - opacity
)));
8328 green
= ((green
* opacity
)
8329 + (background
.green
* ((1 << 8) - opacity
)));
8330 blue
= ((blue
* opacity
)
8331 + (background
.blue
* ((1 << 8) - opacity
)));
8333 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8336 pixels
+= rowstride
- 4 * width
;
8339 #ifdef COLOR_TABLE_SUPPORT
8340 /* Remember colors allocated for this image. */
8341 img
->colors
= colors_in_color_table (&img
->ncolors
);
8342 free_color_table ();
8343 #endif /* COLOR_TABLE_SUPPORT */
8345 fn_g_object_unref (pixbuf
);
8348 img
->height
= height
;
8350 /* Maybe fill in the background field while we have ximg handy.
8351 Casting avoids a GCC warning. */
8352 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8354 /* Put the image into the pixmap, then free the X image and its
8356 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8357 x_destroy_x_image (ximg
);
8362 fn_g_object_unref (rsvg_handle
);
8363 /* FIXME: Use error->message so the user knows what is the actual
8364 problem with the image. */
8365 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8366 fn_g_error_free (err
);
8370 #endif /* defined (HAVE_RSVG) */
8375 /***********************************************************************
8377 ***********************************************************************/
8379 #ifdef HAVE_X_WINDOWS
8380 #define HAVE_GHOSTSCRIPT 1
8381 #endif /* HAVE_X_WINDOWS */
8383 #ifdef HAVE_GHOSTSCRIPT
8385 static int gs_image_p (Lisp_Object object
);
8386 static int gs_load (struct frame
*f
, struct image
*img
);
8387 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8389 /* Keyword symbols. */
8391 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8393 /* Indices of image specification fields in gs_format, below. */
8395 enum gs_keyword_index
8413 /* Vector of image_keyword structures describing the format
8414 of valid user-defined image specifications. */
8416 static const struct image_keyword gs_format
[GS_LAST
] =
8418 {":type", IMAGE_SYMBOL_VALUE
, 1},
8419 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8420 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8421 {":file", IMAGE_STRING_VALUE
, 1},
8422 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8423 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8424 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8425 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
8426 {":relief", IMAGE_INTEGER_VALUE
, 0},
8427 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8428 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8429 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8430 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8433 /* Structure describing the image type `ghostscript'. */
8435 static struct image_type gs_type
=
8445 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8448 gs_clear_image (struct frame
*f
, struct image
*img
)
8450 x_clear_image (f
, img
);
8454 /* Return non-zero if OBJECT is a valid Ghostscript image
8458 gs_image_p (Lisp_Object object
)
8460 struct image_keyword fmt
[GS_LAST
];
8464 memcpy (fmt
, gs_format
, sizeof fmt
);
8466 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8469 /* Bounding box must be a list or vector containing 4 integers. */
8470 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8473 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8474 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8479 else if (VECTORP (tem
))
8481 if (ASIZE (tem
) != 4)
8483 for (i
= 0; i
< 4; ++i
)
8484 if (!INTEGERP (XVECTOR (tem
)->contents
[i
]))
8494 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8498 gs_load (struct frame
*f
, struct image
*img
)
8500 uprintmax_t printnum1
, printnum2
;
8501 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
8502 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8504 double in_width
, in_height
;
8505 Lisp_Object pixel_colors
= Qnil
;
8507 /* Compute pixel size of pixmap needed from the given size in the
8508 image specification. Sizes in the specification are in pt. 1 pt
8509 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8511 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8512 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8513 in_width
*= FRAME_X_DISPLAY_INFO (f
)->resx
;
8514 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8515 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8516 in_height
*= FRAME_X_DISPLAY_INFO (f
)->resy
;
8518 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
8519 && check_image_size (f
, in_width
, in_height
)))
8521 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8524 img
->width
= in_width
;
8525 img
->height
= in_height
;
8527 /* Create the pixmap. */
8528 xassert (img
->pixmap
== NO_PIXMAP
);
8530 if (x_check_image_size (0, img
->width
, img
->height
))
8532 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8534 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8535 img
->width
, img
->height
,
8536 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8542 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8546 /* Call the loader to fill the pixmap. It returns a process object
8547 if successful. We do not record_unwind_protect here because
8548 other places in redisplay like calling window scroll functions
8549 don't either. Let the Lisp loader use `unwind-protect' instead. */
8550 printnum1
= FRAME_X_WINDOW (f
);
8551 printnum2
= img
->pixmap
;
8552 sprintf (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8553 window_and_pixmap_id
= build_string (buffer
);
8555 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
8556 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
8557 sprintf (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8558 pixel_colors
= build_string (buffer
);
8560 XSETFRAME (frame
, f
);
8561 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8563 loader
= intern ("gs-load-image");
8565 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
8566 make_number (img
->width
),
8567 make_number (img
->height
),
8568 window_and_pixmap_id
,
8570 return PROCESSP (img
->lisp_data
);
8574 /* Kill the Ghostscript process that was started to fill PIXMAP on
8575 frame F. Called from XTread_socket when receiving an event
8576 telling Emacs that Ghostscript has finished drawing. */
8579 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8581 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8586 /* Find the image containing PIXMAP. */
8587 for (i
= 0; i
< c
->used
; ++i
)
8588 if (c
->images
[i
]->pixmap
== pixmap
)
8591 /* Should someone in between have cleared the image cache, for
8592 instance, give up. */
8596 /* Kill the GS process. We should have found PIXMAP in the image
8597 cache and its image should contain a process object. */
8599 xassert (PROCESSP (img
->lisp_data
));
8600 Fkill_process (img
->lisp_data
, Qnil
);
8601 img
->lisp_data
= Qnil
;
8603 #if defined (HAVE_X_WINDOWS)
8605 /* On displays with a mutable colormap, figure out the colors
8606 allocated for the image by looking at the pixels of an XImage for
8608 class = FRAME_X_VISUAL (f
)->class;
8609 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8615 /* Try to get an XImage for img->pixmep. */
8616 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8617 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8622 /* Initialize the color table. */
8623 init_color_table ();
8625 /* For each pixel of the image, look its color up in the
8626 color table. After having done so, the color table will
8627 contain an entry for each color used by the image. */
8628 for (y
= 0; y
< img
->height
; ++y
)
8629 for (x
= 0; x
< img
->width
; ++x
)
8631 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8632 lookup_pixel_color (f
, pixel
);
8635 /* Record colors in the image. Free color table and XImage. */
8636 #ifdef COLOR_TABLE_SUPPORT
8637 img
->colors
= colors_in_color_table (&img
->ncolors
);
8638 free_color_table ();
8640 XDestroyImage (ximg
);
8642 #if 0 /* This doesn't seem to be the case. If we free the colors
8643 here, we get a BadAccess later in x_clear_image when
8644 freeing the colors. */
8645 /* We have allocated colors once, but Ghostscript has also
8646 allocated colors on behalf of us. So, to get the
8647 reference counts right, free them once. */
8649 x_free_colors (f
, img
->colors
, img
->ncolors
);
8653 image_error ("Cannot get X image of `%s'; colors will not be freed",
8658 #endif /* HAVE_X_WINDOWS */
8660 /* Now that we have the pixmap, compute mask and transform the
8661 image if requested. */
8663 postprocess_image (f
, img
);
8667 #endif /* HAVE_GHOSTSCRIPT */
8670 /***********************************************************************
8672 ***********************************************************************/
8676 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8677 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8680 return valid_image_p (spec
) ? Qt
: Qnil
;
8684 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8689 if (valid_image_p (spec
))
8690 id
= lookup_image (SELECTED_FRAME (), spec
);
8693 return make_number (id
);
8696 #endif /* GLYPH_DEBUG != 0 */
8699 /***********************************************************************
8701 ***********************************************************************/
8704 /* Image types that rely on external libraries are loaded dynamically
8705 if the library is available. */
8706 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8707 define_image_type (image_type, init_lib_fn (libraries))
8709 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8710 define_image_type (image_type, 1)
8711 #endif /* HAVE_NTGUI */
8713 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 2, 2, 0,
8714 doc
: /* Initialize image library implementing image type TYPE.
8715 Return non-nil if TYPE is a supported image type.
8717 Image types pbm and xbm are prebuilt; other types are loaded here.
8718 Libraries to load are specified in alist LIBRARIES (usually, the value
8719 of `dynamic-library-alist', which see). */)
8720 (Lisp_Object type
, Lisp_Object libraries
)
8723 /* Don't try to reload the library. */
8724 Lisp_Object tested
= Fassq (type
, Vlibrary_cache
);
8726 return XCDR (tested
);
8729 /* Types pbm and xbm are built-in and always available. */
8730 if (EQ (type
, Qpbm
) || EQ (type
, Qxbm
))
8733 #if defined (HAVE_XPM) || defined (HAVE_NS)
8734 if (EQ (type
, Qxpm
))
8735 return CHECK_LIB_AVAILABLE (&xpm_type
, init_xpm_functions
, libraries
);
8738 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8739 if (EQ (type
, Qjpeg
))
8740 return CHECK_LIB_AVAILABLE (&jpeg_type
, init_jpeg_functions
, libraries
);
8743 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8744 if (EQ (type
, Qtiff
))
8745 return CHECK_LIB_AVAILABLE (&tiff_type
, init_tiff_functions
, libraries
);
8748 #if defined (HAVE_GIF) || defined (HAVE_NS)
8749 if (EQ (type
, Qgif
))
8750 return CHECK_LIB_AVAILABLE (&gif_type
, init_gif_functions
, libraries
);
8753 #if defined (HAVE_PNG) || defined (HAVE_NS)
8754 if (EQ (type
, Qpng
))
8755 return CHECK_LIB_AVAILABLE (&png_type
, init_png_functions
, libraries
);
8758 #if defined (HAVE_RSVG)
8759 if (EQ (type
, Qsvg
))
8760 return CHECK_LIB_AVAILABLE (&svg_type
, init_svg_functions
, libraries
);
8763 #if defined (HAVE_IMAGEMAGICK)
8764 if (EQ (type
, Qimagemagick
))
8765 return CHECK_LIB_AVAILABLE (&imagemagick_type
, init_imagemagick_functions
,
8769 #ifdef HAVE_GHOSTSCRIPT
8770 if (EQ (type
, Qpostscript
))
8771 return CHECK_LIB_AVAILABLE (&gs_type
, init_gs_functions
, libraries
);
8774 /* If the type is not recognized, avoid testing it ever again. */
8775 CACHE_IMAGE_TYPE (type
, Qnil
);
8780 syms_of_image (void)
8782 /* Initialize this only once, since that's what we do with Vimage_types
8783 and they are supposed to be in sync. Initializing here gives correct
8784 operation on GNU/Linux of calling dump-emacs after loading some images. */
8787 /* Must be defined now becase we're going to update it below, while
8788 defining the supported image types. */
8789 DEFVAR_LISP ("image-types", Vimage_types
,
8790 doc
: /* List of potentially supported image types.
8791 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
8792 To check whether it is really supported, use `image-type-available-p'. */);
8793 Vimage_types
= Qnil
;
8795 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
8796 doc
: /* Maximum size of images.
8797 Emacs will not load an image into memory if its pixel width or
8798 pixel height exceeds this limit.
8800 If the value is an integer, it directly specifies the maximum
8801 image height and width, measured in pixels. If it is a floating
8802 point number, it specifies the maximum image height and width
8803 as a ratio to the frame height and width. If the value is
8804 non-numeric, there is no explicit limit on the size of images. */);
8805 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
8807 DEFSYM (Qpbm
, "pbm");
8808 ADD_IMAGE_TYPE (Qpbm
);
8810 DEFSYM (Qxbm
, "xbm");
8811 ADD_IMAGE_TYPE (Qxbm
);
8813 define_image_type (&xbm_type
, 1);
8814 define_image_type (&pbm_type
, 1);
8816 DEFSYM (Qcount
, "count");
8817 DEFSYM (Qextension_data
, "extension-data");
8818 DEFSYM (Qdelay
, "delay");
8820 DEFSYM (QCascent
, ":ascent");
8821 DEFSYM (QCmargin
, ":margin");
8822 DEFSYM (QCrelief
, ":relief");
8823 DEFSYM (QCconversion
, ":conversion");
8824 DEFSYM (QCcolor_symbols
, ":color-symbols");
8825 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
8826 DEFSYM (QCindex
, ":index");
8827 DEFSYM (QCgeometry
, ":geometry");
8828 DEFSYM (QCcrop
, ":crop");
8829 DEFSYM (QCrotation
, ":rotation");
8830 DEFSYM (QCmatrix
, ":matrix");
8831 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
8832 DEFSYM (QCmask
, ":mask");
8834 DEFSYM (Qlaplace
, "laplace");
8835 DEFSYM (Qemboss
, "emboss");
8836 DEFSYM (Qedge_detection
, "edge-detection");
8837 DEFSYM (Qheuristic
, "heuristic");
8839 DEFSYM (Qpostscript
, "postscript");
8840 #ifdef HAVE_GHOSTSCRIPT
8841 ADD_IMAGE_TYPE (Qpostscript
);
8842 DEFSYM (QCloader
, ":loader");
8843 DEFSYM (QCbounding_box
, ":bounding-box");
8844 DEFSYM (QCpt_width
, ":pt-width");
8845 DEFSYM (QCpt_height
, ":pt-height");
8846 #endif /* HAVE_GHOSTSCRIPT */
8849 DEFSYM (Qlibpng_version
, "libpng-version");
8850 Fset (Qlibpng_version
,
8852 make_number (PNG_LIBPNG_VER
)
8859 #if defined (HAVE_XPM) || defined (HAVE_NS)
8860 DEFSYM (Qxpm
, "xpm");
8861 ADD_IMAGE_TYPE (Qxpm
);
8864 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8865 DEFSYM (Qjpeg
, "jpeg");
8866 ADD_IMAGE_TYPE (Qjpeg
);
8869 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8870 DEFSYM (Qtiff
, "tiff");
8871 ADD_IMAGE_TYPE (Qtiff
);
8874 #if defined (HAVE_GIF) || defined (HAVE_NS)
8875 DEFSYM (Qgif
, "gif");
8876 ADD_IMAGE_TYPE (Qgif
);
8879 #if defined (HAVE_PNG) || defined (HAVE_NS)
8880 DEFSYM (Qpng
, "png");
8881 ADD_IMAGE_TYPE (Qpng
);
8884 #if defined (HAVE_IMAGEMAGICK)
8885 DEFSYM (Qimagemagick
, "imagemagick");
8886 ADD_IMAGE_TYPE (Qimagemagick
);
8889 #if defined (HAVE_RSVG)
8890 DEFSYM (Qsvg
, "svg");
8891 ADD_IMAGE_TYPE (Qsvg
);
8893 /* Other libraries used directly by svg code. */
8894 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
8895 DEFSYM (Qglib
, "glib");
8896 DEFSYM (Qgobject
, "gobject");
8897 #endif /* HAVE_NTGUI */
8898 #endif /* HAVE_RSVG */
8900 defsubr (&Sinit_image_library
);
8901 #ifdef HAVE_IMAGEMAGICK
8902 defsubr (&Simagemagick_types
);
8904 defsubr (&Sclear_image_cache
);
8905 defsubr (&Simage_flush
);
8906 defsubr (&Simage_size
);
8907 defsubr (&Simage_mask_p
);
8908 defsubr (&Simage_metadata
);
8912 defsubr (&Slookup_image
);
8915 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
8916 doc
: /* Non-nil means always draw a cross over disabled images.
8917 Disabled images are those having a `:conversion disabled' property.
8918 A cross is always drawn on black & white displays. */);
8919 cross_disabled_images
= 0;
8921 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
8922 doc
: /* List of directories to search for window system bitmap files. */);
8923 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
8925 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
8926 doc
: /* Maximum time after which images are removed from the cache.
8927 When an image has not been displayed this many seconds, Emacs
8928 automatically removes it from the image cache. If the cache contains
8929 a large number of images, the actual eviction time may be shorter.
8930 The value can also be nil, meaning the cache is never cleared.
8932 The function `clear-image-cache' disregards this variable. */);
8933 Vimage_cache_eviction_delay
= make_number (300);
8934 #ifdef HAVE_IMAGEMAGICK
8935 DEFVAR_LISP ("imagemagick-render-type", Vimagemagick_render_type
,
8936 doc
: /* Choose between ImageMagick render methods. */);