1 /* Functions for image support on window system.
3 Copyright (C) 1989, 1992-2013 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #if defined HAVE_LIBPNG_PNG_H
26 # include <libpng/png.h>
36 /* This makes the fields of a Display accessible, in Xlib header files. */
38 #define XLIB_ILLEGAL_ACCESS
43 #include "dispextern.h"
44 #include "blockinput.h"
47 #include "character.h"
49 #include "termhooks.h"
52 #ifdef HAVE_SYS_STAT_H
54 #endif /* HAVE_SYS_STAT_H */
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif /* HAVE_SYS_TYPES_H */
60 #ifdef HAVE_WINDOW_SYSTEM
62 #endif /* HAVE_WINDOW_SYSTEM */
65 #define COLOR_TABLE_SUPPORT 1
67 typedef struct x_bitmap_record Bitmap_Record
;
68 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
69 #define NO_PIXMAP None
71 #define PIX_MASK_RETAIN 0
72 #define PIX_MASK_DRAW 1
73 #endif /* HAVE_X_WINDOWS */
77 /* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */
82 /* W32_TODO : Color tables on W32. */
83 #undef COLOR_TABLE_SUPPORT
85 typedef struct w32_bitmap_record Bitmap_Record
;
86 #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
89 #define PIX_MASK_RETAIN 0
90 #define PIX_MASK_DRAW 1
92 #define x_defined_color w32_defined_color
93 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
95 /* Version of libpng that we were compiled with, or -1 if no PNG
96 support was compiled in. This is tested by w32-win.el to correctly
97 set up the alist used to search for PNG libraries. */
98 Lisp_Object Qlibpng_version
;
99 #endif /* HAVE_NTGUI */
102 #undef COLOR_TABLE_SUPPORT
104 typedef struct ns_bitmap_record Bitmap_Record
;
106 #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
109 #define PIX_MASK_RETAIN 0
110 #define PIX_MASK_DRAW 1
112 #define x_defined_color(f, name, color_def, alloc) \
113 ns_defined_color (f, name, color_def, alloc, 0)
114 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
118 /* The symbol `postscript' identifying images of this type. */
120 static Lisp_Object Qpostscript
;
122 static void x_disable_image (struct frame
*, struct image
*);
123 static void x_edge_detection (struct frame
*, struct image
*, Lisp_Object
,
126 static void init_color_table (void);
127 static unsigned long lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
);
128 #ifdef COLOR_TABLE_SUPPORT
129 static void free_color_table (void);
130 static unsigned long *colors_in_color_table (int *n
);
133 Lisp_Object QCmax_width
, QCmax_height
;
135 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
136 id, which is just an int that this section returns. Bitmaps are
137 reference counted so they can be shared among frames.
139 Bitmap indices are guaranteed to be > 0, so a negative number can
140 be used to indicate no bitmap.
142 If you use x_create_bitmap_from_data, then you must keep track of
143 the bitmaps yourself. That is, creating a bitmap from the same
144 data more than once will not be caught. */
147 /* Use with images created by ns_image_for_XPM. */
149 XGetPixel (XImagePtr ximage
, int x
, int y
)
151 return ns_get_pixel (ximage
, x
, y
);
154 /* Use with images created by ns_image_for_XPM; alpha set to 1;
155 pixel is assumed to be in RGB form. */
157 XPutPixel (XImagePtr ximage
, int x
, int y
, unsigned long pixel
)
159 ns_put_pixel (ximage
, x
, y
, pixel
);
164 /* Functions to access the contents of a bitmap, given an id. */
167 x_bitmap_height (FRAME_PTR f
, ptrdiff_t id
)
169 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
173 x_bitmap_width (FRAME_PTR f
, ptrdiff_t id
)
175 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
178 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
180 x_bitmap_pixmap (FRAME_PTR f
, ptrdiff_t id
)
182 /* HAVE_NTGUI needs the explicit cast here. */
183 return (ptrdiff_t) FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
187 #ifdef HAVE_X_WINDOWS
189 x_bitmap_mask (FRAME_PTR f
, ptrdiff_t id
)
191 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
195 /* Allocate a new bitmap record. Returns index of new record. */
198 x_allocate_bitmap_record (FRAME_PTR f
)
200 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
203 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
204 return ++dpyinfo
->bitmaps_last
;
206 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
207 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
211 xpalloc (dpyinfo
->bitmaps
, &dpyinfo
->bitmaps_size
,
212 10, -1, sizeof *dpyinfo
->bitmaps
);
213 return ++dpyinfo
->bitmaps_last
;
216 /* Add one reference to the reference count of the bitmap with id ID. */
219 x_reference_bitmap (FRAME_PTR f
, ptrdiff_t id
)
221 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
224 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
227 x_create_bitmap_from_data (struct frame
*f
, char *bits
, unsigned int width
, unsigned int height
)
229 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
232 #ifdef HAVE_X_WINDOWS
234 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
235 bits
, width
, height
);
238 #endif /* HAVE_X_WINDOWS */
242 bitmap
= CreateBitmap (width
, height
,
243 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_planes
,
244 FRAME_X_DISPLAY_INFO (XFRAME (frame
))->n_cbits
,
248 #endif /* HAVE_NTGUI */
251 void *bitmap
= ns_image_from_XBM (bits
, width
, height
);
256 id
= x_allocate_bitmap_record (f
);
259 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
260 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
263 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
264 dpyinfo
->bitmaps
[id
- 1].height
= height
;
265 dpyinfo
->bitmaps
[id
- 1].width
= width
;
266 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
268 #ifdef HAVE_X_WINDOWS
269 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
270 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
271 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
272 #endif /* HAVE_X_WINDOWS */
275 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
276 dpyinfo
->bitmaps
[id
- 1].hinst
= NULL
;
277 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
278 #endif /* HAVE_NTGUI */
283 /* Create bitmap from file FILE for frame F. */
286 x_create_bitmap_from_file (struct frame
*f
, Lisp_Object file
)
288 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
291 return -1; /* W32_TODO : bitmap support */
292 #endif /* HAVE_NTGUI */
296 void *bitmap
= ns_image_from_file (file
);
302 id
= x_allocate_bitmap_record (f
);
303 dpyinfo
->bitmaps
[id
- 1].img
= bitmap
;
304 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
305 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
306 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
307 dpyinfo
->bitmaps
[id
- 1].height
= ns_image_width (bitmap
);
308 dpyinfo
->bitmaps
[id
- 1].width
= ns_image_height (bitmap
);
309 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
313 #ifdef HAVE_X_WINDOWS
314 unsigned int width
, height
;
316 int xhot
, yhot
, result
;
322 /* Look for an existing bitmap with the same name. */
323 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
325 if (dpyinfo
->bitmaps
[id
].refcount
326 && dpyinfo
->bitmaps
[id
].file
327 && !strcmp (dpyinfo
->bitmaps
[id
].file
, SSDATA (file
)))
329 ++dpyinfo
->bitmaps
[id
].refcount
;
334 /* Search bitmap-file-path for the file, if appropriate. */
335 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
340 filename
= SSDATA (found
);
342 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
343 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
344 if (result
!= BitmapSuccess
)
347 id
= x_allocate_bitmap_record (f
);
348 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
349 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
350 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
351 dpyinfo
->bitmaps
[id
- 1].file
= xmalloc (SBYTES (file
) + 1);
352 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
353 dpyinfo
->bitmaps
[id
- 1].height
= height
;
354 dpyinfo
->bitmaps
[id
- 1].width
= width
;
355 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SSDATA (file
));
358 #endif /* HAVE_X_WINDOWS */
364 free_bitmap_record (Display_Info
*dpyinfo
, Bitmap_Record
*bm
)
366 #ifdef HAVE_X_WINDOWS
367 XFreePixmap (dpyinfo
->display
, bm
->pixmap
);
369 XFreePixmap (dpyinfo
->display
, bm
->mask
);
370 #endif /* HAVE_X_WINDOWS */
373 DeleteObject (bm
->pixmap
);
374 #endif /* HAVE_NTGUI */
377 ns_release_object (bm
->img
);
387 /* Remove reference to bitmap with id number ID. */
390 x_destroy_bitmap (FRAME_PTR f
, ptrdiff_t id
)
392 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
396 Bitmap_Record
*bm
= &dpyinfo
->bitmaps
[id
- 1];
398 if (--bm
->refcount
== 0)
401 free_bitmap_record (dpyinfo
, bm
);
407 /* Free all the bitmaps for the display specified by DPYINFO. */
410 x_destroy_all_bitmaps (Display_Info
*dpyinfo
)
413 Bitmap_Record
*bm
= dpyinfo
->bitmaps
;
415 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++, bm
++)
416 if (bm
->refcount
> 0)
417 free_bitmap_record (dpyinfo
, bm
);
419 dpyinfo
->bitmaps_last
= 0;
422 static bool x_create_x_image_and_pixmap (struct frame
*, int, int, int,
423 XImagePtr
*, Pixmap
*);
424 static void x_destroy_x_image (XImagePtr ximg
);
427 static XImagePtr_or_DC
image_get_x_image_or_dc (struct frame
*, struct image
*,
429 static void image_unget_x_image_or_dc (struct image
*, bool, XImagePtr_or_DC
,
432 static XImagePtr
image_get_x_image (struct frame
*, struct image
*, bool);
433 static void image_unget_x_image (struct image
*, bool, XImagePtr
);
434 #define image_get_x_image_or_dc(f, img, mask_p, dummy) \
435 image_get_x_image (f, img, mask_p)
436 #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \
437 image_unget_x_image (img, mask_p, ximg)
440 #ifdef HAVE_X_WINDOWS
442 static void image_sync_to_pixmaps (struct frame
*, struct image
*);
444 /* Useful functions defined in the section
445 `Image type independent image structures' below. */
447 static unsigned long four_corners_best (XImagePtr ximg
,
450 unsigned long height
);
453 /* Create a mask of a bitmap. Note is this not a perfect mask.
454 It's nicer with some borders in this context */
457 x_create_bitmap_mask (struct frame
*f
, ptrdiff_t id
)
460 XImagePtr ximg
, mask_img
;
461 unsigned long width
, height
;
464 unsigned long x
, y
, xp
, xm
, yp
, ym
;
467 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
472 pixmap
= x_bitmap_pixmap (f
, id
);
473 width
= x_bitmap_width (f
, id
);
474 height
= x_bitmap_height (f
, id
);
477 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
486 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
491 XDestroyImage (ximg
);
495 bg
= four_corners_best (ximg
, NULL
, width
, height
);
497 for (y
= 0; y
< ximg
->height
; ++y
)
499 for (x
= 0; x
< ximg
->width
; ++x
)
501 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
502 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
503 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
504 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
505 if (XGetPixel (ximg
, x
, y
) == bg
506 && XGetPixel (ximg
, x
, yp
) == bg
507 && XGetPixel (ximg
, x
, ym
) == bg
508 && XGetPixel (ximg
, xp
, y
) == bg
509 && XGetPixel (ximg
, xp
, yp
) == bg
510 && XGetPixel (ximg
, xp
, ym
) == bg
511 && XGetPixel (ximg
, xm
, y
) == bg
512 && XGetPixel (ximg
, xm
, yp
) == bg
513 && XGetPixel (ximg
, xm
, ym
) == bg
)
514 XPutPixel (mask_img
, x
, y
, 0);
516 XPutPixel (mask_img
, x
, y
, 1);
520 eassert (input_blocked_p ());
521 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
522 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
524 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
526 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
527 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
529 XDestroyImage (ximg
);
530 x_destroy_x_image (mask_img
);
533 #endif /* HAVE_X_WINDOWS */
536 /***********************************************************************
538 ***********************************************************************/
540 /* List of supported image types. Use define_image_type to add new
541 types. Use lookup_image_type to find a type for a given symbol. */
543 static struct image_type
*image_types
;
545 /* The symbol `xbm' which is used as the type symbol for XBM images. */
547 static Lisp_Object Qxbm
;
551 Lisp_Object QCascent
, QCmargin
, QCrelief
;
552 Lisp_Object QCconversion
;
553 static Lisp_Object QCheuristic_mask
;
554 static Lisp_Object QCcolor_symbols
;
555 static Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
, QCgeometry
;
556 static Lisp_Object QCcrop
, QCrotation
;
560 static Lisp_Object Qcount
, Qextension_data
, Qdelay
;
561 static Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
563 /* Forward function prototypes. */
565 static struct image_type
*lookup_image_type (Lisp_Object
);
566 static void x_laplace (struct frame
*, struct image
*);
567 static void x_emboss (struct frame
*, struct image
*);
568 static void x_build_heuristic_mask (struct frame
*, struct image
*,
571 extern Lisp_Object Vlibrary_cache
;
572 #define CACHE_IMAGE_TYPE(type, status) \
573 do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
575 #define CACHE_IMAGE_TYPE(type, status)
578 #define ADD_IMAGE_TYPE(type) \
579 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
581 /* Define a new image type from TYPE. This adds a copy of TYPE to
582 image_types and caches the loading status of TYPE. */
584 static struct image_type
*
585 define_image_type (struct image_type
*type
)
587 struct image_type
*p
= NULL
;
588 Lisp_Object target_type
= *type
->type
;
593 for (p
= image_types
; p
; p
= p
->next
)
594 if (EQ (*p
->type
, target_type
))
599 #if defined HAVE_NTGUI && defined WINDOWSNT
600 /* If we failed to load the library before, don't try again. */
601 Lisp_Object tested
= Fassq (target_type
, Vlibrary_cache
);
602 if (CONSP (tested
) && NILP (XCDR (tested
)))
607 type_valid
= type
->init ();
608 CACHE_IMAGE_TYPE (target_type
, type_valid
? Qt
: Qnil
);
614 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
615 The initialized data segment is read-only. */
616 p
= xmalloc (sizeof *p
);
618 p
->next
= image_types
;
628 /* Value is true if OBJECT is a valid Lisp image specification. A
629 valid image specification is a list whose car is the symbol
630 `image', and whose rest is a property list. The property list must
631 contain a value for key `:type'. That value must be the name of a
632 supported image type. The rest of the property list depends on the
636 valid_image_p (Lisp_Object object
)
644 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
645 if (EQ (XCAR (tem
), QCtype
))
648 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
650 struct image_type
*type
;
651 type
= lookup_image_type (XCAR (tem
));
653 valid_p
= type
->valid_p (object
);
664 /* Log error message with format string FORMAT and argument ARG.
665 Signaling an error, e.g. when an image cannot be loaded, is not a
666 good idea because this would interrupt redisplay, and the error
667 message display would lead to another redisplay. This function
668 therefore simply displays a message. */
671 image_error (const char *format
, Lisp_Object arg1
, Lisp_Object arg2
)
673 add_to_log (format
, arg1
, arg2
);
678 /***********************************************************************
680 ***********************************************************************/
682 enum image_value_type
684 IMAGE_DONT_CHECK_VALUE_TYPE
,
686 IMAGE_STRING_OR_NIL_VALUE
,
688 IMAGE_POSITIVE_INTEGER_VALUE
,
689 IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
,
690 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
693 IMAGE_FUNCTION_VALUE
,
698 /* Structure used when parsing image specifications. */
702 /* Name of keyword. */
705 /* The type of value allowed. */
706 enum image_value_type type
;
708 /* True means key must be present. */
711 /* Used to recognize duplicate keywords in a property list. */
714 /* The value that was found. */
719 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
720 has the format (image KEYWORD VALUE ...). One of the keyword/
721 value pairs must be `:type TYPE'. KEYWORDS is a vector of
722 image_keywords structures of size NKEYWORDS describing other
723 allowed keyword/value pairs. Value is true if SPEC is valid. */
726 parse_image_spec (Lisp_Object spec
, struct image_keyword
*keywords
,
727 int nkeywords
, Lisp_Object type
)
736 while (CONSP (plist
))
738 Lisp_Object key
, value
;
740 /* First element of a pair must be a symbol. */
742 plist
= XCDR (plist
);
746 /* There must follow a value. */
749 value
= XCAR (plist
);
750 plist
= XCDR (plist
);
752 /* Find key in KEYWORDS. Error if not found. */
753 for (i
= 0; i
< nkeywords
; ++i
)
754 if (strcmp (keywords
[i
].name
, SSDATA (SYMBOL_NAME (key
))) == 0)
760 /* Record that we recognized the keyword. If a keywords
761 was found more than once, it's an error. */
762 keywords
[i
].value
= value
;
763 if (keywords
[i
].count
> 1)
767 /* Check type of value against allowed type. */
768 switch (keywords
[i
].type
)
770 case IMAGE_STRING_VALUE
:
771 if (!STRINGP (value
))
775 case IMAGE_STRING_OR_NIL_VALUE
:
776 if (!STRINGP (value
) && !NILP (value
))
780 case IMAGE_SYMBOL_VALUE
:
781 if (!SYMBOLP (value
))
785 case IMAGE_POSITIVE_INTEGER_VALUE
:
786 if (! RANGED_INTEGERP (1, value
, INT_MAX
))
790 case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
:
791 if (RANGED_INTEGERP (0, value
, INT_MAX
))
794 && RANGED_INTEGERP (0, XCAR (value
), INT_MAX
)
795 && RANGED_INTEGERP (0, XCDR (value
), INT_MAX
))
799 case IMAGE_ASCENT_VALUE
:
800 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
802 else if (RANGED_INTEGERP (0, value
, 100))
806 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
807 /* Unlike the other integer-related cases, this one does not
808 verify that VALUE fits in 'int'. This is because callers
810 if (!INTEGERP (value
) || XINT (value
) < 0)
814 case IMAGE_DONT_CHECK_VALUE_TYPE
:
817 case IMAGE_FUNCTION_VALUE
:
818 value
= indirect_function (value
);
819 if (!NILP (Ffunctionp (value
)))
823 case IMAGE_NUMBER_VALUE
:
824 if (!INTEGERP (value
) && !FLOATP (value
))
828 case IMAGE_INTEGER_VALUE
:
829 if (! TYPE_RANGED_INTEGERP (int, value
))
833 case IMAGE_BOOL_VALUE
:
834 if (!NILP (value
) && !EQ (value
, Qt
))
843 if (EQ (key
, QCtype
) && !EQ (type
, value
))
847 /* Check that all mandatory fields are present. */
848 for (i
= 0; i
< nkeywords
; ++i
)
849 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
856 /* Return the value of KEY in image specification SPEC. Value is nil
857 if KEY is not present in SPEC. Set *FOUND depending on whether KEY
858 was found in SPEC. */
861 image_spec_value (Lisp_Object spec
, Lisp_Object key
, bool *found
)
865 eassert (valid_image_p (spec
));
867 for (tail
= XCDR (spec
);
868 CONSP (tail
) && CONSP (XCDR (tail
));
869 tail
= XCDR (XCDR (tail
)))
871 if (EQ (XCAR (tail
), key
))
875 return XCAR (XCDR (tail
));
885 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
886 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
887 PIXELS non-nil means return the size in pixels, otherwise return the
888 size in canonical character units.
889 FRAME is the frame on which the image will be displayed. FRAME nil
890 or omitted means use the selected frame. */)
891 (Lisp_Object spec
, Lisp_Object pixels
, Lisp_Object frame
)
896 if (valid_image_p (spec
))
898 struct frame
*f
= decode_window_system_frame (frame
);
899 ptrdiff_t id
= lookup_image (f
, spec
);
900 struct image
*img
= IMAGE_FROM_ID (f
, id
);
901 int width
= img
->width
+ 2 * img
->hmargin
;
902 int height
= img
->height
+ 2 * img
->vmargin
;
905 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
906 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
908 size
= Fcons (make_number (width
), make_number (height
));
911 error ("Invalid image specification");
917 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
918 doc
: /* Return t if image SPEC has a mask bitmap.
919 FRAME is the frame on which the image will be displayed. FRAME nil
920 or omitted means use the selected frame. */)
921 (Lisp_Object spec
, Lisp_Object frame
)
926 if (valid_image_p (spec
))
928 struct frame
*f
= decode_window_system_frame (frame
);
929 ptrdiff_t id
= lookup_image (f
, spec
);
930 struct image
*img
= IMAGE_FROM_ID (f
, id
);
935 error ("Invalid image specification");
940 DEFUN ("image-metadata", Fimage_metadata
, Simage_metadata
, 1, 2, 0,
941 doc
: /* Return metadata for image SPEC.
942 FRAME is the frame on which the image will be displayed. FRAME nil
943 or omitted means use the selected frame. */)
944 (Lisp_Object spec
, Lisp_Object frame
)
949 if (valid_image_p (spec
))
951 struct frame
*f
= decode_window_system_frame (frame
);
952 ptrdiff_t id
= lookup_image (f
, spec
);
953 struct image
*img
= IMAGE_FROM_ID (f
, id
);
954 ext
= img
->lisp_data
;
961 /***********************************************************************
962 Image type independent image structures
963 ***********************************************************************/
965 #define MAX_IMAGE_SIZE 10.0
966 /* Allocate and return a new image structure for image specification
967 SPEC. SPEC has a hash value of HASH. */
969 static struct image
*
970 make_image (Lisp_Object spec
, EMACS_UINT hash
)
972 struct image
*img
= xzalloc (sizeof *img
);
973 Lisp_Object file
= image_spec_value (spec
, QCfile
, NULL
);
975 eassert (valid_image_p (spec
));
976 img
->dependencies
= NILP (file
) ? Qnil
: list1 (file
);
977 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
978 eassert (img
->type
!= NULL
);
980 img
->lisp_data
= Qnil
;
981 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
983 img
->corners
[BOT_CORNER
] = -1; /* Full image */
988 /* Free image IMG which was used on frame F, including its resources. */
991 free_image (struct frame
*f
, struct image
*img
)
995 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
997 /* Remove IMG from the hash table of its cache. */
999 img
->prev
->next
= img
->next
;
1001 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
1004 img
->next
->prev
= img
->prev
;
1006 c
->images
[img
->id
] = NULL
;
1008 /* Free resources, then free IMG. */
1009 img
->type
->free (f
, img
);
1014 /* Return true if the given widths and heights are valid for display. */
1017 check_image_size (struct frame
*f
, int width
, int height
)
1021 if (width
<= 0 || height
<= 0)
1024 if (INTEGERP (Vmax_image_size
))
1025 return (width
<= XINT (Vmax_image_size
)
1026 && height
<= XINT (Vmax_image_size
));
1027 else if (FLOATP (Vmax_image_size
))
1031 w
= FRAME_PIXEL_WIDTH (f
);
1032 h
= FRAME_PIXEL_HEIGHT (f
);
1035 w
= h
= 1024; /* Arbitrary size for unknown frame. */
1036 return (width
<= XFLOAT_DATA (Vmax_image_size
) * w
1037 && height
<= XFLOAT_DATA (Vmax_image_size
) * h
);
1043 /* Prepare image IMG for display on frame F. Must be called before
1044 drawing an image. */
1047 prepare_image_for_display (struct frame
*f
, struct image
*img
)
1049 /* We're about to display IMG, so set its timestamp to `now'. */
1050 img
->timestamp
= current_emacs_time ();
1052 /* If IMG doesn't have a pixmap yet, load it now, using the image
1053 type dependent loader function. */
1054 if (img
->pixmap
== NO_PIXMAP
&& !img
->load_failed_p
)
1055 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1057 #ifdef HAVE_X_WINDOWS
1058 if (!img
->load_failed_p
)
1061 image_sync_to_pixmaps (f
, img
);
1068 /* Value is the number of pixels for the ascent of image IMG when
1069 drawn in face FACE. */
1072 image_ascent (struct image
*img
, struct face
*face
, struct glyph_slice
*slice
)
1077 if (slice
->height
== img
->height
)
1078 height
= img
->height
+ img
->vmargin
;
1079 else if (slice
->y
== 0)
1080 height
= slice
->height
+ img
->vmargin
;
1082 height
= slice
->height
;
1084 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
1089 /* W32 specific version. Why?. ++kfs */
1090 ascent
= height
/ 2 - (FONT_DESCENT (face
->font
)
1091 - FONT_BASE (face
->font
)) / 2;
1093 /* This expression is arranged so that if the image can't be
1094 exactly centered, it will be moved slightly up. This is
1095 because a typical font is `top-heavy' (due to the presence
1096 uppercase letters), so the image placement should err towards
1097 being top-heavy too. It also just generally looks better. */
1098 ascent
= (height
+ FONT_BASE (face
->font
)
1099 - FONT_DESCENT (face
->font
) + 1) / 2;
1100 #endif /* HAVE_NTGUI */
1103 ascent
= height
/ 2;
1106 ascent
= height
* (img
->ascent
/ 100.0);
1112 /* Image background colors. */
1114 /* Find the "best" corner color of a bitmap.
1115 On W32, XIMG is assumed to a device context with the bitmap selected. */
1117 static RGB_PIXEL_COLOR
1118 four_corners_best (XImagePtr_or_DC ximg
, int *corners
,
1119 unsigned long width
, unsigned long height
)
1121 RGB_PIXEL_COLOR corner_pixels
[4], best
IF_LINT (= 0);
1124 if (corners
&& corners
[BOT_CORNER
] >= 0)
1126 /* Get the colors at the corner_pixels of ximg. */
1127 corner_pixels
[0] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[TOP_CORNER
]);
1128 corner_pixels
[1] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[TOP_CORNER
]);
1129 corner_pixels
[2] = GET_PIXEL (ximg
, corners
[RIGHT_CORNER
] - 1, corners
[BOT_CORNER
] - 1);
1130 corner_pixels
[3] = GET_PIXEL (ximg
, corners
[LEFT_CORNER
], corners
[BOT_CORNER
] - 1);
1134 /* Get the colors at the corner_pixels of ximg. */
1135 corner_pixels
[0] = GET_PIXEL (ximg
, 0, 0);
1136 corner_pixels
[1] = GET_PIXEL (ximg
, width
- 1, 0);
1137 corner_pixels
[2] = GET_PIXEL (ximg
, width
- 1, height
- 1);
1138 corner_pixels
[3] = GET_PIXEL (ximg
, 0, height
- 1);
1140 /* Choose the most frequently found color as background. */
1141 for (i
= best_count
= 0; i
< 4; ++i
)
1145 for (j
= n
= 0; j
< 4; ++j
)
1146 if (corner_pixels
[i
] == corner_pixels
[j
])
1150 best
= corner_pixels
[i
], best_count
= n
;
1156 /* Portability macros */
1160 #define Free_Pixmap(display, pixmap) \
1161 DeleteObject (pixmap)
1163 #elif defined (HAVE_NS)
1165 #define Free_Pixmap(display, pixmap) \
1166 ns_release_object (pixmap)
1170 #define Free_Pixmap(display, pixmap) \
1171 XFreePixmap (display, pixmap)
1173 #endif /* !HAVE_NTGUI && !HAVE_NS */
1176 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1177 it is guessed heuristically. If non-zero, XIMG is an existing
1178 XImage object (or device context with the image selected on W32) to
1179 use for the heuristic. */
1182 image_background (struct image
*img
, struct frame
*f
, XImagePtr_or_DC ximg
)
1184 if (! img
->background_valid
)
1185 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1187 bool free_ximg
= !ximg
;
1190 #endif /* HAVE_NTGUI */
1193 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
1195 img
->background
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
1198 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
1200 img
->background_valid
= 1;
1203 return img
->background
;
1206 /* Return the `background_transparent' field of IMG. If IMG doesn't
1207 have one yet, it is guessed heuristically. If non-zero, MASK is an
1208 existing XImage object to use for the heuristic. */
1211 image_background_transparent (struct image
*img
, struct frame
*f
, XImagePtr_or_DC mask
)
1213 if (! img
->background_transparent_valid
)
1214 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1218 bool free_mask
= !mask
;
1221 #endif /* HAVE_NTGUI */
1224 mask
= image_get_x_image_or_dc (f
, img
, 1, &prev
);
1226 img
->background_transparent
1227 = (four_corners_best (mask
, img
->corners
, img
->width
, img
->height
) == PIX_MASK_RETAIN
);
1230 image_unget_x_image_or_dc (img
, 1, mask
, prev
);
1233 img
->background_transparent
= 0;
1235 img
->background_transparent_valid
= 1;
1238 return img
->background_transparent
;
1242 /***********************************************************************
1243 Helper functions for X image types
1244 ***********************************************************************/
1246 /* Clear X resources of image IMG on frame F according to FLAGS.
1247 FLAGS is bitwise-or of the following masks:
1248 CLEAR_IMAGE_PIXMAP free the pixmap if any.
1249 CLEAR_IMAGE_MASK means clear the mask pixmap if any.
1250 CLEAR_IMAGE_COLORS means free colors allocated for the image, if
1253 #define CLEAR_IMAGE_PIXMAP (1 << 0)
1254 #define CLEAR_IMAGE_MASK (1 << 1)
1255 #define CLEAR_IMAGE_COLORS (1 << 2)
1258 x_clear_image_1 (struct frame
*f
, struct image
*img
, int flags
)
1260 if (flags
& CLEAR_IMAGE_PIXMAP
)
1264 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
1265 img
->pixmap
= NO_PIXMAP
;
1266 /* NOTE (HAVE_NS): background color is NOT an indexed color! */
1267 img
->background_valid
= 0;
1269 #ifdef HAVE_X_WINDOWS
1272 x_destroy_x_image (img
->ximg
);
1274 img
->background_valid
= 0;
1279 if (flags
& CLEAR_IMAGE_MASK
)
1283 Free_Pixmap (FRAME_X_DISPLAY (f
), img
->mask
);
1284 img
->mask
= NO_PIXMAP
;
1285 img
->background_transparent_valid
= 0;
1287 #ifdef HAVE_X_WINDOWS
1290 x_destroy_x_image (img
->mask_img
);
1291 img
->mask_img
= NULL
;
1292 img
->background_transparent_valid
= 0;
1297 if ((flags
& CLEAR_IMAGE_COLORS
) && img
->ncolors
)
1299 /* W32_TODO: color table support. */
1300 #ifdef HAVE_X_WINDOWS
1301 x_free_colors (f
, img
->colors
, img
->ncolors
);
1302 #endif /* HAVE_X_WINDOWS */
1303 xfree (img
->colors
);
1310 /* Free X resources of image IMG which is used on frame F. */
1313 x_clear_image (struct frame
*f
, struct image
*img
)
1316 x_clear_image_1 (f
, img
,
1317 CLEAR_IMAGE_PIXMAP
| CLEAR_IMAGE_MASK
| CLEAR_IMAGE_COLORS
);
1322 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1323 cannot be allocated, use DFLT. Add a newly allocated color to
1324 IMG->colors, so that it can be freed again. Value is the pixel
1327 static unsigned long
1328 x_alloc_image_color (struct frame
*f
, struct image
*img
, Lisp_Object color_name
,
1332 unsigned long result
;
1334 eassert (STRINGP (color_name
));
1336 if (x_defined_color (f
, SSDATA (color_name
), &color
, 1)
1337 && img
->ncolors
< min (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *img
->colors
,
1340 /* This isn't called frequently so we get away with simply
1341 reallocating the color vector to the needed size, here. */
1342 ptrdiff_t ncolors
= img
->ncolors
+ 1;
1343 img
->colors
= xrealloc (img
->colors
, ncolors
* sizeof *img
->colors
);
1344 img
->colors
[ncolors
- 1] = color
.pixel
;
1345 img
->ncolors
= ncolors
;
1346 result
= color
.pixel
;
1356 /***********************************************************************
1358 ***********************************************************************/
1360 static void cache_image (struct frame
*f
, struct image
*img
);
1362 /* Return a new, initialized image cache that is allocated from the
1363 heap. Call free_image_cache to free an image cache. */
1365 struct image_cache
*
1366 make_image_cache (void)
1368 struct image_cache
*c
= xzalloc (sizeof *c
);
1372 c
->images
= xmalloc (size
* sizeof *c
->images
);
1374 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
1375 c
->buckets
= xzalloc (size
);
1380 /* Find an image matching SPEC in the cache, and return it. If no
1381 image is found, return NULL. */
1382 static struct image
*
1383 search_image_cache (struct frame
*f
, Lisp_Object spec
, EMACS_UINT hash
)
1386 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1387 int i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1389 if (!c
) return NULL
;
1391 /* If the image spec does not specify a background color, the cached
1392 image must have the same background color as the current frame.
1393 The foreground color must also match, for the sake of monochrome
1396 In fact, we could ignore the foreground color matching condition
1397 for color images, or if the image spec specifies :foreground;
1398 similarly we could ignore the background color matching condition
1399 for formats that don't use transparency (such as jpeg), or if the
1400 image spec specifies :background. However, the extra memory
1401 usage is probably negligible in practice, so we don't bother. */
1403 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
1404 if (img
->hash
== hash
1405 && !NILP (Fequal (img
->spec
, spec
))
1406 && img
->frame_foreground
== FRAME_FOREGROUND_PIXEL (f
)
1407 && img
->frame_background
== FRAME_BACKGROUND_PIXEL (f
))
1413 /* Search frame F for an image with spec SPEC, and free it. */
1416 uncache_image (struct frame
*f
, Lisp_Object spec
)
1418 struct image
*img
= search_image_cache (f
, spec
, sxhash (spec
, 0));
1421 free_image (f
, img
);
1422 /* As display glyphs may still be referring to the image ID, we
1423 must garbage the frame (Bug#6426). */
1424 SET_FRAME_GARBAGED (f
);
1429 /* Free image cache of frame F. Be aware that X frames share images
1433 free_image_cache (struct frame
*f
)
1435 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1440 /* Cache should not be referenced by any frame when freed. */
1441 eassert (c
->refcount
== 0);
1443 for (i
= 0; i
< c
->used
; ++i
)
1444 free_image (f
, c
->images
[i
]);
1448 FRAME_IMAGE_CACHE (f
) = NULL
;
1453 /* Clear image cache of frame F. FILTER=t means free all images.
1454 FILTER=nil means clear only images that haven't been
1455 displayed for some time.
1456 Else, only free the images which have FILTER in their `dependencies'.
1457 Should be called from time to time to reduce the number of loaded images.
1458 If image-cache-eviction-delay is non-nil, this frees images in the cache
1459 which weren't displayed for at least that many seconds. */
1462 clear_image_cache (struct frame
*f
, Lisp_Object filter
)
1464 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1468 ptrdiff_t i
, nfreed
= 0;
1470 /* Block input so that we won't be interrupted by a SIGIO
1471 while being in an inconsistent state. */
1476 /* Filter image cache. */
1477 for (i
= 0; i
< c
->used
; ++i
)
1479 struct image
*img
= c
->images
[i
];
1480 if (img
&& (EQ (Qt
, filter
)
1481 || !NILP (Fmember (filter
, img
->dependencies
))))
1483 free_image (f
, img
);
1488 else if (INTEGERP (Vimage_cache_eviction_delay
))
1490 /* Free cache based on timestamp. */
1493 ptrdiff_t nimages
= 0;
1495 for (i
= 0; i
< c
->used
; ++i
)
1499 /* If the number of cached images has grown unusually large,
1500 decrease the cache eviction delay (Bug#6230). */
1501 delay
= XINT (Vimage_cache_eviction_delay
);
1503 delay
= 1600 * delay
/ nimages
/ nimages
;
1504 delay
= max (delay
, 1);
1506 t
= current_emacs_time ();
1507 old
= sub_emacs_time (t
, EMACS_TIME_FROM_DOUBLE (delay
));
1509 for (i
= 0; i
< c
->used
; ++i
)
1511 struct image
*img
= c
->images
[i
];
1512 if (img
&& EMACS_TIME_LT (img
->timestamp
, old
))
1514 free_image (f
, img
);
1520 /* We may be clearing the image cache because, for example,
1521 Emacs was iconified for a longer period of time. In that
1522 case, current matrices may still contain references to
1523 images freed above. So, clear these matrices. */
1526 Lisp_Object tail
, frame
;
1528 FOR_EACH_FRAME (tail
, frame
)
1530 struct frame
*fr
= XFRAME (frame
);
1531 if (FRAME_IMAGE_CACHE (fr
) == c
)
1532 clear_current_matrices (fr
);
1535 ++windows_or_buffers_changed
;
1543 clear_image_caches (Lisp_Object filter
)
1545 /* FIXME: We want to do
1546 * struct terminal *t;
1547 * for (t = terminal_list; t; t = t->next_terminal)
1548 * clear_image_cache (t, filter); */
1549 Lisp_Object tail
, frame
;
1550 FOR_EACH_FRAME (tail
, frame
)
1551 if (FRAME_WINDOW_P (XFRAME (frame
)))
1552 clear_image_cache (XFRAME (frame
), filter
);
1555 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
1557 doc
: /* Clear the image cache.
1558 FILTER nil or a frame means clear all images in the selected frame.
1559 FILTER t means clear the image caches of all frames.
1560 Anything else, means only clear those images which refer to FILTER,
1561 which is then usually a filename. */)
1562 (Lisp_Object filter
)
1564 if (!(EQ (filter
, Qnil
) || FRAMEP (filter
)))
1565 clear_image_caches (filter
);
1567 clear_image_cache (decode_window_system_frame (filter
), Qt
);
1573 DEFUN ("image-flush", Fimage_flush
, Simage_flush
,
1575 doc
: /* Fush the image with specification SPEC on frame FRAME.
1576 This removes the image from the Emacs image cache. If SPEC specifies
1577 an image file, the next redisplay of this image will read from the
1578 current contents of that file.
1580 FRAME nil or omitted means use the selected frame.
1581 FRAME t means refresh the image on all frames. */)
1582 (Lisp_Object spec
, Lisp_Object frame
)
1584 if (!valid_image_p (spec
))
1585 error ("Invalid image specification");
1590 FOR_EACH_FRAME (tail
, frame
)
1592 struct frame
*f
= XFRAME (frame
);
1593 if (FRAME_WINDOW_P (f
))
1594 uncache_image (f
, spec
);
1598 uncache_image (decode_window_system_frame (frame
), spec
);
1604 /* Compute masks and transform image IMG on frame F, as specified
1605 by the image's specification, */
1608 postprocess_image (struct frame
*f
, struct image
*img
)
1610 /* Manipulation of the image's mask. */
1613 Lisp_Object conversion
, spec
;
1618 /* `:heuristic-mask t'
1620 means build a mask heuristically.
1621 `:heuristic-mask (R G B)'
1622 `:mask (heuristic (R G B))'
1623 means build a mask from color (R G B) in the
1626 means remove a mask, if any. */
1628 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
1630 x_build_heuristic_mask (f
, img
, mask
);
1635 mask
= image_spec_value (spec
, QCmask
, &found_p
);
1637 if (EQ (mask
, Qheuristic
))
1638 x_build_heuristic_mask (f
, img
, Qt
);
1639 else if (CONSP (mask
)
1640 && EQ (XCAR (mask
), Qheuristic
))
1642 if (CONSP (XCDR (mask
)))
1643 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
1645 x_build_heuristic_mask (f
, img
, XCDR (mask
));
1647 else if (NILP (mask
) && found_p
&& img
->mask
)
1648 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
1652 /* Should we apply an image transformation algorithm? */
1653 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
1654 if (EQ (conversion
, Qdisabled
))
1655 x_disable_image (f
, img
);
1656 else if (EQ (conversion
, Qlaplace
))
1658 else if (EQ (conversion
, Qemboss
))
1660 else if (CONSP (conversion
)
1661 && EQ (XCAR (conversion
), Qedge_detection
))
1664 tem
= XCDR (conversion
);
1666 x_edge_detection (f
, img
,
1667 Fplist_get (tem
, QCmatrix
),
1668 Fplist_get (tem
, QCcolor_adjustment
));
1674 /* Return the id of image with Lisp specification SPEC on frame F.
1675 SPEC must be a valid Lisp image specification (see valid_image_p). */
1678 lookup_image (struct frame
*f
, Lisp_Object spec
)
1683 /* F must be a window-system frame, and SPEC must be a valid image
1685 eassert (FRAME_WINDOW_P (f
));
1686 eassert (valid_image_p (spec
));
1688 /* Look up SPEC in the hash table of the image cache. */
1689 hash
= sxhash (spec
, 0);
1690 img
= search_image_cache (f
, spec
, hash
);
1691 if (img
&& img
->load_failed_p
)
1693 free_image (f
, img
);
1697 /* If not found, create a new image and cache it. */
1701 img
= make_image (spec
, hash
);
1702 cache_image (f
, img
);
1703 img
->load_failed_p
= ! img
->type
->load (f
, img
);
1704 img
->frame_foreground
= FRAME_FOREGROUND_PIXEL (f
);
1705 img
->frame_background
= FRAME_BACKGROUND_PIXEL (f
);
1707 /* If we can't load the image, and we don't have a width and
1708 height, use some arbitrary width and height so that we can
1709 draw a rectangle for it. */
1710 if (img
->load_failed_p
)
1714 value
= image_spec_value (spec
, QCwidth
, NULL
);
1715 img
->width
= (INTEGERP (value
)
1716 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
1717 value
= image_spec_value (spec
, QCheight
, NULL
);
1718 img
->height
= (INTEGERP (value
)
1719 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
1723 /* Handle image type independent image attributes
1724 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1725 `:background COLOR'. */
1726 Lisp_Object ascent
, margin
, relief
, bg
;
1729 ascent
= image_spec_value (spec
, QCascent
, NULL
);
1730 if (INTEGERP (ascent
))
1731 img
->ascent
= XFASTINT (ascent
);
1732 else if (EQ (ascent
, Qcenter
))
1733 img
->ascent
= CENTERED_IMAGE_ASCENT
;
1735 margin
= image_spec_value (spec
, QCmargin
, NULL
);
1736 if (INTEGERP (margin
))
1737 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
1738 else if (CONSP (margin
))
1740 img
->hmargin
= XFASTINT (XCAR (margin
));
1741 img
->vmargin
= XFASTINT (XCDR (margin
));
1744 relief
= image_spec_value (spec
, QCrelief
, NULL
);
1745 relief_bound
= INT_MAX
- max (img
->hmargin
, img
->vmargin
);
1746 if (RANGED_INTEGERP (- relief_bound
, relief
, relief_bound
))
1748 img
->relief
= XINT (relief
);
1749 img
->hmargin
+= eabs (img
->relief
);
1750 img
->vmargin
+= eabs (img
->relief
);
1753 if (! img
->background_valid
)
1755 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
1759 = x_alloc_image_color (f
, img
, bg
,
1760 FRAME_BACKGROUND_PIXEL (f
));
1761 img
->background_valid
= 1;
1765 /* Do image transformations and compute masks, unless we
1766 don't have the image yet. */
1767 if (!EQ (*img
->type
->type
, Qpostscript
))
1768 postprocess_image (f
, img
);
1774 /* We're using IMG, so set its timestamp to `now'. */
1775 img
->timestamp
= current_emacs_time ();
1777 /* Value is the image id. */
1782 /* Cache image IMG in the image cache of frame F. */
1785 cache_image (struct frame
*f
, struct image
*img
)
1787 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
1790 /* Find a free slot in c->images. */
1791 for (i
= 0; i
< c
->used
; ++i
)
1792 if (c
->images
[i
] == NULL
)
1795 /* If no free slot found, maybe enlarge c->images. */
1796 if (i
== c
->used
&& c
->used
== c
->size
)
1797 c
->images
= xpalloc (c
->images
, &c
->size
, 1, -1, sizeof *c
->images
);
1799 /* Add IMG to c->images, and assign IMG an id. */
1805 /* Add IMG to the cache's hash table. */
1806 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
1807 img
->next
= c
->buckets
[i
];
1809 img
->next
->prev
= img
;
1811 c
->buckets
[i
] = img
;
1815 /* Call FN on every image in the image cache of frame F. Used to mark
1816 Lisp Objects in the image cache. */
1818 /* Mark Lisp objects in image IMG. */
1821 mark_image (struct image
*img
)
1823 mark_object (img
->spec
);
1824 mark_object (img
->dependencies
);
1826 if (!NILP (img
->lisp_data
))
1827 mark_object (img
->lisp_data
);
1832 mark_image_cache (struct image_cache
*c
)
1837 for (i
= 0; i
< c
->used
; ++i
)
1839 mark_image (c
->images
[i
]);
1845 /***********************************************************************
1846 X / NS / W32 support code
1847 ***********************************************************************/
1851 /* Macro for defining functions that will be loaded from image DLLs. */
1852 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
1854 /* Macro for loading those image functions from the library. */
1855 #define LOAD_IMGLIB_FN(lib,func) { \
1856 fn_##func = (void *) GetProcAddress (lib, #func); \
1857 if (!fn_##func) return 0; \
1860 #endif /* WINDOWSNT */
1862 /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the
1864 WIDTH and HEIGHT must both be positive.
1865 If XIMG is null, assume it is a bitmap. */
1867 x_check_image_size (XImagePtr ximg
, int width
, int height
)
1869 #ifdef HAVE_X_WINDOWS
1870 /* Respect Xlib's limits: it cannot deal with images that have more
1871 than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits
1872 of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */
1875 XLIB_BYTES_MAX
= min (INT_MAX
, UINT_MAX
),
1876 X_IMAGE_BYTES_MAX
= min (XLIB_BYTES_MAX
, min (PTRDIFF_MAX
, SIZE_MAX
))
1879 int bitmap_pad
, depth
, bytes_per_line
;
1882 bitmap_pad
= ximg
->bitmap_pad
;
1883 depth
= ximg
->depth
;
1884 bytes_per_line
= ximg
->bytes_per_line
;
1890 bytes_per_line
= (width
>> 3) + ((width
& 7) != 0);
1892 return (width
<= (INT_MAX
- (bitmap_pad
- 1)) / depth
1893 && height
<= X_IMAGE_BYTES_MAX
/ bytes_per_line
);
1895 /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases.
1896 For now, assume that every image size is allowed on these systems. */
1901 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1902 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1903 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1904 via xmalloc. Print error messages via image_error if an error
1905 occurs. Value is true if successful.
1907 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1908 should indicate the bit depth of the image. */
1911 x_create_x_image_and_pixmap (struct frame
*f
, int width
, int height
, int depth
,
1912 XImagePtr
*ximg
, Pixmap
*pixmap
)
1914 #ifdef HAVE_X_WINDOWS
1915 Display
*display
= FRAME_X_DISPLAY (f
);
1916 Window window
= FRAME_X_WINDOW (f
);
1917 Screen
*screen
= FRAME_X_SCREEN (f
);
1919 eassert (input_blocked_p ());
1922 depth
= DefaultDepthOfScreen (screen
);
1923 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
1924 depth
, ZPixmap
, 0, NULL
, width
, height
,
1925 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
1928 image_error ("Unable to allocate X image", Qnil
, Qnil
);
1932 if (! x_check_image_size (*ximg
, width
, height
))
1934 x_destroy_x_image (*ximg
);
1936 image_error ("Image too large (%dx%d)",
1937 make_number (width
), make_number (height
));
1941 /* Allocate image raster. */
1942 (*ximg
)->data
= xmalloc ((*ximg
)->bytes_per_line
* height
);
1944 /* Allocate a pixmap of the same size. */
1945 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
1946 if (*pixmap
== NO_PIXMAP
)
1948 x_destroy_x_image (*ximg
);
1950 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
1955 #endif /* HAVE_X_WINDOWS */
1959 BITMAPINFOHEADER
*header
;
1961 int scanline_width_bits
;
1963 int palette_colors
= 0;
1968 if (depth
!= 1 && depth
!= 4 && depth
!= 8
1969 && depth
!= 16 && depth
!= 24 && depth
!= 32)
1971 image_error ("Invalid image bit depth specified", Qnil
, Qnil
);
1975 scanline_width_bits
= width
* depth
;
1976 remainder
= scanline_width_bits
% 32;
1979 scanline_width_bits
+= 32 - remainder
;
1981 /* Bitmaps with a depth less than 16 need a palette. */
1982 /* BITMAPINFO structure already contains the first RGBQUAD. */
1984 palette_colors
= 1 << (depth
- 1);
1986 *ximg
= xmalloc (sizeof (XImage
) + palette_colors
* sizeof (RGBQUAD
));
1988 header
= &(*ximg
)->info
.bmiHeader
;
1989 memset (&(*ximg
)->info
, 0, sizeof (BITMAPINFO
));
1990 header
->biSize
= sizeof (*header
);
1991 header
->biWidth
= width
;
1992 header
->biHeight
= -height
; /* negative indicates a top-down bitmap. */
1993 header
->biPlanes
= 1;
1994 header
->biBitCount
= depth
;
1995 header
->biCompression
= BI_RGB
;
1996 header
->biClrUsed
= palette_colors
;
1998 /* TODO: fill in palette. */
2001 (*ximg
)->info
.bmiColors
[0].rgbBlue
= 0;
2002 (*ximg
)->info
.bmiColors
[0].rgbGreen
= 0;
2003 (*ximg
)->info
.bmiColors
[0].rgbRed
= 0;
2004 (*ximg
)->info
.bmiColors
[0].rgbReserved
= 0;
2005 (*ximg
)->info
.bmiColors
[1].rgbBlue
= 255;
2006 (*ximg
)->info
.bmiColors
[1].rgbGreen
= 255;
2007 (*ximg
)->info
.bmiColors
[1].rgbRed
= 255;
2008 (*ximg
)->info
.bmiColors
[1].rgbReserved
= 0;
2011 hdc
= get_frame_dc (f
);
2013 /* Create a DIBSection and raster array for the bitmap,
2014 and store its handle in *pixmap. */
2015 *pixmap
= CreateDIBSection (hdc
, &((*ximg
)->info
),
2016 (depth
< 16) ? DIB_PAL_COLORS
: DIB_RGB_COLORS
,
2017 /* casting avoids a GCC warning */
2018 (void **)&((*ximg
)->data
), NULL
, 0);
2020 /* Realize display palette and garbage all frames. */
2021 release_frame_dc (f
, hdc
);
2023 if (*pixmap
== NULL
)
2025 DWORD err
= GetLastError ();
2026 Lisp_Object errcode
;
2027 /* All system errors are < 10000, so the following is safe. */
2028 XSETINT (errcode
, err
);
2029 image_error ("Unable to create bitmap, error code %d", errcode
, Qnil
);
2030 x_destroy_x_image (*ximg
);
2036 #endif /* HAVE_NTGUI */
2039 *pixmap
= ns_image_for_XPM (width
, height
, depth
);
2043 image_error ("Unable to allocate NSImage for XPM pixmap", Qnil
, Qnil
);
2052 /* Destroy XImage XIMG. Free XIMG->data. */
2055 x_destroy_x_image (XImagePtr ximg
)
2057 eassert (input_blocked_p ());
2060 #ifdef HAVE_X_WINDOWS
2063 XDestroyImage (ximg
);
2064 #endif /* HAVE_X_WINDOWS */
2066 /* Data will be freed by DestroyObject. */
2069 #endif /* HAVE_NTGUI */
2071 ns_release_object (ximg
);
2072 #endif /* HAVE_NS */
2077 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2078 are width and height of both the image and pixmap. */
2081 x_put_x_image (struct frame
*f
, XImagePtr ximg
, Pixmap pixmap
, int width
, int height
)
2083 #ifdef HAVE_X_WINDOWS
2086 eassert (input_blocked_p ());
2087 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
2088 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
2089 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
2090 #endif /* HAVE_X_WINDOWS */
2093 #if 0 /* I don't think this is necessary looking at where it is used. */
2094 HDC hdc
= get_frame_dc (f
);
2095 SetDIBits (hdc
, pixmap
, 0, height
, ximg
->data
, &(ximg
->info
), DIB_RGB_COLORS
);
2096 release_frame_dc (f
, hdc
);
2098 #endif /* HAVE_NTGUI */
2101 eassert (ximg
== pixmap
);
2102 ns_retain_object (ximg
);
2106 /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches
2107 with image_put_x_image. */
2110 image_create_x_image_and_pixmap (struct frame
*f
, struct image
*img
,
2111 int width
, int height
, int depth
,
2112 XImagePtr
*ximg
, bool mask_p
)
2114 eassert ((!mask_p
? img
->pixmap
: img
->mask
) == NO_PIXMAP
);
2116 return x_create_x_image_and_pixmap (f
, width
, height
, depth
, ximg
,
2117 !mask_p
? &img
->pixmap
: &img
->mask
);
2120 /* Put X image XIMG into image IMG on frame F, as a mask if and only
2121 if MASK_P. On X, this simply records XIMG on a member of IMG, so
2122 it can be put into the pixmap afterwards via image_sync_to_pixmaps.
2123 On the other platforms, it puts XIMG into the pixmap, then frees
2124 the X image and its buffer. */
2127 image_put_x_image (struct frame
*f
, struct image
*img
, XImagePtr ximg
,
2130 #ifdef HAVE_X_WINDOWS
2133 eassert (img
->ximg
== NULL
);
2138 eassert (img
->mask_img
== NULL
);
2139 img
->mask_img
= ximg
;
2142 x_put_x_image (f
, ximg
, !mask_p
? img
->pixmap
: img
->mask
,
2143 img
->width
, img
->height
);
2144 x_destroy_x_image (ximg
);
2148 #ifdef HAVE_X_WINDOWS
2149 /* Put the X images recorded in IMG on frame F into pixmaps, then free
2150 the X images and their buffers. */
2153 image_sync_to_pixmaps (struct frame
*f
, struct image
*img
)
2157 x_put_x_image (f
, img
->ximg
, img
->pixmap
, img
->width
, img
->height
);
2158 x_destroy_x_image (img
->ximg
);
2163 x_put_x_image (f
, img
->mask_img
, img
->mask
, img
->width
, img
->height
);
2164 x_destroy_x_image (img
->mask_img
);
2165 img
->mask_img
= NULL
;
2171 /* Create a memory device context for IMG on frame F. It stores the
2172 currently selected GDI object into *PREV for future restoration by
2173 image_unget_x_image_or_dc. */
2175 static XImagePtr_or_DC
2176 image_get_x_image_or_dc (struct frame
*f
, struct image
*img
, bool mask_p
,
2179 HDC frame_dc
= get_frame_dc (f
);
2180 XImagePtr_or_DC ximg
= CreateCompatibleDC (frame_dc
);
2182 release_frame_dc (f
, frame_dc
);
2183 *prev
= SelectObject (ximg
, !mask_p
? img
->pixmap
: img
->mask
);
2189 image_unget_x_image_or_dc (struct image
*img
, bool mask_p
,
2190 XImagePtr_or_DC ximg
, HGDIOBJ prev
)
2192 SelectObject (ximg
, prev
);
2195 #else /* !HAVE_NTGUI */
2196 /* Get the X image for IMG on frame F. The resulting X image data
2197 should be treated as read-only at least on X. */
2200 image_get_x_image (struct frame
*f
, struct image
*img
, bool mask_p
)
2202 #ifdef HAVE_X_WINDOWS
2203 XImagePtr ximg_in_img
= !mask_p
? img
->ximg
: img
->mask_img
;
2208 return XGetImage (FRAME_X_DISPLAY (f
), !mask_p
? img
->pixmap
: img
->mask
,
2209 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
2210 #elif defined (HAVE_NS)
2211 XImagePtr pixmap
= !mask_p
? img
->pixmap
: img
->mask
;
2213 ns_retain_object (pixmap
);
2218 static void image_unget_x_image (struct image
*img
, bool mask_p
, XImagePtr ximg
)
2220 #ifdef HAVE_X_WINDOWS
2221 XImagePtr ximg_in_img
= !mask_p
? img
->ximg
: img
->mask_img
;
2224 eassert (ximg
== ximg_in_img
);
2226 XDestroyImage (ximg
);
2227 #elif defined (HAVE_NS)
2228 ns_release_object (ximg
);
2231 #endif /* !HAVE_NTGUI */
2234 /***********************************************************************
2236 ***********************************************************************/
2238 /* Find image file FILE. Look in data-directory/images, then
2239 x-bitmap-file-path. Value is the encoded full name of the file
2240 found, or nil if not found. */
2243 x_find_image_file (Lisp_Object file
)
2245 Lisp_Object file_found
, search_path
;
2248 /* TODO I think this should use something like image-load-path
2249 instead. Unfortunately, that can contain non-string elements. */
2250 search_path
= Fcons (Fexpand_file_name (build_string ("images"),
2252 Vx_bitmap_file_path
);
2254 /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
2255 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
2261 file_found
= ENCODE_FILE (file_found
);
2269 /* Read FILE into memory. Value is a pointer to a buffer allocated
2270 with xmalloc holding FILE's contents. Value is null if an error
2271 occurred. *SIZE is set to the size of the file. */
2273 static unsigned char *
2274 slurp_file (char *file
, ptrdiff_t *size
)
2276 FILE *fp
= fopen (file
, "rb");
2277 unsigned char *buf
= NULL
;
2280 if (fp
&& fstat (fileno (fp
), &st
) == 0
2281 && 0 <= st
.st_size
&& st
.st_size
<= min (PTRDIFF_MAX
, SIZE_MAX
)
2282 && (buf
= xmalloc (st
.st_size
),
2283 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
2304 /***********************************************************************
2306 ***********************************************************************/
2308 static bool xbm_load (struct frame
*f
, struct image
*img
);
2309 static bool xbm_image_p (Lisp_Object object
);
2310 static bool xbm_file_p (Lisp_Object
);
2313 /* Indices of image specification fields in xbm_format, below. */
2315 enum xbm_keyword_index
2333 /* Vector of image_keyword structures describing the format
2334 of valid XBM image specifications. */
2336 static const struct image_keyword xbm_format
[XBM_LAST
] =
2338 {":type", IMAGE_SYMBOL_VALUE
, 1},
2339 {":file", IMAGE_STRING_VALUE
, 0},
2340 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2341 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
2342 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2343 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
2344 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
2345 {":ascent", IMAGE_ASCENT_VALUE
, 0},
2346 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
2347 {":relief", IMAGE_INTEGER_VALUE
, 0},
2348 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2349 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
2350 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
2353 /* Structure describing the image type XBM. */
2355 static struct image_type xbm_type
=
2365 /* Tokens returned from xbm_scan. */
2374 /* Return true if OBJECT is a valid XBM-type image specification.
2375 A valid specification is a list starting with the symbol `image'
2376 The rest of the list is a property list which must contain an
2379 If the specification specifies a file to load, it must contain
2380 an entry `:file FILENAME' where FILENAME is a string.
2382 If the specification is for a bitmap loaded from memory it must
2383 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2384 WIDTH and HEIGHT are integers > 0. DATA may be:
2386 1. a string large enough to hold the bitmap data, i.e. it must
2387 have a size >= (WIDTH + 7) / 8 * HEIGHT
2389 2. a bool-vector of size >= WIDTH * HEIGHT
2391 3. a vector of strings or bool-vectors, one for each line of the
2394 4. a string containing an in-memory XBM file. WIDTH and HEIGHT
2395 may not be specified in this case because they are defined in the
2398 Both the file and data forms may contain the additional entries
2399 `:background COLOR' and `:foreground COLOR'. If not present,
2400 foreground and background of the frame on which the image is
2401 displayed is used. */
2404 xbm_image_p (Lisp_Object object
)
2406 struct image_keyword kw
[XBM_LAST
];
2408 memcpy (kw
, xbm_format
, sizeof kw
);
2409 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
2412 eassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
2414 if (kw
[XBM_FILE
].count
)
2416 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
2419 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
2421 /* In-memory XBM file. */
2422 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
2430 /* Entries for `:width', `:height' and `:data' must be present. */
2431 if (!kw
[XBM_WIDTH
].count
2432 || !kw
[XBM_HEIGHT
].count
2433 || !kw
[XBM_DATA
].count
)
2436 data
= kw
[XBM_DATA
].value
;
2437 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
2438 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
2440 /* Check type of data, and width and height against contents of
2446 /* Number of elements of the vector must be >= height. */
2447 if (ASIZE (data
) < height
)
2450 /* Each string or bool-vector in data must be large enough
2451 for one line of the image. */
2452 for (i
= 0; i
< height
; ++i
)
2454 Lisp_Object elt
= AREF (data
, i
);
2459 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
2462 else if (BOOL_VECTOR_P (elt
))
2464 if (XBOOL_VECTOR (elt
)->size
< width
)
2471 else if (STRINGP (data
))
2474 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
2477 else if (BOOL_VECTOR_P (data
))
2479 if (XBOOL_VECTOR (data
)->size
/ height
< width
)
2490 /* Scan a bitmap file. FP is the stream to read from. Value is
2491 either an enumerator from enum xbm_token, or a character for a
2492 single-character token, or 0 at end of file. If scanning an
2493 identifier, store the lexeme of the identifier in SVAL. If
2494 scanning a number, store its value in *IVAL. */
2497 xbm_scan (unsigned char **s
, unsigned char *end
, char *sval
, int *ival
)
2503 /* Skip white space. */
2504 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
2509 else if (c_isdigit (c
))
2511 int value
= 0, digit
;
2513 if (c
== '0' && *s
< end
)
2516 if (c
== 'x' || c
== 'X')
2523 else if (c
>= 'a' && c
<= 'f')
2524 digit
= c
- 'a' + 10;
2525 else if (c
>= 'A' && c
<= 'F')
2526 digit
= c
- 'A' + 10;
2529 value
= 16 * value
+ digit
;
2532 else if (c_isdigit (c
))
2536 && (c
= *(*s
)++, c_isdigit (c
)))
2537 value
= 8 * value
+ c
- '0';
2544 && (c
= *(*s
)++, c_isdigit (c
)))
2545 value
= 10 * value
+ c
- '0';
2553 else if (c_isalpha (c
) || c
== '_')
2557 && (c
= *(*s
)++, (c_isalnum (c
) || c
== '_')))
2564 else if (c
== '/' && **s
== '*')
2566 /* C-style comment. */
2568 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
2582 /* Create a Windows bitmap from X bitmap data. */
2584 w32_create_pixmap_from_bitmap_data (int width
, int height
, char *data
)
2586 static unsigned char swap_nibble
[16]
2587 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2588 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2589 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2590 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2592 unsigned char *bits
, *p
;
2595 w1
= (width
+ 7) / 8; /* nb of 8bits elt in X bitmap */
2596 w2
= ((width
+ 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2597 bits
= alloca (height
* w2
);
2598 memset (bits
, 0, height
* w2
);
2599 for (i
= 0; i
< height
; i
++)
2602 for (j
= 0; j
< w1
; j
++)
2604 /* Bitswap XBM bytes to match how Windows does things. */
2605 unsigned char c
= *data
++;
2606 *p
++ = (unsigned char)((swap_nibble
[c
& 0xf] << 4)
2607 | (swap_nibble
[(c
>>4) & 0xf]));
2610 bmp
= CreateBitmap (width
, height
, 1, 1, (char *) bits
);
2616 convert_mono_to_color_image (struct frame
*f
, struct image
*img
,
2617 COLORREF foreground
, COLORREF background
)
2619 HDC hdc
, old_img_dc
, new_img_dc
;
2620 HGDIOBJ old_prev
, new_prev
;
2623 hdc
= get_frame_dc (f
);
2624 old_img_dc
= CreateCompatibleDC (hdc
);
2625 new_img_dc
= CreateCompatibleDC (hdc
);
2626 new_pixmap
= CreateCompatibleBitmap (hdc
, img
->width
, img
->height
);
2627 release_frame_dc (f
, hdc
);
2628 old_prev
= SelectObject (old_img_dc
, img
->pixmap
);
2629 new_prev
= SelectObject (new_img_dc
, new_pixmap
);
2630 /* Windows convention for mono bitmaps is black = background,
2631 white = foreground. */
2632 SetTextColor (new_img_dc
, background
);
2633 SetBkColor (new_img_dc
, foreground
);
2635 BitBlt (new_img_dc
, 0, 0, img
->width
, img
->height
, old_img_dc
,
2638 SelectObject (old_img_dc
, old_prev
);
2639 SelectObject (new_img_dc
, new_prev
);
2640 DeleteDC (old_img_dc
);
2641 DeleteDC (new_img_dc
);
2642 DeleteObject (img
->pixmap
);
2643 if (new_pixmap
== 0)
2644 fprintf (stderr
, "Failed to convert image to color.\n");
2646 img
->pixmap
= new_pixmap
;
2649 #define XBM_BIT_SHUFFLE(b) (~(b))
2653 #define XBM_BIT_SHUFFLE(b) (b)
2655 #endif /* HAVE_NTGUI */
2659 Create_Pixmap_From_Bitmap_Data (struct frame
*f
, struct image
*img
, char *data
,
2660 RGB_PIXEL_COLOR fg
, RGB_PIXEL_COLOR bg
,
2661 bool non_default_colors
)
2665 = w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
, data
);
2667 /* If colors were specified, transfer the bitmap to a color one. */
2668 if (non_default_colors
)
2669 convert_mono_to_color_image (f
, img
, fg
, bg
);
2671 #elif defined (HAVE_NS)
2672 img
->pixmap
= ns_image_from_XBM (data
, img
->width
, img
->height
);
2676 (x_check_image_size (0, img
->width
, img
->height
)
2677 ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
2680 img
->width
, img
->height
,
2682 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)))
2684 #endif /* !HAVE_NTGUI && !HAVE_NS */
2689 /* Replacement for XReadBitmapFileData which isn't available under old
2690 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2691 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
2692 the image. Return in *DATA the bitmap data allocated with xmalloc.
2693 Value is true if successful. DATA null means just test if
2694 CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR,
2695 inhibit the call to image_error when the image size is invalid (the
2696 bitmap remains unread). */
2699 xbm_read_bitmap_data (struct frame
*f
, unsigned char *contents
, unsigned char *end
,
2700 int *width
, int *height
, char **data
,
2701 bool inhibit_image_error
)
2703 unsigned char *s
= contents
;
2704 char buffer
[BUFSIZ
];
2707 int bytes_per_line
, i
, nbytes
;
2713 LA1 = xbm_scan (&s, end, buffer, &value)
2715 #define expect(TOKEN) \
2716 if (LA1 != (TOKEN)) \
2721 #define expect_ident(IDENT) \
2722 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
2727 *width
= *height
= -1;
2730 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
2732 /* Parse defines for width, height and hot-spots. */
2736 expect_ident ("define");
2737 expect (XBM_TK_IDENT
);
2739 if (LA1
== XBM_TK_NUMBER
)
2741 char *q
= strrchr (buffer
, '_');
2742 q
= q
? q
+ 1 : buffer
;
2743 if (strcmp (q
, "width") == 0)
2745 else if (strcmp (q
, "height") == 0)
2748 expect (XBM_TK_NUMBER
);
2751 if (!check_image_size (f
, *width
, *height
))
2753 if (!inhibit_image_error
)
2754 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
2757 else if (data
== NULL
)
2760 /* Parse bits. Must start with `static'. */
2761 expect_ident ("static");
2762 if (LA1
== XBM_TK_IDENT
)
2764 if (strcmp (buffer
, "unsigned") == 0)
2767 expect_ident ("char");
2769 else if (strcmp (buffer
, "short") == 0)
2773 if (*width
% 16 && *width
% 16 < 9)
2776 else if (strcmp (buffer
, "char") == 0)
2784 expect (XBM_TK_IDENT
);
2790 if (! x_check_image_size (0, *width
, *height
))
2792 if (!inhibit_image_error
)
2793 image_error ("Image too large (%dx%d)",
2794 make_number (*width
), make_number (*height
));
2797 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
2798 nbytes
= bytes_per_line
* *height
;
2799 p
= *data
= xmalloc (nbytes
);
2803 for (i
= 0; i
< nbytes
; i
+= 2)
2806 expect (XBM_TK_NUMBER
);
2808 *p
++ = XBM_BIT_SHUFFLE (val
);
2809 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
2810 *p
++ = XBM_BIT_SHUFFLE (value
>> 8);
2812 if (LA1
== ',' || LA1
== '}')
2820 for (i
= 0; i
< nbytes
; ++i
)
2823 expect (XBM_TK_NUMBER
);
2825 *p
++ = XBM_BIT_SHUFFLE (val
);
2827 if (LA1
== ',' || LA1
== '}')
2852 /* Load XBM image IMG which will be displayed on frame F from buffer
2853 CONTENTS. END is the end of the buffer. Value is true if
2857 xbm_load_image (struct frame
*f
, struct image
*img
, unsigned char *contents
,
2864 rc
= xbm_read_bitmap_data (f
, contents
, end
, &img
->width
, &img
->height
,
2868 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2869 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2870 bool non_default_colors
= 0;
2873 eassert (img
->width
> 0 && img
->height
> 0);
2875 /* Get foreground and background colors, maybe allocate colors. */
2876 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
2879 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
2880 non_default_colors
= 1;
2882 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
2885 background
= x_alloc_image_color (f
, img
, value
, background
);
2886 img
->background
= background
;
2887 img
->background_valid
= 1;
2888 non_default_colors
= 1;
2891 Create_Pixmap_From_Bitmap_Data (f
, img
, data
,
2892 foreground
, background
,
2893 non_default_colors
);
2896 if (img
->pixmap
== NO_PIXMAP
)
2898 x_clear_image (f
, img
);
2899 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
2905 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2911 /* Value is true if DATA looks like an in-memory XBM file. */
2914 xbm_file_p (Lisp_Object data
)
2917 return (STRINGP (data
)
2918 && xbm_read_bitmap_data (NULL
, SDATA (data
),
2919 (SDATA (data
) + SBYTES (data
)),
2924 /* Fill image IMG which is used on frame F with pixmap data. Value is
2925 true if successful. */
2928 xbm_load (struct frame
*f
, struct image
*img
)
2931 Lisp_Object file_name
;
2933 eassert (xbm_image_p (img
->spec
));
2935 /* If IMG->spec specifies a file name, create a non-file spec from it. */
2936 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
2937 if (STRINGP (file_name
))
2940 unsigned char *contents
;
2943 file
= x_find_image_file (file_name
);
2944 if (!STRINGP (file
))
2946 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
2950 contents
= slurp_file (SSDATA (file
), &size
);
2951 if (contents
== NULL
)
2953 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
2957 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
2962 struct image_keyword fmt
[XBM_LAST
];
2964 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
2965 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
2966 bool non_default_colors
= 0;
2969 bool in_memory_file_p
= 0;
2971 /* See if data looks like an in-memory XBM file. */
2972 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
2973 in_memory_file_p
= xbm_file_p (data
);
2975 /* Parse the image specification. */
2976 memcpy (fmt
, xbm_format
, sizeof fmt
);
2977 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
2980 /* Get specified width, and height. */
2981 if (!in_memory_file_p
)
2983 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
2984 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
2985 eassert (img
->width
> 0 && img
->height
> 0);
2986 if (!check_image_size (f
, img
->width
, img
->height
))
2988 image_error ("Invalid image size (see `max-image-size')",
2994 /* Get foreground and background colors, maybe allocate colors. */
2995 if (fmt
[XBM_FOREGROUND
].count
2996 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
2998 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
3000 non_default_colors
= 1;
3003 if (fmt
[XBM_BACKGROUND
].count
3004 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
3006 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
3008 non_default_colors
= 1;
3011 if (in_memory_file_p
)
3012 success_p
= xbm_load_image (f
, img
, SDATA (data
),
3021 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
3023 p
= bits
= alloca (nbytes
* img
->height
);
3024 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
3026 Lisp_Object line
= AREF (data
, i
);
3028 memcpy (p
, SDATA (line
), nbytes
);
3030 memcpy (p
, XBOOL_VECTOR (line
)->data
, nbytes
);
3033 else if (STRINGP (data
))
3034 bits
= SSDATA (data
);
3036 bits
= (char *) XBOOL_VECTOR (data
)->data
;
3042 /* Windows mono bitmaps are reversed compared with X. */
3043 invertedBits
= bits
;
3044 nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
3046 bits
= alloca (nbytes
);
3047 for (i
= 0; i
< nbytes
; i
++)
3048 bits
[i
] = XBM_BIT_SHUFFLE (invertedBits
[i
]);
3051 /* Create the pixmap. */
3053 if (x_check_image_size (0, img
->width
, img
->height
))
3054 Create_Pixmap_From_Bitmap_Data (f
, img
, bits
,
3055 foreground
, background
,
3056 non_default_colors
);
3058 img
->pixmap
= NO_PIXMAP
;
3064 image_error ("Unable to create pixmap for XBM image `%s'",
3066 x_clear_image (f
, img
);
3076 /***********************************************************************
3078 ***********************************************************************/
3080 #if defined (HAVE_XPM) || defined (HAVE_NS)
3082 static bool xpm_image_p (Lisp_Object object
);
3083 static bool xpm_load (struct frame
*f
, struct image
*img
);
3085 #endif /* HAVE_XPM || HAVE_NS */
3089 /* Indicate to xpm.h that we don't have Xlib. */
3091 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3092 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3093 #define XColor xpm_XColor
3094 #define XImage xpm_XImage
3095 #define Display xpm_Display
3096 #define PIXEL_ALREADY_TYPEDEFED
3097 #include "X11/xpm.h"
3102 #undef PIXEL_ALREADY_TYPEDEFED
3104 #include "X11/xpm.h"
3105 #endif /* HAVE_NTGUI */
3106 #endif /* HAVE_XPM */
3108 #if defined (HAVE_XPM) || defined (HAVE_NS)
3109 /* The symbol `xpm' identifying XPM-format images. */
3111 static Lisp_Object Qxpm
;
3113 /* Indices of image specification fields in xpm_format, below. */
3115 enum xpm_keyword_index
3131 /* Vector of image_keyword structures describing the format
3132 of valid XPM image specifications. */
3134 static const struct image_keyword xpm_format
[XPM_LAST
] =
3136 {":type", IMAGE_SYMBOL_VALUE
, 1},
3137 {":file", IMAGE_STRING_VALUE
, 0},
3138 {":data", IMAGE_STRING_VALUE
, 0},
3139 {":ascent", IMAGE_ASCENT_VALUE
, 0},
3140 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
3141 {":relief", IMAGE_INTEGER_VALUE
, 0},
3142 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3143 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3144 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3145 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
3146 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
3149 #if defined HAVE_NTGUI && defined WINDOWSNT
3150 static bool init_xpm_functions (void);
3152 #define init_xpm_functions NULL
3155 /* Structure describing the image type XPM. */
3157 static struct image_type xpm_type
=
3167 #ifdef HAVE_X_WINDOWS
3169 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3170 functions for allocating image colors. Our own functions handle
3171 color allocation failures more gracefully than the ones on the XPM
3174 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3175 #define ALLOC_XPM_COLORS
3177 #endif /* HAVE_X_WINDOWS */
3179 #ifdef ALLOC_XPM_COLORS
3181 static struct xpm_cached_color
*xpm_cache_color (struct frame
*, char *,
3184 /* An entry in a hash table used to cache color definitions of named
3185 colors. This cache is necessary to speed up XPM image loading in
3186 case we do color allocations ourselves. Without it, we would need
3187 a call to XParseColor per pixel in the image. */
3189 struct xpm_cached_color
3191 /* Next in collision chain. */
3192 struct xpm_cached_color
*next
;
3194 /* Color definition (RGB and pixel color). */
3198 char name
[FLEXIBLE_ARRAY_MEMBER
];
3201 /* The hash table used for the color cache, and its bucket vector
3204 #define XPM_COLOR_CACHE_BUCKETS 1001
3205 static struct xpm_cached_color
**xpm_color_cache
;
3207 /* Initialize the color cache. */
3210 xpm_init_color_cache (struct frame
*f
, XpmAttributes
*attrs
)
3212 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
3213 xpm_color_cache
= xzalloc (nbytes
);
3214 init_color_table ();
3216 if (attrs
->valuemask
& XpmColorSymbols
)
3221 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
3222 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3223 attrs
->colorsymbols
[i
].value
, &color
))
3225 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
3227 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
3232 /* Free the color cache. */
3235 xpm_free_color_cache (void)
3237 struct xpm_cached_color
*p
, *next
;
3240 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
3241 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
3247 xfree (xpm_color_cache
);
3248 xpm_color_cache
= NULL
;
3249 free_color_table ();
3252 /* Return the bucket index for color named COLOR_NAME in the color
3256 xpm_color_bucket (char *color_name
)
3258 EMACS_UINT hash
= hash_string (color_name
, strlen (color_name
));
3259 return hash
% XPM_COLOR_CACHE_BUCKETS
;
3263 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3264 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3267 static struct xpm_cached_color
*
3268 xpm_cache_color (struct frame
*f
, char *color_name
, XColor
*color
, int bucket
)
3271 struct xpm_cached_color
*p
;
3274 bucket
= xpm_color_bucket (color_name
);
3276 nbytes
= offsetof (struct xpm_cached_color
, name
) + strlen (color_name
) + 1;
3277 p
= xmalloc (nbytes
);
3278 strcpy (p
->name
, color_name
);
3280 p
->next
= xpm_color_cache
[bucket
];
3281 xpm_color_cache
[bucket
] = p
;
3285 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3286 return the cached definition in *COLOR. Otherwise, make a new
3287 entry in the cache and allocate the color. Value is false if color
3288 allocation failed. */
3291 xpm_lookup_color (struct frame
*f
, char *color_name
, XColor
*color
)
3293 struct xpm_cached_color
*p
;
3294 int h
= xpm_color_bucket (color_name
);
3296 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
3297 if (strcmp (p
->name
, color_name
) == 0)
3302 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
3305 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
3307 p
= xpm_cache_color (f
, color_name
, color
, h
);
3309 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3310 with transparency, and it's useful. */
3311 else if (strcmp ("opaque", color_name
) == 0)
3313 memset (color
, 0, sizeof (XColor
)); /* Is this necessary/correct? */
3314 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
3315 p
= xpm_cache_color (f
, color_name
, color
, h
);
3322 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3323 CLOSURE is a pointer to the frame on which we allocate the
3324 color. Return in *COLOR the allocated color. Value is non-zero
3328 xpm_alloc_color (Display
*dpy
, Colormap cmap
, char *color_name
, XColor
*color
,
3331 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
3335 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3336 is a pointer to the frame on which we allocate the color. Value is
3337 non-zero if successful. */
3340 xpm_free_colors (Display
*dpy
, Colormap cmap
, Pixel
*pixels
, int npixels
, void *closure
)
3345 #endif /* ALLOC_XPM_COLORS */
3350 /* XPM library details. */
3352 DEF_IMGLIB_FN (void, XpmFreeAttributes
, (XpmAttributes
*));
3353 DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer
, (Display
*, char *, xpm_XImage
**,
3354 xpm_XImage
**, XpmAttributes
*));
3355 DEF_IMGLIB_FN (int, XpmReadFileToImage
, (Display
*, char *, xpm_XImage
**,
3356 xpm_XImage
**, XpmAttributes
*));
3357 DEF_IMGLIB_FN (void, XImageFree
, (xpm_XImage
*));
3360 init_xpm_functions (void)
3364 if (!(library
= w32_delayed_load (Qxpm
)))
3367 LOAD_IMGLIB_FN (library
, XpmFreeAttributes
);
3368 LOAD_IMGLIB_FN (library
, XpmCreateImageFromBuffer
);
3369 LOAD_IMGLIB_FN (library
, XpmReadFileToImage
);
3370 LOAD_IMGLIB_FN (library
, XImageFree
);
3374 #endif /* WINDOWSNT */
3376 #if defined HAVE_NTGUI && !defined WINDOWSNT
3377 /* Glue for code below */
3378 #define fn_XpmReadFileToImage XpmReadFileToImage
3379 #define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer
3380 #define fn_XImageFree XImageFree
3381 #define fn_XpmFreeAttributes XpmFreeAttributes
3382 #endif /* HAVE_NTGUI && !WINDOWSNT */
3384 /* Value is true if COLOR_SYMBOLS is a valid color symbols list
3385 for XPM images. Such a list must consist of conses whose car and
3389 xpm_valid_color_symbols_p (Lisp_Object color_symbols
)
3391 while (CONSP (color_symbols
))
3393 Lisp_Object sym
= XCAR (color_symbols
);
3395 || !STRINGP (XCAR (sym
))
3396 || !STRINGP (XCDR (sym
)))
3398 color_symbols
= XCDR (color_symbols
);
3401 return NILP (color_symbols
);
3405 /* Value is true if OBJECT is a valid XPM image specification. */
3408 xpm_image_p (Lisp_Object object
)
3410 struct image_keyword fmt
[XPM_LAST
];
3411 memcpy (fmt
, xpm_format
, sizeof fmt
);
3412 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
3413 /* Either `:file' or `:data' must be present. */
3414 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
3415 /* Either no `:color-symbols' or it's a list of conses
3416 whose car and cdr are strings. */
3417 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
3418 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
3421 #endif /* HAVE_XPM || HAVE_NS */
3423 #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK
3425 x_create_bitmap_from_xpm_data (struct frame
*f
, const char **bits
)
3427 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3430 XpmAttributes attrs
;
3431 Pixmap bitmap
, mask
;
3433 memset (&attrs
, 0, sizeof attrs
);
3435 attrs
.visual
= FRAME_X_VISUAL (f
);
3436 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3437 attrs
.valuemask
|= XpmVisual
;
3438 attrs
.valuemask
|= XpmColormap
;
3440 rc
= XpmCreatePixmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3441 (char **) bits
, &bitmap
, &mask
, &attrs
);
3442 if (rc
!= XpmSuccess
)
3444 XpmFreeAttributes (&attrs
);
3448 id
= x_allocate_bitmap_record (f
);
3449 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
3450 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
3451 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
3452 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
3453 dpyinfo
->bitmaps
[id
- 1].height
= attrs
.height
;
3454 dpyinfo
->bitmaps
[id
- 1].width
= attrs
.width
;
3455 dpyinfo
->bitmaps
[id
- 1].depth
= attrs
.depth
;
3456 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
3458 XpmFreeAttributes (&attrs
);
3461 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3463 /* Load image IMG which will be displayed on frame F. Value is
3464 true if successful. */
3469 xpm_load (struct frame
*f
, struct image
*img
)
3472 XpmAttributes attrs
;
3473 Lisp_Object specified_file
, color_symbols
;
3476 xpm_XImage
* xpm_image
= NULL
, * xpm_mask
= NULL
;
3477 #endif /* HAVE_NTGUI */
3479 /* Configure the XPM lib. Use the visual of frame F. Allocate
3480 close colors. Return colors allocated. */
3481 memset (&attrs
, 0, sizeof attrs
);
3484 attrs
.visual
= FRAME_X_VISUAL (f
);
3485 attrs
.colormap
= FRAME_X_COLORMAP (f
);
3486 attrs
.valuemask
|= XpmVisual
;
3487 attrs
.valuemask
|= XpmColormap
;
3488 #endif /* HAVE_NTGUI */
3490 #ifdef ALLOC_XPM_COLORS
3491 /* Allocate colors with our own functions which handle
3492 failing color allocation more gracefully. */
3493 attrs
.color_closure
= f
;
3494 attrs
.alloc_color
= xpm_alloc_color
;
3495 attrs
.free_colors
= xpm_free_colors
;
3496 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
3497 #else /* not ALLOC_XPM_COLORS */
3498 /* Let the XPM lib allocate colors. */
3499 attrs
.valuemask
|= XpmReturnAllocPixels
;
3500 #ifdef XpmAllocCloseColors
3501 attrs
.alloc_close_colors
= 1;
3502 attrs
.valuemask
|= XpmAllocCloseColors
;
3503 #else /* not XpmAllocCloseColors */
3504 attrs
.closeness
= 600;
3505 attrs
.valuemask
|= XpmCloseness
;
3506 #endif /* not XpmAllocCloseColors */
3507 #endif /* ALLOC_XPM_COLORS */
3509 /* If image specification contains symbolic color definitions, add
3510 these to `attrs'. */
3511 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
3512 if (CONSP (color_symbols
))
3515 XpmColorSymbol
*xpm_syms
;
3518 attrs
.valuemask
|= XpmColorSymbols
;
3520 /* Count number of symbols. */
3521 attrs
.numsymbols
= 0;
3522 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
3525 /* Allocate an XpmColorSymbol array. */
3526 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
3527 xpm_syms
= alloca (size
);
3528 memset (xpm_syms
, 0, size
);
3529 attrs
.colorsymbols
= xpm_syms
;
3531 /* Fill the color symbol array. */
3532 for (tail
= color_symbols
, i
= 0;
3534 ++i
, tail
= XCDR (tail
))
3538 char *empty_string
= (char *) "";
3540 if (!CONSP (XCAR (tail
)))
3542 xpm_syms
[i
].name
= empty_string
;
3543 xpm_syms
[i
].value
= empty_string
;
3546 name
= XCAR (XCAR (tail
));
3547 color
= XCDR (XCAR (tail
));
3550 xpm_syms
[i
].name
= alloca (SCHARS (name
) + 1);
3551 strcpy (xpm_syms
[i
].name
, SSDATA (name
));
3554 xpm_syms
[i
].name
= empty_string
;
3555 if (STRINGP (color
))
3557 xpm_syms
[i
].value
= alloca (SCHARS (color
) + 1);
3558 strcpy (xpm_syms
[i
].value
, SSDATA (color
));
3561 xpm_syms
[i
].value
= empty_string
;
3565 /* Create a pixmap for the image, either from a file, or from a
3566 string buffer containing data in the same format as an XPM file. */
3567 #ifdef ALLOC_XPM_COLORS
3568 xpm_init_color_cache (f
, &attrs
);
3571 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
3575 HDC frame_dc
= get_frame_dc (f
);
3576 hdc
= CreateCompatibleDC (frame_dc
);
3577 release_frame_dc (f
, frame_dc
);
3579 #endif /* HAVE_NTGUI */
3581 if (STRINGP (specified_file
))
3583 Lisp_Object file
= x_find_image_file (specified_file
);
3584 if (!STRINGP (file
))
3586 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
3587 #ifdef ALLOC_XPM_COLORS
3588 xpm_free_color_cache ();
3594 /* XpmReadFileToPixmap is not available in the Windows port of
3595 libxpm. But XpmReadFileToImage almost does what we want. */
3596 rc
= fn_XpmReadFileToImage (&hdc
, SDATA (file
),
3597 &xpm_image
, &xpm_mask
,
3600 rc
= XpmReadFileToImage (FRAME_X_DISPLAY (f
), SSDATA (file
),
3601 &img
->ximg
, &img
->mask_img
,
3603 #endif /* HAVE_NTGUI */
3607 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
3608 if (!STRINGP (buffer
))
3610 image_error ("Invalid image data `%s'", buffer
, Qnil
);
3611 #ifdef ALLOC_XPM_COLORS
3612 xpm_free_color_cache ();
3617 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3618 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3619 rc
= fn_XpmCreateImageFromBuffer (&hdc
, SDATA (buffer
),
3620 &xpm_image
, &xpm_mask
,
3623 rc
= XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f
), SSDATA (buffer
),
3624 &img
->ximg
, &img
->mask_img
,
3626 #endif /* HAVE_NTGUI */
3629 #ifdef HAVE_X_WINDOWS
3630 if (rc
== XpmSuccess
)
3632 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3633 img
->ximg
->width
, img
->ximg
->height
,
3635 if (img
->pixmap
== NO_PIXMAP
)
3637 x_clear_image (f
, img
);
3640 else if (img
->mask_img
)
3642 img
->mask
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3643 img
->mask_img
->width
,
3644 img
->mask_img
->height
,
3645 img
->mask_img
->depth
);
3646 if (img
->mask
== NO_PIXMAP
)
3648 x_clear_image (f
, img
);
3655 if (rc
== XpmSuccess
)
3657 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3658 img
->colors
= colors_in_color_table (&img
->ncolors
);
3659 #else /* not ALLOC_XPM_COLORS */
3663 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3664 plus some duplicate attributes. */
3665 if (xpm_image
&& xpm_image
->bitmap
)
3667 img
->pixmap
= xpm_image
->bitmap
;
3668 /* XImageFree in libXpm frees XImage struct without destroying
3669 the bitmap, which is what we want. */
3670 fn_XImageFree (xpm_image
);
3672 if (xpm_mask
&& xpm_mask
->bitmap
)
3674 /* The mask appears to be inverted compared with what we expect.
3675 TODO: invert our expectations. See other places where we
3676 have to invert bits because our idea of masks is backwards. */
3678 old_obj
= SelectObject (hdc
, xpm_mask
->bitmap
);
3680 PatBlt (hdc
, 0, 0, xpm_mask
->width
, xpm_mask
->height
, DSTINVERT
);
3681 SelectObject (hdc
, old_obj
);
3683 img
->mask
= xpm_mask
->bitmap
;
3684 fn_XImageFree (xpm_mask
);
3689 #endif /* HAVE_NTGUI */
3691 /* Remember allocated colors. */
3692 img
->colors
= xnmalloc (attrs
.nalloc_pixels
, sizeof *img
->colors
);
3693 img
->ncolors
= attrs
.nalloc_pixels
;
3694 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
3696 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
3697 #ifdef DEBUG_X_COLORS
3698 register_color (img
->colors
[i
]);
3701 #endif /* not ALLOC_XPM_COLORS */
3703 img
->width
= attrs
.width
;
3704 img
->height
= attrs
.height
;
3705 eassert (img
->width
> 0 && img
->height
> 0);
3707 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3709 fn_XpmFreeAttributes (&attrs
);
3711 XpmFreeAttributes (&attrs
);
3712 #endif /* HAVE_NTGUI */
3714 #ifdef HAVE_X_WINDOWS
3715 /* Maybe fill in the background field while we have ximg handy. */
3716 IMAGE_BACKGROUND (img
, f
, img
->ximg
);
3718 /* Fill in the background_transparent field while we have the
3720 image_background_transparent (img
, f
, img
->mask_img
);
3727 #endif /* HAVE_NTGUI */
3732 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
3735 case XpmFileInvalid
:
3736 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
3740 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
3743 case XpmColorFailed
:
3744 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
3748 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
3753 #ifdef ALLOC_XPM_COLORS
3754 xpm_free_color_cache ();
3756 return rc
== XpmSuccess
;
3759 #endif /* HAVE_XPM */
3761 #if defined (HAVE_NS) && !defined (HAVE_XPM)
3763 /* XPM support functions for NS where libxpm is not available.
3764 Only XPM version 3 (without any extensions) is supported. */
3766 static void xpm_put_color_table_v (Lisp_Object
, const unsigned char *,
3768 static Lisp_Object
xpm_get_color_table_v (Lisp_Object
,
3769 const unsigned char *, int);
3770 static void xpm_put_color_table_h (Lisp_Object
, const unsigned char *,
3772 static Lisp_Object
xpm_get_color_table_h (Lisp_Object
,
3773 const unsigned char *, int);
3775 /* Tokens returned from xpm_scan. */
3784 /* Scan an XPM data and return a character (< 256) or a token defined
3785 by enum xpm_token above. *S and END are the start (inclusive) and
3786 the end (exclusive) addresses of the data, respectively. Advance
3787 *S while scanning. If token is either XPM_TK_IDENT or
3788 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
3789 length of the corresponding token, respectively. */
3792 xpm_scan (const unsigned char **s
,
3793 const unsigned char *end
,
3794 const unsigned char **beg
,
3801 /* Skip white-space. */
3802 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
3805 /* gnus-pointer.xpm uses '-' in its identifier.
3806 sb-dir-plus.xpm uses '+' in its identifier. */
3807 if (c_isalpha (c
) || c
== '_' || c
== '-' || c
== '+')
3811 && (c
= **s
, c_isalnum (c
)
3812 || c
== '_' || c
== '-' || c
== '+'))
3815 return XPM_TK_IDENT
;
3820 while (*s
< end
&& **s
!= '"')
3825 return XPM_TK_STRING
;
3829 if (*s
< end
&& **s
== '*')
3831 /* C-style comment. */
3835 while (*s
< end
&& *(*s
)++ != '*')
3838 while (*s
< end
&& **s
!= '/');
3852 /* Functions for color table lookup in XPM data. A key is a string
3853 specifying the color of each pixel in XPM data. A value is either
3854 an integer that specifies a pixel color, Qt that specifies
3855 transparency, or Qnil for the unspecified color. If the length of
3856 the key string is one, a vector is used as a table. Otherwise, a
3857 hash table is used. */
3860 xpm_make_color_table_v (void (**put_func
) (Lisp_Object
,
3861 const unsigned char *,
3864 Lisp_Object (**get_func
) (Lisp_Object
,
3865 const unsigned char *,
3868 *put_func
= xpm_put_color_table_v
;
3869 *get_func
= xpm_get_color_table_v
;
3870 return Fmake_vector (make_number (256), Qnil
);
3874 xpm_put_color_table_v (Lisp_Object color_table
,
3875 const unsigned char *chars_start
,
3879 ASET (color_table
, *chars_start
, color
);
3883 xpm_get_color_table_v (Lisp_Object color_table
,
3884 const unsigned char *chars_start
,
3887 return AREF (color_table
, *chars_start
);
3891 xpm_make_color_table_h (void (**put_func
) (Lisp_Object
,
3892 const unsigned char *,
3895 Lisp_Object (**get_func
) (Lisp_Object
,
3896 const unsigned char *,
3899 *put_func
= xpm_put_color_table_h
;
3900 *get_func
= xpm_get_color_table_h
;
3901 return make_hash_table (hashtest_equal
, make_number (DEFAULT_HASH_SIZE
),
3902 make_float (DEFAULT_REHASH_SIZE
),
3903 make_float (DEFAULT_REHASH_THRESHOLD
),
3908 xpm_put_color_table_h (Lisp_Object color_table
,
3909 const unsigned char *chars_start
,
3913 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3914 EMACS_UINT hash_code
;
3915 Lisp_Object chars
= make_unibyte_string (chars_start
, chars_len
);
3917 hash_lookup (table
, chars
, &hash_code
);
3918 hash_put (table
, chars
, color
, hash_code
);
3922 xpm_get_color_table_h (Lisp_Object color_table
,
3923 const unsigned char *chars_start
,
3926 struct Lisp_Hash_Table
*table
= XHASH_TABLE (color_table
);
3928 hash_lookup (table
, make_unibyte_string (chars_start
, chars_len
), NULL
);
3930 return i
>= 0 ? HASH_VALUE (table
, i
) : Qnil
;
3933 enum xpm_color_key
{
3941 static const char xpm_color_key_strings
[][4] = {"s", "m", "g4", "g", "c"};
3944 xpm_str_to_color_key (const char *s
)
3949 i
< sizeof xpm_color_key_strings
/ sizeof xpm_color_key_strings
[0];
3951 if (strcmp (xpm_color_key_strings
[i
], s
) == 0)
3957 xpm_load_image (struct frame
*f
,
3959 const unsigned char *contents
,
3960 const unsigned char *end
)
3962 const unsigned char *s
= contents
, *beg
, *str
;
3963 unsigned char buffer
[BUFSIZ
];
3964 int width
, height
, x
, y
;
3965 int num_colors
, chars_per_pixel
;
3968 void (*put_color_table
) (Lisp_Object
, const unsigned char *, int, Lisp_Object
);
3969 Lisp_Object (*get_color_table
) (Lisp_Object
, const unsigned char *, int);
3970 Lisp_Object frame
, color_symbols
, color_table
;
3973 XImagePtr ximg
= NULL
, mask_img
= NULL
;
3976 LA1 = xpm_scan (&s, end, &beg, &len)
3978 #define expect(TOKEN) \
3979 if (LA1 != (TOKEN)) \
3984 #define expect_ident(IDENT) \
3985 if (LA1 == XPM_TK_IDENT \
3986 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
3991 if (!(end
- s
>= 9 && memcmp (s
, "/* XPM */", 9) == 0))
3995 expect_ident ("static");
3996 expect_ident ("char");
3998 expect (XPM_TK_IDENT
);
4003 expect (XPM_TK_STRING
);
4006 memcpy (buffer
, beg
, len
);
4008 if (sscanf (buffer
, "%d %d %d %d", &width
, &height
,
4009 &num_colors
, &chars_per_pixel
) != 4
4010 || width
<= 0 || height
<= 0
4011 || num_colors
<= 0 || chars_per_pixel
<= 0)
4014 if (!check_image_size (f
, width
, height
))
4016 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
4020 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0)
4022 || !image_create_x_image_and_pixmap (f
, img
, width
, height
, 1,
4027 image_error ("Image too large", Qnil
, Qnil
);
4033 XSETFRAME (frame
, f
);
4034 if (!NILP (Fxw_display_color_p (frame
)))
4035 best_key
= XPM_COLOR_KEY_C
;
4036 else if (!NILP (Fx_display_grayscale_p (frame
)))
4037 best_key
= (XFASTINT (Fx_display_planes (frame
)) > 2
4038 ? XPM_COLOR_KEY_G
: XPM_COLOR_KEY_G4
);
4040 best_key
= XPM_COLOR_KEY_M
;
4042 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
4043 if (chars_per_pixel
== 1)
4044 color_table
= xpm_make_color_table_v (&put_color_table
,
4047 color_table
= xpm_make_color_table_h (&put_color_table
,
4050 while (num_colors
-- > 0)
4052 char *color
, *max_color
;
4053 int key
, next_key
, max_key
= 0;
4054 Lisp_Object symbol_color
= Qnil
, color_val
;
4057 expect (XPM_TK_STRING
);
4058 if (len
<= chars_per_pixel
|| len
>= BUFSIZ
+ chars_per_pixel
)
4060 memcpy (buffer
, beg
+ chars_per_pixel
, len
- chars_per_pixel
);
4061 buffer
[len
- chars_per_pixel
] = '\0';
4063 str
= strtok (buffer
, " \t");
4066 key
= xpm_str_to_color_key (str
);
4071 color
= strtok (NULL
, " \t");
4075 while ((str
= strtok (NULL
, " \t")) != NULL
)
4077 next_key
= xpm_str_to_color_key (str
);
4080 color
[strlen (color
)] = ' ';
4083 if (key
== XPM_COLOR_KEY_S
)
4085 if (NILP (symbol_color
))
4086 symbol_color
= build_string (color
);
4088 else if (max_key
< key
&& key
<= best_key
)
4098 if (!NILP (color_symbols
) && !NILP (symbol_color
))
4100 Lisp_Object specified_color
= Fassoc (symbol_color
, color_symbols
);
4102 if (CONSP (specified_color
) && STRINGP (XCDR (specified_color
)))
4104 if (xstrcasecmp (SSDATA (XCDR (specified_color
)), "None") == 0)
4106 else if (x_defined_color (f
, SSDATA (XCDR (specified_color
)),
4108 color_val
= make_number (cdef
.pixel
);
4111 if (NILP (color_val
) && max_key
> 0)
4113 if (xstrcasecmp (max_color
, "None") == 0)
4115 else if (x_defined_color (f
, max_color
, &cdef
, 0))
4116 color_val
= make_number (cdef
.pixel
);
4118 if (!NILP (color_val
))
4119 (*put_color_table
) (color_table
, beg
, chars_per_pixel
, color_val
);
4124 for (y
= 0; y
< height
; y
++)
4126 expect (XPM_TK_STRING
);
4128 if (len
< width
* chars_per_pixel
)
4130 for (x
= 0; x
< width
; x
++, str
+= chars_per_pixel
)
4132 Lisp_Object color_val
=
4133 (*get_color_table
) (color_table
, str
, chars_per_pixel
);
4135 XPutPixel (ximg
, x
, y
,
4136 (INTEGERP (color_val
) ? XINT (color_val
)
4137 : FRAME_FOREGROUND_PIXEL (f
)));
4139 XPutPixel (mask_img
, x
, y
,
4140 (!EQ (color_val
, Qt
) ? PIX_MASK_DRAW
4141 : (have_mask
= 1, PIX_MASK_RETAIN
)));
4143 if (EQ (color_val
, Qt
))
4144 ns_set_alpha (ximg
, x
, y
, 0);
4152 img
->height
= height
;
4154 /* Maybe fill in the background field while we have ximg handy. */
4155 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
4156 IMAGE_BACKGROUND (img
, f
, ximg
);
4158 image_put_x_image (f
, img
, ximg
, 0);
4162 /* Fill in the background_transparent field while we have the
4164 image_background_transparent (img
, f
, mask_img
);
4166 image_put_x_image (f
, img
, mask_img
, 1);
4170 x_destroy_x_image (mask_img
);
4171 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
4177 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
4178 x_destroy_x_image (ximg
);
4179 x_destroy_x_image (mask_img
);
4180 x_clear_image (f
, img
);
4189 xpm_load (struct frame
*f
,
4193 Lisp_Object file_name
;
4195 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4196 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
4197 if (STRINGP (file_name
))
4200 unsigned char *contents
;
4203 file
= x_find_image_file (file_name
);
4204 if (!STRINGP (file
))
4206 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
4210 contents
= slurp_file (SSDATA (file
), &size
);
4211 if (contents
== NULL
)
4213 image_error ("Error loading XPM image `%s'", img
->spec
, Qnil
);
4217 success_p
= xpm_load_image (f
, img
, contents
, contents
+ size
);
4224 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
4225 if (!STRINGP (data
))
4227 image_error ("Invalid image data `%s'", data
, Qnil
);
4230 success_p
= xpm_load_image (f
, img
, SDATA (data
),
4231 SDATA (data
) + SBYTES (data
));
4237 #endif /* HAVE_NS && !HAVE_XPM */
4241 /***********************************************************************
4243 ***********************************************************************/
4245 #ifdef COLOR_TABLE_SUPPORT
4247 /* An entry in the color table mapping an RGB color to a pixel color. */
4252 unsigned long pixel
;
4254 /* Next in color table collision list. */
4255 struct ct_color
*next
;
4258 /* The bucket vector size to use. Must be prime. */
4262 /* Value is a hash of the RGB color given by R, G, and B. */
4264 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4266 /* The color hash table. */
4268 static struct ct_color
**ct_table
;
4270 /* Number of entries in the color table. */
4272 static int ct_colors_allocated
;
4275 ct_colors_allocated_max
=
4277 min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (unsigned long))
4280 /* Initialize the color table. */
4283 init_color_table (void)
4285 int size
= CT_SIZE
* sizeof (*ct_table
);
4286 ct_table
= xzalloc (size
);
4287 ct_colors_allocated
= 0;
4291 /* Free memory associated with the color table. */
4294 free_color_table (void)
4297 struct ct_color
*p
, *next
;
4299 for (i
= 0; i
< CT_SIZE
; ++i
)
4300 for (p
= ct_table
[i
]; p
; p
= next
)
4311 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4312 entry for that color already is in the color table, return the
4313 pixel color of that entry. Otherwise, allocate a new color for R,
4314 G, B, and make an entry in the color table. */
4316 static unsigned long
4317 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4319 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
4320 int i
= hash
% CT_SIZE
;
4322 Display_Info
*dpyinfo
;
4324 /* Handle TrueColor visuals specially, which improves performance by
4325 two orders of magnitude. Freeing colors on TrueColor visuals is
4326 a nop, and pixel colors specify RGB values directly. See also
4327 the Xlib spec, chapter 3.1. */
4328 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4329 if (dpyinfo
->red_bits
> 0)
4331 unsigned long pr
, pg
, pb
;
4333 /* Apply gamma-correction like normal color allocation does. */
4337 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
4338 gamma_correct (f
, &color
);
4339 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
4342 /* Scale down RGB values to the visual's bits per RGB, and shift
4343 them to the right position in the pixel color. Note that the
4344 original RGB values are 16-bit values, as usual in X. */
4345 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
4346 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
4347 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
4349 /* Assemble the pixel color. */
4350 return pr
| pg
| pb
;
4353 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4354 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
4360 #ifdef HAVE_X_WINDOWS
4368 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4369 return FRAME_FOREGROUND_PIXEL (f
);
4371 #ifdef HAVE_X_WINDOWS
4376 cmap
= FRAME_X_COLORMAP (f
);
4377 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4380 ++ct_colors_allocated
;
4381 p
= xmalloc (sizeof *p
);
4385 p
->pixel
= color
.pixel
;
4386 p
->next
= ct_table
[i
];
4390 return FRAME_FOREGROUND_PIXEL (f
);
4394 color
= PALETTERGB (r
, g
, b
);
4396 color
= RGB_TO_ULONG (r
, g
, b
);
4397 #endif /* HAVE_NTGUI */
4398 ++ct_colors_allocated
;
4399 p
= xmalloc (sizeof *p
);
4404 p
->next
= ct_table
[i
];
4406 #endif /* HAVE_X_WINDOWS */
4414 /* Look up pixel color PIXEL which is used on frame F in the color
4415 table. If not already present, allocate it. Value is PIXEL. */
4417 static unsigned long
4418 lookup_pixel_color (struct frame
*f
, unsigned long pixel
)
4420 int i
= pixel
% CT_SIZE
;
4423 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4424 if (p
->pixel
== pixel
)
4433 if (ct_colors_allocated_max
<= ct_colors_allocated
)
4434 return FRAME_FOREGROUND_PIXEL (f
);
4436 #ifdef HAVE_X_WINDOWS
4437 cmap
= FRAME_X_COLORMAP (f
);
4438 color
.pixel
= pixel
;
4439 x_query_color (f
, &color
);
4440 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4443 cmap
= DefaultColormapOfScreen (FRAME_X_SCREEN (f
));
4444 color
.pixel
= pixel
;
4445 XQueryColor (NULL
, cmap
, &color
);
4446 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
4448 #endif /* HAVE_X_WINDOWS */
4452 ++ct_colors_allocated
;
4454 p
= xmalloc (sizeof *p
);
4459 p
->next
= ct_table
[i
];
4463 return FRAME_FOREGROUND_PIXEL (f
);
4469 /* Value is a vector of all pixel colors contained in the color table,
4470 allocated via xmalloc. Set *N to the number of colors. */
4472 static unsigned long *
4473 colors_in_color_table (int *n
)
4477 unsigned long *colors
;
4479 if (ct_colors_allocated
== 0)
4486 colors
= xmalloc (ct_colors_allocated
* sizeof *colors
);
4487 *n
= ct_colors_allocated
;
4489 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
4490 for (p
= ct_table
[i
]; p
; p
= p
->next
)
4491 colors
[j
++] = p
->pixel
;
4497 #else /* COLOR_TABLE_SUPPORT */
4499 static unsigned long
4500 lookup_rgb_color (struct frame
*f
, int r
, int g
, int b
)
4502 unsigned long pixel
;
4505 pixel
= PALETTERGB (r
>> 8, g
>> 8, b
>> 8);
4506 #endif /* HAVE_NTGUI */
4509 pixel
= RGB_TO_ULONG (r
>> 8, g
>> 8, b
>> 8);
4510 #endif /* HAVE_NS */
4515 init_color_table (void)
4518 #endif /* COLOR_TABLE_SUPPORT */
4521 /***********************************************************************
4523 ***********************************************************************/
4525 /* Edge detection matrices for different edge-detection
4528 static int emboss_matrix
[9] = {
4530 2, -1, 0, /* y - 1 */
4532 0, 1, -2 /* y + 1 */
4535 static int laplace_matrix
[9] = {
4537 1, 0, 0, /* y - 1 */
4539 0, 0, -1 /* y + 1 */
4542 /* Value is the intensity of the color whose red/green/blue values
4545 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4548 /* On frame F, return an array of XColor structures describing image
4549 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4550 means also fill the red/green/blue members of the XColor
4551 structures. Value is a pointer to the array of XColors structures,
4552 allocated with xmalloc; it must be freed by the caller. */
4555 x_to_xcolors (struct frame
*f
, struct image
*img
, bool rgb_p
)
4559 XImagePtr_or_DC ximg
;
4563 #endif /* HAVE_NTGUI */
4565 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *colors
/ img
->width
< img
->height
)
4566 memory_full (SIZE_MAX
);
4567 colors
= xmalloc (sizeof *colors
* img
->width
* img
->height
);
4569 /* Get the X image or create a memory device context for IMG. */
4570 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
4572 /* Fill the `pixel' members of the XColor array. I wished there
4573 were an easy and portable way to circumvent XGetPixel. */
4575 for (y
= 0; y
< img
->height
; ++y
)
4577 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
4579 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4580 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4582 x_query_colors (f
, row
, img
->width
);
4586 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4588 /* W32_TODO: palette support needed here? */
4589 p
->pixel
= GET_PIXEL (ximg
, x
, y
);
4592 p
->red
= RED16_FROM_ULONG (p
->pixel
);
4593 p
->green
= GREEN16_FROM_ULONG (p
->pixel
);
4594 p
->blue
= BLUE16_FROM_ULONG (p
->pixel
);
4597 #endif /* HAVE_X_WINDOWS */
4600 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
4607 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4608 created with CreateDIBSection, with the pointer to the bit values
4609 stored in ximg->data. */
4612 XPutPixel (XImagePtr ximg
, int x
, int y
, COLORREF color
)
4614 int width
= ximg
->info
.bmiHeader
.biWidth
;
4615 unsigned char * pixel
;
4617 /* True color images. */
4618 if (ximg
->info
.bmiHeader
.biBitCount
== 24)
4620 int rowbytes
= width
* 3;
4621 /* Ensure scanlines are aligned on 4 byte boundaries. */
4623 rowbytes
+= 4 - (rowbytes
% 4);
4625 pixel
= ximg
->data
+ y
* rowbytes
+ x
* 3;
4626 /* Windows bitmaps are in BGR order. */
4627 *pixel
= GetBValue (color
);
4628 *(pixel
+ 1) = GetGValue (color
);
4629 *(pixel
+ 2) = GetRValue (color
);
4631 /* Monochrome images. */
4632 else if (ximg
->info
.bmiHeader
.biBitCount
== 1)
4634 int rowbytes
= width
/ 8;
4635 /* Ensure scanlines are aligned on 4 byte boundaries. */
4637 rowbytes
+= 4 - (rowbytes
% 4);
4638 pixel
= ximg
->data
+ y
* rowbytes
+ x
/ 8;
4639 /* Filter out palette info. */
4640 if (color
& 0x00ffffff)
4641 *pixel
= *pixel
| (1 << x
% 8);
4643 *pixel
= *pixel
& ~(1 << x
% 8);
4646 image_error ("XPutPixel: palette image not supported", Qnil
, Qnil
);
4649 #endif /* HAVE_NTGUI */
4651 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4652 RGB members are set. F is the frame on which this all happens.
4653 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4656 x_from_xcolors (struct frame
*f
, struct image
*img
, XColor
*colors
)
4659 XImagePtr oimg
= NULL
;
4662 init_color_table ();
4664 x_clear_image_1 (f
, img
, CLEAR_IMAGE_PIXMAP
| CLEAR_IMAGE_COLORS
);
4665 image_create_x_image_and_pixmap (f
, img
, img
->width
, img
->height
, 0,
4668 for (y
= 0; y
< img
->height
; ++y
)
4669 for (x
= 0; x
< img
->width
; ++x
, ++p
)
4671 unsigned long pixel
;
4672 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
4673 XPutPixel (oimg
, x
, y
, pixel
);
4678 image_put_x_image (f
, img
, oimg
, 0);
4679 #ifdef COLOR_TABLE_SUPPORT
4680 img
->colors
= colors_in_color_table (&img
->ncolors
);
4681 free_color_table ();
4682 #endif /* COLOR_TABLE_SUPPORT */
4686 /* On frame F, perform edge-detection on image IMG.
4688 MATRIX is a nine-element array specifying the transformation
4689 matrix. See emboss_matrix for an example.
4691 COLOR_ADJUST is a color adjustment added to each pixel of the
4695 x_detect_edges (struct frame
*f
, struct image
*img
, int *matrix
, int color_adjust
)
4697 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4701 for (i
= sum
= 0; i
< 9; ++i
)
4702 sum
+= eabs (matrix
[i
]);
4704 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4706 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *new / img
->width
< img
->height
)
4707 memory_full (SIZE_MAX
);
4708 new = xmalloc (sizeof *new * img
->width
* img
->height
);
4710 for (y
= 0; y
< img
->height
; ++y
)
4712 p
= COLOR (new, 0, y
);
4713 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4714 p
= COLOR (new, img
->width
- 1, y
);
4715 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4718 for (x
= 1; x
< img
->width
- 1; ++x
)
4720 p
= COLOR (new, x
, 0);
4721 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4722 p
= COLOR (new, x
, img
->height
- 1);
4723 p
->red
= p
->green
= p
->blue
= 0xffff/2;
4726 for (y
= 1; y
< img
->height
- 1; ++y
)
4728 p
= COLOR (new, 1, y
);
4730 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
4732 int r
, g
, b
, yy
, xx
;
4735 for (yy
= y
- 1; yy
< y
+ 2; ++yy
)
4736 for (xx
= x
- 1; xx
< x
+ 2; ++xx
, ++i
)
4739 XColor
*t
= COLOR (colors
, xx
, yy
);
4740 r
+= matrix
[i
] * t
->red
;
4741 g
+= matrix
[i
] * t
->green
;
4742 b
+= matrix
[i
] * t
->blue
;
4745 r
= (r
/ sum
+ color_adjust
) & 0xffff;
4746 g
= (g
/ sum
+ color_adjust
) & 0xffff;
4747 b
= (b
/ sum
+ color_adjust
) & 0xffff;
4748 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
4753 x_from_xcolors (f
, img
, new);
4759 /* Perform the pre-defined `emboss' edge-detection on image IMG
4763 x_emboss (struct frame
*f
, struct image
*img
)
4765 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
4769 /* Transform image IMG which is used on frame F with a Laplace
4770 edge-detection algorithm. The result is an image that can be used
4771 to draw disabled buttons, for example. */
4774 x_laplace (struct frame
*f
, struct image
*img
)
4776 x_detect_edges (f
, img
, laplace_matrix
, 45000);
4780 /* Perform edge-detection on image IMG on frame F, with specified
4781 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
4783 MATRIX must be either
4785 - a list of at least 9 numbers in row-major form
4786 - a vector of at least 9 numbers
4788 COLOR_ADJUST nil means use a default; otherwise it must be a
4792 x_edge_detection (struct frame
*f
, struct image
*img
, Lisp_Object matrix
,
4793 Lisp_Object color_adjust
)
4801 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
4802 ++i
, matrix
= XCDR (matrix
))
4803 trans
[i
] = XFLOATINT (XCAR (matrix
));
4805 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
4807 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
4808 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
4811 if (NILP (color_adjust
))
4812 color_adjust
= make_number (0xffff / 2);
4814 if (i
== 9 && NUMBERP (color_adjust
))
4815 x_detect_edges (f
, img
, trans
, XFLOATINT (color_adjust
));
4819 /* Transform image IMG on frame F so that it looks disabled. */
4822 x_disable_image (struct frame
*f
, struct image
*img
)
4824 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
4826 int n_planes
= dpyinfo
->n_planes
* dpyinfo
->n_cbits
;
4828 int n_planes
= dpyinfo
->n_planes
;
4829 #endif /* HAVE_NTGUI */
4833 /* Color (or grayscale). Convert to gray, and equalize. Just
4834 drawing such images with a stipple can look very odd, so
4835 we're using this method instead. */
4836 XColor
*colors
= x_to_xcolors (f
, img
, 1);
4838 const int h
= 15000;
4839 const int l
= 30000;
4841 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
4845 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
4846 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
4847 p
->red
= p
->green
= p
->blue
= i2
;
4850 x_from_xcolors (f
, img
, colors
);
4853 /* Draw a cross over the disabled image, if we must or if we
4855 if (n_planes
< 2 || cross_disabled_images
)
4858 #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */
4860 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
4862 Display
*dpy
= FRAME_X_DISPLAY (f
);
4865 image_sync_to_pixmaps (f
, img
);
4866 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
4867 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
4868 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
4869 img
->width
- 1, img
->height
- 1);
4870 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
4876 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
4877 XSetForeground (dpy
, gc
, MaskForeground (f
));
4878 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
4879 img
->width
- 1, img
->height
- 1);
4880 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
4884 #endif /* !HAVE_NS */
4889 hdc
= get_frame_dc (f
);
4890 bmpdc
= CreateCompatibleDC (hdc
);
4891 release_frame_dc (f
, hdc
);
4893 prev
= SelectObject (bmpdc
, img
->pixmap
);
4895 SetTextColor (bmpdc
, BLACK_PIX_DEFAULT (f
));
4896 MoveToEx (bmpdc
, 0, 0, NULL
);
4897 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4898 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4899 LineTo (bmpdc
, img
->width
- 1, 0);
4903 SelectObject (bmpdc
, img
->mask
);
4904 SetTextColor (bmpdc
, WHITE_PIX_DEFAULT (f
));
4905 MoveToEx (bmpdc
, 0, 0, NULL
);
4906 LineTo (bmpdc
, img
->width
- 1, img
->height
- 1);
4907 MoveToEx (bmpdc
, 0, img
->height
- 1, NULL
);
4908 LineTo (bmpdc
, img
->width
- 1, 0);
4910 SelectObject (bmpdc
, prev
);
4912 #endif /* HAVE_NTGUI */
4917 /* Build a mask for image IMG which is used on frame F. FILE is the
4918 name of an image file, for error messages. HOW determines how to
4919 determine the background color of IMG. If it is a list '(R G B)',
4920 with R, G, and B being integers >= 0, take that as the color of the
4921 background. Otherwise, determine the background color of IMG
4925 x_build_heuristic_mask (struct frame
*f
, struct image
*img
, Lisp_Object how
)
4927 XImagePtr_or_DC ximg
;
4935 #endif /* HAVE_NTGUI */
4937 bool rc
, use_img_background
;
4938 unsigned long bg
= 0;
4941 x_clear_image_1 (f
, img
, CLEAR_IMAGE_MASK
);
4945 /* Create an image and pixmap serving as mask. */
4946 rc
= image_create_x_image_and_pixmap (f
, img
, img
->width
, img
->height
, 1,
4950 #endif /* !HAVE_NS */
4952 /* Create the bit array serving as mask. */
4953 row_width
= (img
->width
+ 7) / 8;
4954 mask_img
= xzalloc (row_width
* img
->height
);
4955 #endif /* HAVE_NTGUI */
4957 /* Get the X image or create a memory device context for IMG. */
4958 ximg
= image_get_x_image_or_dc (f
, img
, 0, &prev
);
4960 /* Determine the background color of ximg. If HOW is `(R G B)'
4961 take that as color. Otherwise, use the image's background color. */
4962 use_img_background
= 1;
4968 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
4970 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
4974 if (i
== 3 && NILP (how
))
4976 char color_name
[30];
4977 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
4980 0x00ffffff & /* Filter out palette info. */
4981 #endif /* HAVE_NTGUI */
4982 x_alloc_image_color (f
, img
, build_string (color_name
), 0));
4983 use_img_background
= 0;
4987 if (use_img_background
)
4988 bg
= four_corners_best (ximg
, img
->corners
, img
->width
, img
->height
);
4990 /* Set all bits in mask_img to 1 whose color in ximg is different
4991 from the background color bg. */
4993 for (y
= 0; y
< img
->height
; ++y
)
4994 for (x
= 0; x
< img
->width
; ++x
)
4996 XPutPixel (mask_img
, x
, y
, (XGetPixel (ximg
, x
, y
) != bg
4997 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
));
4999 if (XGetPixel (ximg
, x
, y
) == bg
)
5000 ns_set_alpha (ximg
, x
, y
, 0);
5001 #endif /* HAVE_NS */
5003 /* Fill in the background_transparent field while we have the mask handy. */
5004 image_background_transparent (img
, f
, mask_img
);
5006 /* Put mask_img into the image. */
5007 image_put_x_image (f
, img
, mask_img
, 1);
5008 #endif /* !HAVE_NS */
5010 for (y
= 0; y
< img
->height
; ++y
)
5011 for (x
= 0; x
< img
->width
; ++x
)
5013 COLORREF p
= GetPixel (ximg
, x
, y
);
5015 mask_img
[y
* row_width
+ x
/ 8] |= 1 << (x
% 8);
5018 /* Create the mask image. */
5019 img
->mask
= w32_create_pixmap_from_bitmap_data (img
->width
, img
->height
,
5021 /* Fill in the background_transparent field while we have the mask handy. */
5022 SelectObject (ximg
, img
->mask
);
5023 image_background_transparent (img
, f
, ximg
);
5025 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
5027 #endif /* HAVE_NTGUI */
5029 image_unget_x_image_or_dc (img
, 0, ximg
, prev
);
5033 /***********************************************************************
5034 PBM (mono, gray, color)
5035 ***********************************************************************/
5037 static bool pbm_image_p (Lisp_Object object
);
5038 static bool pbm_load (struct frame
*f
, struct image
*img
);
5040 /* The symbol `pbm' identifying images of this type. */
5042 static Lisp_Object Qpbm
;
5044 /* Indices of image specification fields in gs_format, below. */
5046 enum pbm_keyword_index
5062 /* Vector of image_keyword structures describing the format
5063 of valid user-defined image specifications. */
5065 static const struct image_keyword pbm_format
[PBM_LAST
] =
5067 {":type", IMAGE_SYMBOL_VALUE
, 1},
5068 {":file", IMAGE_STRING_VALUE
, 0},
5069 {":data", IMAGE_STRING_VALUE
, 0},
5070 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5071 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5072 {":relief", IMAGE_INTEGER_VALUE
, 0},
5073 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5074 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5075 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5076 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
5077 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5080 /* Structure describing the image type `pbm'. */
5082 static struct image_type pbm_type
=
5093 /* Return true if OBJECT is a valid PBM image specification. */
5096 pbm_image_p (Lisp_Object object
)
5098 struct image_keyword fmt
[PBM_LAST
];
5100 memcpy (fmt
, pbm_format
, sizeof fmt
);
5102 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
5105 /* Must specify either :data or :file. */
5106 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
5110 /* Scan a decimal number from *S and return it. Advance *S while
5111 reading the number. END is the end of the string. Value is -1 at
5115 pbm_scan_number (unsigned char **s
, unsigned char *end
)
5117 int c
= 0, val
= -1;
5121 /* Skip white-space. */
5122 while (*s
< end
&& (c
= *(*s
)++, c_isspace (c
)))
5127 /* Skip comment to end of line. */
5128 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
5131 else if (c_isdigit (c
))
5133 /* Read decimal number. */
5135 while (*s
< end
&& (c
= *(*s
)++, c_isdigit (c
)))
5136 val
= 10 * val
+ c
- '0';
5147 /* Load PBM image IMG for use on frame F. */
5150 pbm_load (struct frame
*f
, struct image
*img
)
5154 int width
, height
, max_color_idx
= 0;
5156 Lisp_Object file
, specified_file
;
5157 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
5158 unsigned char *contents
= NULL
;
5159 unsigned char *end
, *p
;
5162 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5164 if (STRINGP (specified_file
))
5166 file
= x_find_image_file (specified_file
);
5167 if (!STRINGP (file
))
5169 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5173 contents
= slurp_file (SSDATA (file
), &size
);
5174 if (contents
== NULL
)
5176 image_error ("Error reading `%s'", file
, Qnil
);
5181 end
= contents
+ size
;
5186 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5187 if (!STRINGP (data
))
5189 image_error ("Invalid image data `%s'", data
, Qnil
);
5193 end
= p
+ SBYTES (data
);
5196 /* Check magic number. */
5197 if (end
- p
< 2 || *p
++ != 'P')
5199 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5208 raw_p
= 0, type
= PBM_MONO
;
5212 raw_p
= 0, type
= PBM_GRAY
;
5216 raw_p
= 0, type
= PBM_COLOR
;
5220 raw_p
= 1, type
= PBM_MONO
;
5224 raw_p
= 1, type
= PBM_GRAY
;
5228 raw_p
= 1, type
= PBM_COLOR
;
5232 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
5236 /* Read width, height, maximum color-component. Characters
5237 starting with `#' up to the end of a line are ignored. */
5238 width
= pbm_scan_number (&p
, end
);
5239 height
= pbm_scan_number (&p
, end
);
5241 if (type
!= PBM_MONO
)
5243 max_color_idx
= pbm_scan_number (&p
, end
);
5244 if (max_color_idx
> 65535 || max_color_idx
< 0)
5246 image_error ("Unsupported maximum PBM color value", Qnil
, Qnil
);
5251 if (!check_image_size (f
, width
, height
))
5253 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5257 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
5260 /* Initialize the color hash table. */
5261 init_color_table ();
5263 if (type
== PBM_MONO
)
5266 struct image_keyword fmt
[PBM_LAST
];
5267 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
5268 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
5270 /* Parse the image specification. */
5271 memcpy (fmt
, pbm_format
, sizeof fmt
);
5272 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
5274 /* Get foreground and background colors, maybe allocate colors. */
5275 if (fmt
[PBM_FOREGROUND
].count
5276 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
5277 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
5278 if (fmt
[PBM_BACKGROUND
].count
5279 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
5281 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
5282 img
->background
= bg
;
5283 img
->background_valid
= 1;
5286 for (y
= 0; y
< height
; ++y
)
5287 for (x
= 0; x
< width
; ++x
)
5295 x_destroy_x_image (ximg
);
5296 x_clear_image (f
, img
);
5297 image_error ("Invalid image size in image `%s'",
5307 g
= pbm_scan_number (&p
, end
);
5309 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
5314 int expected_size
= height
* width
;
5315 if (max_color_idx
> 255)
5317 if (type
== PBM_COLOR
)
5320 if (raw_p
&& p
+ expected_size
> end
)
5322 x_destroy_x_image (ximg
);
5323 x_clear_image (f
, img
);
5324 image_error ("Invalid image size in image `%s'",
5329 for (y
= 0; y
< height
; ++y
)
5330 for (x
= 0; x
< width
; ++x
)
5334 if (type
== PBM_GRAY
&& raw_p
)
5337 if (max_color_idx
> 255)
5338 r
= g
= b
= r
* 256 + *p
++;
5340 else if (type
== PBM_GRAY
)
5341 r
= g
= b
= pbm_scan_number (&p
, end
);
5345 if (max_color_idx
> 255)
5348 if (max_color_idx
> 255)
5351 if (max_color_idx
> 255)
5356 r
= pbm_scan_number (&p
, end
);
5357 g
= pbm_scan_number (&p
, end
);
5358 b
= pbm_scan_number (&p
, end
);
5361 if (r
< 0 || g
< 0 || b
< 0)
5363 x_destroy_x_image (ximg
);
5364 image_error ("Invalid pixel value in image `%s'",
5369 /* RGB values are now in the range 0..max_color_idx.
5370 Scale this to the range 0..0xffff supported by X. */
5371 r
= (double) r
* 65535 / max_color_idx
;
5372 g
= (double) g
* 65535 / max_color_idx
;
5373 b
= (double) b
* 65535 / max_color_idx
;
5374 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5378 #ifdef COLOR_TABLE_SUPPORT
5379 /* Store in IMG->colors the colors allocated for the image, and
5380 free the color table. */
5381 img
->colors
= colors_in_color_table (&img
->ncolors
);
5382 free_color_table ();
5383 #endif /* COLOR_TABLE_SUPPORT */
5386 img
->height
= height
;
5388 /* Maybe fill in the background field while we have ximg handy. */
5390 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5391 /* Casting avoids a GCC warning. */
5392 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
5394 /* Put ximg into the image. */
5395 image_put_x_image (f
, img
, ximg
, 0);
5397 /* X and W32 versions did it here, MAC version above. ++kfs
5399 img->height = height; */
5406 /***********************************************************************
5408 ***********************************************************************/
5410 #if defined (HAVE_PNG) || defined (HAVE_NS)
5412 /* Function prototypes. */
5414 static bool png_image_p (Lisp_Object object
);
5415 static bool png_load (struct frame
*f
, struct image
*img
);
5417 /* The symbol `png' identifying images of this type. */
5419 static Lisp_Object Qpng
;
5421 /* Indices of image specification fields in png_format, below. */
5423 enum png_keyword_index
5438 /* Vector of image_keyword structures describing the format
5439 of valid user-defined image specifications. */
5441 static const struct image_keyword png_format
[PNG_LAST
] =
5443 {":type", IMAGE_SYMBOL_VALUE
, 1},
5444 {":data", IMAGE_STRING_VALUE
, 0},
5445 {":file", IMAGE_STRING_VALUE
, 0},
5446 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5447 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
5448 {":relief", IMAGE_INTEGER_VALUE
, 0},
5449 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5450 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5451 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5452 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
5455 #if defined HAVE_NTGUI && defined WINDOWSNT
5456 static bool init_png_functions (void);
5458 #define init_png_functions NULL
5461 /* Structure describing the image type `png'. */
5463 static struct image_type png_type
=
5473 /* Return true if OBJECT is a valid PNG image specification. */
5476 png_image_p (Lisp_Object object
)
5478 struct image_keyword fmt
[PNG_LAST
];
5479 memcpy (fmt
, png_format
, sizeof fmt
);
5481 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
5484 /* Must specify either the :data or :file keyword. */
5485 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
5488 #endif /* HAVE_PNG || HAVE_NS */
5494 /* PNG library details. */
5496 DEF_IMGLIB_FN (png_voidp
, png_get_io_ptr
, (png_structp
));
5497 DEF_IMGLIB_FN (int, png_sig_cmp
, (png_bytep
, png_size_t
, png_size_t
));
5498 DEF_IMGLIB_FN (png_structp
, png_create_read_struct
, (png_const_charp
, png_voidp
,
5499 png_error_ptr
, png_error_ptr
));
5500 DEF_IMGLIB_FN (png_infop
, png_create_info_struct
, (png_structp
));
5501 DEF_IMGLIB_FN (void, png_destroy_read_struct
, (png_structpp
, png_infopp
, png_infopp
));
5502 DEF_IMGLIB_FN (void, png_set_read_fn
, (png_structp
, png_voidp
, png_rw_ptr
));
5503 DEF_IMGLIB_FN (void, png_set_sig_bytes
, (png_structp
, int));
5504 DEF_IMGLIB_FN (void, png_read_info
, (png_structp
, png_infop
));
5505 DEF_IMGLIB_FN (png_uint_32
, png_get_IHDR
, (png_structp
, png_infop
,
5506 png_uint_32
*, png_uint_32
*,
5507 int *, int *, int *, int *, int *));
5508 DEF_IMGLIB_FN (png_uint_32
, png_get_valid
, (png_structp
, png_infop
, png_uint_32
));
5509 DEF_IMGLIB_FN (void, png_set_strip_16
, (png_structp
));
5510 DEF_IMGLIB_FN (void, png_set_expand
, (png_structp
));
5511 DEF_IMGLIB_FN (void, png_set_gray_to_rgb
, (png_structp
));
5512 DEF_IMGLIB_FN (void, png_set_background
, (png_structp
, png_color_16p
,
5514 DEF_IMGLIB_FN (png_uint_32
, png_get_bKGD
, (png_structp
, png_infop
, png_color_16p
*));
5515 DEF_IMGLIB_FN (void, png_read_update_info
, (png_structp
, png_infop
));
5516 DEF_IMGLIB_FN (png_byte
, png_get_channels
, (png_structp
, png_infop
));
5517 DEF_IMGLIB_FN (png_size_t
, png_get_rowbytes
, (png_structp
, png_infop
));
5518 DEF_IMGLIB_FN (void, png_read_image
, (png_structp
, png_bytepp
));
5519 DEF_IMGLIB_FN (void, png_read_end
, (png_structp
, png_infop
));
5520 DEF_IMGLIB_FN (void, png_error
, (png_structp
, png_const_charp
));
5522 #if (PNG_LIBPNG_VER >= 10500)
5523 DEF_IMGLIB_FN (void, png_longjmp
, (png_structp
, int));
5524 DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn
, (png_structp
, png_longjmp_ptr
, size_t));
5525 #endif /* libpng version >= 1.5 */
5528 init_png_functions (void)
5532 if (!(library
= w32_delayed_load (Qpng
)))
5535 LOAD_IMGLIB_FN (library
, png_get_io_ptr
);
5536 LOAD_IMGLIB_FN (library
, png_sig_cmp
);
5537 LOAD_IMGLIB_FN (library
, png_create_read_struct
);
5538 LOAD_IMGLIB_FN (library
, png_create_info_struct
);
5539 LOAD_IMGLIB_FN (library
, png_destroy_read_struct
);
5540 LOAD_IMGLIB_FN (library
, png_set_read_fn
);
5541 LOAD_IMGLIB_FN (library
, png_set_sig_bytes
);
5542 LOAD_IMGLIB_FN (library
, png_read_info
);
5543 LOAD_IMGLIB_FN (library
, png_get_IHDR
);
5544 LOAD_IMGLIB_FN (library
, png_get_valid
);
5545 LOAD_IMGLIB_FN (library
, png_set_strip_16
);
5546 LOAD_IMGLIB_FN (library
, png_set_expand
);
5547 LOAD_IMGLIB_FN (library
, png_set_gray_to_rgb
);
5548 LOAD_IMGLIB_FN (library
, png_set_background
);
5549 LOAD_IMGLIB_FN (library
, png_get_bKGD
);
5550 LOAD_IMGLIB_FN (library
, png_read_update_info
);
5551 LOAD_IMGLIB_FN (library
, png_get_channels
);
5552 LOAD_IMGLIB_FN (library
, png_get_rowbytes
);
5553 LOAD_IMGLIB_FN (library
, png_read_image
);
5554 LOAD_IMGLIB_FN (library
, png_read_end
);
5555 LOAD_IMGLIB_FN (library
, png_error
);
5557 #if (PNG_LIBPNG_VER >= 10500)
5558 LOAD_IMGLIB_FN (library
, png_longjmp
);
5559 LOAD_IMGLIB_FN (library
, png_set_longjmp_fn
);
5560 #endif /* libpng version >= 1.5 */
5566 #define fn_png_get_io_ptr png_get_io_ptr
5567 #define fn_png_sig_cmp png_sig_cmp
5568 #define fn_png_create_read_struct png_create_read_struct
5569 #define fn_png_create_info_struct png_create_info_struct
5570 #define fn_png_destroy_read_struct png_destroy_read_struct
5571 #define fn_png_set_read_fn png_set_read_fn
5572 #define fn_png_set_sig_bytes png_set_sig_bytes
5573 #define fn_png_read_info png_read_info
5574 #define fn_png_get_IHDR png_get_IHDR
5575 #define fn_png_get_valid png_get_valid
5576 #define fn_png_set_strip_16 png_set_strip_16
5577 #define fn_png_set_expand png_set_expand
5578 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5579 #define fn_png_set_background png_set_background
5580 #define fn_png_get_bKGD png_get_bKGD
5581 #define fn_png_read_update_info png_read_update_info
5582 #define fn_png_get_channels png_get_channels
5583 #define fn_png_get_rowbytes png_get_rowbytes
5584 #define fn_png_read_image png_read_image
5585 #define fn_png_read_end png_read_end
5586 #define fn_png_error png_error
5588 #if (PNG_LIBPNG_VER >= 10500)
5589 #define fn_png_longjmp png_longjmp
5590 #define fn_png_set_longjmp_fn png_set_longjmp_fn
5591 #endif /* libpng version >= 1.5 */
5593 #endif /* WINDOWSNT */
5595 /* Possibly inefficient/inexact substitutes for _setjmp and _longjmp.
5596 Do not use sys_setjmp, as PNG supports only jmp_buf. The _longjmp
5597 substitute may munge the signal mask, but that should be OK here.
5598 MinGW (MS-Windows) uses _setjmp and defines setjmp to _setjmp in
5599 the system header setjmp.h; don't mess up that. */
5600 #ifndef HAVE__SETJMP
5601 # define _setjmp(j) setjmp (j)
5602 # define _longjmp longjmp
5605 #if (PNG_LIBPNG_VER < 10500)
5606 #define PNG_LONGJMP(ptr) (_longjmp ((ptr)->jmpbuf, 1))
5607 #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf)
5609 /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */
5610 #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1))
5611 #define PNG_JMPBUF(ptr) \
5612 (*fn_png_set_longjmp_fn ((ptr), _longjmp, sizeof (jmp_buf)))
5615 /* Error and warning handlers installed when the PNG library
5618 static _Noreturn
void
5619 my_png_error (png_struct
*png_ptr
, const char *msg
)
5621 eassert (png_ptr
!= NULL
);
5622 /* Avoid compiler warning about deprecated direct access to
5623 png_ptr's fields in libpng versions 1.4.x. */
5624 image_error ("PNG error: %s", build_string (msg
), Qnil
);
5625 PNG_LONGJMP (png_ptr
);
5630 my_png_warning (png_struct
*png_ptr
, const char *msg
)
5632 eassert (png_ptr
!= NULL
);
5633 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
5636 /* Memory source for PNG decoding. */
5638 struct png_memory_storage
5640 unsigned char *bytes
; /* The data */
5641 ptrdiff_t len
; /* How big is it? */
5642 ptrdiff_t index
; /* Where are we? */
5646 /* Function set as reader function when reading PNG image from memory.
5647 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5648 bytes from the input to DATA. */
5651 png_read_from_memory (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5653 struct png_memory_storage
*tbr
5654 = (struct png_memory_storage
*) fn_png_get_io_ptr (png_ptr
);
5656 if (length
> tbr
->len
- tbr
->index
)
5657 fn_png_error (png_ptr
, "Read error");
5659 memcpy (data
, tbr
->bytes
+ tbr
->index
, length
);
5660 tbr
->index
= tbr
->index
+ length
;
5664 /* Function set as reader function when reading PNG image from a file.
5665 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5666 bytes from the input to DATA. */
5669 png_read_from_file (png_structp png_ptr
, png_bytep data
, png_size_t length
)
5671 FILE *fp
= (FILE *) fn_png_get_io_ptr (png_ptr
);
5673 if (fread (data
, 1, length
, fp
) < length
)
5674 fn_png_error (png_ptr
, "Read error");
5678 /* Load PNG image IMG for use on frame F. Value is true if
5681 struct png_load_context
5683 /* These are members so that longjmp doesn't munge local variables. */
5684 png_struct
*png_ptr
;
5693 png_load_body (struct frame
*f
, struct image
*img
, struct png_load_context
*c
)
5695 Lisp_Object file
, specified_file
;
5696 Lisp_Object specified_data
;
5699 XImagePtr ximg
, mask_img
= NULL
;
5700 png_struct
*png_ptr
;
5701 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
5704 png_byte
*pixels
= NULL
;
5705 png_byte
**rows
= NULL
;
5706 png_uint_32 width
, height
;
5707 int bit_depth
, color_type
, interlace_type
;
5709 png_uint_32 row_bytes
;
5711 struct png_memory_storage tbr
; /* Data to be read */
5713 /* Find out what file to load. */
5714 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
5715 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
5717 if (NILP (specified_data
))
5719 file
= x_find_image_file (specified_file
);
5720 if (!STRINGP (file
))
5722 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
5726 /* Open the image file. */
5727 fp
= fopen (SSDATA (file
), "rb");
5730 image_error ("Cannot open image file `%s'", file
, Qnil
);
5734 /* Check PNG signature. */
5735 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
5736 || fn_png_sig_cmp (sig
, 0, sizeof sig
))
5738 image_error ("Not a PNG file: `%s'", file
, Qnil
);
5745 if (!STRINGP (specified_data
))
5747 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
5751 /* Read from memory. */
5752 tbr
.bytes
= SDATA (specified_data
);
5753 tbr
.len
= SBYTES (specified_data
);
5756 /* Check PNG signature. */
5757 if (tbr
.len
< sizeof sig
5758 || fn_png_sig_cmp (tbr
.bytes
, 0, sizeof sig
))
5760 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
5764 /* Need to skip past the signature. */
5765 tbr
.bytes
+= sizeof (sig
);
5768 /* Initialize read and info structs for PNG lib. */
5769 png_ptr
= fn_png_create_read_struct (PNG_LIBPNG_VER_STRING
,
5774 info_ptr
= fn_png_create_info_struct (png_ptr
);
5775 end_info
= fn_png_create_info_struct (png_ptr
);
5778 c
->png_ptr
= png_ptr
;
5779 c
->info_ptr
= info_ptr
;
5780 c
->end_info
= end_info
;
5785 if (! (info_ptr
&& end_info
))
5787 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5792 if (fp
) fclose (fp
);
5796 /* Set error jump-back. We come back here when the PNG library
5797 detects an error. */
5798 if (_setjmp (PNG_JMPBUF (png_ptr
)))
5802 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
5810 /* Silence a bogus diagnostic; see GCC bug 54561. */
5811 IF_LINT (fp
= c
->fp
);
5813 /* Read image info. */
5814 if (!NILP (specified_data
))
5815 fn_png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
5817 fn_png_set_read_fn (png_ptr
, (void *) fp
, png_read_from_file
);
5819 fn_png_set_sig_bytes (png_ptr
, sizeof sig
);
5820 fn_png_read_info (png_ptr
, info_ptr
);
5821 fn_png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
5822 &interlace_type
, NULL
, NULL
);
5824 if (! (width
<= INT_MAX
&& height
<= INT_MAX
5825 && check_image_size (f
, width
, height
)))
5827 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
5831 /* Create the X image and pixmap now, so that the work below can be
5832 omitted if the image is too large for X. */
5833 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
5836 /* If image contains simply transparency data, we prefer to
5837 construct a clipping mask. */
5838 if (fn_png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
5843 /* This function is easier to write if we only have to handle
5844 one data format: RGB or RGBA with 8 bits per channel. Let's
5845 transform other formats into that format. */
5847 /* Strip more than 8 bits per channel. */
5848 if (bit_depth
== 16)
5849 fn_png_set_strip_16 (png_ptr
);
5851 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
5853 fn_png_set_expand (png_ptr
);
5855 /* Convert grayscale images to RGB. */
5856 if (color_type
== PNG_COLOR_TYPE_GRAY
5857 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
5858 fn_png_set_gray_to_rgb (png_ptr
);
5860 /* Handle alpha channel by combining the image with a background
5861 color. Do this only if a real alpha channel is supplied. For
5862 simple transparency, we prefer a clipping mask. */
5865 /* png_color_16 *image_bg; */
5866 Lisp_Object specified_bg
5867 = image_spec_value (img
->spec
, QCbackground
, NULL
);
5868 int shift
= (bit_depth
== 16) ? 0 : 8;
5870 if (STRINGP (specified_bg
))
5871 /* The user specified `:background', use that. */
5874 if (x_defined_color (f
, SSDATA (specified_bg
), &color
, 0))
5876 png_color_16 user_bg
;
5878 memset (&user_bg
, 0, sizeof user_bg
);
5879 user_bg
.red
= color
.red
>> shift
;
5880 user_bg
.green
= color
.green
>> shift
;
5881 user_bg
.blue
= color
.blue
>> shift
;
5883 fn_png_set_background (png_ptr
, &user_bg
,
5884 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5889 /* We use the current frame background, ignoring any default
5890 background color set by the image. */
5891 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
5893 png_color_16 frame_background
;
5895 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
5896 x_query_color (f
, &color
);
5898 memset (&frame_background
, 0, sizeof frame_background
);
5899 frame_background
.red
= color
.red
>> shift
;
5900 frame_background
.green
= color
.green
>> shift
;
5901 frame_background
.blue
= color
.blue
>> shift
;
5902 #endif /* HAVE_X_WINDOWS */
5904 fn_png_set_background (png_ptr
, &frame_background
,
5905 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
5909 /* Update info structure. */
5910 fn_png_read_update_info (png_ptr
, info_ptr
);
5912 /* Get number of channels. Valid values are 1 for grayscale images
5913 and images with a palette, 2 for grayscale images with transparency
5914 information (alpha channel), 3 for RGB images, and 4 for RGB
5915 images with alpha channel, i.e. RGBA. If conversions above were
5916 sufficient we should only have 3 or 4 channels here. */
5917 channels
= fn_png_get_channels (png_ptr
, info_ptr
);
5918 eassert (channels
== 3 || channels
== 4);
5920 /* Number of bytes needed for one row of the image. */
5921 row_bytes
= fn_png_get_rowbytes (png_ptr
, info_ptr
);
5923 /* Allocate memory for the image. */
5924 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *rows
< height
5925 || min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *pixels
/ height
< row_bytes
)
5926 memory_full (SIZE_MAX
);
5927 c
->pixels
= pixels
= xmalloc (sizeof *pixels
* row_bytes
* height
);
5928 c
->rows
= rows
= xmalloc (height
* sizeof *rows
);
5929 for (i
= 0; i
< height
; ++i
)
5930 rows
[i
] = pixels
+ i
* row_bytes
;
5932 /* Read the entire image. */
5933 fn_png_read_image (png_ptr
, rows
);
5934 fn_png_read_end (png_ptr
, info_ptr
);
5941 /* Create an image and pixmap serving as mask if the PNG image
5942 contains an alpha channel. */
5945 && !image_create_x_image_and_pixmap (f
, img
, width
, height
, 1,
5948 x_destroy_x_image (ximg
);
5949 x_clear_image_1 (f
, img
, CLEAR_IMAGE_PIXMAP
);
5953 /* Fill the X image and mask from PNG data. */
5954 init_color_table ();
5956 for (y
= 0; y
< height
; ++y
)
5958 png_byte
*p
= rows
[y
];
5960 for (x
= 0; x
< width
; ++x
)
5967 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
5968 /* An alpha channel, aka mask channel, associates variable
5969 transparency with an image. Where other image formats
5970 support binary transparency---fully transparent or fully
5971 opaque---PNG allows up to 254 levels of partial transparency.
5972 The PNG library implements partial transparency by combining
5973 the image with a specified background color.
5975 I'm not sure how to handle this here nicely: because the
5976 background on which the image is displayed may change, for
5977 real alpha channel support, it would be necessary to create
5978 a new image for each possible background.
5980 What I'm doing now is that a mask is created if we have
5981 boolean transparency information. Otherwise I'm using
5982 the frame's background color to combine the image with. */
5987 XPutPixel (mask_img
, x
, y
, *p
> 0 ? PIX_MASK_DRAW
: PIX_MASK_RETAIN
);
5993 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
5994 /* Set IMG's background color from the PNG image, unless the user
5998 if (fn_png_get_bKGD (png_ptr
, info_ptr
, &bg
))
6000 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
6001 img
->background_valid
= 1;
6005 #ifdef COLOR_TABLE_SUPPORT
6006 /* Remember colors allocated for this image. */
6007 img
->colors
= colors_in_color_table (&img
->ncolors
);
6008 free_color_table ();
6009 #endif /* COLOR_TABLE_SUPPORT */
6012 fn_png_destroy_read_struct (&c
->png_ptr
, &c
->info_ptr
, &c
->end_info
);
6017 img
->height
= height
;
6019 /* Maybe fill in the background field while we have ximg handy.
6020 Casting avoids a GCC warning. */
6021 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6023 /* Put ximg into the image. */
6024 image_put_x_image (f
, img
, ximg
, 0);
6026 /* Same for the mask. */
6029 /* Fill in the background_transparent field while we have the
6030 mask handy. Casting avoids a GCC warning. */
6031 image_background_transparent (img
, f
, (XImagePtr_or_DC
)mask_img
);
6033 image_put_x_image (f
, img
, mask_img
, 1);
6040 png_load (struct frame
*f
, struct image
*img
)
6042 struct png_load_context c
;
6043 return png_load_body (f
, img
, &c
);
6046 #else /* HAVE_PNG */
6050 png_load (struct frame
*f
, struct image
*img
)
6052 return ns_load_image (f
, img
,
6053 image_spec_value (img
->spec
, QCfile
, NULL
),
6054 image_spec_value (img
->spec
, QCdata
, NULL
));
6056 #endif /* HAVE_NS */
6059 #endif /* !HAVE_PNG */
6063 /***********************************************************************
6065 ***********************************************************************/
6067 #if defined (HAVE_JPEG) || defined (HAVE_NS)
6069 static bool jpeg_image_p (Lisp_Object object
);
6070 static bool jpeg_load (struct frame
*f
, struct image
*img
);
6072 /* The symbol `jpeg' identifying images of this type. */
6074 static Lisp_Object Qjpeg
;
6076 /* Indices of image specification fields in gs_format, below. */
6078 enum jpeg_keyword_index
6087 JPEG_HEURISTIC_MASK
,
6093 /* Vector of image_keyword structures describing the format
6094 of valid user-defined image specifications. */
6096 static const struct image_keyword jpeg_format
[JPEG_LAST
] =
6098 {":type", IMAGE_SYMBOL_VALUE
, 1},
6099 {":data", IMAGE_STRING_VALUE
, 0},
6100 {":file", IMAGE_STRING_VALUE
, 0},
6101 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6102 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6103 {":relief", IMAGE_INTEGER_VALUE
, 0},
6104 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6105 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6106 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6107 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6110 #if defined HAVE_NTGUI && defined WINDOWSNT
6111 static bool init_jpeg_functions (void);
6113 #define init_jpeg_functions NULL
6116 /* Structure describing the image type `jpeg'. */
6118 static struct image_type jpeg_type
=
6124 init_jpeg_functions
,
6128 /* Return true if OBJECT is a valid JPEG image specification. */
6131 jpeg_image_p (Lisp_Object object
)
6133 struct image_keyword fmt
[JPEG_LAST
];
6135 memcpy (fmt
, jpeg_format
, sizeof fmt
);
6137 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
6140 /* Must specify either the :data or :file keyword. */
6141 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
6144 #endif /* HAVE_JPEG || HAVE_NS */
6148 /* Work around a warning about HAVE_STDLIB_H being redefined in
6150 #ifdef HAVE_STDLIB_H
6151 #undef HAVE_STDLIB_H
6152 #endif /* HAVE_STLIB_H */
6154 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6155 /* In older releases of the jpeg library, jpeglib.h will define boolean
6156 differently depending on __WIN32__, so make sure it is defined. */
6160 /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types.
6161 Some versions of jpeglib try to detect whether rpcndr.h is loaded,
6162 using the Windows boolean type instead of the jpeglib boolean type
6163 if so. Cygwin jpeglib, however, doesn't try to detect whether its
6164 headers are included along with windows.h, so under Cygwin, jpeglib
6165 attempts to define a conflicting boolean type. Worse, forcing
6166 Cygwin jpeglib headers to use the Windows boolean type doesn't work
6167 because it created an ABI incompatibility between the
6168 already-compiled jpeg library and the header interface definition.
6170 The best we can do is to define jpeglib's boolean type to a
6171 different name. This name, jpeg_boolean, remains in effect through
6172 the rest of image.c.
6174 #if defined CYGWIN && defined HAVE_NTGUI
6175 #define boolean jpeg_boolean
6177 #include <jpeglib.h>
6182 /* JPEG library details. */
6183 DEF_IMGLIB_FN (void, jpeg_CreateDecompress
, (j_decompress_ptr
, int, size_t));
6184 DEF_IMGLIB_FN (boolean
, jpeg_start_decompress
, (j_decompress_ptr
));
6185 DEF_IMGLIB_FN (boolean
, jpeg_finish_decompress
, (j_decompress_ptr
));
6186 DEF_IMGLIB_FN (void, jpeg_destroy_decompress
, (j_decompress_ptr
));
6187 DEF_IMGLIB_FN (int, jpeg_read_header
, (j_decompress_ptr
, boolean
));
6188 DEF_IMGLIB_FN (JDIMENSION
, jpeg_read_scanlines
, (j_decompress_ptr
, JSAMPARRAY
, JDIMENSION
));
6189 DEF_IMGLIB_FN (struct jpeg_error_mgr
*, jpeg_std_error
, (struct jpeg_error_mgr
*));
6190 DEF_IMGLIB_FN (boolean
, jpeg_resync_to_restart
, (j_decompress_ptr
, int));
6193 init_jpeg_functions (void)
6197 if (!(library
= w32_delayed_load (Qjpeg
)))
6200 LOAD_IMGLIB_FN (library
, jpeg_finish_decompress
);
6201 LOAD_IMGLIB_FN (library
, jpeg_read_scanlines
);
6202 LOAD_IMGLIB_FN (library
, jpeg_start_decompress
);
6203 LOAD_IMGLIB_FN (library
, jpeg_read_header
);
6204 LOAD_IMGLIB_FN (library
, jpeg_CreateDecompress
);
6205 LOAD_IMGLIB_FN (library
, jpeg_destroy_decompress
);
6206 LOAD_IMGLIB_FN (library
, jpeg_std_error
);
6207 LOAD_IMGLIB_FN (library
, jpeg_resync_to_restart
);
6211 /* Wrapper since we can't directly assign the function pointer
6212 to another function pointer that was declared more completely easily. */
6214 jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo
, int desired
)
6216 return fn_jpeg_resync_to_restart (cinfo
, desired
);
6221 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a)
6222 #define fn_jpeg_start_decompress jpeg_start_decompress
6223 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6224 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6225 #define fn_jpeg_read_header jpeg_read_header
6226 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6227 #define fn_jpeg_std_error jpeg_std_error
6228 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6230 #endif /* WINDOWSNT */
6232 struct my_jpeg_error_mgr
6234 struct jpeg_error_mgr pub
;
6235 sys_jmp_buf setjmp_buffer
;
6237 /* The remaining members are so that longjmp doesn't munge local
6239 struct jpeg_decompress_struct cinfo
;
6243 MY_JPEG_INVALID_IMAGE_SIZE
,
6244 MY_JPEG_CANNOT_CREATE_X
6252 static _Noreturn
void
6253 my_error_exit (j_common_ptr cinfo
)
6255 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
6256 mgr
->failure_code
= MY_JPEG_ERROR_EXIT
;
6257 sys_longjmp (mgr
->setjmp_buffer
, 1);
6261 /* Init source method for JPEG data source manager. Called by
6262 jpeg_read_header() before any data is actually read. See
6263 libjpeg.doc from the JPEG lib distribution. */
6266 our_common_init_source (j_decompress_ptr cinfo
)
6271 /* Method to terminate data source. Called by
6272 jpeg_finish_decompress() after all data has been processed. */
6275 our_common_term_source (j_decompress_ptr cinfo
)
6280 /* Fill input buffer method for JPEG data source manager. Called
6281 whenever more data is needed. We read the whole image in one step,
6282 so this only adds a fake end of input marker at the end. */
6284 static JOCTET our_memory_buffer
[2];
6287 our_memory_fill_input_buffer (j_decompress_ptr cinfo
)
6289 /* Insert a fake EOI marker. */
6290 struct jpeg_source_mgr
*src
= cinfo
->src
;
6292 our_memory_buffer
[0] = (JOCTET
) 0xFF;
6293 our_memory_buffer
[1] = (JOCTET
) JPEG_EOI
;
6295 src
->next_input_byte
= our_memory_buffer
;
6296 src
->bytes_in_buffer
= 2;
6301 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6302 is the JPEG data source manager. */
6305 our_memory_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6307 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6311 if (num_bytes
> src
->bytes_in_buffer
)
6312 ERREXIT (cinfo
, JERR_INPUT_EOF
);
6314 src
->bytes_in_buffer
-= num_bytes
;
6315 src
->next_input_byte
+= num_bytes
;
6320 /* Set up the JPEG lib for reading an image from DATA which contains
6321 LEN bytes. CINFO is the decompression info structure created for
6322 reading the image. */
6325 jpeg_memory_src (j_decompress_ptr cinfo
, JOCTET
*data
, ptrdiff_t len
)
6327 struct jpeg_source_mgr
*src
;
6329 if (cinfo
->src
== NULL
)
6331 /* First time for this JPEG object? */
6332 cinfo
->src
= (struct jpeg_source_mgr
*)
6333 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6334 sizeof (struct jpeg_source_mgr
));
6335 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6336 src
->next_input_byte
= data
;
6339 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
6340 src
->init_source
= our_common_init_source
;
6341 src
->fill_input_buffer
= our_memory_fill_input_buffer
;
6342 src
->skip_input_data
= our_memory_skip_input_data
;
6343 src
->resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6344 src
->term_source
= our_common_term_source
;
6345 src
->bytes_in_buffer
= len
;
6346 src
->next_input_byte
= data
;
6350 struct jpeg_stdio_mgr
6352 struct jpeg_source_mgr mgr
;
6359 /* Size of buffer to read JPEG from file.
6360 Not too big, as we want to use alloc_small. */
6361 #define JPEG_STDIO_BUFFER_SIZE 8192
6364 /* Fill input buffer method for JPEG data source manager. Called
6365 whenever more data is needed. The data is read from a FILE *. */
6368 our_stdio_fill_input_buffer (j_decompress_ptr cinfo
)
6370 struct jpeg_stdio_mgr
*src
;
6372 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6377 bytes
= fread (src
->buffer
, 1, JPEG_STDIO_BUFFER_SIZE
, src
->file
);
6379 src
->mgr
.bytes_in_buffer
= bytes
;
6382 WARNMS (cinfo
, JWRN_JPEG_EOF
);
6384 src
->buffer
[0] = (JOCTET
) 0xFF;
6385 src
->buffer
[1] = (JOCTET
) JPEG_EOI
;
6386 src
->mgr
.bytes_in_buffer
= 2;
6388 src
->mgr
.next_input_byte
= src
->buffer
;
6395 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6396 is the JPEG data source manager. */
6399 our_stdio_skip_input_data (j_decompress_ptr cinfo
, long int num_bytes
)
6401 struct jpeg_stdio_mgr
*src
;
6402 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6404 while (num_bytes
> 0 && !src
->finished
)
6406 if (num_bytes
<= src
->mgr
.bytes_in_buffer
)
6408 src
->mgr
.bytes_in_buffer
-= num_bytes
;
6409 src
->mgr
.next_input_byte
+= num_bytes
;
6414 num_bytes
-= src
->mgr
.bytes_in_buffer
;
6415 src
->mgr
.bytes_in_buffer
= 0;
6416 src
->mgr
.next_input_byte
= NULL
;
6418 our_stdio_fill_input_buffer (cinfo
);
6424 /* Set up the JPEG lib for reading an image from a FILE *.
6425 CINFO is the decompression info structure created for
6426 reading the image. */
6429 jpeg_file_src (j_decompress_ptr cinfo
, FILE *fp
)
6431 struct jpeg_stdio_mgr
*src
;
6433 if (cinfo
->src
!= NULL
)
6434 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6437 /* First time for this JPEG object? */
6438 cinfo
->src
= (struct jpeg_source_mgr
*)
6439 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6440 sizeof (struct jpeg_stdio_mgr
));
6441 src
= (struct jpeg_stdio_mgr
*) cinfo
->src
;
6442 src
->buffer
= (JOCTET
*)
6443 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
6444 JPEG_STDIO_BUFFER_SIZE
);
6449 src
->mgr
.init_source
= our_common_init_source
;
6450 src
->mgr
.fill_input_buffer
= our_stdio_fill_input_buffer
;
6451 src
->mgr
.skip_input_data
= our_stdio_skip_input_data
;
6452 src
->mgr
.resync_to_restart
= jpeg_resync_to_restart_wrapper
; /* Use default method. */
6453 src
->mgr
.term_source
= our_common_term_source
;
6454 src
->mgr
.bytes_in_buffer
= 0;
6455 src
->mgr
.next_input_byte
= NULL
;
6459 /* Load image IMG for use on frame F. Patterned after example.c
6460 from the JPEG lib. */
6463 jpeg_load_body (struct frame
*f
, struct image
*img
,
6464 struct my_jpeg_error_mgr
*mgr
)
6466 Lisp_Object file
, specified_file
;
6467 Lisp_Object specified_data
;
6470 int row_stride
, x
, y
;
6471 XImagePtr ximg
= NULL
;
6472 unsigned long *colors
;
6475 /* Open the JPEG file. */
6476 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6477 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6479 if (NILP (specified_data
))
6481 file
= x_find_image_file (specified_file
);
6482 if (!STRINGP (file
))
6484 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6488 fp
= fopen (SSDATA (file
), "rb");
6491 image_error ("Cannot open `%s'", file
, Qnil
);
6495 else if (!STRINGP (specified_data
))
6497 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6501 IF_LINT (mgr
->fp
= fp
);
6503 /* Customize libjpeg's error handling to call my_error_exit when an
6504 error is detected. This function will perform a longjmp. */
6505 mgr
->cinfo
.err
= fn_jpeg_std_error (&mgr
->pub
);
6506 mgr
->pub
.error_exit
= my_error_exit
;
6507 if (sys_setjmp (mgr
->setjmp_buffer
))
6509 switch (mgr
->failure_code
)
6511 case MY_JPEG_ERROR_EXIT
:
6513 char buf
[JMSG_LENGTH_MAX
];
6514 mgr
->cinfo
.err
->format_message ((j_common_ptr
) &mgr
->cinfo
, buf
);
6515 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
6516 build_string (buf
));
6520 case MY_JPEG_INVALID_IMAGE_SIZE
:
6521 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
6524 case MY_JPEG_CANNOT_CREATE_X
:
6528 /* Close the input file and destroy the JPEG object. */
6531 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6533 /* If we already have an XImage, free that. */
6534 x_destroy_x_image (ximg
);
6536 /* Free pixmap and colors. */
6537 x_clear_image (f
, img
);
6541 /* Silence a bogus diagnostic; see GCC bug 54561. */
6542 IF_LINT (fp
= mgr
->fp
);
6544 /* Create the JPEG decompression object. Let it read from fp.
6545 Read the JPEG image header. */
6546 fn_jpeg_CreateDecompress (&mgr
->cinfo
, JPEG_LIB_VERSION
, sizeof *&mgr
->cinfo
);
6548 if (NILP (specified_data
))
6549 jpeg_file_src (&mgr
->cinfo
, fp
);
6551 jpeg_memory_src (&mgr
->cinfo
, SDATA (specified_data
),
6552 SBYTES (specified_data
));
6554 fn_jpeg_read_header (&mgr
->cinfo
, 1);
6556 /* Customize decompression so that color quantization will be used.
6557 Start decompression. */
6558 mgr
->cinfo
.quantize_colors
= 1;
6559 fn_jpeg_start_decompress (&mgr
->cinfo
);
6560 width
= img
->width
= mgr
->cinfo
.output_width
;
6561 height
= img
->height
= mgr
->cinfo
.output_height
;
6563 if (!check_image_size (f
, width
, height
))
6565 mgr
->failure_code
= MY_JPEG_INVALID_IMAGE_SIZE
;
6566 sys_longjmp (mgr
->setjmp_buffer
, 1);
6569 /* Create X image and pixmap. */
6570 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
6572 mgr
->failure_code
= MY_JPEG_CANNOT_CREATE_X
;
6573 sys_longjmp (mgr
->setjmp_buffer
, 1);
6576 /* Allocate colors. When color quantization is used,
6577 mgr->cinfo.actual_number_of_colors has been set with the number of
6578 colors generated, and mgr->cinfo.colormap is a two-dimensional array
6579 of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
6580 No more than 255 colors will be generated. */
6584 if (mgr
->cinfo
.out_color_components
> 2)
6585 ir
= 0, ig
= 1, ib
= 2;
6586 else if (mgr
->cinfo
.out_color_components
> 1)
6587 ir
= 0, ig
= 1, ib
= 0;
6589 ir
= 0, ig
= 0, ib
= 0;
6591 /* Use the color table mechanism because it handles colors that
6592 cannot be allocated nicely. Such colors will be replaced with
6593 a default color, and we don't have to care about which colors
6594 can be freed safely, and which can't. */
6595 init_color_table ();
6596 colors
= alloca (mgr
->cinfo
.actual_number_of_colors
* sizeof *colors
);
6598 for (i
= 0; i
< mgr
->cinfo
.actual_number_of_colors
; ++i
)
6600 /* Multiply RGB values with 255 because X expects RGB values
6601 in the range 0..0xffff. */
6602 int r
= mgr
->cinfo
.colormap
[ir
][i
] << 8;
6603 int g
= mgr
->cinfo
.colormap
[ig
][i
] << 8;
6604 int b
= mgr
->cinfo
.colormap
[ib
][i
] << 8;
6605 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
6608 #ifdef COLOR_TABLE_SUPPORT
6609 /* Remember those colors actually allocated. */
6610 img
->colors
= colors_in_color_table (&img
->ncolors
);
6611 free_color_table ();
6612 #endif /* COLOR_TABLE_SUPPORT */
6616 row_stride
= width
* mgr
->cinfo
.output_components
;
6617 buffer
= mgr
->cinfo
.mem
->alloc_sarray ((j_common_ptr
) &mgr
->cinfo
,
6618 JPOOL_IMAGE
, row_stride
, 1);
6619 for (y
= 0; y
< height
; ++y
)
6621 fn_jpeg_read_scanlines (&mgr
->cinfo
, buffer
, 1);
6622 for (x
= 0; x
< mgr
->cinfo
.output_width
; ++x
)
6623 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
6627 fn_jpeg_finish_decompress (&mgr
->cinfo
);
6628 fn_jpeg_destroy_decompress (&mgr
->cinfo
);
6632 /* Maybe fill in the background field while we have ximg handy. */
6633 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
6634 /* Casting avoids a GCC warning. */
6635 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
6637 /* Put ximg into the image. */
6638 image_put_x_image (f
, img
, ximg
, 0);
6643 jpeg_load (struct frame
*f
, struct image
*img
)
6645 struct my_jpeg_error_mgr mgr
;
6646 return jpeg_load_body (f
, img
, &mgr
);
6649 #else /* HAVE_JPEG */
6653 jpeg_load (struct frame
*f
, struct image
*img
)
6655 return ns_load_image (f
, img
,
6656 image_spec_value (img
->spec
, QCfile
, NULL
),
6657 image_spec_value (img
->spec
, QCdata
, NULL
));
6659 #endif /* HAVE_NS */
6661 #endif /* !HAVE_JPEG */
6665 /***********************************************************************
6667 ***********************************************************************/
6669 #if defined (HAVE_TIFF) || defined (HAVE_NS)
6671 static bool tiff_image_p (Lisp_Object object
);
6672 static bool tiff_load (struct frame
*f
, struct image
*img
);
6674 /* The symbol `tiff' identifying images of this type. */
6676 static Lisp_Object Qtiff
;
6678 /* Indices of image specification fields in tiff_format, below. */
6680 enum tiff_keyword_index
6689 TIFF_HEURISTIC_MASK
,
6696 /* Vector of image_keyword structures describing the format
6697 of valid user-defined image specifications. */
6699 static const struct image_keyword tiff_format
[TIFF_LAST
] =
6701 {":type", IMAGE_SYMBOL_VALUE
, 1},
6702 {":data", IMAGE_STRING_VALUE
, 0},
6703 {":file", IMAGE_STRING_VALUE
, 0},
6704 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6705 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
6706 {":relief", IMAGE_INTEGER_VALUE
, 0},
6707 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6708 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6709 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6710 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
6711 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0}
6714 #if defined HAVE_NTGUI && defined WINDOWSNT
6715 static bool init_tiff_functions (void);
6717 #define init_tiff_functions NULL
6720 /* Structure describing the image type `tiff'. */
6722 static struct image_type tiff_type
=
6728 init_tiff_functions
,
6732 /* Return true if OBJECT is a valid TIFF image specification. */
6735 tiff_image_p (Lisp_Object object
)
6737 struct image_keyword fmt
[TIFF_LAST
];
6738 memcpy (fmt
, tiff_format
, sizeof fmt
);
6740 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
6743 /* Must specify either the :data or :file keyword. */
6744 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
6747 #endif /* HAVE_TIFF || HAVE_NS */
6755 /* TIFF library details. */
6756 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetErrorHandler
, (TIFFErrorHandler
));
6757 DEF_IMGLIB_FN (TIFFErrorHandler
, TIFFSetWarningHandler
, (TIFFErrorHandler
));
6758 DEF_IMGLIB_FN (TIFF
*, TIFFOpen
, (const char *, const char *));
6759 DEF_IMGLIB_FN (TIFF
*, TIFFClientOpen
, (const char *, const char *, thandle_t
,
6760 TIFFReadWriteProc
, TIFFReadWriteProc
,
6761 TIFFSeekProc
, TIFFCloseProc
, TIFFSizeProc
,
6762 TIFFMapFileProc
, TIFFUnmapFileProc
));
6763 DEF_IMGLIB_FN (int, TIFFGetField
, (TIFF
*, ttag_t
, ...));
6764 DEF_IMGLIB_FN (int, TIFFReadRGBAImage
, (TIFF
*, uint32
, uint32
, uint32
*, int));
6765 DEF_IMGLIB_FN (void, TIFFClose
, (TIFF
*));
6766 DEF_IMGLIB_FN (int, TIFFSetDirectory
, (TIFF
*, tdir_t
));
6769 init_tiff_functions (void)
6773 if (!(library
= w32_delayed_load (Qtiff
)))
6776 LOAD_IMGLIB_FN (library
, TIFFSetErrorHandler
);
6777 LOAD_IMGLIB_FN (library
, TIFFSetWarningHandler
);
6778 LOAD_IMGLIB_FN (library
, TIFFOpen
);
6779 LOAD_IMGLIB_FN (library
, TIFFClientOpen
);
6780 LOAD_IMGLIB_FN (library
, TIFFGetField
);
6781 LOAD_IMGLIB_FN (library
, TIFFReadRGBAImage
);
6782 LOAD_IMGLIB_FN (library
, TIFFClose
);
6783 LOAD_IMGLIB_FN (library
, TIFFSetDirectory
);
6789 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
6790 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
6791 #define fn_TIFFOpen TIFFOpen
6792 #define fn_TIFFClientOpen TIFFClientOpen
6793 #define fn_TIFFGetField TIFFGetField
6794 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
6795 #define fn_TIFFClose TIFFClose
6796 #define fn_TIFFSetDirectory TIFFSetDirectory
6797 #endif /* WINDOWSNT */
6800 /* Reading from a memory buffer for TIFF images Based on the PNG
6801 memory source, but we have to provide a lot of extra functions.
6804 We really only need to implement read and seek, but I am not
6805 convinced that the TIFF library is smart enough not to destroy
6806 itself if we only hand it the function pointers we need to
6811 unsigned char *bytes
;
6818 tiff_read_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6820 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6822 size
= min (size
, src
->len
- src
->index
);
6823 memcpy (buf
, src
->bytes
+ src
->index
, size
);
6829 tiff_write_from_memory (thandle_t data
, tdata_t buf
, tsize_t size
)
6835 tiff_seek_in_memory (thandle_t data
, toff_t off
, int whence
)
6837 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
6842 case SEEK_SET
: /* Go from beginning of source. */
6846 case SEEK_END
: /* Go from end of source. */
6847 idx
= src
->len
+ off
;
6850 case SEEK_CUR
: /* Go from current position. */
6851 idx
= src
->index
+ off
;
6854 default: /* Invalid `whence'. */
6858 if (idx
> src
->len
|| idx
< 0)
6866 tiff_close_memory (thandle_t data
)
6873 tiff_mmap_memory (thandle_t data
, tdata_t
*pbase
, toff_t
*psize
)
6875 /* It is already _IN_ memory. */
6880 tiff_unmap_memory (thandle_t data
, tdata_t base
, toff_t size
)
6882 /* We don't need to do this. */
6886 tiff_size_of_memory (thandle_t data
)
6888 return ((tiff_memory_source
*) data
)->len
;
6891 /* GCC 3.x on x86 Windows targets has a bug that triggers an internal
6892 compiler error compiling tiff_handler, see Bugzilla bug #17406
6893 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring
6894 this function as external works around that problem. */
6895 #if defined (__MINGW32__) && __GNUC__ == 3
6896 # define MINGW_STATIC
6898 # define MINGW_STATIC static
6902 tiff_handler (const char *, const char *, const char *, va_list)
6903 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6905 tiff_handler (const char *log_format
, const char *title
,
6906 const char *format
, va_list ap
)
6908 /* doprnt is not suitable here, as TIFF handlers are called from
6909 libtiff and are passed arbitrary printf directives. Instead, use
6910 vsnprintf, taking care to be portable to nonstandard environments
6911 where vsnprintf returns -1 on buffer overflow. Since it's just a
6912 log entry, it's OK to truncate it. */
6914 int len
= vsnprintf (buf
, sizeof buf
, format
, ap
);
6915 add_to_log (log_format
, build_string (title
),
6916 make_string (buf
, max (0, min (len
, sizeof buf
- 1))));
6920 static void tiff_error_handler (const char *, const char *, va_list)
6921 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6923 tiff_error_handler (const char *title
, const char *format
, va_list ap
)
6925 tiff_handler ("TIFF error: %s %s", title
, format
, ap
);
6929 static void tiff_warning_handler (const char *, const char *, va_list)
6930 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6932 tiff_warning_handler (const char *title
, const char *format
, va_list ap
)
6934 tiff_handler ("TIFF warning: %s %s", title
, format
, ap
);
6938 /* Load TIFF image IMG for use on frame F. Value is true if
6942 tiff_load (struct frame
*f
, struct image
*img
)
6944 Lisp_Object file
, specified_file
;
6945 Lisp_Object specified_data
;
6947 int width
, height
, x
, y
, count
;
6951 tiff_memory_source memsrc
;
6954 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6955 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6957 fn_TIFFSetErrorHandler ((TIFFErrorHandler
) tiff_error_handler
);
6958 fn_TIFFSetWarningHandler ((TIFFErrorHandler
) tiff_warning_handler
);
6960 if (NILP (specified_data
))
6962 /* Read from a file */
6963 file
= x_find_image_file (specified_file
);
6964 if (!STRINGP (file
))
6966 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6970 /* Try to open the image file. */
6971 tiff
= fn_TIFFOpen (SSDATA (file
), "r");
6974 image_error ("Cannot open `%s'", file
, Qnil
);
6980 if (!STRINGP (specified_data
))
6982 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
6986 /* Memory source! */
6987 memsrc
.bytes
= SDATA (specified_data
);
6988 memsrc
.len
= SBYTES (specified_data
);
6991 tiff
= fn_TIFFClientOpen ("memory_source", "r", (thandle_t
)&memsrc
,
6992 tiff_read_from_memory
,
6993 tiff_write_from_memory
,
6994 tiff_seek_in_memory
,
6996 tiff_size_of_memory
,
7002 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
7007 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7008 if (INTEGERP (image
))
7010 EMACS_INT ino
= XFASTINT (image
);
7011 if (! (TYPE_MINIMUM (tdir_t
) <= ino
&& ino
<= TYPE_MAXIMUM (tdir_t
)
7012 && fn_TIFFSetDirectory (tiff
, ino
)))
7014 image_error ("Invalid image number `%s' in image `%s'",
7016 fn_TIFFClose (tiff
);
7021 /* Get width and height of the image, and allocate a raster buffer
7022 of width x height 32-bit values. */
7023 fn_TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
7024 fn_TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
7026 if (!check_image_size (f
, width
, height
))
7028 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7029 fn_TIFFClose (tiff
);
7033 /* Create the X image and pixmap. */
7034 if (! (height
<= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *buf
/ width
7035 && image_create_x_image_and_pixmap (f
, img
, width
, height
, 0,
7038 fn_TIFFClose (tiff
);
7042 buf
= xmalloc (sizeof *buf
* width
* height
);
7044 rc
= fn_TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
7046 /* Count the number of images in the file. */
7047 for (count
= 1; fn_TIFFSetDirectory (tiff
, count
); count
++)
7051 img
->lisp_data
= Fcons (Qcount
,
7052 Fcons (make_number (count
),
7055 fn_TIFFClose (tiff
);
7058 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
7063 /* Initialize the color table. */
7064 init_color_table ();
7066 /* Process the pixel raster. Origin is in the lower-left corner. */
7067 for (y
= 0; y
< height
; ++y
)
7069 uint32
*row
= buf
+ y
* width
;
7071 for (x
= 0; x
< width
; ++x
)
7073 uint32 abgr
= row
[x
];
7074 int r
= TIFFGetR (abgr
) << 8;
7075 int g
= TIFFGetG (abgr
) << 8;
7076 int b
= TIFFGetB (abgr
) << 8;
7077 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
7081 #ifdef COLOR_TABLE_SUPPORT
7082 /* Remember the colors allocated for the image. Free the color table. */
7083 img
->colors
= colors_in_color_table (&img
->ncolors
);
7084 free_color_table ();
7085 #endif /* COLOR_TABLE_SUPPORT */
7088 img
->height
= height
;
7090 /* Maybe fill in the background field while we have ximg handy. */
7091 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7092 /* Casting avoids a GCC warning on W32. */
7093 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7095 /* Put ximg into the image. */
7096 image_put_x_image (f
, img
, ximg
, 0);
7102 #else /* HAVE_TIFF */
7106 tiff_load (struct frame
*f
, struct image
*img
)
7108 return ns_load_image (f
, img
,
7109 image_spec_value (img
->spec
, QCfile
, NULL
),
7110 image_spec_value (img
->spec
, QCdata
, NULL
));
7112 #endif /* HAVE_NS */
7114 #endif /* !HAVE_TIFF */
7118 /***********************************************************************
7120 ***********************************************************************/
7122 #if defined (HAVE_GIF) || defined (HAVE_NS)
7124 static bool gif_image_p (Lisp_Object object
);
7125 static bool gif_load (struct frame
*f
, struct image
*img
);
7126 static void gif_clear_image (struct frame
*f
, struct image
*img
);
7128 /* The symbol `gif' identifying images of this type. */
7130 static Lisp_Object Qgif
;
7132 /* Indices of image specification fields in gif_format, below. */
7134 enum gif_keyword_index
7150 /* Vector of image_keyword structures describing the format
7151 of valid user-defined image specifications. */
7153 static const struct image_keyword gif_format
[GIF_LAST
] =
7155 {":type", IMAGE_SYMBOL_VALUE
, 1},
7156 {":data", IMAGE_STRING_VALUE
, 0},
7157 {":file", IMAGE_STRING_VALUE
, 0},
7158 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7159 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7160 {":relief", IMAGE_INTEGER_VALUE
, 0},
7161 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7162 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7163 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7164 {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
7165 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7168 #if defined HAVE_NTGUI && defined WINDOWSNT
7169 static bool init_gif_functions (void);
7171 #define init_gif_functions NULL
7174 /* Structure describing the image type `gif'. */
7176 static struct image_type gif_type
=
7186 /* Free X resources of GIF image IMG which is used on frame F. */
7189 gif_clear_image (struct frame
*f
, struct image
*img
)
7191 img
->lisp_data
= Qnil
;
7192 x_clear_image (f
, img
);
7195 /* Return true if OBJECT is a valid GIF image specification. */
7198 gif_image_p (Lisp_Object object
)
7200 struct image_keyword fmt
[GIF_LAST
];
7201 memcpy (fmt
, gif_format
, sizeof fmt
);
7203 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
7206 /* Must specify either the :data or :file keyword. */
7207 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
7210 #endif /* HAVE_GIF */
7214 #if defined (HAVE_NTGUI)
7215 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7216 Undefine before redefining to avoid a preprocessor warning. */
7220 /* avoid conflict with QuickdrawText.h */
7221 #define DrawText gif_DrawText
7222 #include <gif_lib.h>
7225 #else /* HAVE_NTGUI */
7227 #include <gif_lib.h>
7229 #endif /* HAVE_NTGUI */
7234 /* GIF library details. */
7235 DEF_IMGLIB_FN (int, DGifCloseFile
, (GifFileType
*));
7236 DEF_IMGLIB_FN (int, DGifSlurp
, (GifFileType
*));
7237 DEF_IMGLIB_FN (GifFileType
*, DGifOpen
, (void *, InputFunc
));
7238 DEF_IMGLIB_FN (GifFileType
*, DGifOpenFileName
, (const char *));
7241 init_gif_functions (void)
7245 if (!(library
= w32_delayed_load (Qgif
)))
7248 LOAD_IMGLIB_FN (library
, DGifCloseFile
);
7249 LOAD_IMGLIB_FN (library
, DGifSlurp
);
7250 LOAD_IMGLIB_FN (library
, DGifOpen
);
7251 LOAD_IMGLIB_FN (library
, DGifOpenFileName
);
7257 #define fn_DGifCloseFile DGifCloseFile
7258 #define fn_DGifSlurp DGifSlurp
7259 #define fn_DGifOpen DGifOpen
7260 #define fn_DGifOpenFileName DGifOpenFileName
7262 #endif /* WINDOWSNT */
7264 /* Reading a GIF image from memory
7265 Based on the PNG memory stuff to a certain extent. */
7269 unsigned char *bytes
;
7275 /* Make the current memory source available to gif_read_from_memory.
7276 It's done this way because not all versions of libungif support
7277 a UserData field in the GifFileType structure. */
7278 static gif_memory_source
*current_gif_memory_src
;
7281 gif_read_from_memory (GifFileType
*file
, GifByteType
*buf
, int len
)
7283 gif_memory_source
*src
= current_gif_memory_src
;
7285 if (len
> src
->len
- src
->index
)
7288 memcpy (buf
, src
->bytes
+ src
->index
, len
);
7294 /* Load GIF image IMG for use on frame F. Value is true if
7297 static const int interlace_start
[] = {0, 4, 2, 1};
7298 static const int interlace_increment
[] = {8, 8, 4, 2};
7300 #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249
7303 gif_load (struct frame
*f
, struct image
*img
)
7306 int rc
, width
, height
, x
, y
, i
, j
;
7308 ColorMapObject
*gif_color_map
;
7309 unsigned long pixel_colors
[256];
7311 gif_memory_source memsrc
;
7312 Lisp_Object specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7313 Lisp_Object specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7314 Lisp_Object specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7315 unsigned long bgcolor
= 0;
7318 if (NILP (specified_data
))
7320 file
= x_find_image_file (specified_file
);
7321 if (!STRINGP (file
))
7323 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7327 /* Open the GIF file. */
7328 gif
= fn_DGifOpenFileName (SSDATA (file
));
7331 image_error ("Cannot open `%s'", file
, Qnil
);
7337 if (!STRINGP (specified_data
))
7339 image_error ("Invalid image data `%s'", specified_data
, Qnil
);
7343 /* Read from memory! */
7344 current_gif_memory_src
= &memsrc
;
7345 memsrc
.bytes
= SDATA (specified_data
);
7346 memsrc
.len
= SBYTES (specified_data
);
7349 gif
= fn_DGifOpen (&memsrc
, gif_read_from_memory
);
7352 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
7357 /* Before reading entire contents, check the declared image size. */
7358 if (!check_image_size (f
, gif
->SWidth
, gif
->SHeight
))
7360 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7361 fn_DGifCloseFile (gif
);
7365 /* Read entire contents. */
7366 rc
= fn_DGifSlurp (gif
);
7367 if (rc
== GIF_ERROR
|| gif
->ImageCount
<= 0)
7369 image_error ("Error reading `%s'", img
->spec
, Qnil
);
7370 fn_DGifCloseFile (gif
);
7374 /* Which sub-image are we to display? */
7376 Lisp_Object image_number
= image_spec_value (img
->spec
, QCindex
, NULL
);
7377 idx
= INTEGERP (image_number
) ? XFASTINT (image_number
) : 0;
7378 if (idx
< 0 || idx
>= gif
->ImageCount
)
7380 image_error ("Invalid image number `%s' in image `%s'",
7381 image_number
, img
->spec
);
7382 fn_DGifCloseFile (gif
);
7387 width
= img
->width
= gif
->SWidth
;
7388 height
= img
->height
= gif
->SHeight
;
7390 img
->corners
[TOP_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Top
;
7391 img
->corners
[LEFT_CORNER
] = gif
->SavedImages
[0].ImageDesc
.Left
;
7392 img
->corners
[BOT_CORNER
]
7393 = img
->corners
[TOP_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Height
;
7394 img
->corners
[RIGHT_CORNER
]
7395 = img
->corners
[LEFT_CORNER
] + gif
->SavedImages
[0].ImageDesc
.Width
;
7397 if (!check_image_size (f
, width
, height
))
7399 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
7400 fn_DGifCloseFile (gif
);
7404 /* Check that the selected subimages fit. It's not clear whether
7405 the GIF spec requires this, but Emacs can crash if they don't fit. */
7406 for (j
= 0; j
<= idx
; ++j
)
7408 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7409 int subimg_width
= subimage
->ImageDesc
.Width
;
7410 int subimg_height
= subimage
->ImageDesc
.Height
;
7411 int subimg_top
= subimage
->ImageDesc
.Top
;
7412 int subimg_left
= subimage
->ImageDesc
.Left
;
7413 if (! (0 <= subimg_width
&& 0 <= subimg_height
7414 && 0 <= subimg_top
&& subimg_top
<= height
- subimg_height
7415 && 0 <= subimg_left
&& subimg_left
<= width
- subimg_width
))
7417 image_error ("Subimage does not fit in image", Qnil
, Qnil
);
7418 fn_DGifCloseFile (gif
);
7423 /* Create the X image and pixmap. */
7424 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
7426 fn_DGifCloseFile (gif
);
7430 /* Clear the part of the screen image not covered by the image.
7431 Full animated GIF support requires more here (see the gif89 spec,
7432 disposal methods). Let's simply assume that the part not covered
7433 by a sub-image is in the frame's background color. */
7434 for (y
= 0; y
< img
->corners
[TOP_CORNER
]; ++y
)
7435 for (x
= 0; x
< width
; ++x
)
7436 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7438 for (y
= img
->corners
[BOT_CORNER
]; y
< height
; ++y
)
7439 for (x
= 0; x
< width
; ++x
)
7440 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7442 for (y
= img
->corners
[TOP_CORNER
]; y
< img
->corners
[BOT_CORNER
]; ++y
)
7444 for (x
= 0; x
< img
->corners
[LEFT_CORNER
]; ++x
)
7445 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7446 for (x
= img
->corners
[RIGHT_CORNER
]; x
< width
; ++x
)
7447 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
7450 /* Read the GIF image into the X image. */
7452 /* FIXME: With the current implementation, loading an animated gif
7453 is quadratic in the number of animation frames, since each frame
7454 is a separate struct image. We must provide a way for a single
7455 gif_load call to construct and save all animation frames. */
7457 init_color_table ();
7458 if (STRINGP (specified_bg
))
7459 bgcolor
= x_alloc_image_color (f
, img
, specified_bg
,
7460 FRAME_BACKGROUND_PIXEL (f
));
7461 for (j
= 0; j
<= idx
; ++j
)
7463 /* We use a local variable `raster' here because RasterBits is a
7464 char *, which invites problems with bytes >= 0x80. */
7465 struct SavedImage
*subimage
= gif
->SavedImages
+ j
;
7466 unsigned char *raster
= (unsigned char *) subimage
->RasterBits
;
7467 int transparency_color_index
= -1;
7469 int subimg_width
= subimage
->ImageDesc
.Width
;
7470 int subimg_height
= subimage
->ImageDesc
.Height
;
7471 int subimg_top
= subimage
->ImageDesc
.Top
;
7472 int subimg_left
= subimage
->ImageDesc
.Left
;
7474 /* Find the Graphic Control Extension block for this sub-image.
7475 Extract the disposal method and transparency color. */
7476 for (i
= 0; i
< subimage
->ExtensionBlockCount
; i
++)
7478 ExtensionBlock
*extblock
= subimage
->ExtensionBlocks
+ i
;
7480 if ((extblock
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
)
7481 && extblock
->ByteCount
== 4
7482 && extblock
->Bytes
[0] & 1)
7484 /* From gif89a spec: 1 = "keep in place", 2 = "restore
7485 to background". Treat any other value like 2. */
7486 disposal
= (extblock
->Bytes
[0] >> 2) & 7;
7487 transparency_color_index
= (unsigned char) extblock
->Bytes
[3];
7492 /* We can't "keep in place" the first subimage. */
7496 /* For disposal == 0, the spec says "No disposal specified. The
7497 decoder is not required to take any action." In practice, it
7498 seems we need to treat this like "keep in place", see e.g.
7499 http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */
7503 /* Allocate subimage colors. */
7504 memset (pixel_colors
, 0, sizeof pixel_colors
);
7505 gif_color_map
= subimage
->ImageDesc
.ColorMap
;
7507 gif_color_map
= gif
->SColorMap
;
7510 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
7512 if (transparency_color_index
== i
)
7513 pixel_colors
[i
] = STRINGP (specified_bg
)
7514 ? bgcolor
: FRAME_BACKGROUND_PIXEL (f
);
7517 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
7518 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
7519 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
7520 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
7524 /* Apply the pixel values. */
7525 if (gif
->SavedImages
[j
].ImageDesc
.Interlace
)
7529 for (y
= 0, row
= interlace_start
[0], pass
= 0;
7531 y
++, row
+= interlace_increment
[pass
])
7533 while (subimg_height
<= row
)
7535 lint_assume (pass
< 3);
7536 row
= interlace_start
[++pass
];
7539 for (x
= 0; x
< subimg_width
; x
++)
7541 int c
= raster
[y
* subimg_width
+ x
];
7542 if (transparency_color_index
!= c
|| disposal
!= 1)
7543 XPutPixel (ximg
, x
+ subimg_left
, row
+ subimg_top
,
7550 for (y
= 0; y
< subimg_height
; ++y
)
7551 for (x
= 0; x
< subimg_width
; ++x
)
7553 int c
= raster
[y
* subimg_width
+ x
];
7554 if (transparency_color_index
!= c
|| disposal
!= 1)
7555 XPutPixel (ximg
, x
+ subimg_left
, y
+ subimg_top
,
7561 #ifdef COLOR_TABLE_SUPPORT
7562 img
->colors
= colors_in_color_table (&img
->ncolors
);
7563 free_color_table ();
7564 #endif /* COLOR_TABLE_SUPPORT */
7566 /* Save GIF image extension data for `image-metadata'.
7567 Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */
7568 img
->lisp_data
= Qnil
;
7569 if (gif
->SavedImages
[idx
].ExtensionBlockCount
> 0)
7572 ExtensionBlock
*ext
= gif
->SavedImages
[idx
].ExtensionBlocks
;
7573 for (i
= 0; i
< gif
->SavedImages
[idx
].ExtensionBlockCount
; i
++, ext
++)
7574 /* Append (... FUNCTION "BYTES") */
7577 = Fcons (make_number (ext
->Function
),
7578 Fcons (make_unibyte_string (ext
->Bytes
, ext
->ByteCount
),
7580 if (ext
->Function
== GIF_LOCAL_DESCRIPTOR_EXTENSION
7581 && ext
->ByteCount
== 4)
7583 delay
= ext
->Bytes
[2] << CHAR_BIT
;
7584 delay
|= ext
->Bytes
[1];
7587 img
->lisp_data
= Fcons (Qextension_data
,
7588 Fcons (img
->lisp_data
, Qnil
));
7592 Fcons (make_float (delay
/ 100.0),
7596 if (gif
->ImageCount
> 1)
7597 img
->lisp_data
= Fcons (Qcount
,
7598 Fcons (make_number (gif
->ImageCount
),
7601 fn_DGifCloseFile (gif
);
7603 /* Maybe fill in the background field while we have ximg handy. */
7604 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7605 /* Casting avoids a GCC warning. */
7606 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
7608 /* Put ximg into the image. */
7609 image_put_x_image (f
, img
, ximg
, 0);
7614 #else /* !HAVE_GIF */
7618 gif_load (struct frame
*f
, struct image
*img
)
7620 return ns_load_image (f
, img
,
7621 image_spec_value (img
->spec
, QCfile
, NULL
),
7622 image_spec_value (img
->spec
, QCdata
, NULL
));
7624 #endif /* HAVE_NS */
7626 #endif /* HAVE_GIF */
7630 compute_image_size (size_t width
, size_t height
,
7632 int *d_width
, int *d_height
)
7635 int desired_width
, desired_height
;
7637 /* If width and/or height is set in the display spec assume we want
7638 to scale to those values. If either h or w is unspecified, the
7639 unspecified should be calculated from the specified to preserve
7641 value
= image_spec_value (spec
, QCwidth
, NULL
);
7642 desired_width
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7643 value
= image_spec_value (spec
, QCheight
, NULL
);
7644 desired_height
= (INTEGERP (value
) ? XFASTINT (value
) : -1);
7646 if (desired_width
== -1)
7648 value
= image_spec_value (spec
, QCmax_width
, NULL
);
7649 if (INTEGERP (value
) &&
7650 width
> XFASTINT (value
))
7652 /* The image is wider than :max-width. */
7653 desired_width
= XFASTINT (value
);
7654 if (desired_height
== -1)
7656 value
= image_spec_value (spec
, QCmax_height
, NULL
);
7657 if (INTEGERP (value
))
7659 /* We have no specified height, but we have a
7660 :max-height value, so check that we satisfy both
7662 desired_height
= (double) desired_width
/ width
* height
;
7663 if (desired_height
> XFASTINT (value
))
7665 desired_height
= XFASTINT (value
);
7666 desired_width
= (double) desired_height
/ height
* width
;
7671 /* We have no specified height and no specified
7672 max-height, so just compute the height. */
7673 desired_height
= (double) desired_width
/ width
* height
;
7679 if (desired_height
== -1)
7681 value
= image_spec_value (spec
, QCmax_height
, NULL
);
7682 if (INTEGERP (value
) &&
7683 height
> XFASTINT (value
))
7684 desired_height
= XFASTINT (value
);
7687 if (desired_width
!= -1 && desired_height
== -1)
7688 /* w known, calculate h. */
7689 desired_height
= (double) desired_width
/ width
* height
;
7691 if (desired_width
== -1 && desired_height
!= -1)
7692 /* h known, calculate w. */
7693 desired_width
= (double) desired_height
/ height
* width
;
7695 *d_width
= desired_width
;
7696 *d_height
= desired_height
;
7699 /***********************************************************************
7701 ***********************************************************************/
7702 #if defined (HAVE_IMAGEMAGICK)
7704 static Lisp_Object Qimagemagick
;
7706 static bool imagemagick_image_p (Lisp_Object
);
7707 static bool imagemagick_load (struct frame
*, struct image
*);
7708 static void imagemagick_clear_image (struct frame
*, struct image
*);
7710 /* Indices of image specification fields in imagemagick_format. */
7712 enum imagemagick_keyword_index
7720 IMAGEMAGICK_ALGORITHM
,
7721 IMAGEMAGICK_HEURISTIC_MASK
,
7723 IMAGEMAGICK_BACKGROUND
,
7726 IMAGEMAGICK_MAX_HEIGHT
,
7727 IMAGEMAGICK_MAX_WIDTH
,
7728 IMAGEMAGICK_ROTATION
,
7733 /* Vector of image_keyword structures describing the format
7734 of valid user-defined image specifications. */
7736 static struct image_keyword imagemagick_format
[IMAGEMAGICK_LAST
] =
7738 {":type", IMAGE_SYMBOL_VALUE
, 1},
7739 {":data", IMAGE_STRING_VALUE
, 0},
7740 {":file", IMAGE_STRING_VALUE
, 0},
7741 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7742 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
7743 {":relief", IMAGE_INTEGER_VALUE
, 0},
7744 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7745 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7746 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7747 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
7748 {":height", IMAGE_INTEGER_VALUE
, 0},
7749 {":width", IMAGE_INTEGER_VALUE
, 0},
7750 {":max-height", IMAGE_INTEGER_VALUE
, 0},
7751 {":max-width", IMAGE_INTEGER_VALUE
, 0},
7752 {":rotation", IMAGE_NUMBER_VALUE
, 0},
7753 {":crop", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
7756 #if defined HAVE_NTGUI && defined WINDOWSNT
7757 static bool init_imagemagick_functions (void);
7759 #define init_imagemagick_functions NULL
7762 /* Structure describing the image type for any image handled via
7765 static struct image_type imagemagick_type
=
7768 imagemagick_image_p
,
7770 imagemagick_clear_image
,
7771 init_imagemagick_functions
,
7775 /* Free X resources of imagemagick image IMG which is used on frame F. */
7778 imagemagick_clear_image (struct frame
*f
,
7781 x_clear_image (f
, img
);
7784 /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do
7785 this by calling parse_image_spec and supplying the keywords that
7786 identify the IMAGEMAGICK format. */
7789 imagemagick_image_p (Lisp_Object object
)
7791 struct image_keyword fmt
[IMAGEMAGICK_LAST
];
7792 memcpy (fmt
, imagemagick_format
, sizeof fmt
);
7794 if (!parse_image_spec (object
, fmt
, IMAGEMAGICK_LAST
, Qimagemagick
))
7797 /* Must specify either the :data or :file keyword. */
7798 return fmt
[IMAGEMAGICK_FILE
].count
+ fmt
[IMAGEMAGICK_DATA
].count
== 1;
7801 /* The GIF library also defines DrawRectangle, but its never used in Emacs.
7802 Therefore rename the function so it doesn't collide with ImageMagick. */
7803 #define DrawRectangle DrawRectangleGif
7804 #include <wand/MagickWand.h>
7806 /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason.
7807 Emacs seems to work fine with the hidden version, so unhide it. */
7808 #include <magick/version.h>
7809 #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665
7810 extern WandExport
void PixelGetMagickColor (const PixelWand
*,
7811 MagickPixelPacket
*);
7814 /* Log ImageMagick error message.
7815 Useful when a ImageMagick function returns the status `MagickFalse'. */
7818 imagemagick_error (MagickWand
*wand
)
7821 ExceptionType severity
;
7823 description
= MagickGetException (wand
, &severity
);
7824 image_error ("ImageMagick error: %s",
7825 build_string (description
),
7827 description
= (char *) MagickRelinquishMemory (description
);
7830 /* Helper function for imagemagick_load, which does the actual loading
7831 given contents and size, apart from frame and image structures,
7832 passed from imagemagick_load. Uses librimagemagick to do most of
7833 the image processing.
7835 F is a pointer to the Emacs frame; IMG to the image structure to
7836 prepare; CONTENTS is the string containing the IMAGEMAGICK data to
7837 be parsed; SIZE is the number of bytes of data; and FILENAME is
7838 either the file name or the image data.
7840 Return true if successful. */
7843 imagemagick_load_image (struct frame
*f
, struct image
*img
,
7844 unsigned char *contents
, unsigned int size
,
7847 size_t width
, height
;
7848 MagickBooleanType status
;
7851 MagickWand
*image_wand
;
7852 MagickWand
*ping_wand
;
7853 PixelIterator
*iterator
;
7854 PixelWand
**pixels
, *bg_wand
= NULL
;
7855 MagickPixelPacket pixel
;
7860 int desired_width
, desired_height
;
7864 /* Handle image index for image types who can contain more than one image.
7865 Interface :index is same as for GIF. First we "ping" the image to see how
7866 many sub-images it contains. Pinging is faster than loading the image to
7867 find out things about it. */
7869 /* Initialize the imagemagick environment. */
7870 MagickWandGenesis ();
7871 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
7872 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
7873 ping_wand
= NewMagickWand ();
7874 /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */
7877 ? MagickPingImage (ping_wand
, filename
)
7878 : MagickPingImageBlob (ping_wand
, contents
, size
);
7880 if (status
== MagickFalse
)
7882 imagemagick_error (ping_wand
);
7883 DestroyMagickWand (ping_wand
);
7887 if (ino
< 0 || ino
>= MagickGetNumberImages (ping_wand
))
7889 image_error ("Invalid image number `%s' in image `%s'",
7891 DestroyMagickWand (ping_wand
);
7895 if (MagickGetNumberImages (ping_wand
) > 1)
7898 Fcons (make_number (MagickGetNumberImages (ping_wand
)),
7901 DestroyMagickWand (ping_wand
);
7903 /* Now we know how many images are inside the file. If it's not a
7904 bundle, the number is one. Load the image data. */
7906 image_wand
= NewMagickWand ();
7909 ? MagickReadImage (image_wand
, filename
)
7910 : MagickReadImageBlob (image_wand
, contents
, size
))
7913 imagemagick_error (image_wand
);
7914 goto imagemagick_error
;
7917 /* Retrieve the frame's background color, for use later. */
7920 Lisp_Object specified_bg
;
7922 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
7923 if (!STRINGP (specified_bg
)
7924 || !x_defined_color (f
, SSDATA (specified_bg
), &bgcolor
, 0))
7927 bgcolor
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
7928 x_query_color (f
, &bgcolor
);
7930 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &bgcolor
, 1);
7934 bg_wand
= NewPixelWand ();
7935 PixelSetRed (bg_wand
, (double) bgcolor
.red
/ 65535);
7936 PixelSetGreen (bg_wand
, (double) bgcolor
.green
/ 65535);
7937 PixelSetBlue (bg_wand
, (double) bgcolor
.blue
/ 65535);
7940 compute_image_size (MagickGetImageWidth (image_wand
),
7941 MagickGetImageHeight (image_wand
),
7942 img
->spec
, &desired_width
, &desired_height
);
7944 if (desired_width
!= -1 && desired_height
!= -1)
7946 status
= MagickScaleImage (image_wand
, desired_width
, desired_height
);
7947 if (status
== MagickFalse
)
7949 image_error ("Imagemagick scale failed", Qnil
, Qnil
);
7950 imagemagick_error (image_wand
);
7951 goto imagemagick_error
;
7955 /* crop behaves similar to image slicing in Emacs but is more memory
7957 crop
= image_spec_value (img
->spec
, QCcrop
, NULL
);
7959 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7961 /* After some testing, it seems MagickCropImage is the fastest crop
7962 function in ImageMagick. This crop function seems to do less copying
7963 than the alternatives, but it still reads the entire image into memory
7964 before cropping, which is apparently difficult to avoid when using
7966 size_t crop_width
= XINT (XCAR (crop
));
7968 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop
)))
7970 size_t crop_height
= XINT (XCAR (crop
));
7972 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7974 ssize_t crop_x
= XINT (XCAR (crop
));
7976 if (CONSP (crop
) && TYPE_RANGED_INTEGERP (ssize_t
, XCAR (crop
)))
7978 ssize_t crop_y
= XINT (XCAR (crop
));
7979 MagickCropImage (image_wand
, crop_width
, crop_height
,
7986 /* Furthermore :rotation. we need background color and angle for
7989 TODO background handling for rotation specified_bg =
7990 image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP
7992 value
= image_spec_value (img
->spec
, QCrotation
, NULL
);
7995 rotation
= extract_float (value
);
7996 status
= MagickRotateImage (image_wand
, bg_wand
, rotation
);
7997 if (status
== MagickFalse
)
7999 image_error ("Imagemagick image rotate failed", Qnil
, Qnil
);
8000 imagemagick_error (image_wand
);
8001 goto imagemagick_error
;
8005 /* Set the canvas background color to the frame or specified
8006 background, and flatten the image. Note: as of ImageMagick
8007 6.6.0, SVG image transparency is not handled properly
8008 (e.g. etc/images/splash.svg shows a white background always). */
8010 MagickWand
*new_wand
;
8011 MagickSetImageBackgroundColor (image_wand
, bg_wand
);
8012 #ifdef HAVE_MAGICKMERGEIMAGELAYERS
8013 new_wand
= MagickMergeImageLayers (image_wand
, MergeLayer
);
8015 new_wand
= MagickFlattenImages (image_wand
);
8017 DestroyMagickWand (image_wand
);
8018 image_wand
= new_wand
;
8021 /* Finally we are done manipulating the image. Figure out the
8022 resulting width/height and transfer ownership to Emacs. */
8023 height
= MagickGetImageHeight (image_wand
);
8024 width
= MagickGetImageWidth (image_wand
);
8026 if (! (width
<= INT_MAX
&& height
<= INT_MAX
8027 && check_image_size (f
, width
, height
)))
8029 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8030 goto imagemagick_error
;
8033 /* We can now get a valid pixel buffer from the imagemagick file, if all
8036 init_color_table ();
8038 #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS
8039 if (imagemagick_render_type
!= 0)
8041 /* Magicexportimage is normally faster than pixelpushing. This
8042 method is also well tested. Some aspects of this method are
8043 ad-hoc and needs to be more researched. */
8044 int imagedepth
= 24; /*MagickGetImageDepth(image_wand);*/
8045 const char *exportdepth
= imagedepth
<= 8 ? "I" : "BGRP"; /*"RGBP";*/
8046 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8047 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, imagedepth
,
8050 #ifdef COLOR_TABLE_SUPPORT
8051 free_color_table ();
8053 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
8054 goto imagemagick_error
;
8057 /* Oddly, the below code doesn't seem to work:*/
8058 /* switch(ximg->bitmap_unit){ */
8060 /* pixelwidth=CharPixel; */
8063 /* pixelwidth=ShortPixel; */
8066 /* pixelwidth=LongPixel; */
8070 Here im just guessing the format of the bitmap.
8071 happens to work fine for:
8074 seems about 3 times as fast as pixel pushing(not carefully measured)
8076 pixelwidth
= CharPixel
; /*??? TODO figure out*/
8077 MagickExportImagePixels (image_wand
, 0, 0, width
, height
,
8078 exportdepth
, pixelwidth
, ximg
->data
);
8081 #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */
8083 size_t image_height
;
8085 /* Try to create a x pixmap to hold the imagemagick pixmap. */
8086 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0,
8089 #ifdef COLOR_TABLE_SUPPORT
8090 free_color_table ();
8092 image_error ("Imagemagick X bitmap allocation failure", Qnil
, Qnil
);
8093 goto imagemagick_error
;
8096 /* Copy imagemagick image to x with primitive yet robust pixel
8097 pusher loop. This has been tested a lot with many different
8100 /* Copy pixels from the imagemagick image structure to the x image map. */
8101 iterator
= NewPixelIterator (image_wand
);
8102 if (iterator
== (PixelIterator
*) NULL
)
8104 #ifdef COLOR_TABLE_SUPPORT
8105 free_color_table ();
8107 x_destroy_x_image (ximg
);
8108 image_error ("Imagemagick pixel iterator creation failed",
8110 goto imagemagick_error
;
8113 image_height
= MagickGetImageHeight (image_wand
);
8114 for (y
= 0; y
< image_height
; y
++)
8116 pixels
= PixelGetNextIteratorRow (iterator
, &width
);
8117 if (pixels
== (PixelWand
**) NULL
)
8119 for (x
= 0; x
< (long) width
; x
++)
8121 PixelGetMagickColor (pixels
[x
], &pixel
);
8122 XPutPixel (ximg
, x
, y
,
8123 lookup_rgb_color (f
,
8129 DestroyPixelIterator (iterator
);
8132 #ifdef COLOR_TABLE_SUPPORT
8133 /* Remember colors allocated for this image. */
8134 img
->colors
= colors_in_color_table (&img
->ncolors
);
8135 free_color_table ();
8136 #endif /* COLOR_TABLE_SUPPORT */
8139 img
->height
= height
;
8141 /* Put ximg into the image. */
8142 image_put_x_image (f
, img
, ximg
, 0);
8144 /* Final cleanup. image_wand should be the only resource left. */
8145 DestroyMagickWand (image_wand
);
8146 if (bg_wand
) DestroyPixelWand (bg_wand
);
8148 /* `MagickWandTerminus' terminates the imagemagick environment. */
8149 MagickWandTerminus ();
8154 DestroyMagickWand (image_wand
);
8155 if (bg_wand
) DestroyPixelWand (bg_wand
);
8157 MagickWandTerminus ();
8158 /* TODO more cleanup. */
8159 image_error ("Error parsing IMAGEMAGICK image `%s'", img
->spec
, Qnil
);
8164 /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if
8165 successful. this function will go into the imagemagick_type structure, and
8166 the prototype thus needs to be compatible with that structure. */
8169 imagemagick_load (struct frame
*f
, struct image
*img
)
8172 Lisp_Object file_name
;
8174 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8175 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8176 if (STRINGP (file_name
))
8180 file
= x_find_image_file (file_name
);
8181 if (!STRINGP (file
))
8183 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8186 success_p
= imagemagick_load_image (f
, img
, 0, 0, SSDATA (file
));
8188 /* Else its not a file, its a lisp object. Load the image from a
8189 lisp object rather than a file. */
8194 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8195 if (!STRINGP (data
))
8197 image_error ("Invalid image data `%s'", data
, Qnil
);
8200 success_p
= imagemagick_load_image (f
, img
, SDATA (data
),
8201 SBYTES (data
), NULL
);
8207 DEFUN ("imagemagick-types", Fimagemagick_types
, Simagemagick_types
, 0, 0, 0,
8208 doc
: /* Return a list of image types supported by ImageMagick.
8209 Each entry in this list is a symbol named after an ImageMagick format
8210 tag. See the ImageMagick manual for a list of ImageMagick formats and
8211 their descriptions (http://www.imagemagick.org/script/formats.php).
8212 You can also try the shell command: `identify -list format'.
8214 Note that ImageMagick recognizes many file-types that Emacs does not
8215 recognize as images, such as C. See `imagemagick-types-enable'
8216 and `imagemagick-types-inhibit'. */)
8219 Lisp_Object typelist
= Qnil
;
8224 Lisp_Object Qimagemagicktype
;
8226 GetExceptionInfo(&ex
);
8227 imtypes
= GetMagickList ("*", &numf
, &ex
);
8228 DestroyExceptionInfo(&ex
);
8230 for (i
= 0; i
< numf
; i
++)
8232 Qimagemagicktype
= intern (imtypes
[i
]);
8233 typelist
= Fcons (Qimagemagicktype
, typelist
);
8235 return Fnreverse (typelist
);
8238 #endif /* defined (HAVE_IMAGEMAGICK) */
8242 /***********************************************************************
8244 ***********************************************************************/
8246 #if defined (HAVE_RSVG)
8248 /* Function prototypes. */
8250 static bool svg_image_p (Lisp_Object object
);
8251 static bool svg_load (struct frame
*f
, struct image
*img
);
8253 static bool svg_load_image (struct frame
*, struct image
*,
8254 unsigned char *, ptrdiff_t);
8256 /* The symbol `svg' identifying images of this type. */
8258 static Lisp_Object Qsvg
;
8260 /* Indices of image specification fields in svg_format, below. */
8262 enum svg_keyword_index
8277 /* Vector of image_keyword structures describing the format
8278 of valid user-defined image specifications. */
8280 static const struct image_keyword svg_format
[SVG_LAST
] =
8282 {":type", IMAGE_SYMBOL_VALUE
, 1},
8283 {":data", IMAGE_STRING_VALUE
, 0},
8284 {":file", IMAGE_STRING_VALUE
, 0},
8285 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8286 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8287 {":relief", IMAGE_INTEGER_VALUE
, 0},
8288 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8289 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8290 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8291 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8294 #if defined HAVE_NTGUI && defined WINDOWSNT
8295 static bool init_svg_functions (void);
8297 #define init_svg_functions NULL
8300 /* Structure describing the image type `svg'. Its the same type of
8301 structure defined for all image formats, handled by emacs image
8302 functions. See struct image_type in dispextern.h. */
8304 static struct image_type svg_type
=
8315 /* Return true if OBJECT is a valid SVG image specification. Do
8316 this by calling parse_image_spec and supplying the keywords that
8317 identify the SVG format. */
8320 svg_image_p (Lisp_Object object
)
8322 struct image_keyword fmt
[SVG_LAST
];
8323 memcpy (fmt
, svg_format
, sizeof fmt
);
8325 if (!parse_image_spec (object
, fmt
, SVG_LAST
, Qsvg
))
8328 /* Must specify either the :data or :file keyword. */
8329 return fmt
[SVG_FILE
].count
+ fmt
[SVG_DATA
].count
== 1;
8332 #include <librsvg/rsvg.h>
8336 /* SVG library functions. */
8337 DEF_IMGLIB_FN (RsvgHandle
*, rsvg_handle_new
, (void));
8338 DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions
, (RsvgHandle
*, RsvgDimensionData
*));
8339 DEF_IMGLIB_FN (gboolean
, rsvg_handle_write
, (RsvgHandle
*, const guchar
*, gsize
, GError
**));
8340 DEF_IMGLIB_FN (gboolean
, rsvg_handle_close
, (RsvgHandle
*, GError
**));
8341 DEF_IMGLIB_FN (GdkPixbuf
*, rsvg_handle_get_pixbuf
, (RsvgHandle
*));
8342 DEF_IMGLIB_FN (void *, rsvg_handle_set_size_callback
, (RsvgHandle
*, RsvgSizeFunc
, gpointer
, GDestroyNotify
));
8344 DEF_IMGLIB_FN (int, gdk_pixbuf_get_width
, (const GdkPixbuf
*));
8345 DEF_IMGLIB_FN (int, gdk_pixbuf_get_height
, (const GdkPixbuf
*));
8346 DEF_IMGLIB_FN (guchar
*, gdk_pixbuf_get_pixels
, (const GdkPixbuf
*));
8347 DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride
, (const GdkPixbuf
*));
8348 DEF_IMGLIB_FN (GdkColorspace
, gdk_pixbuf_get_colorspace
, (const GdkPixbuf
*));
8349 DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels
, (const GdkPixbuf
*));
8350 DEF_IMGLIB_FN (gboolean
, gdk_pixbuf_get_has_alpha
, (const GdkPixbuf
*));
8351 DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample
, (const GdkPixbuf
*));
8353 DEF_IMGLIB_FN (void, g_type_init
, (void));
8354 DEF_IMGLIB_FN (void, g_object_unref
, (gpointer
));
8355 DEF_IMGLIB_FN (void, g_error_free
, (GError
*));
8357 Lisp_Object Qgdk_pixbuf
, Qglib
, Qgobject
;
8360 init_svg_functions (void)
8362 HMODULE library
, gdklib
, glib
, gobject
;
8364 if (!(glib
= w32_delayed_load (Qglib
))
8365 || !(gobject
= w32_delayed_load (Qgobject
))
8366 || !(gdklib
= w32_delayed_load (Qgdk_pixbuf
))
8367 || !(library
= w32_delayed_load (Qsvg
)))
8370 LOAD_IMGLIB_FN (library
, rsvg_handle_new
);
8371 LOAD_IMGLIB_FN (library
, rsvg_handle_get_dimensions
);
8372 LOAD_IMGLIB_FN (library
, rsvg_handle_write
);
8373 LOAD_IMGLIB_FN (library
, rsvg_handle_close
);
8374 LOAD_IMGLIB_FN (library
, rsvg_handle_get_pixbuf
);
8376 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_width
);
8377 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_height
);
8378 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_pixels
);
8379 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_rowstride
);
8380 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_colorspace
);
8381 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_n_channels
);
8382 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_has_alpha
);
8383 LOAD_IMGLIB_FN (gdklib
, gdk_pixbuf_get_bits_per_sample
);
8385 LOAD_IMGLIB_FN (gobject
, g_type_init
);
8386 LOAD_IMGLIB_FN (gobject
, g_object_unref
);
8387 LOAD_IMGLIB_FN (glib
, g_error_free
);
8393 /* The following aliases for library functions allow dynamic loading
8394 to be used on some platforms. */
8395 #define fn_rsvg_handle_new rsvg_handle_new
8396 #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions
8397 #define fn_rsvg_handle_write rsvg_handle_write
8398 #define fn_rsvg_handle_close rsvg_handle_close
8399 #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf
8401 #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width
8402 #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height
8403 #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels
8404 #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride
8405 #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace
8406 #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels
8407 #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha
8408 #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample
8410 #define fn_g_type_init g_type_init
8411 #define fn_g_object_unref g_object_unref
8412 #define fn_g_error_free g_error_free
8413 #endif /* !WINDOWSNT */
8415 /* Load SVG image IMG for use on frame F. Value is true if
8419 svg_load (struct frame
*f
, struct image
*img
)
8422 Lisp_Object file_name
;
8424 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8425 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
8426 if (STRINGP (file_name
))
8429 unsigned char *contents
;
8432 file
= x_find_image_file (file_name
);
8433 if (!STRINGP (file
))
8435 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
8439 /* Read the entire file into memory. */
8440 contents
= slurp_file (SSDATA (file
), &size
);
8441 if (contents
== NULL
)
8443 image_error ("Error loading SVG image `%s'", img
->spec
, Qnil
);
8446 /* If the file was slurped into memory properly, parse it. */
8447 success_p
= svg_load_image (f
, img
, contents
, size
);
8450 /* Else its not a file, its a lisp object. Load the image from a
8451 lisp object rather than a file. */
8456 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8457 if (!STRINGP (data
))
8459 image_error ("Invalid image data `%s'", data
, Qnil
);
8462 success_p
= svg_load_image (f
, img
, SDATA (data
), SBYTES (data
));
8468 /* svg_load_image is a helper function for svg_load, which does the
8469 actual loading given contents and size, apart from frame and image
8470 structures, passed from svg_load.
8472 Uses librsvg to do most of the image processing.
8474 Returns true when successful. */
8476 svg_load_image (struct frame
*f
, /* Pointer to emacs frame structure. */
8477 struct image
*img
, /* Pointer to emacs image structure. */
8478 unsigned char *contents
, /* String containing the SVG XML data to be parsed. */
8479 ptrdiff_t size
) /* Size of data in bytes. */
8481 RsvgHandle
*rsvg_handle
;
8482 RsvgDimensionData dimension_data
;
8487 const guint8
*pixels
;
8490 Lisp_Object specified_bg
;
8495 /* g_type_init is a glib function that must be called prior to using
8496 gnome type library functions. */
8498 /* Make a handle to a new rsvg object. */
8499 rsvg_handle
= fn_rsvg_handle_new ();
8501 /* Parse the contents argument and fill in the rsvg_handle. */
8502 fn_rsvg_handle_write (rsvg_handle
, contents
, size
, &err
);
8503 if (err
) goto rsvg_error
;
8505 /* The parsing is complete, rsvg_handle is ready to used, close it
8506 for further writes. */
8507 fn_rsvg_handle_close (rsvg_handle
, &err
);
8508 if (err
) goto rsvg_error
;
8510 fn_rsvg_handle_get_dimensions (rsvg_handle
, &dimension_data
);
8511 if (! check_image_size (f
, dimension_data
.width
, dimension_data
.height
))
8513 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8517 /* We can now get a valid pixel buffer from the svg file, if all
8519 pixbuf
= fn_rsvg_handle_get_pixbuf (rsvg_handle
);
8520 if (!pixbuf
) goto rsvg_error
;
8521 fn_g_object_unref (rsvg_handle
);
8523 /* Extract some meta data from the svg handle. */
8524 width
= fn_gdk_pixbuf_get_width (pixbuf
);
8525 height
= fn_gdk_pixbuf_get_height (pixbuf
);
8526 pixels
= fn_gdk_pixbuf_get_pixels (pixbuf
);
8527 rowstride
= fn_gdk_pixbuf_get_rowstride (pixbuf
);
8529 /* Validate the svg meta data. */
8530 eassert (fn_gdk_pixbuf_get_colorspace (pixbuf
) == GDK_COLORSPACE_RGB
);
8531 eassert (fn_gdk_pixbuf_get_n_channels (pixbuf
) == 4);
8532 eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf
));
8533 eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf
) == 8);
8535 /* Try to create a x pixmap to hold the svg pixmap. */
8536 if (!image_create_x_image_and_pixmap (f
, img
, width
, height
, 0, &ximg
, 0))
8538 fn_g_object_unref (pixbuf
);
8542 init_color_table ();
8544 /* Handle alpha channel by combining the image with a background
8546 specified_bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
8547 if (!STRINGP (specified_bg
)
8548 || !x_defined_color (f
, SSDATA (specified_bg
), &background
, 0))
8551 background
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8552 x_query_color (f
, &background
);
8554 ns_query_color (FRAME_BACKGROUND_COLOR (f
), &background
, 1);
8558 /* SVG pixmaps specify transparency in the last byte, so right
8559 shift 8 bits to get rid of it, since emacs doesn't support
8561 background
.red
>>= 8;
8562 background
.green
>>= 8;
8563 background
.blue
>>= 8;
8565 /* This loop handles opacity values, since Emacs assumes
8566 non-transparent images. Each pixel must be "flattened" by
8567 calculating the resulting color, given the transparency of the
8568 pixel, and the image background color. */
8569 for (y
= 0; y
< height
; ++y
)
8571 for (x
= 0; x
< width
; ++x
)
8581 opacity
= *pixels
++;
8583 red
= ((red
* opacity
)
8584 + (background
.red
* ((1 << 8) - opacity
)));
8585 green
= ((green
* opacity
)
8586 + (background
.green
* ((1 << 8) - opacity
)));
8587 blue
= ((blue
* opacity
)
8588 + (background
.blue
* ((1 << 8) - opacity
)));
8590 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, red
, green
, blue
));
8593 pixels
+= rowstride
- 4 * width
;
8596 #ifdef COLOR_TABLE_SUPPORT
8597 /* Remember colors allocated for this image. */
8598 img
->colors
= colors_in_color_table (&img
->ncolors
);
8599 free_color_table ();
8600 #endif /* COLOR_TABLE_SUPPORT */
8602 fn_g_object_unref (pixbuf
);
8605 img
->height
= height
;
8607 /* Maybe fill in the background field while we have ximg handy.
8608 Casting avoids a GCC warning. */
8609 IMAGE_BACKGROUND (img
, f
, (XImagePtr_or_DC
)ximg
);
8611 /* Put ximg into the image. */
8612 image_put_x_image (f
, img
, ximg
, 0);
8617 fn_g_object_unref (rsvg_handle
);
8618 /* FIXME: Use error->message so the user knows what is the actual
8619 problem with the image. */
8620 image_error ("Error parsing SVG image `%s'", img
->spec
, Qnil
);
8621 fn_g_error_free (err
);
8625 #endif /* defined (HAVE_RSVG) */
8630 /***********************************************************************
8632 ***********************************************************************/
8634 #ifdef HAVE_X_WINDOWS
8635 #define HAVE_GHOSTSCRIPT 1
8636 #endif /* HAVE_X_WINDOWS */
8638 #ifdef HAVE_GHOSTSCRIPT
8640 static bool gs_image_p (Lisp_Object object
);
8641 static bool gs_load (struct frame
*f
, struct image
*img
);
8642 static void gs_clear_image (struct frame
*f
, struct image
*img
);
8644 /* Keyword symbols. */
8646 static Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
8648 /* Indices of image specification fields in gs_format, below. */
8650 enum gs_keyword_index
8668 /* Vector of image_keyword structures describing the format
8669 of valid user-defined image specifications. */
8671 static const struct image_keyword gs_format
[GS_LAST
] =
8673 {":type", IMAGE_SYMBOL_VALUE
, 1},
8674 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8675 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
8676 {":file", IMAGE_STRING_VALUE
, 1},
8677 {":loader", IMAGE_FUNCTION_VALUE
, 0},
8678 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
8679 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8680 {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR
, 0},
8681 {":relief", IMAGE_INTEGER_VALUE
, 0},
8682 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8683 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8684 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8685 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8688 /* Structure describing the image type `ghostscript'. */
8690 static struct image_type gs_type
=
8701 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8704 gs_clear_image (struct frame
*f
, struct image
*img
)
8706 x_clear_image (f
, img
);
8710 /* Return true if OBJECT is a valid Ghostscript image
8714 gs_image_p (Lisp_Object object
)
8716 struct image_keyword fmt
[GS_LAST
];
8720 memcpy (fmt
, gs_format
, sizeof fmt
);
8722 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
8725 /* Bounding box must be a list or vector containing 4 integers. */
8726 tem
= fmt
[GS_BOUNDING_BOX
].value
;
8729 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
8730 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
8735 else if (VECTORP (tem
))
8737 if (ASIZE (tem
) != 4)
8739 for (i
= 0; i
< 4; ++i
)
8740 if (!INTEGERP (AREF (tem
, i
)))
8750 /* Load Ghostscript image IMG for use on frame F. Value is true
8754 gs_load (struct frame
*f
, struct image
*img
)
8756 uprintmax_t printnum1
, printnum2
;
8757 char buffer
[sizeof " " + INT_STRLEN_BOUND (printmax_t
)];
8758 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
8760 double in_width
, in_height
;
8761 Lisp_Object pixel_colors
= Qnil
;
8763 /* Compute pixel size of pixmap needed from the given size in the
8764 image specification. Sizes in the specification are in pt. 1 pt
8765 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8767 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
8768 in_width
= INTEGERP (pt_width
) ? XFASTINT (pt_width
) / 72.0 : 0;
8769 in_width
*= FRAME_RES_X (f
);
8770 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
8771 in_height
= INTEGERP (pt_height
) ? XFASTINT (pt_height
) / 72.0 : 0;
8772 in_height
*= FRAME_RES_Y (f
);
8774 if (! (in_width
<= INT_MAX
&& in_height
<= INT_MAX
8775 && check_image_size (f
, in_width
, in_height
)))
8777 image_error ("Invalid image size (see `max-image-size')", Qnil
, Qnil
);
8780 img
->width
= in_width
;
8781 img
->height
= in_height
;
8783 /* Create the pixmap. */
8784 eassert (img
->pixmap
== NO_PIXMAP
);
8786 if (x_check_image_size (0, img
->width
, img
->height
))
8788 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8790 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
8791 img
->width
, img
->height
,
8792 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
8798 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
8802 /* Call the loader to fill the pixmap. It returns a process object
8803 if successful. We do not record_unwind_protect here because
8804 other places in redisplay like calling window scroll functions
8805 don't either. Let the Lisp loader use `unwind-protect' instead. */
8806 printnum1
= FRAME_X_WINDOW (f
);
8807 printnum2
= img
->pixmap
;
8808 window_and_pixmap_id
8809 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8811 printnum1
= FRAME_FOREGROUND_PIXEL (f
);
8812 printnum2
= FRAME_BACKGROUND_PIXEL (f
);
8814 = make_formatted_string (buffer
, "%"pMu
" %"pMu
, printnum1
, printnum2
);
8816 XSETFRAME (frame
, f
);
8817 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
8819 loader
= intern ("gs-load-image");
8821 img
->lisp_data
= call6 (loader
, frame
, img
->spec
,
8822 make_number (img
->width
),
8823 make_number (img
->height
),
8824 window_and_pixmap_id
,
8826 return PROCESSP (img
->lisp_data
);
8830 /* Kill the Ghostscript process that was started to fill PIXMAP on
8831 frame F. Called from XTread_socket when receiving an event
8832 telling Emacs that Ghostscript has finished drawing. */
8835 x_kill_gs_process (Pixmap pixmap
, struct frame
*f
)
8837 struct image_cache
*c
= FRAME_IMAGE_CACHE (f
);
8842 /* Find the image containing PIXMAP. */
8843 for (i
= 0; i
< c
->used
; ++i
)
8844 if (c
->images
[i
]->pixmap
== pixmap
)
8847 /* Should someone in between have cleared the image cache, for
8848 instance, give up. */
8852 /* Kill the GS process. We should have found PIXMAP in the image
8853 cache and its image should contain a process object. */
8855 eassert (PROCESSP (img
->lisp_data
));
8856 Fkill_process (img
->lisp_data
, Qnil
);
8857 img
->lisp_data
= Qnil
;
8859 #if defined (HAVE_X_WINDOWS)
8861 /* On displays with a mutable colormap, figure out the colors
8862 allocated for the image by looking at the pixels of an XImage for
8864 class = FRAME_X_VISUAL (f
)->class;
8865 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
8871 /* Try to get an XImage for img->pixmep. */
8872 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
8873 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
8878 /* Initialize the color table. */
8879 init_color_table ();
8881 /* For each pixel of the image, look its color up in the
8882 color table. After having done so, the color table will
8883 contain an entry for each color used by the image. */
8884 for (y
= 0; y
< img
->height
; ++y
)
8885 for (x
= 0; x
< img
->width
; ++x
)
8887 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
8888 lookup_pixel_color (f
, pixel
);
8891 /* Record colors in the image. Free color table and XImage. */
8892 #ifdef COLOR_TABLE_SUPPORT
8893 img
->colors
= colors_in_color_table (&img
->ncolors
);
8894 free_color_table ();
8896 XDestroyImage (ximg
);
8898 #if 0 /* This doesn't seem to be the case. If we free the colors
8899 here, we get a BadAccess later in x_clear_image when
8900 freeing the colors. */
8901 /* We have allocated colors once, but Ghostscript has also
8902 allocated colors on behalf of us. So, to get the
8903 reference counts right, free them once. */
8905 x_free_colors (f
, img
->colors
, img
->ncolors
);
8909 image_error ("Cannot get X image of `%s'; colors will not be freed",
8914 #endif /* HAVE_X_WINDOWS */
8916 /* Now that we have the pixmap, compute mask and transform the
8917 image if requested. */
8919 postprocess_image (f
, img
);
8923 #endif /* HAVE_GHOSTSCRIPT */
8926 /***********************************************************************
8928 ***********************************************************************/
8932 DEFUN ("imagep", Fimagep
, Simagep
, 1, 1, 0,
8933 doc
: /* Value is non-nil if SPEC is a valid image specification. */)
8936 return valid_image_p (spec
) ? Qt
: Qnil
;
8940 DEFUN ("lookup-image", Flookup_image
, Slookup_image
, 1, 1, 0, "")
8945 if (valid_image_p (spec
))
8946 id
= lookup_image (SELECTED_FRAME (), spec
);
8949 return make_number (id
);
8952 #endif /* GLYPH_DEBUG */
8955 /***********************************************************************
8957 ***********************************************************************/
8959 DEFUN ("init-image-library", Finit_image_library
, Sinit_image_library
, 1, 1, 0,
8960 doc
: /* Initialize image library implementing image type TYPE.
8961 Return non-nil if TYPE is a supported image type.
8963 If image libraries are loaded dynamically (currently only the case on
8964 MS-Windows), load the library for TYPE if it is not yet loaded, using
8965 the library file(s) specified by `dynamic-library-alist'. */)
8968 return lookup_image_type (type
) ? Qt
: Qnil
;
8971 /* Look up image type TYPE, and return a pointer to its image_type
8972 structure. Return 0 if TYPE is not a known image type. */
8974 static struct image_type
*
8975 lookup_image_type (Lisp_Object type
)
8977 /* Types pbm and xbm are built-in and always available. */
8978 if (EQ (type
, Qpbm
))
8979 return define_image_type (&pbm_type
);
8981 if (EQ (type
, Qxbm
))
8982 return define_image_type (&xbm_type
);
8984 #if defined (HAVE_XPM) || defined (HAVE_NS)
8985 if (EQ (type
, Qxpm
))
8986 return define_image_type (&xpm_type
);
8989 #if defined (HAVE_JPEG) || defined (HAVE_NS)
8990 if (EQ (type
, Qjpeg
))
8991 return define_image_type (&jpeg_type
);
8994 #if defined (HAVE_TIFF) || defined (HAVE_NS)
8995 if (EQ (type
, Qtiff
))
8996 return define_image_type (&tiff_type
);
8999 #if defined (HAVE_GIF) || defined (HAVE_NS)
9000 if (EQ (type
, Qgif
))
9001 return define_image_type (&gif_type
);
9004 #if defined (HAVE_PNG) || defined (HAVE_NS)
9005 if (EQ (type
, Qpng
))
9006 return define_image_type (&png_type
);
9009 #if defined (HAVE_RSVG)
9010 if (EQ (type
, Qsvg
))
9011 return define_image_type (&svg_type
);
9014 #if defined (HAVE_IMAGEMAGICK)
9015 if (EQ (type
, Qimagemagick
))
9016 return define_image_type (&imagemagick_type
);
9019 #ifdef HAVE_GHOSTSCRIPT
9020 if (EQ (type
, Qpostscript
))
9021 return define_image_type (&gs_type
);
9027 /* Reset image_types before dumping.
9028 Called from Fdump_emacs. */
9031 reset_image_types (void)
9035 struct image_type
*next
= image_types
->next
;
9036 xfree (image_types
);
9042 syms_of_image (void)
9044 /* Initialize this only once; it will be reset before dumping. */
9047 /* Must be defined now because we're going to update it below, while
9048 defining the supported image types. */
9049 DEFVAR_LISP ("image-types", Vimage_types
,
9050 doc
: /* List of potentially supported image types.
9051 Each element of the list is a symbol for an image type, like 'jpeg or 'png.
9052 To check whether it is really supported, use `image-type-available-p'. */);
9053 Vimage_types
= Qnil
;
9055 DEFVAR_LISP ("max-image-size", Vmax_image_size
,
9056 doc
: /* Maximum size of images.
9057 Emacs will not load an image into memory if its pixel width or
9058 pixel height exceeds this limit.
9060 If the value is an integer, it directly specifies the maximum
9061 image height and width, measured in pixels. If it is a floating
9062 point number, it specifies the maximum image height and width
9063 as a ratio to the frame height and width. If the value is
9064 non-numeric, there is no explicit limit on the size of images. */);
9065 Vmax_image_size
= make_float (MAX_IMAGE_SIZE
);
9067 DEFSYM (Qcount
, "count");
9068 DEFSYM (Qextension_data
, "extension-data");
9069 DEFSYM (Qdelay
, "delay");
9071 DEFSYM (QCascent
, ":ascent");
9072 DEFSYM (QCmargin
, ":margin");
9073 DEFSYM (QCrelief
, ":relief");
9074 DEFSYM (QCconversion
, ":conversion");
9075 DEFSYM (QCcolor_symbols
, ":color-symbols");
9076 DEFSYM (QCheuristic_mask
, ":heuristic-mask");
9077 DEFSYM (QCindex
, ":index");
9078 DEFSYM (QCgeometry
, ":geometry");
9079 DEFSYM (QCcrop
, ":crop");
9080 DEFSYM (QCrotation
, ":rotation");
9081 DEFSYM (QCmatrix
, ":matrix");
9082 DEFSYM (QCcolor_adjustment
, ":color-adjustment");
9083 DEFSYM (QCmask
, ":mask");
9085 DEFSYM (Qlaplace
, "laplace");
9086 DEFSYM (Qemboss
, "emboss");
9087 DEFSYM (Qedge_detection
, "edge-detection");
9088 DEFSYM (Qheuristic
, "heuristic");
9090 DEFSYM (Qpostscript
, "postscript");
9091 DEFSYM (QCmax_width
, ":max-width");
9092 DEFSYM (QCmax_height
, ":max-height");
9093 #ifdef HAVE_GHOSTSCRIPT
9094 ADD_IMAGE_TYPE (Qpostscript
);
9095 DEFSYM (QCloader
, ":loader");
9096 DEFSYM (QCbounding_box
, ":bounding-box");
9097 DEFSYM (QCpt_width
, ":pt-width");
9098 DEFSYM (QCpt_height
, ":pt-height");
9099 #endif /* HAVE_GHOSTSCRIPT */
9102 DEFSYM (Qlibpng_version
, "libpng-version");
9103 Fset (Qlibpng_version
,
9105 make_number (PNG_LIBPNG_VER
)
9112 DEFSYM (Qpbm
, "pbm");
9113 ADD_IMAGE_TYPE (Qpbm
);
9115 DEFSYM (Qxbm
, "xbm");
9116 ADD_IMAGE_TYPE (Qxbm
);
9118 #if defined (HAVE_XPM) || defined (HAVE_NS)
9119 DEFSYM (Qxpm
, "xpm");
9120 ADD_IMAGE_TYPE (Qxpm
);
9123 #if defined (HAVE_JPEG) || defined (HAVE_NS)
9124 DEFSYM (Qjpeg
, "jpeg");
9125 ADD_IMAGE_TYPE (Qjpeg
);
9128 #if defined (HAVE_TIFF) || defined (HAVE_NS)
9129 DEFSYM (Qtiff
, "tiff");
9130 ADD_IMAGE_TYPE (Qtiff
);
9133 #if defined (HAVE_GIF) || defined (HAVE_NS)
9134 DEFSYM (Qgif
, "gif");
9135 ADD_IMAGE_TYPE (Qgif
);
9138 #if defined (HAVE_PNG) || defined (HAVE_NS)
9139 DEFSYM (Qpng
, "png");
9140 ADD_IMAGE_TYPE (Qpng
);
9143 #if defined (HAVE_IMAGEMAGICK)
9144 DEFSYM (Qimagemagick
, "imagemagick");
9145 ADD_IMAGE_TYPE (Qimagemagick
);
9148 #if defined (HAVE_RSVG)
9149 DEFSYM (Qsvg
, "svg");
9150 ADD_IMAGE_TYPE (Qsvg
);
9152 /* Other libraries used directly by svg code. */
9153 DEFSYM (Qgdk_pixbuf
, "gdk-pixbuf");
9154 DEFSYM (Qglib
, "glib");
9155 DEFSYM (Qgobject
, "gobject");
9156 #endif /* HAVE_NTGUI */
9157 #endif /* HAVE_RSVG */
9159 defsubr (&Sinit_image_library
);
9160 #ifdef HAVE_IMAGEMAGICK
9161 defsubr (&Simagemagick_types
);
9163 defsubr (&Sclear_image_cache
);
9164 defsubr (&Simage_flush
);
9165 defsubr (&Simage_size
);
9166 defsubr (&Simage_mask_p
);
9167 defsubr (&Simage_metadata
);
9171 defsubr (&Slookup_image
);
9174 DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images
,
9175 doc
: /* Non-nil means always draw a cross over disabled images.
9176 Disabled images are those having a `:conversion disabled' property.
9177 A cross is always drawn on black & white displays. */);
9178 cross_disabled_images
= 0;
9180 DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path
,
9181 doc
: /* List of directories to search for window system bitmap files. */);
9182 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
9184 DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay
,
9185 doc
: /* Maximum time after which images are removed from the cache.
9186 When an image has not been displayed this many seconds, Emacs
9187 automatically removes it from the image cache. If the cache contains
9188 a large number of images, the actual eviction time may be shorter.
9189 The value can also be nil, meaning the cache is never cleared.
9191 The function `clear-image-cache' disregards this variable. */);
9192 Vimage_cache_eviction_delay
= make_number (300);
9193 #ifdef HAVE_IMAGEMAGICK
9194 DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type
,
9195 doc
: /* Integer indicating which ImageMagick rendering method to use.
9197 0 -- the default method (pixel pushing)
9198 1 -- a newer method ("MagickExportImagePixels") that may perform
9199 better (speed etc) in some cases, but has not been as thoroughly
9200 tested with Emacs as the default method. This method requires
9201 ImageMagick version 6.4.6 (approximately) or later.
9203 /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */
9204 imagemagick_render_type
= 0;