comparison src/xfns.c @ 83078:fac24544c283

Merged in changes from CVS HEAD Patches applied: * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-146 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-147 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-148 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-149 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-150 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-151 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-152 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-153 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-154 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-155 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-156 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-157 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-158 Update from CVS git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-118
author Karoly Lorentey <lorentey@elte.hu>
date Tue, 16 Mar 2004 20:27:22 +0000
parents b1f57ac99be5 4ab3a5beaea6
children 4970ad4995f5
comparison
equal deleted inserted replaced
83077:40fd42596e17 83078:fac24544c283
177 177
178 /* Non nil if no window manager is in use. */ 178 /* Non nil if no window manager is in use. */
179 179
180 Lisp_Object Vx_no_window_manager; 180 Lisp_Object Vx_no_window_manager;
181 181
182 /* Search path for bitmap files. */
183
184 Lisp_Object Vx_bitmap_file_path;
185
186 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */ 182 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
187 183
188 Lisp_Object Vx_pixel_size_width_font_regexp; 184 Lisp_Object Vx_pixel_size_width_font_regexp;
189 185
190 Lisp_Object Qnone; 186 Lisp_Object Qnone;
191 Lisp_Object Qsuppress_icon; 187 Lisp_Object Qsuppress_icon;
192 Lisp_Object Qundefined_color; 188 Lisp_Object Qundefined_color;
193 Lisp_Object Qcenter;
194 Lisp_Object Qcompound_text, Qcancel_timer; 189 Lisp_Object Qcompound_text, Qcancel_timer;
195 190
196 /* In dispnew.c */ 191 /* In dispnew.c */
197 192
198 extern Lisp_Object Vwindow_system_version; 193 extern Lisp_Object Vwindow_system_version;
519 } 514 }
520 #endif /* USE_X_TOOLKIT || USE_GTK */ 515 #endif /* USE_X_TOOLKIT || USE_GTK */
521 516
522 517
523 518
524 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
525 id, which is just an int that this section returns. Bitmaps are
526 reference counted so they can be shared among frames.
527
528 Bitmap indices are guaranteed to be > 0, so a negative number can
529 be used to indicate no bitmap.
530
531 If you use x_create_bitmap_from_data, then you must keep track of
532 the bitmaps yourself. That is, creating a bitmap from the same
533 data more than once will not be caught. */
534
535
536 /* Functions to access the contents of a bitmap, given an id. */
537
538 int
539 x_bitmap_height (f, id)
540 FRAME_PTR f;
541 int id;
542 {
543 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
544 }
545
546 int
547 x_bitmap_width (f, id)
548 FRAME_PTR f;
549 int id;
550 {
551 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
552 }
553
554 int
555 x_bitmap_pixmap (f, id)
556 FRAME_PTR f;
557 int id;
558 {
559 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
560 }
561
562 int
563 x_bitmap_mask (f, id)
564 FRAME_PTR f;
565 int id;
566 {
567 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].mask;
568 }
569
570
571 /* Allocate a new bitmap record. Returns index of new record. */
572
573 static int
574 x_allocate_bitmap_record (f)
575 FRAME_PTR f;
576 {
577 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
578 int i;
579
580 if (dpyinfo->bitmaps == NULL)
581 {
582 dpyinfo->bitmaps_size = 10;
583 dpyinfo->bitmaps
584 = (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
585 dpyinfo->bitmaps_last = 1;
586 return 1;
587 }
588
589 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
590 return ++dpyinfo->bitmaps_last;
591
592 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
593 if (dpyinfo->bitmaps[i].refcount == 0)
594 return i + 1;
595
596 dpyinfo->bitmaps_size *= 2;
597 dpyinfo->bitmaps
598 = (struct x_bitmap_record *) xrealloc (dpyinfo->bitmaps,
599 dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
600 return ++dpyinfo->bitmaps_last;
601 }
602
603 /* Add one reference to the reference count of the bitmap with id ID. */
604
605 void
606 x_reference_bitmap (f, id)
607 FRAME_PTR f;
608 int id;
609 {
610 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
611 }
612
613 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
614
615 int
616 x_create_bitmap_from_data (f, bits, width, height)
617 struct frame *f;
618 char *bits;
619 unsigned int width, height;
620 {
621 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
622 Pixmap bitmap;
623 int id;
624
625 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
626 bits, width, height);
627
628
629
630 if (! bitmap)
631 return -1;
632
633 id = x_allocate_bitmap_record (f);
634 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
635 dpyinfo->bitmaps[id - 1].have_mask = 0;
636 dpyinfo->bitmaps[id - 1].file = NULL;
637 dpyinfo->bitmaps[id - 1].refcount = 1;
638 dpyinfo->bitmaps[id - 1].depth = 1;
639 dpyinfo->bitmaps[id - 1].height = height;
640 dpyinfo->bitmaps[id - 1].width = width;
641
642 return id;
643 }
644
645 /* Create bitmap from file FILE for frame F. */
646
647 int
648 x_create_bitmap_from_file (f, file)
649 struct frame *f;
650 Lisp_Object file;
651 {
652 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
653 unsigned int width, height;
654 Pixmap bitmap;
655 int xhot, yhot, result, id;
656 Lisp_Object found;
657 int fd;
658 char *filename;
659
660 /* Look for an existing bitmap with the same name. */
661 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
662 {
663 if (dpyinfo->bitmaps[id].refcount
664 && dpyinfo->bitmaps[id].file
665 && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file)))
666 {
667 ++dpyinfo->bitmaps[id].refcount;
668 return id + 1;
669 }
670 }
671
672 /* Search bitmap-file-path for the file, if appropriate. */
673 fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil);
674 if (fd < 0)
675 return -1;
676 emacs_close (fd);
677
678 filename = (char *) SDATA (found);
679
680 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
681 filename, &width, &height, &bitmap, &xhot, &yhot);
682 if (result != BitmapSuccess)
683 return -1;
684
685 id = x_allocate_bitmap_record (f);
686 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
687 dpyinfo->bitmaps[id - 1].have_mask = 0;
688 dpyinfo->bitmaps[id - 1].refcount = 1;
689 dpyinfo->bitmaps[id - 1].file
690 = (char *) xmalloc (SBYTES (file) + 1);
691 dpyinfo->bitmaps[id - 1].depth = 1;
692 dpyinfo->bitmaps[id - 1].height = height;
693 dpyinfo->bitmaps[id - 1].width = width;
694 strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file));
695
696 return id;
697 }
698
699 /* Remove reference to bitmap with id number ID. */
700
701 void
702 x_destroy_bitmap (f, id)
703 FRAME_PTR f;
704 int id;
705 {
706 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
707
708 if (id > 0)
709 {
710 --dpyinfo->bitmaps[id - 1].refcount;
711 if (dpyinfo->bitmaps[id - 1].refcount == 0)
712 {
713 BLOCK_INPUT;
714 XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
715 if (dpyinfo->bitmaps[id - 1].have_mask)
716 XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].mask);
717 if (dpyinfo->bitmaps[id - 1].file)
718 {
719 xfree (dpyinfo->bitmaps[id - 1].file);
720 dpyinfo->bitmaps[id - 1].file = NULL;
721 }
722 UNBLOCK_INPUT;
723 }
724 }
725 }
726
727 /* Free all the bitmaps for the display specified by DPYINFO. */
728
729 static void
730 x_destroy_all_bitmaps (dpyinfo)
731 struct x_display_info *dpyinfo;
732 {
733 int i;
734 for (i = 0; i < dpyinfo->bitmaps_last; i++)
735 if (dpyinfo->bitmaps[i].refcount > 0)
736 {
737 XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].pixmap);
738 if (dpyinfo->bitmaps[i].have_mask)
739 XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].mask);
740 if (dpyinfo->bitmaps[i].file)
741 xfree (dpyinfo->bitmaps[i].file);
742 }
743 dpyinfo->bitmaps_last = 0;
744 }
745
746
747
748
749 /* Useful functions defined in the section
750 `Image type independent image structures' below. */
751
752 static unsigned long four_corners_best P_ ((XImage *ximg, unsigned long width,
753 unsigned long height));
754
755 static int x_create_x_image_and_pixmap P_ ((struct frame *f, int width, int height,
756 int depth, XImage **ximg,
757 Pixmap *pixmap));
758
759 static void x_destroy_x_image P_ ((XImage *ximg));
760
761
762 /* Create a mask of a bitmap. Note is this not a perfect mask.
763 It's nicer with some borders in this context */
764
765 int
766 x_create_bitmap_mask (f, id)
767 struct frame *f;
768 int id;
769 {
770 Pixmap pixmap, mask;
771 XImage *ximg, *mask_img;
772 unsigned long width, height;
773 int result;
774 unsigned long bg;
775 unsigned long x, y, xp, xm, yp, ym;
776 GC gc;
777
778 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
779
780 if (!(id > 0))
781 return -1;
782
783 pixmap = x_bitmap_pixmap (f, id);
784 width = x_bitmap_width (f, id);
785 height = x_bitmap_height (f, id);
786
787 BLOCK_INPUT;
788 ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height,
789 ~0, ZPixmap);
790
791 if (!ximg)
792 {
793 UNBLOCK_INPUT;
794 return -1;
795 }
796
797 result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask);
798
799 UNBLOCK_INPUT;
800 if (!result)
801 {
802 XDestroyImage (ximg);
803 return -1;
804 }
805
806 bg = four_corners_best (ximg, width, height);
807
808 for (y = 0; y < ximg->height; ++y)
809 {
810 for (x = 0; x < ximg->width; ++x)
811 {
812 xp = x != ximg->width - 1 ? x + 1 : 0;
813 xm = x != 0 ? x - 1 : ximg->width - 1;
814 yp = y != ximg->height - 1 ? y + 1 : 0;
815 ym = y != 0 ? y - 1 : ximg->height - 1;
816 if (XGetPixel (ximg, x, y) == bg
817 && XGetPixel (ximg, x, yp) == bg
818 && XGetPixel (ximg, x, ym) == bg
819 && XGetPixel (ximg, xp, y) == bg
820 && XGetPixel (ximg, xp, yp) == bg
821 && XGetPixel (ximg, xp, ym) == bg
822 && XGetPixel (ximg, xm, y) == bg
823 && XGetPixel (ximg, xm, yp) == bg
824 && XGetPixel (ximg, xm, ym) == bg)
825 XPutPixel (mask_img, x, y, 0);
826 else
827 XPutPixel (mask_img, x, y, 1);
828 }
829 }
830
831 xassert (interrupt_input_blocked);
832 gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL);
833 XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0,
834 width, height);
835 XFreeGC (FRAME_X_DISPLAY (f), gc);
836
837 dpyinfo->bitmaps[id - 1].have_mask = 1;
838 dpyinfo->bitmaps[id - 1].mask = mask;
839
840 XDestroyImage (ximg);
841 x_destroy_x_image (mask_img);
842
843 return 0;
844 }
845
846 static Lisp_Object unwind_create_frame P_ ((Lisp_Object)); 519 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
847 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object)); 520 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
848 static void x_disable_image P_ ((struct frame *, struct image *));
849 521
850 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object)); 522 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
851 static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object)); 523 static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
852 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object)); 524 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
853 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object)); 525 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
867 static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *, 539 static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
868 Lisp_Object, 540 Lisp_Object,
869 Lisp_Object, 541 Lisp_Object,
870 char *, char *, 542 char *, char *,
871 int)); 543 int));
872 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
873 Lisp_Object));
874 static void init_color_table P_ ((void));
875 static void free_color_table P_ ((void));
876 static unsigned long *colors_in_color_table P_ ((int *n));
877 static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
878 static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
879
880
881
882 544
883 545
884 /* Store the screen positions of frame F into XPTR and YPTR. 546 /* Store the screen positions of frame F into XPTR and YPTR.
885 These are the positions of the containing window manager window, 547 These are the positions of the containing window manager window,
886 not Emacs's own window. */ 548 not Emacs's own window. */
1097 { 759 {
1098 f->output_data.x->wait_for_wm = !NILP (new_value); 760 f->output_data.x->wait_for_wm = !NILP (new_value);
1099 } 761 }
1100 762
1101 #ifdef USE_GTK 763 #ifdef USE_GTK
1102
1103 static Lisp_Object x_find_image_file P_ ((Lisp_Object file));
1104 764
1105 /* Set icon from FILE for frame F. By using GTK functions the icon 765 /* Set icon from FILE for frame F. By using GTK functions the icon
1106 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */ 766 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
1107 767
1108 int 768 int
4298 UNBLOCK_INPUT; 3958 UNBLOCK_INPUT;
4299 } 3959 }
4300 3960
4301 3961
4302 /*********************************************************************** 3962 /***********************************************************************
4303 Image types
4304 ***********************************************************************/
4305
4306 /* Value is the number of elements of vector VECTOR. */
4307
4308 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
4309
4310 /* List of supported image types. Use define_image_type to add new
4311 types. Use lookup_image_type to find a type for a given symbol. */
4312
4313 static struct image_type *image_types;
4314
4315 /* The symbol `xbm' which is used as the type symbol for XBM images. */
4316
4317 Lisp_Object Qxbm;
4318
4319 /* Keywords. */
4320
4321 extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
4322 extern Lisp_Object QCdata, QCtype;
4323 Lisp_Object QCascent, QCmargin, QCrelief;
4324 Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
4325 Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
4326
4327 /* Other symbols. */
4328
4329 Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
4330
4331 /* Time in seconds after which images should be removed from the cache
4332 if not displayed. */
4333
4334 Lisp_Object Vimage_cache_eviction_delay;
4335
4336 /* Function prototypes. */
4337
4338 static void define_image_type P_ ((struct image_type *type));
4339 static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
4340 static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
4341 static void x_laplace P_ ((struct frame *, struct image *));
4342 static void x_emboss P_ ((struct frame *, struct image *));
4343 static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
4344 Lisp_Object));
4345
4346
4347 /* Define a new image type from TYPE. This adds a copy of TYPE to
4348 image_types and adds the symbol *TYPE->type to Vimage_types. */
4349
4350 static void
4351 define_image_type (type)
4352 struct image_type *type;
4353 {
4354 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
4355 The initialized data segment is read-only. */
4356 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
4357 bcopy (type, p, sizeof *p);
4358 p->next = image_types;
4359 image_types = p;
4360 Vimage_types = Fcons (*p->type, Vimage_types);
4361 }
4362
4363
4364 /* Look up image type SYMBOL, and return a pointer to its image_type
4365 structure. Value is null if SYMBOL is not a known image type. */
4366
4367 static INLINE struct image_type *
4368 lookup_image_type (symbol)
4369 Lisp_Object symbol;
4370 {
4371 struct image_type *type;
4372
4373 for (type = image_types; type; type = type->next)
4374 if (EQ (symbol, *type->type))
4375 break;
4376
4377 return type;
4378 }
4379
4380
4381 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
4382 valid image specification is a list whose car is the symbol
4383 `image', and whose rest is a property list. The property list must
4384 contain a value for key `:type'. That value must be the name of a
4385 supported image type. The rest of the property list depends on the
4386 image type. */
4387
4388 int
4389 valid_image_p (object)
4390 Lisp_Object object;
4391 {
4392 int valid_p = 0;
4393
4394 if (IMAGEP (object))
4395 {
4396 Lisp_Object tem;
4397
4398 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
4399 if (EQ (XCAR (tem), QCtype))
4400 {
4401 tem = XCDR (tem);
4402 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
4403 {
4404 struct image_type *type;
4405 type = lookup_image_type (XCAR (tem));
4406 if (type)
4407 valid_p = type->valid_p (object);
4408 }
4409
4410 break;
4411 }
4412 }
4413
4414 return valid_p;
4415 }
4416
4417
4418 /* Log error message with format string FORMAT and argument ARG.
4419 Signaling an error, e.g. when an image cannot be loaded, is not a
4420 good idea because this would interrupt redisplay, and the error
4421 message display would lead to another redisplay. This function
4422 therefore simply displays a message. */
4423
4424 static void
4425 image_error (format, arg1, arg2)
4426 char *format;
4427 Lisp_Object arg1, arg2;
4428 {
4429 add_to_log (format, arg1, arg2);
4430 }
4431
4432
4433
4434 /***********************************************************************
4435 Image specifications
4436 ***********************************************************************/
4437
4438 enum image_value_type
4439 {
4440 IMAGE_DONT_CHECK_VALUE_TYPE,
4441 IMAGE_STRING_VALUE,
4442 IMAGE_STRING_OR_NIL_VALUE,
4443 IMAGE_SYMBOL_VALUE,
4444 IMAGE_POSITIVE_INTEGER_VALUE,
4445 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
4446 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
4447 IMAGE_ASCENT_VALUE,
4448 IMAGE_INTEGER_VALUE,
4449 IMAGE_FUNCTION_VALUE,
4450 IMAGE_NUMBER_VALUE,
4451 IMAGE_BOOL_VALUE
4452 };
4453
4454 /* Structure used when parsing image specifications. */
4455
4456 struct image_keyword
4457 {
4458 /* Name of keyword. */
4459 char *name;
4460
4461 /* The type of value allowed. */
4462 enum image_value_type type;
4463
4464 /* Non-zero means key must be present. */
4465 int mandatory_p;
4466
4467 /* Used to recognize duplicate keywords in a property list. */
4468 int count;
4469
4470 /* The value that was found. */
4471 Lisp_Object value;
4472 };
4473
4474
4475 static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
4476 int, Lisp_Object));
4477 static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
4478
4479
4480 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
4481 has the format (image KEYWORD VALUE ...). One of the keyword/
4482 value pairs must be `:type TYPE'. KEYWORDS is a vector of
4483 image_keywords structures of size NKEYWORDS describing other
4484 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
4485
4486 static int
4487 parse_image_spec (spec, keywords, nkeywords, type)
4488 Lisp_Object spec;
4489 struct image_keyword *keywords;
4490 int nkeywords;
4491 Lisp_Object type;
4492 {
4493 int i;
4494 Lisp_Object plist;
4495
4496 if (!IMAGEP (spec))
4497 return 0;
4498
4499 plist = XCDR (spec);
4500 while (CONSP (plist))
4501 {
4502 Lisp_Object key, value;
4503
4504 /* First element of a pair must be a symbol. */
4505 key = XCAR (plist);
4506 plist = XCDR (plist);
4507 if (!SYMBOLP (key))
4508 return 0;
4509
4510 /* There must follow a value. */
4511 if (!CONSP (plist))
4512 return 0;
4513 value = XCAR (plist);
4514 plist = XCDR (plist);
4515
4516 /* Find key in KEYWORDS. Error if not found. */
4517 for (i = 0; i < nkeywords; ++i)
4518 if (strcmp (keywords[i].name, SDATA (SYMBOL_NAME (key))) == 0)
4519 break;
4520
4521 if (i == nkeywords)
4522 continue;
4523
4524 /* Record that we recognized the keyword. If a keywords
4525 was found more than once, it's an error. */
4526 keywords[i].value = value;
4527 ++keywords[i].count;
4528
4529 if (keywords[i].count > 1)
4530 return 0;
4531
4532 /* Check type of value against allowed type. */
4533 switch (keywords[i].type)
4534 {
4535 case IMAGE_STRING_VALUE:
4536 if (!STRINGP (value))
4537 return 0;
4538 break;
4539
4540 case IMAGE_STRING_OR_NIL_VALUE:
4541 if (!STRINGP (value) && !NILP (value))
4542 return 0;
4543 break;
4544
4545 case IMAGE_SYMBOL_VALUE:
4546 if (!SYMBOLP (value))
4547 return 0;
4548 break;
4549
4550 case IMAGE_POSITIVE_INTEGER_VALUE:
4551 if (!INTEGERP (value) || XINT (value) <= 0)
4552 return 0;
4553 break;
4554
4555 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
4556 if (INTEGERP (value) && XINT (value) >= 0)
4557 break;
4558 if (CONSP (value)
4559 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
4560 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
4561 break;
4562 return 0;
4563
4564 case IMAGE_ASCENT_VALUE:
4565 if (SYMBOLP (value) && EQ (value, Qcenter))
4566 break;
4567 else if (INTEGERP (value)
4568 && XINT (value) >= 0
4569 && XINT (value) <= 100)
4570 break;
4571 return 0;
4572
4573 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
4574 if (!INTEGERP (value) || XINT (value) < 0)
4575 return 0;
4576 break;
4577
4578 case IMAGE_DONT_CHECK_VALUE_TYPE:
4579 break;
4580
4581 case IMAGE_FUNCTION_VALUE:
4582 value = indirect_function (value);
4583 if (SUBRP (value)
4584 || COMPILEDP (value)
4585 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
4586 break;
4587 return 0;
4588
4589 case IMAGE_NUMBER_VALUE:
4590 if (!INTEGERP (value) && !FLOATP (value))
4591 return 0;
4592 break;
4593
4594 case IMAGE_INTEGER_VALUE:
4595 if (!INTEGERP (value))
4596 return 0;
4597 break;
4598
4599 case IMAGE_BOOL_VALUE:
4600 if (!NILP (value) && !EQ (value, Qt))
4601 return 0;
4602 break;
4603
4604 default:
4605 abort ();
4606 break;
4607 }
4608
4609 if (EQ (key, QCtype) && !EQ (type, value))
4610 return 0;
4611 }
4612
4613 /* Check that all mandatory fields are present. */
4614 for (i = 0; i < nkeywords; ++i)
4615 if (keywords[i].mandatory_p && keywords[i].count == 0)
4616 return 0;
4617
4618 return NILP (plist);
4619 }
4620
4621
4622 /* Return the value of KEY in image specification SPEC. Value is nil
4623 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
4624 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
4625
4626 static Lisp_Object
4627 image_spec_value (spec, key, found)
4628 Lisp_Object spec, key;
4629 int *found;
4630 {
4631 Lisp_Object tail;
4632
4633 xassert (valid_image_p (spec));
4634
4635 for (tail = XCDR (spec);
4636 CONSP (tail) && CONSP (XCDR (tail));
4637 tail = XCDR (XCDR (tail)))
4638 {
4639 if (EQ (XCAR (tail), key))
4640 {
4641 if (found)
4642 *found = 1;
4643 return XCAR (XCDR (tail));
4644 }
4645 }
4646
4647 if (found)
4648 *found = 0;
4649 return Qnil;
4650 }
4651
4652
4653 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
4654 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
4655 PIXELS non-nil means return the size in pixels, otherwise return the
4656 size in canonical character units.
4657 FRAME is the frame on which the image will be displayed. FRAME nil
4658 or omitted means use the selected frame. */)
4659 (spec, pixels, frame)
4660 Lisp_Object spec, pixels, frame;
4661 {
4662 Lisp_Object size;
4663
4664 size = Qnil;
4665 if (valid_image_p (spec))
4666 {
4667 struct frame *f = check_x_frame (frame);
4668 int id = lookup_image (f, spec);
4669 struct image *img = IMAGE_FROM_ID (f, id);
4670 int width = img->width + 2 * img->hmargin;
4671 int height = img->height + 2 * img->vmargin;
4672
4673 if (NILP (pixels))
4674 size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
4675 make_float ((double) height / FRAME_LINE_HEIGHT (f)));
4676 else
4677 size = Fcons (make_number (width), make_number (height));
4678 }
4679 else
4680 error ("Invalid image specification");
4681
4682 return size;
4683 }
4684
4685
4686 DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
4687 doc: /* Return t if image SPEC has a mask bitmap.
4688 FRAME is the frame on which the image will be displayed. FRAME nil
4689 or omitted means use the selected frame. */)
4690 (spec, frame)
4691 Lisp_Object spec, frame;
4692 {
4693 Lisp_Object mask;
4694
4695 mask = Qnil;
4696 if (valid_image_p (spec))
4697 {
4698 struct frame *f = check_x_frame (frame);
4699 int id = lookup_image (f, spec);
4700 struct image *img = IMAGE_FROM_ID (f, id);
4701 if (img->mask)
4702 mask = Qt;
4703 }
4704 else
4705 error ("Invalid image specification");
4706
4707 return mask;
4708 }
4709
4710
4711
4712 /***********************************************************************
4713 Image type independent image structures
4714 ***********************************************************************/
4715
4716 static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
4717 static void free_image P_ ((struct frame *f, struct image *img));
4718
4719
4720 /* Allocate and return a new image structure for image specification
4721 SPEC. SPEC has a hash value of HASH. */
4722
4723 static struct image *
4724 make_image (spec, hash)
4725 Lisp_Object spec;
4726 unsigned hash;
4727 {
4728 struct image *img = (struct image *) xmalloc (sizeof *img);
4729
4730 xassert (valid_image_p (spec));
4731 bzero (img, sizeof *img);
4732 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
4733 xassert (img->type != NULL);
4734 img->spec = spec;
4735 img->data.lisp_val = Qnil;
4736 img->ascent = DEFAULT_IMAGE_ASCENT;
4737 img->hash = hash;
4738 return img;
4739 }
4740
4741
4742 /* Free image IMG which was used on frame F, including its resources. */
4743
4744 static void
4745 free_image (f, img)
4746 struct frame *f;
4747 struct image *img;
4748 {
4749 if (img)
4750 {
4751 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
4752
4753 /* Remove IMG from the hash table of its cache. */
4754 if (img->prev)
4755 img->prev->next = img->next;
4756 else
4757 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
4758
4759 if (img->next)
4760 img->next->prev = img->prev;
4761
4762 c->images[img->id] = NULL;
4763
4764 /* Free resources, then free IMG. */
4765 img->type->free (f, img);
4766 xfree (img);
4767 }
4768 }
4769
4770
4771 /* Prepare image IMG for display on frame F. Must be called before
4772 drawing an image. */
4773
4774 void
4775 prepare_image_for_display (f, img)
4776 struct frame *f;
4777 struct image *img;
4778 {
4779 EMACS_TIME t;
4780
4781 /* We're about to display IMG, so set its timestamp to `now'. */
4782 EMACS_GET_TIME (t);
4783 img->timestamp = EMACS_SECS (t);
4784
4785 /* If IMG doesn't have a pixmap yet, load it now, using the image
4786 type dependent loader function. */
4787 if (img->pixmap == None && !img->load_failed_p)
4788 img->load_failed_p = img->type->load (f, img) == 0;
4789 }
4790
4791
4792 /* Value is the number of pixels for the ascent of image IMG when
4793 drawn in face FACE. */
4794
4795 int
4796 image_ascent (img, face)
4797 struct image *img;
4798 struct face *face;
4799 {
4800 int height = img->height + img->vmargin;
4801 int ascent;
4802
4803 if (img->ascent == CENTERED_IMAGE_ASCENT)
4804 {
4805 if (face->font)
4806 /* This expression is arranged so that if the image can't be
4807 exactly centered, it will be moved slightly up. This is
4808 because a typical font is `top-heavy' (due to the presence
4809 uppercase letters), so the image placement should err towards
4810 being top-heavy too. It also just generally looks better. */
4811 ascent = (height + face->font->ascent - face->font->descent + 1) / 2;
4812 else
4813 ascent = height / 2;
4814 }
4815 else
4816 ascent = height * img->ascent / 100.0;
4817
4818 return ascent;
4819 }
4820
4821
4822 /* Image background colors. */
4823
4824 static unsigned long
4825 four_corners_best (ximg, width, height)
4826 XImage *ximg;
4827 unsigned long width, height;
4828 {
4829 unsigned long corners[4], best;
4830 int i, best_count;
4831
4832 /* Get the colors at the corners of ximg. */
4833 corners[0] = XGetPixel (ximg, 0, 0);
4834 corners[1] = XGetPixel (ximg, width - 1, 0);
4835 corners[2] = XGetPixel (ximg, width - 1, height - 1);
4836 corners[3] = XGetPixel (ximg, 0, height - 1);
4837
4838 /* Choose the most frequently found color as background. */
4839 for (i = best_count = 0; i < 4; ++i)
4840 {
4841 int j, n;
4842
4843 for (j = n = 0; j < 4; ++j)
4844 if (corners[i] == corners[j])
4845 ++n;
4846
4847 if (n > best_count)
4848 best = corners[i], best_count = n;
4849 }
4850
4851 return best;
4852 }
4853
4854 /* Return the `background' field of IMG. If IMG doesn't have one yet,
4855 it is guessed heuristically. If non-zero, XIMG is an existing XImage
4856 object to use for the heuristic. */
4857
4858 unsigned long
4859 image_background (img, f, ximg)
4860 struct image *img;
4861 struct frame *f;
4862 XImage *ximg;
4863 {
4864 if (! img->background_valid)
4865 /* IMG doesn't have a background yet, try to guess a reasonable value. */
4866 {
4867 int free_ximg = !ximg;
4868
4869 if (! ximg)
4870 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
4871 0, 0, img->width, img->height, ~0, ZPixmap);
4872
4873 img->background = four_corners_best (ximg, img->width, img->height);
4874
4875 if (free_ximg)
4876 XDestroyImage (ximg);
4877
4878 img->background_valid = 1;
4879 }
4880
4881 return img->background;
4882 }
4883
4884 /* Return the `background_transparent' field of IMG. If IMG doesn't
4885 have one yet, it is guessed heuristically. If non-zero, MASK is an
4886 existing XImage object to use for the heuristic. */
4887
4888 int
4889 image_background_transparent (img, f, mask)
4890 struct image *img;
4891 struct frame *f;
4892 XImage *mask;
4893 {
4894 if (! img->background_transparent_valid)
4895 /* IMG doesn't have a background yet, try to guess a reasonable value. */
4896 {
4897 if (img->mask)
4898 {
4899 int free_mask = !mask;
4900
4901 if (! mask)
4902 mask = XGetImage (FRAME_X_DISPLAY (f), img->mask,
4903 0, 0, img->width, img->height, ~0, ZPixmap);
4904
4905 img->background_transparent
4906 = !four_corners_best (mask, img->width, img->height);
4907
4908 if (free_mask)
4909 XDestroyImage (mask);
4910 }
4911 else
4912 img->background_transparent = 0;
4913
4914 img->background_transparent_valid = 1;
4915 }
4916
4917 return img->background_transparent;
4918 }
4919
4920
4921 /***********************************************************************
4922 Helper functions for X image types
4923 ***********************************************************************/
4924
4925 static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
4926 int, int));
4927 static void x_clear_image P_ ((struct frame *f, struct image *img));
4928 static unsigned long x_alloc_image_color P_ ((struct frame *f,
4929 struct image *img,
4930 Lisp_Object color_name,
4931 unsigned long dflt));
4932
4933
4934 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
4935 free the pixmap if any. MASK_P non-zero means clear the mask
4936 pixmap if any. COLORS_P non-zero means free colors allocated for
4937 the image, if any. */
4938
4939 static void
4940 x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
4941 struct frame *f;
4942 struct image *img;
4943 int pixmap_p, mask_p, colors_p;
4944 {
4945 if (pixmap_p && img->pixmap)
4946 {
4947 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
4948 img->pixmap = None;
4949 img->background_valid = 0;
4950 }
4951
4952 if (mask_p && img->mask)
4953 {
4954 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
4955 img->mask = None;
4956 img->background_transparent_valid = 0;
4957 }
4958
4959 if (colors_p && img->ncolors)
4960 {
4961 x_free_colors (f, img->colors, img->ncolors);
4962 xfree (img->colors);
4963 img->colors = NULL;
4964 img->ncolors = 0;
4965 }
4966 }
4967
4968 /* Free X resources of image IMG which is used on frame F. */
4969
4970 static void
4971 x_clear_image (f, img)
4972 struct frame *f;
4973 struct image *img;
4974 {
4975 BLOCK_INPUT;
4976 x_clear_image_1 (f, img, 1, 1, 1);
4977 UNBLOCK_INPUT;
4978 }
4979
4980
4981 /* Allocate color COLOR_NAME for image IMG on frame F. If color
4982 cannot be allocated, use DFLT. Add a newly allocated color to
4983 IMG->colors, so that it can be freed again. Value is the pixel
4984 color. */
4985
4986 static unsigned long
4987 x_alloc_image_color (f, img, color_name, dflt)
4988 struct frame *f;
4989 struct image *img;
4990 Lisp_Object color_name;
4991 unsigned long dflt;
4992 {
4993 XColor color;
4994 unsigned long result;
4995
4996 xassert (STRINGP (color_name));
4997
4998 if (x_defined_color (f, SDATA (color_name), &color, 1))
4999 {
5000 /* This isn't called frequently so we get away with simply
5001 reallocating the color vector to the needed size, here. */
5002 ++img->ncolors;
5003 img->colors =
5004 (unsigned long *) xrealloc (img->colors,
5005 img->ncolors * sizeof *img->colors);
5006 img->colors[img->ncolors - 1] = color.pixel;
5007 result = color.pixel;
5008 }
5009 else
5010 result = dflt;
5011
5012 return result;
5013 }
5014
5015
5016
5017 /***********************************************************************
5018 Image Cache
5019 ***********************************************************************/
5020
5021 static void cache_image P_ ((struct frame *f, struct image *img));
5022 static void postprocess_image P_ ((struct frame *, struct image *));
5023
5024
5025 /* Return a new, initialized image cache that is allocated from the
5026 heap. Call free_image_cache to free an image cache. */
5027
5028 struct image_cache *
5029 make_image_cache ()
5030 {
5031 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
5032 int size;
5033
5034 bzero (c, sizeof *c);
5035 c->size = 50;
5036 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
5037 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5038 c->buckets = (struct image **) xmalloc (size);
5039 bzero (c->buckets, size);
5040 return c;
5041 }
5042
5043
5044 /* Free image cache of frame F. Be aware that X frames share images
5045 caches. */
5046
5047 void
5048 free_image_cache (f)
5049 struct frame *f;
5050 {
5051 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5052 if (c)
5053 {
5054 int i;
5055
5056 /* Cache should not be referenced by any frame when freed. */
5057 xassert (c->refcount == 0);
5058
5059 for (i = 0; i < c->used; ++i)
5060 free_image (f, c->images[i]);
5061 xfree (c->images);
5062 xfree (c->buckets);
5063 xfree (c);
5064 FRAME_X_IMAGE_CACHE (f) = NULL;
5065 }
5066 }
5067
5068
5069 /* Clear image cache of frame F. FORCE_P non-zero means free all
5070 images. FORCE_P zero means clear only images that haven't been
5071 displayed for some time. Should be called from time to time to
5072 reduce the number of loaded images. If image-eviction-seconds is
5073 non-nil, this frees images in the cache which weren't displayed for
5074 at least that many seconds. */
5075
5076 void
5077 clear_image_cache (f, force_p)
5078 struct frame *f;
5079 int force_p;
5080 {
5081 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5082
5083 if (c && INTEGERP (Vimage_cache_eviction_delay))
5084 {
5085 EMACS_TIME t;
5086 unsigned long old;
5087 int i, nfreed;
5088
5089 EMACS_GET_TIME (t);
5090 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
5091
5092 /* Block input so that we won't be interrupted by a SIGIO
5093 while being in an inconsistent state. */
5094 BLOCK_INPUT;
5095
5096 for (i = nfreed = 0; i < c->used; ++i)
5097 {
5098 struct image *img = c->images[i];
5099 if (img != NULL
5100 && (force_p || img->timestamp < old))
5101 {
5102 free_image (f, img);
5103 ++nfreed;
5104 }
5105 }
5106
5107 /* We may be clearing the image cache because, for example,
5108 Emacs was iconified for a longer period of time. In that
5109 case, current matrices may still contain references to
5110 images freed above. So, clear these matrices. */
5111 if (nfreed)
5112 {
5113 Lisp_Object tail, frame;
5114
5115 FOR_EACH_FRAME (tail, frame)
5116 {
5117 struct frame *f = XFRAME (frame);
5118 if (FRAME_X_P (f)
5119 && FRAME_X_IMAGE_CACHE (f) == c)
5120 clear_current_matrices (f);
5121 }
5122
5123 ++windows_or_buffers_changed;
5124 }
5125
5126 UNBLOCK_INPUT;
5127 }
5128 }
5129
5130
5131 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
5132 0, 1, 0,
5133 doc: /* Clear the image cache of FRAME.
5134 FRAME nil or omitted means use the selected frame.
5135 FRAME t means clear the image caches of all frames. */)
5136 (frame)
5137 Lisp_Object frame;
5138 {
5139 if (EQ (frame, Qt))
5140 {
5141 Lisp_Object tail;
5142
5143 FOR_EACH_FRAME (tail, frame)
5144 if (FRAME_X_P (XFRAME (frame)))
5145 clear_image_cache (XFRAME (frame), 1);
5146 }
5147 else
5148 clear_image_cache (check_x_frame (frame), 1);
5149
5150 return Qnil;
5151 }
5152
5153
5154 /* Compute masks and transform image IMG on frame F, as specified
5155 by the image's specification, */
5156
5157 static void
5158 postprocess_image (f, img)
5159 struct frame *f;
5160 struct image *img;
5161 {
5162 /* Manipulation of the image's mask. */
5163 if (img->pixmap)
5164 {
5165 Lisp_Object conversion, spec;
5166 Lisp_Object mask;
5167
5168 spec = img->spec;
5169
5170 /* `:heuristic-mask t'
5171 `:mask heuristic'
5172 means build a mask heuristically.
5173 `:heuristic-mask (R G B)'
5174 `:mask (heuristic (R G B))'
5175 means build a mask from color (R G B) in the
5176 image.
5177 `:mask nil'
5178 means remove a mask, if any. */
5179
5180 mask = image_spec_value (spec, QCheuristic_mask, NULL);
5181 if (!NILP (mask))
5182 x_build_heuristic_mask (f, img, mask);
5183 else
5184 {
5185 int found_p;
5186
5187 mask = image_spec_value (spec, QCmask, &found_p);
5188
5189 if (EQ (mask, Qheuristic))
5190 x_build_heuristic_mask (f, img, Qt);
5191 else if (CONSP (mask)
5192 && EQ (XCAR (mask), Qheuristic))
5193 {
5194 if (CONSP (XCDR (mask)))
5195 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
5196 else
5197 x_build_heuristic_mask (f, img, XCDR (mask));
5198 }
5199 else if (NILP (mask) && found_p && img->mask)
5200 {
5201 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
5202 img->mask = None;
5203 }
5204 }
5205
5206
5207 /* Should we apply an image transformation algorithm? */
5208 conversion = image_spec_value (spec, QCconversion, NULL);
5209 if (EQ (conversion, Qdisabled))
5210 x_disable_image (f, img);
5211 else if (EQ (conversion, Qlaplace))
5212 x_laplace (f, img);
5213 else if (EQ (conversion, Qemboss))
5214 x_emboss (f, img);
5215 else if (CONSP (conversion)
5216 && EQ (XCAR (conversion), Qedge_detection))
5217 {
5218 Lisp_Object tem;
5219 tem = XCDR (conversion);
5220 if (CONSP (tem))
5221 x_edge_detection (f, img,
5222 Fplist_get (tem, QCmatrix),
5223 Fplist_get (tem, QCcolor_adjustment));
5224 }
5225 }
5226 }
5227
5228
5229 /* Return the id of image with Lisp specification SPEC on frame F.
5230 SPEC must be a valid Lisp image specification (see valid_image_p). */
5231
5232 int
5233 lookup_image (f, spec)
5234 struct frame *f;
5235 Lisp_Object spec;
5236 {
5237 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5238 struct image *img;
5239 int i;
5240 unsigned hash;
5241 struct gcpro gcpro1;
5242 EMACS_TIME now;
5243
5244 /* F must be a window-system frame, and SPEC must be a valid image
5245 specification. */
5246 xassert (FRAME_WINDOW_P (f));
5247 xassert (valid_image_p (spec));
5248
5249 GCPRO1 (spec);
5250
5251 /* Look up SPEC in the hash table of the image cache. */
5252 hash = sxhash (spec, 0);
5253 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
5254
5255 for (img = c->buckets[i]; img; img = img->next)
5256 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
5257 break;
5258
5259 /* If not found, create a new image and cache it. */
5260 if (img == NULL)
5261 {
5262 extern Lisp_Object Qpostscript;
5263
5264 BLOCK_INPUT;
5265 img = make_image (spec, hash);
5266 cache_image (f, img);
5267 img->load_failed_p = img->type->load (f, img) == 0;
5268
5269 /* If we can't load the image, and we don't have a width and
5270 height, use some arbitrary width and height so that we can
5271 draw a rectangle for it. */
5272 if (img->load_failed_p)
5273 {
5274 Lisp_Object value;
5275
5276 value = image_spec_value (spec, QCwidth, NULL);
5277 img->width = (INTEGERP (value)
5278 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
5279 value = image_spec_value (spec, QCheight, NULL);
5280 img->height = (INTEGERP (value)
5281 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
5282 }
5283 else
5284 {
5285 /* Handle image type independent image attributes
5286 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
5287 `:background COLOR'. */
5288 Lisp_Object ascent, margin, relief, bg;
5289
5290 ascent = image_spec_value (spec, QCascent, NULL);
5291 if (INTEGERP (ascent))
5292 img->ascent = XFASTINT (ascent);
5293 else if (EQ (ascent, Qcenter))
5294 img->ascent = CENTERED_IMAGE_ASCENT;
5295
5296 margin = image_spec_value (spec, QCmargin, NULL);
5297 if (INTEGERP (margin) && XINT (margin) >= 0)
5298 img->vmargin = img->hmargin = XFASTINT (margin);
5299 else if (CONSP (margin) && INTEGERP (XCAR (margin))
5300 && INTEGERP (XCDR (margin)))
5301 {
5302 if (XINT (XCAR (margin)) > 0)
5303 img->hmargin = XFASTINT (XCAR (margin));
5304 if (XINT (XCDR (margin)) > 0)
5305 img->vmargin = XFASTINT (XCDR (margin));
5306 }
5307
5308 relief = image_spec_value (spec, QCrelief, NULL);
5309 if (INTEGERP (relief))
5310 {
5311 img->relief = XINT (relief);
5312 img->hmargin += abs (img->relief);
5313 img->vmargin += abs (img->relief);
5314 }
5315
5316 if (! img->background_valid)
5317 {
5318 bg = image_spec_value (img->spec, QCbackground, NULL);
5319 if (!NILP (bg))
5320 {
5321 img->background
5322 = x_alloc_image_color (f, img, bg,
5323 FRAME_BACKGROUND_PIXEL (f));
5324 img->background_valid = 1;
5325 }
5326 }
5327
5328 /* Do image transformations and compute masks, unless we
5329 don't have the image yet. */
5330 if (!EQ (*img->type->type, Qpostscript))
5331 postprocess_image (f, img);
5332 }
5333
5334 UNBLOCK_INPUT;
5335 }
5336
5337 /* We're using IMG, so set its timestamp to `now'. */
5338 EMACS_GET_TIME (now);
5339 img->timestamp = EMACS_SECS (now);
5340
5341 UNGCPRO;
5342
5343 /* Value is the image id. */
5344 return img->id;
5345 }
5346
5347
5348 /* Cache image IMG in the image cache of frame F. */
5349
5350 static void
5351 cache_image (f, img)
5352 struct frame *f;
5353 struct image *img;
5354 {
5355 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5356 int i;
5357
5358 /* Find a free slot in c->images. */
5359 for (i = 0; i < c->used; ++i)
5360 if (c->images[i] == NULL)
5361 break;
5362
5363 /* If no free slot found, maybe enlarge c->images. */
5364 if (i == c->used && c->used == c->size)
5365 {
5366 c->size *= 2;
5367 c->images = (struct image **) xrealloc (c->images,
5368 c->size * sizeof *c->images);
5369 }
5370
5371 /* Add IMG to c->images, and assign IMG an id. */
5372 c->images[i] = img;
5373 img->id = i;
5374 if (i == c->used)
5375 ++c->used;
5376
5377 /* Add IMG to the cache's hash table. */
5378 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
5379 img->next = c->buckets[i];
5380 if (img->next)
5381 img->next->prev = img;
5382 img->prev = NULL;
5383 c->buckets[i] = img;
5384 }
5385
5386
5387 /* Call FN on every image in the image cache of frame F. Used to mark
5388 Lisp Objects in the image cache. */
5389
5390 void
5391 forall_images_in_image_cache (f, fn)
5392 struct frame *f;
5393 void (*fn) P_ ((struct image *img));
5394 {
5395 if (FRAME_LIVE_P (f) && FRAME_X_P (f))
5396 {
5397 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5398 if (c)
5399 {
5400 int i;
5401 for (i = 0; i < c->used; ++i)
5402 if (c->images[i])
5403 fn (c->images[i]);
5404 }
5405 }
5406 }
5407
5408
5409
5410 /***********************************************************************
5411 X support code
5412 ***********************************************************************/
5413
5414 static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
5415 XImage **, Pixmap *));
5416 static void x_destroy_x_image P_ ((XImage *));
5417 static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
5418
5419
5420 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
5421 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
5422 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
5423 via xmalloc. Print error messages via image_error if an error
5424 occurs. Value is non-zero if successful. */
5425
5426 static int
5427 x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
5428 struct frame *f;
5429 int width, height, depth;
5430 XImage **ximg;
5431 Pixmap *pixmap;
5432 {
5433 Display *display = FRAME_X_DISPLAY (f);
5434 Screen *screen = FRAME_X_SCREEN (f);
5435 Window window = FRAME_X_WINDOW (f);
5436
5437 xassert (interrupt_input_blocked);
5438
5439 if (depth <= 0)
5440 depth = DefaultDepthOfScreen (screen);
5441 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
5442 depth, ZPixmap, 0, NULL, width, height,
5443 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
5444 if (*ximg == NULL)
5445 {
5446 image_error ("Unable to allocate X image", Qnil, Qnil);
5447 return 0;
5448 }
5449
5450 /* Allocate image raster. */
5451 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
5452
5453 /* Allocate a pixmap of the same size. */
5454 *pixmap = XCreatePixmap (display, window, width, height, depth);
5455 if (*pixmap == None)
5456 {
5457 x_destroy_x_image (*ximg);
5458 *ximg = NULL;
5459 image_error ("Unable to create X pixmap", Qnil, Qnil);
5460 return 0;
5461 }
5462
5463 return 1;
5464 }
5465
5466
5467 /* Destroy XImage XIMG. Free XIMG->data. */
5468
5469 static void
5470 x_destroy_x_image (ximg)
5471 XImage *ximg;
5472 {
5473 xassert (interrupt_input_blocked);
5474 if (ximg)
5475 {
5476 xfree (ximg->data);
5477 ximg->data = NULL;
5478 XDestroyImage (ximg);
5479 }
5480 }
5481
5482
5483 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
5484 are width and height of both the image and pixmap. */
5485
5486 static void
5487 x_put_x_image (f, ximg, pixmap, width, height)
5488 struct frame *f;
5489 XImage *ximg;
5490 Pixmap pixmap;
5491 int width, height;
5492 {
5493 GC gc;
5494
5495 xassert (interrupt_input_blocked);
5496 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
5497 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
5498 XFreeGC (FRAME_X_DISPLAY (f), gc);
5499 }
5500
5501
5502
5503 /***********************************************************************
5504 File Handling
5505 ***********************************************************************/
5506
5507 static Lisp_Object x_find_image_file P_ ((Lisp_Object));
5508 static char *slurp_file P_ ((char *, int *));
5509
5510
5511 /* Find image file FILE. Look in data-directory, then
5512 x-bitmap-file-path. Value is the full name of the file found, or
5513 nil if not found. */
5514
5515 static Lisp_Object
5516 x_find_image_file (file)
5517 Lisp_Object file;
5518 {
5519 Lisp_Object file_found, search_path;
5520 struct gcpro gcpro1, gcpro2;
5521 int fd;
5522
5523 file_found = Qnil;
5524 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
5525 GCPRO2 (file_found, search_path);
5526
5527 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
5528 fd = openp (search_path, file, Qnil, &file_found, Qnil);
5529
5530 if (fd == -1)
5531 file_found = Qnil;
5532 else
5533 close (fd);
5534
5535 UNGCPRO;
5536 return file_found;
5537 }
5538
5539
5540 /* Read FILE into memory. Value is a pointer to a buffer allocated
5541 with xmalloc holding FILE's contents. Value is null if an error
5542 occurred. *SIZE is set to the size of the file. */
5543
5544 static char *
5545 slurp_file (file, size)
5546 char *file;
5547 int *size;
5548 {
5549 FILE *fp = NULL;
5550 char *buf = NULL;
5551 struct stat st;
5552
5553 if (stat (file, &st) == 0
5554 && (fp = fopen (file, "r")) != NULL
5555 && (buf = (char *) xmalloc (st.st_size),
5556 fread (buf, 1, st.st_size, fp) == st.st_size))
5557 {
5558 *size = st.st_size;
5559 fclose (fp);
5560 }
5561 else
5562 {
5563 if (fp)
5564 fclose (fp);
5565 if (buf)
5566 {
5567 xfree (buf);
5568 buf = NULL;
5569 }
5570 }
5571
5572 return buf;
5573 }
5574
5575
5576
5577 /***********************************************************************
5578 XBM images
5579 ***********************************************************************/
5580
5581 static int xbm_scan P_ ((char **, char *, char *, int *));
5582 static int xbm_load P_ ((struct frame *f, struct image *img));
5583 static int xbm_load_image P_ ((struct frame *f, struct image *img,
5584 char *, char *));
5585 static int xbm_image_p P_ ((Lisp_Object object));
5586 static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
5587 unsigned char **));
5588 static int xbm_file_p P_ ((Lisp_Object));
5589
5590
5591 /* Indices of image specification fields in xbm_format, below. */
5592
5593 enum xbm_keyword_index
5594 {
5595 XBM_TYPE,
5596 XBM_FILE,
5597 XBM_WIDTH,
5598 XBM_HEIGHT,
5599 XBM_DATA,
5600 XBM_FOREGROUND,
5601 XBM_BACKGROUND,
5602 XBM_ASCENT,
5603 XBM_MARGIN,
5604 XBM_RELIEF,
5605 XBM_ALGORITHM,
5606 XBM_HEURISTIC_MASK,
5607 XBM_MASK,
5608 XBM_LAST
5609 };
5610
5611 /* Vector of image_keyword structures describing the format
5612 of valid XBM image specifications. */
5613
5614 static struct image_keyword xbm_format[XBM_LAST] =
5615 {
5616 {":type", IMAGE_SYMBOL_VALUE, 1},
5617 {":file", IMAGE_STRING_VALUE, 0},
5618 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
5619 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
5620 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5621 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
5622 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
5623 {":ascent", IMAGE_ASCENT_VALUE, 0},
5624 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
5625 {":relief", IMAGE_INTEGER_VALUE, 0},
5626 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5627 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5628 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
5629 };
5630
5631 /* Structure describing the image type XBM. */
5632
5633 static struct image_type xbm_type =
5634 {
5635 &Qxbm,
5636 xbm_image_p,
5637 xbm_load,
5638 x_clear_image,
5639 NULL
5640 };
5641
5642 /* Tokens returned from xbm_scan. */
5643
5644 enum xbm_token
5645 {
5646 XBM_TK_IDENT = 256,
5647 XBM_TK_NUMBER
5648 };
5649
5650
5651 /* Return non-zero if OBJECT is a valid XBM-type image specification.
5652 A valid specification is a list starting with the symbol `image'
5653 The rest of the list is a property list which must contain an
5654 entry `:type xbm..
5655
5656 If the specification specifies a file to load, it must contain
5657 an entry `:file FILENAME' where FILENAME is a string.
5658
5659 If the specification is for a bitmap loaded from memory it must
5660 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
5661 WIDTH and HEIGHT are integers > 0. DATA may be:
5662
5663 1. a string large enough to hold the bitmap data, i.e. it must
5664 have a size >= (WIDTH + 7) / 8 * HEIGHT
5665
5666 2. a bool-vector of size >= WIDTH * HEIGHT
5667
5668 3. a vector of strings or bool-vectors, one for each line of the
5669 bitmap.
5670
5671 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
5672 may not be specified in this case because they are defined in the
5673 XBM file.
5674
5675 Both the file and data forms may contain the additional entries
5676 `:background COLOR' and `:foreground COLOR'. If not present,
5677 foreground and background of the frame on which the image is
5678 displayed is used. */
5679
5680 static int
5681 xbm_image_p (object)
5682 Lisp_Object object;
5683 {
5684 struct image_keyword kw[XBM_LAST];
5685
5686 bcopy (xbm_format, kw, sizeof kw);
5687 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
5688 return 0;
5689
5690 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
5691
5692 if (kw[XBM_FILE].count)
5693 {
5694 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
5695 return 0;
5696 }
5697 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
5698 {
5699 /* In-memory XBM file. */
5700 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
5701 return 0;
5702 }
5703 else
5704 {
5705 Lisp_Object data;
5706 int width, height;
5707
5708 /* Entries for `:width', `:height' and `:data' must be present. */
5709 if (!kw[XBM_WIDTH].count
5710 || !kw[XBM_HEIGHT].count
5711 || !kw[XBM_DATA].count)
5712 return 0;
5713
5714 data = kw[XBM_DATA].value;
5715 width = XFASTINT (kw[XBM_WIDTH].value);
5716 height = XFASTINT (kw[XBM_HEIGHT].value);
5717
5718 /* Check type of data, and width and height against contents of
5719 data. */
5720 if (VECTORP (data))
5721 {
5722 int i;
5723
5724 /* Number of elements of the vector must be >= height. */
5725 if (XVECTOR (data)->size < height)
5726 return 0;
5727
5728 /* Each string or bool-vector in data must be large enough
5729 for one line of the image. */
5730 for (i = 0; i < height; ++i)
5731 {
5732 Lisp_Object elt = XVECTOR (data)->contents[i];
5733
5734 if (STRINGP (elt))
5735 {
5736 if (SCHARS (elt)
5737 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
5738 return 0;
5739 }
5740 else if (BOOL_VECTOR_P (elt))
5741 {
5742 if (XBOOL_VECTOR (elt)->size < width)
5743 return 0;
5744 }
5745 else
5746 return 0;
5747 }
5748 }
5749 else if (STRINGP (data))
5750 {
5751 if (SCHARS (data)
5752 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
5753 return 0;
5754 }
5755 else if (BOOL_VECTOR_P (data))
5756 {
5757 if (XBOOL_VECTOR (data)->size < width * height)
5758 return 0;
5759 }
5760 else
5761 return 0;
5762 }
5763
5764 return 1;
5765 }
5766
5767
5768 /* Scan a bitmap file. FP is the stream to read from. Value is
5769 either an enumerator from enum xbm_token, or a character for a
5770 single-character token, or 0 at end of file. If scanning an
5771 identifier, store the lexeme of the identifier in SVAL. If
5772 scanning a number, store its value in *IVAL. */
5773
5774 static int
5775 xbm_scan (s, end, sval, ival)
5776 char **s, *end;
5777 char *sval;
5778 int *ival;
5779 {
5780 int c;
5781
5782 loop:
5783
5784 /* Skip white space. */
5785 while (*s < end && (c = *(*s)++, isspace (c)))
5786 ;
5787
5788 if (*s >= end)
5789 c = 0;
5790 else if (isdigit (c))
5791 {
5792 int value = 0, digit;
5793
5794 if (c == '0' && *s < end)
5795 {
5796 c = *(*s)++;
5797 if (c == 'x' || c == 'X')
5798 {
5799 while (*s < end)
5800 {
5801 c = *(*s)++;
5802 if (isdigit (c))
5803 digit = c - '0';
5804 else if (c >= 'a' && c <= 'f')
5805 digit = c - 'a' + 10;
5806 else if (c >= 'A' && c <= 'F')
5807 digit = c - 'A' + 10;
5808 else
5809 break;
5810 value = 16 * value + digit;
5811 }
5812 }
5813 else if (isdigit (c))
5814 {
5815 value = c - '0';
5816 while (*s < end
5817 && (c = *(*s)++, isdigit (c)))
5818 value = 8 * value + c - '0';
5819 }
5820 }
5821 else
5822 {
5823 value = c - '0';
5824 while (*s < end
5825 && (c = *(*s)++, isdigit (c)))
5826 value = 10 * value + c - '0';
5827 }
5828
5829 if (*s < end)
5830 *s = *s - 1;
5831 *ival = value;
5832 c = XBM_TK_NUMBER;
5833 }
5834 else if (isalpha (c) || c == '_')
5835 {
5836 *sval++ = c;
5837 while (*s < end
5838 && (c = *(*s)++, (isalnum (c) || c == '_')))
5839 *sval++ = c;
5840 *sval = 0;
5841 if (*s < end)
5842 *s = *s - 1;
5843 c = XBM_TK_IDENT;
5844 }
5845 else if (c == '/' && **s == '*')
5846 {
5847 /* C-style comment. */
5848 ++*s;
5849 while (**s && (**s != '*' || *(*s + 1) != '/'))
5850 ++*s;
5851 if (**s)
5852 {
5853 *s += 2;
5854 goto loop;
5855 }
5856 }
5857
5858 return c;
5859 }
5860
5861
5862 /* Replacement for XReadBitmapFileData which isn't available under old
5863 X versions. CONTENTS is a pointer to a buffer to parse; END is the
5864 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
5865 the image. Return in *DATA the bitmap data allocated with xmalloc.
5866 Value is non-zero if successful. DATA null means just test if
5867 CONTENTS looks like an in-memory XBM file. */
5868
5869 static int
5870 xbm_read_bitmap_data (contents, end, width, height, data)
5871 char *contents, *end;
5872 int *width, *height;
5873 unsigned char **data;
5874 {
5875 char *s = contents;
5876 char buffer[BUFSIZ];
5877 int padding_p = 0;
5878 int v10 = 0;
5879 int bytes_per_line, i, nbytes;
5880 unsigned char *p;
5881 int value;
5882 int LA1;
5883
5884 #define match() \
5885 LA1 = xbm_scan (&s, end, buffer, &value)
5886
5887 #define expect(TOKEN) \
5888 if (LA1 != (TOKEN)) \
5889 goto failure; \
5890 else \
5891 match ()
5892
5893 #define expect_ident(IDENT) \
5894 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
5895 match (); \
5896 else \
5897 goto failure
5898
5899 *width = *height = -1;
5900 if (data)
5901 *data = NULL;
5902 LA1 = xbm_scan (&s, end, buffer, &value);
5903
5904 /* Parse defines for width, height and hot-spots. */
5905 while (LA1 == '#')
5906 {
5907 match ();
5908 expect_ident ("define");
5909 expect (XBM_TK_IDENT);
5910
5911 if (LA1 == XBM_TK_NUMBER);
5912 {
5913 char *p = strrchr (buffer, '_');
5914 p = p ? p + 1 : buffer;
5915 if (strcmp (p, "width") == 0)
5916 *width = value;
5917 else if (strcmp (p, "height") == 0)
5918 *height = value;
5919 }
5920 expect (XBM_TK_NUMBER);
5921 }
5922
5923 if (*width < 0 || *height < 0)
5924 goto failure;
5925 else if (data == NULL)
5926 goto success;
5927
5928 /* Parse bits. Must start with `static'. */
5929 expect_ident ("static");
5930 if (LA1 == XBM_TK_IDENT)
5931 {
5932 if (strcmp (buffer, "unsigned") == 0)
5933 {
5934 match ();
5935 expect_ident ("char");
5936 }
5937 else if (strcmp (buffer, "short") == 0)
5938 {
5939 match ();
5940 v10 = 1;
5941 if (*width % 16 && *width % 16 < 9)
5942 padding_p = 1;
5943 }
5944 else if (strcmp (buffer, "char") == 0)
5945 match ();
5946 else
5947 goto failure;
5948 }
5949 else
5950 goto failure;
5951
5952 expect (XBM_TK_IDENT);
5953 expect ('[');
5954 expect (']');
5955 expect ('=');
5956 expect ('{');
5957
5958 bytes_per_line = (*width + 7) / 8 + padding_p;
5959 nbytes = bytes_per_line * *height;
5960 p = *data = (char *) xmalloc (nbytes);
5961
5962 if (v10)
5963 {
5964 for (i = 0; i < nbytes; i += 2)
5965 {
5966 int val = value;
5967 expect (XBM_TK_NUMBER);
5968
5969 *p++ = val;
5970 if (!padding_p || ((i + 2) % bytes_per_line))
5971 *p++ = value >> 8;
5972
5973 if (LA1 == ',' || LA1 == '}')
5974 match ();
5975 else
5976 goto failure;
5977 }
5978 }
5979 else
5980 {
5981 for (i = 0; i < nbytes; ++i)
5982 {
5983 int val = value;
5984 expect (XBM_TK_NUMBER);
5985
5986 *p++ = val;
5987
5988 if (LA1 == ',' || LA1 == '}')
5989 match ();
5990 else
5991 goto failure;
5992 }
5993 }
5994
5995 success:
5996 return 1;
5997
5998 failure:
5999
6000 if (data && *data)
6001 {
6002 xfree (*data);
6003 *data = NULL;
6004 }
6005 return 0;
6006
6007 #undef match
6008 #undef expect
6009 #undef expect_ident
6010 }
6011
6012
6013 /* Load XBM image IMG which will be displayed on frame F from buffer
6014 CONTENTS. END is the end of the buffer. Value is non-zero if
6015 successful. */
6016
6017 static int
6018 xbm_load_image (f, img, contents, end)
6019 struct frame *f;
6020 struct image *img;
6021 char *contents, *end;
6022 {
6023 int rc;
6024 unsigned char *data;
6025 int success_p = 0;
6026
6027 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
6028 if (rc)
6029 {
6030 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
6031 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
6032 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
6033 Lisp_Object value;
6034
6035 xassert (img->width > 0 && img->height > 0);
6036
6037 /* Get foreground and background colors, maybe allocate colors. */
6038 value = image_spec_value (img->spec, QCforeground, NULL);
6039 if (!NILP (value))
6040 foreground = x_alloc_image_color (f, img, value, foreground);
6041 value = image_spec_value (img->spec, QCbackground, NULL);
6042 if (!NILP (value))
6043 {
6044 background = x_alloc_image_color (f, img, value, background);
6045 img->background = background;
6046 img->background_valid = 1;
6047 }
6048
6049 img->pixmap
6050 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
6051 FRAME_X_WINDOW (f),
6052 data,
6053 img->width, img->height,
6054 foreground, background,
6055 depth);
6056 xfree (data);
6057
6058 if (img->pixmap == None)
6059 {
6060 x_clear_image (f, img);
6061 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
6062 }
6063 else
6064 success_p = 1;
6065 }
6066 else
6067 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
6068
6069 return success_p;
6070 }
6071
6072
6073 /* Value is non-zero if DATA looks like an in-memory XBM file. */
6074
6075 static int
6076 xbm_file_p (data)
6077 Lisp_Object data;
6078 {
6079 int w, h;
6080 return (STRINGP (data)
6081 && xbm_read_bitmap_data (SDATA (data),
6082 (SDATA (data)
6083 + SBYTES (data)),
6084 &w, &h, NULL));
6085 }
6086
6087
6088 /* Fill image IMG which is used on frame F with pixmap data. Value is
6089 non-zero if successful. */
6090
6091 static int
6092 xbm_load (f, img)
6093 struct frame *f;
6094 struct image *img;
6095 {
6096 int success_p = 0;
6097 Lisp_Object file_name;
6098
6099 xassert (xbm_image_p (img->spec));
6100
6101 /* If IMG->spec specifies a file name, create a non-file spec from it. */
6102 file_name = image_spec_value (img->spec, QCfile, NULL);
6103 if (STRINGP (file_name))
6104 {
6105 Lisp_Object file;
6106 char *contents;
6107 int size;
6108 struct gcpro gcpro1;
6109
6110 file = x_find_image_file (file_name);
6111 GCPRO1 (file);
6112 if (!STRINGP (file))
6113 {
6114 image_error ("Cannot find image file `%s'", file_name, Qnil);
6115 UNGCPRO;
6116 return 0;
6117 }
6118
6119 contents = slurp_file (SDATA (file), &size);
6120 if (contents == NULL)
6121 {
6122 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
6123 UNGCPRO;
6124 return 0;
6125 }
6126
6127 success_p = xbm_load_image (f, img, contents, contents + size);
6128 UNGCPRO;
6129 }
6130 else
6131 {
6132 struct image_keyword fmt[XBM_LAST];
6133 Lisp_Object data;
6134 int depth;
6135 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
6136 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
6137 char *bits;
6138 int parsed_p;
6139 int in_memory_file_p = 0;
6140
6141 /* See if data looks like an in-memory XBM file. */
6142 data = image_spec_value (img->spec, QCdata, NULL);
6143 in_memory_file_p = xbm_file_p (data);
6144
6145 /* Parse the image specification. */
6146 bcopy (xbm_format, fmt, sizeof fmt);
6147 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
6148 xassert (parsed_p);
6149
6150 /* Get specified width, and height. */
6151 if (!in_memory_file_p)
6152 {
6153 img->width = XFASTINT (fmt[XBM_WIDTH].value);
6154 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
6155 xassert (img->width > 0 && img->height > 0);
6156 }
6157
6158 /* Get foreground and background colors, maybe allocate colors. */
6159 if (fmt[XBM_FOREGROUND].count
6160 && STRINGP (fmt[XBM_FOREGROUND].value))
6161 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
6162 foreground);
6163 if (fmt[XBM_BACKGROUND].count
6164 && STRINGP (fmt[XBM_BACKGROUND].value))
6165 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
6166 background);
6167
6168 if (in_memory_file_p)
6169 success_p = xbm_load_image (f, img, SDATA (data),
6170 (SDATA (data)
6171 + SBYTES (data)));
6172 else
6173 {
6174 if (VECTORP (data))
6175 {
6176 int i;
6177 char *p;
6178 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
6179
6180 p = bits = (char *) alloca (nbytes * img->height);
6181 for (i = 0; i < img->height; ++i, p += nbytes)
6182 {
6183 Lisp_Object line = XVECTOR (data)->contents[i];
6184 if (STRINGP (line))
6185 bcopy (SDATA (line), p, nbytes);
6186 else
6187 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
6188 }
6189 }
6190 else if (STRINGP (data))
6191 bits = SDATA (data);
6192 else
6193 bits = XBOOL_VECTOR (data)->data;
6194
6195 /* Create the pixmap. */
6196 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
6197 img->pixmap
6198 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
6199 FRAME_X_WINDOW (f),
6200 bits,
6201 img->width, img->height,
6202 foreground, background,
6203 depth);
6204 if (img->pixmap)
6205 success_p = 1;
6206 else
6207 {
6208 image_error ("Unable to create pixmap for XBM image `%s'",
6209 img->spec, Qnil);
6210 x_clear_image (f, img);
6211 }
6212 }
6213 }
6214
6215 return success_p;
6216 }
6217
6218
6219
6220 /***********************************************************************
6221 XPM images
6222 ***********************************************************************/
6223
6224 #if HAVE_XPM
6225
6226 static int xpm_image_p P_ ((Lisp_Object object));
6227 static int xpm_load P_ ((struct frame *f, struct image *img));
6228 static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
6229
6230 #include "X11/xpm.h"
6231
6232 /* The symbol `xpm' identifying XPM-format images. */
6233
6234 Lisp_Object Qxpm;
6235
6236 /* Indices of image specification fields in xpm_format, below. */
6237
6238 enum xpm_keyword_index
6239 {
6240 XPM_TYPE,
6241 XPM_FILE,
6242 XPM_DATA,
6243 XPM_ASCENT,
6244 XPM_MARGIN,
6245 XPM_RELIEF,
6246 XPM_ALGORITHM,
6247 XPM_HEURISTIC_MASK,
6248 XPM_MASK,
6249 XPM_COLOR_SYMBOLS,
6250 XPM_BACKGROUND,
6251 XPM_LAST
6252 };
6253
6254 /* Vector of image_keyword structures describing the format
6255 of valid XPM image specifications. */
6256
6257 static struct image_keyword xpm_format[XPM_LAST] =
6258 {
6259 {":type", IMAGE_SYMBOL_VALUE, 1},
6260 {":file", IMAGE_STRING_VALUE, 0},
6261 {":data", IMAGE_STRING_VALUE, 0},
6262 {":ascent", IMAGE_ASCENT_VALUE, 0},
6263 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6264 {":relief", IMAGE_INTEGER_VALUE, 0},
6265 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6266 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6267 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6268 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6269 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6270 };
6271
6272 /* Structure describing the image type XBM. */
6273
6274 static struct image_type xpm_type =
6275 {
6276 &Qxpm,
6277 xpm_image_p,
6278 xpm_load,
6279 x_clear_image,
6280 NULL
6281 };
6282
6283
6284 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
6285 functions for allocating image colors. Our own functions handle
6286 color allocation failures more gracefully than the ones on the XPM
6287 lib. */
6288
6289 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
6290 #define ALLOC_XPM_COLORS
6291 #endif
6292
6293 #ifdef ALLOC_XPM_COLORS
6294
6295 static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *));
6296 static void xpm_free_color_cache P_ ((void));
6297 static int xpm_lookup_color P_ ((struct frame *, char *, XColor *));
6298 static int xpm_color_bucket P_ ((char *));
6299 static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *,
6300 XColor *, int));
6301
6302 /* An entry in a hash table used to cache color definitions of named
6303 colors. This cache is necessary to speed up XPM image loading in
6304 case we do color allocations ourselves. Without it, we would need
6305 a call to XParseColor per pixel in the image. */
6306
6307 struct xpm_cached_color
6308 {
6309 /* Next in collision chain. */
6310 struct xpm_cached_color *next;
6311
6312 /* Color definition (RGB and pixel color). */
6313 XColor color;
6314
6315 /* Color name. */
6316 char name[1];
6317 };
6318
6319 /* The hash table used for the color cache, and its bucket vector
6320 size. */
6321
6322 #define XPM_COLOR_CACHE_BUCKETS 1001
6323 struct xpm_cached_color **xpm_color_cache;
6324
6325 /* Initialize the color cache. */
6326
6327 static void
6328 xpm_init_color_cache (f, attrs)
6329 struct frame *f;
6330 XpmAttributes *attrs;
6331 {
6332 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
6333 xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes);
6334 memset (xpm_color_cache, 0, nbytes);
6335 init_color_table ();
6336
6337 if (attrs->valuemask & XpmColorSymbols)
6338 {
6339 int i;
6340 XColor color;
6341
6342 for (i = 0; i < attrs->numsymbols; ++i)
6343 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
6344 attrs->colorsymbols[i].value, &color))
6345 {
6346 color.pixel = lookup_rgb_color (f, color.red, color.green,
6347 color.blue);
6348 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
6349 }
6350 }
6351 }
6352
6353
6354 /* Free the color cache. */
6355
6356 static void
6357 xpm_free_color_cache ()
6358 {
6359 struct xpm_cached_color *p, *next;
6360 int i;
6361
6362 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
6363 for (p = xpm_color_cache[i]; p; p = next)
6364 {
6365 next = p->next;
6366 xfree (p);
6367 }
6368
6369 xfree (xpm_color_cache);
6370 xpm_color_cache = NULL;
6371 free_color_table ();
6372 }
6373
6374
6375 /* Return the bucket index for color named COLOR_NAME in the color
6376 cache. */
6377
6378 static int
6379 xpm_color_bucket (color_name)
6380 char *color_name;
6381 {
6382 unsigned h = 0;
6383 char *s;
6384
6385 for (s = color_name; *s; ++s)
6386 h = (h << 2) ^ *s;
6387 return h %= XPM_COLOR_CACHE_BUCKETS;
6388 }
6389
6390
6391 /* On frame F, cache values COLOR for color with name COLOR_NAME.
6392 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
6393 entry added. */
6394
6395 static struct xpm_cached_color *
6396 xpm_cache_color (f, color_name, color, bucket)
6397 struct frame *f;
6398 char *color_name;
6399 XColor *color;
6400 int bucket;
6401 {
6402 size_t nbytes;
6403 struct xpm_cached_color *p;
6404
6405 if (bucket < 0)
6406 bucket = xpm_color_bucket (color_name);
6407
6408 nbytes = sizeof *p + strlen (color_name);
6409 p = (struct xpm_cached_color *) xmalloc (nbytes);
6410 strcpy (p->name, color_name);
6411 p->color = *color;
6412 p->next = xpm_color_cache[bucket];
6413 xpm_color_cache[bucket] = p;
6414 return p;
6415 }
6416
6417
6418 /* Look up color COLOR_NAME for frame F in the color cache. If found,
6419 return the cached definition in *COLOR. Otherwise, make a new
6420 entry in the cache and allocate the color. Value is zero if color
6421 allocation failed. */
6422
6423 static int
6424 xpm_lookup_color (f, color_name, color)
6425 struct frame *f;
6426 char *color_name;
6427 XColor *color;
6428 {
6429 struct xpm_cached_color *p;
6430 int h = xpm_color_bucket (color_name);
6431
6432 for (p = xpm_color_cache[h]; p; p = p->next)
6433 if (strcmp (p->name, color_name) == 0)
6434 break;
6435
6436 if (p != NULL)
6437 *color = p->color;
6438 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
6439 color_name, color))
6440 {
6441 color->pixel = lookup_rgb_color (f, color->red, color->green,
6442 color->blue);
6443 p = xpm_cache_color (f, color_name, color, h);
6444 }
6445 /* You get `opaque' at least from ImageMagick converting pbm to xpm
6446 with transparency, and it's useful. */
6447 else if (strcmp ("opaque", color_name) == 0)
6448 {
6449 bzero (color, sizeof (XColor)); /* Is this necessary/correct? */
6450 color->pixel = FRAME_FOREGROUND_PIXEL (f);
6451 p = xpm_cache_color (f, color_name, color, h);
6452 }
6453
6454 return p != NULL;
6455 }
6456
6457
6458 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
6459 CLOSURE is a pointer to the frame on which we allocate the
6460 color. Return in *COLOR the allocated color. Value is non-zero
6461 if successful. */
6462
6463 static int
6464 xpm_alloc_color (dpy, cmap, color_name, color, closure)
6465 Display *dpy;
6466 Colormap cmap;
6467 char *color_name;
6468 XColor *color;
6469 void *closure;
6470 {
6471 return xpm_lookup_color ((struct frame *) closure, color_name, color);
6472 }
6473
6474
6475 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
6476 is a pointer to the frame on which we allocate the color. Value is
6477 non-zero if successful. */
6478
6479 static int
6480 xpm_free_colors (dpy, cmap, pixels, npixels, closure)
6481 Display *dpy;
6482 Colormap cmap;
6483 Pixel *pixels;
6484 int npixels;
6485 void *closure;
6486 {
6487 return 1;
6488 }
6489
6490 #endif /* ALLOC_XPM_COLORS */
6491
6492
6493 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
6494 for XPM images. Such a list must consist of conses whose car and
6495 cdr are strings. */
6496
6497 static int
6498 xpm_valid_color_symbols_p (color_symbols)
6499 Lisp_Object color_symbols;
6500 {
6501 while (CONSP (color_symbols))
6502 {
6503 Lisp_Object sym = XCAR (color_symbols);
6504 if (!CONSP (sym)
6505 || !STRINGP (XCAR (sym))
6506 || !STRINGP (XCDR (sym)))
6507 break;
6508 color_symbols = XCDR (color_symbols);
6509 }
6510
6511 return NILP (color_symbols);
6512 }
6513
6514
6515 /* Value is non-zero if OBJECT is a valid XPM image specification. */
6516
6517 static int
6518 xpm_image_p (object)
6519 Lisp_Object object;
6520 {
6521 struct image_keyword fmt[XPM_LAST];
6522 bcopy (xpm_format, fmt, sizeof fmt);
6523 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
6524 /* Either `:file' or `:data' must be present. */
6525 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
6526 /* Either no `:color-symbols' or it's a list of conses
6527 whose car and cdr are strings. */
6528 && (fmt[XPM_COLOR_SYMBOLS].count == 0
6529 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
6530 }
6531
6532
6533 /* Load image IMG which will be displayed on frame F. Value is
6534 non-zero if successful. */
6535
6536 static int
6537 xpm_load (f, img)
6538 struct frame *f;
6539 struct image *img;
6540 {
6541 int rc;
6542 XpmAttributes attrs;
6543 Lisp_Object specified_file, color_symbols;
6544
6545 /* Configure the XPM lib. Use the visual of frame F. Allocate
6546 close colors. Return colors allocated. */
6547 bzero (&attrs, sizeof attrs);
6548 attrs.visual = FRAME_X_VISUAL (f);
6549 attrs.colormap = FRAME_X_COLORMAP (f);
6550 attrs.valuemask |= XpmVisual;
6551 attrs.valuemask |= XpmColormap;
6552
6553 #ifdef ALLOC_XPM_COLORS
6554 /* Allocate colors with our own functions which handle
6555 failing color allocation more gracefully. */
6556 attrs.color_closure = f;
6557 attrs.alloc_color = xpm_alloc_color;
6558 attrs.free_colors = xpm_free_colors;
6559 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
6560 #else /* not ALLOC_XPM_COLORS */
6561 /* Let the XPM lib allocate colors. */
6562 attrs.valuemask |= XpmReturnAllocPixels;
6563 #ifdef XpmAllocCloseColors
6564 attrs.alloc_close_colors = 1;
6565 attrs.valuemask |= XpmAllocCloseColors;
6566 #else /* not XpmAllocCloseColors */
6567 attrs.closeness = 600;
6568 attrs.valuemask |= XpmCloseness;
6569 #endif /* not XpmAllocCloseColors */
6570 #endif /* ALLOC_XPM_COLORS */
6571
6572 /* If image specification contains symbolic color definitions, add
6573 these to `attrs'. */
6574 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
6575 if (CONSP (color_symbols))
6576 {
6577 Lisp_Object tail;
6578 XpmColorSymbol *xpm_syms;
6579 int i, size;
6580
6581 attrs.valuemask |= XpmColorSymbols;
6582
6583 /* Count number of symbols. */
6584 attrs.numsymbols = 0;
6585 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
6586 ++attrs.numsymbols;
6587
6588 /* Allocate an XpmColorSymbol array. */
6589 size = attrs.numsymbols * sizeof *xpm_syms;
6590 xpm_syms = (XpmColorSymbol *) alloca (size);
6591 bzero (xpm_syms, size);
6592 attrs.colorsymbols = xpm_syms;
6593
6594 /* Fill the color symbol array. */
6595 for (tail = color_symbols, i = 0;
6596 CONSP (tail);
6597 ++i, tail = XCDR (tail))
6598 {
6599 Lisp_Object name = XCAR (XCAR (tail));
6600 Lisp_Object color = XCDR (XCAR (tail));
6601 xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1);
6602 strcpy (xpm_syms[i].name, SDATA (name));
6603 xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1);
6604 strcpy (xpm_syms[i].value, SDATA (color));
6605 }
6606 }
6607
6608 /* Create a pixmap for the image, either from a file, or from a
6609 string buffer containing data in the same format as an XPM file. */
6610 #ifdef ALLOC_XPM_COLORS
6611 xpm_init_color_cache (f, &attrs);
6612 #endif
6613
6614 specified_file = image_spec_value (img->spec, QCfile, NULL);
6615 if (STRINGP (specified_file))
6616 {
6617 Lisp_Object file = x_find_image_file (specified_file);
6618 if (!STRINGP (file))
6619 {
6620 image_error ("Cannot find image file `%s'", specified_file, Qnil);
6621 return 0;
6622 }
6623
6624 rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6625 SDATA (file), &img->pixmap, &img->mask,
6626 &attrs);
6627 }
6628 else
6629 {
6630 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
6631 rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6632 SDATA (buffer),
6633 &img->pixmap, &img->mask,
6634 &attrs);
6635 }
6636
6637 if (rc == XpmSuccess)
6638 {
6639 #ifdef ALLOC_XPM_COLORS
6640 img->colors = colors_in_color_table (&img->ncolors);
6641 #else /* not ALLOC_XPM_COLORS */
6642 int i;
6643
6644 img->ncolors = attrs.nalloc_pixels;
6645 img->colors = (unsigned long *) xmalloc (img->ncolors
6646 * sizeof *img->colors);
6647 for (i = 0; i < attrs.nalloc_pixels; ++i)
6648 {
6649 img->colors[i] = attrs.alloc_pixels[i];
6650 #ifdef DEBUG_X_COLORS
6651 register_color (img->colors[i]);
6652 #endif
6653 }
6654 #endif /* not ALLOC_XPM_COLORS */
6655
6656 img->width = attrs.width;
6657 img->height = attrs.height;
6658 xassert (img->width > 0 && img->height > 0);
6659
6660 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
6661 XpmFreeAttributes (&attrs);
6662 }
6663 else
6664 {
6665 switch (rc)
6666 {
6667 case XpmOpenFailed:
6668 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
6669 break;
6670
6671 case XpmFileInvalid:
6672 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
6673 break;
6674
6675 case XpmNoMemory:
6676 image_error ("Out of memory (%s)", img->spec, Qnil);
6677 break;
6678
6679 case XpmColorFailed:
6680 image_error ("Color allocation error (%s)", img->spec, Qnil);
6681 break;
6682
6683 default:
6684 image_error ("Unknown error (%s)", img->spec, Qnil);
6685 break;
6686 }
6687 }
6688
6689 #ifdef ALLOC_XPM_COLORS
6690 xpm_free_color_cache ();
6691 #endif
6692 return rc == XpmSuccess;
6693 }
6694
6695 #endif /* HAVE_XPM != 0 */
6696
6697
6698 /***********************************************************************
6699 Color table
6700 ***********************************************************************/
6701
6702 /* An entry in the color table mapping an RGB color to a pixel color. */
6703
6704 struct ct_color
6705 {
6706 int r, g, b;
6707 unsigned long pixel;
6708
6709 /* Next in color table collision list. */
6710 struct ct_color *next;
6711 };
6712
6713 /* The bucket vector size to use. Must be prime. */
6714
6715 #define CT_SIZE 101
6716
6717 /* Value is a hash of the RGB color given by R, G, and B. */
6718
6719 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
6720
6721 /* The color hash table. */
6722
6723 struct ct_color **ct_table;
6724
6725 /* Number of entries in the color table. */
6726
6727 int ct_colors_allocated;
6728
6729 /* Initialize the color table. */
6730
6731 static void
6732 init_color_table ()
6733 {
6734 int size = CT_SIZE * sizeof (*ct_table);
6735 ct_table = (struct ct_color **) xmalloc (size);
6736 bzero (ct_table, size);
6737 ct_colors_allocated = 0;
6738 }
6739
6740
6741 /* Free memory associated with the color table. */
6742
6743 static void
6744 free_color_table ()
6745 {
6746 int i;
6747 struct ct_color *p, *next;
6748
6749 for (i = 0; i < CT_SIZE; ++i)
6750 for (p = ct_table[i]; p; p = next)
6751 {
6752 next = p->next;
6753 xfree (p);
6754 }
6755
6756 xfree (ct_table);
6757 ct_table = NULL;
6758 }
6759
6760
6761 /* Value is a pixel color for RGB color R, G, B on frame F. If an
6762 entry for that color already is in the color table, return the
6763 pixel color of that entry. Otherwise, allocate a new color for R,
6764 G, B, and make an entry in the color table. */
6765
6766 static unsigned long
6767 lookup_rgb_color (f, r, g, b)
6768 struct frame *f;
6769 int r, g, b;
6770 {
6771 unsigned hash = CT_HASH_RGB (r, g, b);
6772 int i = hash % CT_SIZE;
6773 struct ct_color *p;
6774 struct x_display_info *dpyinfo;
6775
6776 /* Handle TrueColor visuals specially, which improves performance by
6777 two orders of magnitude. Freeing colors on TrueColor visuals is
6778 a nop, and pixel colors specify RGB values directly. See also
6779 the Xlib spec, chapter 3.1. */
6780 dpyinfo = FRAME_X_DISPLAY_INFO (f);
6781 if (dpyinfo->red_bits > 0)
6782 {
6783 unsigned long pr, pg, pb;
6784
6785 /* Apply gamma-correction like normal color allocation does. */
6786 if (f->gamma)
6787 {
6788 XColor color;
6789 color.red = r, color.green = g, color.blue = b;
6790 gamma_correct (f, &color);
6791 r = color.red, g = color.green, b = color.blue;
6792 }
6793
6794 /* Scale down RGB values to the visual's bits per RGB, and shift
6795 them to the right position in the pixel color. Note that the
6796 original RGB values are 16-bit values, as usual in X. */
6797 pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset;
6798 pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset;
6799 pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset;
6800
6801 /* Assemble the pixel color. */
6802 return pr | pg | pb;
6803 }
6804
6805 for (p = ct_table[i]; p; p = p->next)
6806 if (p->r == r && p->g == g && p->b == b)
6807 break;
6808
6809 if (p == NULL)
6810 {
6811 XColor color;
6812 Colormap cmap;
6813 int rc;
6814
6815 color.red = r;
6816 color.green = g;
6817 color.blue = b;
6818
6819 cmap = FRAME_X_COLORMAP (f);
6820 rc = x_alloc_nearest_color (f, cmap, &color);
6821
6822 if (rc)
6823 {
6824 ++ct_colors_allocated;
6825
6826 p = (struct ct_color *) xmalloc (sizeof *p);
6827 p->r = r;
6828 p->g = g;
6829 p->b = b;
6830 p->pixel = color.pixel;
6831 p->next = ct_table[i];
6832 ct_table[i] = p;
6833 }
6834 else
6835 return FRAME_FOREGROUND_PIXEL (f);
6836 }
6837
6838 return p->pixel;
6839 }
6840
6841
6842 /* Look up pixel color PIXEL which is used on frame F in the color
6843 table. If not already present, allocate it. Value is PIXEL. */
6844
6845 static unsigned long
6846 lookup_pixel_color (f, pixel)
6847 struct frame *f;
6848 unsigned long pixel;
6849 {
6850 int i = pixel % CT_SIZE;
6851 struct ct_color *p;
6852
6853 for (p = ct_table[i]; p; p = p->next)
6854 if (p->pixel == pixel)
6855 break;
6856
6857 if (p == NULL)
6858 {
6859 XColor color;
6860 Colormap cmap;
6861 int rc;
6862
6863 cmap = FRAME_X_COLORMAP (f);
6864 color.pixel = pixel;
6865 x_query_color (f, &color);
6866 rc = x_alloc_nearest_color (f, cmap, &color);
6867
6868 if (rc)
6869 {
6870 ++ct_colors_allocated;
6871
6872 p = (struct ct_color *) xmalloc (sizeof *p);
6873 p->r = color.red;
6874 p->g = color.green;
6875 p->b = color.blue;
6876 p->pixel = pixel;
6877 p->next = ct_table[i];
6878 ct_table[i] = p;
6879 }
6880 else
6881 return FRAME_FOREGROUND_PIXEL (f);
6882 }
6883
6884 return p->pixel;
6885 }
6886
6887
6888 /* Value is a vector of all pixel colors contained in the color table,
6889 allocated via xmalloc. Set *N to the number of colors. */
6890
6891 static unsigned long *
6892 colors_in_color_table (n)
6893 int *n;
6894 {
6895 int i, j;
6896 struct ct_color *p;
6897 unsigned long *colors;
6898
6899 if (ct_colors_allocated == 0)
6900 {
6901 *n = 0;
6902 colors = NULL;
6903 }
6904 else
6905 {
6906 colors = (unsigned long *) xmalloc (ct_colors_allocated
6907 * sizeof *colors);
6908 *n = ct_colors_allocated;
6909
6910 for (i = j = 0; i < CT_SIZE; ++i)
6911 for (p = ct_table[i]; p; p = p->next)
6912 colors[j++] = p->pixel;
6913 }
6914
6915 return colors;
6916 }
6917
6918
6919
6920 /***********************************************************************
6921 Algorithms
6922 ***********************************************************************/
6923
6924 static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
6925 static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
6926 static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
6927
6928 /* Non-zero means draw a cross on images having `:conversion
6929 disabled'. */
6930
6931 int cross_disabled_images;
6932
6933 /* Edge detection matrices for different edge-detection
6934 strategies. */
6935
6936 static int emboss_matrix[9] = {
6937 /* x - 1 x x + 1 */
6938 2, -1, 0, /* y - 1 */
6939 -1, 0, 1, /* y */
6940 0, 1, -2 /* y + 1 */
6941 };
6942
6943 static int laplace_matrix[9] = {
6944 /* x - 1 x x + 1 */
6945 1, 0, 0, /* y - 1 */
6946 0, 0, 0, /* y */
6947 0, 0, -1 /* y + 1 */
6948 };
6949
6950 /* Value is the intensity of the color whose red/green/blue values
6951 are R, G, and B. */
6952
6953 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
6954
6955
6956 /* On frame F, return an array of XColor structures describing image
6957 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
6958 non-zero means also fill the red/green/blue members of the XColor
6959 structures. Value is a pointer to the array of XColors structures,
6960 allocated with xmalloc; it must be freed by the caller. */
6961
6962 static XColor *
6963 x_to_xcolors (f, img, rgb_p)
6964 struct frame *f;
6965 struct image *img;
6966 int rgb_p;
6967 {
6968 int x, y;
6969 XColor *colors, *p;
6970 XImage *ximg;
6971
6972 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
6973
6974 /* Get the X image IMG->pixmap. */
6975 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
6976 0, 0, img->width, img->height, ~0, ZPixmap);
6977
6978 /* Fill the `pixel' members of the XColor array. I wished there
6979 were an easy and portable way to circumvent XGetPixel. */
6980 p = colors;
6981 for (y = 0; y < img->height; ++y)
6982 {
6983 XColor *row = p;
6984
6985 for (x = 0; x < img->width; ++x, ++p)
6986 p->pixel = XGetPixel (ximg, x, y);
6987
6988 if (rgb_p)
6989 x_query_colors (f, row, img->width);
6990 }
6991
6992 XDestroyImage (ximg);
6993 return colors;
6994 }
6995
6996
6997 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
6998 RGB members are set. F is the frame on which this all happens.
6999 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
7000
7001 static void
7002 x_from_xcolors (f, img, colors)
7003 struct frame *f;
7004 struct image *img;
7005 XColor *colors;
7006 {
7007 int x, y;
7008 XImage *oimg;
7009 Pixmap pixmap;
7010 XColor *p;
7011
7012 init_color_table ();
7013
7014 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
7015 &oimg, &pixmap);
7016 p = colors;
7017 for (y = 0; y < img->height; ++y)
7018 for (x = 0; x < img->width; ++x, ++p)
7019 {
7020 unsigned long pixel;
7021 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
7022 XPutPixel (oimg, x, y, pixel);
7023 }
7024
7025 xfree (colors);
7026 x_clear_image_1 (f, img, 1, 0, 1);
7027
7028 x_put_x_image (f, oimg, pixmap, img->width, img->height);
7029 x_destroy_x_image (oimg);
7030 img->pixmap = pixmap;
7031 img->colors = colors_in_color_table (&img->ncolors);
7032 free_color_table ();
7033 }
7034
7035
7036 /* On frame F, perform edge-detection on image IMG.
7037
7038 MATRIX is a nine-element array specifying the transformation
7039 matrix. See emboss_matrix for an example.
7040
7041 COLOR_ADJUST is a color adjustment added to each pixel of the
7042 outgoing image. */
7043
7044 static void
7045 x_detect_edges (f, img, matrix, color_adjust)
7046 struct frame *f;
7047 struct image *img;
7048 int matrix[9], color_adjust;
7049 {
7050 XColor *colors = x_to_xcolors (f, img, 1);
7051 XColor *new, *p;
7052 int x, y, i, sum;
7053
7054 for (i = sum = 0; i < 9; ++i)
7055 sum += abs (matrix[i]);
7056
7057 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
7058
7059 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
7060
7061 for (y = 0; y < img->height; ++y)
7062 {
7063 p = COLOR (new, 0, y);
7064 p->red = p->green = p->blue = 0xffff/2;
7065 p = COLOR (new, img->width - 1, y);
7066 p->red = p->green = p->blue = 0xffff/2;
7067 }
7068
7069 for (x = 1; x < img->width - 1; ++x)
7070 {
7071 p = COLOR (new, x, 0);
7072 p->red = p->green = p->blue = 0xffff/2;
7073 p = COLOR (new, x, img->height - 1);
7074 p->red = p->green = p->blue = 0xffff/2;
7075 }
7076
7077 for (y = 1; y < img->height - 1; ++y)
7078 {
7079 p = COLOR (new, 1, y);
7080
7081 for (x = 1; x < img->width - 1; ++x, ++p)
7082 {
7083 int r, g, b, y1, x1;
7084
7085 r = g = b = i = 0;
7086 for (y1 = y - 1; y1 < y + 2; ++y1)
7087 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
7088 if (matrix[i])
7089 {
7090 XColor *t = COLOR (colors, x1, y1);
7091 r += matrix[i] * t->red;
7092 g += matrix[i] * t->green;
7093 b += matrix[i] * t->blue;
7094 }
7095
7096 r = (r / sum + color_adjust) & 0xffff;
7097 g = (g / sum + color_adjust) & 0xffff;
7098 b = (b / sum + color_adjust) & 0xffff;
7099 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
7100 }
7101 }
7102
7103 xfree (colors);
7104 x_from_xcolors (f, img, new);
7105
7106 #undef COLOR
7107 }
7108
7109
7110 /* Perform the pre-defined `emboss' edge-detection on image IMG
7111 on frame F. */
7112
7113 static void
7114 x_emboss (f, img)
7115 struct frame *f;
7116 struct image *img;
7117 {
7118 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
7119 }
7120
7121
7122 /* Perform the pre-defined `laplace' edge-detection on image IMG
7123 on frame F. */
7124
7125 static void
7126 x_laplace (f, img)
7127 struct frame *f;
7128 struct image *img;
7129 {
7130 x_detect_edges (f, img, laplace_matrix, 45000);
7131 }
7132
7133
7134 /* Perform edge-detection on image IMG on frame F, with specified
7135 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
7136
7137 MATRIX must be either
7138
7139 - a list of at least 9 numbers in row-major form
7140 - a vector of at least 9 numbers
7141
7142 COLOR_ADJUST nil means use a default; otherwise it must be a
7143 number. */
7144
7145 static void
7146 x_edge_detection (f, img, matrix, color_adjust)
7147 struct frame *f;
7148 struct image *img;
7149 Lisp_Object matrix, color_adjust;
7150 {
7151 int i = 0;
7152 int trans[9];
7153
7154 if (CONSP (matrix))
7155 {
7156 for (i = 0;
7157 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
7158 ++i, matrix = XCDR (matrix))
7159 trans[i] = XFLOATINT (XCAR (matrix));
7160 }
7161 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
7162 {
7163 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
7164 trans[i] = XFLOATINT (AREF (matrix, i));
7165 }
7166
7167 if (NILP (color_adjust))
7168 color_adjust = make_number (0xffff / 2);
7169
7170 if (i == 9 && NUMBERP (color_adjust))
7171 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
7172 }
7173
7174
7175 /* Transform image IMG on frame F so that it looks disabled. */
7176
7177 static void
7178 x_disable_image (f, img)
7179 struct frame *f;
7180 struct image *img;
7181 {
7182 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7183
7184 if (dpyinfo->n_planes >= 2)
7185 {
7186 /* Color (or grayscale). Convert to gray, and equalize. Just
7187 drawing such images with a stipple can look very odd, so
7188 we're using this method instead. */
7189 XColor *colors = x_to_xcolors (f, img, 1);
7190 XColor *p, *end;
7191 const int h = 15000;
7192 const int l = 30000;
7193
7194 for (p = colors, end = colors + img->width * img->height;
7195 p < end;
7196 ++p)
7197 {
7198 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
7199 int i2 = (0xffff - h - l) * i / 0xffff + l;
7200 p->red = p->green = p->blue = i2;
7201 }
7202
7203 x_from_xcolors (f, img, colors);
7204 }
7205
7206 /* Draw a cross over the disabled image, if we must or if we
7207 should. */
7208 if (dpyinfo->n_planes < 2 || cross_disabled_images)
7209 {
7210 Display *dpy = FRAME_X_DISPLAY (f);
7211 GC gc;
7212
7213 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
7214 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
7215 XDrawLine (dpy, img->pixmap, gc, 0, 0,
7216 img->width - 1, img->height - 1);
7217 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
7218 img->width - 1, 0);
7219 XFreeGC (dpy, gc);
7220
7221 if (img->mask)
7222 {
7223 gc = XCreateGC (dpy, img->mask, 0, NULL);
7224 XSetForeground (dpy, gc, WHITE_PIX_DEFAULT (f));
7225 XDrawLine (dpy, img->mask, gc, 0, 0,
7226 img->width - 1, img->height - 1);
7227 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
7228 img->width - 1, 0);
7229 XFreeGC (dpy, gc);
7230 }
7231 }
7232 }
7233
7234
7235 /* Build a mask for image IMG which is used on frame F. FILE is the
7236 name of an image file, for error messages. HOW determines how to
7237 determine the background color of IMG. If it is a list '(R G B)',
7238 with R, G, and B being integers >= 0, take that as the color of the
7239 background. Otherwise, determine the background color of IMG
7240 heuristically. Value is non-zero if successful. */
7241
7242 static int
7243 x_build_heuristic_mask (f, img, how)
7244 struct frame *f;
7245 struct image *img;
7246 Lisp_Object how;
7247 {
7248 Display *dpy = FRAME_X_DISPLAY (f);
7249 XImage *ximg, *mask_img;
7250 int x, y, rc, use_img_background;
7251 unsigned long bg = 0;
7252
7253 if (img->mask)
7254 {
7255 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
7256 img->mask = None;
7257 img->background_transparent_valid = 0;
7258 }
7259
7260 /* Create an image and pixmap serving as mask. */
7261 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
7262 &mask_img, &img->mask);
7263 if (!rc)
7264 return 0;
7265
7266 /* Get the X image of IMG->pixmap. */
7267 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
7268 ~0, ZPixmap);
7269
7270 /* Determine the background color of ximg. If HOW is `(R G B)'
7271 take that as color. Otherwise, use the image's background color. */
7272 use_img_background = 1;
7273
7274 if (CONSP (how))
7275 {
7276 int rgb[3], i;
7277
7278 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
7279 {
7280 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
7281 how = XCDR (how);
7282 }
7283
7284 if (i == 3 && NILP (how))
7285 {
7286 char color_name[30];
7287 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
7288 bg = x_alloc_image_color (f, img, build_string (color_name), 0);
7289 use_img_background = 0;
7290 }
7291 }
7292
7293 if (use_img_background)
7294 bg = four_corners_best (ximg, img->width, img->height);
7295
7296 /* Set all bits in mask_img to 1 whose color in ximg is different
7297 from the background color bg. */
7298 for (y = 0; y < img->height; ++y)
7299 for (x = 0; x < img->width; ++x)
7300 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
7301
7302 /* Fill in the background_transparent field while we have the mask handy. */
7303 image_background_transparent (img, f, mask_img);
7304
7305 /* Put mask_img into img->mask. */
7306 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
7307 x_destroy_x_image (mask_img);
7308 XDestroyImage (ximg);
7309
7310 return 1;
7311 }
7312
7313
7314
7315 /***********************************************************************
7316 PBM (mono, gray, color)
7317 ***********************************************************************/
7318
7319 static int pbm_image_p P_ ((Lisp_Object object));
7320 static int pbm_load P_ ((struct frame *f, struct image *img));
7321 static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
7322
7323 /* The symbol `pbm' identifying images of this type. */
7324
7325 Lisp_Object Qpbm;
7326
7327 /* Indices of image specification fields in gs_format, below. */
7328
7329 enum pbm_keyword_index
7330 {
7331 PBM_TYPE,
7332 PBM_FILE,
7333 PBM_DATA,
7334 PBM_ASCENT,
7335 PBM_MARGIN,
7336 PBM_RELIEF,
7337 PBM_ALGORITHM,
7338 PBM_HEURISTIC_MASK,
7339 PBM_MASK,
7340 PBM_FOREGROUND,
7341 PBM_BACKGROUND,
7342 PBM_LAST
7343 };
7344
7345 /* Vector of image_keyword structures describing the format
7346 of valid user-defined image specifications. */
7347
7348 static struct image_keyword pbm_format[PBM_LAST] =
7349 {
7350 {":type", IMAGE_SYMBOL_VALUE, 1},
7351 {":file", IMAGE_STRING_VALUE, 0},
7352 {":data", IMAGE_STRING_VALUE, 0},
7353 {":ascent", IMAGE_ASCENT_VALUE, 0},
7354 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
7355 {":relief", IMAGE_INTEGER_VALUE, 0},
7356 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7357 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7358 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7359 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
7360 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
7361 };
7362
7363 /* Structure describing the image type `pbm'. */
7364
7365 static struct image_type pbm_type =
7366 {
7367 &Qpbm,
7368 pbm_image_p,
7369 pbm_load,
7370 x_clear_image,
7371 NULL
7372 };
7373
7374
7375 /* Return non-zero if OBJECT is a valid PBM image specification. */
7376
7377 static int
7378 pbm_image_p (object)
7379 Lisp_Object object;
7380 {
7381 struct image_keyword fmt[PBM_LAST];
7382
7383 bcopy (pbm_format, fmt, sizeof fmt);
7384
7385 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
7386 return 0;
7387
7388 /* Must specify either :data or :file. */
7389 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
7390 }
7391
7392
7393 /* Scan a decimal number from *S and return it. Advance *S while
7394 reading the number. END is the end of the string. Value is -1 at
7395 end of input. */
7396
7397 static int
7398 pbm_scan_number (s, end)
7399 unsigned char **s, *end;
7400 {
7401 int c = 0, val = -1;
7402
7403 while (*s < end)
7404 {
7405 /* Skip white-space. */
7406 while (*s < end && (c = *(*s)++, isspace (c)))
7407 ;
7408
7409 if (c == '#')
7410 {
7411 /* Skip comment to end of line. */
7412 while (*s < end && (c = *(*s)++, c != '\n'))
7413 ;
7414 }
7415 else if (isdigit (c))
7416 {
7417 /* Read decimal number. */
7418 val = c - '0';
7419 while (*s < end && (c = *(*s)++, isdigit (c)))
7420 val = 10 * val + c - '0';
7421 break;
7422 }
7423 else
7424 break;
7425 }
7426
7427 return val;
7428 }
7429
7430
7431 /* Load PBM image IMG for use on frame F. */
7432
7433 static int
7434 pbm_load (f, img)
7435 struct frame *f;
7436 struct image *img;
7437 {
7438 int raw_p, x, y;
7439 int width, height, max_color_idx = 0;
7440 XImage *ximg;
7441 Lisp_Object file, specified_file;
7442 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
7443 struct gcpro gcpro1;
7444 unsigned char *contents = NULL;
7445 unsigned char *end, *p;
7446 int size;
7447
7448 specified_file = image_spec_value (img->spec, QCfile, NULL);
7449 file = Qnil;
7450 GCPRO1 (file);
7451
7452 if (STRINGP (specified_file))
7453 {
7454 file = x_find_image_file (specified_file);
7455 if (!STRINGP (file))
7456 {
7457 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7458 UNGCPRO;
7459 return 0;
7460 }
7461
7462 contents = slurp_file (SDATA (file), &size);
7463 if (contents == NULL)
7464 {
7465 image_error ("Error reading `%s'", file, Qnil);
7466 UNGCPRO;
7467 return 0;
7468 }
7469
7470 p = contents;
7471 end = contents + size;
7472 }
7473 else
7474 {
7475 Lisp_Object data;
7476 data = image_spec_value (img->spec, QCdata, NULL);
7477 p = SDATA (data);
7478 end = p + SBYTES (data);
7479 }
7480
7481 /* Check magic number. */
7482 if (end - p < 2 || *p++ != 'P')
7483 {
7484 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
7485 error:
7486 xfree (contents);
7487 UNGCPRO;
7488 return 0;
7489 }
7490
7491 switch (*p++)
7492 {
7493 case '1':
7494 raw_p = 0, type = PBM_MONO;
7495 break;
7496
7497 case '2':
7498 raw_p = 0, type = PBM_GRAY;
7499 break;
7500
7501 case '3':
7502 raw_p = 0, type = PBM_COLOR;
7503 break;
7504
7505 case '4':
7506 raw_p = 1, type = PBM_MONO;
7507 break;
7508
7509 case '5':
7510 raw_p = 1, type = PBM_GRAY;
7511 break;
7512
7513 case '6':
7514 raw_p = 1, type = PBM_COLOR;
7515 break;
7516
7517 default:
7518 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
7519 goto error;
7520 }
7521
7522 /* Read width, height, maximum color-component. Characters
7523 starting with `#' up to the end of a line are ignored. */
7524 width = pbm_scan_number (&p, end);
7525 height = pbm_scan_number (&p, end);
7526
7527 if (type != PBM_MONO)
7528 {
7529 max_color_idx = pbm_scan_number (&p, end);
7530 if (raw_p && max_color_idx > 255)
7531 max_color_idx = 255;
7532 }
7533
7534 if (width < 0
7535 || height < 0
7536 || (type != PBM_MONO && max_color_idx < 0))
7537 goto error;
7538
7539 if (!x_create_x_image_and_pixmap (f, width, height, 0,
7540 &ximg, &img->pixmap))
7541 goto error;
7542
7543 /* Initialize the color hash table. */
7544 init_color_table ();
7545
7546 if (type == PBM_MONO)
7547 {
7548 int c = 0, g;
7549 struct image_keyword fmt[PBM_LAST];
7550 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
7551 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
7552
7553 /* Parse the image specification. */
7554 bcopy (pbm_format, fmt, sizeof fmt);
7555 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
7556
7557 /* Get foreground and background colors, maybe allocate colors. */
7558 if (fmt[PBM_FOREGROUND].count
7559 && STRINGP (fmt[PBM_FOREGROUND].value))
7560 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
7561 if (fmt[PBM_BACKGROUND].count
7562 && STRINGP (fmt[PBM_BACKGROUND].value))
7563 {
7564 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
7565 img->background = bg;
7566 img->background_valid = 1;
7567 }
7568
7569 for (y = 0; y < height; ++y)
7570 for (x = 0; x < width; ++x)
7571 {
7572 if (raw_p)
7573 {
7574 if ((x & 7) == 0)
7575 c = *p++;
7576 g = c & 0x80;
7577 c <<= 1;
7578 }
7579 else
7580 g = pbm_scan_number (&p, end);
7581
7582 XPutPixel (ximg, x, y, g ? fg : bg);
7583 }
7584 }
7585 else
7586 {
7587 for (y = 0; y < height; ++y)
7588 for (x = 0; x < width; ++x)
7589 {
7590 int r, g, b;
7591
7592 if (type == PBM_GRAY)
7593 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
7594 else if (raw_p)
7595 {
7596 r = *p++;
7597 g = *p++;
7598 b = *p++;
7599 }
7600 else
7601 {
7602 r = pbm_scan_number (&p, end);
7603 g = pbm_scan_number (&p, end);
7604 b = pbm_scan_number (&p, end);
7605 }
7606
7607 if (r < 0 || g < 0 || b < 0)
7608 {
7609 xfree (ximg->data);
7610 ximg->data = NULL;
7611 XDestroyImage (ximg);
7612 image_error ("Invalid pixel value in image `%s'",
7613 img->spec, Qnil);
7614 goto error;
7615 }
7616
7617 /* RGB values are now in the range 0..max_color_idx.
7618 Scale this to the range 0..0xffff supported by X. */
7619 r = (double) r * 65535 / max_color_idx;
7620 g = (double) g * 65535 / max_color_idx;
7621 b = (double) b * 65535 / max_color_idx;
7622 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
7623 }
7624 }
7625
7626 /* Store in IMG->colors the colors allocated for the image, and
7627 free the color table. */
7628 img->colors = colors_in_color_table (&img->ncolors);
7629 free_color_table ();
7630
7631 /* Maybe fill in the background field while we have ximg handy. */
7632 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7633 IMAGE_BACKGROUND (img, f, ximg);
7634
7635 /* Put the image into a pixmap. */
7636 x_put_x_image (f, ximg, img->pixmap, width, height);
7637 x_destroy_x_image (ximg);
7638
7639 img->width = width;
7640 img->height = height;
7641
7642 UNGCPRO;
7643 xfree (contents);
7644 return 1;
7645 }
7646
7647
7648
7649 /***********************************************************************
7650 PNG
7651 ***********************************************************************/
7652
7653 #if HAVE_PNG
7654
7655 #if defined HAVE_LIBPNG_PNG_H
7656 # include <libpng/png.h>
7657 #else
7658 # include <png.h>
7659 #endif
7660
7661 /* Function prototypes. */
7662
7663 static int png_image_p P_ ((Lisp_Object object));
7664 static int png_load P_ ((struct frame *f, struct image *img));
7665
7666 /* The symbol `png' identifying images of this type. */
7667
7668 Lisp_Object Qpng;
7669
7670 /* Indices of image specification fields in png_format, below. */
7671
7672 enum png_keyword_index
7673 {
7674 PNG_TYPE,
7675 PNG_DATA,
7676 PNG_FILE,
7677 PNG_ASCENT,
7678 PNG_MARGIN,
7679 PNG_RELIEF,
7680 PNG_ALGORITHM,
7681 PNG_HEURISTIC_MASK,
7682 PNG_MASK,
7683 PNG_BACKGROUND,
7684 PNG_LAST
7685 };
7686
7687 /* Vector of image_keyword structures describing the format
7688 of valid user-defined image specifications. */
7689
7690 static struct image_keyword png_format[PNG_LAST] =
7691 {
7692 {":type", IMAGE_SYMBOL_VALUE, 1},
7693 {":data", IMAGE_STRING_VALUE, 0},
7694 {":file", IMAGE_STRING_VALUE, 0},
7695 {":ascent", IMAGE_ASCENT_VALUE, 0},
7696 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
7697 {":relief", IMAGE_INTEGER_VALUE, 0},
7698 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7699 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7700 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7701 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
7702 };
7703
7704 /* Structure describing the image type `png'. */
7705
7706 static struct image_type png_type =
7707 {
7708 &Qpng,
7709 png_image_p,
7710 png_load,
7711 x_clear_image,
7712 NULL
7713 };
7714
7715
7716 /* Return non-zero if OBJECT is a valid PNG image specification. */
7717
7718 static int
7719 png_image_p (object)
7720 Lisp_Object object;
7721 {
7722 struct image_keyword fmt[PNG_LAST];
7723 bcopy (png_format, fmt, sizeof fmt);
7724
7725 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
7726 return 0;
7727
7728 /* Must specify either the :data or :file keyword. */
7729 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
7730 }
7731
7732
7733 /* Error and warning handlers installed when the PNG library
7734 is initialized. */
7735
7736 static void
7737 my_png_error (png_ptr, msg)
7738 png_struct *png_ptr;
7739 char *msg;
7740 {
7741 xassert (png_ptr != NULL);
7742 image_error ("PNG error: %s", build_string (msg), Qnil);
7743 longjmp (png_ptr->jmpbuf, 1);
7744 }
7745
7746
7747 static void
7748 my_png_warning (png_ptr, msg)
7749 png_struct *png_ptr;
7750 char *msg;
7751 {
7752 xassert (png_ptr != NULL);
7753 image_error ("PNG warning: %s", build_string (msg), Qnil);
7754 }
7755
7756 /* Memory source for PNG decoding. */
7757
7758 struct png_memory_storage
7759 {
7760 unsigned char *bytes; /* The data */
7761 size_t len; /* How big is it? */
7762 int index; /* Where are we? */
7763 };
7764
7765
7766 /* Function set as reader function when reading PNG image from memory.
7767 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
7768 bytes from the input to DATA. */
7769
7770 static void
7771 png_read_from_memory (png_ptr, data, length)
7772 png_structp png_ptr;
7773 png_bytep data;
7774 png_size_t length;
7775 {
7776 struct png_memory_storage *tbr
7777 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
7778
7779 if (length > tbr->len - tbr->index)
7780 png_error (png_ptr, "Read error");
7781
7782 bcopy (tbr->bytes + tbr->index, data, length);
7783 tbr->index = tbr->index + length;
7784 }
7785
7786 /* Load PNG image IMG for use on frame F. Value is non-zero if
7787 successful. */
7788
7789 static int
7790 png_load (f, img)
7791 struct frame *f;
7792 struct image *img;
7793 {
7794 Lisp_Object file, specified_file;
7795 Lisp_Object specified_data;
7796 int x, y, i;
7797 XImage *ximg, *mask_img = NULL;
7798 struct gcpro gcpro1;
7799 png_struct *png_ptr = NULL;
7800 png_info *info_ptr = NULL, *end_info = NULL;
7801 FILE *volatile fp = NULL;
7802 png_byte sig[8];
7803 png_byte * volatile pixels = NULL;
7804 png_byte ** volatile rows = NULL;
7805 png_uint_32 width, height;
7806 int bit_depth, color_type, interlace_type;
7807 png_byte channels;
7808 png_uint_32 row_bytes;
7809 int transparent_p;
7810 double screen_gamma;
7811 struct png_memory_storage tbr; /* Data to be read */
7812
7813 /* Find out what file to load. */
7814 specified_file = image_spec_value (img->spec, QCfile, NULL);
7815 specified_data = image_spec_value (img->spec, QCdata, NULL);
7816 file = Qnil;
7817 GCPRO1 (file);
7818
7819 if (NILP (specified_data))
7820 {
7821 file = x_find_image_file (specified_file);
7822 if (!STRINGP (file))
7823 {
7824 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7825 UNGCPRO;
7826 return 0;
7827 }
7828
7829 /* Open the image file. */
7830 fp = fopen (SDATA (file), "rb");
7831 if (!fp)
7832 {
7833 image_error ("Cannot open image file `%s'", file, Qnil);
7834 UNGCPRO;
7835 fclose (fp);
7836 return 0;
7837 }
7838
7839 /* Check PNG signature. */
7840 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
7841 || !png_check_sig (sig, sizeof sig))
7842 {
7843 image_error ("Not a PNG file: `%s'", file, Qnil);
7844 UNGCPRO;
7845 fclose (fp);
7846 return 0;
7847 }
7848 }
7849 else
7850 {
7851 /* Read from memory. */
7852 tbr.bytes = SDATA (specified_data);
7853 tbr.len = SBYTES (specified_data);
7854 tbr.index = 0;
7855
7856 /* Check PNG signature. */
7857 if (tbr.len < sizeof sig
7858 || !png_check_sig (tbr.bytes, sizeof sig))
7859 {
7860 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
7861 UNGCPRO;
7862 return 0;
7863 }
7864
7865 /* Need to skip past the signature. */
7866 tbr.bytes += sizeof (sig);
7867 }
7868
7869 /* Initialize read and info structs for PNG lib. */
7870 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
7871 my_png_error, my_png_warning);
7872 if (!png_ptr)
7873 {
7874 if (fp) fclose (fp);
7875 UNGCPRO;
7876 return 0;
7877 }
7878
7879 info_ptr = png_create_info_struct (png_ptr);
7880 if (!info_ptr)
7881 {
7882 png_destroy_read_struct (&png_ptr, NULL, NULL);
7883 if (fp) fclose (fp);
7884 UNGCPRO;
7885 return 0;
7886 }
7887
7888 end_info = png_create_info_struct (png_ptr);
7889 if (!end_info)
7890 {
7891 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
7892 if (fp) fclose (fp);
7893 UNGCPRO;
7894 return 0;
7895 }
7896
7897 /* Set error jump-back. We come back here when the PNG library
7898 detects an error. */
7899 if (setjmp (png_ptr->jmpbuf))
7900 {
7901 error:
7902 if (png_ptr)
7903 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
7904 xfree (pixels);
7905 xfree (rows);
7906 if (fp) fclose (fp);
7907 UNGCPRO;
7908 return 0;
7909 }
7910
7911 /* Read image info. */
7912 if (!NILP (specified_data))
7913 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
7914 else
7915 png_init_io (png_ptr, fp);
7916
7917 png_set_sig_bytes (png_ptr, sizeof sig);
7918 png_read_info (png_ptr, info_ptr);
7919 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
7920 &interlace_type, NULL, NULL);
7921
7922 /* If image contains simply transparency data, we prefer to
7923 construct a clipping mask. */
7924 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
7925 transparent_p = 1;
7926 else
7927 transparent_p = 0;
7928
7929 /* This function is easier to write if we only have to handle
7930 one data format: RGB or RGBA with 8 bits per channel. Let's
7931 transform other formats into that format. */
7932
7933 /* Strip more than 8 bits per channel. */
7934 if (bit_depth == 16)
7935 png_set_strip_16 (png_ptr);
7936
7937 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
7938 if available. */
7939 png_set_expand (png_ptr);
7940
7941 /* Convert grayscale images to RGB. */
7942 if (color_type == PNG_COLOR_TYPE_GRAY
7943 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
7944 png_set_gray_to_rgb (png_ptr);
7945
7946 screen_gamma = (f->gamma ? 1 / f->gamma / 0.45455 : 2.2);
7947
7948 #if 0 /* Avoid double gamma correction for PNG images. */
7949 { /* Tell the PNG lib to handle gamma correction for us. */
7950 int intent;
7951 double image_gamma;
7952 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
7953 if (png_get_sRGB (png_ptr, info_ptr, &intent))
7954 /* The libpng documentation says this is right in this case. */
7955 png_set_gamma (png_ptr, screen_gamma, 0.45455);
7956 else
7957 #endif
7958 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
7959 /* Image contains gamma information. */
7960 png_set_gamma (png_ptr, screen_gamma, image_gamma);
7961 else
7962 /* Use the standard default for the image gamma. */
7963 png_set_gamma (png_ptr, screen_gamma, 0.45455);
7964 }
7965 #endif /* if 0 */
7966
7967 /* Handle alpha channel by combining the image with a background
7968 color. Do this only if a real alpha channel is supplied. For
7969 simple transparency, we prefer a clipping mask. */
7970 if (!transparent_p)
7971 {
7972 png_color_16 *image_bg;
7973 Lisp_Object specified_bg
7974 = image_spec_value (img->spec, QCbackground, NULL);
7975
7976 if (STRINGP (specified_bg))
7977 /* The user specified `:background', use that. */
7978 {
7979 XColor color;
7980 if (x_defined_color (f, SDATA (specified_bg), &color, 0))
7981 {
7982 png_color_16 user_bg;
7983
7984 bzero (&user_bg, sizeof user_bg);
7985 user_bg.red = color.red;
7986 user_bg.green = color.green;
7987 user_bg.blue = color.blue;
7988
7989 png_set_background (png_ptr, &user_bg,
7990 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
7991 }
7992 }
7993 else if (png_get_bKGD (png_ptr, info_ptr, &image_bg))
7994 /* Image contains a background color with which to
7995 combine the image. */
7996 png_set_background (png_ptr, image_bg,
7997 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
7998 else
7999 {
8000 /* Image does not contain a background color with which
8001 to combine the image data via an alpha channel. Use
8002 the frame's background instead. */
8003 XColor color;
8004 Colormap cmap;
8005 png_color_16 frame_background;
8006
8007 cmap = FRAME_X_COLORMAP (f);
8008 color.pixel = FRAME_BACKGROUND_PIXEL (f);
8009 x_query_color (f, &color);
8010
8011 bzero (&frame_background, sizeof frame_background);
8012 frame_background.red = color.red;
8013 frame_background.green = color.green;
8014 frame_background.blue = color.blue;
8015
8016 png_set_background (png_ptr, &frame_background,
8017 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
8018 }
8019 }
8020
8021 /* Update info structure. */
8022 png_read_update_info (png_ptr, info_ptr);
8023
8024 /* Get number of channels. Valid values are 1 for grayscale images
8025 and images with a palette, 2 for grayscale images with transparency
8026 information (alpha channel), 3 for RGB images, and 4 for RGB
8027 images with alpha channel, i.e. RGBA. If conversions above were
8028 sufficient we should only have 3 or 4 channels here. */
8029 channels = png_get_channels (png_ptr, info_ptr);
8030 xassert (channels == 3 || channels == 4);
8031
8032 /* Number of bytes needed for one row of the image. */
8033 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
8034
8035 /* Allocate memory for the image. */
8036 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
8037 rows = (png_byte **) xmalloc (height * sizeof *rows);
8038 for (i = 0; i < height; ++i)
8039 rows[i] = pixels + i * row_bytes;
8040
8041 /* Read the entire image. */
8042 png_read_image (png_ptr, rows);
8043 png_read_end (png_ptr, info_ptr);
8044 if (fp)
8045 {
8046 fclose (fp);
8047 fp = NULL;
8048 }
8049
8050 /* Create the X image and pixmap. */
8051 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
8052 &img->pixmap))
8053 goto error;
8054
8055 /* Create an image and pixmap serving as mask if the PNG image
8056 contains an alpha channel. */
8057 if (channels == 4
8058 && !transparent_p
8059 && !x_create_x_image_and_pixmap (f, width, height, 1,
8060 &mask_img, &img->mask))
8061 {
8062 x_destroy_x_image (ximg);
8063 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
8064 img->pixmap = None;
8065 goto error;
8066 }
8067
8068 /* Fill the X image and mask from PNG data. */
8069 init_color_table ();
8070
8071 for (y = 0; y < height; ++y)
8072 {
8073 png_byte *p = rows[y];
8074
8075 for (x = 0; x < width; ++x)
8076 {
8077 unsigned r, g, b;
8078
8079 r = *p++ << 8;
8080 g = *p++ << 8;
8081 b = *p++ << 8;
8082 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
8083
8084 /* An alpha channel, aka mask channel, associates variable
8085 transparency with an image. Where other image formats
8086 support binary transparency---fully transparent or fully
8087 opaque---PNG allows up to 254 levels of partial transparency.
8088 The PNG library implements partial transparency by combining
8089 the image with a specified background color.
8090
8091 I'm not sure how to handle this here nicely: because the
8092 background on which the image is displayed may change, for
8093 real alpha channel support, it would be necessary to create
8094 a new image for each possible background.
8095
8096 What I'm doing now is that a mask is created if we have
8097 boolean transparency information. Otherwise I'm using
8098 the frame's background color to combine the image with. */
8099
8100 if (channels == 4)
8101 {
8102 if (mask_img)
8103 XPutPixel (mask_img, x, y, *p > 0);
8104 ++p;
8105 }
8106 }
8107 }
8108
8109 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8110 /* Set IMG's background color from the PNG image, unless the user
8111 overrode it. */
8112 {
8113 png_color_16 *bg;
8114 if (png_get_bKGD (png_ptr, info_ptr, &bg))
8115 {
8116 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
8117 img->background_valid = 1;
8118 }
8119 }
8120
8121 /* Remember colors allocated for this image. */
8122 img->colors = colors_in_color_table (&img->ncolors);
8123 free_color_table ();
8124
8125 /* Clean up. */
8126 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
8127 xfree (rows);
8128 xfree (pixels);
8129
8130 img->width = width;
8131 img->height = height;
8132
8133 /* Maybe fill in the background field while we have ximg handy. */
8134 IMAGE_BACKGROUND (img, f, ximg);
8135
8136 /* Put the image into the pixmap, then free the X image and its buffer. */
8137 x_put_x_image (f, ximg, img->pixmap, width, height);
8138 x_destroy_x_image (ximg);
8139
8140 /* Same for the mask. */
8141 if (mask_img)
8142 {
8143 /* Fill in the background_transparent field while we have the mask
8144 handy. */
8145 image_background_transparent (img, f, mask_img);
8146
8147 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
8148 x_destroy_x_image (mask_img);
8149 }
8150
8151 UNGCPRO;
8152 return 1;
8153 }
8154
8155 #endif /* HAVE_PNG != 0 */
8156
8157
8158
8159 /***********************************************************************
8160 JPEG
8161 ***********************************************************************/
8162
8163 #if HAVE_JPEG
8164
8165 /* Work around a warning about HAVE_STDLIB_H being redefined in
8166 jconfig.h. */
8167 #ifdef HAVE_STDLIB_H
8168 #define HAVE_STDLIB_H_1
8169 #undef HAVE_STDLIB_H
8170 #endif /* HAVE_STLIB_H */
8171
8172 #include <jpeglib.h>
8173 #include <jerror.h>
8174 #include <setjmp.h>
8175
8176 #ifdef HAVE_STLIB_H_1
8177 #define HAVE_STDLIB_H 1
8178 #endif
8179
8180 static int jpeg_image_p P_ ((Lisp_Object object));
8181 static int jpeg_load P_ ((struct frame *f, struct image *img));
8182
8183 /* The symbol `jpeg' identifying images of this type. */
8184
8185 Lisp_Object Qjpeg;
8186
8187 /* Indices of image specification fields in gs_format, below. */
8188
8189 enum jpeg_keyword_index
8190 {
8191 JPEG_TYPE,
8192 JPEG_DATA,
8193 JPEG_FILE,
8194 JPEG_ASCENT,
8195 JPEG_MARGIN,
8196 JPEG_RELIEF,
8197 JPEG_ALGORITHM,
8198 JPEG_HEURISTIC_MASK,
8199 JPEG_MASK,
8200 JPEG_BACKGROUND,
8201 JPEG_LAST
8202 };
8203
8204 /* Vector of image_keyword structures describing the format
8205 of valid user-defined image specifications. */
8206
8207 static struct image_keyword jpeg_format[JPEG_LAST] =
8208 {
8209 {":type", IMAGE_SYMBOL_VALUE, 1},
8210 {":data", IMAGE_STRING_VALUE, 0},
8211 {":file", IMAGE_STRING_VALUE, 0},
8212 {":ascent", IMAGE_ASCENT_VALUE, 0},
8213 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
8214 {":relief", IMAGE_INTEGER_VALUE, 0},
8215 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8216 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8217 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8218 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8219 };
8220
8221 /* Structure describing the image type `jpeg'. */
8222
8223 static struct image_type jpeg_type =
8224 {
8225 &Qjpeg,
8226 jpeg_image_p,
8227 jpeg_load,
8228 x_clear_image,
8229 NULL
8230 };
8231
8232
8233 /* Return non-zero if OBJECT is a valid JPEG image specification. */
8234
8235 static int
8236 jpeg_image_p (object)
8237 Lisp_Object object;
8238 {
8239 struct image_keyword fmt[JPEG_LAST];
8240
8241 bcopy (jpeg_format, fmt, sizeof fmt);
8242
8243 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
8244 return 0;
8245
8246 /* Must specify either the :data or :file keyword. */
8247 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
8248 }
8249
8250
8251 struct my_jpeg_error_mgr
8252 {
8253 struct jpeg_error_mgr pub;
8254 jmp_buf setjmp_buffer;
8255 };
8256
8257
8258 static void
8259 my_error_exit (cinfo)
8260 j_common_ptr cinfo;
8261 {
8262 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
8263 longjmp (mgr->setjmp_buffer, 1);
8264 }
8265
8266
8267 /* Init source method for JPEG data source manager. Called by
8268 jpeg_read_header() before any data is actually read. See
8269 libjpeg.doc from the JPEG lib distribution. */
8270
8271 static void
8272 our_init_source (cinfo)
8273 j_decompress_ptr cinfo;
8274 {
8275 }
8276
8277
8278 /* Fill input buffer method for JPEG data source manager. Called
8279 whenever more data is needed. We read the whole image in one step,
8280 so this only adds a fake end of input marker at the end. */
8281
8282 static boolean
8283 our_fill_input_buffer (cinfo)
8284 j_decompress_ptr cinfo;
8285 {
8286 /* Insert a fake EOI marker. */
8287 struct jpeg_source_mgr *src = cinfo->src;
8288 static JOCTET buffer[2];
8289
8290 buffer[0] = (JOCTET) 0xFF;
8291 buffer[1] = (JOCTET) JPEG_EOI;
8292
8293 src->next_input_byte = buffer;
8294 src->bytes_in_buffer = 2;
8295 return TRUE;
8296 }
8297
8298
8299 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
8300 is the JPEG data source manager. */
8301
8302 static void
8303 our_skip_input_data (cinfo, num_bytes)
8304 j_decompress_ptr cinfo;
8305 long num_bytes;
8306 {
8307 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
8308
8309 if (src)
8310 {
8311 if (num_bytes > src->bytes_in_buffer)
8312 ERREXIT (cinfo, JERR_INPUT_EOF);
8313
8314 src->bytes_in_buffer -= num_bytes;
8315 src->next_input_byte += num_bytes;
8316 }
8317 }
8318
8319
8320 /* Method to terminate data source. Called by
8321 jpeg_finish_decompress() after all data has been processed. */
8322
8323 static void
8324 our_term_source (cinfo)
8325 j_decompress_ptr cinfo;
8326 {
8327 }
8328
8329
8330 /* Set up the JPEG lib for reading an image from DATA which contains
8331 LEN bytes. CINFO is the decompression info structure created for
8332 reading the image. */
8333
8334 static void
8335 jpeg_memory_src (cinfo, data, len)
8336 j_decompress_ptr cinfo;
8337 JOCTET *data;
8338 unsigned int len;
8339 {
8340 struct jpeg_source_mgr *src;
8341
8342 if (cinfo->src == NULL)
8343 {
8344 /* First time for this JPEG object? */
8345 cinfo->src = (struct jpeg_source_mgr *)
8346 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
8347 sizeof (struct jpeg_source_mgr));
8348 src = (struct jpeg_source_mgr *) cinfo->src;
8349 src->next_input_byte = data;
8350 }
8351
8352 src = (struct jpeg_source_mgr *) cinfo->src;
8353 src->init_source = our_init_source;
8354 src->fill_input_buffer = our_fill_input_buffer;
8355 src->skip_input_data = our_skip_input_data;
8356 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
8357 src->term_source = our_term_source;
8358 src->bytes_in_buffer = len;
8359 src->next_input_byte = data;
8360 }
8361
8362
8363 /* Load image IMG for use on frame F. Patterned after example.c
8364 from the JPEG lib. */
8365
8366 static int
8367 jpeg_load (f, img)
8368 struct frame *f;
8369 struct image *img;
8370 {
8371 struct jpeg_decompress_struct cinfo;
8372 struct my_jpeg_error_mgr mgr;
8373 Lisp_Object file, specified_file;
8374 Lisp_Object specified_data;
8375 FILE * volatile fp = NULL;
8376 JSAMPARRAY buffer;
8377 int row_stride, x, y;
8378 XImage *ximg = NULL;
8379 int rc;
8380 unsigned long *colors;
8381 int width, height;
8382 struct gcpro gcpro1;
8383
8384 /* Open the JPEG file. */
8385 specified_file = image_spec_value (img->spec, QCfile, NULL);
8386 specified_data = image_spec_value (img->spec, QCdata, NULL);
8387 file = Qnil;
8388 GCPRO1 (file);
8389
8390 if (NILP (specified_data))
8391 {
8392 file = x_find_image_file (specified_file);
8393 if (!STRINGP (file))
8394 {
8395 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8396 UNGCPRO;
8397 return 0;
8398 }
8399
8400 fp = fopen (SDATA (file), "r");
8401 if (fp == NULL)
8402 {
8403 image_error ("Cannot open `%s'", file, Qnil);
8404 UNGCPRO;
8405 return 0;
8406 }
8407 }
8408
8409 /* Customize libjpeg's error handling to call my_error_exit when an
8410 error is detected. This function will perform a longjmp. */
8411 cinfo.err = jpeg_std_error (&mgr.pub);
8412 mgr.pub.error_exit = my_error_exit;
8413
8414 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
8415 {
8416 if (rc == 1)
8417 {
8418 /* Called from my_error_exit. Display a JPEG error. */
8419 char buffer[JMSG_LENGTH_MAX];
8420 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
8421 image_error ("Error reading JPEG image `%s': %s", img->spec,
8422 build_string (buffer));
8423 }
8424
8425 /* Close the input file and destroy the JPEG object. */
8426 if (fp)
8427 fclose ((FILE *) fp);
8428 jpeg_destroy_decompress (&cinfo);
8429
8430 /* If we already have an XImage, free that. */
8431 x_destroy_x_image (ximg);
8432
8433 /* Free pixmap and colors. */
8434 x_clear_image (f, img);
8435
8436 UNGCPRO;
8437 return 0;
8438 }
8439
8440 /* Create the JPEG decompression object. Let it read from fp.
8441 Read the JPEG image header. */
8442 jpeg_create_decompress (&cinfo);
8443
8444 if (NILP (specified_data))
8445 jpeg_stdio_src (&cinfo, (FILE *) fp);
8446 else
8447 jpeg_memory_src (&cinfo, SDATA (specified_data),
8448 SBYTES (specified_data));
8449
8450 jpeg_read_header (&cinfo, TRUE);
8451
8452 /* Customize decompression so that color quantization will be used.
8453 Start decompression. */
8454 cinfo.quantize_colors = TRUE;
8455 jpeg_start_decompress (&cinfo);
8456 width = img->width = cinfo.output_width;
8457 height = img->height = cinfo.output_height;
8458
8459 /* Create X image and pixmap. */
8460 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
8461 longjmp (mgr.setjmp_buffer, 2);
8462
8463 /* Allocate colors. When color quantization is used,
8464 cinfo.actual_number_of_colors has been set with the number of
8465 colors generated, and cinfo.colormap is a two-dimensional array
8466 of color indices in the range 0..cinfo.actual_number_of_colors.
8467 No more than 255 colors will be generated. */
8468 {
8469 int i, ir, ig, ib;
8470
8471 if (cinfo.out_color_components > 2)
8472 ir = 0, ig = 1, ib = 2;
8473 else if (cinfo.out_color_components > 1)
8474 ir = 0, ig = 1, ib = 0;
8475 else
8476 ir = 0, ig = 0, ib = 0;
8477
8478 /* Use the color table mechanism because it handles colors that
8479 cannot be allocated nicely. Such colors will be replaced with
8480 a default color, and we don't have to care about which colors
8481 can be freed safely, and which can't. */
8482 init_color_table ();
8483 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
8484 * sizeof *colors);
8485
8486 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
8487 {
8488 /* Multiply RGB values with 255 because X expects RGB values
8489 in the range 0..0xffff. */
8490 int r = cinfo.colormap[ir][i] << 8;
8491 int g = cinfo.colormap[ig][i] << 8;
8492 int b = cinfo.colormap[ib][i] << 8;
8493 colors[i] = lookup_rgb_color (f, r, g, b);
8494 }
8495
8496 /* Remember those colors actually allocated. */
8497 img->colors = colors_in_color_table (&img->ncolors);
8498 free_color_table ();
8499 }
8500
8501 /* Read pixels. */
8502 row_stride = width * cinfo.output_components;
8503 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
8504 row_stride, 1);
8505 for (y = 0; y < height; ++y)
8506 {
8507 jpeg_read_scanlines (&cinfo, buffer, 1);
8508 for (x = 0; x < cinfo.output_width; ++x)
8509 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
8510 }
8511
8512 /* Clean up. */
8513 jpeg_finish_decompress (&cinfo);
8514 jpeg_destroy_decompress (&cinfo);
8515 if (fp)
8516 fclose ((FILE *) fp);
8517
8518 /* Maybe fill in the background field while we have ximg handy. */
8519 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8520 IMAGE_BACKGROUND (img, f, ximg);
8521
8522 /* Put the image into the pixmap. */
8523 x_put_x_image (f, ximg, img->pixmap, width, height);
8524 x_destroy_x_image (ximg);
8525 UNGCPRO;
8526 return 1;
8527 }
8528
8529 #endif /* HAVE_JPEG */
8530
8531
8532
8533 /***********************************************************************
8534 TIFF
8535 ***********************************************************************/
8536
8537 #if HAVE_TIFF
8538
8539 #include <tiffio.h>
8540
8541 static int tiff_image_p P_ ((Lisp_Object object));
8542 static int tiff_load P_ ((struct frame *f, struct image *img));
8543
8544 /* The symbol `tiff' identifying images of this type. */
8545
8546 Lisp_Object Qtiff;
8547
8548 /* Indices of image specification fields in tiff_format, below. */
8549
8550 enum tiff_keyword_index
8551 {
8552 TIFF_TYPE,
8553 TIFF_DATA,
8554 TIFF_FILE,
8555 TIFF_ASCENT,
8556 TIFF_MARGIN,
8557 TIFF_RELIEF,
8558 TIFF_ALGORITHM,
8559 TIFF_HEURISTIC_MASK,
8560 TIFF_MASK,
8561 TIFF_BACKGROUND,
8562 TIFF_LAST
8563 };
8564
8565 /* Vector of image_keyword structures describing the format
8566 of valid user-defined image specifications. */
8567
8568 static struct image_keyword tiff_format[TIFF_LAST] =
8569 {
8570 {":type", IMAGE_SYMBOL_VALUE, 1},
8571 {":data", IMAGE_STRING_VALUE, 0},
8572 {":file", IMAGE_STRING_VALUE, 0},
8573 {":ascent", IMAGE_ASCENT_VALUE, 0},
8574 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
8575 {":relief", IMAGE_INTEGER_VALUE, 0},
8576 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8577 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8578 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8579 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8580 };
8581
8582 /* Structure describing the image type `tiff'. */
8583
8584 static struct image_type tiff_type =
8585 {
8586 &Qtiff,
8587 tiff_image_p,
8588 tiff_load,
8589 x_clear_image,
8590 NULL
8591 };
8592
8593
8594 /* Return non-zero if OBJECT is a valid TIFF image specification. */
8595
8596 static int
8597 tiff_image_p (object)
8598 Lisp_Object object;
8599 {
8600 struct image_keyword fmt[TIFF_LAST];
8601 bcopy (tiff_format, fmt, sizeof fmt);
8602
8603 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
8604 return 0;
8605
8606 /* Must specify either the :data or :file keyword. */
8607 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
8608 }
8609
8610
8611 /* Reading from a memory buffer for TIFF images Based on the PNG
8612 memory source, but we have to provide a lot of extra functions.
8613 Blah.
8614
8615 We really only need to implement read and seek, but I am not
8616 convinced that the TIFF library is smart enough not to destroy
8617 itself if we only hand it the function pointers we need to
8618 override. */
8619
8620 typedef struct
8621 {
8622 unsigned char *bytes;
8623 size_t len;
8624 int index;
8625 }
8626 tiff_memory_source;
8627
8628
8629 static size_t
8630 tiff_read_from_memory (data, buf, size)
8631 thandle_t data;
8632 tdata_t buf;
8633 tsize_t size;
8634 {
8635 tiff_memory_source *src = (tiff_memory_source *) data;
8636
8637 if (size > src->len - src->index)
8638 return (size_t) -1;
8639 bcopy (src->bytes + src->index, buf, size);
8640 src->index += size;
8641 return size;
8642 }
8643
8644
8645 static size_t
8646 tiff_write_from_memory (data, buf, size)
8647 thandle_t data;
8648 tdata_t buf;
8649 tsize_t size;
8650 {
8651 return (size_t) -1;
8652 }
8653
8654
8655 static toff_t
8656 tiff_seek_in_memory (data, off, whence)
8657 thandle_t data;
8658 toff_t off;
8659 int whence;
8660 {
8661 tiff_memory_source *src = (tiff_memory_source *) data;
8662 int idx;
8663
8664 switch (whence)
8665 {
8666 case SEEK_SET: /* Go from beginning of source. */
8667 idx = off;
8668 break;
8669
8670 case SEEK_END: /* Go from end of source. */
8671 idx = src->len + off;
8672 break;
8673
8674 case SEEK_CUR: /* Go from current position. */
8675 idx = src->index + off;
8676 break;
8677
8678 default: /* Invalid `whence'. */
8679 return -1;
8680 }
8681
8682 if (idx > src->len || idx < 0)
8683 return -1;
8684
8685 src->index = idx;
8686 return src->index;
8687 }
8688
8689
8690 static int
8691 tiff_close_memory (data)
8692 thandle_t data;
8693 {
8694 /* NOOP */
8695 return 0;
8696 }
8697
8698
8699 static int
8700 tiff_mmap_memory (data, pbase, psize)
8701 thandle_t data;
8702 tdata_t *pbase;
8703 toff_t *psize;
8704 {
8705 /* It is already _IN_ memory. */
8706 return 0;
8707 }
8708
8709
8710 static void
8711 tiff_unmap_memory (data, base, size)
8712 thandle_t data;
8713 tdata_t base;
8714 toff_t size;
8715 {
8716 /* We don't need to do this. */
8717 }
8718
8719
8720 static toff_t
8721 tiff_size_of_memory (data)
8722 thandle_t data;
8723 {
8724 return ((tiff_memory_source *) data)->len;
8725 }
8726
8727
8728 static void
8729 tiff_error_handler (title, format, ap)
8730 const char *title, *format;
8731 va_list ap;
8732 {
8733 char buf[512];
8734 int len;
8735
8736 len = sprintf (buf, "TIFF error: %s ", title);
8737 vsprintf (buf + len, format, ap);
8738 add_to_log (buf, Qnil, Qnil);
8739 }
8740
8741
8742 static void
8743 tiff_warning_handler (title, format, ap)
8744 const char *title, *format;
8745 va_list ap;
8746 {
8747 char buf[512];
8748 int len;
8749
8750 len = sprintf (buf, "TIFF warning: %s ", title);
8751 vsprintf (buf + len, format, ap);
8752 add_to_log (buf, Qnil, Qnil);
8753 }
8754
8755
8756 /* Load TIFF image IMG for use on frame F. Value is non-zero if
8757 successful. */
8758
8759 static int
8760 tiff_load (f, img)
8761 struct frame *f;
8762 struct image *img;
8763 {
8764 Lisp_Object file, specified_file;
8765 Lisp_Object specified_data;
8766 TIFF *tiff;
8767 int width, height, x, y;
8768 uint32 *buf;
8769 int rc;
8770 XImage *ximg;
8771 struct gcpro gcpro1;
8772 tiff_memory_source memsrc;
8773
8774 specified_file = image_spec_value (img->spec, QCfile, NULL);
8775 specified_data = image_spec_value (img->spec, QCdata, NULL);
8776 file = Qnil;
8777 GCPRO1 (file);
8778
8779 TIFFSetErrorHandler (tiff_error_handler);
8780 TIFFSetWarningHandler (tiff_warning_handler);
8781
8782 if (NILP (specified_data))
8783 {
8784 /* Read from a file */
8785 file = x_find_image_file (specified_file);
8786 if (!STRINGP (file))
8787 {
8788 image_error ("Cannot find image file `%s'", file, Qnil);
8789 UNGCPRO;
8790 return 0;
8791 }
8792
8793 /* Try to open the image file. */
8794 tiff = TIFFOpen (SDATA (file), "r");
8795 if (tiff == NULL)
8796 {
8797 image_error ("Cannot open `%s'", file, Qnil);
8798 UNGCPRO;
8799 return 0;
8800 }
8801 }
8802 else
8803 {
8804 /* Memory source! */
8805 memsrc.bytes = SDATA (specified_data);
8806 memsrc.len = SBYTES (specified_data);
8807 memsrc.index = 0;
8808
8809 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
8810 (TIFFReadWriteProc) tiff_read_from_memory,
8811 (TIFFReadWriteProc) tiff_write_from_memory,
8812 tiff_seek_in_memory,
8813 tiff_close_memory,
8814 tiff_size_of_memory,
8815 tiff_mmap_memory,
8816 tiff_unmap_memory);
8817
8818 if (!tiff)
8819 {
8820 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
8821 UNGCPRO;
8822 return 0;
8823 }
8824 }
8825
8826 /* Get width and height of the image, and allocate a raster buffer
8827 of width x height 32-bit values. */
8828 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
8829 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
8830 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
8831
8832 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
8833 TIFFClose (tiff);
8834 if (!rc)
8835 {
8836 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
8837 xfree (buf);
8838 UNGCPRO;
8839 return 0;
8840 }
8841
8842 /* Create the X image and pixmap. */
8843 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
8844 {
8845 xfree (buf);
8846 UNGCPRO;
8847 return 0;
8848 }
8849
8850 /* Initialize the color table. */
8851 init_color_table ();
8852
8853 /* Process the pixel raster. Origin is in the lower-left corner. */
8854 for (y = 0; y < height; ++y)
8855 {
8856 uint32 *row = buf + y * width;
8857
8858 for (x = 0; x < width; ++x)
8859 {
8860 uint32 abgr = row[x];
8861 int r = TIFFGetR (abgr) << 8;
8862 int g = TIFFGetG (abgr) << 8;
8863 int b = TIFFGetB (abgr) << 8;
8864 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
8865 }
8866 }
8867
8868 /* Remember the colors allocated for the image. Free the color table. */
8869 img->colors = colors_in_color_table (&img->ncolors);
8870 free_color_table ();
8871
8872 img->width = width;
8873 img->height = height;
8874
8875 /* Maybe fill in the background field while we have ximg handy. */
8876 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8877 IMAGE_BACKGROUND (img, f, ximg);
8878
8879 /* Put the image into the pixmap, then free the X image and its buffer. */
8880 x_put_x_image (f, ximg, img->pixmap, width, height);
8881 x_destroy_x_image (ximg);
8882 xfree (buf);
8883
8884 UNGCPRO;
8885 return 1;
8886 }
8887
8888 #endif /* HAVE_TIFF != 0 */
8889
8890
8891
8892 /***********************************************************************
8893 GIF
8894 ***********************************************************************/
8895
8896 #if HAVE_GIF
8897
8898 #include <gif_lib.h>
8899
8900 static int gif_image_p P_ ((Lisp_Object object));
8901 static int gif_load P_ ((struct frame *f, struct image *img));
8902
8903 /* The symbol `gif' identifying images of this type. */
8904
8905 Lisp_Object Qgif;
8906
8907 /* Indices of image specification fields in gif_format, below. */
8908
8909 enum gif_keyword_index
8910 {
8911 GIF_TYPE,
8912 GIF_DATA,
8913 GIF_FILE,
8914 GIF_ASCENT,
8915 GIF_MARGIN,
8916 GIF_RELIEF,
8917 GIF_ALGORITHM,
8918 GIF_HEURISTIC_MASK,
8919 GIF_MASK,
8920 GIF_IMAGE,
8921 GIF_BACKGROUND,
8922 GIF_LAST
8923 };
8924
8925 /* Vector of image_keyword structures describing the format
8926 of valid user-defined image specifications. */
8927
8928 static struct image_keyword gif_format[GIF_LAST] =
8929 {
8930 {":type", IMAGE_SYMBOL_VALUE, 1},
8931 {":data", IMAGE_STRING_VALUE, 0},
8932 {":file", IMAGE_STRING_VALUE, 0},
8933 {":ascent", IMAGE_ASCENT_VALUE, 0},
8934 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
8935 {":relief", IMAGE_INTEGER_VALUE, 0},
8936 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8937 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8938 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8939 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8940 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8941 };
8942
8943 /* Structure describing the image type `gif'. */
8944
8945 static struct image_type gif_type =
8946 {
8947 &Qgif,
8948 gif_image_p,
8949 gif_load,
8950 x_clear_image,
8951 NULL
8952 };
8953
8954
8955 /* Return non-zero if OBJECT is a valid GIF image specification. */
8956
8957 static int
8958 gif_image_p (object)
8959 Lisp_Object object;
8960 {
8961 struct image_keyword fmt[GIF_LAST];
8962 bcopy (gif_format, fmt, sizeof fmt);
8963
8964 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
8965 return 0;
8966
8967 /* Must specify either the :data or :file keyword. */
8968 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
8969 }
8970
8971
8972 /* Reading a GIF image from memory
8973 Based on the PNG memory stuff to a certain extent. */
8974
8975 typedef struct
8976 {
8977 unsigned char *bytes;
8978 size_t len;
8979 int index;
8980 }
8981 gif_memory_source;
8982
8983
8984 /* Make the current memory source available to gif_read_from_memory.
8985 It's done this way because not all versions of libungif support
8986 a UserData field in the GifFileType structure. */
8987 static gif_memory_source *current_gif_memory_src;
8988
8989 static int
8990 gif_read_from_memory (file, buf, len)
8991 GifFileType *file;
8992 GifByteType *buf;
8993 int len;
8994 {
8995 gif_memory_source *src = current_gif_memory_src;
8996
8997 if (len > src->len - src->index)
8998 return -1;
8999
9000 bcopy (src->bytes + src->index, buf, len);
9001 src->index += len;
9002 return len;
9003 }
9004
9005
9006 /* Load GIF image IMG for use on frame F. Value is non-zero if
9007 successful. */
9008
9009 static int
9010 gif_load (f, img)
9011 struct frame *f;
9012 struct image *img;
9013 {
9014 Lisp_Object file, specified_file;
9015 Lisp_Object specified_data;
9016 int rc, width, height, x, y, i;
9017 XImage *ximg;
9018 ColorMapObject *gif_color_map;
9019 unsigned long pixel_colors[256];
9020 GifFileType *gif;
9021 struct gcpro gcpro1;
9022 Lisp_Object image;
9023 int ino, image_left, image_top, image_width, image_height;
9024 gif_memory_source memsrc;
9025 unsigned char *raster;
9026
9027 specified_file = image_spec_value (img->spec, QCfile, NULL);
9028 specified_data = image_spec_value (img->spec, QCdata, NULL);
9029 file = Qnil;
9030 GCPRO1 (file);
9031
9032 if (NILP (specified_data))
9033 {
9034 file = x_find_image_file (specified_file);
9035 if (!STRINGP (file))
9036 {
9037 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9038 UNGCPRO;
9039 return 0;
9040 }
9041
9042 /* Open the GIF file. */
9043 gif = DGifOpenFileName (SDATA (file));
9044 if (gif == NULL)
9045 {
9046 image_error ("Cannot open `%s'", file, Qnil);
9047 UNGCPRO;
9048 return 0;
9049 }
9050 }
9051 else
9052 {
9053 /* Read from memory! */
9054 current_gif_memory_src = &memsrc;
9055 memsrc.bytes = SDATA (specified_data);
9056 memsrc.len = SBYTES (specified_data);
9057 memsrc.index = 0;
9058
9059 gif = DGifOpen (&memsrc, gif_read_from_memory);
9060 if (!gif)
9061 {
9062 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
9063 UNGCPRO;
9064 return 0;
9065 }
9066 }
9067
9068 /* Read entire contents. */
9069 rc = DGifSlurp (gif);
9070 if (rc == GIF_ERROR)
9071 {
9072 image_error ("Error reading `%s'", img->spec, Qnil);
9073 DGifCloseFile (gif);
9074 UNGCPRO;
9075 return 0;
9076 }
9077
9078 image = image_spec_value (img->spec, QCindex, NULL);
9079 ino = INTEGERP (image) ? XFASTINT (image) : 0;
9080 if (ino >= gif->ImageCount)
9081 {
9082 image_error ("Invalid image number `%s' in image `%s'",
9083 image, img->spec);
9084 DGifCloseFile (gif);
9085 UNGCPRO;
9086 return 0;
9087 }
9088
9089 width = img->width = max (gif->SWidth, gif->Image.Left + gif->Image.Width);
9090 height = img->height = max (gif->SHeight, gif->Image.Top + gif->Image.Height);
9091
9092 /* Create the X image and pixmap. */
9093 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9094 {
9095 DGifCloseFile (gif);
9096 UNGCPRO;
9097 return 0;
9098 }
9099
9100 /* Allocate colors. */
9101 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
9102 if (!gif_color_map)
9103 gif_color_map = gif->SColorMap;
9104 init_color_table ();
9105 bzero (pixel_colors, sizeof pixel_colors);
9106
9107 for (i = 0; i < gif_color_map->ColorCount; ++i)
9108 {
9109 int r = gif_color_map->Colors[i].Red << 8;
9110 int g = gif_color_map->Colors[i].Green << 8;
9111 int b = gif_color_map->Colors[i].Blue << 8;
9112 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
9113 }
9114
9115 img->colors = colors_in_color_table (&img->ncolors);
9116 free_color_table ();
9117
9118 /* Clear the part of the screen image that are not covered by
9119 the image from the GIF file. Full animated GIF support
9120 requires more than can be done here (see the gif89 spec,
9121 disposal methods). Let's simply assume that the part
9122 not covered by a sub-image is in the frame's background color. */
9123 image_top = gif->SavedImages[ino].ImageDesc.Top;
9124 image_left = gif->SavedImages[ino].ImageDesc.Left;
9125 image_width = gif->SavedImages[ino].ImageDesc.Width;
9126 image_height = gif->SavedImages[ino].ImageDesc.Height;
9127
9128 for (y = 0; y < image_top; ++y)
9129 for (x = 0; x < width; ++x)
9130 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9131
9132 for (y = image_top + image_height; y < height; ++y)
9133 for (x = 0; x < width; ++x)
9134 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9135
9136 for (y = image_top; y < image_top + image_height; ++y)
9137 {
9138 for (x = 0; x < image_left; ++x)
9139 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9140 for (x = image_left + image_width; x < width; ++x)
9141 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9142 }
9143
9144 /* Read the GIF image into the X image. We use a local variable
9145 `raster' here because RasterBits below is a char *, and invites
9146 problems with bytes >= 0x80. */
9147 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
9148
9149 if (gif->SavedImages[ino].ImageDesc.Interlace)
9150 {
9151 static int interlace_start[] = {0, 4, 2, 1};
9152 static int interlace_increment[] = {8, 8, 4, 2};
9153 int pass;
9154 int row = interlace_start[0];
9155
9156 pass = 0;
9157
9158 for (y = 0; y < image_height; y++)
9159 {
9160 if (row >= image_height)
9161 {
9162 row = interlace_start[++pass];
9163 while (row >= image_height)
9164 row = interlace_start[++pass];
9165 }
9166
9167 for (x = 0; x < image_width; x++)
9168 {
9169 int i = raster[(y * image_width) + x];
9170 XPutPixel (ximg, x + image_left, row + image_top,
9171 pixel_colors[i]);
9172 }
9173
9174 row += interlace_increment[pass];
9175 }
9176 }
9177 else
9178 {
9179 for (y = 0; y < image_height; ++y)
9180 for (x = 0; x < image_width; ++x)
9181 {
9182 int i = raster[y * image_width + x];
9183 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
9184 }
9185 }
9186
9187 DGifCloseFile (gif);
9188
9189 /* Maybe fill in the background field while we have ximg handy. */
9190 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9191 IMAGE_BACKGROUND (img, f, ximg);
9192
9193 /* Put the image into the pixmap, then free the X image and its buffer. */
9194 x_put_x_image (f, ximg, img->pixmap, width, height);
9195 x_destroy_x_image (ximg);
9196
9197 UNGCPRO;
9198 return 1;
9199 }
9200
9201 #endif /* HAVE_GIF != 0 */
9202
9203
9204
9205 /***********************************************************************
9206 Ghostscript
9207 ***********************************************************************/
9208
9209 static int gs_image_p P_ ((Lisp_Object object));
9210 static int gs_load P_ ((struct frame *f, struct image *img));
9211 static void gs_clear_image P_ ((struct frame *f, struct image *img));
9212
9213 /* The symbol `postscript' identifying images of this type. */
9214
9215 Lisp_Object Qpostscript;
9216
9217 /* Keyword symbols. */
9218
9219 Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
9220
9221 /* Indices of image specification fields in gs_format, below. */
9222
9223 enum gs_keyword_index
9224 {
9225 GS_TYPE,
9226 GS_PT_WIDTH,
9227 GS_PT_HEIGHT,
9228 GS_FILE,
9229 GS_LOADER,
9230 GS_BOUNDING_BOX,
9231 GS_ASCENT,
9232 GS_MARGIN,
9233 GS_RELIEF,
9234 GS_ALGORITHM,
9235 GS_HEURISTIC_MASK,
9236 GS_MASK,
9237 GS_BACKGROUND,
9238 GS_LAST
9239 };
9240
9241 /* Vector of image_keyword structures describing the format
9242 of valid user-defined image specifications. */
9243
9244 static struct image_keyword gs_format[GS_LAST] =
9245 {
9246 {":type", IMAGE_SYMBOL_VALUE, 1},
9247 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9248 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9249 {":file", IMAGE_STRING_VALUE, 1},
9250 {":loader", IMAGE_FUNCTION_VALUE, 0},
9251 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
9252 {":ascent", IMAGE_ASCENT_VALUE, 0},
9253 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
9254 {":relief", IMAGE_INTEGER_VALUE, 0},
9255 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9256 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9257 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9258 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
9259 };
9260
9261 /* Structure describing the image type `ghostscript'. */
9262
9263 static struct image_type gs_type =
9264 {
9265 &Qpostscript,
9266 gs_image_p,
9267 gs_load,
9268 gs_clear_image,
9269 NULL
9270 };
9271
9272
9273 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9274
9275 static void
9276 gs_clear_image (f, img)
9277 struct frame *f;
9278 struct image *img;
9279 {
9280 /* IMG->data.ptr_val may contain a recorded colormap. */
9281 xfree (img->data.ptr_val);
9282 x_clear_image (f, img);
9283 }
9284
9285
9286 /* Return non-zero if OBJECT is a valid Ghostscript image
9287 specification. */
9288
9289 static int
9290 gs_image_p (object)
9291 Lisp_Object object;
9292 {
9293 struct image_keyword fmt[GS_LAST];
9294 Lisp_Object tem;
9295 int i;
9296
9297 bcopy (gs_format, fmt, sizeof fmt);
9298
9299 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
9300 return 0;
9301
9302 /* Bounding box must be a list or vector containing 4 integers. */
9303 tem = fmt[GS_BOUNDING_BOX].value;
9304 if (CONSP (tem))
9305 {
9306 for (i = 0; i < 4; ++i, tem = XCDR (tem))
9307 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
9308 return 0;
9309 if (!NILP (tem))
9310 return 0;
9311 }
9312 else if (VECTORP (tem))
9313 {
9314 if (XVECTOR (tem)->size != 4)
9315 return 0;
9316 for (i = 0; i < 4; ++i)
9317 if (!INTEGERP (XVECTOR (tem)->contents[i]))
9318 return 0;
9319 }
9320 else
9321 return 0;
9322
9323 return 1;
9324 }
9325
9326
9327 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
9328 if successful. */
9329
9330 static int
9331 gs_load (f, img)
9332 struct frame *f;
9333 struct image *img;
9334 {
9335 char buffer[100];
9336 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
9337 struct gcpro gcpro1, gcpro2;
9338 Lisp_Object frame;
9339 double in_width, in_height;
9340 Lisp_Object pixel_colors = Qnil;
9341
9342 /* Compute pixel size of pixmap needed from the given size in the
9343 image specification. Sizes in the specification are in pt. 1 pt
9344 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9345 info. */
9346 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
9347 in_width = XFASTINT (pt_width) / 72.0;
9348 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
9349 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
9350 in_height = XFASTINT (pt_height) / 72.0;
9351 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
9352
9353 /* Create the pixmap. */
9354 xassert (img->pixmap == None);
9355 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9356 img->width, img->height,
9357 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
9358
9359 if (!img->pixmap)
9360 {
9361 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
9362 return 0;
9363 }
9364
9365 /* Call the loader to fill the pixmap. It returns a process object
9366 if successful. We do not record_unwind_protect here because
9367 other places in redisplay like calling window scroll functions
9368 don't either. Let the Lisp loader use `unwind-protect' instead. */
9369 GCPRO2 (window_and_pixmap_id, pixel_colors);
9370
9371 sprintf (buffer, "%lu %lu",
9372 (unsigned long) FRAME_X_WINDOW (f),
9373 (unsigned long) img->pixmap);
9374 window_and_pixmap_id = build_string (buffer);
9375
9376 sprintf (buffer, "%lu %lu",
9377 FRAME_FOREGROUND_PIXEL (f),
9378 FRAME_BACKGROUND_PIXEL (f));
9379 pixel_colors = build_string (buffer);
9380
9381 XSETFRAME (frame, f);
9382 loader = image_spec_value (img->spec, QCloader, NULL);
9383 if (NILP (loader))
9384 loader = intern ("gs-load-image");
9385
9386 img->data.lisp_val = call6 (loader, frame, img->spec,
9387 make_number (img->width),
9388 make_number (img->height),
9389 window_and_pixmap_id,
9390 pixel_colors);
9391 UNGCPRO;
9392 return PROCESSP (img->data.lisp_val);
9393 }
9394
9395
9396 /* Kill the Ghostscript process that was started to fill PIXMAP on
9397 frame F. Called from XTread_socket when receiving an event
9398 telling Emacs that Ghostscript has finished drawing. */
9399
9400 void
9401 x_kill_gs_process (pixmap, f)
9402 Pixmap pixmap;
9403 struct frame *f;
9404 {
9405 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9406 int class, i;
9407 struct image *img;
9408
9409 /* Find the image containing PIXMAP. */
9410 for (i = 0; i < c->used; ++i)
9411 if (c->images[i]->pixmap == pixmap)
9412 break;
9413
9414 /* Should someone in between have cleared the image cache, for
9415 instance, give up. */
9416 if (i == c->used)
9417 return;
9418
9419 /* Kill the GS process. We should have found PIXMAP in the image
9420 cache and its image should contain a process object. */
9421 img = c->images[i];
9422 xassert (PROCESSP (img->data.lisp_val));
9423 Fkill_process (img->data.lisp_val, Qnil);
9424 img->data.lisp_val = Qnil;
9425
9426 /* On displays with a mutable colormap, figure out the colors
9427 allocated for the image by looking at the pixels of an XImage for
9428 img->pixmap. */
9429 class = FRAME_X_VISUAL (f)->class;
9430 if (class != StaticColor && class != StaticGray && class != TrueColor)
9431 {
9432 XImage *ximg;
9433
9434 BLOCK_INPUT;
9435
9436 /* Try to get an XImage for img->pixmep. */
9437 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
9438 0, 0, img->width, img->height, ~0, ZPixmap);
9439 if (ximg)
9440 {
9441 int x, y;
9442
9443 /* Initialize the color table. */
9444 init_color_table ();
9445
9446 /* For each pixel of the image, look its color up in the
9447 color table. After having done so, the color table will
9448 contain an entry for each color used by the image. */
9449 for (y = 0; y < img->height; ++y)
9450 for (x = 0; x < img->width; ++x)
9451 {
9452 unsigned long pixel = XGetPixel (ximg, x, y);
9453 lookup_pixel_color (f, pixel);
9454 }
9455
9456 /* Record colors in the image. Free color table and XImage. */
9457 img->colors = colors_in_color_table (&img->ncolors);
9458 free_color_table ();
9459 XDestroyImage (ximg);
9460
9461 #if 0 /* This doesn't seem to be the case. If we free the colors
9462 here, we get a BadAccess later in x_clear_image when
9463 freeing the colors. */
9464 /* We have allocated colors once, but Ghostscript has also
9465 allocated colors on behalf of us. So, to get the
9466 reference counts right, free them once. */
9467 if (img->ncolors)
9468 x_free_colors (f, img->colors, img->ncolors);
9469 #endif
9470 }
9471 else
9472 image_error ("Cannot get X image of `%s'; colors will not be freed",
9473 img->spec, Qnil);
9474
9475 UNBLOCK_INPUT;
9476 }
9477
9478 /* Now that we have the pixmap, compute mask and transform the
9479 image if requested. */
9480 BLOCK_INPUT;
9481 postprocess_image (f, img);
9482 UNBLOCK_INPUT;
9483 }
9484
9485
9486
9487 /***********************************************************************
9488 Window properties 3963 Window properties
9489 ***********************************************************************/ 3964 ***********************************************************************/
9490 3965
9491 DEFUN ("x-change-window-property", Fx_change_window_property, 3966 DEFUN ("x-change-window-property", Fx_change_window_property,
9492 Sx_change_window_property, 2, 6, 0, 3967 Sx_change_window_property, 2, 6, 0,
10923 staticpro (&Qnone); 5398 staticpro (&Qnone);
10924 Qsuppress_icon = intern ("suppress-icon"); 5399 Qsuppress_icon = intern ("suppress-icon");
10925 staticpro (&Qsuppress_icon); 5400 staticpro (&Qsuppress_icon);
10926 Qundefined_color = intern ("undefined-color"); 5401 Qundefined_color = intern ("undefined-color");
10927 staticpro (&Qundefined_color); 5402 staticpro (&Qundefined_color);
10928 Qcenter = intern ("center");
10929 staticpro (&Qcenter);
10930 Qcompound_text = intern ("compound-text"); 5403 Qcompound_text = intern ("compound-text");
10931 staticpro (&Qcompound_text); 5404 staticpro (&Qcompound_text);
10932 Qcancel_timer = intern ("cancel-timer"); 5405 Qcancel_timer = intern ("cancel-timer");
10933 staticpro (&Qcancel_timer); 5406 staticpro (&Qcancel_timer);
10934 /* This is the end of symbol initialization. */ 5407 /* This is the end of symbol initialization. */
10936 /* Text property `display' should be nonsticky by default. */ 5409 /* Text property `display' should be nonsticky by default. */
10937 Vtext_property_default_nonsticky 5410 Vtext_property_default_nonsticky
10938 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky); 5411 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
10939 5412
10940 5413
10941 Qlaplace = intern ("laplace");
10942 staticpro (&Qlaplace);
10943 Qemboss = intern ("emboss");
10944 staticpro (&Qemboss);
10945 Qedge_detection = intern ("edge-detection");
10946 staticpro (&Qedge_detection);
10947 Qheuristic = intern ("heuristic");
10948 staticpro (&Qheuristic);
10949 QCmatrix = intern (":matrix");
10950 staticpro (&QCmatrix);
10951 QCcolor_adjustment = intern (":color-adjustment");
10952 staticpro (&QCcolor_adjustment);
10953 QCmask = intern (":mask");
10954 staticpro (&QCmask);
10955
10956 Fput (Qundefined_color, Qerror_conditions, 5414 Fput (Qundefined_color, Qerror_conditions,
10957 Fcons (Qundefined_color, Fcons (Qerror, Qnil))); 5415 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
10958 Fput (Qundefined_color, Qerror_message, 5416 Fput (Qundefined_color, Qerror_message,
10959 build_string ("Undefined color")); 5417 build_string ("Undefined color"));
10960
10961 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
10962 doc: /* Non-nil means always draw a cross over disabled images.
10963 Disabled images are those having an `:conversion disabled' property.
10964 A cross is always drawn on black & white displays. */);
10965 cross_disabled_images = 0;
10966
10967 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
10968 doc: /* List of directories to search for window system bitmap files. */);
10969 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
10970 5418
10971 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape, 5419 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
10972 doc: /* The shape of the pointer when over text. 5420 doc: /* The shape of the pointer when over text.
10973 Changing the value does not affect existing frames 5421 Changing the value does not affect existing frames
10974 unless you set the mouse color. */); 5422 unless you set the mouse color. */);
11043 Since Emacs gets width of a font matching with this regexp from 5491 Since Emacs gets width of a font matching with this regexp from
11044 PIXEL_SIZE field of the name, font finding mechanism gets faster for 5492 PIXEL_SIZE field of the name, font finding mechanism gets faster for
11045 such a font. This is especially effective for such large fonts as 5493 such a font. This is especially effective for such large fonts as
11046 Chinese, Japanese, and Korean. */); 5494 Chinese, Japanese, and Korean. */);
11047 Vx_pixel_size_width_font_regexp = Qnil; 5495 Vx_pixel_size_width_font_regexp = Qnil;
11048
11049 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
11050 doc: /* Time after which cached images are removed from the cache.
11051 When an image has not been displayed this many seconds, remove it
11052 from the image cache. Value must be an integer or nil with nil
11053 meaning don't clear the cache. */);
11054 Vimage_cache_eviction_delay = make_number (30 * 60);
11055 5496
11056 #ifdef USE_X_TOOLKIT 5497 #ifdef USE_X_TOOLKIT
11057 Fprovide (intern ("x-toolkit"), Qnil); 5498 Fprovide (intern ("x-toolkit"), Qnil);
11058 #ifdef USE_MOTIF 5499 #ifdef USE_MOTIF
11059 Fprovide (intern ("motif"), Qnil); 5500 Fprovide (intern ("motif"), Qnil);
11119 find_ccl_program_func = x_find_ccl_program; 5560 find_ccl_program_func = x_find_ccl_program;
11120 query_font_func = x_query_font; 5561 query_font_func = x_query_font;
11121 set_frame_fontset_func = x_set_font; 5562 set_frame_fontset_func = x_set_font;
11122 check_window_system_func = check_x; 5563 check_window_system_func = check_x;
11123 5564
11124 /* Images. */
11125 Qxbm = intern ("xbm");
11126 staticpro (&Qxbm);
11127 QCconversion = intern (":conversion");
11128 staticpro (&QCconversion);
11129 QCheuristic_mask = intern (":heuristic-mask");
11130 staticpro (&QCheuristic_mask);
11131 QCcolor_symbols = intern (":color-symbols");
11132 staticpro (&QCcolor_symbols);
11133 QCascent = intern (":ascent");
11134 staticpro (&QCascent);
11135 QCmargin = intern (":margin");
11136 staticpro (&QCmargin);
11137 QCrelief = intern (":relief");
11138 staticpro (&QCrelief);
11139 Qpostscript = intern ("postscript");
11140 staticpro (&Qpostscript);
11141 QCloader = intern (":loader");
11142 staticpro (&QCloader);
11143 QCbounding_box = intern (":bounding-box");
11144 staticpro (&QCbounding_box);
11145 QCpt_width = intern (":pt-width");
11146 staticpro (&QCpt_width);
11147 QCpt_height = intern (":pt-height");
11148 staticpro (&QCpt_height);
11149 QCindex = intern (":index");
11150 staticpro (&QCindex);
11151 Qpbm = intern ("pbm");
11152 staticpro (&Qpbm);
11153
11154 #if HAVE_XPM
11155 Qxpm = intern ("xpm");
11156 staticpro (&Qxpm);
11157 #endif
11158
11159 #if HAVE_JPEG
11160 Qjpeg = intern ("jpeg");
11161 staticpro (&Qjpeg);
11162 #endif
11163
11164 #if HAVE_TIFF
11165 Qtiff = intern ("tiff");
11166 staticpro (&Qtiff);
11167 #endif
11168
11169 #if HAVE_GIF
11170 Qgif = intern ("gif");
11171 staticpro (&Qgif);
11172 #endif
11173
11174 #if HAVE_PNG
11175 Qpng = intern ("png");
11176 staticpro (&Qpng);
11177 #endif
11178
11179 defsubr (&Sclear_image_cache);
11180 defsubr (&Simage_size);
11181 defsubr (&Simage_mask_p);
11182
11183 hourglass_atimer = NULL; 5565 hourglass_atimer = NULL;
11184 hourglass_shown_p = 0; 5566 hourglass_shown_p = 0;
11185 5567
11186 defsubr (&Sx_show_tip); 5568 defsubr (&Sx_show_tip);
11187 defsubr (&Sx_hide_tip); 5569 defsubr (&Sx_hide_tip);
11196 #ifdef USE_MOTIF 5578 #ifdef USE_MOTIF
11197 defsubr (&Sx_file_dialog); 5579 defsubr (&Sx_file_dialog);
11198 #endif 5580 #endif
11199 } 5581 }
11200 5582
11201
11202 void
11203 init_xfns ()
11204 {
11205 image_types = NULL;
11206 Vimage_types = Qnil;
11207
11208 define_image_type (&xbm_type);
11209 define_image_type (&gs_type);
11210 define_image_type (&pbm_type);
11211
11212 #if HAVE_XPM
11213 define_image_type (&xpm_type);
11214 #endif
11215
11216 #if HAVE_JPEG
11217 define_image_type (&jpeg_type);
11218 #endif
11219
11220 #if HAVE_TIFF
11221 define_image_type (&tiff_type);
11222 #endif
11223
11224 #if HAVE_GIF
11225 define_image_type (&gif_type);
11226 #endif
11227
11228 #if HAVE_PNG
11229 define_image_type (&png_type);
11230 #endif
11231 }
11232
11233 #endif /* HAVE_X_WINDOWS */ 5583 #endif /* HAVE_X_WINDOWS */
11234 5584
11235 /* arch-tag: 55040d02-5485-4d58-8b22-95a7a05f3288 5585 /* arch-tag: 55040d02-5485-4d58-8b22-95a7a05f3288
11236 (do not change this comment) */ 5586 (do not change this comment) */