comparison src/xfns.c @ 54335:cf0468e7d5f9

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