Reworked.
[emacs/old-mirror.git] / src / image.c
blob879bbf745a3894b40168d67f57e24aa34aaf85ec
1 /* Functions for image support on window system.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006 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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include <ctype.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
31 /* This makes the fields of a Display accessible, in Xlib header files. */
33 #define XLIB_ILLEGAL_ACCESS
35 #include "lisp.h"
36 #include "frame.h"
37 #include "window.h"
38 #include "dispextern.h"
39 #include "blockinput.h"
40 #include "systime.h"
41 #include <epaths.h>
44 #ifdef HAVE_X_WINDOWS
45 #include "xterm.h"
46 #include <sys/types.h>
47 #include <sys/stat.h>
49 #define COLOR_TABLE_SUPPORT 1
51 typedef struct x_bitmap_record Bitmap_Record;
52 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
53 #define NO_PIXMAP None
55 #define RGB_PIXEL_COLOR unsigned long
57 #define PIX_MASK_RETAIN 0
58 #define PIX_MASK_DRAW 1
59 #endif /* HAVE_X_WINDOWS */
62 #ifdef HAVE_NTGUI
63 #include "w32term.h"
65 /* W32_TODO : Color tables on W32. */
66 #undef COLOR_TABLE_SUPPORT
68 typedef struct w32_bitmap_record Bitmap_Record;
69 #define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y)
70 #define NO_PIXMAP 0
72 #define RGB_PIXEL_COLOR COLORREF
74 #define PIX_MASK_RETAIN 0
75 #define PIX_MASK_DRAW 1
77 #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual
78 #define x_defined_color w32_defined_color
79 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
80 #endif /* HAVE_NTGUI */
83 #ifdef MAC_OS
84 #include "macterm.h"
85 #include <sys/stat.h>
86 #ifndef MAC_OSX
87 #include <alloca.h>
88 #include <sys/param.h>
89 #endif
90 #if TARGET_API_MAC_CARBON
91 #ifdef MAC_OSX
92 #include <QuickTime/QuickTime.h>
93 #else /* not MAC_OSX */
94 #include <QuickTime.h>
95 #endif /* not MAC_OSX */
96 #else /* not TARGET_API_MAC_CARBON */
97 #include <Windows.h>
98 #include <Gestalt.h>
99 #include <TextUtils.h>
100 #include <ImageCompression.h>
101 #include <QuickTimeComponents.h>
102 #endif /* not TARGET_API_MAC_CARBON */
104 /* MAC_TODO : Color tables on Mac. */
105 #undef COLOR_TABLE_SUPPORT
107 #define ZPixmap 0 /* arbitrary */
108 typedef struct mac_bitmap_record Bitmap_Record;
110 #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y)
111 #define NO_PIXMAP 0
113 #define RGB_PIXEL_COLOR unsigned long
115 /* A black pixel in a mask bitmap/pixmap means ``draw a source
116 pixel''. A white pixel means ``retain the current pixel''. */
117 #define PIX_MASK_DRAW RGB_TO_ULONG(0,0,0)
118 #define PIX_MASK_RETAIN RGB_TO_ULONG(255,255,255)
120 #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual
121 #define x_defined_color mac_defined_color
122 #define DefaultDepthOfScreen(screen) (one_mac_display_info.n_planes)
123 #define XDrawLine(display, w, gc, x1, y1, x2, y2) \
124 mac_draw_line_to_pixmap(display, w, gc, x1, y1, x2, y2)
126 #endif /* MAC_OS */
129 /* Search path for bitmap files. */
131 Lisp_Object Vx_bitmap_file_path;
134 static void x_disable_image P_ ((struct frame *, struct image *));
135 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
136 Lisp_Object));
138 static void init_color_table P_ ((void));
139 static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
140 #ifdef COLOR_TABLE_SUPPORT
141 static void free_color_table P_ ((void));
142 static unsigned long *colors_in_color_table P_ ((int *n));
143 static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
144 #endif
146 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
147 id, which is just an int that this section returns. Bitmaps are
148 reference counted so they can be shared among frames.
150 Bitmap indices are guaranteed to be > 0, so a negative number can
151 be used to indicate no bitmap.
153 If you use x_create_bitmap_from_data, then you must keep track of
154 the bitmaps yourself. That is, creating a bitmap from the same
155 data more than once will not be caught. */
157 #ifdef MAC_OS
159 static XImagePtr
160 XGetImage (display, pixmap, x, y, width, height, plane_mask, format)
161 Display *display; /* not used */
162 Pixmap pixmap;
163 int x, y; /* not used */
164 unsigned int width, height; /* not used */
165 unsigned long plane_mask; /* not used */
166 int format; /* not used */
168 #if GLYPH_DEBUG
169 xassert (x == 0 && y == 0);
171 Rect ri, rp;
172 SetRect (&ri, 0, 0, width, height);
173 xassert (EqualRect (&ri, GetPixBounds (GetGWorldPixMap (pixmap), &rp)));
175 xassert (! (pixelsLocked & GetPixelsState (GetGWorldPixMap (pixmap))));
176 #endif
178 LockPixels (GetGWorldPixMap (pixmap));
180 return pixmap;
183 static void
184 XPutPixel (ximage, x, y, pixel)
185 XImagePtr ximage;
186 int x, y;
187 unsigned long pixel;
189 PixMapHandle pixmap = GetGWorldPixMap (ximage);
190 short depth = GetPixDepth (pixmap);
192 if (depth == 32)
194 char *base_addr = GetPixBaseAddr (pixmap);
195 short row_bytes = GetPixRowBytes (pixmap);
197 ((unsigned long *) (base_addr + y * row_bytes))[x] = pixel;
199 else if (depth == 1)
201 char *base_addr = GetPixBaseAddr (pixmap);
202 short row_bytes = GetPixRowBytes (pixmap);
204 if (pixel == PIX_MASK_DRAW)
205 base_addr[y * row_bytes + x / 8] |= (1 << 7) >> (x & 7);
206 else
207 base_addr[y * row_bytes + x / 8] &= ~((1 << 7) >> (x & 7));
209 else
211 CGrafPtr old_port;
212 GDHandle old_gdh;
213 RGBColor color;
215 GetGWorld (&old_port, &old_gdh);
216 SetGWorld (ximage, NULL);
218 color.red = RED16_FROM_ULONG (pixel);
219 color.green = GREEN16_FROM_ULONG (pixel);
220 color.blue = BLUE16_FROM_ULONG (pixel);
222 SetCPixel (x, y, &color);
224 SetGWorld (old_port, old_gdh);
228 static unsigned long
229 XGetPixel (ximage, x, y)
230 XImagePtr ximage;
231 int x, y;
233 PixMapHandle pixmap = GetGWorldPixMap (ximage);
234 short depth = GetPixDepth (pixmap);
236 if (depth == 32)
238 char *base_addr = GetPixBaseAddr (pixmap);
239 short row_bytes = GetPixRowBytes (pixmap);
241 return ((unsigned long *) (base_addr + y * row_bytes))[x];
243 else if (depth == 1)
245 char *base_addr = GetPixBaseAddr (pixmap);
246 short row_bytes = GetPixRowBytes (pixmap);
248 if (base_addr[y * row_bytes + x / 8] & (1 << (~x & 7)))
249 return PIX_MASK_DRAW;
250 else
251 return PIX_MASK_RETAIN;
253 else
255 CGrafPtr old_port;
256 GDHandle old_gdh;
257 RGBColor color;
259 GetGWorld (&old_port, &old_gdh);
260 SetGWorld (ximage, NULL);
262 GetCPixel (x, y, &color);
264 SetGWorld (old_port, old_gdh);
265 return RGB_TO_ULONG (color.red >> 8, color.green >> 8, color.blue >> 8);
269 static void
270 XDestroyImage (ximg)
271 XImagePtr ximg;
273 UnlockPixels (GetGWorldPixMap (ximg));
275 #endif /* MAC_OS */
278 /* Functions to access the contents of a bitmap, given an id. */
281 x_bitmap_height (f, id)
282 FRAME_PTR f;
283 int id;
285 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
289 x_bitmap_width (f, id)
290 FRAME_PTR f;
291 int id;
293 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
296 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI)
298 x_bitmap_pixmap (f, id)
299 FRAME_PTR f;
300 int id;
302 return (int) FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
304 #endif
306 #ifdef HAVE_X_WINDOWS
308 x_bitmap_mask (f, id)
309 FRAME_PTR f;
310 int id;
312 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].mask;
314 #endif
316 /* Allocate a new bitmap record. Returns index of new record. */
318 static int
319 x_allocate_bitmap_record (f)
320 FRAME_PTR f;
322 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
323 int i;
325 if (dpyinfo->bitmaps == NULL)
327 dpyinfo->bitmaps_size = 10;
328 dpyinfo->bitmaps
329 = (Bitmap_Record *) xmalloc (dpyinfo->bitmaps_size * sizeof (Bitmap_Record));
330 dpyinfo->bitmaps_last = 1;
331 return 1;
334 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
335 return ++dpyinfo->bitmaps_last;
337 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
338 if (dpyinfo->bitmaps[i].refcount == 0)
339 return i + 1;
341 dpyinfo->bitmaps_size *= 2;
342 dpyinfo->bitmaps
343 = (Bitmap_Record *) xrealloc (dpyinfo->bitmaps,
344 dpyinfo->bitmaps_size * sizeof (Bitmap_Record));
345 return ++dpyinfo->bitmaps_last;
348 /* Add one reference to the reference count of the bitmap with id ID. */
350 void
351 x_reference_bitmap (f, id)
352 FRAME_PTR f;
353 int id;
355 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
358 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
361 x_create_bitmap_from_data (f, bits, width, height)
362 struct frame *f;
363 char *bits;
364 unsigned int width, height;
366 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
367 int id;
369 #ifdef HAVE_X_WINDOWS
370 Pixmap bitmap;
371 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
372 bits, width, height);
373 if (! bitmap)
374 return -1;
375 #endif /* HAVE_X_WINDOWS */
377 #ifdef HAVE_NTGUI
378 Pixmap bitmap;
379 bitmap = CreateBitmap (width, height,
380 FRAME_X_DISPLAY_INFO (XFRAME (frame))->n_planes,
381 FRAME_X_DISPLAY_INFO (XFRAME (frame))->n_cbits,
382 bits);
383 if (! bitmap)
384 return -1;
385 #endif /* HAVE_NTGUI */
387 #ifdef MAC_OS
388 /* MAC_TODO: for now fail if width is not mod 16 (toolbox requires it) */
389 if (width % 16 != 0)
390 return -1;
391 #endif
393 id = x_allocate_bitmap_record (f);
394 #ifdef MAC_OS
395 dpyinfo->bitmaps[id - 1].bitmap_data = (char *) xmalloc (height * width);
396 if (! dpyinfo->bitmaps[id - 1].bitmap_data)
397 return -1;
398 bcopy (bits, dpyinfo->bitmaps[id - 1].bitmap_data, height * width);
399 #endif /* MAC_OS */
401 dpyinfo->bitmaps[id - 1].file = NULL;
402 dpyinfo->bitmaps[id - 1].height = height;
403 dpyinfo->bitmaps[id - 1].width = width;
404 dpyinfo->bitmaps[id - 1].refcount = 1;
406 #ifdef HAVE_X_WINDOWS
407 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
408 dpyinfo->bitmaps[id - 1].have_mask = 0;
409 dpyinfo->bitmaps[id - 1].depth = 1;
410 #endif /* HAVE_X_WINDOWS */
412 #ifdef HAVE_NTGUI
413 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
414 dpyinfo->bitmaps[id - 1].hinst = NULL;
415 dpyinfo->bitmaps[id - 1].depth = 1;
416 #endif /* HAVE_NTGUI */
418 return id;
421 /* Create bitmap from file FILE for frame F. */
424 x_create_bitmap_from_file (f, file)
425 struct frame *f;
426 Lisp_Object file;
428 #ifdef MAC_OS
429 return -1; /* MAC_TODO : bitmap support */
430 #endif /* MAC_OS */
432 #ifdef HAVE_NTGUI
433 return -1; /* W32_TODO : bitmap support */
434 #endif /* HAVE_NTGUI */
436 #ifdef HAVE_X_WINDOWS
437 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
438 unsigned int width, height;
439 Pixmap bitmap;
440 int xhot, yhot, result, id;
441 Lisp_Object found;
442 int fd;
443 char *filename;
445 /* Look for an existing bitmap with the same name. */
446 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
448 if (dpyinfo->bitmaps[id].refcount
449 && dpyinfo->bitmaps[id].file
450 && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file)))
452 ++dpyinfo->bitmaps[id].refcount;
453 return id + 1;
457 /* Search bitmap-file-path for the file, if appropriate. */
458 fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil);
459 if (fd < 0)
460 return -1;
461 emacs_close (fd);
463 filename = (char *) SDATA (found);
465 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
466 filename, &width, &height, &bitmap, &xhot, &yhot);
467 if (result != BitmapSuccess)
468 return -1;
470 id = x_allocate_bitmap_record (f);
471 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
472 dpyinfo->bitmaps[id - 1].have_mask = 0;
473 dpyinfo->bitmaps[id - 1].refcount = 1;
474 dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (SBYTES (file) + 1);
475 dpyinfo->bitmaps[id - 1].depth = 1;
476 dpyinfo->bitmaps[id - 1].height = height;
477 dpyinfo->bitmaps[id - 1].width = width;
478 strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file));
480 return id;
481 #endif /* HAVE_X_WINDOWS */
484 /* Free bitmap B. */
486 static void
487 Free_Bitmap_Record (dpyinfo, bm)
488 Display_Info *dpyinfo;
489 Bitmap_Record *bm;
491 #ifdef HAVE_X_WINDOWS
492 XFreePixmap (dpyinfo->display, bm->pixmap);
493 if (bm->have_mask)
494 XFreePixmap (dpyinfo->display, bm->mask);
495 #endif /* HAVE_X_WINDOWS */
497 #ifdef HAVE_NTGUI
498 DeleteObject (bm->pixmap);
499 #endif /* HAVE_NTGUI */
501 #ifdef MAC_OS
502 xfree (bm->bitmap_data); /* Added ++kfs */
503 bm->bitmap_data = NULL;
504 #endif /* MAC_OS */
506 if (bm->file)
508 xfree (bm->file);
509 bm->file = NULL;
513 /* Remove reference to bitmap with id number ID. */
515 void
516 x_destroy_bitmap (f, id)
517 FRAME_PTR f;
518 int id;
520 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
522 if (id > 0)
524 Bitmap_Record *bm = &dpyinfo->bitmaps[id - 1];
526 if (--bm->refcount == 0)
528 BLOCK_INPUT;
529 Free_Bitmap_Record (dpyinfo, bm);
530 UNBLOCK_INPUT;
535 /* Free all the bitmaps for the display specified by DPYINFO. */
537 void
538 x_destroy_all_bitmaps (dpyinfo)
539 Display_Info *dpyinfo;
541 int i;
542 Bitmap_Record *bm = dpyinfo->bitmaps;
544 for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++)
545 if (bm->refcount > 0)
546 Free_Bitmap_Record (dpyinfo, bm);
548 dpyinfo->bitmaps_last = 0;
552 #ifdef HAVE_X_WINDOWS
554 /* Useful functions defined in the section
555 `Image type independent image structures' below. */
557 static unsigned long four_corners_best P_ ((XImagePtr ximg, unsigned long width,
558 unsigned long height));
560 static int x_create_x_image_and_pixmap P_ ((struct frame *f, int width, int height,
561 int depth, XImagePtr *ximg,
562 Pixmap *pixmap));
564 static void x_destroy_x_image P_ ((XImagePtr ximg));
567 /* Create a mask of a bitmap. Note is this not a perfect mask.
568 It's nicer with some borders in this context */
571 x_create_bitmap_mask (f, id)
572 struct frame *f;
573 int id;
575 Pixmap pixmap, mask;
576 XImagePtr ximg, mask_img;
577 unsigned long width, height;
578 int result;
579 unsigned long bg;
580 unsigned long x, y, xp, xm, yp, ym;
581 GC gc;
583 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
585 if (!(id > 0))
586 return -1;
588 pixmap = x_bitmap_pixmap (f, id);
589 width = x_bitmap_width (f, id);
590 height = x_bitmap_height (f, id);
592 BLOCK_INPUT;
593 ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height,
594 ~0, ZPixmap);
596 if (!ximg)
598 UNBLOCK_INPUT;
599 return -1;
602 result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask);
604 UNBLOCK_INPUT;
605 if (!result)
607 XDestroyImage (ximg);
608 return -1;
611 bg = four_corners_best (ximg, width, height);
613 for (y = 0; y < ximg->height; ++y)
615 for (x = 0; x < ximg->width; ++x)
617 xp = x != ximg->width - 1 ? x + 1 : 0;
618 xm = x != 0 ? x - 1 : ximg->width - 1;
619 yp = y != ximg->height - 1 ? y + 1 : 0;
620 ym = y != 0 ? y - 1 : ximg->height - 1;
621 if (XGetPixel (ximg, x, y) == bg
622 && XGetPixel (ximg, x, yp) == bg
623 && XGetPixel (ximg, x, ym) == bg
624 && XGetPixel (ximg, xp, y) == bg
625 && XGetPixel (ximg, xp, yp) == bg
626 && XGetPixel (ximg, xp, ym) == bg
627 && XGetPixel (ximg, xm, y) == bg
628 && XGetPixel (ximg, xm, yp) == bg
629 && XGetPixel (ximg, xm, ym) == bg)
630 XPutPixel (mask_img, x, y, 0);
631 else
632 XPutPixel (mask_img, x, y, 1);
636 xassert (interrupt_input_blocked);
637 gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL);
638 XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0,
639 width, height);
640 XFreeGC (FRAME_X_DISPLAY (f), gc);
642 dpyinfo->bitmaps[id - 1].have_mask = 1;
643 dpyinfo->bitmaps[id - 1].mask = mask;
645 XDestroyImage (ximg);
646 x_destroy_x_image (mask_img);
648 return 0;
651 #endif /* HAVE_X_WINDOWS */
654 /***********************************************************************
655 Image types
656 ***********************************************************************/
658 /* Value is the number of elements of vector VECTOR. */
660 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
662 /* List of supported image types. Use define_image_type to add new
663 types. Use lookup_image_type to find a type for a given symbol. */
665 static struct image_type *image_types;
667 /* A list of symbols, one for each supported image type. */
669 Lisp_Object Vimage_types;
671 /* An alist of image types and libraries that implement the type. */
673 Lisp_Object Vimage_library_alist;
675 /* Cache for delayed-loading image types. */
677 static Lisp_Object Vimage_type_cache;
679 /* The symbol `xbm' which is used as the type symbol for XBM images. */
681 Lisp_Object Qxbm;
683 /* Keywords. */
685 extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
686 extern Lisp_Object QCdata, QCtype;
687 extern Lisp_Object Qcenter;
688 Lisp_Object QCascent, QCmargin, QCrelief;
689 Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
690 Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
692 /* Other symbols. */
694 Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
696 /* Time in seconds after which images should be removed from the cache
697 if not displayed. */
699 Lisp_Object Vimage_cache_eviction_delay;
701 /* Function prototypes. */
703 static Lisp_Object define_image_type P_ ((struct image_type *type, int loaded));
704 static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
705 static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
706 static void x_laplace P_ ((struct frame *, struct image *));
707 static void x_emboss P_ ((struct frame *, struct image *));
708 static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
709 Lisp_Object));
711 #define CACHE_IMAGE_TYPE(type, status) \
712 do { Vimage_type_cache = Fcons (Fcons (type, status), Vimage_type_cache); } while (0)
714 #define ADD_IMAGE_TYPE(type) \
715 do { Vimage_types = Fcons (type, Vimage_types); } while (0)
717 /* Define a new image type from TYPE. This adds a copy of TYPE to
718 image_types and caches the loading status of TYPE. */
720 static Lisp_Object
721 define_image_type (type, loaded)
722 struct image_type *type;
723 int loaded;
725 Lisp_Object success;
727 if (!loaded)
728 success = Qnil;
729 else
731 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
732 The initialized data segment is read-only. */
733 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
734 bcopy (type, p, sizeof *p);
735 p->next = image_types;
736 image_types = p;
737 success = Qt;
740 CACHE_IMAGE_TYPE (*type->type, success);
741 return success;
745 /* Look up image type SYMBOL, and return a pointer to its image_type
746 structure. Value is null if SYMBOL is not a known image type. */
748 static INLINE struct image_type *
749 lookup_image_type (symbol)
750 Lisp_Object symbol;
752 struct image_type *type;
754 /* We must initialize the image-type if it hasn't been already. */
755 if (NILP (Finit_image_library (symbol, Vimage_library_alist)))
756 return 0; /* unimplemented */
758 for (type = image_types; type; type = type->next)
759 if (EQ (symbol, *type->type))
760 break;
762 return type;
766 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
767 valid image specification is a list whose car is the symbol
768 `image', and whose rest is a property list. The property list must
769 contain a value for key `:type'. That value must be the name of a
770 supported image type. The rest of the property list depends on the
771 image type. */
774 valid_image_p (object)
775 Lisp_Object object;
777 int valid_p = 0;
779 if (IMAGEP (object))
781 Lisp_Object tem;
783 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
784 if (EQ (XCAR (tem), QCtype))
786 tem = XCDR (tem);
787 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
789 struct image_type *type;
790 type = lookup_image_type (XCAR (tem));
791 if (type)
792 valid_p = type->valid_p (object);
795 break;
799 return valid_p;
803 /* Log error message with format string FORMAT and argument ARG.
804 Signaling an error, e.g. when an image cannot be loaded, is not a
805 good idea because this would interrupt redisplay, and the error
806 message display would lead to another redisplay. This function
807 therefore simply displays a message. */
809 static void
810 image_error (format, arg1, arg2)
811 char *format;
812 Lisp_Object arg1, arg2;
814 add_to_log (format, arg1, arg2);
819 /***********************************************************************
820 Image specifications
821 ***********************************************************************/
823 enum image_value_type
825 IMAGE_DONT_CHECK_VALUE_TYPE,
826 IMAGE_STRING_VALUE,
827 IMAGE_STRING_OR_NIL_VALUE,
828 IMAGE_SYMBOL_VALUE,
829 IMAGE_POSITIVE_INTEGER_VALUE,
830 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
831 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
832 IMAGE_ASCENT_VALUE,
833 IMAGE_INTEGER_VALUE,
834 IMAGE_FUNCTION_VALUE,
835 IMAGE_NUMBER_VALUE,
836 IMAGE_BOOL_VALUE
839 /* Structure used when parsing image specifications. */
841 struct image_keyword
843 /* Name of keyword. */
844 char *name;
846 /* The type of value allowed. */
847 enum image_value_type type;
849 /* Non-zero means key must be present. */
850 int mandatory_p;
852 /* Used to recognize duplicate keywords in a property list. */
853 int count;
855 /* The value that was found. */
856 Lisp_Object value;
860 static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
861 int, Lisp_Object));
862 static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
865 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
866 has the format (image KEYWORD VALUE ...). One of the keyword/
867 value pairs must be `:type TYPE'. KEYWORDS is a vector of
868 image_keywords structures of size NKEYWORDS describing other
869 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
871 static int
872 parse_image_spec (spec, keywords, nkeywords, type)
873 Lisp_Object spec;
874 struct image_keyword *keywords;
875 int nkeywords;
876 Lisp_Object type;
878 int i;
879 Lisp_Object plist;
881 if (!IMAGEP (spec))
882 return 0;
884 plist = XCDR (spec);
885 while (CONSP (plist))
887 Lisp_Object key, value;
889 /* First element of a pair must be a symbol. */
890 key = XCAR (plist);
891 plist = XCDR (plist);
892 if (!SYMBOLP (key))
893 return 0;
895 /* There must follow a value. */
896 if (!CONSP (plist))
897 return 0;
898 value = XCAR (plist);
899 plist = XCDR (plist);
901 /* Find key in KEYWORDS. Error if not found. */
902 for (i = 0; i < nkeywords; ++i)
903 if (strcmp (keywords[i].name, SDATA (SYMBOL_NAME (key))) == 0)
904 break;
906 if (i == nkeywords)
907 continue;
909 /* Record that we recognized the keyword. If a keywords
910 was found more than once, it's an error. */
911 keywords[i].value = value;
912 ++keywords[i].count;
914 if (keywords[i].count > 1)
915 return 0;
917 /* Check type of value against allowed type. */
918 switch (keywords[i].type)
920 case IMAGE_STRING_VALUE:
921 if (!STRINGP (value))
922 return 0;
923 break;
925 case IMAGE_STRING_OR_NIL_VALUE:
926 if (!STRINGP (value) && !NILP (value))
927 return 0;
928 break;
930 case IMAGE_SYMBOL_VALUE:
931 if (!SYMBOLP (value))
932 return 0;
933 break;
935 case IMAGE_POSITIVE_INTEGER_VALUE:
936 if (!INTEGERP (value) || XINT (value) <= 0)
937 return 0;
938 break;
940 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
941 if (INTEGERP (value) && XINT (value) >= 0)
942 break;
943 if (CONSP (value)
944 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
945 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
946 break;
947 return 0;
949 case IMAGE_ASCENT_VALUE:
950 if (SYMBOLP (value) && EQ (value, Qcenter))
951 break;
952 else if (INTEGERP (value)
953 && XINT (value) >= 0
954 && XINT (value) <= 100)
955 break;
956 return 0;
958 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
959 if (!INTEGERP (value) || XINT (value) < 0)
960 return 0;
961 break;
963 case IMAGE_DONT_CHECK_VALUE_TYPE:
964 break;
966 case IMAGE_FUNCTION_VALUE:
967 value = indirect_function (value);
968 if (SUBRP (value)
969 || COMPILEDP (value)
970 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
971 break;
972 return 0;
974 case IMAGE_NUMBER_VALUE:
975 if (!INTEGERP (value) && !FLOATP (value))
976 return 0;
977 break;
979 case IMAGE_INTEGER_VALUE:
980 if (!INTEGERP (value))
981 return 0;
982 break;
984 case IMAGE_BOOL_VALUE:
985 if (!NILP (value) && !EQ (value, Qt))
986 return 0;
987 break;
989 default:
990 abort ();
991 break;
994 if (EQ (key, QCtype) && !EQ (type, value))
995 return 0;
998 /* Check that all mandatory fields are present. */
999 for (i = 0; i < nkeywords; ++i)
1000 if (keywords[i].mandatory_p && keywords[i].count == 0)
1001 return 0;
1003 return NILP (plist);
1007 /* Return the value of KEY in image specification SPEC. Value is nil
1008 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
1009 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
1011 static Lisp_Object
1012 image_spec_value (spec, key, found)
1013 Lisp_Object spec, key;
1014 int *found;
1016 Lisp_Object tail;
1018 xassert (valid_image_p (spec));
1020 for (tail = XCDR (spec);
1021 CONSP (tail) && CONSP (XCDR (tail));
1022 tail = XCDR (XCDR (tail)))
1024 if (EQ (XCAR (tail), key))
1026 if (found)
1027 *found = 1;
1028 return XCAR (XCDR (tail));
1032 if (found)
1033 *found = 0;
1034 return Qnil;
1038 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
1039 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
1040 PIXELS non-nil means return the size in pixels, otherwise return the
1041 size in canonical character units.
1042 FRAME is the frame on which the image will be displayed. FRAME nil
1043 or omitted means use the selected frame. */)
1044 (spec, pixels, frame)
1045 Lisp_Object spec, pixels, frame;
1047 Lisp_Object size;
1049 size = Qnil;
1050 if (valid_image_p (spec))
1052 struct frame *f = check_x_frame (frame);
1053 int id = lookup_image (f, spec);
1054 struct image *img = IMAGE_FROM_ID (f, id);
1055 int width = img->width + 2 * img->hmargin;
1056 int height = img->height + 2 * img->vmargin;
1058 if (NILP (pixels))
1059 size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
1060 make_float ((double) height / FRAME_LINE_HEIGHT (f)));
1061 else
1062 size = Fcons (make_number (width), make_number (height));
1064 else
1065 error ("Invalid image specification");
1067 return size;
1071 DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
1072 doc: /* Return t if image SPEC has a mask bitmap.
1073 FRAME is the frame on which the image will be displayed. FRAME nil
1074 or omitted means use the selected frame. */)
1075 (spec, frame)
1076 Lisp_Object spec, frame;
1078 Lisp_Object mask;
1080 mask = Qnil;
1081 if (valid_image_p (spec))
1083 struct frame *f = check_x_frame (frame);
1084 int id = lookup_image (f, spec);
1085 struct image *img = IMAGE_FROM_ID (f, id);
1086 if (img->mask)
1087 mask = Qt;
1089 else
1090 error ("Invalid image specification");
1092 return mask;
1096 /***********************************************************************
1097 Image type independent image structures
1098 ***********************************************************************/
1100 static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
1101 static void free_image P_ ((struct frame *f, struct image *img));
1102 static int check_image_size P_ ((struct frame *f, int width, int height));
1104 #define MAX_IMAGE_SIZE 6.0
1105 Lisp_Object Vmax_image_size;
1107 /* Allocate and return a new image structure for image specification
1108 SPEC. SPEC has a hash value of HASH. */
1110 static struct image *
1111 make_image (spec, hash)
1112 Lisp_Object spec;
1113 unsigned hash;
1115 struct image *img = (struct image *) xmalloc (sizeof *img);
1117 xassert (valid_image_p (spec));
1118 bzero (img, sizeof *img);
1119 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
1120 xassert (img->type != NULL);
1121 img->spec = spec;
1122 img->data.lisp_val = Qnil;
1123 img->ascent = DEFAULT_IMAGE_ASCENT;
1124 img->hash = hash;
1125 return img;
1129 /* Free image IMG which was used on frame F, including its resources. */
1131 static void
1132 free_image (f, img)
1133 struct frame *f;
1134 struct image *img;
1136 if (img)
1138 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
1140 /* Remove IMG from the hash table of its cache. */
1141 if (img->prev)
1142 img->prev->next = img->next;
1143 else
1144 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
1146 if (img->next)
1147 img->next->prev = img->prev;
1149 c->images[img->id] = NULL;
1151 /* Free resources, then free IMG. */
1152 img->type->free (f, img);
1153 xfree (img);
1157 /* Return 1 if the given widths and heights are valid for display;
1158 otherwise, return 0. */
1161 check_image_size (f, width, height)
1162 struct frame *f;
1163 int width;
1164 int height;
1166 int w, h;
1168 if (width <= 0 || height <= 0)
1169 return 0;
1171 if (INTEGERP (Vmax_image_size))
1172 w = h = XINT (Vmax_image_size);
1173 else if (FLOATP (Vmax_image_size))
1175 if (f != NULL)
1177 w = FRAME_PIXEL_WIDTH (f);
1178 h = FRAME_PIXEL_HEIGHT (f);
1180 else
1181 w = h = 1024; /* Arbitrary size for unknown frame. */
1182 w = (int) (XFLOAT_DATA (Vmax_image_size) * w);
1183 h = (int) (XFLOAT_DATA (Vmax_image_size) * h);
1185 else
1186 return 1;
1188 return (width <= w && height <= h);
1191 /* Prepare image IMG for display on frame F. Must be called before
1192 drawing an image. */
1194 void
1195 prepare_image_for_display (f, img)
1196 struct frame *f;
1197 struct image *img;
1199 EMACS_TIME t;
1201 /* We're about to display IMG, so set its timestamp to `now'. */
1202 EMACS_GET_TIME (t);
1203 img->timestamp = EMACS_SECS (t);
1205 /* If IMG doesn't have a pixmap yet, load it now, using the image
1206 type dependent loader function. */
1207 if (img->pixmap == NO_PIXMAP && !img->load_failed_p)
1208 img->load_failed_p = img->type->load (f, img) == 0;
1212 /* Value is the number of pixels for the ascent of image IMG when
1213 drawn in face FACE. */
1216 image_ascent (img, face, slice)
1217 struct image *img;
1218 struct face *face;
1219 struct glyph_slice *slice;
1221 int height;
1222 int ascent;
1224 if (slice->height == img->height)
1225 height = img->height + img->vmargin;
1226 else if (slice->y == 0)
1227 height = slice->height + img->vmargin;
1228 else
1229 height = slice->height;
1231 if (img->ascent == CENTERED_IMAGE_ASCENT)
1233 if (face->font)
1235 #ifdef HAVE_NTGUI
1236 /* W32 specific version. Why?. ++kfs */
1237 ascent = height / 2 - (FONT_DESCENT(face->font)
1238 - FONT_BASE(face->font)) / 2;
1239 #else
1240 /* This expression is arranged so that if the image can't be
1241 exactly centered, it will be moved slightly up. This is
1242 because a typical font is `top-heavy' (due to the presence
1243 uppercase letters), so the image placement should err towards
1244 being top-heavy too. It also just generally looks better. */
1245 ascent = (height + face->font->ascent - face->font->descent + 1) / 2;
1246 #endif /* HAVE_NTGUI */
1248 else
1249 ascent = height / 2;
1251 else
1252 ascent = (int) (height * img->ascent / 100.0);
1254 return ascent;
1258 /* Image background colors. */
1260 /* Find the "best" corner color of a bitmap.
1261 On W32, XIMG is assumed to a device context with the bitmap selected. */
1263 static RGB_PIXEL_COLOR
1264 four_corners_best (ximg, width, height)
1265 XImagePtr_or_DC ximg;
1266 unsigned long width, height;
1268 RGB_PIXEL_COLOR corners[4], best;
1269 int i, best_count;
1271 /* Get the colors at the corners of ximg. */
1272 corners[0] = GET_PIXEL (ximg, 0, 0);
1273 corners[1] = GET_PIXEL (ximg, width - 1, 0);
1274 corners[2] = GET_PIXEL (ximg, width - 1, height - 1);
1275 corners[3] = GET_PIXEL (ximg, 0, height - 1);
1277 /* Choose the most frequently found color as background. */
1278 for (i = best_count = 0; i < 4; ++i)
1280 int j, n;
1282 for (j = n = 0; j < 4; ++j)
1283 if (corners[i] == corners[j])
1284 ++n;
1286 if (n > best_count)
1287 best = corners[i], best_count = n;
1290 return best;
1293 /* Portability macros */
1295 #ifdef HAVE_NTGUI
1297 #define Destroy_Image(img_dc, prev) \
1298 do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0)
1300 #define Free_Pixmap(display, pixmap) \
1301 DeleteObject (pixmap)
1303 #else
1305 #define Destroy_Image(ximg, dummy) \
1306 XDestroyImage (ximg)
1308 #define Free_Pixmap(display, pixmap) \
1309 XFreePixmap (display, pixmap)
1311 #endif /* HAVE_NTGUI */
1314 /* Return the `background' field of IMG. If IMG doesn't have one yet,
1315 it is guessed heuristically. If non-zero, XIMG is an existing
1316 XImage object (or device context with the image selected on W32) to
1317 use for the heuristic. */
1319 RGB_PIXEL_COLOR
1320 image_background (img, f, ximg)
1321 struct image *img;
1322 struct frame *f;
1323 XImagePtr_or_DC ximg;
1325 if (! img->background_valid)
1326 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1328 int free_ximg = !ximg;
1329 #ifdef HAVE_NTGUI
1330 HGDIOBJ prev;
1331 #endif /* HAVE_NTGUI */
1333 if (free_ximg)
1335 #ifndef HAVE_NTGUI
1336 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
1337 0, 0, img->width, img->height, ~0, ZPixmap);
1338 #else
1339 HDC frame_dc = get_frame_dc (f);
1340 ximg = CreateCompatibleDC (frame_dc);
1341 release_frame_dc (f, frame_dc);
1342 prev = SelectObject (ximg, img->pixmap);
1343 #endif /* !HAVE_NTGUI */
1346 img->background = four_corners_best (ximg, img->width, img->height);
1348 if (free_ximg)
1349 Destroy_Image (ximg, prev);
1351 img->background_valid = 1;
1354 return img->background;
1357 /* Return the `background_transparent' field of IMG. If IMG doesn't
1358 have one yet, it is guessed heuristically. If non-zero, MASK is an
1359 existing XImage object to use for the heuristic. */
1362 image_background_transparent (img, f, mask)
1363 struct image *img;
1364 struct frame *f;
1365 XImagePtr_or_DC mask;
1367 if (! img->background_transparent_valid)
1368 /* IMG doesn't have a background yet, try to guess a reasonable value. */
1370 if (img->mask)
1372 int free_mask = !mask;
1373 #ifdef HAVE_NTGUI
1374 HGDIOBJ prev;
1375 #endif /* HAVE_NTGUI */
1377 if (free_mask)
1379 #ifndef HAVE_NTGUI
1380 mask = XGetImage (FRAME_X_DISPLAY (f), img->mask,
1381 0, 0, img->width, img->height, ~0, ZPixmap);
1382 #else
1383 HDC frame_dc = get_frame_dc (f);
1384 mask = CreateCompatibleDC (frame_dc);
1385 release_frame_dc (f, frame_dc);
1386 prev = SelectObject (mask, img->mask);
1387 #endif /* HAVE_NTGUI */
1390 img->background_transparent
1391 = (four_corners_best (mask, img->width, img->height) == PIX_MASK_RETAIN);
1393 if (free_mask)
1394 Destroy_Image (mask, prev);
1396 else
1397 img->background_transparent = 0;
1399 img->background_transparent_valid = 1;
1402 return img->background_transparent;
1406 /***********************************************************************
1407 Helper functions for X image types
1408 ***********************************************************************/
1410 static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
1411 int, int));
1412 static void x_clear_image P_ ((struct frame *f, struct image *img));
1413 static unsigned long x_alloc_image_color P_ ((struct frame *f,
1414 struct image *img,
1415 Lisp_Object color_name,
1416 unsigned long dflt));
1419 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
1420 free the pixmap if any. MASK_P non-zero means clear the mask
1421 pixmap if any. COLORS_P non-zero means free colors allocated for
1422 the image, if any. */
1424 static void
1425 x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
1426 struct frame *f;
1427 struct image *img;
1428 int pixmap_p, mask_p, colors_p;
1430 if (pixmap_p && img->pixmap)
1432 Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap);
1433 img->pixmap = NO_PIXMAP;
1434 img->background_valid = 0;
1437 if (mask_p && img->mask)
1439 Free_Pixmap (FRAME_X_DISPLAY (f), img->mask);
1440 img->mask = NO_PIXMAP;
1441 img->background_transparent_valid = 0;
1444 if (colors_p && img->ncolors)
1446 /* MAC_TODO: color table support. */
1447 /* W32_TODO: color table support. */
1448 #ifdef HAVE_X_WINDOWS
1449 x_free_colors (f, img->colors, img->ncolors);
1450 #endif /* HAVE_X_WINDOWS */
1451 xfree (img->colors);
1452 img->colors = NULL;
1453 img->ncolors = 0;
1457 /* Free X resources of image IMG which is used on frame F. */
1459 static void
1460 x_clear_image (f, img)
1461 struct frame *f;
1462 struct image *img;
1464 BLOCK_INPUT;
1465 x_clear_image_1 (f, img, 1, 1, 1);
1466 UNBLOCK_INPUT;
1470 /* Allocate color COLOR_NAME for image IMG on frame F. If color
1471 cannot be allocated, use DFLT. Add a newly allocated color to
1472 IMG->colors, so that it can be freed again. Value is the pixel
1473 color. */
1475 static unsigned long
1476 x_alloc_image_color (f, img, color_name, dflt)
1477 struct frame *f;
1478 struct image *img;
1479 Lisp_Object color_name;
1480 unsigned long dflt;
1482 XColor color;
1483 unsigned long result;
1485 xassert (STRINGP (color_name));
1487 if (x_defined_color (f, SDATA (color_name), &color, 1))
1489 /* This isn't called frequently so we get away with simply
1490 reallocating the color vector to the needed size, here. */
1491 ++img->ncolors;
1492 img->colors =
1493 (unsigned long *) xrealloc (img->colors,
1494 img->ncolors * sizeof *img->colors);
1495 img->colors[img->ncolors - 1] = color.pixel;
1496 result = color.pixel;
1498 else
1499 result = dflt;
1501 return result;
1506 /***********************************************************************
1507 Image Cache
1508 ***********************************************************************/
1510 static void cache_image P_ ((struct frame *f, struct image *img));
1511 static void postprocess_image P_ ((struct frame *, struct image *));
1513 /* Return a new, initialized image cache that is allocated from the
1514 heap. Call free_image_cache to free an image cache. */
1516 struct image_cache *
1517 make_image_cache ()
1519 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
1520 int size;
1522 bzero (c, sizeof *c);
1523 c->size = 50;
1524 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
1525 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
1526 c->buckets = (struct image **) xmalloc (size);
1527 bzero (c->buckets, size);
1528 return c;
1532 /* Free image cache of frame F. Be aware that X frames share images
1533 caches. */
1535 void
1536 free_image_cache (f)
1537 struct frame *f;
1539 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
1540 if (c)
1542 int i;
1544 /* Cache should not be referenced by any frame when freed. */
1545 xassert (c->refcount == 0);
1547 for (i = 0; i < c->used; ++i)
1548 free_image (f, c->images[i]);
1549 xfree (c->images);
1550 xfree (c->buckets);
1551 xfree (c);
1552 FRAME_X_IMAGE_CACHE (f) = NULL;
1557 /* Clear image cache of frame F. FORCE_P non-zero means free all
1558 images. FORCE_P zero means clear only images that haven't been
1559 displayed for some time. Should be called from time to time to
1560 reduce the number of loaded images. If image-eviction-seconds is
1561 non-nil, this frees images in the cache which weren't displayed for
1562 at least that many seconds. */
1564 void
1565 clear_image_cache (f, force_p)
1566 struct frame *f;
1567 int force_p;
1569 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
1571 if (c && INTEGERP (Vimage_cache_eviction_delay))
1573 EMACS_TIME t;
1574 unsigned long old;
1575 int i, nfreed;
1577 EMACS_GET_TIME (t);
1578 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
1580 /* Block input so that we won't be interrupted by a SIGIO
1581 while being in an inconsistent state. */
1582 BLOCK_INPUT;
1584 for (i = nfreed = 0; i < c->used; ++i)
1586 struct image *img = c->images[i];
1587 if (img != NULL
1588 && (force_p || img->timestamp < old))
1590 free_image (f, img);
1591 ++nfreed;
1595 /* We may be clearing the image cache because, for example,
1596 Emacs was iconified for a longer period of time. In that
1597 case, current matrices may still contain references to
1598 images freed above. So, clear these matrices. */
1599 if (nfreed)
1601 Lisp_Object tail, frame;
1603 FOR_EACH_FRAME (tail, frame)
1605 struct frame *f = XFRAME (frame);
1606 if (FRAME_WINDOW_P (f)
1607 && FRAME_X_IMAGE_CACHE (f) == c)
1608 clear_current_matrices (f);
1611 ++windows_or_buffers_changed;
1614 UNBLOCK_INPUT;
1619 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
1620 0, 1, 0,
1621 doc: /* Clear the image cache of FRAME.
1622 FRAME nil or omitted means use the selected frame.
1623 FRAME t means clear the image caches of all frames. */)
1624 (frame)
1625 Lisp_Object frame;
1627 if (EQ (frame, Qt))
1629 Lisp_Object tail;
1631 FOR_EACH_FRAME (tail, frame)
1632 if (FRAME_WINDOW_P (XFRAME (frame)))
1633 clear_image_cache (XFRAME (frame), 1);
1635 else
1636 clear_image_cache (check_x_frame (frame), 1);
1638 return Qnil;
1642 /* Compute masks and transform image IMG on frame F, as specified
1643 by the image's specification, */
1645 static void
1646 postprocess_image (f, img)
1647 struct frame *f;
1648 struct image *img;
1650 /* Manipulation of the image's mask. */
1651 if (img->pixmap)
1653 Lisp_Object conversion, spec;
1654 Lisp_Object mask;
1656 spec = img->spec;
1658 /* `:heuristic-mask t'
1659 `:mask heuristic'
1660 means build a mask heuristically.
1661 `:heuristic-mask (R G B)'
1662 `:mask (heuristic (R G B))'
1663 means build a mask from color (R G B) in the
1664 image.
1665 `:mask nil'
1666 means remove a mask, if any. */
1668 mask = image_spec_value (spec, QCheuristic_mask, NULL);
1669 if (!NILP (mask))
1670 x_build_heuristic_mask (f, img, mask);
1671 else
1673 int found_p;
1675 mask = image_spec_value (spec, QCmask, &found_p);
1677 if (EQ (mask, Qheuristic))
1678 x_build_heuristic_mask (f, img, Qt);
1679 else if (CONSP (mask)
1680 && EQ (XCAR (mask), Qheuristic))
1682 if (CONSP (XCDR (mask)))
1683 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
1684 else
1685 x_build_heuristic_mask (f, img, XCDR (mask));
1687 else if (NILP (mask) && found_p && img->mask)
1689 Free_Pixmap (FRAME_X_DISPLAY (f), img->mask);
1690 img->mask = NO_PIXMAP;
1695 /* Should we apply an image transformation algorithm? */
1696 conversion = image_spec_value (spec, QCconversion, NULL);
1697 if (EQ (conversion, Qdisabled))
1698 x_disable_image (f, img);
1699 else if (EQ (conversion, Qlaplace))
1700 x_laplace (f, img);
1701 else if (EQ (conversion, Qemboss))
1702 x_emboss (f, img);
1703 else if (CONSP (conversion)
1704 && EQ (XCAR (conversion), Qedge_detection))
1706 Lisp_Object tem;
1707 tem = XCDR (conversion);
1708 if (CONSP (tem))
1709 x_edge_detection (f, img,
1710 Fplist_get (tem, QCmatrix),
1711 Fplist_get (tem, QCcolor_adjustment));
1717 /* Return the id of image with Lisp specification SPEC on frame F.
1718 SPEC must be a valid Lisp image specification (see valid_image_p). */
1721 lookup_image (f, spec)
1722 struct frame *f;
1723 Lisp_Object spec;
1725 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
1726 struct image *img;
1727 int i;
1728 unsigned hash;
1729 struct gcpro gcpro1;
1730 EMACS_TIME now;
1732 /* F must be a window-system frame, and SPEC must be a valid image
1733 specification. */
1734 xassert (FRAME_WINDOW_P (f));
1735 xassert (valid_image_p (spec));
1737 GCPRO1 (spec);
1739 /* Look up SPEC in the hash table of the image cache. */
1740 hash = sxhash (spec, 0);
1741 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
1743 for (img = c->buckets[i]; img; img = img->next)
1744 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
1745 break;
1747 if (img && img->load_failed_p)
1749 free_image (f, img);
1750 img = NULL;
1753 /* If not found, create a new image and cache it. */
1754 if (img == NULL)
1756 extern Lisp_Object Qpostscript;
1758 BLOCK_INPUT;
1759 img = make_image (spec, hash);
1760 cache_image (f, img);
1761 img->load_failed_p = img->type->load (f, img) == 0;
1763 /* If we can't load the image, and we don't have a width and
1764 height, use some arbitrary width and height so that we can
1765 draw a rectangle for it. */
1766 if (img->load_failed_p)
1768 Lisp_Object value;
1770 value = image_spec_value (spec, QCwidth, NULL);
1771 img->width = (INTEGERP (value)
1772 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
1773 value = image_spec_value (spec, QCheight, NULL);
1774 img->height = (INTEGERP (value)
1775 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
1777 else
1779 /* Handle image type independent image attributes
1780 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
1781 `:background COLOR'. */
1782 Lisp_Object ascent, margin, relief, bg;
1784 ascent = image_spec_value (spec, QCascent, NULL);
1785 if (INTEGERP (ascent))
1786 img->ascent = XFASTINT (ascent);
1787 else if (EQ (ascent, Qcenter))
1788 img->ascent = CENTERED_IMAGE_ASCENT;
1790 margin = image_spec_value (spec, QCmargin, NULL);
1791 if (INTEGERP (margin) && XINT (margin) >= 0)
1792 img->vmargin = img->hmargin = XFASTINT (margin);
1793 else if (CONSP (margin) && INTEGERP (XCAR (margin))
1794 && INTEGERP (XCDR (margin)))
1796 if (XINT (XCAR (margin)) > 0)
1797 img->hmargin = XFASTINT (XCAR (margin));
1798 if (XINT (XCDR (margin)) > 0)
1799 img->vmargin = XFASTINT (XCDR (margin));
1802 relief = image_spec_value (spec, QCrelief, NULL);
1803 if (INTEGERP (relief))
1805 img->relief = XINT (relief);
1806 img->hmargin += abs (img->relief);
1807 img->vmargin += abs (img->relief);
1810 if (! img->background_valid)
1812 bg = image_spec_value (img->spec, QCbackground, NULL);
1813 if (!NILP (bg))
1815 img->background
1816 = x_alloc_image_color (f, img, bg,
1817 FRAME_BACKGROUND_PIXEL (f));
1818 img->background_valid = 1;
1822 /* Do image transformations and compute masks, unless we
1823 don't have the image yet. */
1824 if (!EQ (*img->type->type, Qpostscript))
1825 postprocess_image (f, img);
1828 UNBLOCK_INPUT;
1831 /* We're using IMG, so set its timestamp to `now'. */
1832 EMACS_GET_TIME (now);
1833 img->timestamp = EMACS_SECS (now);
1835 UNGCPRO;
1837 /* Value is the image id. */
1838 return img->id;
1842 /* Cache image IMG in the image cache of frame F. */
1844 static void
1845 cache_image (f, img)
1846 struct frame *f;
1847 struct image *img;
1849 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
1850 int i;
1852 /* Find a free slot in c->images. */
1853 for (i = 0; i < c->used; ++i)
1854 if (c->images[i] == NULL)
1855 break;
1857 /* If no free slot found, maybe enlarge c->images. */
1858 if (i == c->used && c->used == c->size)
1860 c->size *= 2;
1861 c->images = (struct image **) xrealloc (c->images,
1862 c->size * sizeof *c->images);
1865 /* Add IMG to c->images, and assign IMG an id. */
1866 c->images[i] = img;
1867 img->id = i;
1868 if (i == c->used)
1869 ++c->used;
1871 /* Add IMG to the cache's hash table. */
1872 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
1873 img->next = c->buckets[i];
1874 if (img->next)
1875 img->next->prev = img;
1876 img->prev = NULL;
1877 c->buckets[i] = img;
1881 /* Call FN on every image in the image cache of frame F. Used to mark
1882 Lisp Objects in the image cache. */
1884 void
1885 forall_images_in_image_cache (f, fn)
1886 struct frame *f;
1887 void (*fn) P_ ((struct image *img));
1889 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f))
1891 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
1892 if (c)
1894 int i;
1895 for (i = 0; i < c->used; ++i)
1896 if (c->images[i])
1897 fn (c->images[i]);
1904 /***********************************************************************
1905 X / MAC / W32 support code
1906 ***********************************************************************/
1908 #ifdef HAVE_NTGUI
1910 /* Macro for defining functions that will be loaded from image DLLs. */
1911 #define DEF_IMGLIB_FN(func) int (FAR CDECL *fn_##func)()
1913 /* Macro for loading those image functions from the library. */
1914 #define LOAD_IMGLIB_FN(lib,func) { \
1915 fn_##func = (void *) GetProcAddress (lib, #func); \
1916 if (!fn_##func) return 0; \
1919 /* Load a DLL implementing an image type.
1920 The `image-library-alist' variable associates a symbol,
1921 identifying an image type, to a list of possible filenames.
1922 The function returns NULL if no library could be loaded for
1923 the given image type, or if the library was previously loaded;
1924 else the handle of the DLL. */
1925 static HMODULE
1926 w32_delayed_load (Lisp_Object libraries, Lisp_Object type)
1928 HMODULE library = NULL;
1930 if (CONSP (libraries) && NILP (Fassq (type, Vimage_type_cache)))
1932 Lisp_Object dlls = Fassq (type, libraries);
1934 if (CONSP (dlls))
1935 for (dlls = XCDR (dlls); CONSP (dlls); dlls = XCDR (dlls))
1937 CHECK_STRING_CAR (dlls);
1938 if (library = LoadLibrary (SDATA (XCAR (dlls))))
1939 break;
1943 return library;
1946 #endif /* HAVE_NTGUI */
1948 static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
1949 XImagePtr *, Pixmap *));
1950 static void x_destroy_x_image P_ ((XImagePtr));
1951 static void x_put_x_image P_ ((struct frame *, XImagePtr, Pixmap, int, int));
1954 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
1955 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
1956 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
1957 via xmalloc. Print error messages via image_error if an error
1958 occurs. Value is non-zero if successful.
1960 On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH
1961 should indicate the bit depth of the image. */
1963 static int
1964 x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
1965 struct frame *f;
1966 int width, height, depth;
1967 XImagePtr *ximg;
1968 Pixmap *pixmap;
1970 #ifdef HAVE_X_WINDOWS
1971 Display *display = FRAME_X_DISPLAY (f);
1972 Window window = FRAME_X_WINDOW (f);
1973 Screen *screen = FRAME_X_SCREEN (f);
1975 xassert (interrupt_input_blocked);
1977 if (depth <= 0)
1978 depth = DefaultDepthOfScreen (screen);
1979 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
1980 depth, ZPixmap, 0, NULL, width, height,
1981 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
1982 if (*ximg == NULL)
1984 image_error ("Unable to allocate X image", Qnil, Qnil);
1985 return 0;
1988 /* Allocate image raster. */
1989 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
1991 /* Allocate a pixmap of the same size. */
1992 *pixmap = XCreatePixmap (display, window, width, height, depth);
1993 if (*pixmap == NO_PIXMAP)
1995 x_destroy_x_image (*ximg);
1996 *ximg = NULL;
1997 image_error ("Unable to create X pixmap", Qnil, Qnil);
1998 return 0;
2001 return 1;
2002 #endif /* HAVE_X_WINDOWS */
2004 #ifdef HAVE_NTGUI
2006 BITMAPINFOHEADER *header;
2007 HDC hdc;
2008 int scanline_width_bits;
2009 int remainder;
2010 int palette_colors = 0;
2012 if (depth == 0)
2013 depth = 24;
2015 if (depth != 1 && depth != 4 && depth != 8
2016 && depth != 16 && depth != 24 && depth != 32)
2018 image_error ("Invalid image bit depth specified", Qnil, Qnil);
2019 return 0;
2022 scanline_width_bits = width * depth;
2023 remainder = scanline_width_bits % 32;
2025 if (remainder)
2026 scanline_width_bits += 32 - remainder;
2028 /* Bitmaps with a depth less than 16 need a palette. */
2029 /* BITMAPINFO structure already contains the first RGBQUAD. */
2030 if (depth < 16)
2031 palette_colors = 1 << depth - 1;
2033 *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
2034 if (*ximg == NULL)
2036 image_error ("Unable to allocate memory for XImage", Qnil, Qnil);
2037 return 0;
2040 header = &((*ximg)->info.bmiHeader);
2041 bzero (&((*ximg)->info), sizeof (BITMAPINFO));
2042 header->biSize = sizeof (*header);
2043 header->biWidth = width;
2044 header->biHeight = -height; /* negative indicates a top-down bitmap. */
2045 header->biPlanes = 1;
2046 header->biBitCount = depth;
2047 header->biCompression = BI_RGB;
2048 header->biClrUsed = palette_colors;
2050 /* TODO: fill in palette. */
2051 if (depth == 1)
2053 (*ximg)->info.bmiColors[0].rgbBlue = 0;
2054 (*ximg)->info.bmiColors[0].rgbGreen = 0;
2055 (*ximg)->info.bmiColors[0].rgbRed = 0;
2056 (*ximg)->info.bmiColors[0].rgbReserved = 0;
2057 (*ximg)->info.bmiColors[1].rgbBlue = 255;
2058 (*ximg)->info.bmiColors[1].rgbGreen = 255;
2059 (*ximg)->info.bmiColors[1].rgbRed = 255;
2060 (*ximg)->info.bmiColors[1].rgbReserved = 0;
2063 hdc = get_frame_dc (f);
2065 /* Create a DIBSection and raster array for the bitmap,
2066 and store its handle in *pixmap. */
2067 *pixmap = CreateDIBSection (hdc, &((*ximg)->info),
2068 (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS,
2069 /* casting avoids a GCC warning */
2070 (void **)&((*ximg)->data), NULL, 0);
2072 /* Realize display palette and garbage all frames. */
2073 release_frame_dc (f, hdc);
2075 if (*pixmap == NULL)
2077 DWORD err = GetLastError();
2078 Lisp_Object errcode;
2079 /* All system errors are < 10000, so the following is safe. */
2080 XSETINT (errcode, (int) err);
2081 image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
2082 x_destroy_x_image (*ximg);
2083 return 0;
2086 return 1;
2088 #endif /* HAVE_NTGUI */
2090 #ifdef MAC_OS
2091 Display *display = FRAME_X_DISPLAY (f);
2092 Window window = FRAME_X_WINDOW (f);
2094 xassert (interrupt_input_blocked);
2096 /* Allocate a pixmap of the same size. */
2097 *pixmap = XCreatePixmap (display, window, width, height, depth);
2098 if (*pixmap == NO_PIXMAP)
2100 *ximg = NULL;
2101 image_error ("Unable to create X pixmap", Qnil, Qnil);
2102 return 0;
2105 LockPixels (GetGWorldPixMap (*pixmap));
2106 *ximg = *pixmap;
2107 return 1;
2109 #endif /* MAC_OS */
2113 /* Destroy XImage XIMG. Free XIMG->data. */
2115 static void
2116 x_destroy_x_image (ximg)
2117 XImagePtr ximg;
2119 xassert (interrupt_input_blocked);
2120 if (ximg)
2122 #ifdef HAVE_X_WINDOWS
2123 xfree (ximg->data);
2124 ximg->data = NULL;
2125 XDestroyImage (ximg);
2126 #endif /* HAVE_X_WINDOWS */
2127 #ifdef HAVE_NTGUI
2128 /* Data will be freed by DestroyObject. */
2129 ximg->data = NULL;
2130 xfree (ximg);
2131 #endif /* HAVE_NTGUI */
2132 #ifdef MAC_OS
2133 XDestroyImage (ximg);
2134 #endif /* MAC_OS */
2139 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
2140 are width and height of both the image and pixmap. */
2142 static void
2143 x_put_x_image (f, ximg, pixmap, width, height)
2144 struct frame *f;
2145 XImagePtr ximg;
2146 Pixmap pixmap;
2147 int width, height;
2149 #ifdef HAVE_X_WINDOWS
2150 GC gc;
2152 xassert (interrupt_input_blocked);
2153 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
2154 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
2155 XFreeGC (FRAME_X_DISPLAY (f), gc);
2156 #endif /* HAVE_X_WINDOWS */
2158 #ifdef HAVE_NTGUI
2159 #if 0 /* I don't think this is necessary looking at where it is used. */
2160 HDC hdc = get_frame_dc (f);
2161 SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS);
2162 release_frame_dc (f, hdc);
2163 #endif
2164 #endif /* HAVE_NTGUI */
2166 #ifdef MAC_OS
2167 xassert (ximg == pixmap);
2168 #endif /* MAC_OS */
2172 /***********************************************************************
2173 File Handling
2174 ***********************************************************************/
2176 static unsigned char *slurp_file P_ ((char *, int *));
2179 /* Find image file FILE. Look in data-directory, then
2180 x-bitmap-file-path. Value is the full name of the file found, or
2181 nil if not found. */
2183 Lisp_Object
2184 x_find_image_file (file)
2185 Lisp_Object file;
2187 Lisp_Object file_found, search_path;
2188 struct gcpro gcpro1, gcpro2;
2189 int fd;
2191 file_found = Qnil;
2192 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
2193 GCPRO2 (file_found, search_path);
2195 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
2196 fd = openp (search_path, file, Qnil, &file_found, Qnil);
2198 if (fd == -1)
2199 file_found = Qnil;
2200 else
2201 close (fd);
2203 UNGCPRO;
2204 return file_found;
2208 /* Read FILE into memory. Value is a pointer to a buffer allocated
2209 with xmalloc holding FILE's contents. Value is null if an error
2210 occurred. *SIZE is set to the size of the file. */
2212 static unsigned char *
2213 slurp_file (file, size)
2214 char *file;
2215 int *size;
2217 FILE *fp = NULL;
2218 unsigned char *buf = NULL;
2219 struct stat st;
2221 if (stat (file, &st) == 0
2222 && (fp = fopen (file, "rb")) != NULL
2223 && (buf = (unsigned char *) xmalloc (st.st_size),
2224 fread (buf, 1, st.st_size, fp) == st.st_size))
2226 *size = st.st_size;
2227 fclose (fp);
2229 else
2231 if (fp)
2232 fclose (fp);
2233 if (buf)
2235 xfree (buf);
2236 buf = NULL;
2240 return buf;
2245 #ifdef MAC_OS
2247 /***********************************************************************
2248 MAC Image Load Functions
2249 ***********************************************************************/
2251 static int image_load_quicktime P_ ((struct frame *, struct image *img,
2252 OSType));
2253 #ifdef MAC_OSX
2254 static int image_load_quartz2d P_ ((struct frame *, struct image *img, int));
2255 #endif
2257 static OSErr
2258 find_image_fsspec (specified_file, file, fss)
2259 Lisp_Object specified_file, *file;
2260 FSSpec *fss;
2262 OSErr err;
2263 AEDesc desc;
2265 *file = x_find_image_file (specified_file);
2266 if (!STRINGP (*file))
2267 return fnfErr; /* file or directory not found;
2268 incomplete pathname */
2269 /* Try to open the image file. */
2270 err = AECoercePtr (TYPE_FILE_NAME, SDATA (*file),
2271 SBYTES (*file), typeFSS, &desc);
2272 if (err == noErr)
2274 #if TARGET_API_MAC_CARBON
2275 err = AEGetDescData (&desc, fss, sizeof (FSSpec));
2276 #else
2277 *fss = *(FSSpec *)(*(desc.dataHandle));
2278 #endif
2279 AEDisposeDesc (&desc);
2281 return err;
2284 static int
2285 image_load_qt_1 (f, img, type, fss, dh)
2286 struct frame *f;
2287 struct image *img;
2288 OSType type;
2289 FSSpec *fss;
2290 Handle dh;
2292 OSErr err;
2293 GraphicsImportComponent gi;
2294 Rect rect;
2295 int width, height;
2296 ImageDescriptionHandle desc_handle;
2297 short draw_all_pixels;
2298 Lisp_Object specified_bg;
2299 XColor color;
2300 XImagePtr ximg;
2301 RGBColor bg_color;
2303 err = OpenADefaultComponent (GraphicsImporterComponentType,
2304 type, &gi);
2305 if (err != noErr)
2307 image_error ("Cannot get importer component for `%s'", img->spec, Qnil);
2308 return 0;
2310 if (dh == NULL)
2312 /* read from file system spec */
2313 err = GraphicsImportSetDataFile (gi, fss);
2314 if (err != noErr)
2316 image_error ("Cannot set fsspec to graphics importer for '%s'",
2317 img->spec, Qnil);
2318 goto error;
2321 else
2323 /* read from data handle */
2324 err = GraphicsImportSetDataHandle (gi, dh);
2325 if (err != noErr)
2327 image_error ("Cannot set data handle to graphics importer for `%s'",
2328 img->spec, Qnil);
2329 goto error;
2332 err = GraphicsImportGetImageDescription (gi, &desc_handle);
2333 if (err != noErr || desc_handle == NULL)
2335 image_error ("Error reading `%s'", img->spec, Qnil);
2336 goto error;
2338 width = img->width = (*desc_handle)->width;
2339 height = img->height = (*desc_handle)->height;
2340 DisposeHandle ((Handle)desc_handle);
2342 if (!check_image_size (f, width, height))
2344 image_error ("Invalid image size", Qnil, Qnil);
2345 goto error;
2348 err = GraphicsImportDoesDrawAllPixels (gi, &draw_all_pixels);
2349 #if 0
2350 /* Don't check the error code here. It may have an undocumented
2351 value -32766. */
2352 if (err != noErr)
2354 image_error ("Error reading `%s'", img->spec, Qnil);
2355 goto error;
2357 #endif
2358 if (draw_all_pixels != graphicsImporterDrawsAllPixels)
2360 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
2361 if (!STRINGP (specified_bg) ||
2362 !mac_defined_color (f, SDATA (specified_bg), &color, 0))
2364 color.pixel = FRAME_BACKGROUND_PIXEL (f);
2365 color.red = RED16_FROM_ULONG (color.pixel);
2366 color.green = GREEN16_FROM_ULONG (color.pixel);
2367 color.blue = BLUE16_FROM_ULONG (color.pixel);
2371 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
2372 goto error;
2373 if (draw_all_pixels != graphicsImporterDrawsAllPixels)
2375 CGrafPtr old_port;
2376 GDHandle old_gdh;
2378 GetGWorld (&old_port, &old_gdh);
2379 SetGWorld (ximg, NULL);
2380 bg_color.red = color.red;
2381 bg_color.green = color.green;
2382 bg_color.blue = color.blue;
2383 RGBBackColor (&bg_color);
2384 #if TARGET_API_MAC_CARBON
2385 GetPortBounds (ximg, &rect);
2386 EraseRect (&rect);
2387 #else
2388 EraseRect (&(ximg->portRect));
2389 #endif
2390 SetGWorld (old_port, old_gdh);
2392 GraphicsImportSetGWorld (gi, ximg, NULL);
2393 GraphicsImportDraw (gi);
2394 CloseComponent (gi);
2396 /* Maybe fill in the background field while we have ximg handy. */
2397 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
2398 IMAGE_BACKGROUND (img, f, ximg);
2400 /* Put the image into the pixmap. */
2401 x_put_x_image (f, ximg, img->pixmap, width, height);
2402 x_destroy_x_image (ximg);
2403 return 1;
2405 error:
2406 CloseComponent (gi);
2407 return 0;
2411 /* Load an image using the QuickTime Graphics Importer.
2412 Note: The alpha channel does not work for PNG images. */
2413 static int
2414 image_load_quicktime (f, img, type)
2415 struct frame *f;
2416 struct image *img;
2417 OSType type;
2419 Lisp_Object specified_file;
2420 Lisp_Object specified_data;
2421 OSErr err;
2423 specified_file = image_spec_value (img->spec, QCfile, NULL);
2424 specified_data = image_spec_value (img->spec, QCdata, NULL);
2426 if (NILP (specified_data))
2428 /* Read from a file */
2429 Lisp_Object file;
2430 FSSpec fss;
2432 err = find_image_fsspec (specified_file, &file, &fss);
2433 if (err != noErr)
2435 if (err == fnfErr)
2436 image_error ("Cannot find image file `%s'", specified_file, Qnil);
2437 else
2438 image_error ("Cannot open `%s'", file, Qnil);
2439 return 0;
2441 return image_load_qt_1 (f, img, type, &fss, NULL);
2443 else
2445 /* Memory source! */
2446 int success_p;
2447 Handle dh;
2449 err = PtrToHand (SDATA (specified_data), &dh, SBYTES (specified_data));
2450 if (err != noErr)
2452 image_error ("Cannot allocate data handle for `%s'",
2453 img->spec, Qnil);
2454 return 0;
2456 success_p = image_load_qt_1 (f, img, type, NULL, dh);
2457 DisposeHandle (dh);
2458 return success_p;
2463 #ifdef MAC_OSX
2464 /* Load a PNG/JPEG image using Quartz 2D decoding routines.
2465 CGImageCreateWithPNGDataProvider is provided after Mac OS X 10.2.
2466 So don't use this function directly but determine at runtime
2467 whether it exists. */
2468 typedef CGImageRef (*CGImageCreateWithPNGDataProviderProcType)
2469 (CGDataProviderRef, const float [], bool, CGColorRenderingIntent);
2470 static CGImageCreateWithPNGDataProviderProcType MyCGImageCreateWithPNGDataProvider;
2473 static void
2474 init_image_func_pointer ()
2476 if (NSIsSymbolNameDefined ("_CGImageCreateWithPNGDataProvider"))
2478 MyCGImageCreateWithPNGDataProvider
2479 = (CGImageCreateWithPNGDataProviderProcType)
2480 NSAddressOfSymbol (NSLookupAndBindSymbol
2481 ("_CGImageCreateWithPNGDataProvider"));
2483 else
2484 MyCGImageCreateWithPNGDataProvider = NULL;
2488 static int
2489 image_load_quartz2d (f, img, png_p)
2490 struct frame *f;
2491 struct image *img;
2492 int png_p;
2494 Lisp_Object file, specified_file;
2495 Lisp_Object specified_data, specified_bg;
2496 struct gcpro gcpro1;
2497 CGDataProviderRef source;
2498 CGImageRef image;
2499 int width, height;
2500 XColor color;
2501 XImagePtr ximg = NULL;
2502 CGContextRef context;
2503 CGRect rectangle;
2505 /* Open the file. */
2506 specified_file = image_spec_value (img->spec, QCfile, NULL);
2507 specified_data = image_spec_value (img->spec, QCdata, NULL);
2509 file = Qnil;
2510 GCPRO1 (file);
2512 if (NILP (specified_data))
2514 CFStringRef path;
2515 CFURLRef url;
2517 file = x_find_image_file (specified_file);
2518 if (!STRINGP (file))
2520 image_error ("Cannot find image file `%s'", specified_file, Qnil);
2521 UNGCPRO;
2522 return 0;
2524 path = cfstring_create_with_string (file);
2525 url = CFURLCreateWithFileSystemPath (NULL, path,
2526 kCFURLPOSIXPathStyle, 0);
2527 CFRelease (path);
2528 source = CGDataProviderCreateWithURL (url);
2529 CFRelease (url);
2531 else
2532 source = CGDataProviderCreateWithData (NULL, SDATA (specified_data),
2533 SBYTES (specified_data), NULL);
2535 if (png_p)
2536 image = (*MyCGImageCreateWithPNGDataProvider) (source, NULL, FALSE,
2537 kCGRenderingIntentDefault);
2538 else
2539 image = CGImageCreateWithJPEGDataProvider (source, NULL, FALSE,
2540 kCGRenderingIntentDefault);
2542 CGDataProviderRelease (source);
2543 if (image == NULL)
2545 UNGCPRO;
2546 image_error ("Error reading image `%s'", img->spec, Qnil);
2547 return 0;
2549 width = img->width = CGImageGetWidth (image);
2550 height = img->height = CGImageGetHeight (image);
2552 if (!check_image_size (f, width, height))
2554 CGImageRelease (image);
2555 UNGCPRO;
2556 image_error ("Invalid image size", Qnil, Qnil);
2557 return 0;
2560 if (png_p)
2562 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
2563 if (!STRINGP (specified_bg) ||
2564 !mac_defined_color (f, SDATA (specified_bg), &color, 0))
2566 color.pixel = FRAME_BACKGROUND_PIXEL (f);
2567 color.red = RED16_FROM_ULONG (color.pixel);
2568 color.green = GREEN16_FROM_ULONG (color.pixel);
2569 color.blue = BLUE16_FROM_ULONG (color.pixel);
2573 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
2575 CGImageRelease (image);
2576 UNGCPRO;
2577 return 0;
2579 rectangle = CGRectMake (0, 0, width, height);
2580 QDBeginCGContext (ximg, &context);
2581 if (png_p)
2583 CGContextSetRGBFillColor (context, color.red / 65535.0,
2584 color.green / 65535.0,
2585 color.blue / 65535.0, 1.0);
2586 CGContextFillRect (context, rectangle);
2588 CGContextDrawImage (context, rectangle, image);
2589 QDEndCGContext (ximg, &context);
2590 CGImageRelease (image);
2592 /* Maybe fill in the background field while we have ximg handy. */
2593 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
2594 IMAGE_BACKGROUND (img, f, ximg);
2596 /* Put the image into the pixmap. */
2597 x_put_x_image (f, ximg, img->pixmap, width, height);
2598 x_destroy_x_image (ximg);
2599 UNGCPRO;
2600 return 1;
2602 #endif
2604 #endif /* MAC_OS */
2607 /***********************************************************************
2608 XBM images
2609 ***********************************************************************/
2611 static int xbm_scan P_ ((unsigned char **, unsigned char *, char *, int *));
2612 static int xbm_load P_ ((struct frame *f, struct image *img));
2613 static int xbm_load_image P_ ((struct frame *f, struct image *img,
2614 unsigned char *, unsigned char *));
2615 static int xbm_image_p P_ ((Lisp_Object object));
2616 static int xbm_read_bitmap_data P_ ((struct frame *f,
2617 unsigned char *, unsigned char *,
2618 int *, int *, unsigned char **));
2619 static int xbm_file_p P_ ((Lisp_Object));
2622 /* Indices of image specification fields in xbm_format, below. */
2624 enum xbm_keyword_index
2626 XBM_TYPE,
2627 XBM_FILE,
2628 XBM_WIDTH,
2629 XBM_HEIGHT,
2630 XBM_DATA,
2631 XBM_FOREGROUND,
2632 XBM_BACKGROUND,
2633 XBM_ASCENT,
2634 XBM_MARGIN,
2635 XBM_RELIEF,
2636 XBM_ALGORITHM,
2637 XBM_HEURISTIC_MASK,
2638 XBM_MASK,
2639 XBM_LAST
2642 /* Vector of image_keyword structures describing the format
2643 of valid XBM image specifications. */
2645 static struct image_keyword xbm_format[XBM_LAST] =
2647 {":type", IMAGE_SYMBOL_VALUE, 1},
2648 {":file", IMAGE_STRING_VALUE, 0},
2649 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
2650 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
2651 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2652 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
2653 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
2654 {":ascent", IMAGE_ASCENT_VALUE, 0},
2655 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
2656 {":relief", IMAGE_INTEGER_VALUE, 0},
2657 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2658 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
2659 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
2662 /* Structure describing the image type XBM. */
2664 static struct image_type xbm_type =
2666 &Qxbm,
2667 xbm_image_p,
2668 xbm_load,
2669 x_clear_image,
2670 NULL
2673 /* Tokens returned from xbm_scan. */
2675 enum xbm_token
2677 XBM_TK_IDENT = 256,
2678 XBM_TK_NUMBER
2682 /* Return non-zero if OBJECT is a valid XBM-type image specification.
2683 A valid specification is a list starting with the symbol `image'
2684 The rest of the list is a property list which must contain an
2685 entry `:type xbm..
2687 If the specification specifies a file to load, it must contain
2688 an entry `:file FILENAME' where FILENAME is a string.
2690 If the specification is for a bitmap loaded from memory it must
2691 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
2692 WIDTH and HEIGHT are integers > 0. DATA may be:
2694 1. a string large enough to hold the bitmap data, i.e. it must
2695 have a size >= (WIDTH + 7) / 8 * HEIGHT
2697 2. a bool-vector of size >= WIDTH * HEIGHT
2699 3. a vector of strings or bool-vectors, one for each line of the
2700 bitmap.
2702 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
2703 may not be specified in this case because they are defined in the
2704 XBM file.
2706 Both the file and data forms may contain the additional entries
2707 `:background COLOR' and `:foreground COLOR'. If not present,
2708 foreground and background of the frame on which the image is
2709 displayed is used. */
2711 static int
2712 xbm_image_p (object)
2713 Lisp_Object object;
2715 struct image_keyword kw[XBM_LAST];
2717 bcopy (xbm_format, kw, sizeof kw);
2718 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
2719 return 0;
2721 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
2723 if (kw[XBM_FILE].count)
2725 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
2726 return 0;
2728 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
2730 /* In-memory XBM file. */
2731 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
2732 return 0;
2734 else
2736 Lisp_Object data;
2737 int width, height;
2739 /* Entries for `:width', `:height' and `:data' must be present. */
2740 if (!kw[XBM_WIDTH].count
2741 || !kw[XBM_HEIGHT].count
2742 || !kw[XBM_DATA].count)
2743 return 0;
2745 data = kw[XBM_DATA].value;
2746 width = XFASTINT (kw[XBM_WIDTH].value);
2747 height = XFASTINT (kw[XBM_HEIGHT].value);
2749 /* Check type of data, and width and height against contents of
2750 data. */
2751 if (VECTORP (data))
2753 int i;
2755 /* Number of elements of the vector must be >= height. */
2756 if (XVECTOR (data)->size < height)
2757 return 0;
2759 /* Each string or bool-vector in data must be large enough
2760 for one line of the image. */
2761 for (i = 0; i < height; ++i)
2763 Lisp_Object elt = XVECTOR (data)->contents[i];
2765 if (STRINGP (elt))
2767 if (SCHARS (elt)
2768 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
2769 return 0;
2771 else if (BOOL_VECTOR_P (elt))
2773 if (XBOOL_VECTOR (elt)->size < width)
2774 return 0;
2776 else
2777 return 0;
2780 else if (STRINGP (data))
2782 if (SCHARS (data)
2783 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
2784 return 0;
2786 else if (BOOL_VECTOR_P (data))
2788 if (XBOOL_VECTOR (data)->size < width * height)
2789 return 0;
2791 else
2792 return 0;
2795 return 1;
2799 /* Scan a bitmap file. FP is the stream to read from. Value is
2800 either an enumerator from enum xbm_token, or a character for a
2801 single-character token, or 0 at end of file. If scanning an
2802 identifier, store the lexeme of the identifier in SVAL. If
2803 scanning a number, store its value in *IVAL. */
2805 static int
2806 xbm_scan (s, end, sval, ival)
2807 unsigned char **s, *end;
2808 char *sval;
2809 int *ival;
2811 unsigned int c;
2813 loop:
2815 /* Skip white space. */
2816 while (*s < end && (c = *(*s)++, isspace (c)))
2819 if (*s >= end)
2820 c = 0;
2821 else if (isdigit (c))
2823 int value = 0, digit;
2825 if (c == '0' && *s < end)
2827 c = *(*s)++;
2828 if (c == 'x' || c == 'X')
2830 while (*s < end)
2832 c = *(*s)++;
2833 if (isdigit (c))
2834 digit = c - '0';
2835 else if (c >= 'a' && c <= 'f')
2836 digit = c - 'a' + 10;
2837 else if (c >= 'A' && c <= 'F')
2838 digit = c - 'A' + 10;
2839 else
2840 break;
2841 value = 16 * value + digit;
2844 else if (isdigit (c))
2846 value = c - '0';
2847 while (*s < end
2848 && (c = *(*s)++, isdigit (c)))
2849 value = 8 * value + c - '0';
2852 else
2854 value = c - '0';
2855 while (*s < end
2856 && (c = *(*s)++, isdigit (c)))
2857 value = 10 * value + c - '0';
2860 if (*s < end)
2861 *s = *s - 1;
2862 *ival = value;
2863 c = XBM_TK_NUMBER;
2865 else if (isalpha (c) || c == '_')
2867 *sval++ = c;
2868 while (*s < end
2869 && (c = *(*s)++, (isalnum (c) || c == '_')))
2870 *sval++ = c;
2871 *sval = 0;
2872 if (*s < end)
2873 *s = *s - 1;
2874 c = XBM_TK_IDENT;
2876 else if (c == '/' && **s == '*')
2878 /* C-style comment. */
2879 ++*s;
2880 while (**s && (**s != '*' || *(*s + 1) != '/'))
2881 ++*s;
2882 if (**s)
2884 *s += 2;
2885 goto loop;
2889 return c;
2892 #ifdef HAVE_NTGUI
2894 /* Create a Windows bitmap from X bitmap data. */
2895 static HBITMAP
2896 w32_create_pixmap_from_bitmap_data (int width, int height, char *data)
2898 static unsigned char swap_nibble[16]
2899 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
2900 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
2901 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
2902 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
2903 int i, j, w1, w2;
2904 unsigned char *bits, *p;
2905 HBITMAP bmp;
2907 w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */
2908 w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
2909 bits = (unsigned char *) alloca (height * w2);
2910 bzero (bits, height * w2);
2911 for (i = 0; i < height; i++)
2913 p = bits + i*w2;
2914 for (j = 0; j < w1; j++)
2916 /* Bitswap XBM bytes to match how Windows does things. */
2917 unsigned char c = *data++;
2918 *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4)
2919 | (swap_nibble[(c>>4) & 0xf]));
2922 bmp = CreateBitmap (width, height, 1, 1, (char *) bits);
2924 return bmp;
2927 static void convert_mono_to_color_image (f, img, foreground, background)
2928 struct frame *f;
2929 struct image *img;
2930 COLORREF foreground, background;
2932 HDC hdc, old_img_dc, new_img_dc;
2933 HGDIOBJ old_prev, new_prev;
2934 HBITMAP new_pixmap;
2936 hdc = get_frame_dc (f);
2937 old_img_dc = CreateCompatibleDC (hdc);
2938 new_img_dc = CreateCompatibleDC (hdc);
2939 new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height);
2940 release_frame_dc (f, hdc);
2941 old_prev = SelectObject (old_img_dc, img->pixmap);
2942 new_prev = SelectObject (new_img_dc, new_pixmap);
2943 SetTextColor (new_img_dc, foreground);
2944 SetBkColor (new_img_dc, background);
2946 BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc,
2947 0, 0, SRCCOPY);
2949 SelectObject (old_img_dc, old_prev);
2950 SelectObject (new_img_dc, new_prev);
2951 DeleteDC (old_img_dc);
2952 DeleteDC (new_img_dc);
2953 DeleteObject (img->pixmap);
2954 if (new_pixmap == 0)
2955 fprintf (stderr, "Failed to convert image to color.\n");
2956 else
2957 img->pixmap = new_pixmap;
2960 #define XBM_BIT_SHUFFLE(b) (~(b))
2962 #else
2964 #define XBM_BIT_SHUFFLE(b) (b)
2966 #endif /* HAVE_NTGUI */
2969 static void
2970 Create_Pixmap_From_Bitmap_Data(f, img, data, fg, bg, non_default_colors)
2971 struct frame *f;
2972 struct image *img;
2973 char *data;
2974 RGB_PIXEL_COLOR fg, bg;
2975 int non_default_colors;
2977 #ifdef HAVE_NTGUI
2978 img->pixmap
2979 = w32_create_pixmap_from_bitmap_data (img->width, img->height, data);
2981 /* If colors were specified, transfer the bitmap to a color one. */
2982 if (non_default_colors)
2983 convert_mono_to_color_image (f, img, fg, bg);
2984 #else
2985 img->pixmap
2986 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
2987 FRAME_X_WINDOW (f),
2988 data,
2989 img->width, img->height,
2990 fg, bg,
2991 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
2992 #endif /* HAVE_NTGUI */
2997 /* Replacement for XReadBitmapFileData which isn't available under old
2998 X versions. CONTENTS is a pointer to a buffer to parse; END is the
2999 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
3000 the image. Return in *DATA the bitmap data allocated with xmalloc.
3001 Value is non-zero if successful. DATA null means just test if
3002 CONTENTS looks like an in-memory XBM file. */
3004 static int
3005 xbm_read_bitmap_data (f, contents, end, width, height, data)
3006 struct frame *f;
3007 unsigned char *contents, *end;
3008 int *width, *height;
3009 unsigned char **data;
3011 unsigned char *s = contents;
3012 char buffer[BUFSIZ];
3013 int padding_p = 0;
3014 int v10 = 0;
3015 int bytes_per_line, i, nbytes;
3016 unsigned char *p;
3017 int value;
3018 int LA1;
3020 #define match() \
3021 LA1 = xbm_scan (&s, end, buffer, &value)
3023 #define expect(TOKEN) \
3024 if (LA1 != (TOKEN)) \
3025 goto failure; \
3026 else \
3027 match ()
3029 #define expect_ident(IDENT) \
3030 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
3031 match (); \
3032 else \
3033 goto failure
3035 *width = *height = -1;
3036 if (data)
3037 *data = NULL;
3038 LA1 = xbm_scan (&s, end, buffer, &value);
3040 /* Parse defines for width, height and hot-spots. */
3041 while (LA1 == '#')
3043 match ();
3044 expect_ident ("define");
3045 expect (XBM_TK_IDENT);
3047 if (LA1 == XBM_TK_NUMBER);
3049 char *p = strrchr (buffer, '_');
3050 p = p ? p + 1 : buffer;
3051 if (strcmp (p, "width") == 0)
3052 *width = value;
3053 else if (strcmp (p, "height") == 0)
3054 *height = value;
3056 expect (XBM_TK_NUMBER);
3059 if (!check_image_size (f, *width, *height))
3060 goto failure;
3061 else if (data == NULL)
3062 goto success;
3064 /* Parse bits. Must start with `static'. */
3065 expect_ident ("static");
3066 if (LA1 == XBM_TK_IDENT)
3068 if (strcmp (buffer, "unsigned") == 0)
3070 match ();
3071 expect_ident ("char");
3073 else if (strcmp (buffer, "short") == 0)
3075 match ();
3076 v10 = 1;
3077 if (*width % 16 && *width % 16 < 9)
3078 padding_p = 1;
3080 else if (strcmp (buffer, "char") == 0)
3081 match ();
3082 else
3083 goto failure;
3085 else
3086 goto failure;
3088 expect (XBM_TK_IDENT);
3089 expect ('[');
3090 expect (']');
3091 expect ('=');
3092 expect ('{');
3094 bytes_per_line = (*width + 7) / 8 + padding_p;
3095 nbytes = bytes_per_line * *height;
3096 p = *data = (unsigned char *) xmalloc (nbytes);
3098 if (v10)
3100 for (i = 0; i < nbytes; i += 2)
3102 int val = value;
3103 expect (XBM_TK_NUMBER);
3105 *p++ = XBM_BIT_SHUFFLE (val);
3106 if (!padding_p || ((i + 2) % bytes_per_line))
3107 *p++ = XBM_BIT_SHUFFLE (value >> 8);
3109 if (LA1 == ',' || LA1 == '}')
3110 match ();
3111 else
3112 goto failure;
3115 else
3117 for (i = 0; i < nbytes; ++i)
3119 int val = value;
3120 expect (XBM_TK_NUMBER);
3122 *p++ = XBM_BIT_SHUFFLE (val);
3124 if (LA1 == ',' || LA1 == '}')
3125 match ();
3126 else
3127 goto failure;
3131 success:
3132 return 1;
3134 failure:
3136 if (data && *data)
3138 xfree (*data);
3139 *data = NULL;
3141 return 0;
3143 #undef match
3144 #undef expect
3145 #undef expect_ident
3149 /* Load XBM image IMG which will be displayed on frame F from buffer
3150 CONTENTS. END is the end of the buffer. Value is non-zero if
3151 successful. */
3153 static int
3154 xbm_load_image (f, img, contents, end)
3155 struct frame *f;
3156 struct image *img;
3157 unsigned char *contents, *end;
3159 int rc;
3160 unsigned char *data;
3161 int success_p = 0;
3163 rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height, &data);
3164 if (rc)
3166 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
3167 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
3168 int non_default_colors = 0;
3169 Lisp_Object value;
3171 xassert (img->width > 0 && img->height > 0);
3173 /* Get foreground and background colors, maybe allocate colors. */
3174 value = image_spec_value (img->spec, QCforeground, NULL);
3175 if (!NILP (value))
3177 foreground = x_alloc_image_color (f, img, value, foreground);
3178 non_default_colors = 1;
3180 value = image_spec_value (img->spec, QCbackground, NULL);
3181 if (!NILP (value))
3183 background = x_alloc_image_color (f, img, value, background);
3184 img->background = background;
3185 img->background_valid = 1;
3186 non_default_colors = 1;
3189 Create_Pixmap_From_Bitmap_Data (f, img, data,
3190 foreground, background,
3191 non_default_colors);
3192 xfree (data);
3194 if (img->pixmap == NO_PIXMAP)
3196 x_clear_image (f, img);
3197 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
3199 else
3200 success_p = 1;
3202 else
3203 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
3205 return success_p;
3209 /* Value is non-zero if DATA looks like an in-memory XBM file. */
3211 static int
3212 xbm_file_p (data)
3213 Lisp_Object data;
3215 int w, h;
3216 return (STRINGP (data)
3217 && xbm_read_bitmap_data (NULL, SDATA (data),
3218 (SDATA (data)
3219 + SBYTES (data)),
3220 &w, &h, NULL));
3224 /* Fill image IMG which is used on frame F with pixmap data. Value is
3225 non-zero if successful. */
3227 static int
3228 xbm_load (f, img)
3229 struct frame *f;
3230 struct image *img;
3232 int success_p = 0;
3233 Lisp_Object file_name;
3235 xassert (xbm_image_p (img->spec));
3237 /* If IMG->spec specifies a file name, create a non-file spec from it. */
3238 file_name = image_spec_value (img->spec, QCfile, NULL);
3239 if (STRINGP (file_name))
3241 Lisp_Object file;
3242 unsigned char *contents;
3243 int size;
3244 struct gcpro gcpro1;
3246 file = x_find_image_file (file_name);
3247 GCPRO1 (file);
3248 if (!STRINGP (file))
3250 image_error ("Cannot find image file `%s'", file_name, Qnil);
3251 UNGCPRO;
3252 return 0;
3255 contents = slurp_file (SDATA (file), &size);
3256 if (contents == NULL)
3258 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
3259 UNGCPRO;
3260 return 0;
3263 success_p = xbm_load_image (f, img, contents, contents + size);
3264 UNGCPRO;
3266 else
3268 struct image_keyword fmt[XBM_LAST];
3269 Lisp_Object data;
3270 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
3271 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
3272 int non_default_colors = 0;
3273 char *bits;
3274 int parsed_p;
3275 int in_memory_file_p = 0;
3277 /* See if data looks like an in-memory XBM file. */
3278 data = image_spec_value (img->spec, QCdata, NULL);
3279 in_memory_file_p = xbm_file_p (data);
3281 /* Parse the image specification. */
3282 bcopy (xbm_format, fmt, sizeof fmt);
3283 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
3284 xassert (parsed_p);
3286 /* Get specified width, and height. */
3287 if (!in_memory_file_p)
3289 img->width = XFASTINT (fmt[XBM_WIDTH].value);
3290 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
3291 xassert (img->width > 0 && img->height > 0);
3294 /* Get foreground and background colors, maybe allocate colors. */
3295 if (fmt[XBM_FOREGROUND].count
3296 && STRINGP (fmt[XBM_FOREGROUND].value))
3298 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
3299 foreground);
3300 non_default_colors = 1;
3303 if (fmt[XBM_BACKGROUND].count
3304 && STRINGP (fmt[XBM_BACKGROUND].value))
3306 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
3307 background);
3308 non_default_colors = 1;
3311 if (in_memory_file_p)
3312 success_p = xbm_load_image (f, img, SDATA (data),
3313 (SDATA (data)
3314 + SBYTES (data)));
3315 else
3317 if (VECTORP (data))
3319 int i;
3320 char *p;
3321 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
3323 p = bits = (char *) alloca (nbytes * img->height);
3324 for (i = 0; i < img->height; ++i, p += nbytes)
3326 Lisp_Object line = XVECTOR (data)->contents[i];
3327 if (STRINGP (line))
3328 bcopy (SDATA (line), p, nbytes);
3329 else
3330 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
3333 else if (STRINGP (data))
3334 bits = SDATA (data);
3335 else
3336 bits = XBOOL_VECTOR (data)->data;
3338 /* Create the pixmap. */
3340 Create_Pixmap_From_Bitmap_Data (f, img, bits,
3341 foreground, background,
3342 non_default_colors);
3343 if (img->pixmap)
3344 success_p = 1;
3345 else
3347 image_error ("Unable to create pixmap for XBM image `%s'",
3348 img->spec, Qnil);
3349 x_clear_image (f, img);
3354 return success_p;
3359 /***********************************************************************
3360 XPM images
3361 ***********************************************************************/
3363 #if defined (HAVE_XPM) || defined (MAC_OS)
3365 static int xpm_image_p P_ ((Lisp_Object object));
3366 static int xpm_load P_ ((struct frame *f, struct image *img));
3367 static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
3369 #endif /* HAVE_XPM || MAC_OS */
3371 #ifdef HAVE_XPM
3372 #ifdef HAVE_NTGUI
3373 /* Indicate to xpm.h that we don't have Xlib. */
3374 #define FOR_MSW
3375 /* simx.h in xpm defines XColor and XImage differently than Emacs. */
3376 /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */
3377 #define XColor xpm_XColor
3378 #define XImage xpm_XImage
3379 #define Display xpm_Display
3380 #define PIXEL_ALREADY_TYPEDEFED
3381 #include "X11/xpm.h"
3382 #undef FOR_MSW
3383 #undef XColor
3384 #undef XImage
3385 #undef Display
3386 #undef PIXEL_ALREADY_TYPEDEFED
3387 #else
3388 #include "X11/xpm.h"
3389 #endif /* HAVE_NTGUI */
3390 #endif /* HAVE_XPM */
3392 #if defined (HAVE_XPM) || defined (MAC_OS)
3393 /* The symbol `xpm' identifying XPM-format images. */
3395 Lisp_Object Qxpm;
3397 /* Indices of image specification fields in xpm_format, below. */
3399 enum xpm_keyword_index
3401 XPM_TYPE,
3402 XPM_FILE,
3403 XPM_DATA,
3404 XPM_ASCENT,
3405 XPM_MARGIN,
3406 XPM_RELIEF,
3407 XPM_ALGORITHM,
3408 XPM_HEURISTIC_MASK,
3409 XPM_MASK,
3410 XPM_COLOR_SYMBOLS,
3411 XPM_BACKGROUND,
3412 XPM_LAST
3415 /* Vector of image_keyword structures describing the format
3416 of valid XPM image specifications. */
3418 static struct image_keyword xpm_format[XPM_LAST] =
3420 {":type", IMAGE_SYMBOL_VALUE, 1},
3421 {":file", IMAGE_STRING_VALUE, 0},
3422 {":data", IMAGE_STRING_VALUE, 0},
3423 {":ascent", IMAGE_ASCENT_VALUE, 0},
3424 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
3425 {":relief", IMAGE_INTEGER_VALUE, 0},
3426 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3427 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3428 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3429 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3430 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
3433 /* Structure describing the image type XPM. */
3435 static struct image_type xpm_type =
3437 &Qxpm,
3438 xpm_image_p,
3439 xpm_load,
3440 x_clear_image,
3441 NULL
3444 #ifdef HAVE_X_WINDOWS
3446 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
3447 functions for allocating image colors. Our own functions handle
3448 color allocation failures more gracefully than the ones on the XPM
3449 lib. */
3451 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
3452 #define ALLOC_XPM_COLORS
3453 #endif
3454 #endif /* HAVE_X_WINDOWS */
3456 #ifdef ALLOC_XPM_COLORS
3458 static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *));
3459 static void xpm_free_color_cache P_ ((void));
3460 static int xpm_lookup_color P_ ((struct frame *, char *, XColor *));
3461 static int xpm_color_bucket P_ ((char *));
3462 static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *,
3463 XColor *, int));
3465 /* An entry in a hash table used to cache color definitions of named
3466 colors. This cache is necessary to speed up XPM image loading in
3467 case we do color allocations ourselves. Without it, we would need
3468 a call to XParseColor per pixel in the image. */
3470 struct xpm_cached_color
3472 /* Next in collision chain. */
3473 struct xpm_cached_color *next;
3475 /* Color definition (RGB and pixel color). */
3476 XColor color;
3478 /* Color name. */
3479 char name[1];
3482 /* The hash table used for the color cache, and its bucket vector
3483 size. */
3485 #define XPM_COLOR_CACHE_BUCKETS 1001
3486 struct xpm_cached_color **xpm_color_cache;
3488 /* Initialize the color cache. */
3490 static void
3491 xpm_init_color_cache (f, attrs)
3492 struct frame *f;
3493 XpmAttributes *attrs;
3495 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
3496 xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes);
3497 memset (xpm_color_cache, 0, nbytes);
3498 init_color_table ();
3500 if (attrs->valuemask & XpmColorSymbols)
3502 int i;
3503 XColor color;
3505 for (i = 0; i < attrs->numsymbols; ++i)
3506 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
3507 attrs->colorsymbols[i].value, &color))
3509 color.pixel = lookup_rgb_color (f, color.red, color.green,
3510 color.blue);
3511 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
3516 /* Free the color cache. */
3518 static void
3519 xpm_free_color_cache ()
3521 struct xpm_cached_color *p, *next;
3522 int i;
3524 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
3525 for (p = xpm_color_cache[i]; p; p = next)
3527 next = p->next;
3528 xfree (p);
3531 xfree (xpm_color_cache);
3532 xpm_color_cache = NULL;
3533 free_color_table ();
3536 /* Return the bucket index for color named COLOR_NAME in the color
3537 cache. */
3539 static int
3540 xpm_color_bucket (color_name)
3541 char *color_name;
3543 unsigned h = 0;
3544 char *s;
3546 for (s = color_name; *s; ++s)
3547 h = (h << 2) ^ *s;
3548 return h %= XPM_COLOR_CACHE_BUCKETS;
3552 /* On frame F, cache values COLOR for color with name COLOR_NAME.
3553 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
3554 entry added. */
3556 static struct xpm_cached_color *
3557 xpm_cache_color (f, color_name, color, bucket)
3558 struct frame *f;
3559 char *color_name;
3560 XColor *color;
3561 int bucket;
3563 size_t nbytes;
3564 struct xpm_cached_color *p;
3566 if (bucket < 0)
3567 bucket = xpm_color_bucket (color_name);
3569 nbytes = sizeof *p + strlen (color_name);
3570 p = (struct xpm_cached_color *) xmalloc (nbytes);
3571 strcpy (p->name, color_name);
3572 p->color = *color;
3573 p->next = xpm_color_cache[bucket];
3574 xpm_color_cache[bucket] = p;
3575 return p;
3578 /* Look up color COLOR_NAME for frame F in the color cache. If found,
3579 return the cached definition in *COLOR. Otherwise, make a new
3580 entry in the cache and allocate the color. Value is zero if color
3581 allocation failed. */
3583 static int
3584 xpm_lookup_color (f, color_name, color)
3585 struct frame *f;
3586 char *color_name;
3587 XColor *color;
3589 struct xpm_cached_color *p;
3590 int h = xpm_color_bucket (color_name);
3592 for (p = xpm_color_cache[h]; p; p = p->next)
3593 if (strcmp (p->name, color_name) == 0)
3594 break;
3596 if (p != NULL)
3597 *color = p->color;
3598 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
3599 color_name, color))
3601 color->pixel = lookup_rgb_color (f, color->red, color->green,
3602 color->blue);
3603 p = xpm_cache_color (f, color_name, color, h);
3605 /* You get `opaque' at least from ImageMagick converting pbm to xpm
3606 with transparency, and it's useful. */
3607 else if (strcmp ("opaque", color_name) == 0)
3609 bzero (color, sizeof (XColor)); /* Is this necessary/correct? */
3610 color->pixel = FRAME_FOREGROUND_PIXEL (f);
3611 p = xpm_cache_color (f, color_name, color, h);
3614 return p != NULL;
3618 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
3619 CLOSURE is a pointer to the frame on which we allocate the
3620 color. Return in *COLOR the allocated color. Value is non-zero
3621 if successful. */
3623 static int
3624 xpm_alloc_color (dpy, cmap, color_name, color, closure)
3625 Display *dpy;
3626 Colormap cmap;
3627 char *color_name;
3628 XColor *color;
3629 void *closure;
3631 return xpm_lookup_color ((struct frame *) closure, color_name, color);
3635 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
3636 is a pointer to the frame on which we allocate the color. Value is
3637 non-zero if successful. */
3639 static int
3640 xpm_free_colors (dpy, cmap, pixels, npixels, closure)
3641 Display *dpy;
3642 Colormap cmap;
3643 Pixel *pixels;
3644 int npixels;
3645 void *closure;
3647 return 1;
3650 #endif /* ALLOC_XPM_COLORS */
3653 #ifdef HAVE_NTGUI
3655 /* XPM library details. */
3657 DEF_IMGLIB_FN (XpmFreeAttributes);
3658 DEF_IMGLIB_FN (XpmCreateImageFromBuffer);
3659 DEF_IMGLIB_FN (XpmReadFileToImage);
3660 DEF_IMGLIB_FN (XImageFree);
3662 static int
3663 init_xpm_functions (Lisp_Object libraries)
3665 HMODULE library;
3667 if (!(library = w32_delayed_load (libraries, Qxpm)))
3668 return 0;
3670 LOAD_IMGLIB_FN (library, XpmFreeAttributes);
3671 LOAD_IMGLIB_FN (library, XpmCreateImageFromBuffer);
3672 LOAD_IMGLIB_FN (library, XpmReadFileToImage);
3673 LOAD_IMGLIB_FN (library, XImageFree);
3674 return 1;
3677 #endif /* HAVE_NTGUI */
3680 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
3681 for XPM images. Such a list must consist of conses whose car and
3682 cdr are strings. */
3684 static int
3685 xpm_valid_color_symbols_p (color_symbols)
3686 Lisp_Object color_symbols;
3688 while (CONSP (color_symbols))
3690 Lisp_Object sym = XCAR (color_symbols);
3691 if (!CONSP (sym)
3692 || !STRINGP (XCAR (sym))
3693 || !STRINGP (XCDR (sym)))
3694 break;
3695 color_symbols = XCDR (color_symbols);
3698 return NILP (color_symbols);
3702 /* Value is non-zero if OBJECT is a valid XPM image specification. */
3704 static int
3705 xpm_image_p (object)
3706 Lisp_Object object;
3708 struct image_keyword fmt[XPM_LAST];
3709 bcopy (xpm_format, fmt, sizeof fmt);
3710 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
3711 /* Either `:file' or `:data' must be present. */
3712 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
3713 /* Either no `:color-symbols' or it's a list of conses
3714 whose car and cdr are strings. */
3715 && (fmt[XPM_COLOR_SYMBOLS].count == 0
3716 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
3719 #endif /* HAVE_XPM || MAC_OS */
3721 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
3723 x_create_bitmap_from_xpm_data (f, bits)
3724 struct frame *f;
3725 char **bits;
3727 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3728 int id, rc;
3729 XpmAttributes attrs;
3730 Pixmap bitmap, mask;
3732 bzero (&attrs, sizeof attrs);
3734 attrs.visual = FRAME_X_VISUAL (f);
3735 attrs.colormap = FRAME_X_COLORMAP (f);
3736 attrs.valuemask |= XpmVisual;
3737 attrs.valuemask |= XpmColormap;
3739 rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3740 bits, &bitmap, &mask, &attrs);
3741 if (rc != XpmSuccess)
3743 XpmFreeAttributes (&attrs);
3744 return -1;
3747 id = x_allocate_bitmap_record (f);
3748 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
3749 dpyinfo->bitmaps[id - 1].have_mask = 1;
3750 dpyinfo->bitmaps[id - 1].mask = mask;
3751 dpyinfo->bitmaps[id - 1].file = NULL;
3752 dpyinfo->bitmaps[id - 1].height = attrs.height;
3753 dpyinfo->bitmaps[id - 1].width = attrs.width;
3754 dpyinfo->bitmaps[id - 1].depth = attrs.depth;
3755 dpyinfo->bitmaps[id - 1].refcount = 1;
3757 XpmFreeAttributes (&attrs);
3758 return id;
3760 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
3762 /* Load image IMG which will be displayed on frame F. Value is
3763 non-zero if successful. */
3765 #ifdef HAVE_XPM
3767 static int
3768 xpm_load (f, img)
3769 struct frame *f;
3770 struct image *img;
3772 int rc;
3773 XpmAttributes attrs;
3774 Lisp_Object specified_file, color_symbols;
3775 #ifdef HAVE_NTGUI
3776 HDC hdc;
3777 xpm_XImage * xpm_image = NULL, * xpm_mask = NULL;
3778 #endif /* HAVE_NTGUI */
3780 /* Configure the XPM lib. Use the visual of frame F. Allocate
3781 close colors. Return colors allocated. */
3782 bzero (&attrs, sizeof attrs);
3784 #ifndef HAVE_NTGUI
3785 attrs.visual = FRAME_X_VISUAL (f);
3786 attrs.colormap = FRAME_X_COLORMAP (f);
3787 attrs.valuemask |= XpmVisual;
3788 attrs.valuemask |= XpmColormap;
3789 #endif /* HAVE_NTGUI */
3791 #ifdef ALLOC_XPM_COLORS
3792 /* Allocate colors with our own functions which handle
3793 failing color allocation more gracefully. */
3794 attrs.color_closure = f;
3795 attrs.alloc_color = xpm_alloc_color;
3796 attrs.free_colors = xpm_free_colors;
3797 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
3798 #else /* not ALLOC_XPM_COLORS */
3799 /* Let the XPM lib allocate colors. */
3800 attrs.valuemask |= XpmReturnAllocPixels;
3801 #ifdef XpmAllocCloseColors
3802 attrs.alloc_close_colors = 1;
3803 attrs.valuemask |= XpmAllocCloseColors;
3804 #else /* not XpmAllocCloseColors */
3805 attrs.closeness = 600;
3806 attrs.valuemask |= XpmCloseness;
3807 #endif /* not XpmAllocCloseColors */
3808 #endif /* ALLOC_XPM_COLORS */
3809 #ifdef ALLOC_XPM_COLORS
3810 xpm_init_color_cache (f, &attrs);
3811 #endif
3813 /* If image specification contains symbolic color definitions, add
3814 these to `attrs'. */
3815 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
3816 if (CONSP (color_symbols))
3818 Lisp_Object tail;
3819 XpmColorSymbol *xpm_syms;
3820 int i, size;
3822 attrs.valuemask |= XpmColorSymbols;
3824 /* Count number of symbols. */
3825 attrs.numsymbols = 0;
3826 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
3827 ++attrs.numsymbols;
3829 /* Allocate an XpmColorSymbol array. */
3830 size = attrs.numsymbols * sizeof *xpm_syms;
3831 xpm_syms = (XpmColorSymbol *) alloca (size);
3832 bzero (xpm_syms, size);
3833 attrs.colorsymbols = xpm_syms;
3835 /* Fill the color symbol array. */
3836 for (tail = color_symbols, i = 0;
3837 CONSP (tail);
3838 ++i, tail = XCDR (tail))
3840 Lisp_Object name = XCAR (XCAR (tail));
3841 Lisp_Object color = XCDR (XCAR (tail));
3842 xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1);
3843 strcpy (xpm_syms[i].name, SDATA (name));
3844 xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1);
3845 strcpy (xpm_syms[i].value, SDATA (color));
3849 /* Create a pixmap for the image, either from a file, or from a
3850 string buffer containing data in the same format as an XPM file. */
3851 #ifdef ALLOC_XPM_COLORS
3852 xpm_init_color_cache (f, &attrs);
3853 #endif
3855 specified_file = image_spec_value (img->spec, QCfile, NULL);
3857 #ifdef HAVE_NTGUI
3859 HDC frame_dc = get_frame_dc (f);
3860 hdc = CreateCompatibleDC (frame_dc);
3861 release_frame_dc (f, frame_dc);
3863 #endif /* HAVE_NTGUI */
3865 if (STRINGP (specified_file))
3867 Lisp_Object file = x_find_image_file (specified_file);
3868 if (!STRINGP (file))
3870 image_error ("Cannot find image file `%s'", specified_file, Qnil);
3871 return 0;
3874 #ifdef HAVE_NTGUI
3875 /* XpmReadFileToPixmap is not available in the Windows port of
3876 libxpm. But XpmReadFileToImage almost does what we want. */
3877 rc = fn_XpmReadFileToImage (&hdc, SDATA (file),
3878 &xpm_image, &xpm_mask,
3879 &attrs);
3880 #else
3881 rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3882 SDATA (file), &img->pixmap, &img->mask,
3883 &attrs);
3884 #endif /* HAVE_NTGUI */
3886 else
3888 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
3889 #ifdef HAVE_NTGUI
3890 /* XpmCreatePixmapFromBuffer is not available in the Windows port
3891 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
3892 rc = fn_XpmCreateImageFromBuffer (&hdc, SDATA (buffer),
3893 &xpm_image, &xpm_mask,
3894 &attrs);
3895 #else
3896 rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3897 SDATA (buffer),
3898 &img->pixmap, &img->mask,
3899 &attrs);
3900 #endif /* HAVE_NTGUI */
3903 if (rc == XpmSuccess)
3905 #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS)
3906 img->colors = colors_in_color_table (&img->ncolors);
3907 #else /* not ALLOC_XPM_COLORS */
3908 int i;
3910 #ifdef HAVE_NTGUI
3911 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
3912 plus some duplicate attributes. */
3913 if (xpm_image && xpm_image->bitmap)
3915 img->pixmap = xpm_image->bitmap;
3916 /* XImageFree in libXpm frees XImage struct without destroying
3917 the bitmap, which is what we want. */
3918 fn_XImageFree (xpm_image);
3920 if (xpm_mask && xpm_mask->bitmap)
3922 /* The mask appears to be inverted compared with what we expect.
3923 TODO: invert our expectations. See other places where we
3924 have to invert bits because our idea of masks is backwards. */
3925 HGDIOBJ old_obj;
3926 old_obj = SelectObject (hdc, xpm_mask->bitmap);
3928 PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT);
3929 SelectObject (hdc, old_obj);
3931 img->mask = xpm_mask->bitmap;
3932 fn_XImageFree (xpm_mask);
3933 DeleteDC (hdc);
3936 DeleteDC (hdc);
3937 #endif /* HAVE_NTGUI */
3939 /* Remember allocated colors. */
3940 img->ncolors = attrs.nalloc_pixels;
3941 img->colors = (unsigned long *) xmalloc (img->ncolors
3942 * sizeof *img->colors);
3943 for (i = 0; i < attrs.nalloc_pixels; ++i)
3945 img->colors[i] = attrs.alloc_pixels[i];
3946 #ifdef DEBUG_X_COLORS
3947 register_color (img->colors[i]);
3948 #endif
3950 #endif /* not ALLOC_XPM_COLORS */
3952 img->width = attrs.width;
3953 img->height = attrs.height;
3954 xassert (img->width > 0 && img->height > 0);
3956 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
3957 #ifdef HAVE_NTGUI
3958 fn_XpmFreeAttributes (&attrs);
3959 #else
3960 XpmFreeAttributes (&attrs);
3961 #endif /* HAVE_NTGUI */
3963 else
3965 #ifdef HAVE_NTGUI
3966 DeleteDC (hdc);
3967 #endif /* HAVE_NTGUI */
3969 switch (rc)
3971 case XpmOpenFailed:
3972 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
3973 break;
3975 case XpmFileInvalid:
3976 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
3977 break;
3979 case XpmNoMemory:
3980 image_error ("Out of memory (%s)", img->spec, Qnil);
3981 break;
3983 case XpmColorFailed:
3984 image_error ("Color allocation error (%s)", img->spec, Qnil);
3985 break;
3987 default:
3988 image_error ("Unknown error (%s)", img->spec, Qnil);
3989 break;
3993 #ifdef ALLOC_XPM_COLORS
3994 xpm_free_color_cache ();
3995 #endif
3996 return rc == XpmSuccess;
3999 #endif /* HAVE_XPM */
4001 #ifdef MAC_OS
4003 /* XPM support functions for Mac OS where libxpm is not available.
4004 Only XPM version 3 (without any extensions) is supported. */
4006 static int xpm_scan P_ ((unsigned char **, unsigned char *,
4007 unsigned char **, int *));
4008 static Lisp_Object xpm_make_color_table_v
4009 P_ ((void (**) (Lisp_Object, unsigned char *, int, Lisp_Object),
4010 Lisp_Object (**) (Lisp_Object, unsigned char *, int)));
4011 static void xpm_put_color_table_v P_ ((Lisp_Object, unsigned char *,
4012 int, Lisp_Object));
4013 static Lisp_Object xpm_get_color_table_v P_ ((Lisp_Object,
4014 unsigned char *, int));
4015 static Lisp_Object xpm_make_color_table_h
4016 P_ ((void (**) (Lisp_Object, unsigned char *, int, Lisp_Object),
4017 Lisp_Object (**) (Lisp_Object, unsigned char *, int)));
4018 static void xpm_put_color_table_h P_ ((Lisp_Object, unsigned char *,
4019 int, Lisp_Object));
4020 static Lisp_Object xpm_get_color_table_h P_ ((Lisp_Object,
4021 unsigned char *, int));
4022 static int xpm_str_to_color_key P_ ((char *));
4023 static int xpm_load_image P_ ((struct frame *, struct image *,
4024 unsigned char *, unsigned char *));
4026 /* Tokens returned from xpm_scan. */
4028 enum xpm_token
4030 XPM_TK_IDENT = 256,
4031 XPM_TK_STRING,
4032 XPM_TK_EOF
4035 /* Scan an XPM data and return a character (< 256) or a token defined
4036 by enum xpm_token above. *S and END are the start (inclusive) and
4037 the end (exclusive) addresses of the data, respectively. Advance
4038 *S while scanning. If token is either XPM_TK_IDENT or
4039 XPM_TK_STRING, *BEG and *LEN are set to the start address and the
4040 length of the corresponding token, respectively. */
4042 static int
4043 xpm_scan (s, end, beg, len)
4044 unsigned char **s, *end, **beg;
4045 int *len;
4047 int c;
4049 while (*s < end)
4051 /* Skip white-space. */
4052 while (*s < end && (c = *(*s)++, isspace (c)))
4055 /* gnus-pointer.xpm uses '-' in its identifier.
4056 sb-dir-plus.xpm uses '+' in its identifier. */
4057 if (isalpha (c) || c == '_' || c == '-' || c == '+')
4059 *beg = *s - 1;
4060 while (*s < end &&
4061 (c = **s, isalnum (c) || c == '_' || c == '-' || c == '+'))
4062 ++*s;
4063 *len = *s - *beg;
4064 return XPM_TK_IDENT;
4066 else if (c == '"')
4068 *beg = *s;
4069 while (*s < end && **s != '"')
4070 ++*s;
4071 *len = *s - *beg;
4072 if (*s < end)
4073 ++*s;
4074 return XPM_TK_STRING;
4076 else if (c == '/')
4078 if (*s < end && **s == '*')
4080 /* C-style comment. */
4081 ++*s;
4084 while (*s < end && *(*s)++ != '*')
4087 while (*s < end && **s != '/');
4088 if (*s < end)
4089 ++*s;
4091 else
4092 return c;
4094 else
4095 return c;
4098 return XPM_TK_EOF;
4101 /* Functions for color table lookup in XPM data. A Key is a string
4102 specifying the color of each pixel in XPM data. A value is either
4103 an integer that specifies a pixel color, Qt that specifies
4104 transparency, or Qnil for the unspecified color. If the length of
4105 the key string is one, a vector is used as a table. Otherwise, a
4106 hash table is used. */
4108 static Lisp_Object
4109 xpm_make_color_table_v (put_func, get_func)
4110 void (**put_func) (Lisp_Object, unsigned char *, int, Lisp_Object);
4111 Lisp_Object (**get_func) (Lisp_Object, unsigned char *, int);
4113 *put_func = xpm_put_color_table_v;
4114 *get_func = xpm_get_color_table_v;
4115 return Fmake_vector (make_number (256), Qnil);
4118 static void
4119 xpm_put_color_table_v (color_table, chars_start, chars_len, color)
4120 Lisp_Object color_table;
4121 unsigned char *chars_start;
4122 int chars_len;
4123 Lisp_Object color;
4125 XVECTOR (color_table)->contents[*chars_start] = color;
4128 static Lisp_Object
4129 xpm_get_color_table_v (color_table, chars_start, chars_len)
4130 Lisp_Object color_table;
4131 unsigned char *chars_start;
4132 int chars_len;
4134 return XVECTOR (color_table)->contents[*chars_start];
4137 static Lisp_Object
4138 xpm_make_color_table_h (put_func, get_func)
4139 void (**put_func) (Lisp_Object, unsigned char *, int, Lisp_Object);
4140 Lisp_Object (**get_func) (Lisp_Object, unsigned char *, int);
4142 *put_func = xpm_put_color_table_h;
4143 *get_func = xpm_get_color_table_h;
4144 return make_hash_table (Qequal, make_number (DEFAULT_HASH_SIZE),
4145 make_float (DEFAULT_REHASH_SIZE),
4146 make_float (DEFAULT_REHASH_THRESHOLD),
4147 Qnil, Qnil, Qnil);
4150 static void
4151 xpm_put_color_table_h (color_table, chars_start, chars_len, color)
4152 Lisp_Object color_table;
4153 unsigned char *chars_start;
4154 int chars_len;
4155 Lisp_Object color;
4157 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
4158 unsigned hash_code;
4159 Lisp_Object chars = make_unibyte_string (chars_start, chars_len);
4161 hash_lookup (table, chars, &hash_code);
4162 hash_put (table, chars, color, hash_code);
4165 static Lisp_Object
4166 xpm_get_color_table_h (color_table, chars_start, chars_len)
4167 Lisp_Object color_table;
4168 unsigned char *chars_start;
4169 int chars_len;
4171 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
4172 int i = hash_lookup (table, make_unibyte_string (chars_start, chars_len),
4173 NULL);
4175 return i >= 0 ? HASH_VALUE (table, i) : Qnil;
4178 enum xpm_color_key {
4179 XPM_COLOR_KEY_S,
4180 XPM_COLOR_KEY_M,
4181 XPM_COLOR_KEY_G4,
4182 XPM_COLOR_KEY_G,
4183 XPM_COLOR_KEY_C
4186 static char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"};
4188 static int
4189 xpm_str_to_color_key (s)
4190 char *s;
4192 int i;
4194 for (i = 0;
4195 i < sizeof xpm_color_key_strings / sizeof xpm_color_key_strings[0];
4196 i++)
4197 if (strcmp (xpm_color_key_strings[i], s) == 0)
4198 return i;
4199 return -1;
4202 static int
4203 xpm_load_image (f, img, contents, end)
4204 struct frame *f;
4205 struct image *img;
4206 unsigned char *contents, *end;
4208 unsigned char *s = contents, *beg, *str;
4209 unsigned char buffer[BUFSIZ];
4210 int width, height, x, y;
4211 int num_colors, chars_per_pixel;
4212 int len, LA1;
4213 void (*put_color_table) (Lisp_Object, unsigned char *, int, Lisp_Object);
4214 Lisp_Object (*get_color_table) (Lisp_Object, unsigned char *, int);
4215 Lisp_Object frame, color_symbols, color_table;
4216 int best_key, have_mask = 0;
4217 XImagePtr ximg = NULL, mask_img = NULL;
4219 #define match() \
4220 LA1 = xpm_scan (&s, end, &beg, &len)
4222 #define expect(TOKEN) \
4223 if (LA1 != (TOKEN)) \
4224 goto failure; \
4225 else \
4226 match ()
4228 #define expect_ident(IDENT) \
4229 if (LA1 == XPM_TK_IDENT \
4230 && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \
4231 match (); \
4232 else \
4233 goto failure
4235 if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0))
4236 goto failure;
4237 s += 9;
4238 match();
4239 expect_ident ("static");
4240 expect_ident ("char");
4241 expect ('*');
4242 expect (XPM_TK_IDENT);
4243 expect ('[');
4244 expect (']');
4245 expect ('=');
4246 expect ('{');
4247 expect (XPM_TK_STRING);
4248 if (len >= BUFSIZ)
4249 goto failure;
4250 memcpy (buffer, beg, len);
4251 buffer[len] = '\0';
4252 if (sscanf (buffer, "%d %d %d %d", &width, &height,
4253 &num_colors, &chars_per_pixel) != 4
4254 || width <= 0 || height <= 0
4255 || num_colors <= 0 || chars_per_pixel <= 0)
4256 goto failure;
4258 if (!check_image_size (f, width, height))
4260 image_error ("Invalid image size", Qnil, Qnil);
4261 goto failure;
4264 expect (',');
4266 XSETFRAME (frame, f);
4267 if (!NILP (Fxw_display_color_p (frame)))
4268 best_key = XPM_COLOR_KEY_C;
4269 else if (!NILP (Fx_display_grayscale_p (frame)))
4270 best_key = (XFASTINT (Fx_display_planes (frame)) > 2
4271 ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4);
4272 else
4273 best_key = XPM_COLOR_KEY_M;
4275 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
4276 if (chars_per_pixel == 1)
4277 color_table = xpm_make_color_table_v (&put_color_table,
4278 &get_color_table);
4279 else
4280 color_table = xpm_make_color_table_h (&put_color_table,
4281 &get_color_table);
4283 while (num_colors-- > 0)
4285 unsigned char *color, *max_color;
4286 int key, next_key, max_key = 0;
4287 Lisp_Object symbol_color = Qnil, color_val;
4288 XColor cdef;
4290 expect (XPM_TK_STRING);
4291 if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel)
4292 goto failure;
4293 memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel);
4294 buffer[len - chars_per_pixel] = '\0';
4296 str = strtok (buffer, " \t");
4297 if (str == NULL)
4298 goto failure;
4299 key = xpm_str_to_color_key (str);
4300 if (key < 0)
4301 goto failure;
4304 color = strtok (NULL, " \t");
4305 if (color == NULL)
4306 goto failure;
4308 while (str = strtok (NULL, " \t"))
4310 next_key = xpm_str_to_color_key (str);
4311 if (next_key >= 0)
4312 break;
4313 color[strlen (color)] = ' ';
4316 if (key == XPM_COLOR_KEY_S)
4318 if (NILP (symbol_color))
4319 symbol_color = build_string (color);
4321 else if (max_key < key && key <= best_key)
4323 max_key = key;
4324 max_color = color;
4326 key = next_key;
4328 while (str);
4330 color_val = Qnil;
4331 if (!NILP (color_symbols) && !NILP (symbol_color))
4333 Lisp_Object specified_color = Fassoc (symbol_color, color_symbols);
4335 if (CONSP (specified_color) && STRINGP (XCDR (specified_color)))
4336 if (xstricmp (SDATA (XCDR (specified_color)), "None") == 0)
4337 color_val = Qt;
4338 else if (x_defined_color (f, SDATA (XCDR (specified_color)),
4339 &cdef, 0))
4340 color_val = make_number (cdef.pixel);
4342 if (NILP (color_val) && max_key > 0)
4343 if (xstricmp (max_color, "None") == 0)
4344 color_val = Qt;
4345 else if (x_defined_color (f, max_color, &cdef, 0))
4346 color_val = make_number (cdef.pixel);
4347 if (!NILP (color_val))
4348 (*put_color_table) (color_table, beg, chars_per_pixel, color_val);
4350 expect (',');
4353 if (!x_create_x_image_and_pixmap (f, width, height, 0,
4354 &ximg, &img->pixmap)
4355 || !x_create_x_image_and_pixmap (f, width, height, 1,
4356 &mask_img, &img->mask))
4358 image_error ("Out of memory (%s)", img->spec, Qnil);
4359 goto error;
4362 for (y = 0; y < height; y++)
4364 expect (XPM_TK_STRING);
4365 str = beg;
4366 if (len < width * chars_per_pixel)
4367 goto failure;
4368 for (x = 0; x < width; x++, str += chars_per_pixel)
4370 Lisp_Object color_val =
4371 (*get_color_table) (color_table, str, chars_per_pixel);
4373 XPutPixel (ximg, x, y,
4374 (INTEGERP (color_val) ? XINT (color_val)
4375 : FRAME_FOREGROUND_PIXEL (f)));
4376 XPutPixel (mask_img, x, y,
4377 (!EQ (color_val, Qt) ? PIX_MASK_DRAW
4378 : (have_mask = 1, PIX_MASK_RETAIN)));
4380 if (y + 1 < height)
4381 expect (',');
4384 img->width = width;
4385 img->height = height;
4387 x_put_x_image (f, ximg, img->pixmap, width, height);
4388 x_destroy_x_image (ximg);
4389 if (have_mask)
4391 /* Fill in the background_transparent field while we have the
4392 mask handy. */
4393 image_background_transparent (img, f, mask_img);
4395 x_put_x_image (f, mask_img, img->mask, width, height);
4396 x_destroy_x_image (mask_img);
4398 else
4400 x_destroy_x_image (mask_img);
4401 Free_Pixmap (FRAME_X_DISPLAY (f), img->mask);
4402 img->mask = NO_PIXMAP;
4405 return 1;
4407 failure:
4408 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
4409 error:
4410 x_destroy_x_image (ximg);
4411 x_destroy_x_image (mask_img);
4412 x_clear_image (f, img);
4413 return 0;
4415 #undef match
4416 #undef expect
4417 #undef expect_ident
4420 static int
4421 xpm_load (f, img)
4422 struct frame *f;
4423 struct image *img;
4425 int success_p = 0;
4426 Lisp_Object file_name;
4428 /* If IMG->spec specifies a file name, create a non-file spec from it. */
4429 file_name = image_spec_value (img->spec, QCfile, NULL);
4430 if (STRINGP (file_name))
4432 Lisp_Object file;
4433 unsigned char *contents;
4434 int size;
4435 struct gcpro gcpro1;
4437 file = x_find_image_file (file_name);
4438 GCPRO1 (file);
4439 if (!STRINGP (file))
4441 image_error ("Cannot find image file `%s'", file_name, Qnil);
4442 UNGCPRO;
4443 return 0;
4446 contents = slurp_file (SDATA (file), &size);
4447 if (contents == NULL)
4449 image_error ("Error loading XPM image `%s'", img->spec, Qnil);
4450 UNGCPRO;
4451 return 0;
4454 success_p = xpm_load_image (f, img, contents, contents + size);
4455 xfree (contents);
4456 UNGCPRO;
4458 else
4460 Lisp_Object data;
4462 data = image_spec_value (img->spec, QCdata, NULL);
4463 success_p = xpm_load_image (f, img, SDATA (data),
4464 SDATA (data) + SBYTES (data));
4467 return success_p;
4470 #endif /* MAC_OS */
4474 /***********************************************************************
4475 Color table
4476 ***********************************************************************/
4478 #ifdef COLOR_TABLE_SUPPORT
4480 /* An entry in the color table mapping an RGB color to a pixel color. */
4482 struct ct_color
4484 int r, g, b;
4485 unsigned long pixel;
4487 /* Next in color table collision list. */
4488 struct ct_color *next;
4491 /* The bucket vector size to use. Must be prime. */
4493 #define CT_SIZE 101
4495 /* Value is a hash of the RGB color given by R, G, and B. */
4497 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
4499 /* The color hash table. */
4501 struct ct_color **ct_table;
4503 /* Number of entries in the color table. */
4505 int ct_colors_allocated;
4507 /* Initialize the color table. */
4509 static void
4510 init_color_table ()
4512 int size = CT_SIZE * sizeof (*ct_table);
4513 ct_table = (struct ct_color **) xmalloc (size);
4514 bzero (ct_table, size);
4515 ct_colors_allocated = 0;
4519 /* Free memory associated with the color table. */
4521 static void
4522 free_color_table ()
4524 int i;
4525 struct ct_color *p, *next;
4527 for (i = 0; i < CT_SIZE; ++i)
4528 for (p = ct_table[i]; p; p = next)
4530 next = p->next;
4531 xfree (p);
4534 xfree (ct_table);
4535 ct_table = NULL;
4539 /* Value is a pixel color for RGB color R, G, B on frame F. If an
4540 entry for that color already is in the color table, return the
4541 pixel color of that entry. Otherwise, allocate a new color for R,
4542 G, B, and make an entry in the color table. */
4544 static unsigned long
4545 lookup_rgb_color (f, r, g, b)
4546 struct frame *f;
4547 int r, g, b;
4549 unsigned hash = CT_HASH_RGB (r, g, b);
4550 int i = hash % CT_SIZE;
4551 struct ct_color *p;
4552 Display_Info *dpyinfo;
4554 /* Handle TrueColor visuals specially, which improves performance by
4555 two orders of magnitude. Freeing colors on TrueColor visuals is
4556 a nop, and pixel colors specify RGB values directly. See also
4557 the Xlib spec, chapter 3.1. */
4558 dpyinfo = FRAME_X_DISPLAY_INFO (f);
4559 if (dpyinfo->red_bits > 0)
4561 unsigned long pr, pg, pb;
4563 /* Apply gamma-correction like normal color allocation does. */
4564 if (f->gamma)
4566 XColor color;
4567 color.red = r, color.green = g, color.blue = b;
4568 gamma_correct (f, &color);
4569 r = color.red, g = color.green, b = color.blue;
4572 /* Scale down RGB values to the visual's bits per RGB, and shift
4573 them to the right position in the pixel color. Note that the
4574 original RGB values are 16-bit values, as usual in X. */
4575 pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset;
4576 pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset;
4577 pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset;
4579 /* Assemble the pixel color. */
4580 return pr | pg | pb;
4583 for (p = ct_table[i]; p; p = p->next)
4584 if (p->r == r && p->g == g && p->b == b)
4585 break;
4587 if (p == NULL)
4590 #ifdef HAVE_X_WINDOWS
4591 XColor color;
4592 Colormap cmap;
4593 int rc;
4595 color.red = r;
4596 color.green = g;
4597 color.blue = b;
4599 cmap = FRAME_X_COLORMAP (f);
4600 rc = x_alloc_nearest_color (f, cmap, &color);
4601 if (rc)
4603 ++ct_colors_allocated;
4604 p = (struct ct_color *) xmalloc (sizeof *p);
4605 p->r = r;
4606 p->g = g;
4607 p->b = b;
4608 p->pixel = color.pixel;
4609 p->next = ct_table[i];
4610 ct_table[i] = p;
4612 else
4613 return FRAME_FOREGROUND_PIXEL (f);
4615 #else
4616 COLORREF color;
4617 #ifdef HAVE_NTGUI
4618 color = PALETTERGB (r, g, b);
4619 #else
4620 color = RGB_TO_ULONG (r, g, b);
4621 #endif /* HAVE_NTGUI */
4622 ++ct_colors_allocated;
4623 p = (struct ct_color *) xmalloc (sizeof *p);
4624 p->r = r;
4625 p->g = g;
4626 p->b = b;
4627 p->pixel = color;
4628 p->next = ct_table[i];
4629 ct_table[i] = p;
4630 #endif /* HAVE_X_WINDOWS */
4634 return p->pixel;
4638 /* Look up pixel color PIXEL which is used on frame F in the color
4639 table. If not already present, allocate it. Value is PIXEL. */
4641 static unsigned long
4642 lookup_pixel_color (f, pixel)
4643 struct frame *f;
4644 unsigned long pixel;
4646 int i = pixel % CT_SIZE;
4647 struct ct_color *p;
4649 for (p = ct_table[i]; p; p = p->next)
4650 if (p->pixel == pixel)
4651 break;
4653 if (p == NULL)
4655 XColor color;
4656 Colormap cmap;
4657 int rc;
4659 #ifdef HAVE_X_WINDOWS
4660 cmap = FRAME_X_COLORMAP (f);
4661 color.pixel = pixel;
4662 x_query_color (f, &color);
4663 rc = x_alloc_nearest_color (f, cmap, &color);
4664 #else
4665 BLOCK_INPUT;
4666 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
4667 color.pixel = pixel;
4668 XQueryColor (NULL, cmap, &color);
4669 rc = x_alloc_nearest_color (f, cmap, &color);
4670 UNBLOCK_INPUT;
4671 #endif /* HAVE_X_WINDOWS */
4673 if (rc)
4675 ++ct_colors_allocated;
4677 p = (struct ct_color *) xmalloc (sizeof *p);
4678 p->r = color.red;
4679 p->g = color.green;
4680 p->b = color.blue;
4681 p->pixel = pixel;
4682 p->next = ct_table[i];
4683 ct_table[i] = p;
4685 else
4686 return FRAME_FOREGROUND_PIXEL (f);
4688 return p->pixel;
4692 /* Value is a vector of all pixel colors contained in the color table,
4693 allocated via xmalloc. Set *N to the number of colors. */
4695 static unsigned long *
4696 colors_in_color_table (n)
4697 int *n;
4699 int i, j;
4700 struct ct_color *p;
4701 unsigned long *colors;
4703 if (ct_colors_allocated == 0)
4705 *n = 0;
4706 colors = NULL;
4708 else
4710 colors = (unsigned long *) xmalloc (ct_colors_allocated
4711 * sizeof *colors);
4712 *n = ct_colors_allocated;
4714 for (i = j = 0; i < CT_SIZE; ++i)
4715 for (p = ct_table[i]; p; p = p->next)
4716 colors[j++] = p->pixel;
4719 return colors;
4722 #else /* COLOR_TABLE_SUPPORT */
4724 static unsigned long
4725 lookup_rgb_color (f, r, g, b)
4726 struct frame *f;
4727 int r, g, b;
4729 unsigned long pixel;
4731 #ifdef MAC_OS
4732 pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8);
4733 gamma_correct (f, &pixel);
4734 #endif /* MAC_OS */
4736 #ifdef HAVE_NTGUI
4737 pixel = PALETTERGB (r >> 8, g >> 8, b >> 8);
4738 #endif /* HAVE_NTGUI */
4740 return pixel;
4743 static void
4744 init_color_table ()
4747 #endif /* COLOR_TABLE_SUPPORT */
4750 /***********************************************************************
4751 Algorithms
4752 ***********************************************************************/
4754 static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
4755 static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
4756 static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
4758 #ifdef HAVE_NTGUI
4759 static void XPutPixel (XImagePtr , int, int, COLORREF);
4760 #endif /* HAVE_NTGUI */
4762 /* Non-zero means draw a cross on images having `:conversion
4763 disabled'. */
4765 int cross_disabled_images;
4767 /* Edge detection matrices for different edge-detection
4768 strategies. */
4770 static int emboss_matrix[9] = {
4771 /* x - 1 x x + 1 */
4772 2, -1, 0, /* y - 1 */
4773 -1, 0, 1, /* y */
4774 0, 1, -2 /* y + 1 */
4777 static int laplace_matrix[9] = {
4778 /* x - 1 x x + 1 */
4779 1, 0, 0, /* y - 1 */
4780 0, 0, 0, /* y */
4781 0, 0, -1 /* y + 1 */
4784 /* Value is the intensity of the color whose red/green/blue values
4785 are R, G, and B. */
4787 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
4790 /* On frame F, return an array of XColor structures describing image
4791 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
4792 non-zero means also fill the red/green/blue members of the XColor
4793 structures. Value is a pointer to the array of XColors structures,
4794 allocated with xmalloc; it must be freed by the caller. */
4796 static XColor *
4797 x_to_xcolors (f, img, rgb_p)
4798 struct frame *f;
4799 struct image *img;
4800 int rgb_p;
4802 int x, y;
4803 XColor *colors, *p;
4804 XImagePtr_or_DC ximg;
4805 #ifdef HAVE_NTGUI
4806 HDC hdc;
4807 HGDIOBJ prev;
4808 #endif /* HAVE_NTGUI */
4810 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
4812 #ifndef HAVE_NTGUI
4813 /* Get the X image IMG->pixmap. */
4814 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
4815 0, 0, img->width, img->height, ~0, ZPixmap);
4816 #else
4817 /* Load the image into a memory device context. */
4818 hdc = get_frame_dc (f);
4819 ximg = CreateCompatibleDC (hdc);
4820 release_frame_dc (f, hdc);
4821 prev = SelectObject (ximg, img->pixmap);
4822 #endif /* HAVE_NTGUI */
4824 /* Fill the `pixel' members of the XColor array. I wished there
4825 were an easy and portable way to circumvent XGetPixel. */
4826 p = colors;
4827 for (y = 0; y < img->height; ++y)
4829 XColor *row = p;
4831 #ifdef HAVE_X_WINDOWS
4832 for (x = 0; x < img->width; ++x, ++p)
4833 p->pixel = XGetPixel (ximg, x, y);
4834 if (rgb_p)
4835 x_query_colors (f, row, img->width);
4837 #else
4839 for (x = 0; x < img->width; ++x, ++p)
4841 /* W32_TODO: palette support needed here? */
4842 p->pixel = GET_PIXEL (ximg, x, y);
4843 if (rgb_p)
4845 #ifdef MAC_OS
4846 p->red = RED16_FROM_ULONG (p->pixel);
4847 p->green = GREEN16_FROM_ULONG (p->pixel);
4848 p->blue = BLUE16_FROM_ULONG (p->pixel);
4849 #endif /* MAC_OS */
4850 #ifdef HAVE_NTGUI
4851 p->red = 256 * GetRValue (p->pixel);
4852 p->green = 256 * GetGValue (p->pixel);
4853 p->blue = 256 * GetBValue (p->pixel);
4854 #endif /* HAVE_NTGUI */
4857 #endif /* HAVE_X_WINDOWS */
4860 Destroy_Image (ximg, prev);
4862 return colors;
4865 #ifdef HAVE_NTGUI
4867 /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
4868 created with CreateDIBSection, with the pointer to the bit values
4869 stored in ximg->data. */
4871 static void XPutPixel (ximg, x, y, color)
4872 XImagePtr ximg;
4873 int x, y;
4874 COLORREF color;
4876 int width = ximg->info.bmiHeader.biWidth;
4877 int height = ximg->info.bmiHeader.biHeight;
4878 unsigned char * pixel;
4880 /* True color images. */
4881 if (ximg->info.bmiHeader.biBitCount == 24)
4883 int rowbytes = width * 3;
4884 /* Ensure scanlines are aligned on 4 byte boundaries. */
4885 if (rowbytes % 4)
4886 rowbytes += 4 - (rowbytes % 4);
4888 pixel = ximg->data + y * rowbytes + x * 3;
4889 /* Windows bitmaps are in BGR order. */
4890 *pixel = GetBValue (color);
4891 *(pixel + 1) = GetGValue (color);
4892 *(pixel + 2) = GetRValue (color);
4894 /* Monochrome images. */
4895 else if (ximg->info.bmiHeader.biBitCount == 1)
4897 int rowbytes = width / 8;
4898 /* Ensure scanlines are aligned on 4 byte boundaries. */
4899 if (rowbytes % 4)
4900 rowbytes += 4 - (rowbytes % 4);
4901 pixel = ximg->data + y * rowbytes + x / 8;
4902 /* Filter out palette info. */
4903 if (color & 0x00ffffff)
4904 *pixel = *pixel | (1 << x % 8);
4905 else
4906 *pixel = *pixel & ~(1 << x % 8);
4908 else
4909 image_error ("XPutPixel: palette image not supported", Qnil, Qnil);
4912 #endif /* HAVE_NTGUI */
4914 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
4915 RGB members are set. F is the frame on which this all happens.
4916 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
4918 static void
4919 x_from_xcolors (f, img, colors)
4920 struct frame *f;
4921 struct image *img;
4922 XColor *colors;
4924 int x, y;
4925 XImagePtr oimg;
4926 Pixmap pixmap;
4927 XColor *p;
4929 init_color_table ();
4931 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
4932 &oimg, &pixmap);
4933 p = colors;
4934 for (y = 0; y < img->height; ++y)
4935 for (x = 0; x < img->width; ++x, ++p)
4937 unsigned long pixel;
4938 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
4939 XPutPixel (oimg, x, y, pixel);
4942 xfree (colors);
4943 x_clear_image_1 (f, img, 1, 0, 1);
4945 x_put_x_image (f, oimg, pixmap, img->width, img->height);
4946 x_destroy_x_image (oimg);
4947 img->pixmap = pixmap;
4948 #ifdef COLOR_TABLE_SUPPORT
4949 img->colors = colors_in_color_table (&img->ncolors);
4950 free_color_table ();
4951 #endif /* COLOR_TABLE_SUPPORT */
4955 /* On frame F, perform edge-detection on image IMG.
4957 MATRIX is a nine-element array specifying the transformation
4958 matrix. See emboss_matrix for an example.
4960 COLOR_ADJUST is a color adjustment added to each pixel of the
4961 outgoing image. */
4963 static void
4964 x_detect_edges (f, img, matrix, color_adjust)
4965 struct frame *f;
4966 struct image *img;
4967 int matrix[9], color_adjust;
4969 XColor *colors = x_to_xcolors (f, img, 1);
4970 XColor *new, *p;
4971 int x, y, i, sum;
4973 for (i = sum = 0; i < 9; ++i)
4974 sum += abs (matrix[i]);
4976 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4978 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
4980 for (y = 0; y < img->height; ++y)
4982 p = COLOR (new, 0, y);
4983 p->red = p->green = p->blue = 0xffff/2;
4984 p = COLOR (new, img->width - 1, y);
4985 p->red = p->green = p->blue = 0xffff/2;
4988 for (x = 1; x < img->width - 1; ++x)
4990 p = COLOR (new, x, 0);
4991 p->red = p->green = p->blue = 0xffff/2;
4992 p = COLOR (new, x, img->height - 1);
4993 p->red = p->green = p->blue = 0xffff/2;
4996 for (y = 1; y < img->height - 1; ++y)
4998 p = COLOR (new, 1, y);
5000 for (x = 1; x < img->width - 1; ++x, ++p)
5002 int r, g, b, y1, x1;
5004 r = g = b = i = 0;
5005 for (y1 = y - 1; y1 < y + 2; ++y1)
5006 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
5007 if (matrix[i])
5009 XColor *t = COLOR (colors, x1, y1);
5010 r += matrix[i] * t->red;
5011 g += matrix[i] * t->green;
5012 b += matrix[i] * t->blue;
5015 r = (r / sum + color_adjust) & 0xffff;
5016 g = (g / sum + color_adjust) & 0xffff;
5017 b = (b / sum + color_adjust) & 0xffff;
5018 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
5022 xfree (colors);
5023 x_from_xcolors (f, img, new);
5025 #undef COLOR
5029 /* Perform the pre-defined `emboss' edge-detection on image IMG
5030 on frame F. */
5032 static void
5033 x_emboss (f, img)
5034 struct frame *f;
5035 struct image *img;
5037 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
5041 /* Transform image IMG which is used on frame F with a Laplace
5042 edge-detection algorithm. The result is an image that can be used
5043 to draw disabled buttons, for example. */
5045 static void
5046 x_laplace (f, img)
5047 struct frame *f;
5048 struct image *img;
5050 x_detect_edges (f, img, laplace_matrix, 45000);
5054 /* Perform edge-detection on image IMG on frame F, with specified
5055 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
5057 MATRIX must be either
5059 - a list of at least 9 numbers in row-major form
5060 - a vector of at least 9 numbers
5062 COLOR_ADJUST nil means use a default; otherwise it must be a
5063 number. */
5065 static void
5066 x_edge_detection (f, img, matrix, color_adjust)
5067 struct frame *f;
5068 struct image *img;
5069 Lisp_Object matrix, color_adjust;
5071 int i = 0;
5072 int trans[9];
5074 if (CONSP (matrix))
5076 for (i = 0;
5077 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
5078 ++i, matrix = XCDR (matrix))
5079 trans[i] = XFLOATINT (XCAR (matrix));
5081 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
5083 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
5084 trans[i] = XFLOATINT (AREF (matrix, i));
5087 if (NILP (color_adjust))
5088 color_adjust = make_number (0xffff / 2);
5090 if (i == 9 && NUMBERP (color_adjust))
5091 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
5095 /* Transform image IMG on frame F so that it looks disabled. */
5097 static void
5098 x_disable_image (f, img)
5099 struct frame *f;
5100 struct image *img;
5102 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5103 #ifdef HAVE_NTGUI
5104 int n_planes = dpyinfo->n_planes * dpyinfo->n_cbits;
5105 #else
5106 int n_planes = dpyinfo->n_planes;
5107 #endif /* HAVE_NTGUI */
5109 if (n_planes >= 2)
5111 /* Color (or grayscale). Convert to gray, and equalize. Just
5112 drawing such images with a stipple can look very odd, so
5113 we're using this method instead. */
5114 XColor *colors = x_to_xcolors (f, img, 1);
5115 XColor *p, *end;
5116 const int h = 15000;
5117 const int l = 30000;
5119 for (p = colors, end = colors + img->width * img->height;
5120 p < end;
5121 ++p)
5123 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
5124 int i2 = (0xffff - h - l) * i / 0xffff + l;
5125 p->red = p->green = p->blue = i2;
5128 x_from_xcolors (f, img, colors);
5131 /* Draw a cross over the disabled image, if we must or if we
5132 should. */
5133 if (n_planes < 2 || cross_disabled_images)
5135 #ifndef HAVE_NTGUI
5136 Display *dpy = FRAME_X_DISPLAY (f);
5137 GC gc;
5139 #ifdef MAC_OS
5140 #define XCreateGC_pixmap(dpy, pixmap) XCreateGC (dpy, NULL, 0, NULL)
5141 #define MaskForeground(f) PIX_MASK_DRAW
5142 #else
5143 #define XCreateGC_pixmap(dpy, pixmap) XCreateGC (dpy, pixmap, 0, NULL)
5144 #define MaskForeground(f) WHITE_PIX_DEFAULT (f)
5145 #endif
5147 gc = XCreateGC_pixmap (dpy, img->pixmap);
5148 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
5149 XDrawLine (dpy, img->pixmap, gc, 0, 0,
5150 img->width - 1, img->height - 1);
5151 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
5152 img->width - 1, 0);
5153 XFreeGC (dpy, gc);
5155 if (img->mask)
5157 gc = XCreateGC_pixmap (dpy, img->mask);
5158 XSetForeground (dpy, gc, MaskForeground (f));
5159 XDrawLine (dpy, img->mask, gc, 0, 0,
5160 img->width - 1, img->height - 1);
5161 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
5162 img->width - 1, 0);
5163 XFreeGC (dpy, gc);
5165 #else
5166 HDC hdc, bmpdc;
5167 HGDIOBJ prev;
5169 hdc = get_frame_dc (f);
5170 bmpdc = CreateCompatibleDC (hdc);
5171 release_frame_dc (f, hdc);
5173 prev = SelectObject (bmpdc, img->pixmap);
5175 SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f));
5176 MoveToEx (bmpdc, 0, 0, NULL);
5177 LineTo (bmpdc, img->width - 1, img->height - 1);
5178 MoveToEx (bmpdc, 0, img->height - 1, NULL);
5179 LineTo (bmpdc, img->width - 1, 0);
5181 if (img->mask)
5183 SelectObject (bmpdc, img->mask);
5184 SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f));
5185 MoveToEx (bmpdc, 0, 0, NULL);
5186 LineTo (bmpdc, img->width - 1, img->height - 1);
5187 MoveToEx (bmpdc, 0, img->height - 1, NULL);
5188 LineTo (bmpdc, img->width - 1, 0);
5190 SelectObject (bmpdc, prev);
5191 DeleteDC (bmpdc);
5192 #endif /* HAVE_NTGUI */
5197 /* Build a mask for image IMG which is used on frame F. FILE is the
5198 name of an image file, for error messages. HOW determines how to
5199 determine the background color of IMG. If it is a list '(R G B)',
5200 with R, G, and B being integers >= 0, take that as the color of the
5201 background. Otherwise, determine the background color of IMG
5202 heuristically. Value is non-zero if successful. */
5204 static int
5205 x_build_heuristic_mask (f, img, how)
5206 struct frame *f;
5207 struct image *img;
5208 Lisp_Object how;
5210 XImagePtr_or_DC ximg;
5211 #ifndef HAVE_NTGUI
5212 XImagePtr mask_img;
5213 #else
5214 HDC frame_dc;
5215 HGDIOBJ prev;
5216 char *mask_img;
5217 int row_width;
5218 #endif /* HAVE_NTGUI */
5219 int x, y, rc, use_img_background;
5220 unsigned long bg = 0;
5222 if (img->mask)
5224 Free_Pixmap (FRAME_X_DISPLAY (f), img->mask);
5225 img->mask = NO_PIXMAP;
5226 img->background_transparent_valid = 0;
5229 #ifndef HAVE_NTGUI
5230 /* Create an image and pixmap serving as mask. */
5231 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
5232 &mask_img, &img->mask);
5233 if (!rc)
5234 return 0;
5236 /* Get the X image of IMG->pixmap. */
5237 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, 0, 0,
5238 img->width, img->height,
5239 ~0, ZPixmap);
5240 #else
5241 /* Create the bit array serving as mask. */
5242 row_width = (img->width + 7) / 8;
5243 mask_img = xmalloc (row_width * img->height);
5244 bzero (mask_img, row_width * img->height);
5246 /* Create a memory device context for IMG->pixmap. */
5247 frame_dc = get_frame_dc (f);
5248 ximg = CreateCompatibleDC (frame_dc);
5249 release_frame_dc (f, frame_dc);
5250 prev = SelectObject (ximg, img->pixmap);
5251 #endif /* HAVE_NTGUI */
5253 /* Determine the background color of ximg. If HOW is `(R G B)'
5254 take that as color. Otherwise, use the image's background color. */
5255 use_img_background = 1;
5257 if (CONSP (how))
5259 int rgb[3], i;
5261 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
5263 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
5264 how = XCDR (how);
5267 if (i == 3 && NILP (how))
5269 char color_name[30];
5270 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
5271 bg = (
5272 #ifdef HAVE_NTGUI
5273 0x00ffffff & /* Filter out palette info. */
5274 #endif /* HAVE_NTGUI */
5275 x_alloc_image_color (f, img, build_string (color_name), 0));
5276 use_img_background = 0;
5280 if (use_img_background)
5281 bg = four_corners_best (ximg, img->width, img->height);
5283 /* Set all bits in mask_img to 1 whose color in ximg is different
5284 from the background color bg. */
5285 #ifndef HAVE_NTGUI
5286 for (y = 0; y < img->height; ++y)
5287 for (x = 0; x < img->width; ++x)
5288 XPutPixel (mask_img, x, y, (XGetPixel (ximg, x, y) != bg
5289 ? PIX_MASK_DRAW : PIX_MASK_RETAIN));
5291 /* Fill in the background_transparent field while we have the mask handy. */
5292 image_background_transparent (img, f, mask_img);
5294 /* Put mask_img into img->mask. */
5295 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
5296 x_destroy_x_image (mask_img);
5298 #else
5299 for (y = 0; y < img->height; ++y)
5300 for (x = 0; x < img->width; ++x)
5302 COLORREF p = GetPixel (ximg, x, y);
5303 if (p != bg)
5304 mask_img[y * row_width + x / 8] |= 1 << (x % 8);
5307 /* Create the mask image. */
5308 img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height,
5309 mask_img);
5310 /* Fill in the background_transparent field while we have the mask handy. */
5311 SelectObject (ximg, img->mask);
5312 image_background_transparent (img, f, ximg);
5314 /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */
5315 xfree (mask_img);
5316 #endif /* HAVE_NTGUI */
5318 Destroy_Image (ximg, prev);
5320 return 1;
5324 /***********************************************************************
5325 PBM (mono, gray, color)
5326 ***********************************************************************/
5328 static int pbm_image_p P_ ((Lisp_Object object));
5329 static int pbm_load P_ ((struct frame *f, struct image *img));
5330 static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
5332 /* The symbol `pbm' identifying images of this type. */
5334 Lisp_Object Qpbm;
5336 /* Indices of image specification fields in gs_format, below. */
5338 enum pbm_keyword_index
5340 PBM_TYPE,
5341 PBM_FILE,
5342 PBM_DATA,
5343 PBM_ASCENT,
5344 PBM_MARGIN,
5345 PBM_RELIEF,
5346 PBM_ALGORITHM,
5347 PBM_HEURISTIC_MASK,
5348 PBM_MASK,
5349 PBM_FOREGROUND,
5350 PBM_BACKGROUND,
5351 PBM_LAST
5354 /* Vector of image_keyword structures describing the format
5355 of valid user-defined image specifications. */
5357 static struct image_keyword pbm_format[PBM_LAST] =
5359 {":type", IMAGE_SYMBOL_VALUE, 1},
5360 {":file", IMAGE_STRING_VALUE, 0},
5361 {":data", IMAGE_STRING_VALUE, 0},
5362 {":ascent", IMAGE_ASCENT_VALUE, 0},
5363 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
5364 {":relief", IMAGE_INTEGER_VALUE, 0},
5365 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5366 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5367 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5368 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
5369 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
5372 /* Structure describing the image type `pbm'. */
5374 static struct image_type pbm_type =
5376 &Qpbm,
5377 pbm_image_p,
5378 pbm_load,
5379 x_clear_image,
5380 NULL
5384 /* Return non-zero if OBJECT is a valid PBM image specification. */
5386 static int
5387 pbm_image_p (object)
5388 Lisp_Object object;
5390 struct image_keyword fmt[PBM_LAST];
5392 bcopy (pbm_format, fmt, sizeof fmt);
5394 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
5395 return 0;
5397 /* Must specify either :data or :file. */
5398 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
5402 /* Scan a decimal number from *S and return it. Advance *S while
5403 reading the number. END is the end of the string. Value is -1 at
5404 end of input. */
5406 static int
5407 pbm_scan_number (s, end)
5408 unsigned char **s, *end;
5410 int c = 0, val = -1;
5412 while (*s < end)
5414 /* Skip white-space. */
5415 while (*s < end && (c = *(*s)++, isspace (c)))
5418 if (c == '#')
5420 /* Skip comment to end of line. */
5421 while (*s < end && (c = *(*s)++, c != '\n'))
5424 else if (isdigit (c))
5426 /* Read decimal number. */
5427 val = c - '0';
5428 while (*s < end && (c = *(*s)++, isdigit (c)))
5429 val = 10 * val + c - '0';
5430 break;
5432 else
5433 break;
5436 return val;
5440 #ifdef HAVE_NTGUI
5441 #if 0 /* Unused. ++kfs */
5443 /* Read FILE into memory. Value is a pointer to a buffer allocated
5444 with xmalloc holding FILE's contents. Value is null if an error
5445 occurred. *SIZE is set to the size of the file. */
5447 static char *
5448 pbm_read_file (file, size)
5449 Lisp_Object file;
5450 int *size;
5452 FILE *fp = NULL;
5453 char *buf = NULL;
5454 struct stat st;
5456 if (stat (SDATA (file), &st) == 0
5457 && (fp = fopen (SDATA (file), "rb")) != NULL
5458 && (buf = (char *) xmalloc (st.st_size),
5459 fread (buf, 1, st.st_size, fp) == st.st_size))
5461 *size = st.st_size;
5462 fclose (fp);
5464 else
5466 if (fp)
5467 fclose (fp);
5468 if (buf)
5470 xfree (buf);
5471 buf = NULL;
5475 return buf;
5477 #endif
5478 #endif /* HAVE_NTGUI */
5480 /* Load PBM image IMG for use on frame F. */
5482 static int
5483 pbm_load (f, img)
5484 struct frame *f;
5485 struct image *img;
5487 int raw_p, x, y;
5488 int width, height, max_color_idx = 0;
5489 XImagePtr ximg;
5490 Lisp_Object file, specified_file;
5491 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
5492 struct gcpro gcpro1;
5493 unsigned char *contents = NULL;
5494 unsigned char *end, *p;
5495 int size;
5497 specified_file = image_spec_value (img->spec, QCfile, NULL);
5498 file = Qnil;
5499 GCPRO1 (file);
5501 if (STRINGP (specified_file))
5503 file = x_find_image_file (specified_file);
5504 if (!STRINGP (file))
5506 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5507 UNGCPRO;
5508 return 0;
5511 contents = slurp_file (SDATA (file), &size);
5512 if (contents == NULL)
5514 image_error ("Error reading `%s'", file, Qnil);
5515 UNGCPRO;
5516 return 0;
5519 p = contents;
5520 end = contents + size;
5522 else
5524 Lisp_Object data;
5525 data = image_spec_value (img->spec, QCdata, NULL);
5526 p = SDATA (data);
5527 end = p + SBYTES (data);
5530 /* Check magic number. */
5531 if (end - p < 2 || *p++ != 'P')
5533 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
5534 error:
5535 xfree (contents);
5536 UNGCPRO;
5537 return 0;
5540 switch (*p++)
5542 case '1':
5543 raw_p = 0, type = PBM_MONO;
5544 break;
5546 case '2':
5547 raw_p = 0, type = PBM_GRAY;
5548 break;
5550 case '3':
5551 raw_p = 0, type = PBM_COLOR;
5552 break;
5554 case '4':
5555 raw_p = 1, type = PBM_MONO;
5556 break;
5558 case '5':
5559 raw_p = 1, type = PBM_GRAY;
5560 break;
5562 case '6':
5563 raw_p = 1, type = PBM_COLOR;
5564 break;
5566 default:
5567 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
5568 goto error;
5571 /* Read width, height, maximum color-component. Characters
5572 starting with `#' up to the end of a line are ignored. */
5573 width = pbm_scan_number (&p, end);
5574 height = pbm_scan_number (&p, end);
5576 if (type != PBM_MONO)
5578 max_color_idx = pbm_scan_number (&p, end);
5579 if (raw_p && max_color_idx > 255)
5580 max_color_idx = 255;
5583 if (!check_image_size (f, width, height)
5584 || (type != PBM_MONO && max_color_idx < 0))
5585 goto error;
5587 if (!x_create_x_image_and_pixmap (f, width, height, 0,
5588 &ximg, &img->pixmap))
5589 goto error;
5591 /* Initialize the color hash table. */
5592 init_color_table ();
5594 if (type == PBM_MONO)
5596 int c = 0, g;
5597 struct image_keyword fmt[PBM_LAST];
5598 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
5599 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
5601 /* Parse the image specification. */
5602 bcopy (pbm_format, fmt, sizeof fmt);
5603 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
5605 /* Get foreground and background colors, maybe allocate colors. */
5606 if (fmt[PBM_FOREGROUND].count
5607 && STRINGP (fmt[PBM_FOREGROUND].value))
5608 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
5609 if (fmt[PBM_BACKGROUND].count
5610 && STRINGP (fmt[PBM_BACKGROUND].value))
5612 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
5613 img->background = bg;
5614 img->background_valid = 1;
5617 for (y = 0; y < height; ++y)
5618 for (x = 0; x < width; ++x)
5620 if (raw_p)
5622 if ((x & 7) == 0)
5623 c = *p++;
5624 g = c & 0x80;
5625 c <<= 1;
5627 else
5628 g = pbm_scan_number (&p, end);
5630 XPutPixel (ximg, x, y, g ? fg : bg);
5633 else
5635 for (y = 0; y < height; ++y)
5636 for (x = 0; x < width; ++x)
5638 int r, g, b;
5640 if (type == PBM_GRAY)
5641 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
5642 else if (raw_p)
5644 r = *p++;
5645 g = *p++;
5646 b = *p++;
5648 else
5650 r = pbm_scan_number (&p, end);
5651 g = pbm_scan_number (&p, end);
5652 b = pbm_scan_number (&p, end);
5655 if (r < 0 || g < 0 || b < 0)
5657 x_destroy_x_image (ximg);
5658 image_error ("Invalid pixel value in image `%s'",
5659 img->spec, Qnil);
5660 goto error;
5663 /* RGB values are now in the range 0..max_color_idx.
5664 Scale this to the range 0..0xffff supported by X. */
5665 r = (double) r * 65535 / max_color_idx;
5666 g = (double) g * 65535 / max_color_idx;
5667 b = (double) b * 65535 / max_color_idx;
5668 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
5672 #ifdef COLOR_TABLE_SUPPORT
5673 /* Store in IMG->colors the colors allocated for the image, and
5674 free the color table. */
5675 img->colors = colors_in_color_table (&img->ncolors);
5676 free_color_table ();
5677 #endif /* COLOR_TABLE_SUPPORT */
5679 img->width = width;
5680 img->height = height;
5682 /* Maybe fill in the background field while we have ximg handy. */
5684 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
5685 /* Casting avoids a GCC warning. */
5686 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
5688 /* Put the image into a pixmap. */
5689 x_put_x_image (f, ximg, img->pixmap, width, height);
5690 x_destroy_x_image (ximg);
5692 /* X and W32 versions did it here, MAC version above. ++kfs
5693 img->width = width;
5694 img->height = height; */
5696 UNGCPRO;
5697 xfree (contents);
5698 return 1;
5702 /***********************************************************************
5704 ***********************************************************************/
5706 #if defined (HAVE_PNG) || defined (MAC_OS)
5708 /* Function prototypes. */
5710 static int png_image_p P_ ((Lisp_Object object));
5711 static int png_load P_ ((struct frame *f, struct image *img));
5713 /* The symbol `png' identifying images of this type. */
5715 Lisp_Object Qpng;
5717 /* Indices of image specification fields in png_format, below. */
5719 enum png_keyword_index
5721 PNG_TYPE,
5722 PNG_DATA,
5723 PNG_FILE,
5724 PNG_ASCENT,
5725 PNG_MARGIN,
5726 PNG_RELIEF,
5727 PNG_ALGORITHM,
5728 PNG_HEURISTIC_MASK,
5729 PNG_MASK,
5730 PNG_BACKGROUND,
5731 PNG_LAST
5734 /* Vector of image_keyword structures describing the format
5735 of valid user-defined image specifications. */
5737 static struct image_keyword png_format[PNG_LAST] =
5739 {":type", IMAGE_SYMBOL_VALUE, 1},
5740 {":data", IMAGE_STRING_VALUE, 0},
5741 {":file", IMAGE_STRING_VALUE, 0},
5742 {":ascent", IMAGE_ASCENT_VALUE, 0},
5743 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
5744 {":relief", IMAGE_INTEGER_VALUE, 0},
5745 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5746 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5747 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5748 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
5751 /* Structure describing the image type `png'. */
5753 static struct image_type png_type =
5755 &Qpng,
5756 png_image_p,
5757 png_load,
5758 x_clear_image,
5759 NULL
5762 /* Return non-zero if OBJECT is a valid PNG image specification. */
5764 static int
5765 png_image_p (object)
5766 Lisp_Object object;
5768 struct image_keyword fmt[PNG_LAST];
5769 bcopy (png_format, fmt, sizeof fmt);
5771 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
5772 return 0;
5774 /* Must specify either the :data or :file keyword. */
5775 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
5778 #endif /* HAVE_PNG || MAC_OS */
5781 #ifdef HAVE_PNG
5783 #if defined HAVE_LIBPNG_PNG_H
5784 # include <libpng/png.h>
5785 #else
5786 # include <png.h>
5787 #endif
5789 #ifdef HAVE_NTGUI
5790 /* PNG library details. */
5792 DEF_IMGLIB_FN (png_get_io_ptr);
5793 DEF_IMGLIB_FN (png_check_sig);
5794 DEF_IMGLIB_FN (png_create_read_struct);
5795 DEF_IMGLIB_FN (png_create_info_struct);
5796 DEF_IMGLIB_FN (png_destroy_read_struct);
5797 DEF_IMGLIB_FN (png_set_read_fn);
5798 DEF_IMGLIB_FN (png_set_sig_bytes);
5799 DEF_IMGLIB_FN (png_read_info);
5800 DEF_IMGLIB_FN (png_get_IHDR);
5801 DEF_IMGLIB_FN (png_get_valid);
5802 DEF_IMGLIB_FN (png_set_strip_16);
5803 DEF_IMGLIB_FN (png_set_expand);
5804 DEF_IMGLIB_FN (png_set_gray_to_rgb);
5805 DEF_IMGLIB_FN (png_set_background);
5806 DEF_IMGLIB_FN (png_get_bKGD);
5807 DEF_IMGLIB_FN (png_read_update_info);
5808 DEF_IMGLIB_FN (png_get_channels);
5809 DEF_IMGLIB_FN (png_get_rowbytes);
5810 DEF_IMGLIB_FN (png_read_image);
5811 DEF_IMGLIB_FN (png_read_end);
5812 DEF_IMGLIB_FN (png_error);
5814 static int
5815 init_png_functions (Lisp_Object libraries)
5817 HMODULE library;
5819 /* Try loading libpng under probable names. */
5820 if (!(library = w32_delayed_load (libraries, Qpng)))
5821 return 0;
5823 LOAD_IMGLIB_FN (library, png_get_io_ptr);
5824 LOAD_IMGLIB_FN (library, png_check_sig);
5825 LOAD_IMGLIB_FN (library, png_create_read_struct);
5826 LOAD_IMGLIB_FN (library, png_create_info_struct);
5827 LOAD_IMGLIB_FN (library, png_destroy_read_struct);
5828 LOAD_IMGLIB_FN (library, png_set_read_fn);
5829 LOAD_IMGLIB_FN (library, png_set_sig_bytes);
5830 LOAD_IMGLIB_FN (library, png_read_info);
5831 LOAD_IMGLIB_FN (library, png_get_IHDR);
5832 LOAD_IMGLIB_FN (library, png_get_valid);
5833 LOAD_IMGLIB_FN (library, png_set_strip_16);
5834 LOAD_IMGLIB_FN (library, png_set_expand);
5835 LOAD_IMGLIB_FN (library, png_set_gray_to_rgb);
5836 LOAD_IMGLIB_FN (library, png_set_background);
5837 LOAD_IMGLIB_FN (library, png_get_bKGD);
5838 LOAD_IMGLIB_FN (library, png_read_update_info);
5839 LOAD_IMGLIB_FN (library, png_get_channels);
5840 LOAD_IMGLIB_FN (library, png_get_rowbytes);
5841 LOAD_IMGLIB_FN (library, png_read_image);
5842 LOAD_IMGLIB_FN (library, png_read_end);
5843 LOAD_IMGLIB_FN (library, png_error);
5844 return 1;
5846 #else
5848 #define fn_png_get_io_ptr png_get_io_ptr
5849 #define fn_png_check_sig png_check_sig
5850 #define fn_png_create_read_struct png_create_read_struct
5851 #define fn_png_create_info_struct png_create_info_struct
5852 #define fn_png_destroy_read_struct png_destroy_read_struct
5853 #define fn_png_set_read_fn png_set_read_fn
5854 #define fn_png_set_sig_bytes png_set_sig_bytes
5855 #define fn_png_read_info png_read_info
5856 #define fn_png_get_IHDR png_get_IHDR
5857 #define fn_png_get_valid png_get_valid
5858 #define fn_png_set_strip_16 png_set_strip_16
5859 #define fn_png_set_expand png_set_expand
5860 #define fn_png_set_gray_to_rgb png_set_gray_to_rgb
5861 #define fn_png_set_background png_set_background
5862 #define fn_png_get_bKGD png_get_bKGD
5863 #define fn_png_read_update_info png_read_update_info
5864 #define fn_png_get_channels png_get_channels
5865 #define fn_png_get_rowbytes png_get_rowbytes
5866 #define fn_png_read_image png_read_image
5867 #define fn_png_read_end png_read_end
5868 #define fn_png_error png_error
5870 #endif /* HAVE_NTGUI */
5872 /* Error and warning handlers installed when the PNG library
5873 is initialized. */
5875 static void
5876 my_png_error (png_ptr, msg)
5877 png_struct *png_ptr;
5878 char *msg;
5880 xassert (png_ptr != NULL);
5881 image_error ("PNG error: %s", build_string (msg), Qnil);
5882 longjmp (png_ptr->jmpbuf, 1);
5886 static void
5887 my_png_warning (png_ptr, msg)
5888 png_struct *png_ptr;
5889 char *msg;
5891 xassert (png_ptr != NULL);
5892 image_error ("PNG warning: %s", build_string (msg), Qnil);
5895 /* Memory source for PNG decoding. */
5897 struct png_memory_storage
5899 unsigned char *bytes; /* The data */
5900 size_t len; /* How big is it? */
5901 int index; /* Where are we? */
5905 /* Function set as reader function when reading PNG image from memory.
5906 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5907 bytes from the input to DATA. */
5909 static void
5910 png_read_from_memory (png_ptr, data, length)
5911 png_structp png_ptr;
5912 png_bytep data;
5913 png_size_t length;
5915 struct png_memory_storage *tbr
5916 = (struct png_memory_storage *) fn_png_get_io_ptr (png_ptr);
5918 if (length > tbr->len - tbr->index)
5919 fn_png_error (png_ptr, "Read error");
5921 bcopy (tbr->bytes + tbr->index, data, length);
5922 tbr->index = tbr->index + length;
5926 /* Function set as reader function when reading PNG image from a file.
5927 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
5928 bytes from the input to DATA. */
5930 static void
5931 png_read_from_file (png_ptr, data, length)
5932 png_structp png_ptr;
5933 png_bytep data;
5934 png_size_t length;
5936 FILE *fp = (FILE *) fn_png_get_io_ptr (png_ptr);
5938 if (fread (data, 1, length, fp) < length)
5939 fn_png_error (png_ptr, "Read error");
5943 /* Load PNG image IMG for use on frame F. Value is non-zero if
5944 successful. */
5946 static int
5947 png_load (f, img)
5948 struct frame *f;
5949 struct image *img;
5951 Lisp_Object file, specified_file;
5952 Lisp_Object specified_data;
5953 int x, y, i;
5954 XImagePtr ximg, mask_img = NULL;
5955 struct gcpro gcpro1;
5956 png_struct *png_ptr = NULL;
5957 png_info *info_ptr = NULL, *end_info = NULL;
5958 FILE *volatile fp = NULL;
5959 png_byte sig[8];
5960 png_byte * volatile pixels = NULL;
5961 png_byte ** volatile rows = NULL;
5962 png_uint_32 width, height;
5963 int bit_depth, color_type, interlace_type;
5964 png_byte channels;
5965 png_uint_32 row_bytes;
5966 int transparent_p;
5967 double screen_gamma;
5968 struct png_memory_storage tbr; /* Data to be read */
5970 /* Find out what file to load. */
5971 specified_file = image_spec_value (img->spec, QCfile, NULL);
5972 specified_data = image_spec_value (img->spec, QCdata, NULL);
5973 file = Qnil;
5974 GCPRO1 (file);
5976 if (NILP (specified_data))
5978 file = x_find_image_file (specified_file);
5979 if (!STRINGP (file))
5981 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5982 UNGCPRO;
5983 return 0;
5986 /* Open the image file. */
5987 fp = fopen (SDATA (file), "rb");
5988 if (!fp)
5990 image_error ("Cannot open image file `%s'", file, Qnil);
5991 UNGCPRO;
5992 fclose (fp);
5993 return 0;
5996 /* Check PNG signature. */
5997 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
5998 || !fn_png_check_sig (sig, sizeof sig))
6000 image_error ("Not a PNG file: `%s'", file, Qnil);
6001 UNGCPRO;
6002 fclose (fp);
6003 return 0;
6006 else
6008 /* Read from memory. */
6009 tbr.bytes = SDATA (specified_data);
6010 tbr.len = SBYTES (specified_data);
6011 tbr.index = 0;
6013 /* Check PNG signature. */
6014 if (tbr.len < sizeof sig
6015 || !fn_png_check_sig (tbr.bytes, sizeof sig))
6017 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
6018 UNGCPRO;
6019 return 0;
6022 /* Need to skip past the signature. */
6023 tbr.bytes += sizeof (sig);
6026 /* Initialize read and info structs for PNG lib. Casting return
6027 value avoids a GCC warning on W32. */
6028 png_ptr = (png_structp)fn_png_create_read_struct (PNG_LIBPNG_VER_STRING,
6029 NULL, my_png_error,
6030 my_png_warning);
6031 if (!png_ptr)
6033 if (fp) fclose (fp);
6034 UNGCPRO;
6035 return 0;
6038 /* Casting return value avoids a GCC warning on W32. */
6039 info_ptr = (png_infop)fn_png_create_info_struct (png_ptr);
6040 if (!info_ptr)
6042 fn_png_destroy_read_struct (&png_ptr, NULL, NULL);
6043 if (fp) fclose (fp);
6044 UNGCPRO;
6045 return 0;
6048 /* Casting return value avoids a GCC warning on W32. */
6049 end_info = (png_infop)fn_png_create_info_struct (png_ptr);
6050 if (!end_info)
6052 fn_png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
6053 if (fp) fclose (fp);
6054 UNGCPRO;
6055 return 0;
6058 /* Set error jump-back. We come back here when the PNG library
6059 detects an error. */
6060 if (setjmp (png_ptr->jmpbuf))
6062 error:
6063 if (png_ptr)
6064 fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
6065 xfree (pixels);
6066 xfree (rows);
6067 if (fp) fclose (fp);
6068 UNGCPRO;
6069 return 0;
6072 /* Read image info. */
6073 if (!NILP (specified_data))
6074 fn_png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
6075 else
6076 fn_png_set_read_fn (png_ptr, (void *) fp, png_read_from_file);
6078 fn_png_set_sig_bytes (png_ptr, sizeof sig);
6079 fn_png_read_info (png_ptr, info_ptr);
6080 fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
6081 &interlace_type, NULL, NULL);
6083 if (!check_image_size (f, width, height))
6084 goto error;
6086 /* If image contains simply transparency data, we prefer to
6087 construct a clipping mask. */
6088 if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
6089 transparent_p = 1;
6090 else
6091 transparent_p = 0;
6093 /* This function is easier to write if we only have to handle
6094 one data format: RGB or RGBA with 8 bits per channel. Let's
6095 transform other formats into that format. */
6097 /* Strip more than 8 bits per channel. */
6098 if (bit_depth == 16)
6099 fn_png_set_strip_16 (png_ptr);
6101 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
6102 if available. */
6103 fn_png_set_expand (png_ptr);
6105 /* Convert grayscale images to RGB. */
6106 if (color_type == PNG_COLOR_TYPE_GRAY
6107 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
6108 fn_png_set_gray_to_rgb (png_ptr);
6110 screen_gamma = (f->gamma ? 1 / f->gamma / 0.45455 : 2.2);
6112 #if 0 /* Avoid double gamma correction for PNG images. */
6113 { /* Tell the PNG lib to handle gamma correction for us. */
6114 int intent;
6115 double image_gamma;
6116 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
6117 if (png_get_sRGB (png_ptr, info_ptr, &intent))
6118 /* The libpng documentation says this is right in this case. */
6119 png_set_gamma (png_ptr, screen_gamma, 0.45455);
6120 else
6121 #endif
6122 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
6123 /* Image contains gamma information. */
6124 png_set_gamma (png_ptr, screen_gamma, image_gamma);
6125 else
6126 /* Use the standard default for the image gamma. */
6127 png_set_gamma (png_ptr, screen_gamma, 0.45455);
6129 #endif /* if 0 */
6131 /* Handle alpha channel by combining the image with a background
6132 color. Do this only if a real alpha channel is supplied. For
6133 simple transparency, we prefer a clipping mask. */
6134 if (!transparent_p)
6136 png_color_16 *image_bg;
6137 Lisp_Object specified_bg
6138 = image_spec_value (img->spec, QCbackground, NULL);
6140 if (STRINGP (specified_bg))
6141 /* The user specified `:background', use that. */
6143 /* W32 version incorrectly used COLORREF here!! ++kfs */
6144 XColor color;
6145 if (x_defined_color (f, SDATA (specified_bg), &color, 0))
6147 png_color_16 user_bg;
6149 bzero (&user_bg, sizeof user_bg);
6150 user_bg.red = color.red >> 8;
6151 user_bg.green = color.green >> 8;
6152 user_bg.blue = color.blue >> 8;
6154 fn_png_set_background (png_ptr, &user_bg,
6155 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
6158 else if (fn_png_get_bKGD (png_ptr, info_ptr, &image_bg))
6159 /* Image contains a background color with which to
6160 combine the image. */
6161 fn_png_set_background (png_ptr, image_bg,
6162 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
6163 else
6165 /* Image does not contain a background color with which
6166 to combine the image data via an alpha channel. Use
6167 the frame's background instead. */
6168 #ifdef HAVE_X_WINDOWS
6169 XColor color;
6170 png_color_16 frame_background;
6172 color.pixel = FRAME_BACKGROUND_PIXEL (f);
6173 x_query_color (f, &color);
6175 bzero (&frame_background, sizeof frame_background);
6176 frame_background.red = color.red >> 8;
6177 frame_background.green = color.green >> 8;
6178 frame_background.blue = color.blue >> 8;
6179 #endif /* HAVE_X_WINDOWS */
6181 #ifdef HAVE_NTGUI
6182 COLORREF color;
6183 png_color_16 frame_background;
6184 color = FRAME_BACKGROUND_PIXEL (f);
6185 #if 0 /* W32 TODO : Colormap support. */
6186 x_query_color (f, &color);
6187 #endif
6188 bzero (&frame_background, sizeof frame_background);
6189 frame_background.red = GetRValue (color);
6190 frame_background.green = GetGValue (color);
6191 frame_background.blue = GetBValue (color);
6192 #endif /* HAVE_NTGUI */
6194 #ifdef MAC_OS
6195 unsigned long color;
6196 png_color_16 frame_background;
6197 color = FRAME_BACKGROUND_PIXEL (f);
6198 #if 0 /* MAC/W32 TODO : Colormap support. */
6199 x_query_color (f, &color);
6200 #endif
6201 bzero (&frame_background, sizeof frame_background);
6202 frame_background.red = RED_FROM_ULONG (color);
6203 frame_background.green = GREEN_FROM_ULONG (color);
6204 frame_background.blue = BLUE_FROM_ULONG (color);
6205 #endif /* MAC_OS */
6207 fn_png_set_background (png_ptr, &frame_background,
6208 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
6212 /* Update info structure. */
6213 fn_png_read_update_info (png_ptr, info_ptr);
6215 /* Get number of channels. Valid values are 1 for grayscale images
6216 and images with a palette, 2 for grayscale images with transparency
6217 information (alpha channel), 3 for RGB images, and 4 for RGB
6218 images with alpha channel, i.e. RGBA. If conversions above were
6219 sufficient we should only have 3 or 4 channels here. */
6220 channels = fn_png_get_channels (png_ptr, info_ptr);
6221 xassert (channels == 3 || channels == 4);
6223 /* Number of bytes needed for one row of the image. */
6224 row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr);
6226 /* Allocate memory for the image. */
6227 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
6228 rows = (png_byte **) xmalloc (height * sizeof *rows);
6229 for (i = 0; i < height; ++i)
6230 rows[i] = pixels + i * row_bytes;
6232 /* Read the entire image. */
6233 fn_png_read_image (png_ptr, rows);
6234 fn_png_read_end (png_ptr, info_ptr);
6235 if (fp)
6237 fclose (fp);
6238 fp = NULL;
6241 /* Create the X image and pixmap. */
6242 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
6243 &img->pixmap))
6244 goto error;
6246 /* Create an image and pixmap serving as mask if the PNG image
6247 contains an alpha channel. */
6248 if (channels == 4
6249 && !transparent_p
6250 && !x_create_x_image_and_pixmap (f, width, height, 1,
6251 &mask_img, &img->mask))
6253 x_destroy_x_image (ximg);
6254 Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap);
6255 img->pixmap = NO_PIXMAP;
6256 goto error;
6259 /* Fill the X image and mask from PNG data. */
6260 init_color_table ();
6262 for (y = 0; y < height; ++y)
6264 png_byte *p = rows[y];
6266 for (x = 0; x < width; ++x)
6268 unsigned r, g, b;
6270 r = *p++ << 8;
6271 g = *p++ << 8;
6272 b = *p++ << 8;
6273 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
6274 /* An alpha channel, aka mask channel, associates variable
6275 transparency with an image. Where other image formats
6276 support binary transparency---fully transparent or fully
6277 opaque---PNG allows up to 254 levels of partial transparency.
6278 The PNG library implements partial transparency by combining
6279 the image with a specified background color.
6281 I'm not sure how to handle this here nicely: because the
6282 background on which the image is displayed may change, for
6283 real alpha channel support, it would be necessary to create
6284 a new image for each possible background.
6286 What I'm doing now is that a mask is created if we have
6287 boolean transparency information. Otherwise I'm using
6288 the frame's background color to combine the image with. */
6290 if (channels == 4)
6292 if (mask_img)
6293 XPutPixel (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN);
6294 ++p;
6299 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
6300 /* Set IMG's background color from the PNG image, unless the user
6301 overrode it. */
6303 png_color_16 *bg;
6304 if (fn_png_get_bKGD (png_ptr, info_ptr, &bg))
6306 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
6307 img->background_valid = 1;
6311 #ifdef COLOR_TABLE_SUPPORT
6312 /* Remember colors allocated for this image. */
6313 img->colors = colors_in_color_table (&img->ncolors);
6314 free_color_table ();
6315 #endif /* COLOR_TABLE_SUPPORT */
6317 /* Clean up. */
6318 fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
6319 xfree (rows);
6320 xfree (pixels);
6322 img->width = width;
6323 img->height = height;
6325 /* Maybe fill in the background field while we have ximg handy.
6326 Casting avoids a GCC warning. */
6327 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
6329 /* Put the image into the pixmap, then free the X image and its buffer. */
6330 x_put_x_image (f, ximg, img->pixmap, width, height);
6331 x_destroy_x_image (ximg);
6333 /* Same for the mask. */
6334 if (mask_img)
6336 /* Fill in the background_transparent field while we have the
6337 mask handy. Casting avoids a GCC warning. */
6338 image_background_transparent (img, f, (XImagePtr_or_DC)mask_img);
6340 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
6341 x_destroy_x_image (mask_img);
6344 UNGCPRO;
6345 return 1;
6348 #else /* HAVE_PNG */
6350 #ifdef MAC_OS
6351 static int
6352 png_load (f, img)
6353 struct frame *f;
6354 struct image *img;
6356 #ifdef MAC_OSX
6357 if (MyCGImageCreateWithPNGDataProvider)
6358 return image_load_quartz2d (f, img, 1);
6359 else
6360 #endif
6361 return image_load_quicktime (f, img, kQTFileTypePNG);
6363 #endif /* MAC_OS */
6365 #endif /* !HAVE_PNG */
6369 /***********************************************************************
6370 JPEG
6371 ***********************************************************************/
6373 #if defined (HAVE_JPEG) || defined (MAC_OS)
6375 static int jpeg_image_p P_ ((Lisp_Object object));
6376 static int jpeg_load P_ ((struct frame *f, struct image *img));
6378 /* The symbol `jpeg' identifying images of this type. */
6380 Lisp_Object Qjpeg;
6382 /* Indices of image specification fields in gs_format, below. */
6384 enum jpeg_keyword_index
6386 JPEG_TYPE,
6387 JPEG_DATA,
6388 JPEG_FILE,
6389 JPEG_ASCENT,
6390 JPEG_MARGIN,
6391 JPEG_RELIEF,
6392 JPEG_ALGORITHM,
6393 JPEG_HEURISTIC_MASK,
6394 JPEG_MASK,
6395 JPEG_BACKGROUND,
6396 JPEG_LAST
6399 /* Vector of image_keyword structures describing the format
6400 of valid user-defined image specifications. */
6402 static struct image_keyword jpeg_format[JPEG_LAST] =
6404 {":type", IMAGE_SYMBOL_VALUE, 1},
6405 {":data", IMAGE_STRING_VALUE, 0},
6406 {":file", IMAGE_STRING_VALUE, 0},
6407 {":ascent", IMAGE_ASCENT_VALUE, 0},
6408 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6409 {":relief", IMAGE_INTEGER_VALUE, 0},
6410 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6411 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6412 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6413 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6416 /* Structure describing the image type `jpeg'. */
6418 static struct image_type jpeg_type =
6420 &Qjpeg,
6421 jpeg_image_p,
6422 jpeg_load,
6423 x_clear_image,
6424 NULL
6427 /* Return non-zero if OBJECT is a valid JPEG image specification. */
6429 static int
6430 jpeg_image_p (object)
6431 Lisp_Object object;
6433 struct image_keyword fmt[JPEG_LAST];
6435 bcopy (jpeg_format, fmt, sizeof fmt);
6437 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
6438 return 0;
6440 /* Must specify either the :data or :file keyword. */
6441 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
6444 #endif /* HAVE_JPEG || MAC_OS */
6446 #ifdef HAVE_JPEG
6448 /* Work around a warning about HAVE_STDLIB_H being redefined in
6449 jconfig.h. */
6450 #ifdef HAVE_STDLIB_H
6451 #define HAVE_STDLIB_H_1
6452 #undef HAVE_STDLIB_H
6453 #endif /* HAVE_STLIB_H */
6455 #if defined (HAVE_NTGUI) && !defined (__WIN32__)
6456 /* In older releases of the jpeg library, jpeglib.h will define boolean
6457 differently depending on __WIN32__, so make sure it is defined. */
6458 #define __WIN32__ 1
6459 #endif
6461 #include <jpeglib.h>
6462 #include <jerror.h>
6463 #include <setjmp.h>
6465 #ifdef HAVE_STLIB_H_1
6466 #define HAVE_STDLIB_H 1
6467 #endif
6469 #ifdef HAVE_NTGUI
6471 /* JPEG library details. */
6472 DEF_IMGLIB_FN (jpeg_CreateDecompress);
6473 DEF_IMGLIB_FN (jpeg_start_decompress);
6474 DEF_IMGLIB_FN (jpeg_finish_decompress);
6475 DEF_IMGLIB_FN (jpeg_destroy_decompress);
6476 DEF_IMGLIB_FN (jpeg_read_header);
6477 DEF_IMGLIB_FN (jpeg_read_scanlines);
6478 DEF_IMGLIB_FN (jpeg_std_error);
6479 DEF_IMGLIB_FN (jpeg_resync_to_restart);
6481 static int
6482 init_jpeg_functions (Lisp_Object libraries)
6484 HMODULE library;
6486 if (!(library = w32_delayed_load (libraries, Qjpeg)))
6487 return 0;
6489 LOAD_IMGLIB_FN (library, jpeg_finish_decompress);
6490 LOAD_IMGLIB_FN (library, jpeg_read_scanlines);
6491 LOAD_IMGLIB_FN (library, jpeg_start_decompress);
6492 LOAD_IMGLIB_FN (library, jpeg_read_header);
6493 LOAD_IMGLIB_FN (library, jpeg_CreateDecompress);
6494 LOAD_IMGLIB_FN (library, jpeg_destroy_decompress);
6495 LOAD_IMGLIB_FN (library, jpeg_std_error);
6496 LOAD_IMGLIB_FN (library, jpeg_resync_to_restart);
6497 return 1;
6500 /* Wrapper since we can't directly assign the function pointer
6501 to another function pointer that was declared more completely easily. */
6502 static boolean
6503 jpeg_resync_to_restart_wrapper(cinfo, desired)
6504 j_decompress_ptr cinfo;
6505 int desired;
6507 return fn_jpeg_resync_to_restart (cinfo, desired);
6510 #else
6512 #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a)
6513 #define fn_jpeg_start_decompress jpeg_start_decompress
6514 #define fn_jpeg_finish_decompress jpeg_finish_decompress
6515 #define fn_jpeg_destroy_decompress jpeg_destroy_decompress
6516 #define fn_jpeg_read_header jpeg_read_header
6517 #define fn_jpeg_read_scanlines jpeg_read_scanlines
6518 #define fn_jpeg_std_error jpeg_std_error
6519 #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart
6521 #endif /* HAVE_NTGUI */
6523 struct my_jpeg_error_mgr
6525 struct jpeg_error_mgr pub;
6526 jmp_buf setjmp_buffer;
6530 static void
6531 my_error_exit (cinfo)
6532 j_common_ptr cinfo;
6534 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
6535 longjmp (mgr->setjmp_buffer, 1);
6539 /* Init source method for JPEG data source manager. Called by
6540 jpeg_read_header() before any data is actually read. See
6541 libjpeg.doc from the JPEG lib distribution. */
6543 static void
6544 our_common_init_source (cinfo)
6545 j_decompress_ptr cinfo;
6550 /* Method to terminate data source. Called by
6551 jpeg_finish_decompress() after all data has been processed. */
6553 static void
6554 our_common_term_source (cinfo)
6555 j_decompress_ptr cinfo;
6560 /* Fill input buffer method for JPEG data source manager. Called
6561 whenever more data is needed. We read the whole image in one step,
6562 so this only adds a fake end of input marker at the end. */
6564 static boolean
6565 our_memory_fill_input_buffer (cinfo)
6566 j_decompress_ptr cinfo;
6568 /* Insert a fake EOI marker. */
6569 struct jpeg_source_mgr *src = cinfo->src;
6570 static JOCTET buffer[2];
6572 buffer[0] = (JOCTET) 0xFF;
6573 buffer[1] = (JOCTET) JPEG_EOI;
6575 src->next_input_byte = buffer;
6576 src->bytes_in_buffer = 2;
6577 return 1;
6581 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6582 is the JPEG data source manager. */
6584 static void
6585 our_memory_skip_input_data (cinfo, num_bytes)
6586 j_decompress_ptr cinfo;
6587 long num_bytes;
6589 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
6591 if (src)
6593 if (num_bytes > src->bytes_in_buffer)
6594 ERREXIT (cinfo, JERR_INPUT_EOF);
6596 src->bytes_in_buffer -= num_bytes;
6597 src->next_input_byte += num_bytes;
6602 /* Set up the JPEG lib for reading an image from DATA which contains
6603 LEN bytes. CINFO is the decompression info structure created for
6604 reading the image. */
6606 static void
6607 jpeg_memory_src (cinfo, data, len)
6608 j_decompress_ptr cinfo;
6609 JOCTET *data;
6610 unsigned int len;
6612 struct jpeg_source_mgr *src;
6614 if (cinfo->src == NULL)
6616 /* First time for this JPEG object? */
6617 cinfo->src = (struct jpeg_source_mgr *)
6618 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
6619 sizeof (struct jpeg_source_mgr));
6620 src = (struct jpeg_source_mgr *) cinfo->src;
6621 src->next_input_byte = data;
6624 src = (struct jpeg_source_mgr *) cinfo->src;
6625 src->init_source = our_common_init_source;
6626 src->fill_input_buffer = our_memory_fill_input_buffer;
6627 src->skip_input_data = our_memory_skip_input_data;
6628 src->resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */
6629 src->term_source = our_common_term_source;
6630 src->bytes_in_buffer = len;
6631 src->next_input_byte = data;
6635 struct jpeg_stdio_mgr
6637 struct jpeg_source_mgr mgr;
6638 boolean finished;
6639 FILE *file;
6640 JOCTET *buffer;
6644 /* Size of buffer to read JPEG from file.
6645 Not too big, as we want to use alloc_small. */
6646 #define JPEG_STDIO_BUFFER_SIZE 8192
6649 /* Fill input buffer method for JPEG data source manager. Called
6650 whenever more data is needed. The data is read from a FILE *. */
6652 static boolean
6653 our_stdio_fill_input_buffer (cinfo)
6654 j_decompress_ptr cinfo;
6656 struct jpeg_stdio_mgr *src;
6658 src = (struct jpeg_stdio_mgr *) cinfo->src;
6659 if (!src->finished)
6661 size_t bytes;
6663 bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file);
6664 if (bytes > 0)
6665 src->mgr.bytes_in_buffer = bytes;
6666 else
6668 WARNMS (cinfo, JWRN_JPEG_EOF);
6669 src->finished = 1;
6670 src->buffer[0] = (JOCTET) 0xFF;
6671 src->buffer[1] = (JOCTET) JPEG_EOI;
6672 src->mgr.bytes_in_buffer = 2;
6674 src->mgr.next_input_byte = src->buffer;
6677 return 1;
6681 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
6682 is the JPEG data source manager. */
6684 static void
6685 our_stdio_skip_input_data (cinfo, num_bytes)
6686 j_decompress_ptr cinfo;
6687 long num_bytes;
6689 struct jpeg_stdio_mgr *src;
6690 src = (struct jpeg_stdio_mgr *) cinfo->src;
6692 while (num_bytes > 0 && !src->finished)
6694 if (num_bytes <= src->mgr.bytes_in_buffer)
6696 src->mgr.bytes_in_buffer -= num_bytes;
6697 src->mgr.next_input_byte += num_bytes;
6698 break;
6700 else
6702 num_bytes -= src->mgr.bytes_in_buffer;
6703 src->mgr.bytes_in_buffer = 0;
6704 src->mgr.next_input_byte = NULL;
6706 our_stdio_fill_input_buffer (cinfo);
6712 /* Set up the JPEG lib for reading an image from a FILE *.
6713 CINFO is the decompression info structure created for
6714 reading the image. */
6716 static void
6717 jpeg_file_src (cinfo, fp)
6718 j_decompress_ptr cinfo;
6719 FILE *fp;
6721 struct jpeg_stdio_mgr *src;
6723 if (cinfo->src != NULL)
6724 src = (struct jpeg_stdio_mgr *) cinfo->src;
6725 else
6727 /* First time for this JPEG object? */
6728 cinfo->src = (struct jpeg_source_mgr *)
6729 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
6730 sizeof (struct jpeg_stdio_mgr));
6731 src = (struct jpeg_stdio_mgr *) cinfo->src;
6732 src->buffer = (JOCTET *)
6733 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
6734 JPEG_STDIO_BUFFER_SIZE);
6737 src->file = fp;
6738 src->finished = 0;
6739 src->mgr.init_source = our_common_init_source;
6740 src->mgr.fill_input_buffer = our_stdio_fill_input_buffer;
6741 src->mgr.skip_input_data = our_stdio_skip_input_data;
6742 src->mgr.resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */
6743 src->mgr.term_source = our_common_term_source;
6744 src->mgr.bytes_in_buffer = 0;
6745 src->mgr.next_input_byte = NULL;
6749 /* Load image IMG for use on frame F. Patterned after example.c
6750 from the JPEG lib. */
6752 static int
6753 jpeg_load (f, img)
6754 struct frame *f;
6755 struct image *img;
6757 struct jpeg_decompress_struct cinfo;
6758 struct my_jpeg_error_mgr mgr;
6759 Lisp_Object file, specified_file;
6760 Lisp_Object specified_data;
6761 FILE * volatile fp = NULL;
6762 JSAMPARRAY buffer;
6763 int row_stride, x, y;
6764 XImagePtr ximg = NULL;
6765 int rc;
6766 unsigned long *colors;
6767 int width, height;
6768 struct gcpro gcpro1;
6770 /* Open the JPEG file. */
6771 specified_file = image_spec_value (img->spec, QCfile, NULL);
6772 specified_data = image_spec_value (img->spec, QCdata, NULL);
6773 file = Qnil;
6774 GCPRO1 (file);
6776 if (NILP (specified_data))
6778 file = x_find_image_file (specified_file);
6779 if (!STRINGP (file))
6781 image_error ("Cannot find image file `%s'", specified_file, Qnil);
6782 UNGCPRO;
6783 return 0;
6786 fp = fopen (SDATA (file), "rb");
6787 if (fp == NULL)
6789 image_error ("Cannot open `%s'", file, Qnil);
6790 UNGCPRO;
6791 return 0;
6795 /* Customize libjpeg's error handling to call my_error_exit when an
6796 error is detected. This function will perform a longjmp.
6797 Casting return value avoids a GCC warning on W32. */
6798 cinfo.err = (struct jpeg_error_mgr *)fn_jpeg_std_error (&mgr.pub);
6799 mgr.pub.error_exit = my_error_exit;
6801 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
6803 if (rc == 1)
6805 /* Called from my_error_exit. Display a JPEG error. */
6806 char buffer[JMSG_LENGTH_MAX];
6807 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
6808 image_error ("Error reading JPEG image `%s': %s", img->spec,
6809 build_string (buffer));
6812 /* Close the input file and destroy the JPEG object. */
6813 if (fp)
6814 fclose ((FILE *) fp);
6815 fn_jpeg_destroy_decompress (&cinfo);
6817 /* If we already have an XImage, free that. */
6818 x_destroy_x_image (ximg);
6820 /* Free pixmap and colors. */
6821 x_clear_image (f, img);
6823 UNGCPRO;
6824 return 0;
6827 /* Create the JPEG decompression object. Let it read from fp.
6828 Read the JPEG image header. */
6829 fn_jpeg_CreateDecompress (&cinfo, JPEG_LIB_VERSION, sizeof (cinfo));
6831 if (NILP (specified_data))
6832 jpeg_file_src (&cinfo, (FILE *) fp);
6833 else
6834 jpeg_memory_src (&cinfo, SDATA (specified_data),
6835 SBYTES (specified_data));
6837 fn_jpeg_read_header (&cinfo, 1);
6839 /* Customize decompression so that color quantization will be used.
6840 Start decompression. */
6841 cinfo.quantize_colors = 1;
6842 fn_jpeg_start_decompress (&cinfo);
6843 width = img->width = cinfo.output_width;
6844 height = img->height = cinfo.output_height;
6846 if (!check_image_size (f, width, height))
6848 image_error ("Invalid image size", Qnil, Qnil);
6849 longjmp (mgr.setjmp_buffer, 2);
6852 /* Create X image and pixmap. */
6853 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
6854 longjmp (mgr.setjmp_buffer, 2);
6856 /* Allocate colors. When color quantization is used,
6857 cinfo.actual_number_of_colors has been set with the number of
6858 colors generated, and cinfo.colormap is a two-dimensional array
6859 of color indices in the range 0..cinfo.actual_number_of_colors.
6860 No more than 255 colors will be generated. */
6862 int i, ir, ig, ib;
6864 if (cinfo.out_color_components > 2)
6865 ir = 0, ig = 1, ib = 2;
6866 else if (cinfo.out_color_components > 1)
6867 ir = 0, ig = 1, ib = 0;
6868 else
6869 ir = 0, ig = 0, ib = 0;
6871 /* Use the color table mechanism because it handles colors that
6872 cannot be allocated nicely. Such colors will be replaced with
6873 a default color, and we don't have to care about which colors
6874 can be freed safely, and which can't. */
6875 init_color_table ();
6876 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
6877 * sizeof *colors);
6879 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
6881 /* Multiply RGB values with 255 because X expects RGB values
6882 in the range 0..0xffff. */
6883 int r = cinfo.colormap[ir][i] << 8;
6884 int g = cinfo.colormap[ig][i] << 8;
6885 int b = cinfo.colormap[ib][i] << 8;
6886 colors[i] = lookup_rgb_color (f, r, g, b);
6889 #ifdef COLOR_TABLE_SUPPORT
6890 /* Remember those colors actually allocated. */
6891 img->colors = colors_in_color_table (&img->ncolors);
6892 free_color_table ();
6893 #endif /* COLOR_TABLE_SUPPORT */
6896 /* Read pixels. */
6897 row_stride = width * cinfo.output_components;
6898 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
6899 row_stride, 1);
6900 for (y = 0; y < height; ++y)
6902 fn_jpeg_read_scanlines (&cinfo, buffer, 1);
6903 for (x = 0; x < cinfo.output_width; ++x)
6904 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
6907 /* Clean up. */
6908 fn_jpeg_finish_decompress (&cinfo);
6909 fn_jpeg_destroy_decompress (&cinfo);
6910 if (fp)
6911 fclose ((FILE *) fp);
6913 /* Maybe fill in the background field while we have ximg handy. */
6914 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
6915 /* Casting avoids a GCC warning. */
6916 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
6918 /* Put the image into the pixmap. */
6919 x_put_x_image (f, ximg, img->pixmap, width, height);
6920 x_destroy_x_image (ximg);
6921 UNGCPRO;
6922 return 1;
6925 #else /* HAVE_JPEG */
6927 #ifdef MAC_OS
6928 static int
6929 jpeg_load (f, img)
6930 struct frame *f;
6931 struct image *img;
6933 #ifdef MAC_OSX
6934 return image_load_quartz2d (f, img, 0);
6935 #else
6936 return image_load_quicktime (f, img, kQTFileTypeJPEG);
6937 #endif
6939 #endif /* MAC_OS */
6941 #endif /* !HAVE_JPEG */
6945 /***********************************************************************
6946 TIFF
6947 ***********************************************************************/
6949 #if defined (HAVE_TIFF) || defined (MAC_OS)
6951 static int tiff_image_p P_ ((Lisp_Object object));
6952 static int tiff_load P_ ((struct frame *f, struct image *img));
6954 /* The symbol `tiff' identifying images of this type. */
6956 Lisp_Object Qtiff;
6958 /* Indices of image specification fields in tiff_format, below. */
6960 enum tiff_keyword_index
6962 TIFF_TYPE,
6963 TIFF_DATA,
6964 TIFF_FILE,
6965 TIFF_ASCENT,
6966 TIFF_MARGIN,
6967 TIFF_RELIEF,
6968 TIFF_ALGORITHM,
6969 TIFF_HEURISTIC_MASK,
6970 TIFF_MASK,
6971 TIFF_BACKGROUND,
6972 TIFF_LAST
6975 /* Vector of image_keyword structures describing the format
6976 of valid user-defined image specifications. */
6978 static struct image_keyword tiff_format[TIFF_LAST] =
6980 {":type", IMAGE_SYMBOL_VALUE, 1},
6981 {":data", IMAGE_STRING_VALUE, 0},
6982 {":file", IMAGE_STRING_VALUE, 0},
6983 {":ascent", IMAGE_ASCENT_VALUE, 0},
6984 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6985 {":relief", IMAGE_INTEGER_VALUE, 0},
6986 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6987 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6988 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6989 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6992 /* Structure describing the image type `tiff'. */
6994 static struct image_type tiff_type =
6996 &Qtiff,
6997 tiff_image_p,
6998 tiff_load,
6999 x_clear_image,
7000 NULL
7003 /* Return non-zero if OBJECT is a valid TIFF image specification. */
7005 static int
7006 tiff_image_p (object)
7007 Lisp_Object object;
7009 struct image_keyword fmt[TIFF_LAST];
7010 bcopy (tiff_format, fmt, sizeof fmt);
7012 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
7013 return 0;
7015 /* Must specify either the :data or :file keyword. */
7016 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
7019 #endif /* HAVE_TIFF || MAC_OS */
7021 #ifdef HAVE_TIFF
7023 #include <tiffio.h>
7025 #ifdef HAVE_NTGUI
7027 /* TIFF library details. */
7028 DEF_IMGLIB_FN (TIFFSetErrorHandler);
7029 DEF_IMGLIB_FN (TIFFSetWarningHandler);
7030 DEF_IMGLIB_FN (TIFFOpen);
7031 DEF_IMGLIB_FN (TIFFClientOpen);
7032 DEF_IMGLIB_FN (TIFFGetField);
7033 DEF_IMGLIB_FN (TIFFReadRGBAImage);
7034 DEF_IMGLIB_FN (TIFFClose);
7036 static int
7037 init_tiff_functions (Lisp_Object libraries)
7039 HMODULE library;
7041 if (!(library = w32_delayed_load (libraries, Qtiff)))
7042 return 0;
7044 LOAD_IMGLIB_FN (library, TIFFSetErrorHandler);
7045 LOAD_IMGLIB_FN (library, TIFFSetWarningHandler);
7046 LOAD_IMGLIB_FN (library, TIFFOpen);
7047 LOAD_IMGLIB_FN (library, TIFFClientOpen);
7048 LOAD_IMGLIB_FN (library, TIFFGetField);
7049 LOAD_IMGLIB_FN (library, TIFFReadRGBAImage);
7050 LOAD_IMGLIB_FN (library, TIFFClose);
7051 return 1;
7054 #else
7056 #define fn_TIFFSetErrorHandler TIFFSetErrorHandler
7057 #define fn_TIFFSetWarningHandler TIFFSetWarningHandler
7058 #define fn_TIFFOpen TIFFOpen
7059 #define fn_TIFFClientOpen TIFFClientOpen
7060 #define fn_TIFFGetField TIFFGetField
7061 #define fn_TIFFReadRGBAImage TIFFReadRGBAImage
7062 #define fn_TIFFClose TIFFClose
7064 #endif /* HAVE_NTGUI */
7067 /* Reading from a memory buffer for TIFF images Based on the PNG
7068 memory source, but we have to provide a lot of extra functions.
7069 Blah.
7071 We really only need to implement read and seek, but I am not
7072 convinced that the TIFF library is smart enough not to destroy
7073 itself if we only hand it the function pointers we need to
7074 override. */
7076 typedef struct
7078 unsigned char *bytes;
7079 size_t len;
7080 int index;
7082 tiff_memory_source;
7084 static size_t
7085 tiff_read_from_memory (data, buf, size)
7086 thandle_t data;
7087 tdata_t buf;
7088 tsize_t size;
7090 tiff_memory_source *src = (tiff_memory_source *) data;
7092 if (size > src->len - src->index)
7093 return (size_t) -1;
7094 bcopy (src->bytes + src->index, buf, size);
7095 src->index += size;
7096 return size;
7099 static size_t
7100 tiff_write_from_memory (data, buf, size)
7101 thandle_t data;
7102 tdata_t buf;
7103 tsize_t size;
7105 return (size_t) -1;
7108 static toff_t
7109 tiff_seek_in_memory (data, off, whence)
7110 thandle_t data;
7111 toff_t off;
7112 int whence;
7114 tiff_memory_source *src = (tiff_memory_source *) data;
7115 int idx;
7117 switch (whence)
7119 case SEEK_SET: /* Go from beginning of source. */
7120 idx = off;
7121 break;
7123 case SEEK_END: /* Go from end of source. */
7124 idx = src->len + off;
7125 break;
7127 case SEEK_CUR: /* Go from current position. */
7128 idx = src->index + off;
7129 break;
7131 default: /* Invalid `whence'. */
7132 return -1;
7135 if (idx > src->len || idx < 0)
7136 return -1;
7138 src->index = idx;
7139 return src->index;
7142 static int
7143 tiff_close_memory (data)
7144 thandle_t data;
7146 /* NOOP */
7147 return 0;
7150 static int
7151 tiff_mmap_memory (data, pbase, psize)
7152 thandle_t data;
7153 tdata_t *pbase;
7154 toff_t *psize;
7156 /* It is already _IN_ memory. */
7157 return 0;
7160 static void
7161 tiff_unmap_memory (data, base, size)
7162 thandle_t data;
7163 tdata_t base;
7164 toff_t size;
7166 /* We don't need to do this. */
7169 static toff_t
7170 tiff_size_of_memory (data)
7171 thandle_t data;
7173 return ((tiff_memory_source *) data)->len;
7177 static void
7178 tiff_error_handler (title, format, ap)
7179 const char *title, *format;
7180 va_list ap;
7182 char buf[512];
7183 int len;
7185 len = sprintf (buf, "TIFF error: %s ", title);
7186 vsprintf (buf + len, format, ap);
7187 add_to_log (buf, Qnil, Qnil);
7191 static void
7192 tiff_warning_handler (title, format, ap)
7193 const char *title, *format;
7194 va_list ap;
7196 char buf[512];
7197 int len;
7199 len = sprintf (buf, "TIFF warning: %s ", title);
7200 vsprintf (buf + len, format, ap);
7201 add_to_log (buf, Qnil, Qnil);
7205 /* Load TIFF image IMG for use on frame F. Value is non-zero if
7206 successful. */
7208 static int
7209 tiff_load (f, img)
7210 struct frame *f;
7211 struct image *img;
7213 Lisp_Object file, specified_file;
7214 Lisp_Object specified_data;
7215 TIFF *tiff;
7216 int width, height, x, y;
7217 uint32 *buf;
7218 int rc;
7219 XImagePtr ximg;
7220 struct gcpro gcpro1;
7221 tiff_memory_source memsrc;
7223 specified_file = image_spec_value (img->spec, QCfile, NULL);
7224 specified_data = image_spec_value (img->spec, QCdata, NULL);
7225 file = Qnil;
7226 GCPRO1 (file);
7228 fn_TIFFSetErrorHandler (tiff_error_handler);
7229 fn_TIFFSetWarningHandler (tiff_warning_handler);
7231 if (NILP (specified_data))
7233 /* Read from a file */
7234 file = x_find_image_file (specified_file);
7235 if (!STRINGP (file))
7237 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7238 UNGCPRO;
7239 return 0;
7242 /* Try to open the image file. Casting return value avoids a
7243 GCC warning on W32. */
7244 tiff = (TIFF *)fn_TIFFOpen (SDATA (file), "r");
7245 if (tiff == NULL)
7247 image_error ("Cannot open `%s'", file, Qnil);
7248 UNGCPRO;
7249 return 0;
7252 else
7254 /* Memory source! */
7255 memsrc.bytes = SDATA (specified_data);
7256 memsrc.len = SBYTES (specified_data);
7257 memsrc.index = 0;
7259 /* Casting return value avoids a GCC warning on W32. */
7260 tiff = (TIFF *)fn_TIFFClientOpen ("memory_source", "r", &memsrc,
7261 (TIFFReadWriteProc) tiff_read_from_memory,
7262 (TIFFReadWriteProc) tiff_write_from_memory,
7263 tiff_seek_in_memory,
7264 tiff_close_memory,
7265 tiff_size_of_memory,
7266 tiff_mmap_memory,
7267 tiff_unmap_memory);
7269 if (!tiff)
7271 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
7272 UNGCPRO;
7273 return 0;
7277 /* Get width and height of the image, and allocate a raster buffer
7278 of width x height 32-bit values. */
7279 fn_TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
7280 fn_TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
7282 if (!check_image_size (f, width, height))
7284 image_error ("Invalid image size", Qnil, Qnil);
7285 UNGCPRO;
7286 return 0;
7289 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
7291 rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0);
7292 fn_TIFFClose (tiff);
7293 if (!rc)
7295 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
7296 xfree (buf);
7297 UNGCPRO;
7298 return 0;
7301 /* Create the X image and pixmap. */
7302 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
7304 xfree (buf);
7305 UNGCPRO;
7306 return 0;
7309 /* Initialize the color table. */
7310 init_color_table ();
7312 /* Process the pixel raster. Origin is in the lower-left corner. */
7313 for (y = 0; y < height; ++y)
7315 uint32 *row = buf + y * width;
7317 for (x = 0; x < width; ++x)
7319 uint32 abgr = row[x];
7320 int r = TIFFGetR (abgr) << 8;
7321 int g = TIFFGetG (abgr) << 8;
7322 int b = TIFFGetB (abgr) << 8;
7323 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
7327 #ifdef COLOR_TABLE_SUPPORT
7328 /* Remember the colors allocated for the image. Free the color table. */
7329 img->colors = colors_in_color_table (&img->ncolors);
7330 free_color_table ();
7331 #endif /* COLOR_TABLE_SUPPORT */
7333 img->width = width;
7334 img->height = height;
7336 /* Maybe fill in the background field while we have ximg handy. */
7337 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7338 /* Casting avoids a GCC warning on W32. */
7339 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
7341 /* Put the image into the pixmap, then free the X image and its buffer. */
7342 x_put_x_image (f, ximg, img->pixmap, width, height);
7343 x_destroy_x_image (ximg);
7344 xfree (buf);
7346 UNGCPRO;
7347 return 1;
7350 #else /* HAVE_TIFF */
7352 #ifdef MAC_OS
7353 static int
7354 tiff_load (f, img)
7355 struct frame *f;
7356 struct image *img;
7358 return image_load_quicktime (f, img, kQTFileTypeTIFF);
7360 #endif /* MAC_OS */
7362 #endif /* !HAVE_TIFF */
7366 /***********************************************************************
7368 ***********************************************************************/
7370 #if defined (HAVE_GIF) || defined (MAC_OS)
7372 static int gif_image_p P_ ((Lisp_Object object));
7373 static int gif_load P_ ((struct frame *f, struct image *img));
7375 /* The symbol `gif' identifying images of this type. */
7377 Lisp_Object Qgif;
7379 /* Indices of image specification fields in gif_format, below. */
7381 enum gif_keyword_index
7383 GIF_TYPE,
7384 GIF_DATA,
7385 GIF_FILE,
7386 GIF_ASCENT,
7387 GIF_MARGIN,
7388 GIF_RELIEF,
7389 GIF_ALGORITHM,
7390 GIF_HEURISTIC_MASK,
7391 GIF_MASK,
7392 GIF_IMAGE,
7393 GIF_BACKGROUND,
7394 GIF_LAST
7397 /* Vector of image_keyword structures describing the format
7398 of valid user-defined image specifications. */
7400 static struct image_keyword gif_format[GIF_LAST] =
7402 {":type", IMAGE_SYMBOL_VALUE, 1},
7403 {":data", IMAGE_STRING_VALUE, 0},
7404 {":file", IMAGE_STRING_VALUE, 0},
7405 {":ascent", IMAGE_ASCENT_VALUE, 0},
7406 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
7407 {":relief", IMAGE_INTEGER_VALUE, 0},
7408 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7409 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7410 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7411 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
7412 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
7415 /* Structure describing the image type `gif'. */
7417 static struct image_type gif_type =
7419 &Qgif,
7420 gif_image_p,
7421 gif_load,
7422 x_clear_image,
7423 NULL
7426 /* Return non-zero if OBJECT is a valid GIF image specification. */
7428 static int
7429 gif_image_p (object)
7430 Lisp_Object object;
7432 struct image_keyword fmt[GIF_LAST];
7433 bcopy (gif_format, fmt, sizeof fmt);
7435 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
7436 return 0;
7438 /* Must specify either the :data or :file keyword. */
7439 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
7442 #endif /* HAVE_GIF || MAC_OS */
7444 #ifdef HAVE_GIF
7446 #if defined (HAVE_NTGUI) || defined (MAC_OS)
7447 /* winuser.h might define DrawText to DrawTextA or DrawTextW.
7448 Undefine before redefining to avoid a preprocessor warning. */
7449 #ifdef DrawText
7450 #undef DrawText
7451 #endif
7452 /* avoid conflict with QuickdrawText.h */
7453 #define DrawText gif_DrawText
7454 #include <gif_lib.h>
7455 #undef DrawText
7457 #else /* HAVE_NTGUI || MAC_OS */
7459 #include <gif_lib.h>
7461 #endif /* HAVE_NTGUI || MAC_OS */
7464 #ifdef HAVE_NTGUI
7466 /* GIF library details. */
7467 DEF_IMGLIB_FN (DGifCloseFile);
7468 DEF_IMGLIB_FN (DGifSlurp);
7469 DEF_IMGLIB_FN (DGifOpen);
7470 DEF_IMGLIB_FN (DGifOpenFileName);
7472 static int
7473 init_gif_functions (Lisp_Object libraries)
7475 HMODULE library;
7477 if (!(library = w32_delayed_load (libraries, Qgif)))
7478 return 0;
7480 LOAD_IMGLIB_FN (library, DGifCloseFile);
7481 LOAD_IMGLIB_FN (library, DGifSlurp);
7482 LOAD_IMGLIB_FN (library, DGifOpen);
7483 LOAD_IMGLIB_FN (library, DGifOpenFileName);
7484 return 1;
7487 #else
7489 #define fn_DGifCloseFile DGifCloseFile
7490 #define fn_DGifSlurp DGifSlurp
7491 #define fn_DGifOpen DGifOpen
7492 #define fn_DGifOpenFileName DGifOpenFileName
7494 #endif /* HAVE_NTGUI */
7496 /* Reading a GIF image from memory
7497 Based on the PNG memory stuff to a certain extent. */
7499 typedef struct
7501 unsigned char *bytes;
7502 size_t len;
7503 int index;
7505 gif_memory_source;
7507 /* Make the current memory source available to gif_read_from_memory.
7508 It's done this way because not all versions of libungif support
7509 a UserData field in the GifFileType structure. */
7510 static gif_memory_source *current_gif_memory_src;
7512 static int
7513 gif_read_from_memory (file, buf, len)
7514 GifFileType *file;
7515 GifByteType *buf;
7516 int len;
7518 gif_memory_source *src = current_gif_memory_src;
7520 if (len > src->len - src->index)
7521 return -1;
7523 bcopy (src->bytes + src->index, buf, len);
7524 src->index += len;
7525 return len;
7529 /* Load GIF image IMG for use on frame F. Value is non-zero if
7530 successful. */
7532 static int
7533 gif_load (f, img)
7534 struct frame *f;
7535 struct image *img;
7537 Lisp_Object file, specified_file;
7538 Lisp_Object specified_data;
7539 int rc, width, height, x, y, i;
7540 XImagePtr ximg;
7541 ColorMapObject *gif_color_map;
7542 unsigned long pixel_colors[256];
7543 GifFileType *gif;
7544 struct gcpro gcpro1;
7545 Lisp_Object image;
7546 int ino, image_left, image_top, image_width, image_height;
7547 gif_memory_source memsrc;
7548 unsigned char *raster;
7550 specified_file = image_spec_value (img->spec, QCfile, NULL);
7551 specified_data = image_spec_value (img->spec, QCdata, NULL);
7552 file = Qnil;
7553 GCPRO1 (file);
7555 if (NILP (specified_data))
7557 file = x_find_image_file (specified_file);
7558 if (!STRINGP (file))
7560 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7561 UNGCPRO;
7562 return 0;
7565 /* Open the GIF file. Casting return value avoids a GCC warning
7566 on W32. */
7567 gif = (GifFileType *)fn_DGifOpenFileName (SDATA (file));
7568 if (gif == NULL)
7570 image_error ("Cannot open `%s'", file, Qnil);
7571 UNGCPRO;
7572 return 0;
7575 else
7577 /* Read from memory! */
7578 current_gif_memory_src = &memsrc;
7579 memsrc.bytes = SDATA (specified_data);
7580 memsrc.len = SBYTES (specified_data);
7581 memsrc.index = 0;
7583 /* Casting return value avoids a GCC warning on W32. */
7584 gif = (GifFileType *)fn_DGifOpen(&memsrc, gif_read_from_memory);
7585 if (!gif)
7587 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
7588 UNGCPRO;
7589 return 0;
7593 /* Before reading entire contents, check the declared image size. */
7594 if (!check_image_size (f, gif->SWidth, gif->SHeight))
7596 image_error ("Invalid image size", Qnil, Qnil);
7597 fn_DGifCloseFile (gif);
7598 UNGCPRO;
7599 return 0;
7602 /* Read entire contents. */
7603 rc = fn_DGifSlurp (gif);
7604 if (rc == GIF_ERROR)
7606 image_error ("Error reading `%s'", img->spec, Qnil);
7607 fn_DGifCloseFile (gif);
7608 UNGCPRO;
7609 return 0;
7612 image = image_spec_value (img->spec, QCindex, NULL);
7613 ino = INTEGERP (image) ? XFASTINT (image) : 0;
7614 if (ino >= gif->ImageCount)
7616 image_error ("Invalid image number `%s' in image `%s'",
7617 image, img->spec);
7618 fn_DGifCloseFile (gif);
7619 UNGCPRO;
7620 return 0;
7623 image_top = gif->SavedImages[ino].ImageDesc.Top;
7624 image_left = gif->SavedImages[ino].ImageDesc.Left;
7625 image_width = gif->SavedImages[ino].ImageDesc.Width;
7626 image_height = gif->SavedImages[ino].ImageDesc.Height;
7628 width = img->width = max (gif->SWidth,
7629 max (gif->Image.Left + gif->Image.Width,
7630 image_left + image_width));
7631 height = img->height = max (gif->SHeight,
7632 max (gif->Image.Top + gif->Image.Height,
7633 image_top + image_height));
7635 if (!check_image_size (f, width, height))
7637 image_error ("Invalid image size", Qnil, Qnil);
7638 fn_DGifCloseFile (gif);
7639 UNGCPRO;
7640 return 0;
7643 /* Create the X image and pixmap. */
7644 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
7646 fn_DGifCloseFile (gif);
7647 UNGCPRO;
7648 return 0;
7651 /* Allocate colors. */
7652 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
7653 if (!gif_color_map)
7654 gif_color_map = gif->SColorMap;
7655 init_color_table ();
7656 bzero (pixel_colors, sizeof pixel_colors);
7658 for (i = 0; i < gif_color_map->ColorCount; ++i)
7660 int r = gif_color_map->Colors[i].Red << 8;
7661 int g = gif_color_map->Colors[i].Green << 8;
7662 int b = gif_color_map->Colors[i].Blue << 8;
7663 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
7666 #ifdef COLOR_TABLE_SUPPORT
7667 img->colors = colors_in_color_table (&img->ncolors);
7668 free_color_table ();
7669 #endif /* COLOR_TABLE_SUPPORT */
7671 /* Clear the part of the screen image that are not covered by
7672 the image from the GIF file. Full animated GIF support
7673 requires more than can be done here (see the gif89 spec,
7674 disposal methods). Let's simply assume that the part
7675 not covered by a sub-image is in the frame's background color. */
7676 for (y = 0; y < image_top; ++y)
7677 for (x = 0; x < width; ++x)
7678 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7680 for (y = image_top + image_height; y < height; ++y)
7681 for (x = 0; x < width; ++x)
7682 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7684 for (y = image_top; y < image_top + image_height; ++y)
7686 for (x = 0; x < image_left; ++x)
7687 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7688 for (x = image_left + image_width; x < width; ++x)
7689 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
7692 /* Read the GIF image into the X image. We use a local variable
7693 `raster' here because RasterBits below is a char *, and invites
7694 problems with bytes >= 0x80. */
7695 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
7697 if (gif->SavedImages[ino].ImageDesc.Interlace)
7699 static int interlace_start[] = {0, 4, 2, 1};
7700 static int interlace_increment[] = {8, 8, 4, 2};
7701 int pass;
7702 int row = interlace_start[0];
7704 pass = 0;
7706 for (y = 0; y < image_height; y++)
7708 if (row >= image_height)
7710 row = interlace_start[++pass];
7711 while (row >= image_height)
7712 row = interlace_start[++pass];
7715 for (x = 0; x < image_width; x++)
7717 int i = raster[(y * image_width) + x];
7718 XPutPixel (ximg, x + image_left, row + image_top,
7719 pixel_colors[i]);
7722 row += interlace_increment[pass];
7725 else
7727 for (y = 0; y < image_height; ++y)
7728 for (x = 0; x < image_width; ++x)
7730 int i = raster[y * image_width + x];
7731 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
7735 fn_DGifCloseFile (gif);
7737 /* Maybe fill in the background field while we have ximg handy. */
7738 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7739 /* Casting avoids a GCC warning. */
7740 IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);
7742 /* Put the image into the pixmap, then free the X image and its buffer. */
7743 x_put_x_image (f, ximg, img->pixmap, width, height);
7744 x_destroy_x_image (ximg);
7746 UNGCPRO;
7747 return 1;
7750 #else /* !HAVE_GIF */
7752 #ifdef MAC_OS
7753 static int
7754 gif_load (f, img)
7755 struct frame *f;
7756 struct image *img;
7758 Lisp_Object specified_file, file;
7759 Lisp_Object specified_data;
7760 OSErr err;
7761 Boolean graphic_p, movie_p, prefer_graphic_p;
7762 Handle dh = NULL;
7763 Movie movie = NULL;
7764 Lisp_Object image;
7765 Track track = NULL;
7766 Media media = NULL;
7767 long nsamples;
7768 Rect rect;
7769 Lisp_Object specified_bg;
7770 XColor color;
7771 RGBColor bg_color;
7772 int width, height;
7773 XImagePtr ximg;
7774 TimeValue time;
7775 struct gcpro gcpro1;
7776 int ino;
7777 CGrafPtr old_port;
7778 GDHandle old_gdh;
7780 specified_file = image_spec_value (img->spec, QCfile, NULL);
7781 specified_data = image_spec_value (img->spec, QCdata, NULL);
7783 /* Animated gifs use QuickTime Movie Toolbox. So initialize it here. */
7784 EnterMovies ();
7786 if (NILP (specified_data))
7788 /* Read from a file */
7789 FSSpec fss;
7790 short refnum;
7792 err = find_image_fsspec (specified_file, &file, &fss);
7793 if (err != noErr)
7795 if (err == fnfErr)
7796 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7797 else
7798 goto open_error;
7801 err = CanQuickTimeOpenFile (&fss, kQTFileTypeGIF, 0,
7802 &graphic_p, &movie_p, &prefer_graphic_p, 0);
7803 if (err != noErr)
7804 goto open_error;
7806 if (!graphic_p && !movie_p)
7807 goto open_error;
7808 if (prefer_graphic_p)
7809 return image_load_qt_1 (f, img, kQTFileTypeGIF, &fss, NULL);
7810 err = OpenMovieFile (&fss, &refnum, fsRdPerm);
7811 if (err != noErr)
7812 goto open_error;
7813 err = NewMovieFromFile (&movie, refnum, NULL, NULL, 0, NULL);
7814 CloseMovieFile (refnum);
7815 if (err != noErr)
7817 image_error ("Error reading `%s'", file, Qnil);
7818 return 0;
7821 else
7823 /* Memory source! */
7824 Handle dref = NULL;
7825 long file_type_atom[3];
7827 err = PtrToHand (SDATA (specified_data), &dh, SBYTES (specified_data));
7828 if (err != noErr)
7830 image_error ("Cannot allocate data handle for `%s'",
7831 img->spec, Qnil);
7832 goto error;
7835 file_type_atom[0] = EndianU32_NtoB (sizeof (long) * 3);
7836 file_type_atom[1] = EndianU32_NtoB (kDataRefExtensionMacOSFileType);
7837 file_type_atom[2] = EndianU32_NtoB (kQTFileTypeGIF);
7838 err = PtrToHand (&dh, &dref, sizeof (Handle));
7839 if (err == noErr)
7840 /* no file name */
7841 err = PtrAndHand ("\p", dref, 1);
7842 if (err == noErr)
7843 err = PtrAndHand (file_type_atom, dref, sizeof (long) * 3);
7844 if (err != noErr)
7846 image_error ("Cannot allocate handle data ref for `%s'", img->spec, Qnil);
7847 goto error;
7849 err = CanQuickTimeOpenDataRef (dref, HandleDataHandlerSubType, &graphic_p,
7850 &movie_p, &prefer_graphic_p, 0);
7851 if (err != noErr)
7852 goto open_error;
7854 if (!graphic_p && !movie_p)
7855 goto open_error;
7856 if (prefer_graphic_p)
7858 int success_p;
7860 DisposeHandle (dref);
7861 success_p = image_load_qt_1 (f, img, kQTFileTypeGIF, NULL, dh);
7862 DisposeHandle (dh);
7863 return success_p;
7865 err = NewMovieFromDataRef (&movie, 0, NULL, dref,
7866 HandleDataHandlerSubType);
7867 DisposeHandle (dref);
7868 if (err != noErr)
7869 goto open_error;
7872 image = image_spec_value (img->spec, QCindex, NULL);
7873 ino = INTEGERP (image) ? XFASTINT (image) : 0;
7874 track = GetMovieIndTrack (movie, 1);
7875 media = GetTrackMedia (track);
7876 nsamples = GetMediaSampleCount (media);
7877 if (ino >= nsamples)
7879 image_error ("Invalid image number `%s' in image `%s'",
7880 image, img->spec);
7881 goto error;
7884 specified_bg = image_spec_value (img->spec, QCbackground, NULL);
7885 if (!STRINGP (specified_bg) ||
7886 !mac_defined_color (f, SDATA (specified_bg), &color, 0))
7888 color.pixel = FRAME_BACKGROUND_PIXEL (f);
7889 color.red = RED16_FROM_ULONG (color.pixel);
7890 color.green = GREEN16_FROM_ULONG (color.pixel);
7891 color.blue = BLUE16_FROM_ULONG (color.pixel);
7893 GetMovieBox (movie, &rect);
7894 width = img->width = rect.right - rect.left;
7895 height = img->height = rect.bottom - rect.top;
7896 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
7897 goto error;
7899 GetGWorld (&old_port, &old_gdh);
7900 SetGWorld (ximg, NULL);
7901 bg_color.red = color.red;
7902 bg_color.green = color.green;
7903 bg_color.blue = color.blue;
7904 RGBBackColor (&bg_color);
7905 SetGWorld (old_port, old_gdh);
7906 SetMovieActive (movie, 1);
7907 SetMovieGWorld (movie, ximg, NULL);
7908 SampleNumToMediaTime (media, ino + 1, &time, NULL);
7909 SetMovieTimeValue (movie, time);
7910 MoviesTask (movie, 0L);
7911 DisposeTrackMedia (media);
7912 DisposeMovieTrack (track);
7913 DisposeMovie (movie);
7914 if (dh)
7915 DisposeHandle (dh);
7916 /* Maybe fill in the background field while we have ximg handy. */
7917 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7918 IMAGE_BACKGROUND (img, f, ximg);
7920 /* Put the image into the pixmap. */
7921 x_put_x_image (f, ximg, img->pixmap, width, height);
7922 x_destroy_x_image (ximg);
7923 return 1;
7925 open_error:
7926 image_error ("Cannot open `%s'", file, Qnil);
7927 error:
7928 if (media)
7929 DisposeTrackMedia (media);
7930 if (track)
7931 DisposeMovieTrack (track);
7932 if (movie)
7933 DisposeMovie (movie);
7934 if (dh)
7935 DisposeHandle (dh);
7936 return 0;
7938 #endif /* MAC_OS */
7940 #endif /* HAVE_GIF */
7944 /***********************************************************************
7945 Ghostscript
7946 ***********************************************************************/
7948 #ifdef HAVE_X_WINDOWS
7949 #define HAVE_GHOSTSCRIPT 1
7950 #endif /* HAVE_X_WINDOWS */
7952 /* The symbol `postscript' identifying images of this type. */
7954 Lisp_Object Qpostscript;
7956 #ifdef HAVE_GHOSTSCRIPT
7958 static int gs_image_p P_ ((Lisp_Object object));
7959 static int gs_load P_ ((struct frame *f, struct image *img));
7960 static void gs_clear_image P_ ((struct frame *f, struct image *img));
7962 /* Keyword symbols. */
7964 Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
7966 /* Indices of image specification fields in gs_format, below. */
7968 enum gs_keyword_index
7970 GS_TYPE,
7971 GS_PT_WIDTH,
7972 GS_PT_HEIGHT,
7973 GS_FILE,
7974 GS_LOADER,
7975 GS_BOUNDING_BOX,
7976 GS_ASCENT,
7977 GS_MARGIN,
7978 GS_RELIEF,
7979 GS_ALGORITHM,
7980 GS_HEURISTIC_MASK,
7981 GS_MASK,
7982 GS_BACKGROUND,
7983 GS_LAST
7986 /* Vector of image_keyword structures describing the format
7987 of valid user-defined image specifications. */
7989 static struct image_keyword gs_format[GS_LAST] =
7991 {":type", IMAGE_SYMBOL_VALUE, 1},
7992 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
7993 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
7994 {":file", IMAGE_STRING_VALUE, 1},
7995 {":loader", IMAGE_FUNCTION_VALUE, 0},
7996 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
7997 {":ascent", IMAGE_ASCENT_VALUE, 0},
7998 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
7999 {":relief", IMAGE_INTEGER_VALUE, 0},
8000 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8001 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8002 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8003 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8006 /* Structure describing the image type `ghostscript'. */
8008 static struct image_type gs_type =
8010 &Qpostscript,
8011 gs_image_p,
8012 gs_load,
8013 gs_clear_image,
8014 NULL
8018 /* Free X resources of Ghostscript image IMG which is used on frame F. */
8020 static void
8021 gs_clear_image (f, img)
8022 struct frame *f;
8023 struct image *img;
8025 /* IMG->data.ptr_val may contain a recorded colormap. */
8026 xfree (img->data.ptr_val);
8027 x_clear_image (f, img);
8031 /* Return non-zero if OBJECT is a valid Ghostscript image
8032 specification. */
8034 static int
8035 gs_image_p (object)
8036 Lisp_Object object;
8038 struct image_keyword fmt[GS_LAST];
8039 Lisp_Object tem;
8040 int i;
8042 bcopy (gs_format, fmt, sizeof fmt);
8044 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
8045 return 0;
8047 /* Bounding box must be a list or vector containing 4 integers. */
8048 tem = fmt[GS_BOUNDING_BOX].value;
8049 if (CONSP (tem))
8051 for (i = 0; i < 4; ++i, tem = XCDR (tem))
8052 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
8053 return 0;
8054 if (!NILP (tem))
8055 return 0;
8057 else if (VECTORP (tem))
8059 if (XVECTOR (tem)->size != 4)
8060 return 0;
8061 for (i = 0; i < 4; ++i)
8062 if (!INTEGERP (XVECTOR (tem)->contents[i]))
8063 return 0;
8065 else
8066 return 0;
8068 return 1;
8072 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
8073 if successful. */
8075 static int
8076 gs_load (f, img)
8077 struct frame *f;
8078 struct image *img;
8080 char buffer[100];
8081 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
8082 struct gcpro gcpro1, gcpro2;
8083 Lisp_Object frame;
8084 double in_width, in_height;
8085 Lisp_Object pixel_colors = Qnil;
8087 /* Compute pixel size of pixmap needed from the given size in the
8088 image specification. Sizes in the specification are in pt. 1 pt
8089 = 1/72 in, xdpi and ydpi are stored in the frame's X display
8090 info. */
8091 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
8092 in_width = XFASTINT (pt_width) / 72.0;
8093 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
8094 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
8095 in_height = XFASTINT (pt_height) / 72.0;
8096 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
8098 if (!check_image_size (f, img->width, img->height))
8100 image_error ("Invalid image size", Qnil, Qnil);
8101 return 0;
8104 /* Create the pixmap. */
8105 xassert (img->pixmap == NO_PIXMAP);
8107 /* Only W32 version did BLOCK_INPUT here. ++kfs */
8108 BLOCK_INPUT;
8109 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8110 img->width, img->height,
8111 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
8112 UNBLOCK_INPUT;
8114 if (!img->pixmap)
8116 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
8117 return 0;
8120 /* Call the loader to fill the pixmap. It returns a process object
8121 if successful. We do not record_unwind_protect here because
8122 other places in redisplay like calling window scroll functions
8123 don't either. Let the Lisp loader use `unwind-protect' instead. */
8124 GCPRO2 (window_and_pixmap_id, pixel_colors);
8126 sprintf (buffer, "%lu %lu",
8127 (unsigned long) FRAME_X_WINDOW (f),
8128 (unsigned long) img->pixmap);
8129 window_and_pixmap_id = build_string (buffer);
8131 sprintf (buffer, "%lu %lu",
8132 FRAME_FOREGROUND_PIXEL (f),
8133 FRAME_BACKGROUND_PIXEL (f));
8134 pixel_colors = build_string (buffer);
8136 XSETFRAME (frame, f);
8137 loader = image_spec_value (img->spec, QCloader, NULL);
8138 if (NILP (loader))
8139 loader = intern ("gs-load-image");
8141 img->data.lisp_val = call6 (loader, frame, img->spec,
8142 make_number (img->width),
8143 make_number (img->height),
8144 window_and_pixmap_id,
8145 pixel_colors);
8146 UNGCPRO;
8147 return PROCESSP (img->data.lisp_val);
8151 /* Kill the Ghostscript process that was started to fill PIXMAP on
8152 frame F. Called from XTread_socket when receiving an event
8153 telling Emacs that Ghostscript has finished drawing. */
8155 void
8156 x_kill_gs_process (pixmap, f)
8157 Pixmap pixmap;
8158 struct frame *f;
8160 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8161 int class, i;
8162 struct image *img;
8164 /* Find the image containing PIXMAP. */
8165 for (i = 0; i < c->used; ++i)
8166 if (c->images[i]->pixmap == pixmap)
8167 break;
8169 /* Should someone in between have cleared the image cache, for
8170 instance, give up. */
8171 if (i == c->used)
8172 return;
8174 /* Kill the GS process. We should have found PIXMAP in the image
8175 cache and its image should contain a process object. */
8176 img = c->images[i];
8177 xassert (PROCESSP (img->data.lisp_val));
8178 Fkill_process (img->data.lisp_val, Qnil);
8179 img->data.lisp_val = Qnil;
8181 #if defined (HAVE_X_WINDOWS)
8183 /* On displays with a mutable colormap, figure out the colors
8184 allocated for the image by looking at the pixels of an XImage for
8185 img->pixmap. */
8186 class = FRAME_X_VISUAL (f)->class;
8187 if (class != StaticColor && class != StaticGray && class != TrueColor)
8189 XImagePtr ximg;
8191 BLOCK_INPUT;
8193 /* Try to get an XImage for img->pixmep. */
8194 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
8195 0, 0, img->width, img->height, ~0, ZPixmap);
8196 if (ximg)
8198 int x, y;
8200 /* Initialize the color table. */
8201 init_color_table ();
8203 /* For each pixel of the image, look its color up in the
8204 color table. After having done so, the color table will
8205 contain an entry for each color used by the image. */
8206 for (y = 0; y < img->height; ++y)
8207 for (x = 0; x < img->width; ++x)
8209 unsigned long pixel = XGetPixel (ximg, x, y);
8210 lookup_pixel_color (f, pixel);
8213 /* Record colors in the image. Free color table and XImage. */
8214 #ifdef COLOR_TABLE_SUPPORT
8215 img->colors = colors_in_color_table (&img->ncolors);
8216 free_color_table ();
8217 #endif
8218 XDestroyImage (ximg);
8220 #if 0 /* This doesn't seem to be the case. If we free the colors
8221 here, we get a BadAccess later in x_clear_image when
8222 freeing the colors. */
8223 /* We have allocated colors once, but Ghostscript has also
8224 allocated colors on behalf of us. So, to get the
8225 reference counts right, free them once. */
8226 if (img->ncolors)
8227 x_free_colors (f, img->colors, img->ncolors);
8228 #endif
8230 else
8231 image_error ("Cannot get X image of `%s'; colors will not be freed",
8232 img->spec, Qnil);
8234 UNBLOCK_INPUT;
8236 #endif /* HAVE_X_WINDOWS */
8238 /* Now that we have the pixmap, compute mask and transform the
8239 image if requested. */
8240 BLOCK_INPUT;
8241 postprocess_image (f, img);
8242 UNBLOCK_INPUT;
8245 #endif /* HAVE_GHOSTSCRIPT */
8248 /***********************************************************************
8249 Tests
8250 ***********************************************************************/
8252 #if GLYPH_DEBUG
8254 DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0,
8255 doc: /* Value is non-nil if SPEC is a valid image specification. */)
8256 (spec)
8257 Lisp_Object spec;
8259 return valid_image_p (spec) ? Qt : Qnil;
8263 DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
8264 (spec)
8265 Lisp_Object spec;
8267 int id = -1;
8269 if (valid_image_p (spec))
8270 id = lookup_image (SELECTED_FRAME (), spec);
8272 debug_print (spec);
8273 return make_number (id);
8276 #endif /* GLYPH_DEBUG != 0 */
8279 /***********************************************************************
8280 Initialization
8281 ***********************************************************************/
8283 #ifdef HAVE_NTGUI
8284 /* Image types that rely on external libraries are loaded dynamically
8285 if the library is available. */
8286 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8287 define_image_type (image_type, init_lib_fn (libraries))
8288 #else
8289 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
8290 define_image_type (image_type, 1)
8291 #endif /* HAVE_NTGUI */
8293 DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 2, 2, 0,
8294 doc: /* Initialize image library implementing image type TYPE.
8295 Return non-nil if TYPE is a supported image type.
8297 Image types pbm and xbm are prebuilt; other types are loaded here.
8298 Libraries to load are specified in alist LIBRARIES (usually, the value
8299 of `image-library-alist', which see). */)
8300 (type, libraries)
8301 Lisp_Object type, libraries;
8303 Lisp_Object tested;
8305 /* Don't try to reload the library. */
8306 tested = Fassq (type, Vimage_type_cache);
8307 if (CONSP (tested))
8308 return XCDR (tested);
8310 #if defined (HAVE_XPM) || defined (MAC_OS)
8311 if (EQ (type, Qxpm))
8312 return CHECK_LIB_AVAILABLE (&xpm_type, init_xpm_functions, libraries);
8313 #endif
8315 #if defined (HAVE_JPEG) || defined (MAC_OS)
8316 if (EQ (type, Qjpeg))
8317 return CHECK_LIB_AVAILABLE (&jpeg_type, init_jpeg_functions, libraries);
8318 #endif
8320 #if defined (HAVE_TIFF) || defined (MAC_OS)
8321 if (EQ (type, Qtiff))
8322 return CHECK_LIB_AVAILABLE (&tiff_type, init_tiff_functions, libraries);
8323 #endif
8325 #if defined (HAVE_GIF) || defined (MAC_OS)
8326 if (EQ (type, Qgif))
8327 return CHECK_LIB_AVAILABLE (&gif_type, init_gif_functions, libraries);
8328 #endif
8330 #if defined (HAVE_PNG) || defined (MAC_OS)
8331 if (EQ (type, Qpng))
8332 return CHECK_LIB_AVAILABLE (&png_type, init_png_functions, libraries);
8333 #endif
8335 #ifdef HAVE_GHOSTSCRIPT
8336 if (EQ (type, Qpostscript))
8337 return CHECK_LIB_AVAILABLE (&gs_type, init_gs_functions, libraries);
8338 #endif
8340 /* If the type is not recognized, avoid testing it ever again. */
8341 CACHE_IMAGE_TYPE (type, Qnil);
8342 return Qnil;
8345 void
8346 syms_of_image ()
8348 extern Lisp_Object Qrisky_local_variable; /* Syms_of_xdisp has already run. */
8350 /* Initialize this only once, since that's what we do with Vimage_types
8351 and they are supposed to be in sync. Initializing here gives correct
8352 operation on GNU/Linux of calling dump-emacs after loading some images. */
8353 image_types = NULL;
8355 /* Must be defined now becase we're going to update it below, while
8356 defining the supported image types. */
8357 DEFVAR_LISP ("image-types", &Vimage_types,
8358 doc: /* List of potentially supported image types.
8359 Each element of the list is a symbol for a image type, like 'jpeg or 'png.
8360 To check whether it is really supported, use `image-type-available-p'. */);
8361 Vimage_types = Qnil;
8363 DEFVAR_LISP ("image-library-alist", &Vimage_library_alist,
8364 doc: /* Alist of image types vs external libraries needed to display them.
8366 Each element is a list (IMAGE-TYPE LIBRARY...), where the car is a symbol
8367 representing a supported image type, and the rest are strings giving
8368 alternate filenames for the corresponding external libraries.
8370 Emacs tries to load the libraries in the order they appear on the
8371 list; if none is loaded, the running session of Emacs won't
8372 support the image type. Types 'pbm and 'xbm don't need to be
8373 listed; they're always supported. */);
8374 Vimage_library_alist = Qnil;
8375 Fput (intern ("image-library-alist"), Qrisky_local_variable, Qt);
8377 DEFVAR_LISP ("max-image-size", &Vmax_image_size,
8378 doc: /* Maximum size of images.
8379 Emacs will not load an image into memory if its pixel width or
8380 pixel height exceeds this limit.
8382 If the value is an integer, it directly specifies the maximum
8383 image height and width, measured in pixels. If it is a floating
8384 point number, it specifies the maximum image height and width
8385 as a ratio to the frame height and width. If the value is
8386 non-numeric, there is no explicit limit on the size of images. */);
8387 Vmax_image_size = make_float (MAX_IMAGE_SIZE);
8389 Vimage_type_cache = Qnil;
8390 staticpro (&Vimage_type_cache);
8392 Qpbm = intern ("pbm");
8393 staticpro (&Qpbm);
8394 ADD_IMAGE_TYPE(Qpbm);
8396 Qxbm = intern ("xbm");
8397 staticpro (&Qxbm);
8398 ADD_IMAGE_TYPE(Qxbm);
8400 define_image_type (&xbm_type, 1);
8401 define_image_type (&pbm_type, 1);
8403 QCascent = intern (":ascent");
8404 staticpro (&QCascent);
8405 QCmargin = intern (":margin");
8406 staticpro (&QCmargin);
8407 QCrelief = intern (":relief");
8408 staticpro (&QCrelief);
8409 QCconversion = intern (":conversion");
8410 staticpro (&QCconversion);
8411 QCcolor_symbols = intern (":color-symbols");
8412 staticpro (&QCcolor_symbols);
8413 QCheuristic_mask = intern (":heuristic-mask");
8414 staticpro (&QCheuristic_mask);
8415 QCindex = intern (":index");
8416 staticpro (&QCindex);
8417 QCmatrix = intern (":matrix");
8418 staticpro (&QCmatrix);
8419 QCcolor_adjustment = intern (":color-adjustment");
8420 staticpro (&QCcolor_adjustment);
8421 QCmask = intern (":mask");
8422 staticpro (&QCmask);
8424 Qlaplace = intern ("laplace");
8425 staticpro (&Qlaplace);
8426 Qemboss = intern ("emboss");
8427 staticpro (&Qemboss);
8428 Qedge_detection = intern ("edge-detection");
8429 staticpro (&Qedge_detection);
8430 Qheuristic = intern ("heuristic");
8431 staticpro (&Qheuristic);
8433 Qpostscript = intern ("postscript");
8434 staticpro (&Qpostscript);
8435 #ifdef HAVE_GHOSTSCRIPT
8436 ADD_IMAGE_TYPE(Qpostscript);
8437 QCloader = intern (":loader");
8438 staticpro (&QCloader);
8439 QCbounding_box = intern (":bounding-box");
8440 staticpro (&QCbounding_box);
8441 QCpt_width = intern (":pt-width");
8442 staticpro (&QCpt_width);
8443 QCpt_height = intern (":pt-height");
8444 staticpro (&QCpt_height);
8445 #endif /* HAVE_GHOSTSCRIPT */
8447 #if defined (HAVE_XPM) || defined (MAC_OS)
8448 Qxpm = intern ("xpm");
8449 staticpro (&Qxpm);
8450 ADD_IMAGE_TYPE(Qxpm);
8451 #endif
8453 #if defined (HAVE_JPEG) || defined (MAC_OS)
8454 Qjpeg = intern ("jpeg");
8455 staticpro (&Qjpeg);
8456 ADD_IMAGE_TYPE(Qjpeg);
8457 #endif
8459 #if defined (HAVE_TIFF) || defined (MAC_OS)
8460 Qtiff = intern ("tiff");
8461 staticpro (&Qtiff);
8462 ADD_IMAGE_TYPE(Qtiff);
8463 #endif
8465 #if defined (HAVE_GIF) || defined (MAC_OS)
8466 Qgif = intern ("gif");
8467 staticpro (&Qgif);
8468 ADD_IMAGE_TYPE(Qgif);
8469 #endif
8471 #if defined (HAVE_PNG) || defined (MAC_OS)
8472 Qpng = intern ("png");
8473 staticpro (&Qpng);
8474 ADD_IMAGE_TYPE(Qpng);
8475 #endif
8477 defsubr (&Sinit_image_library);
8478 defsubr (&Sclear_image_cache);
8479 defsubr (&Simage_size);
8480 defsubr (&Simage_mask_p);
8482 #if GLYPH_DEBUG
8483 defsubr (&Simagep);
8484 defsubr (&Slookup_image);
8485 #endif
8487 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
8488 doc: /* Non-nil means always draw a cross over disabled images.
8489 Disabled images are those having an `:conversion disabled' property.
8490 A cross is always drawn on black & white displays. */);
8491 cross_disabled_images = 0;
8493 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
8494 doc: /* List of directories to search for window system bitmap files. */);
8495 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
8497 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
8498 doc: /* Time after which cached images are removed from the cache.
8499 When an image has not been displayed this many seconds, remove it
8500 from the image cache. Value must be an integer or nil with nil
8501 meaning don't clear the cache. */);
8502 Vimage_cache_eviction_delay = make_number (30 * 60);
8505 void
8506 init_image ()
8508 #if defined (MAC_OSX) && TARGET_API_MAC_CARBON
8509 init_image_func_pointer ();
8510 #endif
8513 /* arch-tag: 123c2a5e-14a8-4c53-ab95-af47d7db49b9
8514 (do not change this comment) */