Commit | Line | Data |
---|---|---|
4ef0d4d0 | 1 | /* Functions for image support on window system. |
0b5538bd | 2 | Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
5df4f04c | 3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
6678dc11 | 4 | Free Software Foundation, Inc. |
4ef0d4d0 KS |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
4ef0d4d0 | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
4ef0d4d0 KS |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ef0d4d0 KS |
20 | |
21 | #include <config.h> | |
4ef0d4d0 KS |
22 | #include <stdio.h> |
23 | #include <math.h> | |
be8c4ce4 | 24 | #include <ctype.h> |
4ef0d4d0 KS |
25 | |
26 | #ifdef HAVE_UNISTD_H | |
27 | #include <unistd.h> | |
28 | #endif | |
29 | ||
d7306fe6 DN |
30 | #ifdef HAVE_PNG |
31 | #if defined HAVE_LIBPNG_PNG_H | |
32 | # include <libpng/png.h> | |
33 | #else | |
34 | # include <png.h> | |
35 | #endif | |
285d07e2 | 36 | #endif |
d7306fe6 | 37 | |
c3417a74 DN |
38 | #include <setjmp.h> |
39 | ||
4ef0d4d0 KS |
40 | /* This makes the fields of a Display accessible, in Xlib header files. */ |
41 | ||
42 | #define XLIB_ILLEGAL_ACCESS | |
43 | ||
44 | #include "lisp.h" | |
45 | #include "frame.h" | |
46 | #include "window.h" | |
47 | #include "dispextern.h" | |
48 | #include "blockinput.h" | |
49 | #include "systime.h" | |
50 | #include <epaths.h> | |
3dcc8b84 | 51 | #include "character.h" |
022af124 | 52 | #include "coding.h" |
6087292e | 53 | #include "termhooks.h" |
3dcc8b84 | 54 | #include "font.h" |
4ef0d4d0 KS |
55 | |
56 | #ifdef HAVE_X_WINDOWS | |
57 | #include "xterm.h" | |
58 | #include <sys/types.h> | |
59 | #include <sys/stat.h> | |
60 | ||
61 | #define COLOR_TABLE_SUPPORT 1 | |
62 | ||
63 | typedef struct x_bitmap_record Bitmap_Record; | |
4ef0d4d0 KS |
64 | #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y) |
65 | #define NO_PIXMAP None | |
4ef0d4d0 KS |
66 | |
67 | #define RGB_PIXEL_COLOR unsigned long | |
68 | ||
9ecf6403 YM |
69 | #define PIX_MASK_RETAIN 0 |
70 | #define PIX_MASK_DRAW 1 | |
4ef0d4d0 KS |
71 | #endif /* HAVE_X_WINDOWS */ |
72 | ||
73 | ||
74 | #ifdef HAVE_NTGUI | |
75 | #include "w32term.h" | |
76 | ||
77 | /* W32_TODO : Color tables on W32. */ | |
78 | #undef COLOR_TABLE_SUPPORT | |
79 | ||
80 | typedef struct w32_bitmap_record Bitmap_Record; | |
4ef0d4d0 KS |
81 | #define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y) |
82 | #define NO_PIXMAP 0 | |
4ef0d4d0 KS |
83 | |
84 | #define RGB_PIXEL_COLOR COLORREF | |
85 | ||
9ecf6403 YM |
86 | #define PIX_MASK_RETAIN 0 |
87 | #define PIX_MASK_DRAW 1 | |
4ef0d4d0 KS |
88 | |
89 | #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual | |
90 | #define x_defined_color w32_defined_color | |
91 | #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits) | |
8b7d0a16 JR |
92 | |
93 | /* Functions from w32term.c that depend on XColor (so can't go in w32term.h | |
94 | without modifying lots of files). */ | |
95 | extern void x_query_colors (struct frame *f, XColor *colors, int ncolors); | |
96 | extern void x_query_color (struct frame *f, XColor *color); | |
5be1c984 EZ |
97 | |
98 | /* Version of libpng that we were compiled with, or -1 if no PNG | |
99 | support was compiled in. This is tested by w32-win.el to correctly | |
100 | set up the alist used to search for PNG libraries. */ | |
101 | Lisp_Object Qlibpng_version; | |
4ef0d4d0 KS |
102 | #endif /* HAVE_NTGUI */ |
103 | ||
edfda783 AR |
104 | #ifdef HAVE_NS |
105 | #include "nsterm.h" | |
106 | #include <sys/types.h> | |
107 | #include <sys/stat.h> | |
108 | ||
109 | #undef COLOR_TABLE_SUPPORT | |
110 | ||
111 | typedef struct ns_bitmap_record Bitmap_Record; | |
112 | ||
113 | #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y) | |
114 | #define NO_PIXMAP 0 | |
115 | ||
116 | #define RGB_PIXEL_COLOR unsigned long | |
117 | #define ZPixmap 0 | |
118 | ||
119 | #define PIX_MASK_RETAIN 0 | |
120 | #define PIX_MASK_DRAW 1 | |
121 | ||
122 | #define FRAME_X_VISUAL FRAME_NS_DISPLAY_INFO(f)->visual | |
123 | #define x_defined_color(f, name, color_def, alloc) \ | |
124 | ns_defined_color (f, name, color_def, alloc, 0) | |
125 | #define FRAME_X_SCREEN(f) 0 | |
9e50ff0c | 126 | #define DefaultDepthOfScreen(screen) x_display_list->n_planes |
edfda783 AR |
127 | #endif /* HAVE_NS */ |
128 | ||
129 | ||
4ef0d4d0 KS |
130 | /* Search path for bitmap files. */ |
131 | ||
132 | Lisp_Object Vx_bitmap_file_path; | |
133 | ||
134 | ||
135 | static void x_disable_image P_ ((struct frame *, struct image *)); | |
136 | static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object, | |
137 | Lisp_Object)); | |
138 | ||
139 | static void init_color_table P_ ((void)); | |
140 | static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b)); | |
141 | #ifdef COLOR_TABLE_SUPPORT | |
142 | static void free_color_table P_ ((void)); | |
143 | static unsigned long *colors_in_color_table P_ ((int *n)); | |
144 | static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p)); | |
145 | #endif | |
146 | ||
147 | /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap | |
148 | id, which is just an int that this section returns. Bitmaps are | |
149 | reference counted so they can be shared among frames. | |
150 | ||
151 | Bitmap indices are guaranteed to be > 0, so a negative number can | |
152 | be used to indicate no bitmap. | |
153 | ||
154 | If you use x_create_bitmap_from_data, then you must keep track of | |
155 | the bitmaps yourself. That is, creating a bitmap from the same | |
156 | data more than once will not be caught. */ | |
157 | ||
edfda783 AR |
158 | #ifdef HAVE_NS |
159 | XImagePtr | |
160 | XGetImage (Display *display, Pixmap pixmap, int x, int y, | |
161 | unsigned int width, unsigned int height, | |
162 | unsigned long plane_mask, int format) | |
163 | { | |
6fb5f7da | 164 | /* TODO: not sure what this function is supposed to do.. */ |
edfda783 AR |
165 | ns_retain_object(pixmap); |
166 | return pixmap; | |
167 | } | |
168 | ||
169 | /* use with imgs created by ns_image_for_XPM */ | |
170 | unsigned long | |
171 | XGetPixel (XImagePtr ximage, int x, int y) | |
172 | { | |
173 | return ns_get_pixel(ximage, x, y); | |
174 | } | |
175 | ||
176 | /* use with imgs created by ns_image_for_XPM; alpha set to 1; | |
177 | pixel is assumed to be in form RGB */ | |
178 | void | |
179 | XPutPixel (XImagePtr ximage, int x, int y, unsigned long pixel) | |
180 | { | |
181 | ns_put_pixel(ximage, x, y, pixel); | |
182 | } | |
183 | #endif /* HAVE_NS */ | |
184 | ||
4ef0d4d0 KS |
185 | |
186 | /* Functions to access the contents of a bitmap, given an id. */ | |
187 | ||
188 | int | |
189 | x_bitmap_height (f, id) | |
190 | FRAME_PTR f; | |
191 | int id; | |
192 | { | |
193 | return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height; | |
194 | } | |
195 | ||
196 | int | |
197 | x_bitmap_width (f, id) | |
198 | FRAME_PTR f; | |
199 | int id; | |
200 | { | |
201 | return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width; | |
202 | } | |
203 | ||
204 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) | |
205 | int | |
206 | x_bitmap_pixmap (f, id) | |
207 | FRAME_PTR f; | |
208 | int id; | |
209 | { | |
210 | return (int) FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap; | |
211 | } | |
212 | #endif | |
213 | ||
214 | #ifdef HAVE_X_WINDOWS | |
215 | int | |
216 | x_bitmap_mask (f, id) | |
217 | FRAME_PTR f; | |
218 | int id; | |
219 | { | |
220 | return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].mask; | |
221 | } | |
222 | #endif | |
223 | ||
224 | /* Allocate a new bitmap record. Returns index of new record. */ | |
225 | ||
226 | static int | |
227 | x_allocate_bitmap_record (f) | |
228 | FRAME_PTR f; | |
229 | { | |
230 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
231 | int i; | |
232 | ||
233 | if (dpyinfo->bitmaps == NULL) | |
234 | { | |
235 | dpyinfo->bitmaps_size = 10; | |
236 | dpyinfo->bitmaps | |
237 | = (Bitmap_Record *) xmalloc (dpyinfo->bitmaps_size * sizeof (Bitmap_Record)); | |
238 | dpyinfo->bitmaps_last = 1; | |
239 | return 1; | |
240 | } | |
241 | ||
242 | if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size) | |
243 | return ++dpyinfo->bitmaps_last; | |
244 | ||
245 | for (i = 0; i < dpyinfo->bitmaps_size; ++i) | |
246 | if (dpyinfo->bitmaps[i].refcount == 0) | |
247 | return i + 1; | |
248 | ||
249 | dpyinfo->bitmaps_size *= 2; | |
250 | dpyinfo->bitmaps | |
251 | = (Bitmap_Record *) xrealloc (dpyinfo->bitmaps, | |
252 | dpyinfo->bitmaps_size * sizeof (Bitmap_Record)); | |
253 | return ++dpyinfo->bitmaps_last; | |
254 | } | |
255 | ||
256 | /* Add one reference to the reference count of the bitmap with id ID. */ | |
257 | ||
258 | void | |
259 | x_reference_bitmap (f, id) | |
260 | FRAME_PTR f; | |
261 | int id; | |
262 | { | |
263 | ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount; | |
264 | } | |
265 | ||
266 | /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */ | |
267 | ||
268 | int | |
269 | x_create_bitmap_from_data (f, bits, width, height) | |
270 | struct frame *f; | |
271 | char *bits; | |
272 | unsigned int width, height; | |
273 | { | |
274 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
275 | int id; | |
276 | ||
277 | #ifdef HAVE_X_WINDOWS | |
278 | Pixmap bitmap; | |
279 | bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
280 | bits, width, height); | |
281 | if (! bitmap) | |
282 | return -1; | |
283 | #endif /* HAVE_X_WINDOWS */ | |
284 | ||
285 | #ifdef HAVE_NTGUI | |
286 | Pixmap bitmap; | |
287 | bitmap = CreateBitmap (width, height, | |
288 | FRAME_X_DISPLAY_INFO (XFRAME (frame))->n_planes, | |
289 | FRAME_X_DISPLAY_INFO (XFRAME (frame))->n_cbits, | |
290 | bits); | |
291 | if (! bitmap) | |
292 | return -1; | |
293 | #endif /* HAVE_NTGUI */ | |
294 | ||
edfda783 AR |
295 | #ifdef HAVE_NS |
296 | void *bitmap = ns_image_from_XBM(bits, width, height); | |
297 | if (!bitmap) | |
298 | return -1; | |
299 | #endif | |
300 | ||
4ef0d4d0 | 301 | id = x_allocate_bitmap_record (f); |
4ef0d4d0 | 302 | |
edfda783 AR |
303 | #ifdef HAVE_NS |
304 | dpyinfo->bitmaps[id - 1].img = bitmap; | |
305 | dpyinfo->bitmaps[id - 1].depth = 1; | |
306 | #endif | |
307 | ||
4ef0d4d0 KS |
308 | dpyinfo->bitmaps[id - 1].file = NULL; |
309 | dpyinfo->bitmaps[id - 1].height = height; | |
310 | dpyinfo->bitmaps[id - 1].width = width; | |
311 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
312 | ||
313 | #ifdef HAVE_X_WINDOWS | |
314 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
315 | dpyinfo->bitmaps[id - 1].have_mask = 0; | |
316 | dpyinfo->bitmaps[id - 1].depth = 1; | |
317 | #endif /* HAVE_X_WINDOWS */ | |
318 | ||
319 | #ifdef HAVE_NTGUI | |
320 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
321 | dpyinfo->bitmaps[id - 1].hinst = NULL; | |
322 | dpyinfo->bitmaps[id - 1].depth = 1; | |
323 | #endif /* HAVE_NTGUI */ | |
324 | ||
325 | return id; | |
326 | } | |
327 | ||
328 | /* Create bitmap from file FILE for frame F. */ | |
329 | ||
330 | int | |
331 | x_create_bitmap_from_file (f, file) | |
332 | struct frame *f; | |
333 | Lisp_Object file; | |
334 | { | |
edfda783 AR |
335 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); |
336 | ||
4ef0d4d0 KS |
337 | #ifdef HAVE_NTGUI |
338 | return -1; /* W32_TODO : bitmap support */ | |
339 | #endif /* HAVE_NTGUI */ | |
340 | ||
edfda783 AR |
341 | #ifdef HAVE_NS |
342 | int id; | |
343 | void *bitmap = ns_image_from_file(file); | |
344 | ||
345 | if (!bitmap) | |
346 | return -1; | |
347 | ||
348 | ||
349 | id = x_allocate_bitmap_record (f); | |
350 | dpyinfo->bitmaps[id - 1].img = bitmap; | |
351 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
352 | dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (SBYTES (file) + 1); | |
353 | dpyinfo->bitmaps[id - 1].depth = 1; | |
354 | dpyinfo->bitmaps[id - 1].height = ns_image_width(bitmap); | |
355 | dpyinfo->bitmaps[id - 1].width = ns_image_height(bitmap); | |
356 | strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file)); | |
357 | return id; | |
358 | #endif | |
359 | ||
4ef0d4d0 | 360 | #ifdef HAVE_X_WINDOWS |
4ef0d4d0 KS |
361 | unsigned int width, height; |
362 | Pixmap bitmap; | |
363 | int xhot, yhot, result, id; | |
364 | Lisp_Object found; | |
365 | int fd; | |
366 | char *filename; | |
367 | ||
368 | /* Look for an existing bitmap with the same name. */ | |
369 | for (id = 0; id < dpyinfo->bitmaps_last; ++id) | |
370 | { | |
371 | if (dpyinfo->bitmaps[id].refcount | |
372 | && dpyinfo->bitmaps[id].file | |
373 | && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file))) | |
374 | { | |
375 | ++dpyinfo->bitmaps[id].refcount; | |
376 | return id + 1; | |
377 | } | |
378 | } | |
379 | ||
380 | /* Search bitmap-file-path for the file, if appropriate. */ | |
381 | fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil); | |
382 | if (fd < 0) | |
383 | return -1; | |
384 | emacs_close (fd); | |
385 | ||
386 | filename = (char *) SDATA (found); | |
387 | ||
388 | result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
389 | filename, &width, &height, &bitmap, &xhot, &yhot); | |
390 | if (result != BitmapSuccess) | |
391 | return -1; | |
392 | ||
393 | id = x_allocate_bitmap_record (f); | |
394 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
395 | dpyinfo->bitmaps[id - 1].have_mask = 0; | |
396 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
397 | dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (SBYTES (file) + 1); | |
398 | dpyinfo->bitmaps[id - 1].depth = 1; | |
399 | dpyinfo->bitmaps[id - 1].height = height; | |
400 | dpyinfo->bitmaps[id - 1].width = width; | |
401 | strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file)); | |
402 | ||
403 | return id; | |
404 | #endif /* HAVE_X_WINDOWS */ | |
405 | } | |
406 | ||
407 | /* Free bitmap B. */ | |
408 | ||
409 | static void | |
65342ae3 | 410 | free_bitmap_record (dpyinfo, bm) |
4ef0d4d0 KS |
411 | Display_Info *dpyinfo; |
412 | Bitmap_Record *bm; | |
413 | { | |
414 | #ifdef HAVE_X_WINDOWS | |
415 | XFreePixmap (dpyinfo->display, bm->pixmap); | |
416 | if (bm->have_mask) | |
417 | XFreePixmap (dpyinfo->display, bm->mask); | |
418 | #endif /* HAVE_X_WINDOWS */ | |
419 | ||
420 | #ifdef HAVE_NTGUI | |
421 | DeleteObject (bm->pixmap); | |
422 | #endif /* HAVE_NTGUI */ | |
423 | ||
edfda783 AR |
424 | #ifdef HAVE_NS |
425 | ns_release_object(bm->img); | |
426 | #endif | |
427 | ||
4ef0d4d0 KS |
428 | if (bm->file) |
429 | { | |
430 | xfree (bm->file); | |
431 | bm->file = NULL; | |
432 | } | |
433 | } | |
434 | ||
435 | /* Remove reference to bitmap with id number ID. */ | |
436 | ||
437 | void | |
438 | x_destroy_bitmap (f, id) | |
439 | FRAME_PTR f; | |
440 | int id; | |
441 | { | |
442 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
443 | ||
444 | if (id > 0) | |
445 | { | |
446 | Bitmap_Record *bm = &dpyinfo->bitmaps[id - 1]; | |
447 | ||
448 | if (--bm->refcount == 0) | |
449 | { | |
450 | BLOCK_INPUT; | |
65342ae3 | 451 | free_bitmap_record (dpyinfo, bm); |
4ef0d4d0 KS |
452 | UNBLOCK_INPUT; |
453 | } | |
454 | } | |
455 | } | |
456 | ||
457 | /* Free all the bitmaps for the display specified by DPYINFO. */ | |
458 | ||
459 | void | |
460 | x_destroy_all_bitmaps (dpyinfo) | |
461 | Display_Info *dpyinfo; | |
462 | { | |
463 | int i; | |
464 | Bitmap_Record *bm = dpyinfo->bitmaps; | |
465 | ||
466 | for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++) | |
467 | if (bm->refcount > 0) | |
65342ae3 | 468 | free_bitmap_record (dpyinfo, bm); |
4ef0d4d0 KS |
469 | |
470 | dpyinfo->bitmaps_last = 0; | |
471 | } | |
472 | ||
473 | ||
474 | #ifdef HAVE_X_WINDOWS | |
475 | ||
476 | /* Useful functions defined in the section | |
477 | `Image type independent image structures' below. */ | |
478 | ||
6ac3c7bb KS |
479 | static unsigned long four_corners_best P_ ((XImagePtr ximg, |
480 | int *corners, | |
481 | unsigned long width, | |
4ef0d4d0 KS |
482 | unsigned long height)); |
483 | ||
484 | static int x_create_x_image_and_pixmap P_ ((struct frame *f, int width, int height, | |
485 | int depth, XImagePtr *ximg, | |
486 | Pixmap *pixmap)); | |
487 | ||
488 | static void x_destroy_x_image P_ ((XImagePtr ximg)); | |
489 | ||
490 | ||
491 | /* Create a mask of a bitmap. Note is this not a perfect mask. | |
492 | It's nicer with some borders in this context */ | |
493 | ||
494 | int | |
495 | x_create_bitmap_mask (f, id) | |
496 | struct frame *f; | |
497 | int id; | |
498 | { | |
499 | Pixmap pixmap, mask; | |
500 | XImagePtr ximg, mask_img; | |
501 | unsigned long width, height; | |
502 | int result; | |
503 | unsigned long bg; | |
504 | unsigned long x, y, xp, xm, yp, ym; | |
505 | GC gc; | |
506 | ||
507 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
508 | ||
509 | if (!(id > 0)) | |
510 | return -1; | |
511 | ||
512 | pixmap = x_bitmap_pixmap (f, id); | |
513 | width = x_bitmap_width (f, id); | |
514 | height = x_bitmap_height (f, id); | |
515 | ||
516 | BLOCK_INPUT; | |
517 | ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height, | |
518 | ~0, ZPixmap); | |
519 | ||
520 | if (!ximg) | |
521 | { | |
522 | UNBLOCK_INPUT; | |
523 | return -1; | |
524 | } | |
525 | ||
526 | result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask); | |
527 | ||
528 | UNBLOCK_INPUT; | |
529 | if (!result) | |
530 | { | |
531 | XDestroyImage (ximg); | |
532 | return -1; | |
533 | } | |
534 | ||
6ac3c7bb | 535 | bg = four_corners_best (ximg, NULL, width, height); |
4ef0d4d0 KS |
536 | |
537 | for (y = 0; y < ximg->height; ++y) | |
538 | { | |
539 | for (x = 0; x < ximg->width; ++x) | |
540 | { | |
541 | xp = x != ximg->width - 1 ? x + 1 : 0; | |
542 | xm = x != 0 ? x - 1 : ximg->width - 1; | |
543 | yp = y != ximg->height - 1 ? y + 1 : 0; | |
544 | ym = y != 0 ? y - 1 : ximg->height - 1; | |
545 | if (XGetPixel (ximg, x, y) == bg | |
546 | && XGetPixel (ximg, x, yp) == bg | |
547 | && XGetPixel (ximg, x, ym) == bg | |
548 | && XGetPixel (ximg, xp, y) == bg | |
549 | && XGetPixel (ximg, xp, yp) == bg | |
550 | && XGetPixel (ximg, xp, ym) == bg | |
551 | && XGetPixel (ximg, xm, y) == bg | |
552 | && XGetPixel (ximg, xm, yp) == bg | |
553 | && XGetPixel (ximg, xm, ym) == bg) | |
554 | XPutPixel (mask_img, x, y, 0); | |
555 | else | |
556 | XPutPixel (mask_img, x, y, 1); | |
557 | } | |
558 | } | |
559 | ||
560 | xassert (interrupt_input_blocked); | |
561 | gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL); | |
562 | XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0, | |
563 | width, height); | |
564 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
565 | ||
566 | dpyinfo->bitmaps[id - 1].have_mask = 1; | |
567 | dpyinfo->bitmaps[id - 1].mask = mask; | |
568 | ||
569 | XDestroyImage (ximg); | |
570 | x_destroy_x_image (mask_img); | |
571 | ||
572 | return 0; | |
573 | } | |
574 | ||
575 | #endif /* HAVE_X_WINDOWS */ | |
576 | ||
577 | ||
578 | /*********************************************************************** | |
579 | Image types | |
580 | ***********************************************************************/ | |
581 | ||
582 | /* Value is the number of elements of vector VECTOR. */ | |
583 | ||
584 | #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR)) | |
585 | ||
586 | /* List of supported image types. Use define_image_type to add new | |
587 | types. Use lookup_image_type to find a type for a given symbol. */ | |
588 | ||
589 | static struct image_type *image_types; | |
590 | ||
0855eb52 JB |
591 | /* A list of symbols, one for each supported image type. */ |
592 | ||
593 | Lisp_Object Vimage_types; | |
594 | ||
2a611c1a JB |
595 | /* An alist of image types and libraries that implement the type. */ |
596 | ||
597 | Lisp_Object Vimage_library_alist; | |
598 | ||
0855eb52 JB |
599 | /* Cache for delayed-loading image types. */ |
600 | ||
601 | static Lisp_Object Vimage_type_cache; | |
602 | ||
4ef0d4d0 KS |
603 | /* The symbol `xbm' which is used as the type symbol for XBM images. */ |
604 | ||
605 | Lisp_Object Qxbm; | |
606 | ||
607 | /* Keywords. */ | |
608 | ||
609 | extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile; | |
e047f448 | 610 | extern Lisp_Object QCdata, QCtype; |
e1dfec15 | 611 | extern Lisp_Object Qcenter; |
e047f448 | 612 | Lisp_Object QCascent, QCmargin, QCrelief, Qcount; |
4ef0d4d0 KS |
613 | Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask; |
614 | Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask; | |
615 | ||
616 | /* Other symbols. */ | |
617 | ||
618 | Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic; | |
4ef0d4d0 KS |
619 | |
620 | /* Time in seconds after which images should be removed from the cache | |
621 | if not displayed. */ | |
622 | ||
623 | Lisp_Object Vimage_cache_eviction_delay; | |
624 | ||
625 | /* Function prototypes. */ | |
626 | ||
0855eb52 | 627 | static Lisp_Object define_image_type P_ ((struct image_type *type, int loaded)); |
4ef0d4d0 KS |
628 | static struct image_type *lookup_image_type P_ ((Lisp_Object symbol)); |
629 | static void image_error P_ ((char *format, Lisp_Object, Lisp_Object)); | |
630 | static void x_laplace P_ ((struct frame *, struct image *)); | |
631 | static void x_emboss P_ ((struct frame *, struct image *)); | |
632 | static int x_build_heuristic_mask P_ ((struct frame *, struct image *, | |
633 | Lisp_Object)); | |
634 | ||
0855eb52 JB |
635 | #define CACHE_IMAGE_TYPE(type, status) \ |
636 | do { Vimage_type_cache = Fcons (Fcons (type, status), Vimage_type_cache); } while (0) | |
637 | ||
638 | #define ADD_IMAGE_TYPE(type) \ | |
639 | do { Vimage_types = Fcons (type, Vimage_types); } while (0) | |
4ef0d4d0 KS |
640 | |
641 | /* Define a new image type from TYPE. This adds a copy of TYPE to | |
0855eb52 | 642 | image_types and caches the loading status of TYPE. */ |
4ef0d4d0 | 643 | |
0855eb52 JB |
644 | static Lisp_Object |
645 | define_image_type (type, loaded) | |
4ef0d4d0 | 646 | struct image_type *type; |
0855eb52 | 647 | int loaded; |
4ef0d4d0 | 648 | { |
0855eb52 JB |
649 | Lisp_Object success; |
650 | ||
651 | if (!loaded) | |
652 | success = Qnil; | |
653 | else | |
654 | { | |
655 | /* Make a copy of TYPE to avoid a bus error in a dumped Emacs. | |
656 | The initialized data segment is read-only. */ | |
657 | struct image_type *p = (struct image_type *) xmalloc (sizeof *p); | |
658 | bcopy (type, p, sizeof *p); | |
659 | p->next = image_types; | |
660 | image_types = p; | |
661 | success = Qt; | |
662 | } | |
663 | ||
adac86db | 664 | CACHE_IMAGE_TYPE (*type->type, success); |
0855eb52 | 665 | return success; |
4ef0d4d0 KS |
666 | } |
667 | ||
668 | ||
669 | /* Look up image type SYMBOL, and return a pointer to its image_type | |
670 | structure. Value is null if SYMBOL is not a known image type. */ | |
671 | ||
672 | static INLINE struct image_type * | |
673 | lookup_image_type (symbol) | |
674 | Lisp_Object symbol; | |
675 | { | |
676 | struct image_type *type; | |
677 | ||
803aac3e | 678 | /* We must initialize the image-type if it hasn't been already. */ |
2a611c1a | 679 | if (NILP (Finit_image_library (symbol, Vimage_library_alist))) |
803aac3e MB |
680 | return 0; /* unimplemented */ |
681 | ||
4ef0d4d0 KS |
682 | for (type = image_types; type; type = type->next) |
683 | if (EQ (symbol, *type->type)) | |
684 | break; | |
685 | ||
686 | return type; | |
687 | } | |
688 | ||
689 | ||
690 | /* Value is non-zero if OBJECT is a valid Lisp image specification. A | |
691 | valid image specification is a list whose car is the symbol | |
692 | `image', and whose rest is a property list. The property list must | |
693 | contain a value for key `:type'. That value must be the name of a | |
694 | supported image type. The rest of the property list depends on the | |
695 | image type. */ | |
696 | ||
697 | int | |
698 | valid_image_p (object) | |
699 | Lisp_Object object; | |
700 | { | |
701 | int valid_p = 0; | |
702 | ||
703 | if (IMAGEP (object)) | |
704 | { | |
705 | Lisp_Object tem; | |
706 | ||
707 | for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem)) | |
708 | if (EQ (XCAR (tem), QCtype)) | |
709 | { | |
710 | tem = XCDR (tem); | |
711 | if (CONSP (tem) && SYMBOLP (XCAR (tem))) | |
712 | { | |
713 | struct image_type *type; | |
714 | type = lookup_image_type (XCAR (tem)); | |
715 | if (type) | |
716 | valid_p = type->valid_p (object); | |
717 | } | |
718 | ||
719 | break; | |
720 | } | |
721 | } | |
722 | ||
723 | return valid_p; | |
724 | } | |
725 | ||
726 | ||
727 | /* Log error message with format string FORMAT and argument ARG. | |
728 | Signaling an error, e.g. when an image cannot be loaded, is not a | |
729 | good idea because this would interrupt redisplay, and the error | |
730 | message display would lead to another redisplay. This function | |
731 | therefore simply displays a message. */ | |
732 | ||
733 | static void | |
734 | image_error (format, arg1, arg2) | |
735 | char *format; | |
736 | Lisp_Object arg1, arg2; | |
737 | { | |
738 | add_to_log (format, arg1, arg2); | |
739 | } | |
740 | ||
741 | ||
742 | \f | |
743 | /*********************************************************************** | |
744 | Image specifications | |
745 | ***********************************************************************/ | |
746 | ||
747 | enum image_value_type | |
748 | { | |
749 | IMAGE_DONT_CHECK_VALUE_TYPE, | |
750 | IMAGE_STRING_VALUE, | |
751 | IMAGE_STRING_OR_NIL_VALUE, | |
752 | IMAGE_SYMBOL_VALUE, | |
753 | IMAGE_POSITIVE_INTEGER_VALUE, | |
754 | IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, | |
755 | IMAGE_NON_NEGATIVE_INTEGER_VALUE, | |
756 | IMAGE_ASCENT_VALUE, | |
757 | IMAGE_INTEGER_VALUE, | |
758 | IMAGE_FUNCTION_VALUE, | |
759 | IMAGE_NUMBER_VALUE, | |
760 | IMAGE_BOOL_VALUE | |
761 | }; | |
762 | ||
763 | /* Structure used when parsing image specifications. */ | |
764 | ||
765 | struct image_keyword | |
766 | { | |
767 | /* Name of keyword. */ | |
768 | char *name; | |
769 | ||
770 | /* The type of value allowed. */ | |
771 | enum image_value_type type; | |
772 | ||
773 | /* Non-zero means key must be present. */ | |
774 | int mandatory_p; | |
775 | ||
776 | /* Used to recognize duplicate keywords in a property list. */ | |
777 | int count; | |
778 | ||
779 | /* The value that was found. */ | |
780 | Lisp_Object value; | |
781 | }; | |
782 | ||
783 | ||
784 | static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *, | |
785 | int, Lisp_Object)); | |
786 | static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *)); | |
787 | ||
788 | ||
789 | /* Parse image spec SPEC according to KEYWORDS. A valid image spec | |
790 | has the format (image KEYWORD VALUE ...). One of the keyword/ | |
791 | value pairs must be `:type TYPE'. KEYWORDS is a vector of | |
792 | image_keywords structures of size NKEYWORDS describing other | |
793 | allowed keyword/value pairs. Value is non-zero if SPEC is valid. */ | |
794 | ||
795 | static int | |
796 | parse_image_spec (spec, keywords, nkeywords, type) | |
797 | Lisp_Object spec; | |
798 | struct image_keyword *keywords; | |
799 | int nkeywords; | |
800 | Lisp_Object type; | |
801 | { | |
802 | int i; | |
803 | Lisp_Object plist; | |
804 | ||
805 | if (!IMAGEP (spec)) | |
806 | return 0; | |
807 | ||
808 | plist = XCDR (spec); | |
809 | while (CONSP (plist)) | |
810 | { | |
811 | Lisp_Object key, value; | |
812 | ||
813 | /* First element of a pair must be a symbol. */ | |
814 | key = XCAR (plist); | |
815 | plist = XCDR (plist); | |
816 | if (!SYMBOLP (key)) | |
817 | return 0; | |
818 | ||
819 | /* There must follow a value. */ | |
820 | if (!CONSP (plist)) | |
821 | return 0; | |
822 | value = XCAR (plist); | |
823 | plist = XCDR (plist); | |
824 | ||
825 | /* Find key in KEYWORDS. Error if not found. */ | |
826 | for (i = 0; i < nkeywords; ++i) | |
827 | if (strcmp (keywords[i].name, SDATA (SYMBOL_NAME (key))) == 0) | |
828 | break; | |
829 | ||
830 | if (i == nkeywords) | |
831 | continue; | |
832 | ||
833 | /* Record that we recognized the keyword. If a keywords | |
834 | was found more than once, it's an error. */ | |
835 | keywords[i].value = value; | |
836 | ++keywords[i].count; | |
837 | ||
838 | if (keywords[i].count > 1) | |
839 | return 0; | |
840 | ||
841 | /* Check type of value against allowed type. */ | |
842 | switch (keywords[i].type) | |
843 | { | |
844 | case IMAGE_STRING_VALUE: | |
845 | if (!STRINGP (value)) | |
846 | return 0; | |
847 | break; | |
848 | ||
849 | case IMAGE_STRING_OR_NIL_VALUE: | |
850 | if (!STRINGP (value) && !NILP (value)) | |
851 | return 0; | |
852 | break; | |
853 | ||
854 | case IMAGE_SYMBOL_VALUE: | |
855 | if (!SYMBOLP (value)) | |
856 | return 0; | |
857 | break; | |
858 | ||
859 | case IMAGE_POSITIVE_INTEGER_VALUE: | |
860 | if (!INTEGERP (value) || XINT (value) <= 0) | |
861 | return 0; | |
862 | break; | |
863 | ||
864 | case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR: | |
865 | if (INTEGERP (value) && XINT (value) >= 0) | |
866 | break; | |
867 | if (CONSP (value) | |
868 | && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value)) | |
869 | && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0) | |
870 | break; | |
871 | return 0; | |
872 | ||
873 | case IMAGE_ASCENT_VALUE: | |
874 | if (SYMBOLP (value) && EQ (value, Qcenter)) | |
875 | break; | |
876 | else if (INTEGERP (value) | |
877 | && XINT (value) >= 0 | |
878 | && XINT (value) <= 100) | |
879 | break; | |
880 | return 0; | |
881 | ||
882 | case IMAGE_NON_NEGATIVE_INTEGER_VALUE: | |
883 | if (!INTEGERP (value) || XINT (value) < 0) | |
884 | return 0; | |
885 | break; | |
886 | ||
887 | case IMAGE_DONT_CHECK_VALUE_TYPE: | |
888 | break; | |
889 | ||
890 | case IMAGE_FUNCTION_VALUE: | |
891 | value = indirect_function (value); | |
892 | if (SUBRP (value) | |
893 | || COMPILEDP (value) | |
894 | || (CONSP (value) && EQ (XCAR (value), Qlambda))) | |
895 | break; | |
896 | return 0; | |
897 | ||
898 | case IMAGE_NUMBER_VALUE: | |
899 | if (!INTEGERP (value) && !FLOATP (value)) | |
900 | return 0; | |
901 | break; | |
902 | ||
903 | case IMAGE_INTEGER_VALUE: | |
904 | if (!INTEGERP (value)) | |
905 | return 0; | |
906 | break; | |
907 | ||
908 | case IMAGE_BOOL_VALUE: | |
909 | if (!NILP (value) && !EQ (value, Qt)) | |
910 | return 0; | |
911 | break; | |
912 | ||
913 | default: | |
914 | abort (); | |
915 | break; | |
916 | } | |
917 | ||
918 | if (EQ (key, QCtype) && !EQ (type, value)) | |
919 | return 0; | |
920 | } | |
921 | ||
922 | /* Check that all mandatory fields are present. */ | |
923 | for (i = 0; i < nkeywords; ++i) | |
924 | if (keywords[i].mandatory_p && keywords[i].count == 0) | |
925 | return 0; | |
926 | ||
927 | return NILP (plist); | |
928 | } | |
929 | ||
930 | ||
931 | /* Return the value of KEY in image specification SPEC. Value is nil | |
932 | if KEY is not present in SPEC. if FOUND is not null, set *FOUND | |
933 | to 1 if KEY was found in SPEC, set it to 0 otherwise. */ | |
934 | ||
935 | static Lisp_Object | |
936 | image_spec_value (spec, key, found) | |
937 | Lisp_Object spec, key; | |
938 | int *found; | |
939 | { | |
940 | Lisp_Object tail; | |
941 | ||
942 | xassert (valid_image_p (spec)); | |
943 | ||
944 | for (tail = XCDR (spec); | |
945 | CONSP (tail) && CONSP (XCDR (tail)); | |
946 | tail = XCDR (XCDR (tail))) | |
947 | { | |
948 | if (EQ (XCAR (tail), key)) | |
949 | { | |
950 | if (found) | |
951 | *found = 1; | |
952 | return XCAR (XCDR (tail)); | |
953 | } | |
954 | } | |
955 | ||
956 | if (found) | |
957 | *found = 0; | |
958 | return Qnil; | |
959 | } | |
960 | ||
961 | ||
962 | DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0, | |
963 | doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT). | |
964 | PIXELS non-nil means return the size in pixels, otherwise return the | |
965 | size in canonical character units. | |
966 | FRAME is the frame on which the image will be displayed. FRAME nil | |
967 | or omitted means use the selected frame. */) | |
968 | (spec, pixels, frame) | |
969 | Lisp_Object spec, pixels, frame; | |
970 | { | |
971 | Lisp_Object size; | |
972 | ||
973 | size = Qnil; | |
974 | if (valid_image_p (spec)) | |
975 | { | |
976 | struct frame *f = check_x_frame (frame); | |
977 | int id = lookup_image (f, spec); | |
978 | struct image *img = IMAGE_FROM_ID (f, id); | |
979 | int width = img->width + 2 * img->hmargin; | |
980 | int height = img->height + 2 * img->vmargin; | |
981 | ||
982 | if (NILP (pixels)) | |
983 | size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)), | |
984 | make_float ((double) height / FRAME_LINE_HEIGHT (f))); | |
985 | else | |
986 | size = Fcons (make_number (width), make_number (height)); | |
987 | } | |
988 | else | |
989 | error ("Invalid image specification"); | |
990 | ||
991 | return size; | |
992 | } | |
993 | ||
994 | ||
995 | DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0, | |
996 | doc: /* Return t if image SPEC has a mask bitmap. | |
997 | FRAME is the frame on which the image will be displayed. FRAME nil | |
998 | or omitted means use the selected frame. */) | |
999 | (spec, frame) | |
1000 | Lisp_Object spec, frame; | |
1001 | { | |
1002 | Lisp_Object mask; | |
1003 | ||
1004 | mask = Qnil; | |
1005 | if (valid_image_p (spec)) | |
1006 | { | |
1007 | struct frame *f = check_x_frame (frame); | |
1008 | int id = lookup_image (f, spec); | |
1009 | struct image *img = IMAGE_FROM_ID (f, id); | |
1010 | if (img->mask) | |
1011 | mask = Qt; | |
1012 | } | |
1013 | else | |
1014 | error ("Invalid image specification"); | |
1015 | ||
1016 | return mask; | |
1017 | } | |
1018 | ||
6ac3c7bb KS |
1019 | DEFUN ("image-extension-data", Fimage_extension_data, Simage_extension_data, 1, 2, 0, |
1020 | doc: /* Return extension data for image SPEC. | |
1021 | FRAME is the frame on which the image will be displayed. FRAME nil | |
1022 | or omitted means use the selected frame. */) | |
1023 | (spec, frame) | |
1024 | Lisp_Object spec, frame; | |
1025 | { | |
1026 | Lisp_Object ext; | |
1027 | ||
1028 | ext = Qnil; | |
1029 | if (valid_image_p (spec)) | |
1030 | { | |
1031 | struct frame *f = check_x_frame (frame); | |
1032 | int id = lookup_image (f, spec); | |
1033 | struct image *img = IMAGE_FROM_ID (f, id); | |
1034 | ext = img->data.lisp_val; | |
1035 | } | |
1036 | ||
1037 | return ext; | |
1038 | } | |
1039 | ||
4ef0d4d0 KS |
1040 | \f |
1041 | /*********************************************************************** | |
1042 | Image type independent image structures | |
1043 | ***********************************************************************/ | |
1044 | ||
1045 | static struct image *make_image P_ ((Lisp_Object spec, unsigned hash)); | |
1046 | static void free_image P_ ((struct frame *f, struct image *img)); | |
f1f25b99 | 1047 | static int check_image_size P_ ((struct frame *f, int width, int height)); |
4ef0d4d0 | 1048 | |
f1f25b99 CY |
1049 | #define MAX_IMAGE_SIZE 6.0 |
1050 | Lisp_Object Vmax_image_size; | |
4ef0d4d0 KS |
1051 | |
1052 | /* Allocate and return a new image structure for image specification | |
1053 | SPEC. SPEC has a hash value of HASH. */ | |
1054 | ||
1055 | static struct image * | |
1056 | make_image (spec, hash) | |
1057 | Lisp_Object spec; | |
1058 | unsigned hash; | |
1059 | { | |
1060 | struct image *img = (struct image *) xmalloc (sizeof *img); | |
a2bc5bdd | 1061 | Lisp_Object file = image_spec_value (spec, QCfile, NULL); |
4ef0d4d0 KS |
1062 | |
1063 | xassert (valid_image_p (spec)); | |
1064 | bzero (img, sizeof *img); | |
a2bc5bdd | 1065 | img->dependencies = NILP (file) ? Qnil : list1 (file); |
4ef0d4d0 KS |
1066 | img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL)); |
1067 | xassert (img->type != NULL); | |
1068 | img->spec = spec; | |
1069 | img->data.lisp_val = Qnil; | |
1070 | img->ascent = DEFAULT_IMAGE_ASCENT; | |
1071 | img->hash = hash; | |
6ac3c7bb | 1072 | img->corners[BOT_CORNER] = -1; /* Full image */ |
4ef0d4d0 KS |
1073 | return img; |
1074 | } | |
1075 | ||
1076 | ||
1077 | /* Free image IMG which was used on frame F, including its resources. */ | |
1078 | ||
1079 | static void | |
1080 | free_image (f, img) | |
1081 | struct frame *f; | |
1082 | struct image *img; | |
1083 | { | |
1084 | if (img) | |
1085 | { | |
354884c4 | 1086 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
1087 | |
1088 | /* Remove IMG from the hash table of its cache. */ | |
1089 | if (img->prev) | |
1090 | img->prev->next = img->next; | |
1091 | else | |
1092 | c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next; | |
1093 | ||
1094 | if (img->next) | |
1095 | img->next->prev = img->prev; | |
1096 | ||
1097 | c->images[img->id] = NULL; | |
1098 | ||
1099 | /* Free resources, then free IMG. */ | |
1100 | img->type->free (f, img); | |
1101 | xfree (img); | |
1102 | } | |
1103 | } | |
1104 | ||
f1f25b99 CY |
1105 | /* Return 1 if the given widths and heights are valid for display; |
1106 | otherwise, return 0. */ | |
1107 | ||
1108 | int | |
1109 | check_image_size (f, width, height) | |
1110 | struct frame *f; | |
1111 | int width; | |
1112 | int height; | |
1113 | { | |
7df4765a | 1114 | int w, h; |
f1f25b99 | 1115 | |
7df4765a | 1116 | if (width <= 0 || height <= 0) |
f1f25b99 CY |
1117 | return 0; |
1118 | ||
7df4765a KS |
1119 | if (INTEGERP (Vmax_image_size)) |
1120 | w = h = XINT (Vmax_image_size); | |
1121 | else if (FLOATP (Vmax_image_size)) | |
1122 | { | |
1123 | if (f != NULL) | |
1124 | { | |
1125 | w = FRAME_PIXEL_WIDTH (f); | |
1126 | h = FRAME_PIXEL_HEIGHT (f); | |
1127 | } | |
1128 | else | |
1129 | w = h = 1024; /* Arbitrary size for unknown frame. */ | |
1130 | w = (int) (XFLOAT_DATA (Vmax_image_size) * w); | |
1131 | h = (int) (XFLOAT_DATA (Vmax_image_size) * h); | |
1132 | } | |
1133 | else | |
1134 | return 1; | |
1135 | ||
1136 | return (width <= w && height <= h); | |
f1f25b99 | 1137 | } |
4ef0d4d0 KS |
1138 | |
1139 | /* Prepare image IMG for display on frame F. Must be called before | |
1140 | drawing an image. */ | |
1141 | ||
1142 | void | |
1143 | prepare_image_for_display (f, img) | |
1144 | struct frame *f; | |
1145 | struct image *img; | |
1146 | { | |
1147 | EMACS_TIME t; | |
1148 | ||
1149 | /* We're about to display IMG, so set its timestamp to `now'. */ | |
1150 | EMACS_GET_TIME (t); | |
1151 | img->timestamp = EMACS_SECS (t); | |
1152 | ||
1153 | /* If IMG doesn't have a pixmap yet, load it now, using the image | |
1154 | type dependent loader function. */ | |
1155 | if (img->pixmap == NO_PIXMAP && !img->load_failed_p) | |
1156 | img->load_failed_p = img->type->load (f, img) == 0; | |
c7fea325 | 1157 | |
4ef0d4d0 KS |
1158 | } |
1159 | ||
1160 | ||
1161 | /* Value is the number of pixels for the ascent of image IMG when | |
1162 | drawn in face FACE. */ | |
1163 | ||
1164 | int | |
1938bc36 | 1165 | image_ascent (img, face, slice) |
4ef0d4d0 KS |
1166 | struct image *img; |
1167 | struct face *face; | |
1938bc36 | 1168 | struct glyph_slice *slice; |
4ef0d4d0 | 1169 | { |
1938bc36 | 1170 | int height; |
4ef0d4d0 KS |
1171 | int ascent; |
1172 | ||
1938bc36 KS |
1173 | if (slice->height == img->height) |
1174 | height = img->height + img->vmargin; | |
1175 | else if (slice->y == 0) | |
1176 | height = slice->height + img->vmargin; | |
1177 | else | |
1178 | height = slice->height; | |
1179 | ||
4ef0d4d0 KS |
1180 | if (img->ascent == CENTERED_IMAGE_ASCENT) |
1181 | { | |
1182 | if (face->font) | |
1183 | { | |
1184 | #ifdef HAVE_NTGUI | |
1185 | /* W32 specific version. Why?. ++kfs */ | |
5fc9fdeb JR |
1186 | ascent = height / 2 - (FONT_DESCENT (face->font) |
1187 | - FONT_BASE (face->font)) / 2; | |
4ef0d4d0 KS |
1188 | #else |
1189 | /* This expression is arranged so that if the image can't be | |
1190 | exactly centered, it will be moved slightly up. This is | |
1191 | because a typical font is `top-heavy' (due to the presence | |
1192 | uppercase letters), so the image placement should err towards | |
1193 | being top-heavy too. It also just generally looks better. */ | |
edfda783 AR |
1194 | ascent = (height + FONT_BASE(face->font) |
1195 | - FONT_DESCENT(face->font) + 1) / 2; | |
4ef0d4d0 KS |
1196 | #endif /* HAVE_NTGUI */ |
1197 | } | |
1198 | else | |
1199 | ascent = height / 2; | |
1200 | } | |
1201 | else | |
1202 | ascent = (int) (height * img->ascent / 100.0); | |
1203 | ||
1204 | return ascent; | |
1205 | } | |
1206 | ||
1207 | \f | |
1208 | /* Image background colors. */ | |
1209 | ||
1210 | /* Find the "best" corner color of a bitmap. | |
1211 | On W32, XIMG is assumed to a device context with the bitmap selected. */ | |
1212 | ||
1213 | static RGB_PIXEL_COLOR | |
6ac3c7bb | 1214 | four_corners_best (ximg, corners, width, height) |
4ef0d4d0 | 1215 | XImagePtr_or_DC ximg; |
6ac3c7bb | 1216 | int *corners; |
4ef0d4d0 KS |
1217 | unsigned long width, height; |
1218 | { | |
6ac3c7bb | 1219 | RGB_PIXEL_COLOR corner_pixels[4], best; |
4ef0d4d0 KS |
1220 | int i, best_count; |
1221 | ||
6ac3c7bb KS |
1222 | if (corners && corners[BOT_CORNER] >= 0) |
1223 | { | |
1224 | /* Get the colors at the corner_pixels of ximg. */ | |
1225 | corner_pixels[0] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[TOP_CORNER]); | |
1226 | corner_pixels[1] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[TOP_CORNER]); | |
1227 | corner_pixels[2] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[BOT_CORNER] - 1); | |
1228 | corner_pixels[3] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[BOT_CORNER] - 1); | |
1229 | } | |
1230 | else | |
1231 | { | |
1232 | /* Get the colors at the corner_pixels of ximg. */ | |
1233 | corner_pixels[0] = GET_PIXEL (ximg, 0, 0); | |
1234 | corner_pixels[1] = GET_PIXEL (ximg, width - 1, 0); | |
1235 | corner_pixels[2] = GET_PIXEL (ximg, width - 1, height - 1); | |
1236 | corner_pixels[3] = GET_PIXEL (ximg, 0, height - 1); | |
1237 | } | |
4ef0d4d0 KS |
1238 | /* Choose the most frequently found color as background. */ |
1239 | for (i = best_count = 0; i < 4; ++i) | |
1240 | { | |
1241 | int j, n; | |
1242 | ||
1243 | for (j = n = 0; j < 4; ++j) | |
6ac3c7bb | 1244 | if (corner_pixels[i] == corner_pixels[j]) |
4ef0d4d0 KS |
1245 | ++n; |
1246 | ||
1247 | if (n > best_count) | |
6ac3c7bb | 1248 | best = corner_pixels[i], best_count = n; |
4ef0d4d0 KS |
1249 | } |
1250 | ||
1251 | return best; | |
1252 | } | |
1253 | ||
1254 | /* Portability macros */ | |
1255 | ||
1256 | #ifdef HAVE_NTGUI | |
1257 | ||
1258 | #define Destroy_Image(img_dc, prev) \ | |
1259 | do { SelectObject (img_dc, prev); DeleteDC (img_dc); } while (0) | |
1260 | ||
1261 | #define Free_Pixmap(display, pixmap) \ | |
1262 | DeleteObject (pixmap) | |
1263 | ||
edfda783 AR |
1264 | #elif defined (HAVE_NS) |
1265 | ||
1266 | #define Destroy_Image(ximg, dummy) \ | |
1267 | ns_release_object(ximg) | |
1268 | ||
1269 | #define Free_Pixmap(display, pixmap) \ | |
1270 | ns_release_object(pixmap) | |
1271 | ||
4ef0d4d0 KS |
1272 | #else |
1273 | ||
1274 | #define Destroy_Image(ximg, dummy) \ | |
1275 | XDestroyImage (ximg) | |
1276 | ||
1277 | #define Free_Pixmap(display, pixmap) \ | |
1278 | XFreePixmap (display, pixmap) | |
1279 | ||
edfda783 | 1280 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
1281 | |
1282 | ||
1283 | /* Return the `background' field of IMG. If IMG doesn't have one yet, | |
1284 | it is guessed heuristically. If non-zero, XIMG is an existing | |
1285 | XImage object (or device context with the image selected on W32) to | |
1f026899 | 1286 | use for the heuristic. */ |
4ef0d4d0 KS |
1287 | |
1288 | RGB_PIXEL_COLOR | |
1289 | image_background (img, f, ximg) | |
1290 | struct image *img; | |
1291 | struct frame *f; | |
1292 | XImagePtr_or_DC ximg; | |
1293 | { | |
1294 | if (! img->background_valid) | |
1295 | /* IMG doesn't have a background yet, try to guess a reasonable value. */ | |
1296 | { | |
1297 | int free_ximg = !ximg; | |
1298 | #ifdef HAVE_NTGUI | |
1299 | HGDIOBJ prev; | |
1300 | #endif /* HAVE_NTGUI */ | |
1301 | ||
1302 | if (free_ximg) | |
1303 | { | |
1304 | #ifndef HAVE_NTGUI | |
1305 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, | |
1306 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
1307 | #else | |
1308 | HDC frame_dc = get_frame_dc (f); | |
1309 | ximg = CreateCompatibleDC (frame_dc); | |
1310 | release_frame_dc (f, frame_dc); | |
1311 | prev = SelectObject (ximg, img->pixmap); | |
1312 | #endif /* !HAVE_NTGUI */ | |
1313 | } | |
1314 | ||
6ac3c7bb | 1315 | img->background = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
1316 | |
1317 | if (free_ximg) | |
1318 | Destroy_Image (ximg, prev); | |
1f026899 | 1319 | |
4ef0d4d0 KS |
1320 | img->background_valid = 1; |
1321 | } | |
1322 | ||
1323 | return img->background; | |
1324 | } | |
1325 | ||
1326 | /* Return the `background_transparent' field of IMG. If IMG doesn't | |
1327 | have one yet, it is guessed heuristically. If non-zero, MASK is an | |
1328 | existing XImage object to use for the heuristic. */ | |
1329 | ||
1330 | int | |
1331 | image_background_transparent (img, f, mask) | |
1332 | struct image *img; | |
1333 | struct frame *f; | |
1334 | XImagePtr_or_DC mask; | |
1335 | { | |
1336 | if (! img->background_transparent_valid) | |
1337 | /* IMG doesn't have a background yet, try to guess a reasonable value. */ | |
1338 | { | |
1339 | if (img->mask) | |
1340 | { | |
1341 | int free_mask = !mask; | |
1342 | #ifdef HAVE_NTGUI | |
1343 | HGDIOBJ prev; | |
1344 | #endif /* HAVE_NTGUI */ | |
1345 | ||
1346 | if (free_mask) | |
1347 | { | |
1348 | #ifndef HAVE_NTGUI | |
1349 | mask = XGetImage (FRAME_X_DISPLAY (f), img->mask, | |
1350 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
1351 | #else | |
1352 | HDC frame_dc = get_frame_dc (f); | |
1353 | mask = CreateCompatibleDC (frame_dc); | |
1354 | release_frame_dc (f, frame_dc); | |
1355 | prev = SelectObject (mask, img->mask); | |
1356 | #endif /* HAVE_NTGUI */ | |
1357 | } | |
1358 | ||
1359 | img->background_transparent | |
6ac3c7bb | 1360 | = (four_corners_best (mask, img->corners, img->width, img->height) == PIX_MASK_RETAIN); |
4ef0d4d0 KS |
1361 | |
1362 | if (free_mask) | |
1363 | Destroy_Image (mask, prev); | |
1364 | } | |
1365 | else | |
1366 | img->background_transparent = 0; | |
1367 | ||
1368 | img->background_transparent_valid = 1; | |
1369 | } | |
1370 | ||
1371 | return img->background_transparent; | |
1372 | } | |
1373 | ||
1374 | \f | |
1375 | /*********************************************************************** | |
1376 | Helper functions for X image types | |
1377 | ***********************************************************************/ | |
1378 | ||
1379 | static void x_clear_image_1 P_ ((struct frame *, struct image *, int, | |
1380 | int, int)); | |
1381 | static void x_clear_image P_ ((struct frame *f, struct image *img)); | |
1382 | static unsigned long x_alloc_image_color P_ ((struct frame *f, | |
1383 | struct image *img, | |
1384 | Lisp_Object color_name, | |
1385 | unsigned long dflt)); | |
1386 | ||
1387 | ||
1388 | /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means | |
1389 | free the pixmap if any. MASK_P non-zero means clear the mask | |
1390 | pixmap if any. COLORS_P non-zero means free colors allocated for | |
1391 | the image, if any. */ | |
1392 | ||
1393 | static void | |
1394 | x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p) | |
1395 | struct frame *f; | |
1396 | struct image *img; | |
1397 | int pixmap_p, mask_p, colors_p; | |
1398 | { | |
1399 | if (pixmap_p && img->pixmap) | |
1400 | { | |
1401 | Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap); | |
1402 | img->pixmap = NO_PIXMAP; | |
5a06864f | 1403 | /* NOTE (HAVE_NS): background color is NOT an indexed color! */ |
4ef0d4d0 KS |
1404 | img->background_valid = 0; |
1405 | } | |
1406 | ||
1407 | if (mask_p && img->mask) | |
1408 | { | |
1409 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
1410 | img->mask = NO_PIXMAP; | |
1411 | img->background_transparent_valid = 0; | |
1412 | } | |
1413 | ||
1414 | if (colors_p && img->ncolors) | |
1415 | { | |
4ef0d4d0 KS |
1416 | /* W32_TODO: color table support. */ |
1417 | #ifdef HAVE_X_WINDOWS | |
1418 | x_free_colors (f, img->colors, img->ncolors); | |
1419 | #endif /* HAVE_X_WINDOWS */ | |
1420 | xfree (img->colors); | |
1421 | img->colors = NULL; | |
1422 | img->ncolors = 0; | |
1423 | } | |
c7fea325 | 1424 | |
4ef0d4d0 KS |
1425 | } |
1426 | ||
1427 | /* Free X resources of image IMG which is used on frame F. */ | |
1428 | ||
1429 | static void | |
1430 | x_clear_image (f, img) | |
1431 | struct frame *f; | |
1432 | struct image *img; | |
1433 | { | |
1434 | BLOCK_INPUT; | |
1435 | x_clear_image_1 (f, img, 1, 1, 1); | |
1436 | UNBLOCK_INPUT; | |
1437 | } | |
1438 | ||
1439 | ||
1440 | /* Allocate color COLOR_NAME for image IMG on frame F. If color | |
1441 | cannot be allocated, use DFLT. Add a newly allocated color to | |
1442 | IMG->colors, so that it can be freed again. Value is the pixel | |
1443 | color. */ | |
1444 | ||
1445 | static unsigned long | |
1446 | x_alloc_image_color (f, img, color_name, dflt) | |
1447 | struct frame *f; | |
1448 | struct image *img; | |
1449 | Lisp_Object color_name; | |
1450 | unsigned long dflt; | |
1451 | { | |
1452 | XColor color; | |
1453 | unsigned long result; | |
1454 | ||
1455 | xassert (STRINGP (color_name)); | |
1456 | ||
1457 | if (x_defined_color (f, SDATA (color_name), &color, 1)) | |
1458 | { | |
1459 | /* This isn't called frequently so we get away with simply | |
1460 | reallocating the color vector to the needed size, here. */ | |
1461 | ++img->ncolors; | |
1462 | img->colors = | |
1463 | (unsigned long *) xrealloc (img->colors, | |
1464 | img->ncolors * sizeof *img->colors); | |
1465 | img->colors[img->ncolors - 1] = color.pixel; | |
1466 | result = color.pixel; | |
1467 | } | |
1468 | else | |
1469 | result = dflt; | |
1470 | ||
1471 | return result; | |
1472 | } | |
1473 | ||
1474 | ||
1475 | \f | |
1476 | /*********************************************************************** | |
1477 | Image Cache | |
1478 | ***********************************************************************/ | |
1479 | ||
d3c35d17 | 1480 | static struct image *search_image_cache P_ ((struct frame *, Lisp_Object, unsigned)); |
4ef0d4d0 KS |
1481 | static void cache_image P_ ((struct frame *f, struct image *img)); |
1482 | static void postprocess_image P_ ((struct frame *, struct image *)); | |
1483 | ||
1484 | /* Return a new, initialized image cache that is allocated from the | |
1485 | heap. Call free_image_cache to free an image cache. */ | |
1486 | ||
1487 | struct image_cache * | |
1488 | make_image_cache () | |
1489 | { | |
1490 | struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c); | |
1491 | int size; | |
1492 | ||
1493 | bzero (c, sizeof *c); | |
1494 | c->size = 50; | |
1495 | c->images = (struct image **) xmalloc (c->size * sizeof *c->images); | |
1496 | size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets; | |
1497 | c->buckets = (struct image **) xmalloc (size); | |
1498 | bzero (c->buckets, size); | |
1499 | return c; | |
1500 | } | |
1501 | ||
1502 | ||
d3c35d17 CY |
1503 | /* Find an image matching SPEC in the cache, and return it. If no |
1504 | image is found, return NULL. */ | |
1505 | static struct image * | |
1506 | search_image_cache (f, spec, hash) | |
483e7ae0 CY |
1507 | struct frame *f; |
1508 | Lisp_Object spec; | |
d3c35d17 | 1509 | unsigned hash; |
483e7ae0 | 1510 | { |
91be6b66 | 1511 | struct image *img; |
354884c4 | 1512 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
483e7ae0 CY |
1513 | int i = hash % IMAGE_CACHE_BUCKETS_SIZE; |
1514 | ||
8198352b CY |
1515 | if (!c) return NULL; |
1516 | ||
d3c35d17 CY |
1517 | /* If the image spec does not specify a background color, the cached |
1518 | image must have the same background color as the current frame. | |
cf26ebe8 CY |
1519 | The foreground color must also match, for the sake of monochrome |
1520 | images. | |
1521 | ||
1522 | In fact, we could ignore the foreground color matching condition | |
1523 | for color images, or if the image spec specifies :foreground; | |
1524 | similarly we could ignore the background color matching condition | |
1525 | for formats that don't use transparency (such as jpeg), or if the | |
1526 | image spec specifies :background. However, the extra memory | |
1527 | usage is probably negligible in practice, so we don't bother. */ | |
d3c35d17 | 1528 | |
483e7ae0 | 1529 | for (img = c->buckets[i]; img; img = img->next) |
d3c35d17 CY |
1530 | if (img->hash == hash |
1531 | && !NILP (Fequal (img->spec, spec)) | |
c5b8e0ea YM |
1532 | && img->frame_foreground == FRAME_FOREGROUND_PIXEL (f) |
1533 | && img->frame_background == FRAME_BACKGROUND_PIXEL (f)) | |
d3c35d17 CY |
1534 | break; |
1535 | return img; | |
1536 | } | |
1537 | ||
1538 | ||
1539 | /* Search frame F for an image with spec SPEC, and free it. */ | |
1540 | ||
1541 | static void | |
1542 | uncache_image (f, spec) | |
1543 | struct frame *f; | |
1544 | Lisp_Object spec; | |
1545 | { | |
1546 | struct image *img = search_image_cache (f, spec, sxhash (spec, 0)); | |
1547 | if (img) | |
be3faa80 CY |
1548 | { |
1549 | free_image (f, img); | |
1550 | /* As display glyphs may still be referring to the image ID, we | |
1551 | must garbage the frame (Bug#6426). */ | |
1552 | SET_FRAME_GARBAGED (f); | |
1553 | } | |
483e7ae0 CY |
1554 | } |
1555 | ||
1556 | ||
4ef0d4d0 KS |
1557 | /* Free image cache of frame F. Be aware that X frames share images |
1558 | caches. */ | |
1559 | ||
1560 | void | |
1561 | free_image_cache (f) | |
1562 | struct frame *f; | |
1563 | { | |
354884c4 | 1564 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
1565 | if (c) |
1566 | { | |
1567 | int i; | |
1568 | ||
1569 | /* Cache should not be referenced by any frame when freed. */ | |
1570 | xassert (c->refcount == 0); | |
1571 | ||
1572 | for (i = 0; i < c->used; ++i) | |
1573 | free_image (f, c->images[i]); | |
1574 | xfree (c->images); | |
1575 | xfree (c->buckets); | |
1576 | xfree (c); | |
354884c4 | 1577 | FRAME_IMAGE_CACHE (f) = NULL; |
4ef0d4d0 KS |
1578 | } |
1579 | } | |
1580 | ||
1581 | ||
a2bc5bdd SM |
1582 | /* Clear image cache of frame F. FILTER=t means free all images. |
1583 | FILTER=nil means clear only images that haven't been | |
1584 | displayed for some time. | |
1585 | Else, only free the images which have FILTER in their `dependencies'. | |
1586 | Should be called from time to time to reduce the number of loaded images. | |
1587 | If image-cache-eviction-delay is non-nil, this frees images in the cache | |
1588 | which weren't displayed for at least that many seconds. */ | |
4ef0d4d0 KS |
1589 | |
1590 | void | |
a2bc5bdd | 1591 | clear_image_cache (struct frame *f, Lisp_Object filter) |
4ef0d4d0 | 1592 | { |
354884c4 | 1593 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 | 1594 | |
a2bc5bdd | 1595 | if (c && (!NILP (filter) || INTEGERP (Vimage_cache_eviction_delay))) |
4ef0d4d0 KS |
1596 | { |
1597 | EMACS_TIME t; | |
1598 | unsigned long old; | |
1599 | int i, nfreed; | |
1600 | ||
1601 | EMACS_GET_TIME (t); | |
1602 | old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay); | |
1603 | ||
1604 | /* Block input so that we won't be interrupted by a SIGIO | |
1605 | while being in an inconsistent state. */ | |
1606 | BLOCK_INPUT; | |
1607 | ||
1608 | for (i = nfreed = 0; i < c->used; ++i) | |
1609 | { | |
1610 | struct image *img = c->images[i]; | |
1611 | if (img != NULL | |
a2bc5bdd SM |
1612 | && (NILP (filter) ? img->timestamp < old |
1613 | : (EQ (Qt, filter) | |
1614 | || !NILP (Fmember (filter, img->dependencies))))) | |
4ef0d4d0 KS |
1615 | { |
1616 | free_image (f, img); | |
1617 | ++nfreed; | |
1618 | } | |
1619 | } | |
1620 | ||
1621 | /* We may be clearing the image cache because, for example, | |
1622 | Emacs was iconified for a longer period of time. In that | |
1623 | case, current matrices may still contain references to | |
1624 | images freed above. So, clear these matrices. */ | |
1625 | if (nfreed) | |
1626 | { | |
1627 | Lisp_Object tail, frame; | |
1628 | ||
1629 | FOR_EACH_FRAME (tail, frame) | |
1630 | { | |
1631 | struct frame *f = XFRAME (frame); | |
354884c4 | 1632 | if (FRAME_IMAGE_CACHE (f) == c) |
4ef0d4d0 KS |
1633 | clear_current_matrices (f); |
1634 | } | |
1635 | ||
1636 | ++windows_or_buffers_changed; | |
1637 | } | |
1638 | ||
1639 | UNBLOCK_INPUT; | |
1640 | } | |
1641 | } | |
1642 | ||
354884c4 | 1643 | void |
a2bc5bdd | 1644 | clear_image_caches (Lisp_Object filter) |
354884c4 SM |
1645 | { |
1646 | /* FIXME: We want to do | |
1647 | * struct terminal *t; | |
1648 | * for (t = terminal_list; t; t = t->next_terminal) | |
1649 | * clear_image_cache (t, filter); */ | |
1650 | Lisp_Object tail, frame; | |
1651 | FOR_EACH_FRAME (tail, frame) | |
1652 | if (FRAME_WINDOW_P (XFRAME (frame))) | |
a2bc5bdd | 1653 | clear_image_cache (XFRAME (frame), filter); |
354884c4 | 1654 | } |
4ef0d4d0 KS |
1655 | |
1656 | DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache, | |
1657 | 0, 1, 0, | |
a2bc5bdd SM |
1658 | doc: /* Clear the image cache. |
1659 | FILTER nil or a frame means clear all images in the selected frame. | |
1660 | FILTER t means clear the image caches of all frames. | |
1661 | Anything else, means only clear those images which refer to FILTER, | |
1662 | which is then usually a filename. */) | |
1663 | (filter) | |
1664 | Lisp_Object filter; | |
4ef0d4d0 | 1665 | { |
a2bc5bdd SM |
1666 | if (!(EQ (filter, Qnil) || FRAMEP (filter))) |
1667 | clear_image_caches (filter); | |
4ef0d4d0 | 1668 | else |
a2bc5bdd | 1669 | clear_image_cache (check_x_frame (filter), Qt); |
4ef0d4d0 KS |
1670 | |
1671 | return Qnil; | |
1672 | } | |
1673 | ||
1674 | ||
483e7ae0 CY |
1675 | DEFUN ("image-refresh", Fimage_refresh, Simage_refresh, |
1676 | 1, 2, 0, | |
1677 | doc: /* Refresh the image with specification SPEC on frame FRAME. | |
1678 | If SPEC specifies an image file, the displayed image is updated with | |
1679 | the current contents of that file. | |
1680 | FRAME nil or omitted means use the selected frame. | |
1681 | FRAME t means refresh the image on all frames. */) | |
1682 | (spec, frame) | |
1683 | Lisp_Object spec, frame; | |
1684 | { | |
1685 | if (!valid_image_p (spec)) | |
1686 | error ("Invalid image specification"); | |
1687 | ||
1688 | if (EQ (frame, Qt)) | |
1689 | { | |
1690 | Lisp_Object tail; | |
1691 | FOR_EACH_FRAME (tail, frame) | |
1692 | { | |
1693 | struct frame *f = XFRAME (frame); | |
1694 | if (FRAME_WINDOW_P (f)) | |
1695 | uncache_image (f, spec); | |
1696 | } | |
1697 | } | |
1698 | else | |
1699 | uncache_image (check_x_frame (frame), spec); | |
1700 | ||
1701 | return Qnil; | |
1702 | } | |
1703 | ||
1704 | ||
4ef0d4d0 KS |
1705 | /* Compute masks and transform image IMG on frame F, as specified |
1706 | by the image's specification, */ | |
1707 | ||
1708 | static void | |
1709 | postprocess_image (f, img) | |
1710 | struct frame *f; | |
1711 | struct image *img; | |
1712 | { | |
1713 | /* Manipulation of the image's mask. */ | |
1714 | if (img->pixmap) | |
1715 | { | |
1716 | Lisp_Object conversion, spec; | |
1717 | Lisp_Object mask; | |
1718 | ||
1719 | spec = img->spec; | |
1720 | ||
1721 | /* `:heuristic-mask t' | |
1722 | `:mask heuristic' | |
1723 | means build a mask heuristically. | |
1724 | `:heuristic-mask (R G B)' | |
1725 | `:mask (heuristic (R G B))' | |
1726 | means build a mask from color (R G B) in the | |
1727 | image. | |
1728 | `:mask nil' | |
1729 | means remove a mask, if any. */ | |
1730 | ||
1731 | mask = image_spec_value (spec, QCheuristic_mask, NULL); | |
1732 | if (!NILP (mask)) | |
1733 | x_build_heuristic_mask (f, img, mask); | |
1734 | else | |
1735 | { | |
1736 | int found_p; | |
1737 | ||
1738 | mask = image_spec_value (spec, QCmask, &found_p); | |
1739 | ||
1740 | if (EQ (mask, Qheuristic)) | |
1741 | x_build_heuristic_mask (f, img, Qt); | |
1742 | else if (CONSP (mask) | |
1743 | && EQ (XCAR (mask), Qheuristic)) | |
1744 | { | |
1745 | if (CONSP (XCDR (mask))) | |
1746 | x_build_heuristic_mask (f, img, XCAR (XCDR (mask))); | |
1747 | else | |
1748 | x_build_heuristic_mask (f, img, XCDR (mask)); | |
1749 | } | |
1750 | else if (NILP (mask) && found_p && img->mask) | |
1751 | { | |
1752 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
1753 | img->mask = NO_PIXMAP; | |
1754 | } | |
1755 | } | |
1756 | ||
1757 | ||
1758 | /* Should we apply an image transformation algorithm? */ | |
1759 | conversion = image_spec_value (spec, QCconversion, NULL); | |
1760 | if (EQ (conversion, Qdisabled)) | |
1761 | x_disable_image (f, img); | |
1762 | else if (EQ (conversion, Qlaplace)) | |
1763 | x_laplace (f, img); | |
1764 | else if (EQ (conversion, Qemboss)) | |
1765 | x_emboss (f, img); | |
1766 | else if (CONSP (conversion) | |
1767 | && EQ (XCAR (conversion), Qedge_detection)) | |
1768 | { | |
1769 | Lisp_Object tem; | |
1770 | tem = XCDR (conversion); | |
1771 | if (CONSP (tem)) | |
1772 | x_edge_detection (f, img, | |
1773 | Fplist_get (tem, QCmatrix), | |
1774 | Fplist_get (tem, QCcolor_adjustment)); | |
1775 | } | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | ||
1780 | /* Return the id of image with Lisp specification SPEC on frame F. | |
1781 | SPEC must be a valid Lisp image specification (see valid_image_p). */ | |
1782 | ||
1783 | int | |
1784 | lookup_image (f, spec) | |
1785 | struct frame *f; | |
1786 | Lisp_Object spec; | |
1787 | { | |
4a0e3f08 | 1788 | struct image_cache *c; |
4ef0d4d0 | 1789 | struct image *img; |
4ef0d4d0 KS |
1790 | unsigned hash; |
1791 | struct gcpro gcpro1; | |
1792 | EMACS_TIME now; | |
1793 | ||
1794 | /* F must be a window-system frame, and SPEC must be a valid image | |
1795 | specification. */ | |
1796 | xassert (FRAME_WINDOW_P (f)); | |
1797 | xassert (valid_image_p (spec)); | |
1798 | ||
354884c4 | 1799 | c = FRAME_IMAGE_CACHE (f); |
4a0e3f08 | 1800 | |
4ef0d4d0 KS |
1801 | GCPRO1 (spec); |
1802 | ||
1803 | /* Look up SPEC in the hash table of the image cache. */ | |
1804 | hash = sxhash (spec, 0); | |
d3c35d17 | 1805 | img = search_image_cache (f, spec, hash); |
f1f25b99 CY |
1806 | if (img && img->load_failed_p) |
1807 | { | |
1808 | free_image (f, img); | |
1809 | img = NULL; | |
1810 | } | |
1811 | ||
4ef0d4d0 KS |
1812 | /* If not found, create a new image and cache it. */ |
1813 | if (img == NULL) | |
1814 | { | |
1815 | extern Lisp_Object Qpostscript; | |
1816 | ||
1817 | BLOCK_INPUT; | |
1818 | img = make_image (spec, hash); | |
1819 | cache_image (f, img); | |
1820 | img->load_failed_p = img->type->load (f, img) == 0; | |
c5b8e0ea YM |
1821 | img->frame_foreground = FRAME_FOREGROUND_PIXEL (f); |
1822 | img->frame_background = FRAME_BACKGROUND_PIXEL (f); | |
4ef0d4d0 KS |
1823 | |
1824 | /* If we can't load the image, and we don't have a width and | |
1825 | height, use some arbitrary width and height so that we can | |
1826 | draw a rectangle for it. */ | |
1827 | if (img->load_failed_p) | |
1828 | { | |
1829 | Lisp_Object value; | |
1830 | ||
1831 | value = image_spec_value (spec, QCwidth, NULL); | |
1832 | img->width = (INTEGERP (value) | |
1833 | ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH); | |
1834 | value = image_spec_value (spec, QCheight, NULL); | |
1835 | img->height = (INTEGERP (value) | |
1836 | ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT); | |
1837 | } | |
1838 | else | |
1839 | { | |
1840 | /* Handle image type independent image attributes | |
1841 | `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF', | |
1842 | `:background COLOR'. */ | |
1843 | Lisp_Object ascent, margin, relief, bg; | |
1844 | ||
1845 | ascent = image_spec_value (spec, QCascent, NULL); | |
1846 | if (INTEGERP (ascent)) | |
1847 | img->ascent = XFASTINT (ascent); | |
1848 | else if (EQ (ascent, Qcenter)) | |
1849 | img->ascent = CENTERED_IMAGE_ASCENT; | |
1850 | ||
1851 | margin = image_spec_value (spec, QCmargin, NULL); | |
1852 | if (INTEGERP (margin) && XINT (margin) >= 0) | |
1853 | img->vmargin = img->hmargin = XFASTINT (margin); | |
1854 | else if (CONSP (margin) && INTEGERP (XCAR (margin)) | |
1855 | && INTEGERP (XCDR (margin))) | |
1856 | { | |
1857 | if (XINT (XCAR (margin)) > 0) | |
1858 | img->hmargin = XFASTINT (XCAR (margin)); | |
1859 | if (XINT (XCDR (margin)) > 0) | |
1860 | img->vmargin = XFASTINT (XCDR (margin)); | |
1861 | } | |
1862 | ||
1863 | relief = image_spec_value (spec, QCrelief, NULL); | |
1864 | if (INTEGERP (relief)) | |
1865 | { | |
1866 | img->relief = XINT (relief); | |
1ea40aa2 EZ |
1867 | img->hmargin += eabs (img->relief); |
1868 | img->vmargin += eabs (img->relief); | |
4ef0d4d0 KS |
1869 | } |
1870 | ||
1871 | if (! img->background_valid) | |
1872 | { | |
1873 | bg = image_spec_value (img->spec, QCbackground, NULL); | |
1874 | if (!NILP (bg)) | |
1875 | { | |
1876 | img->background | |
1877 | = x_alloc_image_color (f, img, bg, | |
1878 | FRAME_BACKGROUND_PIXEL (f)); | |
1879 | img->background_valid = 1; | |
1880 | } | |
1881 | } | |
1882 | ||
1883 | /* Do image transformations and compute masks, unless we | |
1884 | don't have the image yet. */ | |
1885 | if (!EQ (*img->type->type, Qpostscript)) | |
1886 | postprocess_image (f, img); | |
1887 | } | |
1888 | ||
1889 | UNBLOCK_INPUT; | |
1890 | } | |
1891 | ||
1892 | /* We're using IMG, so set its timestamp to `now'. */ | |
1893 | EMACS_GET_TIME (now); | |
1894 | img->timestamp = EMACS_SECS (now); | |
1895 | ||
1896 | UNGCPRO; | |
1897 | ||
1898 | /* Value is the image id. */ | |
1899 | return img->id; | |
1900 | } | |
1901 | ||
1902 | ||
1903 | /* Cache image IMG in the image cache of frame F. */ | |
1904 | ||
1905 | static void | |
1906 | cache_image (f, img) | |
1907 | struct frame *f; | |
1908 | struct image *img; | |
1909 | { | |
354884c4 | 1910 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
1911 | int i; |
1912 | ||
1913 | /* Find a free slot in c->images. */ | |
1914 | for (i = 0; i < c->used; ++i) | |
1915 | if (c->images[i] == NULL) | |
1916 | break; | |
1917 | ||
1918 | /* If no free slot found, maybe enlarge c->images. */ | |
1919 | if (i == c->used && c->used == c->size) | |
1920 | { | |
1921 | c->size *= 2; | |
1922 | c->images = (struct image **) xrealloc (c->images, | |
1923 | c->size * sizeof *c->images); | |
1924 | } | |
1925 | ||
1926 | /* Add IMG to c->images, and assign IMG an id. */ | |
1927 | c->images[i] = img; | |
1928 | img->id = i; | |
1929 | if (i == c->used) | |
1930 | ++c->used; | |
1931 | ||
1932 | /* Add IMG to the cache's hash table. */ | |
1933 | i = img->hash % IMAGE_CACHE_BUCKETS_SIZE; | |
1934 | img->next = c->buckets[i]; | |
1935 | if (img->next) | |
1936 | img->next->prev = img; | |
1937 | img->prev = NULL; | |
1938 | c->buckets[i] = img; | |
1939 | } | |
1940 | ||
1941 | ||
1942 | /* Call FN on every image in the image cache of frame F. Used to mark | |
1943 | Lisp Objects in the image cache. */ | |
1944 | ||
354884c4 SM |
1945 | /* Mark Lisp objects in image IMG. */ |
1946 | ||
1947 | static void | |
1948 | mark_image (img) | |
1949 | struct image *img; | |
1950 | { | |
1951 | mark_object (img->spec); | |
a2bc5bdd | 1952 | mark_object (img->dependencies); |
354884c4 SM |
1953 | |
1954 | if (!NILP (img->data.lisp_val)) | |
1955 | mark_object (img->data.lisp_val); | |
1956 | } | |
1957 | ||
1958 | ||
4ef0d4d0 | 1959 | void |
354884c4 | 1960 | mark_image_cache (struct image_cache *c) |
4ef0d4d0 | 1961 | { |
354884c4 | 1962 | if (c) |
4ef0d4d0 | 1963 | { |
354884c4 SM |
1964 | int i; |
1965 | for (i = 0; i < c->used; ++i) | |
1966 | if (c->images[i]) | |
1967 | mark_image (c->images[i]); | |
4ef0d4d0 KS |
1968 | } |
1969 | } | |
1970 | ||
1971 | ||
1972 | \f | |
1973 | /*********************************************************************** | |
9e2a2647 | 1974 | X / NS / W32 support code |
4ef0d4d0 KS |
1975 | ***********************************************************************/ |
1976 | ||
1977 | #ifdef HAVE_NTGUI | |
1978 | ||
1979 | /* Macro for defining functions that will be loaded from image DLLs. */ | |
09e8eb5b | 1980 | #define DEF_IMGLIB_FN(func) int (FAR CDECL *fn_##func)() |
4ef0d4d0 KS |
1981 | |
1982 | /* Macro for loading those image functions from the library. */ | |
1983 | #define LOAD_IMGLIB_FN(lib,func) { \ | |
1984 | fn_##func = (void *) GetProcAddress (lib, #func); \ | |
1985 | if (!fn_##func) return 0; \ | |
1986 | } | |
1987 | ||
0855eb52 JB |
1988 | /* Load a DLL implementing an image type. |
1989 | The `image-library-alist' variable associates a symbol, | |
3046a84b | 1990 | identifying an image type, to a list of possible filenames. |
0855eb52 JB |
1991 | The function returns NULL if no library could be loaded for |
1992 | the given image type, or if the library was previously loaded; | |
1993 | else the handle of the DLL. */ | |
1994 | static HMODULE | |
1995 | w32_delayed_load (Lisp_Object libraries, Lisp_Object type) | |
1996 | { | |
1997 | HMODULE library = NULL; | |
1998 | ||
1999 | if (CONSP (libraries) && NILP (Fassq (type, Vimage_type_cache))) | |
2000 | { | |
2001 | Lisp_Object dlls = Fassq (type, libraries); | |
2002 | ||
2003 | if (CONSP (dlls)) | |
2004 | for (dlls = XCDR (dlls); CONSP (dlls); dlls = XCDR (dlls)) | |
2005 | { | |
2006 | CHECK_STRING_CAR (dlls); | |
2007 | if (library = LoadLibrary (SDATA (XCAR (dlls)))) | |
2008 | break; | |
2009 | } | |
2010 | } | |
2011 | ||
2012 | return library; | |
2013 | } | |
2014 | ||
4ef0d4d0 KS |
2015 | #endif /* HAVE_NTGUI */ |
2016 | ||
2017 | static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int, | |
2018 | XImagePtr *, Pixmap *)); | |
2019 | static void x_destroy_x_image P_ ((XImagePtr)); | |
2020 | static void x_put_x_image P_ ((struct frame *, XImagePtr, Pixmap, int, int)); | |
2021 | ||
2022 | ||
2023 | /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on | |
2024 | frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created. | |
2025 | Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated | |
2026 | via xmalloc. Print error messages via image_error if an error | |
2027 | occurs. Value is non-zero if successful. | |
2028 | ||
2029 | On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH | |
2030 | should indicate the bit depth of the image. */ | |
2031 | ||
2032 | static int | |
2033 | x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap) | |
2034 | struct frame *f; | |
2035 | int width, height, depth; | |
2036 | XImagePtr *ximg; | |
2037 | Pixmap *pixmap; | |
2038 | { | |
2039 | #ifdef HAVE_X_WINDOWS | |
2040 | Display *display = FRAME_X_DISPLAY (f); | |
2041 | Window window = FRAME_X_WINDOW (f); | |
2042 | Screen *screen = FRAME_X_SCREEN (f); | |
2043 | ||
2044 | xassert (interrupt_input_blocked); | |
2045 | ||
2046 | if (depth <= 0) | |
2047 | depth = DefaultDepthOfScreen (screen); | |
2048 | *ximg = XCreateImage (display, DefaultVisualOfScreen (screen), | |
2049 | depth, ZPixmap, 0, NULL, width, height, | |
2050 | depth > 16 ? 32 : depth > 8 ? 16 : 8, 0); | |
2051 | if (*ximg == NULL) | |
2052 | { | |
2053 | image_error ("Unable to allocate X image", Qnil, Qnil); | |
2054 | return 0; | |
2055 | } | |
2056 | ||
2057 | /* Allocate image raster. */ | |
2058 | (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height); | |
2059 | ||
2060 | /* Allocate a pixmap of the same size. */ | |
2061 | *pixmap = XCreatePixmap (display, window, width, height, depth); | |
2062 | if (*pixmap == NO_PIXMAP) | |
2063 | { | |
2064 | x_destroy_x_image (*ximg); | |
2065 | *ximg = NULL; | |
2066 | image_error ("Unable to create X pixmap", Qnil, Qnil); | |
2067 | return 0; | |
2068 | } | |
2069 | ||
2070 | return 1; | |
2071 | #endif /* HAVE_X_WINDOWS */ | |
2072 | ||
2073 | #ifdef HAVE_NTGUI | |
2074 | ||
2075 | BITMAPINFOHEADER *header; | |
2076 | HDC hdc; | |
2077 | int scanline_width_bits; | |
2078 | int remainder; | |
2079 | int palette_colors = 0; | |
2080 | ||
2081 | if (depth == 0) | |
2082 | depth = 24; | |
2083 | ||
2084 | if (depth != 1 && depth != 4 && depth != 8 | |
2085 | && depth != 16 && depth != 24 && depth != 32) | |
2086 | { | |
2087 | image_error ("Invalid image bit depth specified", Qnil, Qnil); | |
2088 | return 0; | |
2089 | } | |
2090 | ||
2091 | scanline_width_bits = width * depth; | |
2092 | remainder = scanline_width_bits % 32; | |
2093 | ||
2094 | if (remainder) | |
2095 | scanline_width_bits += 32 - remainder; | |
2096 | ||
2097 | /* Bitmaps with a depth less than 16 need a palette. */ | |
2098 | /* BITMAPINFO structure already contains the first RGBQUAD. */ | |
2099 | if (depth < 16) | |
2100 | palette_colors = 1 << depth - 1; | |
2101 | ||
2102 | *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD)); | |
2103 | if (*ximg == NULL) | |
2104 | { | |
2105 | image_error ("Unable to allocate memory for XImage", Qnil, Qnil); | |
2106 | return 0; | |
2107 | } | |
2108 | ||
2109 | header = &((*ximg)->info.bmiHeader); | |
2110 | bzero (&((*ximg)->info), sizeof (BITMAPINFO)); | |
2111 | header->biSize = sizeof (*header); | |
2112 | header->biWidth = width; | |
2113 | header->biHeight = -height; /* negative indicates a top-down bitmap. */ | |
2114 | header->biPlanes = 1; | |
2115 | header->biBitCount = depth; | |
2116 | header->biCompression = BI_RGB; | |
2117 | header->biClrUsed = palette_colors; | |
2118 | ||
2119 | /* TODO: fill in palette. */ | |
2120 | if (depth == 1) | |
2121 | { | |
2122 | (*ximg)->info.bmiColors[0].rgbBlue = 0; | |
2123 | (*ximg)->info.bmiColors[0].rgbGreen = 0; | |
2124 | (*ximg)->info.bmiColors[0].rgbRed = 0; | |
2125 | (*ximg)->info.bmiColors[0].rgbReserved = 0; | |
2126 | (*ximg)->info.bmiColors[1].rgbBlue = 255; | |
2127 | (*ximg)->info.bmiColors[1].rgbGreen = 255; | |
2128 | (*ximg)->info.bmiColors[1].rgbRed = 255; | |
2129 | (*ximg)->info.bmiColors[1].rgbReserved = 0; | |
2130 | } | |
2131 | ||
2132 | hdc = get_frame_dc (f); | |
2133 | ||
2134 | /* Create a DIBSection and raster array for the bitmap, | |
2135 | and store its handle in *pixmap. */ | |
2136 | *pixmap = CreateDIBSection (hdc, &((*ximg)->info), | |
2137 | (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS, | |
2e09fef1 EZ |
2138 | /* casting avoids a GCC warning */ |
2139 | (void **)&((*ximg)->data), NULL, 0); | |
4ef0d4d0 KS |
2140 | |
2141 | /* Realize display palette and garbage all frames. */ | |
2142 | release_frame_dc (f, hdc); | |
2143 | ||
2144 | if (*pixmap == NULL) | |
2145 | { | |
5fc9fdeb | 2146 | DWORD err = GetLastError (); |
4ef0d4d0 KS |
2147 | Lisp_Object errcode; |
2148 | /* All system errors are < 10000, so the following is safe. */ | |
2149 | XSETINT (errcode, (int) err); | |
2150 | image_error ("Unable to create bitmap, error code %d", errcode, Qnil); | |
2151 | x_destroy_x_image (*ximg); | |
2152 | return 0; | |
2153 | } | |
2154 | ||
2155 | return 1; | |
2156 | ||
2157 | #endif /* HAVE_NTGUI */ | |
2158 | ||
edfda783 AR |
2159 | #ifdef HAVE_NS |
2160 | *pixmap = ns_image_for_XPM(width, height, depth); | |
2161 | if (*pixmap == 0) | |
2162 | { | |
2163 | *ximg = NULL; | |
2164 | image_error ("Unable to allocate NSImage for XPM pixmap", Qnil, Qnil); | |
2165 | return 0; | |
2166 | } | |
2167 | *ximg = *pixmap; | |
2168 | return 1; | |
2169 | #endif | |
4ef0d4d0 KS |
2170 | } |
2171 | ||
2172 | ||
2173 | /* Destroy XImage XIMG. Free XIMG->data. */ | |
2174 | ||
2175 | static void | |
2176 | x_destroy_x_image (ximg) | |
2177 | XImagePtr ximg; | |
2178 | { | |
2179 | xassert (interrupt_input_blocked); | |
2180 | if (ximg) | |
2181 | { | |
2182 | #ifdef HAVE_X_WINDOWS | |
2183 | xfree (ximg->data); | |
2184 | ximg->data = NULL; | |
2185 | XDestroyImage (ximg); | |
2186 | #endif /* HAVE_X_WINDOWS */ | |
2187 | #ifdef HAVE_NTGUI | |
2188 | /* Data will be freed by DestroyObject. */ | |
2189 | ximg->data = NULL; | |
2190 | xfree (ximg); | |
2191 | #endif /* HAVE_NTGUI */ | |
edfda783 AR |
2192 | #ifdef HAVE_NS |
2193 | ns_release_object(ximg); | |
2194 | #endif /* HAVE_NS */ | |
4ef0d4d0 KS |
2195 | } |
2196 | } | |
2197 | ||
2198 | ||
2199 | /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT | |
2200 | are width and height of both the image and pixmap. */ | |
2201 | ||
2202 | static void | |
2203 | x_put_x_image (f, ximg, pixmap, width, height) | |
2204 | struct frame *f; | |
2205 | XImagePtr ximg; | |
2206 | Pixmap pixmap; | |
2207 | int width, height; | |
2208 | { | |
2209 | #ifdef HAVE_X_WINDOWS | |
2210 | GC gc; | |
2211 | ||
2212 | xassert (interrupt_input_blocked); | |
2213 | gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL); | |
2214 | XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height); | |
2215 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
2216 | #endif /* HAVE_X_WINDOWS */ | |
2217 | ||
2218 | #ifdef HAVE_NTGUI | |
2219 | #if 0 /* I don't think this is necessary looking at where it is used. */ | |
2220 | HDC hdc = get_frame_dc (f); | |
2221 | SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS); | |
2222 | release_frame_dc (f, hdc); | |
2223 | #endif | |
2224 | #endif /* HAVE_NTGUI */ | |
2225 | ||
edfda783 AR |
2226 | #ifdef HAVE_NS |
2227 | xassert (ximg == pixmap); | |
2228 | ns_retain_object(ximg); | |
2229 | #endif | |
4ef0d4d0 KS |
2230 | } |
2231 | ||
2232 | \f | |
2233 | /*********************************************************************** | |
2234 | File Handling | |
2235 | ***********************************************************************/ | |
2236 | ||
4ef0d4d0 KS |
2237 | static unsigned char *slurp_file P_ ((char *, int *)); |
2238 | ||
2239 | ||
6678dc11 | 2240 | /* Find image file FILE. Look in data-directory/images, then |
022af124 KH |
2241 | x-bitmap-file-path. Value is the encoded full name of the file |
2242 | found, or nil if not found. */ | |
4ef0d4d0 | 2243 | |
c640aa9b | 2244 | Lisp_Object |
4ef0d4d0 KS |
2245 | x_find_image_file (file) |
2246 | Lisp_Object file; | |
2247 | { | |
2248 | Lisp_Object file_found, search_path; | |
2249 | struct gcpro gcpro1, gcpro2; | |
2250 | int fd; | |
2251 | ||
2252 | file_found = Qnil; | |
6678dc11 GM |
2253 | /* TODO I think this should use something like image-load-path |
2254 | instead. Unfortunately, that can contain non-string elements. */ | |
2255 | search_path = Fcons (Fexpand_file_name (build_string ("images"), | |
2256 | Vdata_directory), | |
2257 | Vx_bitmap_file_path); | |
4ef0d4d0 KS |
2258 | GCPRO2 (file_found, search_path); |
2259 | ||
050b82f6 | 2260 | /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */ |
4ef0d4d0 KS |
2261 | fd = openp (search_path, file, Qnil, &file_found, Qnil); |
2262 | ||
2263 | if (fd == -1) | |
2264 | file_found = Qnil; | |
2265 | else | |
022af124 KH |
2266 | { |
2267 | file_found = ENCODE_FILE (file_found); | |
2268 | close (fd); | |
2269 | } | |
4ef0d4d0 KS |
2270 | |
2271 | UNGCPRO; | |
2272 | return file_found; | |
2273 | } | |
2274 | ||
2275 | ||
2276 | /* Read FILE into memory. Value is a pointer to a buffer allocated | |
2277 | with xmalloc holding FILE's contents. Value is null if an error | |
2278 | occurred. *SIZE is set to the size of the file. */ | |
2279 | ||
2280 | static unsigned char * | |
2281 | slurp_file (file, size) | |
2282 | char *file; | |
2283 | int *size; | |
2284 | { | |
2285 | FILE *fp = NULL; | |
2286 | unsigned char *buf = NULL; | |
2287 | struct stat st; | |
2288 | ||
2289 | if (stat (file, &st) == 0 | |
2290 | && (fp = fopen (file, "rb")) != NULL | |
fa8459a3 | 2291 | && (buf = (unsigned char *) xmalloc (st.st_size), |
4ef0d4d0 KS |
2292 | fread (buf, 1, st.st_size, fp) == st.st_size)) |
2293 | { | |
2294 | *size = st.st_size; | |
2295 | fclose (fp); | |
2296 | } | |
2297 | else | |
2298 | { | |
2299 | if (fp) | |
2300 | fclose (fp); | |
2301 | if (buf) | |
2302 | { | |
2303 | xfree (buf); | |
2304 | buf = NULL; | |
2305 | } | |
2306 | } | |
2307 | ||
2308 | return buf; | |
2309 | } | |
2310 | ||
2311 | ||
2312 | \f | |
4ef0d4d0 KS |
2313 | /*********************************************************************** |
2314 | XBM images | |
2315 | ***********************************************************************/ | |
2316 | ||
2317 | static int xbm_scan P_ ((unsigned char **, unsigned char *, char *, int *)); | |
2318 | static int xbm_load P_ ((struct frame *f, struct image *img)); | |
2319 | static int xbm_load_image P_ ((struct frame *f, struct image *img, | |
2320 | unsigned char *, unsigned char *)); | |
2321 | static int xbm_image_p P_ ((Lisp_Object object)); | |
f1f25b99 CY |
2322 | static int xbm_read_bitmap_data P_ ((struct frame *f, |
2323 | unsigned char *, unsigned char *, | |
ce959360 | 2324 | int *, int *, unsigned char **, int)); |
4ef0d4d0 KS |
2325 | static int xbm_file_p P_ ((Lisp_Object)); |
2326 | ||
2327 | ||
2328 | /* Indices of image specification fields in xbm_format, below. */ | |
2329 | ||
2330 | enum xbm_keyword_index | |
2331 | { | |
2332 | XBM_TYPE, | |
2333 | XBM_FILE, | |
2334 | XBM_WIDTH, | |
2335 | XBM_HEIGHT, | |
2336 | XBM_DATA, | |
2337 | XBM_FOREGROUND, | |
2338 | XBM_BACKGROUND, | |
2339 | XBM_ASCENT, | |
2340 | XBM_MARGIN, | |
2341 | XBM_RELIEF, | |
2342 | XBM_ALGORITHM, | |
2343 | XBM_HEURISTIC_MASK, | |
2344 | XBM_MASK, | |
2345 | XBM_LAST | |
2346 | }; | |
2347 | ||
2348 | /* Vector of image_keyword structures describing the format | |
2349 | of valid XBM image specifications. */ | |
2350 | ||
91433552 | 2351 | static const struct image_keyword xbm_format[XBM_LAST] = |
4ef0d4d0 KS |
2352 | { |
2353 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
2354 | {":file", IMAGE_STRING_VALUE, 0}, | |
2355 | {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2356 | {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2357 | {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2358 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2359 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2360 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
2361 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
2362 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
2363 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2364 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2365 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0} | |
2366 | }; | |
2367 | ||
2368 | /* Structure describing the image type XBM. */ | |
2369 | ||
2370 | static struct image_type xbm_type = | |
2371 | { | |
2372 | &Qxbm, | |
2373 | xbm_image_p, | |
2374 | xbm_load, | |
2375 | x_clear_image, | |
2376 | NULL | |
2377 | }; | |
2378 | ||
2379 | /* Tokens returned from xbm_scan. */ | |
2380 | ||
2381 | enum xbm_token | |
2382 | { | |
2383 | XBM_TK_IDENT = 256, | |
2384 | XBM_TK_NUMBER | |
2385 | }; | |
2386 | ||
2387 | ||
2388 | /* Return non-zero if OBJECT is a valid XBM-type image specification. | |
2389 | A valid specification is a list starting with the symbol `image' | |
2390 | The rest of the list is a property list which must contain an | |
2391 | entry `:type xbm.. | |
2392 | ||
2393 | If the specification specifies a file to load, it must contain | |
2394 | an entry `:file FILENAME' where FILENAME is a string. | |
2395 | ||
2396 | If the specification is for a bitmap loaded from memory it must | |
2397 | contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where | |
2398 | WIDTH and HEIGHT are integers > 0. DATA may be: | |
2399 | ||
2400 | 1. a string large enough to hold the bitmap data, i.e. it must | |
2401 | have a size >= (WIDTH + 7) / 8 * HEIGHT | |
2402 | ||
2403 | 2. a bool-vector of size >= WIDTH * HEIGHT | |
2404 | ||
2405 | 3. a vector of strings or bool-vectors, one for each line of the | |
2406 | bitmap. | |
2407 | ||
bbe6f2aa | 2408 | 4. a string containing an in-memory XBM file. WIDTH and HEIGHT |
4ef0d4d0 KS |
2409 | may not be specified in this case because they are defined in the |
2410 | XBM file. | |
2411 | ||
2412 | Both the file and data forms may contain the additional entries | |
2413 | `:background COLOR' and `:foreground COLOR'. If not present, | |
2414 | foreground and background of the frame on which the image is | |
2415 | displayed is used. */ | |
2416 | ||
2417 | static int | |
2418 | xbm_image_p (object) | |
2419 | Lisp_Object object; | |
2420 | { | |
2421 | struct image_keyword kw[XBM_LAST]; | |
2422 | ||
2423 | bcopy (xbm_format, kw, sizeof kw); | |
2424 | if (!parse_image_spec (object, kw, XBM_LAST, Qxbm)) | |
2425 | return 0; | |
2426 | ||
2427 | xassert (EQ (kw[XBM_TYPE].value, Qxbm)); | |
2428 | ||
2429 | if (kw[XBM_FILE].count) | |
2430 | { | |
2431 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count) | |
2432 | return 0; | |
2433 | } | |
2434 | else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value)) | |
2435 | { | |
2436 | /* In-memory XBM file. */ | |
2437 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count) | |
2438 | return 0; | |
2439 | } | |
2440 | else | |
2441 | { | |
2442 | Lisp_Object data; | |
2443 | int width, height; | |
2444 | ||
2445 | /* Entries for `:width', `:height' and `:data' must be present. */ | |
2446 | if (!kw[XBM_WIDTH].count | |
2447 | || !kw[XBM_HEIGHT].count | |
2448 | || !kw[XBM_DATA].count) | |
2449 | return 0; | |
2450 | ||
2451 | data = kw[XBM_DATA].value; | |
2452 | width = XFASTINT (kw[XBM_WIDTH].value); | |
2453 | height = XFASTINT (kw[XBM_HEIGHT].value); | |
2454 | ||
2455 | /* Check type of data, and width and height against contents of | |
2456 | data. */ | |
2457 | if (VECTORP (data)) | |
2458 | { | |
2459 | int i; | |
2460 | ||
2461 | /* Number of elements of the vector must be >= height. */ | |
2462 | if (XVECTOR (data)->size < height) | |
2463 | return 0; | |
2464 | ||
2465 | /* Each string or bool-vector in data must be large enough | |
2466 | for one line of the image. */ | |
2467 | for (i = 0; i < height; ++i) | |
2468 | { | |
2469 | Lisp_Object elt = XVECTOR (data)->contents[i]; | |
2470 | ||
2471 | if (STRINGP (elt)) | |
2472 | { | |
2473 | if (SCHARS (elt) | |
2474 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR) | |
2475 | return 0; | |
2476 | } | |
2477 | else if (BOOL_VECTOR_P (elt)) | |
2478 | { | |
2479 | if (XBOOL_VECTOR (elt)->size < width) | |
2480 | return 0; | |
2481 | } | |
2482 | else | |
2483 | return 0; | |
2484 | } | |
2485 | } | |
2486 | else if (STRINGP (data)) | |
2487 | { | |
2488 | if (SCHARS (data) | |
2489 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height) | |
2490 | return 0; | |
2491 | } | |
2492 | else if (BOOL_VECTOR_P (data)) | |
2493 | { | |
2494 | if (XBOOL_VECTOR (data)->size < width * height) | |
2495 | return 0; | |
2496 | } | |
2497 | else | |
2498 | return 0; | |
2499 | } | |
2500 | ||
2501 | return 1; | |
2502 | } | |
2503 | ||
2504 | ||
2505 | /* Scan a bitmap file. FP is the stream to read from. Value is | |
2506 | either an enumerator from enum xbm_token, or a character for a | |
2507 | single-character token, or 0 at end of file. If scanning an | |
2508 | identifier, store the lexeme of the identifier in SVAL. If | |
2509 | scanning a number, store its value in *IVAL. */ | |
2510 | ||
2511 | static int | |
2512 | xbm_scan (s, end, sval, ival) | |
2513 | unsigned char **s, *end; | |
2514 | char *sval; | |
2515 | int *ival; | |
2516 | { | |
2517 | unsigned int c; | |
2518 | ||
2519 | loop: | |
2520 | ||
2521 | /* Skip white space. */ | |
2522 | while (*s < end && (c = *(*s)++, isspace (c))) | |
2523 | ; | |
2524 | ||
2525 | if (*s >= end) | |
2526 | c = 0; | |
2527 | else if (isdigit (c)) | |
2528 | { | |
2529 | int value = 0, digit; | |
2530 | ||
2531 | if (c == '0' && *s < end) | |
2532 | { | |
2533 | c = *(*s)++; | |
2534 | if (c == 'x' || c == 'X') | |
2535 | { | |
2536 | while (*s < end) | |
2537 | { | |
2538 | c = *(*s)++; | |
2539 | if (isdigit (c)) | |
2540 | digit = c - '0'; | |
2541 | else if (c >= 'a' && c <= 'f') | |
2542 | digit = c - 'a' + 10; | |
2543 | else if (c >= 'A' && c <= 'F') | |
2544 | digit = c - 'A' + 10; | |
2545 | else | |
2546 | break; | |
2547 | value = 16 * value + digit; | |
2548 | } | |
2549 | } | |
2550 | else if (isdigit (c)) | |
2551 | { | |
2552 | value = c - '0'; | |
2553 | while (*s < end | |
2554 | && (c = *(*s)++, isdigit (c))) | |
2555 | value = 8 * value + c - '0'; | |
2556 | } | |
2557 | } | |
2558 | else | |
2559 | { | |
2560 | value = c - '0'; | |
2561 | while (*s < end | |
2562 | && (c = *(*s)++, isdigit (c))) | |
2563 | value = 10 * value + c - '0'; | |
2564 | } | |
2565 | ||
2566 | if (*s < end) | |
2567 | *s = *s - 1; | |
2568 | *ival = value; | |
2569 | c = XBM_TK_NUMBER; | |
2570 | } | |
2571 | else if (isalpha (c) || c == '_') | |
2572 | { | |
2573 | *sval++ = c; | |
2574 | while (*s < end | |
2575 | && (c = *(*s)++, (isalnum (c) || c == '_'))) | |
2576 | *sval++ = c; | |
2577 | *sval = 0; | |
2578 | if (*s < end) | |
2579 | *s = *s - 1; | |
2580 | c = XBM_TK_IDENT; | |
2581 | } | |
2582 | else if (c == '/' && **s == '*') | |
2583 | { | |
2584 | /* C-style comment. */ | |
2585 | ++*s; | |
2586 | while (**s && (**s != '*' || *(*s + 1) != '/')) | |
2587 | ++*s; | |
2588 | if (**s) | |
2589 | { | |
2590 | *s += 2; | |
2591 | goto loop; | |
2592 | } | |
2593 | } | |
2594 | ||
2595 | return c; | |
2596 | } | |
2597 | ||
2598 | #ifdef HAVE_NTGUI | |
2599 | ||
2600 | /* Create a Windows bitmap from X bitmap data. */ | |
2601 | static HBITMAP | |
2602 | w32_create_pixmap_from_bitmap_data (int width, int height, char *data) | |
2603 | { | |
2604 | static unsigned char swap_nibble[16] | |
2605 | = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */ | |
2606 | 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */ | |
2607 | 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */ | |
2608 | 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */ | |
2609 | int i, j, w1, w2; | |
2610 | unsigned char *bits, *p; | |
2611 | HBITMAP bmp; | |
2612 | ||
2613 | w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */ | |
2614 | w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */ | |
2615 | bits = (unsigned char *) alloca (height * w2); | |
2616 | bzero (bits, height * w2); | |
2617 | for (i = 0; i < height; i++) | |
2618 | { | |
2619 | p = bits + i*w2; | |
2620 | for (j = 0; j < w1; j++) | |
2621 | { | |
2622 | /* Bitswap XBM bytes to match how Windows does things. */ | |
2623 | unsigned char c = *data++; | |
2624 | *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4) | |
2625 | | (swap_nibble[(c>>4) & 0xf])); | |
2626 | } | |
2627 | } | |
2628 | bmp = CreateBitmap (width, height, 1, 1, (char *) bits); | |
2629 | ||
2630 | return bmp; | |
2631 | } | |
2632 | ||
3046a84b JB |
2633 | static void |
2634 | convert_mono_to_color_image (f, img, foreground, background) | |
4ef0d4d0 KS |
2635 | struct frame *f; |
2636 | struct image *img; | |
2637 | COLORREF foreground, background; | |
2638 | { | |
2639 | HDC hdc, old_img_dc, new_img_dc; | |
2640 | HGDIOBJ old_prev, new_prev; | |
2641 | HBITMAP new_pixmap; | |
2642 | ||
2643 | hdc = get_frame_dc (f); | |
2644 | old_img_dc = CreateCompatibleDC (hdc); | |
2645 | new_img_dc = CreateCompatibleDC (hdc); | |
2646 | new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height); | |
2647 | release_frame_dc (f, hdc); | |
2648 | old_prev = SelectObject (old_img_dc, img->pixmap); | |
2649 | new_prev = SelectObject (new_img_dc, new_pixmap); | |
44b1dc2e JR |
2650 | /* Windows convention for mono bitmaps is black = background, |
2651 | white = foreground. */ | |
2386b1f1 JR |
2652 | SetTextColor (new_img_dc, background); |
2653 | SetBkColor (new_img_dc, foreground); | |
4ef0d4d0 KS |
2654 | |
2655 | BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc, | |
2656 | 0, 0, SRCCOPY); | |
2657 | ||
2658 | SelectObject (old_img_dc, old_prev); | |
2659 | SelectObject (new_img_dc, new_prev); | |
2660 | DeleteDC (old_img_dc); | |
2661 | DeleteDC (new_img_dc); | |
2662 | DeleteObject (img->pixmap); | |
2663 | if (new_pixmap == 0) | |
2664 | fprintf (stderr, "Failed to convert image to color.\n"); | |
2665 | else | |
2666 | img->pixmap = new_pixmap; | |
2667 | } | |
2668 | ||
2669 | #define XBM_BIT_SHUFFLE(b) (~(b)) | |
2670 | ||
2671 | #else | |
2672 | ||
2673 | #define XBM_BIT_SHUFFLE(b) (b) | |
2674 | ||
2675 | #endif /* HAVE_NTGUI */ | |
2676 | ||
2677 | ||
2678 | static void | |
3046a84b | 2679 | Create_Pixmap_From_Bitmap_Data (f, img, data, fg, bg, non_default_colors) |
4ef0d4d0 KS |
2680 | struct frame *f; |
2681 | struct image *img; | |
2682 | char *data; | |
2683 | RGB_PIXEL_COLOR fg, bg; | |
2684 | int non_default_colors; | |
2685 | { | |
2686 | #ifdef HAVE_NTGUI | |
2687 | img->pixmap | |
2688 | = w32_create_pixmap_from_bitmap_data (img->width, img->height, data); | |
2689 | ||
2690 | /* If colors were specified, transfer the bitmap to a color one. */ | |
2691 | if (non_default_colors) | |
2692 | convert_mono_to_color_image (f, img, fg, bg); | |
edfda783 AR |
2693 | |
2694 | #elif defined (HAVE_NS) | |
2695 | img->pixmap = ns_image_from_XBM(data, img->width, img->height); | |
2696 | ||
4ef0d4d0 KS |
2697 | #else |
2698 | img->pixmap | |
2699 | = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f), | |
2700 | FRAME_X_WINDOW (f), | |
2701 | data, | |
2702 | img->width, img->height, | |
2703 | fg, bg, | |
2704 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))); | |
edfda783 | 2705 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
2706 | } |
2707 | ||
2708 | ||
2709 | ||
2710 | /* Replacement for XReadBitmapFileData which isn't available under old | |
2711 | X versions. CONTENTS is a pointer to a buffer to parse; END is the | |
2712 | buffer's end. Set *WIDTH and *HEIGHT to the width and height of | |
2713 | the image. Return in *DATA the bitmap data allocated with xmalloc. | |
2714 | Value is non-zero if successful. DATA null means just test if | |
ce959360 CY |
2715 | CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR |
2716 | is non-zero, inhibit the call to image_error when the image size is | |
2717 | invalid (the bitmap remains unread). */ | |
4ef0d4d0 KS |
2718 | |
2719 | static int | |
ce959360 | 2720 | xbm_read_bitmap_data (f, contents, end, width, height, data, inhibit_image_error) |
f1f25b99 | 2721 | struct frame *f; |
4ef0d4d0 KS |
2722 | unsigned char *contents, *end; |
2723 | int *width, *height; | |
2724 | unsigned char **data; | |
ce959360 | 2725 | int inhibit_image_error; |
4ef0d4d0 KS |
2726 | { |
2727 | unsigned char *s = contents; | |
2728 | char buffer[BUFSIZ]; | |
2729 | int padding_p = 0; | |
2730 | int v10 = 0; | |
2731 | int bytes_per_line, i, nbytes; | |
2732 | unsigned char *p; | |
2733 | int value; | |
2734 | int LA1; | |
2735 | ||
2736 | #define match() \ | |
2737 | LA1 = xbm_scan (&s, end, buffer, &value) | |
2738 | ||
2739 | #define expect(TOKEN) \ | |
2740 | if (LA1 != (TOKEN)) \ | |
2741 | goto failure; \ | |
2742 | else \ | |
2743 | match () | |
2744 | ||
2745 | #define expect_ident(IDENT) \ | |
2746 | if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \ | |
2747 | match (); \ | |
2748 | else \ | |
2749 | goto failure | |
2750 | ||
2751 | *width = *height = -1; | |
2752 | if (data) | |
2753 | *data = NULL; | |
2754 | LA1 = xbm_scan (&s, end, buffer, &value); | |
2755 | ||
2756 | /* Parse defines for width, height and hot-spots. */ | |
2757 | while (LA1 == '#') | |
2758 | { | |
2759 | match (); | |
2760 | expect_ident ("define"); | |
2761 | expect (XBM_TK_IDENT); | |
2762 | ||
431feaf6 | 2763 | if (LA1 == XBM_TK_NUMBER) |
4ef0d4d0 KS |
2764 | { |
2765 | char *p = strrchr (buffer, '_'); | |
2766 | p = p ? p + 1 : buffer; | |
2767 | if (strcmp (p, "width") == 0) | |
2768 | *width = value; | |
2769 | else if (strcmp (p, "height") == 0) | |
2770 | *height = value; | |
2771 | } | |
2772 | expect (XBM_TK_NUMBER); | |
2773 | } | |
2774 | ||
f1f25b99 | 2775 | if (!check_image_size (f, *width, *height)) |
10ea2b82 | 2776 | { |
ce959360 CY |
2777 | if (!inhibit_image_error) |
2778 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
10ea2b82 JR |
2779 | goto failure; |
2780 | } | |
4ef0d4d0 KS |
2781 | else if (data == NULL) |
2782 | goto success; | |
2783 | ||
2784 | /* Parse bits. Must start with `static'. */ | |
2785 | expect_ident ("static"); | |
2786 | if (LA1 == XBM_TK_IDENT) | |
2787 | { | |
2788 | if (strcmp (buffer, "unsigned") == 0) | |
2789 | { | |
2790 | match (); | |
2791 | expect_ident ("char"); | |
2792 | } | |
2793 | else if (strcmp (buffer, "short") == 0) | |
2794 | { | |
2795 | match (); | |
2796 | v10 = 1; | |
2797 | if (*width % 16 && *width % 16 < 9) | |
2798 | padding_p = 1; | |
2799 | } | |
2800 | else if (strcmp (buffer, "char") == 0) | |
2801 | match (); | |
2802 | else | |
2803 | goto failure; | |
2804 | } | |
2805 | else | |
2806 | goto failure; | |
2807 | ||
2808 | expect (XBM_TK_IDENT); | |
2809 | expect ('['); | |
2810 | expect (']'); | |
2811 | expect ('='); | |
2812 | expect ('{'); | |
2813 | ||
2814 | bytes_per_line = (*width + 7) / 8 + padding_p; | |
2815 | nbytes = bytes_per_line * *height; | |
fa8459a3 | 2816 | p = *data = (unsigned char *) xmalloc (nbytes); |
4ef0d4d0 KS |
2817 | |
2818 | if (v10) | |
2819 | { | |
2820 | for (i = 0; i < nbytes; i += 2) | |
2821 | { | |
2822 | int val = value; | |
2823 | expect (XBM_TK_NUMBER); | |
2824 | ||
2825 | *p++ = XBM_BIT_SHUFFLE (val); | |
2826 | if (!padding_p || ((i + 2) % bytes_per_line)) | |
2827 | *p++ = XBM_BIT_SHUFFLE (value >> 8); | |
2828 | ||
2829 | if (LA1 == ',' || LA1 == '}') | |
2830 | match (); | |
2831 | else | |
2832 | goto failure; | |
2833 | } | |
2834 | } | |
2835 | else | |
2836 | { | |
2837 | for (i = 0; i < nbytes; ++i) | |
2838 | { | |
2839 | int val = value; | |
2840 | expect (XBM_TK_NUMBER); | |
2841 | ||
2842 | *p++ = XBM_BIT_SHUFFLE (val); | |
2843 | ||
2844 | if (LA1 == ',' || LA1 == '}') | |
2845 | match (); | |
2846 | else | |
2847 | goto failure; | |
2848 | } | |
2849 | } | |
2850 | ||
2851 | success: | |
2852 | return 1; | |
2853 | ||
2854 | failure: | |
2855 | ||
2856 | if (data && *data) | |
2857 | { | |
2858 | xfree (*data); | |
2859 | *data = NULL; | |
2860 | } | |
2861 | return 0; | |
2862 | ||
2863 | #undef match | |
2864 | #undef expect | |
2865 | #undef expect_ident | |
2866 | } | |
2867 | ||
2868 | ||
2869 | /* Load XBM image IMG which will be displayed on frame F from buffer | |
2870 | CONTENTS. END is the end of the buffer. Value is non-zero if | |
2871 | successful. */ | |
2872 | ||
2873 | static int | |
2874 | xbm_load_image (f, img, contents, end) | |
2875 | struct frame *f; | |
2876 | struct image *img; | |
2877 | unsigned char *contents, *end; | |
2878 | { | |
2879 | int rc; | |
2880 | unsigned char *data; | |
2881 | int success_p = 0; | |
2882 | ||
ce959360 CY |
2883 | rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height, |
2884 | &data, 0); | |
4ef0d4d0 KS |
2885 | if (rc) |
2886 | { | |
2887 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2888 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
2889 | int non_default_colors = 0; | |
2890 | Lisp_Object value; | |
2891 | ||
2892 | xassert (img->width > 0 && img->height > 0); | |
2893 | ||
2894 | /* Get foreground and background colors, maybe allocate colors. */ | |
2895 | value = image_spec_value (img->spec, QCforeground, NULL); | |
2896 | if (!NILP (value)) | |
2897 | { | |
2898 | foreground = x_alloc_image_color (f, img, value, foreground); | |
2899 | non_default_colors = 1; | |
2900 | } | |
2901 | value = image_spec_value (img->spec, QCbackground, NULL); | |
2902 | if (!NILP (value)) | |
2903 | { | |
2904 | background = x_alloc_image_color (f, img, value, background); | |
2905 | img->background = background; | |
2906 | img->background_valid = 1; | |
2907 | non_default_colors = 1; | |
2908 | } | |
2909 | ||
1f026899 | 2910 | Create_Pixmap_From_Bitmap_Data (f, img, data, |
4ef0d4d0 KS |
2911 | foreground, background, |
2912 | non_default_colors); | |
2913 | xfree (data); | |
2914 | ||
2915 | if (img->pixmap == NO_PIXMAP) | |
2916 | { | |
2917 | x_clear_image (f, img); | |
2918 | image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil); | |
2919 | } | |
2920 | else | |
2921 | success_p = 1; | |
2922 | } | |
2923 | else | |
2924 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
2925 | ||
2926 | return success_p; | |
2927 | } | |
2928 | ||
2929 | ||
2930 | /* Value is non-zero if DATA looks like an in-memory XBM file. */ | |
2931 | ||
2932 | static int | |
2933 | xbm_file_p (data) | |
2934 | Lisp_Object data; | |
2935 | { | |
2936 | int w, h; | |
2937 | return (STRINGP (data) | |
f1f25b99 | 2938 | && xbm_read_bitmap_data (NULL, SDATA (data), |
ce959360 CY |
2939 | (SDATA (data) + SBYTES (data)), |
2940 | &w, &h, NULL, 1)); | |
4ef0d4d0 KS |
2941 | } |
2942 | ||
2943 | ||
2944 | /* Fill image IMG which is used on frame F with pixmap data. Value is | |
2945 | non-zero if successful. */ | |
2946 | ||
2947 | static int | |
2948 | xbm_load (f, img) | |
2949 | struct frame *f; | |
2950 | struct image *img; | |
2951 | { | |
2952 | int success_p = 0; | |
2953 | Lisp_Object file_name; | |
2954 | ||
2955 | xassert (xbm_image_p (img->spec)); | |
2956 | ||
2957 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
2958 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
2959 | if (STRINGP (file_name)) | |
2960 | { | |
2961 | Lisp_Object file; | |
2962 | unsigned char *contents; | |
2963 | int size; | |
2964 | struct gcpro gcpro1; | |
2965 | ||
2966 | file = x_find_image_file (file_name); | |
2967 | GCPRO1 (file); | |
2968 | if (!STRINGP (file)) | |
2969 | { | |
2970 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
2971 | UNGCPRO; | |
2972 | return 0; | |
2973 | } | |
2974 | ||
2975 | contents = slurp_file (SDATA (file), &size); | |
2976 | if (contents == NULL) | |
2977 | { | |
2978 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
2979 | UNGCPRO; | |
2980 | return 0; | |
2981 | } | |
2982 | ||
2983 | success_p = xbm_load_image (f, img, contents, contents + size); | |
2984 | UNGCPRO; | |
2985 | } | |
2986 | else | |
2987 | { | |
2988 | struct image_keyword fmt[XBM_LAST]; | |
2989 | Lisp_Object data; | |
2990 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2991 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
2992 | int non_default_colors = 0; | |
2993 | char *bits; | |
2994 | int parsed_p; | |
2995 | int in_memory_file_p = 0; | |
2996 | ||
2997 | /* See if data looks like an in-memory XBM file. */ | |
2998 | data = image_spec_value (img->spec, QCdata, NULL); | |
2999 | in_memory_file_p = xbm_file_p (data); | |
3000 | ||
3001 | /* Parse the image specification. */ | |
3002 | bcopy (xbm_format, fmt, sizeof fmt); | |
3003 | parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm); | |
3004 | xassert (parsed_p); | |
3005 | ||
3006 | /* Get specified width, and height. */ | |
3007 | if (!in_memory_file_p) | |
3008 | { | |
3009 | img->width = XFASTINT (fmt[XBM_WIDTH].value); | |
3010 | img->height = XFASTINT (fmt[XBM_HEIGHT].value); | |
3011 | xassert (img->width > 0 && img->height > 0); | |
3012 | } | |
3013 | ||
3014 | /* Get foreground and background colors, maybe allocate colors. */ | |
3015 | if (fmt[XBM_FOREGROUND].count | |
3016 | && STRINGP (fmt[XBM_FOREGROUND].value)) | |
3017 | { | |
3018 | foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value, | |
3019 | foreground); | |
3020 | non_default_colors = 1; | |
3021 | } | |
3022 | ||
3023 | if (fmt[XBM_BACKGROUND].count | |
3024 | && STRINGP (fmt[XBM_BACKGROUND].value)) | |
3025 | { | |
3026 | background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value, | |
3027 | background); | |
3028 | non_default_colors = 1; | |
3029 | } | |
3030 | ||
3031 | if (in_memory_file_p) | |
3032 | success_p = xbm_load_image (f, img, SDATA (data), | |
3033 | (SDATA (data) | |
3034 | + SBYTES (data))); | |
3035 | else | |
3036 | { | |
3037 | if (VECTORP (data)) | |
3038 | { | |
3039 | int i; | |
3040 | char *p; | |
3041 | int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR; | |
3042 | ||
3043 | p = bits = (char *) alloca (nbytes * img->height); | |
3044 | for (i = 0; i < img->height; ++i, p += nbytes) | |
3045 | { | |
3046 | Lisp_Object line = XVECTOR (data)->contents[i]; | |
3047 | if (STRINGP (line)) | |
3048 | bcopy (SDATA (line), p, nbytes); | |
3049 | else | |
3050 | bcopy (XBOOL_VECTOR (line)->data, p, nbytes); | |
3051 | } | |
3052 | } | |
3053 | else if (STRINGP (data)) | |
3054 | bits = SDATA (data); | |
3055 | else | |
3056 | bits = XBOOL_VECTOR (data)->data; | |
3057 | ||
44b1dc2e JR |
3058 | #ifdef WINDOWSNT |
3059 | { | |
3060 | char *invertedBits; | |
3061 | int nbytes, i; | |
3062 | /* Windows mono bitmaps are reversed compared with X. */ | |
3063 | invertedBits = bits; | |
285d07e2 | 3064 | nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR |
44b1dc2e JR |
3065 | * img->height; |
3066 | bits = (char *) alloca(nbytes); | |
3067 | for (i = 0; i < nbytes; i++) | |
3068 | bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]); | |
3069 | } | |
3070 | #endif | |
4ef0d4d0 KS |
3071 | /* Create the pixmap. */ |
3072 | ||
3073 | Create_Pixmap_From_Bitmap_Data (f, img, bits, | |
3074 | foreground, background, | |
3075 | non_default_colors); | |
3076 | if (img->pixmap) | |
3077 | success_p = 1; | |
3078 | else | |
3079 | { | |
3080 | image_error ("Unable to create pixmap for XBM image `%s'", | |
3081 | img->spec, Qnil); | |
3082 | x_clear_image (f, img); | |
3083 | } | |
3084 | } | |
3085 | } | |
3086 | ||
3087 | return success_p; | |
3088 | } | |
3089 | ||
3090 | ||
3091 | \f | |
3092 | /*********************************************************************** | |
3093 | XPM images | |
3094 | ***********************************************************************/ | |
3095 | ||
9e2a2647 | 3096 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 KS |
3097 | |
3098 | static int xpm_image_p P_ ((Lisp_Object object)); | |
3099 | static int xpm_load P_ ((struct frame *f, struct image *img)); | |
3100 | static int xpm_valid_color_symbols_p P_ ((Lisp_Object)); | |
3101 | ||
9e2a2647 | 3102 | #endif /* HAVE_XPM || HAVE_NS */ |
ea1aaa6f ST |
3103 | |
3104 | #ifdef HAVE_XPM | |
4ef0d4d0 KS |
3105 | #ifdef HAVE_NTGUI |
3106 | /* Indicate to xpm.h that we don't have Xlib. */ | |
3107 | #define FOR_MSW | |
3108 | /* simx.h in xpm defines XColor and XImage differently than Emacs. */ | |
279d3293 | 3109 | /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */ |
4ef0d4d0 KS |
3110 | #define XColor xpm_XColor |
3111 | #define XImage xpm_XImage | |
279d3293 | 3112 | #define Display xpm_Display |
4ef0d4d0 KS |
3113 | #define PIXEL_ALREADY_TYPEDEFED |
3114 | #include "X11/xpm.h" | |
3115 | #undef FOR_MSW | |
3116 | #undef XColor | |
3117 | #undef XImage | |
279d3293 | 3118 | #undef Display |
4ef0d4d0 KS |
3119 | #undef PIXEL_ALREADY_TYPEDEFED |
3120 | #else | |
3121 | #include "X11/xpm.h" | |
3122 | #endif /* HAVE_NTGUI */ | |
ea1aaa6f | 3123 | #endif /* HAVE_XPM */ |
4ef0d4d0 | 3124 | |
9e2a2647 | 3125 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 KS |
3126 | /* The symbol `xpm' identifying XPM-format images. */ |
3127 | ||
3128 | Lisp_Object Qxpm; | |
3129 | ||
3130 | /* Indices of image specification fields in xpm_format, below. */ | |
3131 | ||
3132 | enum xpm_keyword_index | |
3133 | { | |
3134 | XPM_TYPE, | |
3135 | XPM_FILE, | |
3136 | XPM_DATA, | |
3137 | XPM_ASCENT, | |
3138 | XPM_MARGIN, | |
3139 | XPM_RELIEF, | |
3140 | XPM_ALGORITHM, | |
3141 | XPM_HEURISTIC_MASK, | |
3142 | XPM_MASK, | |
3143 | XPM_COLOR_SYMBOLS, | |
3144 | XPM_BACKGROUND, | |
3145 | XPM_LAST | |
3146 | }; | |
3147 | ||
3148 | /* Vector of image_keyword structures describing the format | |
3149 | of valid XPM image specifications. */ | |
3150 | ||
91433552 | 3151 | static const struct image_keyword xpm_format[XPM_LAST] = |
4ef0d4d0 KS |
3152 | { |
3153 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
3154 | {":file", IMAGE_STRING_VALUE, 0}, | |
3155 | {":data", IMAGE_STRING_VALUE, 0}, | |
3156 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
3157 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
3158 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
3159 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3160 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3161 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3162 | {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3163 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
3164 | }; | |
3165 | ||
3166 | /* Structure describing the image type XPM. */ | |
3167 | ||
3168 | static struct image_type xpm_type = | |
3169 | { | |
3170 | &Qxpm, | |
3171 | xpm_image_p, | |
3172 | xpm_load, | |
3173 | x_clear_image, | |
3174 | NULL | |
3175 | }; | |
3176 | ||
3177 | #ifdef HAVE_X_WINDOWS | |
3178 | ||
3179 | /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation | |
3180 | functions for allocating image colors. Our own functions handle | |
3181 | color allocation failures more gracefully than the ones on the XPM | |
3182 | lib. */ | |
3183 | ||
3184 | #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure | |
3185 | #define ALLOC_XPM_COLORS | |
3186 | #endif | |
3187 | #endif /* HAVE_X_WINDOWS */ | |
3188 | ||
3189 | #ifdef ALLOC_XPM_COLORS | |
3190 | ||
3191 | static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *)); | |
3192 | static void xpm_free_color_cache P_ ((void)); | |
3193 | static int xpm_lookup_color P_ ((struct frame *, char *, XColor *)); | |
3194 | static int xpm_color_bucket P_ ((char *)); | |
3195 | static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *, | |
3196 | XColor *, int)); | |
3197 | ||
3198 | /* An entry in a hash table used to cache color definitions of named | |
3199 | colors. This cache is necessary to speed up XPM image loading in | |
3200 | case we do color allocations ourselves. Without it, we would need | |
3201 | a call to XParseColor per pixel in the image. */ | |
3202 | ||
3203 | struct xpm_cached_color | |
3204 | { | |
3205 | /* Next in collision chain. */ | |
3206 | struct xpm_cached_color *next; | |
3207 | ||
3208 | /* Color definition (RGB and pixel color). */ | |
3209 | XColor color; | |
3210 | ||
3211 | /* Color name. */ | |
3212 | char name[1]; | |
3213 | }; | |
3214 | ||
3215 | /* The hash table used for the color cache, and its bucket vector | |
3216 | size. */ | |
3217 | ||
3218 | #define XPM_COLOR_CACHE_BUCKETS 1001 | |
3219 | struct xpm_cached_color **xpm_color_cache; | |
3220 | ||
3221 | /* Initialize the color cache. */ | |
3222 | ||
3223 | static void | |
3224 | xpm_init_color_cache (f, attrs) | |
3225 | struct frame *f; | |
3226 | XpmAttributes *attrs; | |
3227 | { | |
3228 | size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache; | |
3229 | xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes); | |
3230 | memset (xpm_color_cache, 0, nbytes); | |
3231 | init_color_table (); | |
3232 | ||
3233 | if (attrs->valuemask & XpmColorSymbols) | |
3234 | { | |
3235 | int i; | |
3236 | XColor color; | |
3237 | ||
3238 | for (i = 0; i < attrs->numsymbols; ++i) | |
3239 | if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3240 | attrs->colorsymbols[i].value, &color)) | |
3241 | { | |
3242 | color.pixel = lookup_rgb_color (f, color.red, color.green, | |
3243 | color.blue); | |
3244 | xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1); | |
3245 | } | |
3246 | } | |
3247 | } | |
3248 | ||
3249 | /* Free the color cache. */ | |
3250 | ||
3251 | static void | |
3252 | xpm_free_color_cache () | |
3253 | { | |
3254 | struct xpm_cached_color *p, *next; | |
3255 | int i; | |
3256 | ||
3257 | for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i) | |
3258 | for (p = xpm_color_cache[i]; p; p = next) | |
3259 | { | |
3260 | next = p->next; | |
3261 | xfree (p); | |
3262 | } | |
3263 | ||
3264 | xfree (xpm_color_cache); | |
3265 | xpm_color_cache = NULL; | |
3266 | free_color_table (); | |
3267 | } | |
3268 | ||
3269 | /* Return the bucket index for color named COLOR_NAME in the color | |
3270 | cache. */ | |
3271 | ||
3272 | static int | |
3273 | xpm_color_bucket (color_name) | |
3274 | char *color_name; | |
3275 | { | |
3276 | unsigned h = 0; | |
3277 | char *s; | |
3278 | ||
3279 | for (s = color_name; *s; ++s) | |
3280 | h = (h << 2) ^ *s; | |
3281 | return h %= XPM_COLOR_CACHE_BUCKETS; | |
3282 | } | |
3283 | ||
3284 | ||
3285 | /* On frame F, cache values COLOR for color with name COLOR_NAME. | |
3286 | BUCKET, if >= 0, is a precomputed bucket index. Value is the cache | |
3287 | entry added. */ | |
3288 | ||
3289 | static struct xpm_cached_color * | |
3290 | xpm_cache_color (f, color_name, color, bucket) | |
3291 | struct frame *f; | |
3292 | char *color_name; | |
3293 | XColor *color; | |
3294 | int bucket; | |
3295 | { | |
3296 | size_t nbytes; | |
3297 | struct xpm_cached_color *p; | |
3298 | ||
3299 | if (bucket < 0) | |
3300 | bucket = xpm_color_bucket (color_name); | |
3301 | ||
3302 | nbytes = sizeof *p + strlen (color_name); | |
3303 | p = (struct xpm_cached_color *) xmalloc (nbytes); | |
3304 | strcpy (p->name, color_name); | |
3305 | p->color = *color; | |
3306 | p->next = xpm_color_cache[bucket]; | |
3307 | xpm_color_cache[bucket] = p; | |
3308 | return p; | |
3309 | } | |
3310 | ||
3311 | /* Look up color COLOR_NAME for frame F in the color cache. If found, | |
3312 | return the cached definition in *COLOR. Otherwise, make a new | |
3313 | entry in the cache and allocate the color. Value is zero if color | |
3314 | allocation failed. */ | |
3315 | ||
3316 | static int | |
3317 | xpm_lookup_color (f, color_name, color) | |
3318 | struct frame *f; | |
3319 | char *color_name; | |
3320 | XColor *color; | |
3321 | { | |
3322 | struct xpm_cached_color *p; | |
3323 | int h = xpm_color_bucket (color_name); | |
3324 | ||
3325 | for (p = xpm_color_cache[h]; p; p = p->next) | |
3326 | if (strcmp (p->name, color_name) == 0) | |
3327 | break; | |
3328 | ||
3329 | if (p != NULL) | |
3330 | *color = p->color; | |
3331 | else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3332 | color_name, color)) | |
3333 | { | |
3334 | color->pixel = lookup_rgb_color (f, color->red, color->green, | |
3335 | color->blue); | |
3336 | p = xpm_cache_color (f, color_name, color, h); | |
3337 | } | |
3338 | /* You get `opaque' at least from ImageMagick converting pbm to xpm | |
3339 | with transparency, and it's useful. */ | |
3340 | else if (strcmp ("opaque", color_name) == 0) | |
3341 | { | |
3342 | bzero (color, sizeof (XColor)); /* Is this necessary/correct? */ | |
3343 | color->pixel = FRAME_FOREGROUND_PIXEL (f); | |
3344 | p = xpm_cache_color (f, color_name, color, h); | |
3345 | } | |
3346 | ||
3347 | return p != NULL; | |
3348 | } | |
3349 | ||
3350 | ||
3351 | /* Callback for allocating color COLOR_NAME. Called from the XPM lib. | |
3352 | CLOSURE is a pointer to the frame on which we allocate the | |
3353 | color. Return in *COLOR the allocated color. Value is non-zero | |
3354 | if successful. */ | |
3355 | ||
3356 | static int | |
3357 | xpm_alloc_color (dpy, cmap, color_name, color, closure) | |
3358 | Display *dpy; | |
3359 | Colormap cmap; | |
3360 | char *color_name; | |
3361 | XColor *color; | |
3362 | void *closure; | |
3363 | { | |
3364 | return xpm_lookup_color ((struct frame *) closure, color_name, color); | |
3365 | } | |
3366 | ||
3367 | ||
3368 | /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE | |
3369 | is a pointer to the frame on which we allocate the color. Value is | |
3370 | non-zero if successful. */ | |
3371 | ||
3372 | static int | |
3373 | xpm_free_colors (dpy, cmap, pixels, npixels, closure) | |
3374 | Display *dpy; | |
3375 | Colormap cmap; | |
3376 | Pixel *pixels; | |
3377 | int npixels; | |
3378 | void *closure; | |
3379 | { | |
3380 | return 1; | |
3381 | } | |
3382 | ||
3383 | #endif /* ALLOC_XPM_COLORS */ | |
3384 | ||
3385 | ||
3386 | #ifdef HAVE_NTGUI | |
3387 | ||
3388 | /* XPM library details. */ | |
3389 | ||
3390 | DEF_IMGLIB_FN (XpmFreeAttributes); | |
3391 | DEF_IMGLIB_FN (XpmCreateImageFromBuffer); | |
3392 | DEF_IMGLIB_FN (XpmReadFileToImage); | |
3393 | DEF_IMGLIB_FN (XImageFree); | |
3394 | ||
4ef0d4d0 | 3395 | static int |
0855eb52 | 3396 | init_xpm_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
3397 | { |
3398 | HMODULE library; | |
3399 | ||
0855eb52 | 3400 | if (!(library = w32_delayed_load (libraries, Qxpm))) |
4ef0d4d0 KS |
3401 | return 0; |
3402 | ||
3403 | LOAD_IMGLIB_FN (library, XpmFreeAttributes); | |
3404 | LOAD_IMGLIB_FN (library, XpmCreateImageFromBuffer); | |
3405 | LOAD_IMGLIB_FN (library, XpmReadFileToImage); | |
3406 | LOAD_IMGLIB_FN (library, XImageFree); | |
3407 | return 1; | |
3408 | } | |
3409 | ||
3410 | #endif /* HAVE_NTGUI */ | |
3411 | ||
3412 | ||
3413 | /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list | |
3414 | for XPM images. Such a list must consist of conses whose car and | |
3415 | cdr are strings. */ | |
3416 | ||
3417 | static int | |
3418 | xpm_valid_color_symbols_p (color_symbols) | |
3419 | Lisp_Object color_symbols; | |
3420 | { | |
3421 | while (CONSP (color_symbols)) | |
3422 | { | |
3423 | Lisp_Object sym = XCAR (color_symbols); | |
3424 | if (!CONSP (sym) | |
3425 | || !STRINGP (XCAR (sym)) | |
3426 | || !STRINGP (XCDR (sym))) | |
3427 | break; | |
3428 | color_symbols = XCDR (color_symbols); | |
3429 | } | |
3430 | ||
3431 | return NILP (color_symbols); | |
3432 | } | |
3433 | ||
3434 | ||
3435 | /* Value is non-zero if OBJECT is a valid XPM image specification. */ | |
3436 | ||
3437 | static int | |
3438 | xpm_image_p (object) | |
3439 | Lisp_Object object; | |
3440 | { | |
3441 | struct image_keyword fmt[XPM_LAST]; | |
3442 | bcopy (xpm_format, fmt, sizeof fmt); | |
3443 | return (parse_image_spec (object, fmt, XPM_LAST, Qxpm) | |
3444 | /* Either `:file' or `:data' must be present. */ | |
3445 | && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1 | |
3446 | /* Either no `:color-symbols' or it's a list of conses | |
3447 | whose car and cdr are strings. */ | |
3448 | && (fmt[XPM_COLOR_SYMBOLS].count == 0 | |
3449 | || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))); | |
3450 | } | |
3451 | ||
9e2a2647 | 3452 | #endif /* HAVE_XPM || HAVE_NS */ |
4ef0d4d0 | 3453 | |
786a43d6 CY |
3454 | #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) |
3455 | int | |
3456 | x_create_bitmap_from_xpm_data (f, bits) | |
3457 | struct frame *f; | |
3458 | char **bits; | |
3459 | { | |
3460 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
3461 | int id, rc; | |
3462 | XpmAttributes attrs; | |
3463 | Pixmap bitmap, mask; | |
3464 | ||
3465 | bzero (&attrs, sizeof attrs); | |
3466 | ||
54188d8f CY |
3467 | attrs.visual = FRAME_X_VISUAL (f); |
3468 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3469 | attrs.valuemask |= XpmVisual; | |
3470 | attrs.valuemask |= XpmColormap; | |
3471 | ||
786a43d6 CY |
3472 | rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
3473 | bits, &bitmap, &mask, &attrs); | |
3474 | if (rc != XpmSuccess) | |
fe45ad15 CY |
3475 | { |
3476 | XpmFreeAttributes (&attrs); | |
3477 | return -1; | |
3478 | } | |
786a43d6 CY |
3479 | |
3480 | id = x_allocate_bitmap_record (f); | |
786a43d6 CY |
3481 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; |
3482 | dpyinfo->bitmaps[id - 1].have_mask = 1; | |
3483 | dpyinfo->bitmaps[id - 1].mask = mask; | |
3484 | dpyinfo->bitmaps[id - 1].file = NULL; | |
3485 | dpyinfo->bitmaps[id - 1].height = attrs.height; | |
3486 | dpyinfo->bitmaps[id - 1].width = attrs.width; | |
3487 | dpyinfo->bitmaps[id - 1].depth = attrs.depth; | |
3488 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
3489 | ||
3490 | XpmFreeAttributes (&attrs); | |
3491 | return id; | |
3492 | } | |
0d876a14 | 3493 | #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */ |
786a43d6 | 3494 | |
4ef0d4d0 KS |
3495 | /* Load image IMG which will be displayed on frame F. Value is |
3496 | non-zero if successful. */ | |
3497 | ||
ea1aaa6f ST |
3498 | #ifdef HAVE_XPM |
3499 | ||
4ef0d4d0 KS |
3500 | static int |
3501 | xpm_load (f, img) | |
3502 | struct frame *f; | |
3503 | struct image *img; | |
3504 | { | |
3505 | int rc; | |
3506 | XpmAttributes attrs; | |
3507 | Lisp_Object specified_file, color_symbols; | |
3508 | #ifdef HAVE_NTGUI | |
3509 | HDC hdc; | |
3510 | xpm_XImage * xpm_image = NULL, * xpm_mask = NULL; | |
3511 | #endif /* HAVE_NTGUI */ | |
3512 | ||
3513 | /* Configure the XPM lib. Use the visual of frame F. Allocate | |
3514 | close colors. Return colors allocated. */ | |
3515 | bzero (&attrs, sizeof attrs); | |
3516 | ||
3517 | #ifndef HAVE_NTGUI | |
3518 | attrs.visual = FRAME_X_VISUAL (f); | |
3519 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3520 | attrs.valuemask |= XpmVisual; | |
3521 | attrs.valuemask |= XpmColormap; | |
3522 | #endif /* HAVE_NTGUI */ | |
3523 | ||
3524 | #ifdef ALLOC_XPM_COLORS | |
3525 | /* Allocate colors with our own functions which handle | |
3526 | failing color allocation more gracefully. */ | |
3527 | attrs.color_closure = f; | |
3528 | attrs.alloc_color = xpm_alloc_color; | |
3529 | attrs.free_colors = xpm_free_colors; | |
3530 | attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure; | |
3531 | #else /* not ALLOC_XPM_COLORS */ | |
3532 | /* Let the XPM lib allocate colors. */ | |
3533 | attrs.valuemask |= XpmReturnAllocPixels; | |
3534 | #ifdef XpmAllocCloseColors | |
3535 | attrs.alloc_close_colors = 1; | |
3536 | attrs.valuemask |= XpmAllocCloseColors; | |
3537 | #else /* not XpmAllocCloseColors */ | |
3538 | attrs.closeness = 600; | |
3539 | attrs.valuemask |= XpmCloseness; | |
3540 | #endif /* not XpmAllocCloseColors */ | |
3541 | #endif /* ALLOC_XPM_COLORS */ | |
3542 | ||
3543 | /* If image specification contains symbolic color definitions, add | |
3544 | these to `attrs'. */ | |
3545 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
3546 | if (CONSP (color_symbols)) | |
3547 | { | |
3548 | Lisp_Object tail; | |
3549 | XpmColorSymbol *xpm_syms; | |
3550 | int i, size; | |
3551 | ||
3552 | attrs.valuemask |= XpmColorSymbols; | |
3553 | ||
3554 | /* Count number of symbols. */ | |
3555 | attrs.numsymbols = 0; | |
3556 | for (tail = color_symbols; CONSP (tail); tail = XCDR (tail)) | |
3557 | ++attrs.numsymbols; | |
3558 | ||
3559 | /* Allocate an XpmColorSymbol array. */ | |
3560 | size = attrs.numsymbols * sizeof *xpm_syms; | |
3561 | xpm_syms = (XpmColorSymbol *) alloca (size); | |
3562 | bzero (xpm_syms, size); | |
3563 | attrs.colorsymbols = xpm_syms; | |
3564 | ||
3565 | /* Fill the color symbol array. */ | |
3566 | for (tail = color_symbols, i = 0; | |
3567 | CONSP (tail); | |
3568 | ++i, tail = XCDR (tail)) | |
3569 | { | |
3570 | Lisp_Object name = XCAR (XCAR (tail)); | |
3571 | Lisp_Object color = XCDR (XCAR (tail)); | |
3572 | xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1); | |
3573 | strcpy (xpm_syms[i].name, SDATA (name)); | |
3574 | xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1); | |
3575 | strcpy (xpm_syms[i].value, SDATA (color)); | |
3576 | } | |
3577 | } | |
3578 | ||
3579 | /* Create a pixmap for the image, either from a file, or from a | |
3580 | string buffer containing data in the same format as an XPM file. */ | |
3581 | #ifdef ALLOC_XPM_COLORS | |
3582 | xpm_init_color_cache (f, &attrs); | |
3583 | #endif | |
3584 | ||
3585 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
3586 | ||
3587 | #ifdef HAVE_NTGUI | |
3588 | { | |
3589 | HDC frame_dc = get_frame_dc (f); | |
3590 | hdc = CreateCompatibleDC (frame_dc); | |
3591 | release_frame_dc (f, frame_dc); | |
3592 | } | |
3593 | #endif /* HAVE_NTGUI */ | |
3594 | ||
3595 | if (STRINGP (specified_file)) | |
3596 | { | |
3597 | Lisp_Object file = x_find_image_file (specified_file); | |
3598 | if (!STRINGP (file)) | |
3599 | { | |
3600 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
3601 | return 0; | |
3602 | } | |
3603 | ||
3604 | #ifdef HAVE_NTGUI | |
3605 | /* XpmReadFileToPixmap is not available in the Windows port of | |
3606 | libxpm. But XpmReadFileToImage almost does what we want. */ | |
3607 | rc = fn_XpmReadFileToImage (&hdc, SDATA (file), | |
3608 | &xpm_image, &xpm_mask, | |
3609 | &attrs); | |
3610 | #else | |
3611 | rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3612 | SDATA (file), &img->pixmap, &img->mask, | |
3613 | &attrs); | |
3614 | #endif /* HAVE_NTGUI */ | |
3615 | } | |
3616 | else | |
3617 | { | |
3618 | Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL); | |
3619 | #ifdef HAVE_NTGUI | |
3620 | /* XpmCreatePixmapFromBuffer is not available in the Windows port | |
3621 | of libxpm. But XpmCreateImageFromBuffer almost does what we want. */ | |
3622 | rc = fn_XpmCreateImageFromBuffer (&hdc, SDATA (buffer), | |
3623 | &xpm_image, &xpm_mask, | |
3624 | &attrs); | |
3625 | #else | |
3626 | rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3627 | SDATA (buffer), | |
3628 | &img->pixmap, &img->mask, | |
3629 | &attrs); | |
3630 | #endif /* HAVE_NTGUI */ | |
3631 | } | |
3632 | ||
3633 | if (rc == XpmSuccess) | |
3634 | { | |
3635 | #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS) | |
3636 | img->colors = colors_in_color_table (&img->ncolors); | |
3637 | #else /* not ALLOC_XPM_COLORS */ | |
3638 | int i; | |
3639 | ||
3640 | #ifdef HAVE_NTGUI | |
3641 | /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap, | |
3642 | plus some duplicate attributes. */ | |
3643 | if (xpm_image && xpm_image->bitmap) | |
3644 | { | |
3645 | img->pixmap = xpm_image->bitmap; | |
3646 | /* XImageFree in libXpm frees XImage struct without destroying | |
3647 | the bitmap, which is what we want. */ | |
3648 | fn_XImageFree (xpm_image); | |
3649 | } | |
3650 | if (xpm_mask && xpm_mask->bitmap) | |
3651 | { | |
3652 | /* The mask appears to be inverted compared with what we expect. | |
3653 | TODO: invert our expectations. See other places where we | |
3654 | have to invert bits because our idea of masks is backwards. */ | |
3655 | HGDIOBJ old_obj; | |
3656 | old_obj = SelectObject (hdc, xpm_mask->bitmap); | |
3657 | ||
3658 | PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT); | |
3659 | SelectObject (hdc, old_obj); | |
3660 | ||
3661 | img->mask = xpm_mask->bitmap; | |
3662 | fn_XImageFree (xpm_mask); | |
3663 | DeleteDC (hdc); | |
3664 | } | |
3665 | ||
3666 | DeleteDC (hdc); | |
3667 | #endif /* HAVE_NTGUI */ | |
3668 | ||
3669 | /* Remember allocated colors. */ | |
3670 | img->ncolors = attrs.nalloc_pixels; | |
3671 | img->colors = (unsigned long *) xmalloc (img->ncolors | |
3672 | * sizeof *img->colors); | |
3673 | for (i = 0; i < attrs.nalloc_pixels; ++i) | |
3674 | { | |
3675 | img->colors[i] = attrs.alloc_pixels[i]; | |
3676 | #ifdef DEBUG_X_COLORS | |
3677 | register_color (img->colors[i]); | |
3678 | #endif | |
3679 | } | |
3680 | #endif /* not ALLOC_XPM_COLORS */ | |
3681 | ||
3682 | img->width = attrs.width; | |
3683 | img->height = attrs.height; | |
3684 | xassert (img->width > 0 && img->height > 0); | |
3685 | ||
3686 | /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */ | |
3687 | #ifdef HAVE_NTGUI | |
3688 | fn_XpmFreeAttributes (&attrs); | |
3689 | #else | |
3690 | XpmFreeAttributes (&attrs); | |
3691 | #endif /* HAVE_NTGUI */ | |
3692 | } | |
3693 | else | |
3694 | { | |
3695 | #ifdef HAVE_NTGUI | |
3696 | DeleteDC (hdc); | |
3697 | #endif /* HAVE_NTGUI */ | |
3698 | ||
3699 | switch (rc) | |
3700 | { | |
3701 | case XpmOpenFailed: | |
3702 | image_error ("Error opening XPM file (%s)", img->spec, Qnil); | |
3703 | break; | |
3704 | ||
3705 | case XpmFileInvalid: | |
3706 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
3707 | break; | |
3708 | ||
3709 | case XpmNoMemory: | |
3710 | image_error ("Out of memory (%s)", img->spec, Qnil); | |
3711 | break; | |
3712 | ||
3713 | case XpmColorFailed: | |
3714 | image_error ("Color allocation error (%s)", img->spec, Qnil); | |
3715 | break; | |
3716 | ||
3717 | default: | |
3718 | image_error ("Unknown error (%s)", img->spec, Qnil); | |
3719 | break; | |
3720 | } | |
3721 | } | |
3722 | ||
3723 | #ifdef ALLOC_XPM_COLORS | |
3724 | xpm_free_color_cache (); | |
3725 | #endif | |
3726 | return rc == XpmSuccess; | |
3727 | } | |
3728 | ||
3729 | #endif /* HAVE_XPM */ | |
3730 | ||
9e2a2647 | 3731 | #if defined (HAVE_NS) && !defined (HAVE_XPM) |
ea1aaa6f | 3732 | |
9e2a2647 | 3733 | /* XPM support functions for NS where libxpm is not available. |
ea1aaa6f ST |
3734 | Only XPM version 3 (without any extensions) is supported. */ |
3735 | ||
46accb8f YM |
3736 | static int xpm_scan P_ ((const unsigned char **, const unsigned char *, |
3737 | const unsigned char **, int *)); | |
ea1aaa6f | 3738 | static Lisp_Object xpm_make_color_table_v |
46accb8f YM |
3739 | P_ ((void (**) (Lisp_Object, const unsigned char *, int, Lisp_Object), |
3740 | Lisp_Object (**) (Lisp_Object, const unsigned char *, int))); | |
3741 | static void xpm_put_color_table_v P_ ((Lisp_Object, const unsigned char *, | |
9ecf6403 | 3742 | int, Lisp_Object)); |
ea1aaa6f | 3743 | static Lisp_Object xpm_get_color_table_v P_ ((Lisp_Object, |
46accb8f | 3744 | const unsigned char *, int)); |
ea1aaa6f | 3745 | static Lisp_Object xpm_make_color_table_h |
46accb8f YM |
3746 | P_ ((void (**) (Lisp_Object, const unsigned char *, int, Lisp_Object), |
3747 | Lisp_Object (**) (Lisp_Object, const unsigned char *, int))); | |
3748 | static void xpm_put_color_table_h P_ ((Lisp_Object, const unsigned char *, | |
9ecf6403 | 3749 | int, Lisp_Object)); |
ea1aaa6f | 3750 | static Lisp_Object xpm_get_color_table_h P_ ((Lisp_Object, |
46accb8f YM |
3751 | const unsigned char *, int)); |
3752 | static int xpm_str_to_color_key P_ ((const char *)); | |
ea1aaa6f | 3753 | static int xpm_load_image P_ ((struct frame *, struct image *, |
46accb8f | 3754 | const unsigned char *, const unsigned char *)); |
ea1aaa6f ST |
3755 | |
3756 | /* Tokens returned from xpm_scan. */ | |
3757 | ||
3758 | enum xpm_token | |
3759 | { | |
3760 | XPM_TK_IDENT = 256, | |
3761 | XPM_TK_STRING, | |
3762 | XPM_TK_EOF | |
3763 | }; | |
3764 | ||
3765 | /* Scan an XPM data and return a character (< 256) or a token defined | |
3766 | by enum xpm_token above. *S and END are the start (inclusive) and | |
3767 | the end (exclusive) addresses of the data, respectively. Advance | |
3768 | *S while scanning. If token is either XPM_TK_IDENT or | |
3769 | XPM_TK_STRING, *BEG and *LEN are set to the start address and the | |
3770 | length of the corresponding token, respectively. */ | |
3771 | ||
3772 | static int | |
3773 | xpm_scan (s, end, beg, len) | |
46accb8f | 3774 | const unsigned char **s, *end, **beg; |
ea1aaa6f ST |
3775 | int *len; |
3776 | { | |
3777 | int c; | |
3778 | ||
3779 | while (*s < end) | |
3780 | { | |
3781 | /* Skip white-space. */ | |
3782 | while (*s < end && (c = *(*s)++, isspace (c))) | |
9ecf6403 | 3783 | ; |
ea1aaa6f ST |
3784 | |
3785 | /* gnus-pointer.xpm uses '-' in its identifier. | |
9ecf6403 | 3786 | sb-dir-plus.xpm uses '+' in its identifier. */ |
ea1aaa6f | 3787 | if (isalpha (c) || c == '_' || c == '-' || c == '+') |
9ecf6403 YM |
3788 | { |
3789 | *beg = *s - 1; | |
2ff0845e YM |
3790 | while (*s < end |
3791 | && (c = **s, isalnum (c) || c == '_' || c == '-' || c == '+')) | |
9ecf6403 YM |
3792 | ++*s; |
3793 | *len = *s - *beg; | |
3794 | return XPM_TK_IDENT; | |
3795 | } | |
ea1aaa6f | 3796 | else if (c == '"') |
9ecf6403 YM |
3797 | { |
3798 | *beg = *s; | |
3799 | while (*s < end && **s != '"') | |
3800 | ++*s; | |
3801 | *len = *s - *beg; | |
3802 | if (*s < end) | |
3803 | ++*s; | |
3804 | return XPM_TK_STRING; | |
3805 | } | |
ea1aaa6f | 3806 | else if (c == '/') |
9ecf6403 YM |
3807 | { |
3808 | if (*s < end && **s == '*') | |
3809 | { | |
3810 | /* C-style comment. */ | |
3811 | ++*s; | |
3812 | do | |
3813 | { | |
3814 | while (*s < end && *(*s)++ != '*') | |
3815 | ; | |
3816 | } | |
3817 | while (*s < end && **s != '/'); | |
3818 | if (*s < end) | |
3819 | ++*s; | |
3820 | } | |
3821 | else | |
3822 | return c; | |
3823 | } | |
ea1aaa6f | 3824 | else |
9ecf6403 | 3825 | return c; |
ea1aaa6f ST |
3826 | } |
3827 | ||
3828 | return XPM_TK_EOF; | |
3829 | } | |
3830 | ||
bbe6f2aa | 3831 | /* Functions for color table lookup in XPM data. A key is a string |
ea1aaa6f ST |
3832 | specifying the color of each pixel in XPM data. A value is either |
3833 | an integer that specifies a pixel color, Qt that specifies | |
3834 | transparency, or Qnil for the unspecified color. If the length of | |
3835 | the key string is one, a vector is used as a table. Otherwise, a | |
3836 | hash table is used. */ | |
3837 | ||
3838 | static Lisp_Object | |
3839 | xpm_make_color_table_v (put_func, get_func) | |
46accb8f YM |
3840 | void (**put_func) (Lisp_Object, const unsigned char *, int, Lisp_Object); |
3841 | Lisp_Object (**get_func) (Lisp_Object, const unsigned char *, int); | |
ea1aaa6f ST |
3842 | { |
3843 | *put_func = xpm_put_color_table_v; | |
3844 | *get_func = xpm_get_color_table_v; | |
3845 | return Fmake_vector (make_number (256), Qnil); | |
3846 | } | |
3847 | ||
3848 | static void | |
3849 | xpm_put_color_table_v (color_table, chars_start, chars_len, color) | |
3850 | Lisp_Object color_table; | |
46accb8f | 3851 | const unsigned char *chars_start; |
ea1aaa6f ST |
3852 | int chars_len; |
3853 | Lisp_Object color; | |
3854 | { | |
3855 | XVECTOR (color_table)->contents[*chars_start] = color; | |
3856 | } | |
3857 | ||
3858 | static Lisp_Object | |
3859 | xpm_get_color_table_v (color_table, chars_start, chars_len) | |
3860 | Lisp_Object color_table; | |
46accb8f | 3861 | const unsigned char *chars_start; |
ea1aaa6f ST |
3862 | int chars_len; |
3863 | { | |
3864 | return XVECTOR (color_table)->contents[*chars_start]; | |
3865 | } | |
3866 | ||
3867 | static Lisp_Object | |
3868 | xpm_make_color_table_h (put_func, get_func) | |
46accb8f YM |
3869 | void (**put_func) (Lisp_Object, const unsigned char *, int, Lisp_Object); |
3870 | Lisp_Object (**get_func) (Lisp_Object, const unsigned char *, int); | |
ea1aaa6f ST |
3871 | { |
3872 | *put_func = xpm_put_color_table_h; | |
3873 | *get_func = xpm_get_color_table_h; | |
3874 | return make_hash_table (Qequal, make_number (DEFAULT_HASH_SIZE), | |
9ecf6403 YM |
3875 | make_float (DEFAULT_REHASH_SIZE), |
3876 | make_float (DEFAULT_REHASH_THRESHOLD), | |
3877 | Qnil, Qnil, Qnil); | |
ea1aaa6f ST |
3878 | } |
3879 | ||
3880 | static void | |
3881 | xpm_put_color_table_h (color_table, chars_start, chars_len, color) | |
3882 | Lisp_Object color_table; | |
46accb8f | 3883 | const unsigned char *chars_start; |
ea1aaa6f ST |
3884 | int chars_len; |
3885 | Lisp_Object color; | |
3886 | { | |
3887 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
3888 | unsigned hash_code; | |
3889 | Lisp_Object chars = make_unibyte_string (chars_start, chars_len); | |
3890 | ||
3891 | hash_lookup (table, chars, &hash_code); | |
3892 | hash_put (table, chars, color, hash_code); | |
3893 | } | |
3894 | ||
3895 | static Lisp_Object | |
3896 | xpm_get_color_table_h (color_table, chars_start, chars_len) | |
3897 | Lisp_Object color_table; | |
46accb8f | 3898 | const unsigned char *chars_start; |
ea1aaa6f ST |
3899 | int chars_len; |
3900 | { | |
3901 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
3902 | int i = hash_lookup (table, make_unibyte_string (chars_start, chars_len), | |
9ecf6403 | 3903 | NULL); |
ea1aaa6f ST |
3904 | |
3905 | return i >= 0 ? HASH_VALUE (table, i) : Qnil; | |
3906 | } | |
3907 | ||
3908 | enum xpm_color_key { | |
3909 | XPM_COLOR_KEY_S, | |
3910 | XPM_COLOR_KEY_M, | |
3911 | XPM_COLOR_KEY_G4, | |
3912 | XPM_COLOR_KEY_G, | |
3913 | XPM_COLOR_KEY_C | |
3914 | }; | |
3915 | ||
46accb8f | 3916 | static const char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"}; |
ea1aaa6f ST |
3917 | |
3918 | static int | |
3919 | xpm_str_to_color_key (s) | |
46accb8f | 3920 | const char *s; |
ea1aaa6f ST |
3921 | { |
3922 | int i; | |
3923 | ||
3924 | for (i = 0; | |
3925 | i < sizeof xpm_color_key_strings / sizeof xpm_color_key_strings[0]; | |
3926 | i++) | |
3927 | if (strcmp (xpm_color_key_strings[i], s) == 0) | |
3928 | return i; | |
3929 | return -1; | |
3930 | } | |
3931 | ||
3932 | static int | |
3933 | xpm_load_image (f, img, contents, end) | |
3934 | struct frame *f; | |
3935 | struct image *img; | |
46accb8f | 3936 | const unsigned char *contents, *end; |
ea1aaa6f | 3937 | { |
46accb8f | 3938 | const unsigned char *s = contents, *beg, *str; |
ea1aaa6f ST |
3939 | unsigned char buffer[BUFSIZ]; |
3940 | int width, height, x, y; | |
3941 | int num_colors, chars_per_pixel; | |
3942 | int len, LA1; | |
46accb8f YM |
3943 | void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object); |
3944 | Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int); | |
ea1aaa6f ST |
3945 | Lisp_Object frame, color_symbols, color_table; |
3946 | int best_key, have_mask = 0; | |
3947 | XImagePtr ximg = NULL, mask_img = NULL; | |
3948 | ||
3949 | #define match() \ | |
3950 | LA1 = xpm_scan (&s, end, &beg, &len) | |
3951 | ||
9ecf6403 YM |
3952 | #define expect(TOKEN) \ |
3953 | if (LA1 != (TOKEN)) \ | |
3954 | goto failure; \ | |
3955 | else \ | |
ea1aaa6f ST |
3956 | match () |
3957 | ||
9ecf6403 | 3958 | #define expect_ident(IDENT) \ |
ea1aaa6f | 3959 | if (LA1 == XPM_TK_IDENT \ |
9ecf6403 YM |
3960 | && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \ |
3961 | match (); \ | |
3962 | else \ | |
ea1aaa6f ST |
3963 | goto failure |
3964 | ||
3965 | if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0)) | |
3966 | goto failure; | |
3967 | s += 9; | |
3968 | match(); | |
3969 | expect_ident ("static"); | |
3970 | expect_ident ("char"); | |
3971 | expect ('*'); | |
3972 | expect (XPM_TK_IDENT); | |
3973 | expect ('['); | |
3974 | expect (']'); | |
3975 | expect ('='); | |
3976 | expect ('{'); | |
3977 | expect (XPM_TK_STRING); | |
3978 | if (len >= BUFSIZ) | |
3979 | goto failure; | |
3980 | memcpy (buffer, beg, len); | |
3981 | buffer[len] = '\0'; | |
3982 | if (sscanf (buffer, "%d %d %d %d", &width, &height, | |
9ecf6403 | 3983 | &num_colors, &chars_per_pixel) != 4 |
ea1aaa6f ST |
3984 | || width <= 0 || height <= 0 |
3985 | || num_colors <= 0 || chars_per_pixel <= 0) | |
3986 | goto failure; | |
de297941 YM |
3987 | |
3988 | if (!check_image_size (f, width, height)) | |
3989 | { | |
10ea2b82 | 3990 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
de297941 YM |
3991 | goto failure; |
3992 | } | |
3993 | ||
ea1aaa6f ST |
3994 | expect (','); |
3995 | ||
3996 | XSETFRAME (frame, f); | |
3997 | if (!NILP (Fxw_display_color_p (frame))) | |
3998 | best_key = XPM_COLOR_KEY_C; | |
3999 | else if (!NILP (Fx_display_grayscale_p (frame))) | |
4000 | best_key = (XFASTINT (Fx_display_planes (frame)) > 2 | |
9ecf6403 | 4001 | ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4); |
ea1aaa6f ST |
4002 | else |
4003 | best_key = XPM_COLOR_KEY_M; | |
4004 | ||
4005 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
4006 | if (chars_per_pixel == 1) | |
4007 | color_table = xpm_make_color_table_v (&put_color_table, | |
9ecf6403 | 4008 | &get_color_table); |
ea1aaa6f ST |
4009 | else |
4010 | color_table = xpm_make_color_table_h (&put_color_table, | |
9ecf6403 | 4011 | &get_color_table); |
ea1aaa6f ST |
4012 | |
4013 | while (num_colors-- > 0) | |
4014 | { | |
4015 | unsigned char *color, *max_color; | |
4016 | int key, next_key, max_key = 0; | |
4017 | Lisp_Object symbol_color = Qnil, color_val; | |
4018 | XColor cdef; | |
4019 | ||
4020 | expect (XPM_TK_STRING); | |
4021 | if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel) | |
9ecf6403 | 4022 | goto failure; |
ea1aaa6f ST |
4023 | memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel); |
4024 | buffer[len - chars_per_pixel] = '\0'; | |
4025 | ||
4026 | str = strtok (buffer, " \t"); | |
4027 | if (str == NULL) | |
9ecf6403 | 4028 | goto failure; |
ea1aaa6f ST |
4029 | key = xpm_str_to_color_key (str); |
4030 | if (key < 0) | |
9ecf6403 | 4031 | goto failure; |
ea1aaa6f | 4032 | do |
9ecf6403 YM |
4033 | { |
4034 | color = strtok (NULL, " \t"); | |
4035 | if (color == NULL) | |
4036 | goto failure; | |
ea1aaa6f | 4037 | |
9db94f82 | 4038 | while ((str = strtok (NULL, " \t")) != NULL) |
9ecf6403 YM |
4039 | { |
4040 | next_key = xpm_str_to_color_key (str); | |
4041 | if (next_key >= 0) | |
4042 | break; | |
4043 | color[strlen (color)] = ' '; | |
4044 | } | |
ea1aaa6f | 4045 | |
9ecf6403 YM |
4046 | if (key == XPM_COLOR_KEY_S) |
4047 | { | |
4048 | if (NILP (symbol_color)) | |
4049 | symbol_color = build_string (color); | |
4050 | } | |
4051 | else if (max_key < key && key <= best_key) | |
4052 | { | |
4053 | max_key = key; | |
4054 | max_color = color; | |
4055 | } | |
4056 | key = next_key; | |
4057 | } | |
ea1aaa6f ST |
4058 | while (str); |
4059 | ||
4060 | color_val = Qnil; | |
4061 | if (!NILP (color_symbols) && !NILP (symbol_color)) | |
9ecf6403 YM |
4062 | { |
4063 | Lisp_Object specified_color = Fassoc (symbol_color, color_symbols); | |
4064 | ||
4065 | if (CONSP (specified_color) && STRINGP (XCDR (specified_color))) | |
9db94f82 | 4066 | { |
05131107 | 4067 | if (xstrcasecmp (SDATA (XCDR (specified_color)), "None") == 0) |
9db94f82 YM |
4068 | color_val = Qt; |
4069 | else if (x_defined_color (f, SDATA (XCDR (specified_color)), | |
4070 | &cdef, 0)) | |
4071 | color_val = make_number (cdef.pixel); | |
4072 | } | |
9ecf6403 | 4073 | } |
ea1aaa6f | 4074 | if (NILP (color_val) && max_key > 0) |
9db94f82 | 4075 | { |
05131107 | 4076 | if (xstrcasecmp (max_color, "None") == 0) |
9db94f82 YM |
4077 | color_val = Qt; |
4078 | else if (x_defined_color (f, max_color, &cdef, 0)) | |
4079 | color_val = make_number (cdef.pixel); | |
4080 | } | |
ea1aaa6f | 4081 | if (!NILP (color_val)) |
9ecf6403 | 4082 | (*put_color_table) (color_table, beg, chars_per_pixel, color_val); |
ea1aaa6f ST |
4083 | |
4084 | expect (','); | |
4085 | } | |
4086 | ||
4087 | if (!x_create_x_image_and_pixmap (f, width, height, 0, | |
9ecf6403 | 4088 | &ximg, &img->pixmap) |
edfda783 | 4089 | #ifndef HAVE_NS |
ea1aaa6f | 4090 | || !x_create_x_image_and_pixmap (f, width, height, 1, |
edfda783 AR |
4091 | &mask_img, &img->mask) |
4092 | #endif | |
4093 | ) | |
ea1aaa6f ST |
4094 | { |
4095 | image_error ("Out of memory (%s)", img->spec, Qnil); | |
4096 | goto error; | |
4097 | } | |
4098 | ||
4099 | for (y = 0; y < height; y++) | |
4100 | { | |
4101 | expect (XPM_TK_STRING); | |
4102 | str = beg; | |
4103 | if (len < width * chars_per_pixel) | |
9ecf6403 | 4104 | goto failure; |
ea1aaa6f | 4105 | for (x = 0; x < width; x++, str += chars_per_pixel) |
9ecf6403 YM |
4106 | { |
4107 | Lisp_Object color_val = | |
4108 | (*get_color_table) (color_table, str, chars_per_pixel); | |
4109 | ||
4110 | XPutPixel (ximg, x, y, | |
4111 | (INTEGERP (color_val) ? XINT (color_val) | |
4112 | : FRAME_FOREGROUND_PIXEL (f))); | |
edfda783 | 4113 | #ifndef HAVE_NS |
9ecf6403 YM |
4114 | XPutPixel (mask_img, x, y, |
4115 | (!EQ (color_val, Qt) ? PIX_MASK_DRAW | |
4116 | : (have_mask = 1, PIX_MASK_RETAIN))); | |
edfda783 AR |
4117 | #else |
4118 | if (EQ(color_val, Qt)) | |
4119 | ns_set_alpha(ximg, x, y, 0); | |
4120 | #endif | |
9ecf6403 | 4121 | } |
ea1aaa6f | 4122 | if (y + 1 < height) |
9ecf6403 | 4123 | expect (','); |
ea1aaa6f ST |
4124 | } |
4125 | ||
4126 | img->width = width; | |
4127 | img->height = height; | |
4128 | ||
0b8ac842 YM |
4129 | /* Maybe fill in the background field while we have ximg handy. */ |
4130 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
4131 | IMAGE_BACKGROUND (img, f, ximg); | |
4132 | ||
ea1aaa6f ST |
4133 | x_put_x_image (f, ximg, img->pixmap, width, height); |
4134 | x_destroy_x_image (ximg); | |
edfda783 | 4135 | #ifndef HAVE_NS |
ea1aaa6f ST |
4136 | if (have_mask) |
4137 | { | |
9ecf6403 YM |
4138 | /* Fill in the background_transparent field while we have the |
4139 | mask handy. */ | |
4140 | image_background_transparent (img, f, mask_img); | |
4141 | ||
ea1aaa6f ST |
4142 | x_put_x_image (f, mask_img, img->mask, width, height); |
4143 | x_destroy_x_image (mask_img); | |
4144 | } | |
4145 | else | |
4146 | { | |
4147 | x_destroy_x_image (mask_img); | |
4148 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
4149 | img->mask = NO_PIXMAP; | |
4150 | } | |
edfda783 | 4151 | #endif |
ea1aaa6f ST |
4152 | return 1; |
4153 | ||
4154 | failure: | |
4155 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
4156 | error: | |
4157 | x_destroy_x_image (ximg); | |
4158 | x_destroy_x_image (mask_img); | |
4159 | x_clear_image (f, img); | |
4160 | return 0; | |
4161 | ||
4162 | #undef match | |
4163 | #undef expect | |
4164 | #undef expect_ident | |
4165 | } | |
4166 | ||
4167 | static int | |
4168 | xpm_load (f, img) | |
4169 | struct frame *f; | |
4170 | struct image *img; | |
4171 | { | |
4172 | int success_p = 0; | |
4173 | Lisp_Object file_name; | |
4174 | ||
4175 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
4176 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
4177 | if (STRINGP (file_name)) | |
4178 | { | |
4179 | Lisp_Object file; | |
4180 | unsigned char *contents; | |
4181 | int size; | |
4182 | struct gcpro gcpro1; | |
4183 | ||
4184 | file = x_find_image_file (file_name); | |
4185 | GCPRO1 (file); | |
4186 | if (!STRINGP (file)) | |
9ecf6403 YM |
4187 | { |
4188 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
4189 | UNGCPRO; | |
4190 | return 0; | |
4191 | } | |
ea1aaa6f ST |
4192 | |
4193 | contents = slurp_file (SDATA (file), &size); | |
4194 | if (contents == NULL) | |
9ecf6403 YM |
4195 | { |
4196 | image_error ("Error loading XPM image `%s'", img->spec, Qnil); | |
4197 | UNGCPRO; | |
4198 | return 0; | |
4199 | } | |
ea1aaa6f ST |
4200 | |
4201 | success_p = xpm_load_image (f, img, contents, contents + size); | |
4202 | xfree (contents); | |
4203 | UNGCPRO; | |
4204 | } | |
4205 | else | |
4206 | { | |
4207 | Lisp_Object data; | |
4208 | ||
4209 | data = image_spec_value (img->spec, QCdata, NULL); | |
4210 | success_p = xpm_load_image (f, img, SDATA (data), | |
9ecf6403 | 4211 | SDATA (data) + SBYTES (data)); |
ea1aaa6f ST |
4212 | } |
4213 | ||
4214 | return success_p; | |
4215 | } | |
4216 | ||
9e2a2647 | 4217 | #endif /* HAVE_NS && !HAVE_XPM */ |
b0da69a7 | 4218 | |
ea1aaa6f | 4219 | |
4ef0d4d0 KS |
4220 | \f |
4221 | /*********************************************************************** | |
4222 | Color table | |
4223 | ***********************************************************************/ | |
4224 | ||
4225 | #ifdef COLOR_TABLE_SUPPORT | |
4226 | ||
4227 | /* An entry in the color table mapping an RGB color to a pixel color. */ | |
4228 | ||
4229 | struct ct_color | |
4230 | { | |
4231 | int r, g, b; | |
4232 | unsigned long pixel; | |
4233 | ||
4234 | /* Next in color table collision list. */ | |
4235 | struct ct_color *next; | |
4236 | }; | |
4237 | ||
4238 | /* The bucket vector size to use. Must be prime. */ | |
4239 | ||
4240 | #define CT_SIZE 101 | |
4241 | ||
4242 | /* Value is a hash of the RGB color given by R, G, and B. */ | |
4243 | ||
4244 | #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B)) | |
4245 | ||
4246 | /* The color hash table. */ | |
4247 | ||
4248 | struct ct_color **ct_table; | |
4249 | ||
4250 | /* Number of entries in the color table. */ | |
4251 | ||
4252 | int ct_colors_allocated; | |
4253 | ||
4254 | /* Initialize the color table. */ | |
4255 | ||
4256 | static void | |
4257 | init_color_table () | |
4258 | { | |
4259 | int size = CT_SIZE * sizeof (*ct_table); | |
4260 | ct_table = (struct ct_color **) xmalloc (size); | |
4261 | bzero (ct_table, size); | |
4262 | ct_colors_allocated = 0; | |
4263 | } | |
4264 | ||
4265 | ||
4266 | /* Free memory associated with the color table. */ | |
4267 | ||
4268 | static void | |
4269 | free_color_table () | |
4270 | { | |
4271 | int i; | |
4272 | struct ct_color *p, *next; | |
4273 | ||
4274 | for (i = 0; i < CT_SIZE; ++i) | |
4275 | for (p = ct_table[i]; p; p = next) | |
4276 | { | |
4277 | next = p->next; | |
4278 | xfree (p); | |
4279 | } | |
4280 | ||
4281 | xfree (ct_table); | |
4282 | ct_table = NULL; | |
4283 | } | |
4284 | ||
4285 | ||
4286 | /* Value is a pixel color for RGB color R, G, B on frame F. If an | |
4287 | entry for that color already is in the color table, return the | |
4288 | pixel color of that entry. Otherwise, allocate a new color for R, | |
4289 | G, B, and make an entry in the color table. */ | |
4290 | ||
4291 | static unsigned long | |
4292 | lookup_rgb_color (f, r, g, b) | |
4293 | struct frame *f; | |
4294 | int r, g, b; | |
4295 | { | |
4296 | unsigned hash = CT_HASH_RGB (r, g, b); | |
4297 | int i = hash % CT_SIZE; | |
4298 | struct ct_color *p; | |
4299 | Display_Info *dpyinfo; | |
4300 | ||
4301 | /* Handle TrueColor visuals specially, which improves performance by | |
4302 | two orders of magnitude. Freeing colors on TrueColor visuals is | |
4303 | a nop, and pixel colors specify RGB values directly. See also | |
4304 | the Xlib spec, chapter 3.1. */ | |
4305 | dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
4306 | if (dpyinfo->red_bits > 0) | |
4307 | { | |
4308 | unsigned long pr, pg, pb; | |
4309 | ||
4310 | /* Apply gamma-correction like normal color allocation does. */ | |
4311 | if (f->gamma) | |
4312 | { | |
4313 | XColor color; | |
4314 | color.red = r, color.green = g, color.blue = b; | |
4315 | gamma_correct (f, &color); | |
4316 | r = color.red, g = color.green, b = color.blue; | |
4317 | } | |
4318 | ||
4319 | /* Scale down RGB values to the visual's bits per RGB, and shift | |
4320 | them to the right position in the pixel color. Note that the | |
4321 | original RGB values are 16-bit values, as usual in X. */ | |
4322 | pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset; | |
4323 | pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset; | |
4324 | pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset; | |
4325 | ||
4326 | /* Assemble the pixel color. */ | |
4327 | return pr | pg | pb; | |
4328 | } | |
1f026899 | 4329 | |
4ef0d4d0 KS |
4330 | for (p = ct_table[i]; p; p = p->next) |
4331 | if (p->r == r && p->g == g && p->b == b) | |
4332 | break; | |
4333 | ||
4334 | if (p == NULL) | |
4335 | { | |
4336 | ||
4337 | #ifdef HAVE_X_WINDOWS | |
4338 | XColor color; | |
4339 | Colormap cmap; | |
4340 | int rc; | |
4341 | ||
4342 | color.red = r; | |
4343 | color.green = g; | |
4344 | color.blue = b; | |
4345 | ||
4346 | cmap = FRAME_X_COLORMAP (f); | |
4347 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4348 | if (rc) | |
4349 | { | |
4350 | ++ct_colors_allocated; | |
4351 | p = (struct ct_color *) xmalloc (sizeof *p); | |
4352 | p->r = r; | |
4353 | p->g = g; | |
4354 | p->b = b; | |
4355 | p->pixel = color.pixel; | |
4356 | p->next = ct_table[i]; | |
4357 | ct_table[i] = p; | |
4358 | } | |
4359 | else | |
4360 | return FRAME_FOREGROUND_PIXEL (f); | |
4361 | ||
4362 | #else | |
4363 | COLORREF color; | |
4364 | #ifdef HAVE_NTGUI | |
4365 | color = PALETTERGB (r, g, b); | |
4366 | #else | |
4367 | color = RGB_TO_ULONG (r, g, b); | |
4368 | #endif /* HAVE_NTGUI */ | |
4369 | ++ct_colors_allocated; | |
4370 | p = (struct ct_color *) xmalloc (sizeof *p); | |
4371 | p->r = r; | |
4372 | p->g = g; | |
4373 | p->b = b; | |
4374 | p->pixel = color; | |
4375 | p->next = ct_table[i]; | |
4376 | ct_table[i] = p; | |
4377 | #endif /* HAVE_X_WINDOWS */ | |
4378 | ||
4379 | } | |
4380 | ||
4381 | return p->pixel; | |
4382 | } | |
4383 | ||
4384 | ||
4385 | /* Look up pixel color PIXEL which is used on frame F in the color | |
4386 | table. If not already present, allocate it. Value is PIXEL. */ | |
4387 | ||
4388 | static unsigned long | |
4389 | lookup_pixel_color (f, pixel) | |
4390 | struct frame *f; | |
4391 | unsigned long pixel; | |
4392 | { | |
4393 | int i = pixel % CT_SIZE; | |
4394 | struct ct_color *p; | |
4395 | ||
4396 | for (p = ct_table[i]; p; p = p->next) | |
4397 | if (p->pixel == pixel) | |
4398 | break; | |
4399 | ||
4400 | if (p == NULL) | |
4401 | { | |
4402 | XColor color; | |
4403 | Colormap cmap; | |
4404 | int rc; | |
4405 | ||
4406 | #ifdef HAVE_X_WINDOWS | |
4407 | cmap = FRAME_X_COLORMAP (f); | |
4408 | color.pixel = pixel; | |
4409 | x_query_color (f, &color); | |
4410 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4411 | #else | |
4412 | BLOCK_INPUT; | |
4413 | cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f)); | |
4414 | color.pixel = pixel; | |
4415 | XQueryColor (NULL, cmap, &color); | |
4416 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4417 | UNBLOCK_INPUT; | |
4418 | #endif /* HAVE_X_WINDOWS */ | |
4419 | ||
4420 | if (rc) | |
4421 | { | |
4422 | ++ct_colors_allocated; | |
4423 | ||
4424 | p = (struct ct_color *) xmalloc (sizeof *p); | |
4425 | p->r = color.red; | |
4426 | p->g = color.green; | |
4427 | p->b = color.blue; | |
4428 | p->pixel = pixel; | |
4429 | p->next = ct_table[i]; | |
4430 | ct_table[i] = p; | |
4431 | } | |
4432 | else | |
4433 | return FRAME_FOREGROUND_PIXEL (f); | |
4434 | } | |
4435 | return p->pixel; | |
4436 | } | |
4437 | ||
4438 | ||
4439 | /* Value is a vector of all pixel colors contained in the color table, | |
4440 | allocated via xmalloc. Set *N to the number of colors. */ | |
4441 | ||
4442 | static unsigned long * | |
4443 | colors_in_color_table (n) | |
4444 | int *n; | |
4445 | { | |
4446 | int i, j; | |
4447 | struct ct_color *p; | |
4448 | unsigned long *colors; | |
4449 | ||
4450 | if (ct_colors_allocated == 0) | |
4451 | { | |
4452 | *n = 0; | |
4453 | colors = NULL; | |
4454 | } | |
4455 | else | |
4456 | { | |
4457 | colors = (unsigned long *) xmalloc (ct_colors_allocated | |
4458 | * sizeof *colors); | |
4459 | *n = ct_colors_allocated; | |
4460 | ||
4461 | for (i = j = 0; i < CT_SIZE; ++i) | |
4462 | for (p = ct_table[i]; p; p = p->next) | |
4463 | colors[j++] = p->pixel; | |
4464 | } | |
4465 | ||
4466 | return colors; | |
4467 | } | |
4468 | ||
4469 | #else /* COLOR_TABLE_SUPPORT */ | |
4470 | ||
4471 | static unsigned long | |
4472 | lookup_rgb_color (f, r, g, b) | |
4473 | struct frame *f; | |
4474 | int r, g, b; | |
4475 | { | |
4476 | unsigned long pixel; | |
4477 | ||
4ef0d4d0 KS |
4478 | #ifdef HAVE_NTGUI |
4479 | pixel = PALETTERGB (r >> 8, g >> 8, b >> 8); | |
4480 | #endif /* HAVE_NTGUI */ | |
4481 | ||
edfda783 AR |
4482 | #ifdef HAVE_NS |
4483 | pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8); | |
4484 | #endif /* HAVE_NS */ | |
4ef0d4d0 KS |
4485 | return pixel; |
4486 | } | |
4487 | ||
4488 | static void | |
4489 | init_color_table () | |
4490 | { | |
4491 | } | |
4492 | #endif /* COLOR_TABLE_SUPPORT */ | |
4493 | ||
4494 | \f | |
4495 | /*********************************************************************** | |
4496 | Algorithms | |
4497 | ***********************************************************************/ | |
4498 | ||
4499 | static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int)); | |
4500 | static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *)); | |
4501 | static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int)); | |
4502 | ||
4503 | #ifdef HAVE_NTGUI | |
4504 | static void XPutPixel (XImagePtr , int, int, COLORREF); | |
4505 | #endif /* HAVE_NTGUI */ | |
4506 | ||
4507 | /* Non-zero means draw a cross on images having `:conversion | |
4508 | disabled'. */ | |
4509 | ||
4510 | int cross_disabled_images; | |
4511 | ||
4512 | /* Edge detection matrices for different edge-detection | |
4513 | strategies. */ | |
4514 | ||
4515 | static int emboss_matrix[9] = { | |
4516 | /* x - 1 x x + 1 */ | |
4517 | 2, -1, 0, /* y - 1 */ | |
4518 | -1, 0, 1, /* y */ | |
4519 | 0, 1, -2 /* y + 1 */ | |
4520 | }; | |
4521 | ||
4522 | static int laplace_matrix[9] = { | |
4523 | /* x - 1 x x + 1 */ | |
4524 | 1, 0, 0, /* y - 1 */ | |
4525 | 0, 0, 0, /* y */ | |
4526 | 0, 0, -1 /* y + 1 */ | |
4527 | }; | |
4528 | ||
4529 | /* Value is the intensity of the color whose red/green/blue values | |
4530 | are R, G, and B. */ | |
4531 | ||
4532 | #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6) | |
4533 | ||
4534 | ||
4535 | /* On frame F, return an array of XColor structures describing image | |
4536 | IMG->pixmap. Each XColor structure has its pixel color set. RGB_P | |
4537 | non-zero means also fill the red/green/blue members of the XColor | |
4538 | structures. Value is a pointer to the array of XColors structures, | |
4539 | allocated with xmalloc; it must be freed by the caller. */ | |
4540 | ||
4541 | static XColor * | |
4542 | x_to_xcolors (f, img, rgb_p) | |
4543 | struct frame *f; | |
4544 | struct image *img; | |
4545 | int rgb_p; | |
4546 | { | |
4547 | int x, y; | |
4548 | XColor *colors, *p; | |
4549 | XImagePtr_or_DC ximg; | |
4550 | #ifdef HAVE_NTGUI | |
4551 | HDC hdc; | |
4552 | HGDIOBJ prev; | |
4553 | #endif /* HAVE_NTGUI */ | |
4554 | ||
4555 | colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors); | |
4556 | ||
4557 | #ifndef HAVE_NTGUI | |
4558 | /* Get the X image IMG->pixmap. */ | |
4559 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, | |
4560 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
4561 | #else | |
4562 | /* Load the image into a memory device context. */ | |
4563 | hdc = get_frame_dc (f); | |
4564 | ximg = CreateCompatibleDC (hdc); | |
4565 | release_frame_dc (f, hdc); | |
4566 | prev = SelectObject (ximg, img->pixmap); | |
4567 | #endif /* HAVE_NTGUI */ | |
4568 | ||
4569 | /* Fill the `pixel' members of the XColor array. I wished there | |
4570 | were an easy and portable way to circumvent XGetPixel. */ | |
4571 | p = colors; | |
4572 | for (y = 0; y < img->height; ++y) | |
4573 | { | |
4574 | XColor *row = p; | |
4575 | ||
8b7d0a16 | 4576 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
4ef0d4d0 | 4577 | for (x = 0; x < img->width; ++x, ++p) |
8b7d0a16 | 4578 | p->pixel = GET_PIXEL (ximg, x, y); |
4ef0d4d0 KS |
4579 | if (rgb_p) |
4580 | x_query_colors (f, row, img->width); | |
4581 | ||
4582 | #else | |
4583 | ||
4584 | for (x = 0; x < img->width; ++x, ++p) | |
4585 | { | |
4586 | /* W32_TODO: palette support needed here? */ | |
4587 | p->pixel = GET_PIXEL (ximg, x, y); | |
4588 | if (rgb_p) | |
4589 | { | |
4ef0d4d0 KS |
4590 | p->red = RED16_FROM_ULONG (p->pixel); |
4591 | p->green = GREEN16_FROM_ULONG (p->pixel); | |
4592 | p->blue = BLUE16_FROM_ULONG (p->pixel); | |
4ef0d4d0 KS |
4593 | } |
4594 | } | |
4595 | #endif /* HAVE_X_WINDOWS */ | |
4596 | } | |
4597 | ||
4598 | Destroy_Image (ximg, prev); | |
4599 | ||
4600 | return colors; | |
4601 | } | |
4602 | ||
4603 | #ifdef HAVE_NTGUI | |
4604 | ||
4605 | /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been | |
4606 | created with CreateDIBSection, with the pointer to the bit values | |
4607 | stored in ximg->data. */ | |
4608 | ||
3046a84b JB |
4609 | static void |
4610 | XPutPixel (ximg, x, y, color) | |
4ef0d4d0 KS |
4611 | XImagePtr ximg; |
4612 | int x, y; | |
4613 | COLORREF color; | |
4614 | { | |
4615 | int width = ximg->info.bmiHeader.biWidth; | |
4616 | int height = ximg->info.bmiHeader.biHeight; | |
4617 | unsigned char * pixel; | |
4618 | ||
4619 | /* True color images. */ | |
4620 | if (ximg->info.bmiHeader.biBitCount == 24) | |
4621 | { | |
4622 | int rowbytes = width * 3; | |
4623 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4624 | if (rowbytes % 4) | |
4625 | rowbytes += 4 - (rowbytes % 4); | |
4626 | ||
4627 | pixel = ximg->data + y * rowbytes + x * 3; | |
4628 | /* Windows bitmaps are in BGR order. */ | |
4629 | *pixel = GetBValue (color); | |
4630 | *(pixel + 1) = GetGValue (color); | |
4631 | *(pixel + 2) = GetRValue (color); | |
4632 | } | |
4633 | /* Monochrome images. */ | |
4634 | else if (ximg->info.bmiHeader.biBitCount == 1) | |
4635 | { | |
4636 | int rowbytes = width / 8; | |
4637 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4638 | if (rowbytes % 4) | |
4639 | rowbytes += 4 - (rowbytes % 4); | |
4640 | pixel = ximg->data + y * rowbytes + x / 8; | |
4641 | /* Filter out palette info. */ | |
4642 | if (color & 0x00ffffff) | |
4643 | *pixel = *pixel | (1 << x % 8); | |
4644 | else | |
4645 | *pixel = *pixel & ~(1 << x % 8); | |
4646 | } | |
4647 | else | |
4648 | image_error ("XPutPixel: palette image not supported", Qnil, Qnil); | |
4649 | } | |
4650 | ||
4651 | #endif /* HAVE_NTGUI */ | |
4652 | ||
4653 | /* Create IMG->pixmap from an array COLORS of XColor structures, whose | |
4654 | RGB members are set. F is the frame on which this all happens. | |
4655 | COLORS will be freed; an existing IMG->pixmap will be freed, too. */ | |
4656 | ||
4657 | static void | |
4658 | x_from_xcolors (f, img, colors) | |
4659 | struct frame *f; | |
4660 | struct image *img; | |
4661 | XColor *colors; | |
4662 | { | |
4663 | int x, y; | |
edfda783 | 4664 | XImagePtr oimg = NULL; |
9d7112ed | 4665 | Pixmap pixmap; |
4ef0d4d0 KS |
4666 | XColor *p; |
4667 | ||
4668 | init_color_table (); | |
4669 | ||
4670 | x_create_x_image_and_pixmap (f, img->width, img->height, 0, | |
4671 | &oimg, &pixmap); | |
4672 | p = colors; | |
4673 | for (y = 0; y < img->height; ++y) | |
4674 | for (x = 0; x < img->width; ++x, ++p) | |
4675 | { | |
4676 | unsigned long pixel; | |
4677 | pixel = lookup_rgb_color (f, p->red, p->green, p->blue); | |
4678 | XPutPixel (oimg, x, y, pixel); | |
4679 | } | |
4680 | ||
4681 | xfree (colors); | |
4682 | x_clear_image_1 (f, img, 1, 0, 1); | |
4683 | ||
4684 | x_put_x_image (f, oimg, pixmap, img->width, img->height); | |
4685 | x_destroy_x_image (oimg); | |
4686 | img->pixmap = pixmap; | |
4687 | #ifdef COLOR_TABLE_SUPPORT | |
4688 | img->colors = colors_in_color_table (&img->ncolors); | |
4689 | free_color_table (); | |
4690 | #endif /* COLOR_TABLE_SUPPORT */ | |
4691 | } | |
4692 | ||
4693 | ||
4694 | /* On frame F, perform edge-detection on image IMG. | |
4695 | ||
4696 | MATRIX is a nine-element array specifying the transformation | |
4697 | matrix. See emboss_matrix for an example. | |
4698 | ||
4699 | COLOR_ADJUST is a color adjustment added to each pixel of the | |
4700 | outgoing image. */ | |
4701 | ||
4702 | static void | |
4703 | x_detect_edges (f, img, matrix, color_adjust) | |
4704 | struct frame *f; | |
4705 | struct image *img; | |
4706 | int matrix[9], color_adjust; | |
4707 | { | |
4708 | XColor *colors = x_to_xcolors (f, img, 1); | |
4709 | XColor *new, *p; | |
4710 | int x, y, i, sum; | |
4711 | ||
4712 | for (i = sum = 0; i < 9; ++i) | |
1ea40aa2 | 4713 | sum += eabs (matrix[i]); |
4ef0d4d0 KS |
4714 | |
4715 | #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X)) | |
4716 | ||
4717 | new = (XColor *) xmalloc (img->width * img->height * sizeof *new); | |
4718 | ||
4719 | for (y = 0; y < img->height; ++y) | |
4720 | { | |
4721 | p = COLOR (new, 0, y); | |
4722 | p->red = p->green = p->blue = 0xffff/2; | |
4723 | p = COLOR (new, img->width - 1, y); | |
4724 | p->red = p->green = p->blue = 0xffff/2; | |
4725 | } | |
4726 | ||
4727 | for (x = 1; x < img->width - 1; ++x) | |
4728 | { | |
4729 | p = COLOR (new, x, 0); | |
4730 | p->red = p->green = p->blue = 0xffff/2; | |
4731 | p = COLOR (new, x, img->height - 1); | |
4732 | p->red = p->green = p->blue = 0xffff/2; | |
4733 | } | |
4734 | ||
4735 | for (y = 1; y < img->height - 1; ++y) | |
4736 | { | |
4737 | p = COLOR (new, 1, y); | |
4738 | ||
4739 | for (x = 1; x < img->width - 1; ++x, ++p) | |
4740 | { | |
4741 | int r, g, b, y1, x1; | |
4742 | ||
4743 | r = g = b = i = 0; | |
4744 | for (y1 = y - 1; y1 < y + 2; ++y1) | |
4745 | for (x1 = x - 1; x1 < x + 2; ++x1, ++i) | |
4746 | if (matrix[i]) | |
4747 | { | |
4748 | XColor *t = COLOR (colors, x1, y1); | |
4749 | r += matrix[i] * t->red; | |
4750 | g += matrix[i] * t->green; | |
4751 | b += matrix[i] * t->blue; | |
4752 | } | |
4753 | ||
4754 | r = (r / sum + color_adjust) & 0xffff; | |
4755 | g = (g / sum + color_adjust) & 0xffff; | |
4756 | b = (b / sum + color_adjust) & 0xffff; | |
4757 | p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b); | |
4758 | } | |
4759 | } | |
4760 | ||
4761 | xfree (colors); | |
4762 | x_from_xcolors (f, img, new); | |
4763 | ||
4764 | #undef COLOR | |
4765 | } | |
4766 | ||
4767 | ||
4768 | /* Perform the pre-defined `emboss' edge-detection on image IMG | |
4769 | on frame F. */ | |
4770 | ||
4771 | static void | |
4772 | x_emboss (f, img) | |
4773 | struct frame *f; | |
4774 | struct image *img; | |
4775 | { | |
4776 | x_detect_edges (f, img, emboss_matrix, 0xffff / 2); | |
4777 | } | |
4778 | ||
4779 | ||
4780 | /* Transform image IMG which is used on frame F with a Laplace | |
4781 | edge-detection algorithm. The result is an image that can be used | |
4782 | to draw disabled buttons, for example. */ | |
4783 | ||
4784 | static void | |
4785 | x_laplace (f, img) | |
4786 | struct frame *f; | |
4787 | struct image *img; | |
4788 | { | |
4789 | x_detect_edges (f, img, laplace_matrix, 45000); | |
4790 | } | |
4791 | ||
4792 | ||
4793 | /* Perform edge-detection on image IMG on frame F, with specified | |
4794 | transformation matrix MATRIX and color-adjustment COLOR_ADJUST. | |
4795 | ||
4796 | MATRIX must be either | |
4797 | ||
4798 | - a list of at least 9 numbers in row-major form | |
4799 | - a vector of at least 9 numbers | |
4800 | ||
4801 | COLOR_ADJUST nil means use a default; otherwise it must be a | |
4802 | number. */ | |
4803 | ||
4804 | static void | |
4805 | x_edge_detection (f, img, matrix, color_adjust) | |
4806 | struct frame *f; | |
4807 | struct image *img; | |
4808 | Lisp_Object matrix, color_adjust; | |
4809 | { | |
4810 | int i = 0; | |
4811 | int trans[9]; | |
4812 | ||
4813 | if (CONSP (matrix)) | |
4814 | { | |
4815 | for (i = 0; | |
4816 | i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix)); | |
4817 | ++i, matrix = XCDR (matrix)) | |
4818 | trans[i] = XFLOATINT (XCAR (matrix)); | |
4819 | } | |
4820 | else if (VECTORP (matrix) && ASIZE (matrix) >= 9) | |
4821 | { | |
4822 | for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i) | |
4823 | trans[i] = XFLOATINT (AREF (matrix, i)); | |
4824 | } | |
4825 | ||
4826 | if (NILP (color_adjust)) | |
4827 | color_adjust = make_number (0xffff / 2); | |
4828 | ||
4829 | if (i == 9 && NUMBERP (color_adjust)) | |
4830 | x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust)); | |
4831 | } | |
4832 | ||
4833 | ||
4834 | /* Transform image IMG on frame F so that it looks disabled. */ | |
4835 | ||
4836 | static void | |
4837 | x_disable_image (f, img) | |
4838 | struct frame *f; | |
4839 | struct image *img; | |
4840 | { | |
4841 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
4842 | #ifdef HAVE_NTGUI | |
4843 | int n_planes = dpyinfo->n_planes * dpyinfo->n_cbits; | |
4844 | #else | |
4845 | int n_planes = dpyinfo->n_planes; | |
4846 | #endif /* HAVE_NTGUI */ | |
4847 | ||
4848 | if (n_planes >= 2) | |
4849 | { | |
4850 | /* Color (or grayscale). Convert to gray, and equalize. Just | |
4851 | drawing such images with a stipple can look very odd, so | |
4852 | we're using this method instead. */ | |
4853 | XColor *colors = x_to_xcolors (f, img, 1); | |
4854 | XColor *p, *end; | |
4855 | const int h = 15000; | |
4856 | const int l = 30000; | |
4857 | ||
4858 | for (p = colors, end = colors + img->width * img->height; | |
4859 | p < end; | |
4860 | ++p) | |
4861 | { | |
4862 | int i = COLOR_INTENSITY (p->red, p->green, p->blue); | |
4863 | int i2 = (0xffff - h - l) * i / 0xffff + l; | |
4864 | p->red = p->green = p->blue = i2; | |
4865 | } | |
4866 | ||
4867 | x_from_xcolors (f, img, colors); | |
4868 | } | |
4869 | ||
4870 | /* Draw a cross over the disabled image, if we must or if we | |
4871 | should. */ | |
4872 | if (n_planes < 2 || cross_disabled_images) | |
4873 | { | |
4874 | #ifndef HAVE_NTGUI | |
4875 | Display *dpy = FRAME_X_DISPLAY (f); | |
4876 | GC gc; | |
4877 | ||
5afa3a81 | 4878 | #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */ |
edfda783 | 4879 | |
4ef0d4d0 | 4880 | #define MaskForeground(f) WHITE_PIX_DEFAULT (f) |
4ef0d4d0 | 4881 | |
ae8279db | 4882 | gc = XCreateGC (dpy, img->pixmap, 0, NULL); |
4ef0d4d0 KS |
4883 | XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f)); |
4884 | XDrawLine (dpy, img->pixmap, gc, 0, 0, | |
4885 | img->width - 1, img->height - 1); | |
4886 | XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1, | |
4887 | img->width - 1, 0); | |
4888 | XFreeGC (dpy, gc); | |
4889 | ||
4890 | if (img->mask) | |
4891 | { | |
ae8279db | 4892 | gc = XCreateGC (dpy, img->mask, 0, NULL); |
4ef0d4d0 KS |
4893 | XSetForeground (dpy, gc, MaskForeground (f)); |
4894 | XDrawLine (dpy, img->mask, gc, 0, 0, | |
4895 | img->width - 1, img->height - 1); | |
4896 | XDrawLine (dpy, img->mask, gc, 0, img->height - 1, | |
4897 | img->width - 1, 0); | |
4898 | XFreeGC (dpy, gc); | |
4899 | } | |
edfda783 | 4900 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4901 | #else |
4902 | HDC hdc, bmpdc; | |
4903 | HGDIOBJ prev; | |
4904 | ||
4905 | hdc = get_frame_dc (f); | |
4906 | bmpdc = CreateCompatibleDC (hdc); | |
4907 | release_frame_dc (f, hdc); | |
4908 | ||
4909 | prev = SelectObject (bmpdc, img->pixmap); | |
4910 | ||
4911 | SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f)); | |
4912 | MoveToEx (bmpdc, 0, 0, NULL); | |
4913 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4914 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4915 | LineTo (bmpdc, img->width - 1, 0); | |
4916 | ||
4917 | if (img->mask) | |
4918 | { | |
4919 | SelectObject (bmpdc, img->mask); | |
4920 | SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f)); | |
4921 | MoveToEx (bmpdc, 0, 0, NULL); | |
4922 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4923 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4924 | LineTo (bmpdc, img->width - 1, 0); | |
4925 | } | |
4926 | SelectObject (bmpdc, prev); | |
4927 | DeleteDC (bmpdc); | |
4928 | #endif /* HAVE_NTGUI */ | |
4929 | } | |
4930 | } | |
4931 | ||
4932 | ||
4933 | /* Build a mask for image IMG which is used on frame F. FILE is the | |
4934 | name of an image file, for error messages. HOW determines how to | |
4935 | determine the background color of IMG. If it is a list '(R G B)', | |
4936 | with R, G, and B being integers >= 0, take that as the color of the | |
4937 | background. Otherwise, determine the background color of IMG | |
4938 | heuristically. Value is non-zero if successful. */ | |
4939 | ||
4940 | static int | |
4941 | x_build_heuristic_mask (f, img, how) | |
4942 | struct frame *f; | |
4943 | struct image *img; | |
4944 | Lisp_Object how; | |
4945 | { | |
4946 | XImagePtr_or_DC ximg; | |
4947 | #ifndef HAVE_NTGUI | |
4948 | XImagePtr mask_img; | |
4949 | #else | |
4950 | HDC frame_dc; | |
4951 | HGDIOBJ prev; | |
4952 | char *mask_img; | |
4953 | int row_width; | |
4954 | #endif /* HAVE_NTGUI */ | |
4955 | int x, y, rc, use_img_background; | |
4956 | unsigned long bg = 0; | |
4957 | ||
4958 | if (img->mask) | |
4959 | { | |
4960 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
4961 | img->mask = NO_PIXMAP; | |
4962 | img->background_transparent_valid = 0; | |
4963 | } | |
4964 | ||
4965 | #ifndef HAVE_NTGUI | |
edfda783 | 4966 | #ifndef HAVE_NS |
4ef0d4d0 KS |
4967 | /* Create an image and pixmap serving as mask. */ |
4968 | rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1, | |
4969 | &mask_img, &img->mask); | |
4970 | if (!rc) | |
4971 | return 0; | |
edfda783 | 4972 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4973 | |
4974 | /* Get the X image of IMG->pixmap. */ | |
4975 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, 0, 0, | |
4976 | img->width, img->height, | |
4977 | ~0, ZPixmap); | |
4978 | #else | |
4979 | /* Create the bit array serving as mask. */ | |
4980 | row_width = (img->width + 7) / 8; | |
4981 | mask_img = xmalloc (row_width * img->height); | |
4982 | bzero (mask_img, row_width * img->height); | |
4983 | ||
4984 | /* Create a memory device context for IMG->pixmap. */ | |
4985 | frame_dc = get_frame_dc (f); | |
4986 | ximg = CreateCompatibleDC (frame_dc); | |
4987 | release_frame_dc (f, frame_dc); | |
4988 | prev = SelectObject (ximg, img->pixmap); | |
4989 | #endif /* HAVE_NTGUI */ | |
4990 | ||
4991 | /* Determine the background color of ximg. If HOW is `(R G B)' | |
4992 | take that as color. Otherwise, use the image's background color. */ | |
4993 | use_img_background = 1; | |
4994 | ||
4995 | if (CONSP (how)) | |
4996 | { | |
4997 | int rgb[3], i; | |
4998 | ||
4999 | for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i) | |
5000 | { | |
5001 | rgb[i] = XFASTINT (XCAR (how)) & 0xffff; | |
5002 | how = XCDR (how); | |
5003 | } | |
5004 | ||
5005 | if (i == 3 && NILP (how)) | |
5006 | { | |
5007 | char color_name[30]; | |
5008 | sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]); | |
5009 | bg = ( | |
5010 | #ifdef HAVE_NTGUI | |
5011 | 0x00ffffff & /* Filter out palette info. */ | |
5012 | #endif /* HAVE_NTGUI */ | |
5013 | x_alloc_image_color (f, img, build_string (color_name), 0)); | |
5014 | use_img_background = 0; | |
5015 | } | |
5016 | } | |
5017 | ||
5018 | if (use_img_background) | |
6ac3c7bb | 5019 | bg = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
5020 | |
5021 | /* Set all bits in mask_img to 1 whose color in ximg is different | |
5022 | from the background color bg. */ | |
5023 | #ifndef HAVE_NTGUI | |
5024 | for (y = 0; y < img->height; ++y) | |
5025 | for (x = 0; x < img->width; ++x) | |
edfda783 | 5026 | #ifndef HAVE_NS |
4ef0d4d0 | 5027 | XPutPixel (mask_img, x, y, (XGetPixel (ximg, x, y) != bg |
9ecf6403 | 5028 | ? PIX_MASK_DRAW : PIX_MASK_RETAIN)); |
edfda783 AR |
5029 | #else |
5030 | if (XGetPixel (ximg, x, y) == bg) | |
5031 | ns_set_alpha(ximg, x, y, 0); | |
5032 | #endif /* HAVE_NS */ | |
5033 | #ifndef HAVE_NS | |
4ef0d4d0 KS |
5034 | /* Fill in the background_transparent field while we have the mask handy. */ |
5035 | image_background_transparent (img, f, mask_img); | |
5036 | ||
5037 | /* Put mask_img into img->mask. */ | |
5038 | x_put_x_image (f, mask_img, img->mask, img->width, img->height); | |
5039 | x_destroy_x_image (mask_img); | |
edfda783 | 5040 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
5041 | #else |
5042 | for (y = 0; y < img->height; ++y) | |
5043 | for (x = 0; x < img->width; ++x) | |
5044 | { | |
5045 | COLORREF p = GetPixel (ximg, x, y); | |
5046 | if (p != bg) | |
5047 | mask_img[y * row_width + x / 8] |= 1 << (x % 8); | |
5048 | } | |
5049 | ||
5050 | /* Create the mask image. */ | |
5051 | img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height, | |
5052 | mask_img); | |
5053 | /* Fill in the background_transparent field while we have the mask handy. */ | |
5054 | SelectObject (ximg, img->mask); | |
5055 | image_background_transparent (img, f, ximg); | |
5056 | ||
5057 | /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */ | |
5058 | xfree (mask_img); | |
5059 | #endif /* HAVE_NTGUI */ | |
5060 | ||
5061 | Destroy_Image (ximg, prev); | |
5062 | ||
5063 | return 1; | |
5064 | } | |
5065 | ||
5066 | \f | |
5067 | /*********************************************************************** | |
5068 | PBM (mono, gray, color) | |
5069 | ***********************************************************************/ | |
5070 | ||
5071 | static int pbm_image_p P_ ((Lisp_Object object)); | |
5072 | static int pbm_load P_ ((struct frame *f, struct image *img)); | |
5073 | static int pbm_scan_number P_ ((unsigned char **, unsigned char *)); | |
5074 | ||
5075 | /* The symbol `pbm' identifying images of this type. */ | |
5076 | ||
5077 | Lisp_Object Qpbm; | |
5078 | ||
5079 | /* Indices of image specification fields in gs_format, below. */ | |
5080 | ||
5081 | enum pbm_keyword_index | |
5082 | { | |
5083 | PBM_TYPE, | |
5084 | PBM_FILE, | |
5085 | PBM_DATA, | |
5086 | PBM_ASCENT, | |
5087 | PBM_MARGIN, | |
5088 | PBM_RELIEF, | |
5089 | PBM_ALGORITHM, | |
5090 | PBM_HEURISTIC_MASK, | |
5091 | PBM_MASK, | |
5092 | PBM_FOREGROUND, | |
5093 | PBM_BACKGROUND, | |
5094 | PBM_LAST | |
5095 | }; | |
5096 | ||
5097 | /* Vector of image_keyword structures describing the format | |
5098 | of valid user-defined image specifications. */ | |
5099 | ||
91433552 | 5100 | static const struct image_keyword pbm_format[PBM_LAST] = |
4ef0d4d0 KS |
5101 | { |
5102 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5103 | {":file", IMAGE_STRING_VALUE, 0}, | |
5104 | {":data", IMAGE_STRING_VALUE, 0}, | |
5105 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
5106 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
5107 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
5108 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5109 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5110 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5111 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
5112 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5113 | }; | |
5114 | ||
5115 | /* Structure describing the image type `pbm'. */ | |
5116 | ||
5117 | static struct image_type pbm_type = | |
5118 | { | |
5119 | &Qpbm, | |
5120 | pbm_image_p, | |
5121 | pbm_load, | |
5122 | x_clear_image, | |
5123 | NULL | |
5124 | }; | |
5125 | ||
5126 | ||
5127 | /* Return non-zero if OBJECT is a valid PBM image specification. */ | |
5128 | ||
5129 | static int | |
5130 | pbm_image_p (object) | |
5131 | Lisp_Object object; | |
5132 | { | |
5133 | struct image_keyword fmt[PBM_LAST]; | |
5134 | ||
5135 | bcopy (pbm_format, fmt, sizeof fmt); | |
5136 | ||
5137 | if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)) | |
5138 | return 0; | |
5139 | ||
5140 | /* Must specify either :data or :file. */ | |
5141 | return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1; | |
5142 | } | |
5143 | ||
5144 | ||
5145 | /* Scan a decimal number from *S and return it. Advance *S while | |
5146 | reading the number. END is the end of the string. Value is -1 at | |
5147 | end of input. */ | |
5148 | ||
5149 | static int | |
5150 | pbm_scan_number (s, end) | |
5151 | unsigned char **s, *end; | |
5152 | { | |
5153 | int c = 0, val = -1; | |
5154 | ||
5155 | while (*s < end) | |
5156 | { | |
5157 | /* Skip white-space. */ | |
5158 | while (*s < end && (c = *(*s)++, isspace (c))) | |
5159 | ; | |
5160 | ||
5161 | if (c == '#') | |
5162 | { | |
5163 | /* Skip comment to end of line. */ | |
5164 | while (*s < end && (c = *(*s)++, c != '\n')) | |
5165 | ; | |
5166 | } | |
5167 | else if (isdigit (c)) | |
5168 | { | |
5169 | /* Read decimal number. */ | |
5170 | val = c - '0'; | |
5171 | while (*s < end && (c = *(*s)++, isdigit (c))) | |
5172 | val = 10 * val + c - '0'; | |
5173 | break; | |
5174 | } | |
5175 | else | |
5176 | break; | |
5177 | } | |
5178 | ||
5179 | return val; | |
5180 | } | |
5181 | ||
5182 | ||
5183 | #ifdef HAVE_NTGUI | |
5184 | #if 0 /* Unused. ++kfs */ | |
5185 | ||
5186 | /* Read FILE into memory. Value is a pointer to a buffer allocated | |
5187 | with xmalloc holding FILE's contents. Value is null if an error | |
5188 | occurred. *SIZE is set to the size of the file. */ | |
5189 | ||
5190 | static char * | |
5191 | pbm_read_file (file, size) | |
5192 | Lisp_Object file; | |
5193 | int *size; | |
5194 | { | |
5195 | FILE *fp = NULL; | |
5196 | char *buf = NULL; | |
5197 | struct stat st; | |
5198 | ||
5199 | if (stat (SDATA (file), &st) == 0 | |
5200 | && (fp = fopen (SDATA (file), "rb")) != NULL | |
5201 | && (buf = (char *) xmalloc (st.st_size), | |
5202 | fread (buf, 1, st.st_size, fp) == st.st_size)) | |
5203 | { | |
5204 | *size = st.st_size; | |
5205 | fclose (fp); | |
5206 | } | |
5207 | else | |
5208 | { | |
5209 | if (fp) | |
5210 | fclose (fp); | |
5211 | if (buf) | |
5212 | { | |
5213 | xfree (buf); | |
5214 | buf = NULL; | |
5215 | } | |
5216 | } | |
5217 | ||
5218 | return buf; | |
5219 | } | |
5220 | #endif | |
5221 | #endif /* HAVE_NTGUI */ | |
5222 | ||
5223 | /* Load PBM image IMG for use on frame F. */ | |
5224 | ||
5225 | static int | |
5226 | pbm_load (f, img) | |
5227 | struct frame *f; | |
5228 | struct image *img; | |
5229 | { | |
5230 | int raw_p, x, y; | |
5231 | int width, height, max_color_idx = 0; | |
5232 | XImagePtr ximg; | |
5233 | Lisp_Object file, specified_file; | |
5234 | enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type; | |
5235 | struct gcpro gcpro1; | |
5236 | unsigned char *contents = NULL; | |
5237 | unsigned char *end, *p; | |
5238 | int size; | |
5239 | ||
5240 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
5241 | file = Qnil; | |
5242 | GCPRO1 (file); | |
5243 | ||
5244 | if (STRINGP (specified_file)) | |
5245 | { | |
5246 | file = x_find_image_file (specified_file); | |
5247 | if (!STRINGP (file)) | |
5248 | { | |
5249 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
5250 | UNGCPRO; | |
5251 | return 0; | |
5252 | } | |
5253 | ||
5254 | contents = slurp_file (SDATA (file), &size); | |
5255 | if (contents == NULL) | |
5256 | { | |
5257 | image_error ("Error reading `%s'", file, Qnil); | |
5258 | UNGCPRO; | |
5259 | return 0; | |
5260 | } | |
5261 | ||
5262 | p = contents; | |
5263 | end = contents + size; | |
5264 | } | |
5265 | else | |
5266 | { | |
5267 | Lisp_Object data; | |
5268 | data = image_spec_value (img->spec, QCdata, NULL); | |
5269 | p = SDATA (data); | |
5270 | end = p + SBYTES (data); | |
5271 | } | |
5272 | ||
5273 | /* Check magic number. */ | |
5274 | if (end - p < 2 || *p++ != 'P') | |
5275 | { | |
5276 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5277 | error: | |
5278 | xfree (contents); | |
5279 | UNGCPRO; | |
5280 | return 0; | |
5281 | } | |
5282 | ||
5283 | switch (*p++) | |
5284 | { | |
5285 | case '1': | |
5286 | raw_p = 0, type = PBM_MONO; | |
5287 | break; | |
5288 | ||
5289 | case '2': | |
5290 | raw_p = 0, type = PBM_GRAY; | |
5291 | break; | |
5292 | ||
5293 | case '3': | |
5294 | raw_p = 0, type = PBM_COLOR; | |
5295 | break; | |
5296 | ||
5297 | case '4': | |
5298 | raw_p = 1, type = PBM_MONO; | |
5299 | break; | |
5300 | ||
5301 | case '5': | |
5302 | raw_p = 1, type = PBM_GRAY; | |
5303 | break; | |
5304 | ||
5305 | case '6': | |
5306 | raw_p = 1, type = PBM_COLOR; | |
5307 | break; | |
5308 | ||
5309 | default: | |
5310 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5311 | goto error; | |
5312 | } | |
5313 | ||
5314 | /* Read width, height, maximum color-component. Characters | |
5315 | starting with `#' up to the end of a line are ignored. */ | |
5316 | width = pbm_scan_number (&p, end); | |
5317 | height = pbm_scan_number (&p, end); | |
5318 | ||
5319 | if (type != PBM_MONO) | |
5320 | { | |
5321 | max_color_idx = pbm_scan_number (&p, end); | |
b9c89e11 JR |
5322 | if (max_color_idx > 65535 || max_color_idx < 0) |
5323 | { | |
5324 | image_error ("Unsupported maximum PBM color value", Qnil, Qnil); | |
5325 | goto error; | |
5326 | } | |
4ef0d4d0 KS |
5327 | } |
5328 | ||
b9c89e11 JR |
5329 | if (!check_image_size (f, width, height)) |
5330 | { | |
10ea2b82 | 5331 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
b9c89e11 JR |
5332 | goto error; |
5333 | } | |
4ef0d4d0 KS |
5334 | |
5335 | if (!x_create_x_image_and_pixmap (f, width, height, 0, | |
5336 | &ximg, &img->pixmap)) | |
5337 | goto error; | |
5338 | ||
5339 | /* Initialize the color hash table. */ | |
5340 | init_color_table (); | |
5341 | ||
5342 | if (type == PBM_MONO) | |
5343 | { | |
5344 | int c = 0, g; | |
5345 | struct image_keyword fmt[PBM_LAST]; | |
5346 | unsigned long fg = FRAME_FOREGROUND_PIXEL (f); | |
5347 | unsigned long bg = FRAME_BACKGROUND_PIXEL (f); | |
5348 | ||
5349 | /* Parse the image specification. */ | |
5350 | bcopy (pbm_format, fmt, sizeof fmt); | |
5351 | parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm); | |
5352 | ||
5353 | /* Get foreground and background colors, maybe allocate colors. */ | |
5354 | if (fmt[PBM_FOREGROUND].count | |
5355 | && STRINGP (fmt[PBM_FOREGROUND].value)) | |
5356 | fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg); | |
5357 | if (fmt[PBM_BACKGROUND].count | |
5358 | && STRINGP (fmt[PBM_BACKGROUND].value)) | |
5359 | { | |
5360 | bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg); | |
5361 | img->background = bg; | |
5362 | img->background_valid = 1; | |
5363 | } | |
5364 | ||
5365 | for (y = 0; y < height; ++y) | |
5366 | for (x = 0; x < width; ++x) | |
5367 | { | |
5368 | if (raw_p) | |
5369 | { | |
5370 | if ((x & 7) == 0) | |
c295e710 CY |
5371 | { |
5372 | if (p >= end) | |
5373 | { | |
5374 | x_destroy_x_image (ximg); | |
5375 | x_clear_image (f, img); | |
5376 | image_error ("Invalid image size in image `%s'", | |
5377 | img->spec, Qnil); | |
5378 | goto error; | |
5379 | } | |
5380 | c = *p++; | |
5381 | } | |
4ef0d4d0 KS |
5382 | g = c & 0x80; |
5383 | c <<= 1; | |
5384 | } | |
5385 | else | |
5386 | g = pbm_scan_number (&p, end); | |
5387 | ||
5388 | XPutPixel (ximg, x, y, g ? fg : bg); | |
5389 | } | |
5390 | } | |
5391 | else | |
5392 | { | |
b9c89e11 JR |
5393 | int expected_size = height * width; |
5394 | if (max_color_idx > 255) | |
5395 | expected_size *= 2; | |
5396 | if (type == PBM_COLOR) | |
5397 | expected_size *= 3; | |
5398 | ||
5399 | if (raw_p && p + expected_size > end) | |
39e653ea CY |
5400 | { |
5401 | x_destroy_x_image (ximg); | |
fb0b0862 | 5402 | x_clear_image (f, img); |
39e653ea CY |
5403 | image_error ("Invalid image size in image `%s'", |
5404 | img->spec, Qnil); | |
5405 | goto error; | |
5406 | } | |
5407 | ||
4ef0d4d0 KS |
5408 | for (y = 0; y < height; ++y) |
5409 | for (x = 0; x < width; ++x) | |
5410 | { | |
5411 | int r, g, b; | |
5412 | ||
b9c89e11 JR |
5413 | if (type == PBM_GRAY && raw_p) |
5414 | { | |
5415 | r = g = b = *p++; | |
5416 | if (max_color_idx > 255) | |
5417 | r = g = b = r * 256 + *p++; | |
5418 | } | |
5419 | else if (type == PBM_GRAY) | |
5420 | r = g = b = pbm_scan_number (&p, end); | |
4ef0d4d0 KS |
5421 | else if (raw_p) |
5422 | { | |
5423 | r = *p++; | |
b9c89e11 JR |
5424 | if (max_color_idx > 255) |
5425 | r = r * 256 + *p++; | |
4ef0d4d0 | 5426 | g = *p++; |
b9c89e11 JR |
5427 | if (max_color_idx > 255) |
5428 | g = g * 256 + *p++; | |
4ef0d4d0 | 5429 | b = *p++; |
b9c89e11 JR |
5430 | if (max_color_idx > 255) |
5431 | b = b * 256 + *p++; | |
4ef0d4d0 KS |
5432 | } |
5433 | else | |
5434 | { | |
5435 | r = pbm_scan_number (&p, end); | |
5436 | g = pbm_scan_number (&p, end); | |
5437 | b = pbm_scan_number (&p, end); | |
5438 | } | |
5439 | ||
5440 | if (r < 0 || g < 0 || b < 0) | |
5441 | { | |
5442 | x_destroy_x_image (ximg); | |
5443 | image_error ("Invalid pixel value in image `%s'", | |
5444 | img->spec, Qnil); | |
5445 | goto error; | |
5446 | } | |
5447 | ||
5448 | /* RGB values are now in the range 0..max_color_idx. | |
5449 | Scale this to the range 0..0xffff supported by X. */ | |
5450 | r = (double) r * 65535 / max_color_idx; | |
5451 | g = (double) g * 65535 / max_color_idx; | |
5452 | b = (double) b * 65535 / max_color_idx; | |
5453 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
5454 | } | |
5455 | } | |
5456 | ||
5457 | #ifdef COLOR_TABLE_SUPPORT | |
5458 | /* Store in IMG->colors the colors allocated for the image, and | |
5459 | free the color table. */ | |
5460 | img->colors = colors_in_color_table (&img->ncolors); | |
5461 | free_color_table (); | |
5462 | #endif /* COLOR_TABLE_SUPPORT */ | |
5463 | ||
5464 | img->width = width; | |
5465 | img->height = height; | |
5466 | ||
5467 | /* Maybe fill in the background field while we have ximg handy. */ | |
5468 | ||
5469 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
5470 | /* Casting avoids a GCC warning. */ |
5471 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
5472 | |
5473 | /* Put the image into a pixmap. */ | |
5474 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
5475 | x_destroy_x_image (ximg); | |
5476 | ||
5477 | /* X and W32 versions did it here, MAC version above. ++kfs | |
1f026899 | 5478 | img->width = width; |
4ef0d4d0 KS |
5479 | img->height = height; */ |
5480 | ||
5481 | UNGCPRO; | |
5482 | xfree (contents); | |
5483 | return 1; | |
5484 | } | |
5485 | ||
5486 | \f | |
5487 | /*********************************************************************** | |
5488 | PNG | |
5489 | ***********************************************************************/ | |
5490 | ||
9e2a2647 | 5491 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
4ef0d4d0 KS |
5492 | |
5493 | /* Function prototypes. */ | |
5494 | ||
5495 | static int png_image_p P_ ((Lisp_Object object)); | |
5496 | static int png_load P_ ((struct frame *f, struct image *img)); | |
5497 | ||
5498 | /* The symbol `png' identifying images of this type. */ | |
5499 | ||
5500 | Lisp_Object Qpng; | |
5501 | ||
5502 | /* Indices of image specification fields in png_format, below. */ | |
5503 | ||
5504 | enum png_keyword_index | |
5505 | { | |
5506 | PNG_TYPE, | |
5507 | PNG_DATA, | |
5508 | PNG_FILE, | |
5509 | PNG_ASCENT, | |
5510 | PNG_MARGIN, | |
5511 | PNG_RELIEF, | |
5512 | PNG_ALGORITHM, | |
5513 | PNG_HEURISTIC_MASK, | |
5514 | PNG_MASK, | |
5515 | PNG_BACKGROUND, | |
5516 | PNG_LAST | |
5517 | }; | |
5518 | ||
5519 | /* Vector of image_keyword structures describing the format | |
5520 | of valid user-defined image specifications. */ | |
5521 | ||
91433552 | 5522 | static const struct image_keyword png_format[PNG_LAST] = |
4ef0d4d0 KS |
5523 | { |
5524 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5525 | {":data", IMAGE_STRING_VALUE, 0}, | |
5526 | {":file", IMAGE_STRING_VALUE, 0}, | |
5527 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
5528 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
5529 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
5530 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5531 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5532 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5533 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5534 | }; | |
5535 | ||
5536 | /* Structure describing the image type `png'. */ | |
5537 | ||
5538 | static struct image_type png_type = | |
5539 | { | |
5540 | &Qpng, | |
5541 | png_image_p, | |
5542 | png_load, | |
5543 | x_clear_image, | |
5544 | NULL | |
5545 | }; | |
5546 | ||
5547 | /* Return non-zero if OBJECT is a valid PNG image specification. */ | |
5548 | ||
5549 | static int | |
5550 | png_image_p (object) | |
5551 | Lisp_Object object; | |
5552 | { | |
5553 | struct image_keyword fmt[PNG_LAST]; | |
5554 | bcopy (png_format, fmt, sizeof fmt); | |
5555 | ||
5556 | if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)) | |
5557 | return 0; | |
5558 | ||
5559 | /* Must specify either the :data or :file keyword. */ | |
5560 | return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1; | |
5561 | } | |
5562 | ||
9e2a2647 | 5563 | #endif /* HAVE_PNG || HAVE_NS */ |
4ef0d4d0 KS |
5564 | |
5565 | ||
5566 | #ifdef HAVE_PNG | |
5567 | ||
4ef0d4d0 KS |
5568 | #ifdef HAVE_NTGUI |
5569 | /* PNG library details. */ | |
5570 | ||
5571 | DEF_IMGLIB_FN (png_get_io_ptr); | |
285d07e2 | 5572 | DEF_IMGLIB_FN (png_sig_cmp); |
4ef0d4d0 KS |
5573 | DEF_IMGLIB_FN (png_create_read_struct); |
5574 | DEF_IMGLIB_FN (png_create_info_struct); | |
5575 | DEF_IMGLIB_FN (png_destroy_read_struct); | |
5576 | DEF_IMGLIB_FN (png_set_read_fn); | |
4ef0d4d0 KS |
5577 | DEF_IMGLIB_FN (png_set_sig_bytes); |
5578 | DEF_IMGLIB_FN (png_read_info); | |
5579 | DEF_IMGLIB_FN (png_get_IHDR); | |
5580 | DEF_IMGLIB_FN (png_get_valid); | |
5581 | DEF_IMGLIB_FN (png_set_strip_16); | |
5582 | DEF_IMGLIB_FN (png_set_expand); | |
5583 | DEF_IMGLIB_FN (png_set_gray_to_rgb); | |
5584 | DEF_IMGLIB_FN (png_set_background); | |
5585 | DEF_IMGLIB_FN (png_get_bKGD); | |
5586 | DEF_IMGLIB_FN (png_read_update_info); | |
5587 | DEF_IMGLIB_FN (png_get_channels); | |
5588 | DEF_IMGLIB_FN (png_get_rowbytes); | |
5589 | DEF_IMGLIB_FN (png_read_image); | |
5590 | DEF_IMGLIB_FN (png_read_end); | |
5591 | DEF_IMGLIB_FN (png_error); | |
5592 | ||
5593 | static int | |
0855eb52 | 5594 | init_png_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
5595 | { |
5596 | HMODULE library; | |
5597 | ||
4ef0d4d0 | 5598 | /* Try loading libpng under probable names. */ |
0855eb52 | 5599 | if (!(library = w32_delayed_load (libraries, Qpng))) |
4ef0d4d0 KS |
5600 | return 0; |
5601 | ||
5602 | LOAD_IMGLIB_FN (library, png_get_io_ptr); | |
285d07e2 | 5603 | LOAD_IMGLIB_FN (library, png_sig_cmp); |
4ef0d4d0 KS |
5604 | LOAD_IMGLIB_FN (library, png_create_read_struct); |
5605 | LOAD_IMGLIB_FN (library, png_create_info_struct); | |
5606 | LOAD_IMGLIB_FN (library, png_destroy_read_struct); | |
5607 | LOAD_IMGLIB_FN (library, png_set_read_fn); | |
4ef0d4d0 KS |
5608 | LOAD_IMGLIB_FN (library, png_set_sig_bytes); |
5609 | LOAD_IMGLIB_FN (library, png_read_info); | |
5610 | LOAD_IMGLIB_FN (library, png_get_IHDR); | |
5611 | LOAD_IMGLIB_FN (library, png_get_valid); | |
5612 | LOAD_IMGLIB_FN (library, png_set_strip_16); | |
5613 | LOAD_IMGLIB_FN (library, png_set_expand); | |
5614 | LOAD_IMGLIB_FN (library, png_set_gray_to_rgb); | |
5615 | LOAD_IMGLIB_FN (library, png_set_background); | |
5616 | LOAD_IMGLIB_FN (library, png_get_bKGD); | |
5617 | LOAD_IMGLIB_FN (library, png_read_update_info); | |
5618 | LOAD_IMGLIB_FN (library, png_get_channels); | |
5619 | LOAD_IMGLIB_FN (library, png_get_rowbytes); | |
5620 | LOAD_IMGLIB_FN (library, png_read_image); | |
5621 | LOAD_IMGLIB_FN (library, png_read_end); | |
5622 | LOAD_IMGLIB_FN (library, png_error); | |
5623 | return 1; | |
5624 | } | |
5625 | #else | |
5626 | ||
5627 | #define fn_png_get_io_ptr png_get_io_ptr | |
285d07e2 | 5628 | #define fn_png_sig_cmp png_sig_cmp |
4ef0d4d0 KS |
5629 | #define fn_png_create_read_struct png_create_read_struct |
5630 | #define fn_png_create_info_struct png_create_info_struct | |
5631 | #define fn_png_destroy_read_struct png_destroy_read_struct | |
5632 | #define fn_png_set_read_fn png_set_read_fn | |
4ef0d4d0 KS |
5633 | #define fn_png_set_sig_bytes png_set_sig_bytes |
5634 | #define fn_png_read_info png_read_info | |
5635 | #define fn_png_get_IHDR png_get_IHDR | |
5636 | #define fn_png_get_valid png_get_valid | |
5637 | #define fn_png_set_strip_16 png_set_strip_16 | |
5638 | #define fn_png_set_expand png_set_expand | |
5639 | #define fn_png_set_gray_to_rgb png_set_gray_to_rgb | |
5640 | #define fn_png_set_background png_set_background | |
5641 | #define fn_png_get_bKGD png_get_bKGD | |
5642 | #define fn_png_read_update_info png_read_update_info | |
5643 | #define fn_png_get_channels png_get_channels | |
5644 | #define fn_png_get_rowbytes png_get_rowbytes | |
5645 | #define fn_png_read_image png_read_image | |
5646 | #define fn_png_read_end png_read_end | |
5647 | #define fn_png_error png_error | |
5648 | ||
5649 | #endif /* HAVE_NTGUI */ | |
5650 | ||
5651 | /* Error and warning handlers installed when the PNG library | |
5652 | is initialized. */ | |
5653 | ||
5654 | static void | |
5655 | my_png_error (png_ptr, msg) | |
5656 | png_struct *png_ptr; | |
5657 | char *msg; | |
5658 | { | |
5659 | xassert (png_ptr != NULL); | |
5be1c984 EZ |
5660 | /* Avoid compiler warning about deprecated direct access to |
5661 | png_ptr's fields in libpng versions 1.4.x. */ | |
4ef0d4d0 | 5662 | image_error ("PNG error: %s", build_string (msg), Qnil); |
fb724e55 | 5663 | longjmp (png_ptr->jmpbuf, 1); |
4ef0d4d0 KS |
5664 | } |
5665 | ||
5666 | ||
5667 | static void | |
5668 | my_png_warning (png_ptr, msg) | |
5669 | png_struct *png_ptr; | |
5670 | char *msg; | |
5671 | { | |
5672 | xassert (png_ptr != NULL); | |
5673 | image_error ("PNG warning: %s", build_string (msg), Qnil); | |
5674 | } | |
5675 | ||
5676 | /* Memory source for PNG decoding. */ | |
5677 | ||
5678 | struct png_memory_storage | |
5679 | { | |
5680 | unsigned char *bytes; /* The data */ | |
5681 | size_t len; /* How big is it? */ | |
5682 | int index; /* Where are we? */ | |
5683 | }; | |
5684 | ||
5685 | ||
5686 | /* Function set as reader function when reading PNG image from memory. | |
5687 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5688 | bytes from the input to DATA. */ | |
5689 | ||
5690 | static void | |
5691 | png_read_from_memory (png_ptr, data, length) | |
5692 | png_structp png_ptr; | |
5693 | png_bytep data; | |
5694 | png_size_t length; | |
5695 | { | |
5696 | struct png_memory_storage *tbr | |
5697 | = (struct png_memory_storage *) fn_png_get_io_ptr (png_ptr); | |
5698 | ||
5699 | if (length > tbr->len - tbr->index) | |
5700 | fn_png_error (png_ptr, "Read error"); | |
5701 | ||
5702 | bcopy (tbr->bytes + tbr->index, data, length); | |
5703 | tbr->index = tbr->index + length; | |
5704 | } | |
5705 | ||
b0da69a7 | 5706 | |
9a22a004 JB |
5707 | /* Function set as reader function when reading PNG image from a file. |
5708 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5709 | bytes from the input to DATA. */ | |
5710 | ||
5711 | static void | |
5712 | png_read_from_file (png_ptr, data, length) | |
5713 | png_structp png_ptr; | |
5714 | png_bytep data; | |
5715 | png_size_t length; | |
5716 | { | |
5717 | FILE *fp = (FILE *) fn_png_get_io_ptr (png_ptr); | |
5718 | ||
5719 | if (fread (data, 1, length, fp) < length) | |
5720 | fn_png_error (png_ptr, "Read error"); | |
5721 | } | |
5722 | ||
5723 | ||
4ef0d4d0 KS |
5724 | /* Load PNG image IMG for use on frame F. Value is non-zero if |
5725 | successful. */ | |
5726 | ||
5727 | static int | |
5728 | png_load (f, img) | |
5729 | struct frame *f; | |
5730 | struct image *img; | |
5731 | { | |
5732 | Lisp_Object file, specified_file; | |
5733 | Lisp_Object specified_data; | |
5734 | int x, y, i; | |
5735 | XImagePtr ximg, mask_img = NULL; | |
5736 | struct gcpro gcpro1; | |
5737 | png_struct *png_ptr = NULL; | |
5738 | png_info *info_ptr = NULL, *end_info = NULL; | |
5739 | FILE *volatile fp = NULL; | |
5740 | png_byte sig[8]; | |
5741 | png_byte * volatile pixels = NULL; | |
5742 | png_byte ** volatile rows = NULL; | |
5743 | png_uint_32 width, height; | |
5744 | int bit_depth, color_type, interlace_type; | |
5745 | png_byte channels; | |
5746 | png_uint_32 row_bytes; | |
5747 | int transparent_p; | |
4ef0d4d0 KS |
5748 | struct png_memory_storage tbr; /* Data to be read */ |
5749 | ||
5750 | /* Find out what file to load. */ | |
5751 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
5752 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
5753 | file = Qnil; | |
5754 | GCPRO1 (file); | |
5755 | ||
5756 | if (NILP (specified_data)) | |
5757 | { | |
5758 | file = x_find_image_file (specified_file); | |
5759 | if (!STRINGP (file)) | |
5760 | { | |
5761 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
5762 | UNGCPRO; | |
5763 | return 0; | |
5764 | } | |
5765 | ||
5766 | /* Open the image file. */ | |
5767 | fp = fopen (SDATA (file), "rb"); | |
5768 | if (!fp) | |
5769 | { | |
5770 | image_error ("Cannot open image file `%s'", file, Qnil); | |
5771 | UNGCPRO; | |
4ef0d4d0 KS |
5772 | return 0; |
5773 | } | |
5774 | ||
5775 | /* Check PNG signature. */ | |
5776 | if (fread (sig, 1, sizeof sig, fp) != sizeof sig | |
285d07e2 | 5777 | || fn_png_sig_cmp (sig, 0, sizeof sig)) |
4ef0d4d0 KS |
5778 | { |
5779 | image_error ("Not a PNG file: `%s'", file, Qnil); | |
5780 | UNGCPRO; | |
5781 | fclose (fp); | |
5782 | return 0; | |
5783 | } | |
5784 | } | |
5785 | else | |
5786 | { | |
5787 | /* Read from memory. */ | |
5788 | tbr.bytes = SDATA (specified_data); | |
5789 | tbr.len = SBYTES (specified_data); | |
5790 | tbr.index = 0; | |
5791 | ||
5792 | /* Check PNG signature. */ | |
5793 | if (tbr.len < sizeof sig | |
285d07e2 | 5794 | || fn_png_sig_cmp (tbr.bytes, 0, sizeof sig)) |
4ef0d4d0 KS |
5795 | { |
5796 | image_error ("Not a PNG image: `%s'", img->spec, Qnil); | |
5797 | UNGCPRO; | |
5798 | return 0; | |
5799 | } | |
5800 | ||
5801 | /* Need to skip past the signature. */ | |
5802 | tbr.bytes += sizeof (sig); | |
5803 | } | |
5804 | ||
2e09fef1 EZ |
5805 | /* Initialize read and info structs for PNG lib. Casting return |
5806 | value avoids a GCC warning on W32. */ | |
5807 | png_ptr = (png_structp)fn_png_create_read_struct (PNG_LIBPNG_VER_STRING, | |
5808 | NULL, my_png_error, | |
5809 | my_png_warning); | |
4ef0d4d0 KS |
5810 | if (!png_ptr) |
5811 | { | |
5812 | if (fp) fclose (fp); | |
5813 | UNGCPRO; | |
5814 | return 0; | |
5815 | } | |
5816 | ||
2e09fef1 EZ |
5817 | /* Casting return value avoids a GCC warning on W32. */ |
5818 | info_ptr = (png_infop)fn_png_create_info_struct (png_ptr); | |
4ef0d4d0 KS |
5819 | if (!info_ptr) |
5820 | { | |
5821 | fn_png_destroy_read_struct (&png_ptr, NULL, NULL); | |
5822 | if (fp) fclose (fp); | |
5823 | UNGCPRO; | |
5824 | return 0; | |
5825 | } | |
5826 | ||
2e09fef1 EZ |
5827 | /* Casting return value avoids a GCC warning on W32. */ |
5828 | end_info = (png_infop)fn_png_create_info_struct (png_ptr); | |
4ef0d4d0 KS |
5829 | if (!end_info) |
5830 | { | |
5831 | fn_png_destroy_read_struct (&png_ptr, &info_ptr, NULL); | |
5832 | if (fp) fclose (fp); | |
5833 | UNGCPRO; | |
5834 | return 0; | |
5835 | } | |
5836 | ||
5837 | /* Set error jump-back. We come back here when the PNG library | |
5838 | detects an error. */ | |
fb724e55 | 5839 | if (setjmp (png_ptr->jmpbuf)) |
4ef0d4d0 KS |
5840 | { |
5841 | error: | |
5842 | if (png_ptr) | |
5843 | fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); | |
5844 | xfree (pixels); | |
5845 | xfree (rows); | |
5846 | if (fp) fclose (fp); | |
5847 | UNGCPRO; | |
5848 | return 0; | |
5849 | } | |
5850 | ||
5851 | /* Read image info. */ | |
5852 | if (!NILP (specified_data)) | |
5853 | fn_png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory); | |
5854 | else | |
9a22a004 | 5855 | fn_png_set_read_fn (png_ptr, (void *) fp, png_read_from_file); |
4ef0d4d0 KS |
5856 | |
5857 | fn_png_set_sig_bytes (png_ptr, sizeof sig); | |
5858 | fn_png_read_info (png_ptr, info_ptr); | |
5859 | fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, | |
5860 | &interlace_type, NULL, NULL); | |
5861 | ||
f1f25b99 | 5862 | if (!check_image_size (f, width, height)) |
10ea2b82 JR |
5863 | { |
5864 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
5865 | goto error; | |
5866 | } | |
4ef0d4d0 KS |
5867 | /* If image contains simply transparency data, we prefer to |
5868 | construct a clipping mask. */ | |
5869 | if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) | |
5870 | transparent_p = 1; | |
5871 | else | |
5872 | transparent_p = 0; | |
5873 | ||
5874 | /* This function is easier to write if we only have to handle | |
5875 | one data format: RGB or RGBA with 8 bits per channel. Let's | |
5876 | transform other formats into that format. */ | |
5877 | ||
5878 | /* Strip more than 8 bits per channel. */ | |
5879 | if (bit_depth == 16) | |
5880 | fn_png_set_strip_16 (png_ptr); | |
5881 | ||
5882 | /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel | |
5883 | if available. */ | |
5884 | fn_png_set_expand (png_ptr); | |
5885 | ||
5886 | /* Convert grayscale images to RGB. */ | |
5887 | if (color_type == PNG_COLOR_TYPE_GRAY | |
5888 | || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | |
5889 | fn_png_set_gray_to_rgb (png_ptr); | |
5890 | ||
4ef0d4d0 KS |
5891 | /* Handle alpha channel by combining the image with a background |
5892 | color. Do this only if a real alpha channel is supplied. For | |
5893 | simple transparency, we prefer a clipping mask. */ | |
5894 | if (!transparent_p) | |
5895 | { | |
917ac1a9 | 5896 | /* png_color_16 *image_bg; */ |
4ef0d4d0 KS |
5897 | Lisp_Object specified_bg |
5898 | = image_spec_value (img->spec, QCbackground, NULL); | |
c4f13f44 | 5899 | int shift = (bit_depth == 16) ? 0 : 8; |
4ef0d4d0 KS |
5900 | |
5901 | if (STRINGP (specified_bg)) | |
5902 | /* The user specified `:background', use that. */ | |
5903 | { | |
4ef0d4d0 KS |
5904 | XColor color; |
5905 | if (x_defined_color (f, SDATA (specified_bg), &color, 0)) | |
5906 | { | |
5907 | png_color_16 user_bg; | |
5908 | ||
5909 | bzero (&user_bg, sizeof user_bg); | |
c4f13f44 CY |
5910 | user_bg.red = color.red >> shift; |
5911 | user_bg.green = color.green >> shift; | |
5912 | user_bg.blue = color.blue >> shift; | |
4ef0d4d0 KS |
5913 | |
5914 | fn_png_set_background (png_ptr, &user_bg, | |
5915 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
5916 | } | |
5917 | } | |
4ef0d4d0 KS |
5918 | else |
5919 | { | |
c4f13f44 CY |
5920 | /* We use the current frame background, ignoring any default |
5921 | background color set by the image. */ | |
8b7d0a16 | 5922 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
4ef0d4d0 KS |
5923 | XColor color; |
5924 | png_color_16 frame_background; | |
5925 | ||
5926 | color.pixel = FRAME_BACKGROUND_PIXEL (f); | |
5927 | x_query_color (f, &color); | |
5928 | ||
5929 | bzero (&frame_background, sizeof frame_background); | |
c4f13f44 CY |
5930 | frame_background.red = color.red >> shift; |
5931 | frame_background.green = color.green >> shift; | |
5932 | frame_background.blue = color.blue >> shift; | |
4ef0d4d0 KS |
5933 | #endif /* HAVE_X_WINDOWS */ |
5934 | ||
4ef0d4d0 KS |
5935 | fn_png_set_background (png_ptr, &frame_background, |
5936 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
5937 | } | |
5938 | } | |
5939 | ||
5940 | /* Update info structure. */ | |
5941 | fn_png_read_update_info (png_ptr, info_ptr); | |
5942 | ||
5943 | /* Get number of channels. Valid values are 1 for grayscale images | |
5944 | and images with a palette, 2 for grayscale images with transparency | |
5945 | information (alpha channel), 3 for RGB images, and 4 for RGB | |
5946 | images with alpha channel, i.e. RGBA. If conversions above were | |
5947 | sufficient we should only have 3 or 4 channels here. */ | |
5948 | channels = fn_png_get_channels (png_ptr, info_ptr); | |
5949 | xassert (channels == 3 || channels == 4); | |
5950 | ||
5951 | /* Number of bytes needed for one row of the image. */ | |
5952 | row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr); | |
5953 | ||
5954 | /* Allocate memory for the image. */ | |
5955 | pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels); | |
5956 | rows = (png_byte **) xmalloc (height * sizeof *rows); | |
5957 | for (i = 0; i < height; ++i) | |
5958 | rows[i] = pixels + i * row_bytes; | |
5959 | ||
5960 | /* Read the entire image. */ | |
5961 | fn_png_read_image (png_ptr, rows); | |
5962 | fn_png_read_end (png_ptr, info_ptr); | |
5963 | if (fp) | |
5964 | { | |
5965 | fclose (fp); | |
5966 | fp = NULL; | |
5967 | } | |
5968 | ||
5969 | /* Create the X image and pixmap. */ | |
5970 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, | |
5971 | &img->pixmap)) | |
5972 | goto error; | |
5973 | ||
5974 | /* Create an image and pixmap serving as mask if the PNG image | |
5975 | contains an alpha channel. */ | |
5976 | if (channels == 4 | |
5977 | && !transparent_p | |
5978 | && !x_create_x_image_and_pixmap (f, width, height, 1, | |
5979 | &mask_img, &img->mask)) | |
5980 | { | |
5981 | x_destroy_x_image (ximg); | |
5982 | Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap); | |
5983 | img->pixmap = NO_PIXMAP; | |
5984 | goto error; | |
5985 | } | |
5986 | ||
5987 | /* Fill the X image and mask from PNG data. */ | |
5988 | init_color_table (); | |
5989 | ||
5990 | for (y = 0; y < height; ++y) | |
5991 | { | |
5992 | png_byte *p = rows[y]; | |
5993 | ||
5994 | for (x = 0; x < width; ++x) | |
5995 | { | |
5996 | unsigned r, g, b; | |
5997 | ||
5998 | r = *p++ << 8; | |
5999 | g = *p++ << 8; | |
6000 | b = *p++ << 8; | |
6001 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
6002 | /* An alpha channel, aka mask channel, associates variable | |
6003 | transparency with an image. Where other image formats | |
6004 | support binary transparency---fully transparent or fully | |
6005 | opaque---PNG allows up to 254 levels of partial transparency. | |
6006 | The PNG library implements partial transparency by combining | |
6007 | the image with a specified background color. | |
6008 | ||
6009 | I'm not sure how to handle this here nicely: because the | |
6010 | background on which the image is displayed may change, for | |
6011 | real alpha channel support, it would be necessary to create | |
6012 | a new image for each possible background. | |
6013 | ||
6014 | What I'm doing now is that a mask is created if we have | |
6015 | boolean transparency information. Otherwise I'm using | |
6016 | the frame's background color to combine the image with. */ | |
6017 | ||
6018 | if (channels == 4) | |
6019 | { | |
6020 | if (mask_img) | |
9ecf6403 | 6021 | XPutPixel (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN); |
4ef0d4d0 KS |
6022 | ++p; |
6023 | } | |
6024 | } | |
6025 | } | |
6026 | ||
6027 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
6028 | /* Set IMG's background color from the PNG image, unless the user | |
6029 | overrode it. */ | |
6030 | { | |
6031 | png_color_16 *bg; | |
6032 | if (fn_png_get_bKGD (png_ptr, info_ptr, &bg)) | |
6033 | { | |
6034 | img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue); | |
6035 | img->background_valid = 1; | |
6036 | } | |
6037 | } | |
6038 | ||
6039 | #ifdef COLOR_TABLE_SUPPORT | |
6040 | /* Remember colors allocated for this image. */ | |
6041 | img->colors = colors_in_color_table (&img->ncolors); | |
6042 | free_color_table (); | |
6043 | #endif /* COLOR_TABLE_SUPPORT */ | |
6044 | ||
6045 | /* Clean up. */ | |
6046 | fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); | |
6047 | xfree (rows); | |
6048 | xfree (pixels); | |
6049 | ||
6050 | img->width = width; | |
6051 | img->height = height; | |
6052 | ||
2e09fef1 EZ |
6053 | /* Maybe fill in the background field while we have ximg handy. |
6054 | Casting avoids a GCC warning. */ | |
6055 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
6056 | |
6057 | /* Put the image into the pixmap, then free the X image and its buffer. */ | |
6058 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
6059 | x_destroy_x_image (ximg); | |
6060 | ||
6061 | /* Same for the mask. */ | |
6062 | if (mask_img) | |
6063 | { | |
2e09fef1 EZ |
6064 | /* Fill in the background_transparent field while we have the |
6065 | mask handy. Casting avoids a GCC warning. */ | |
6066 | image_background_transparent (img, f, (XImagePtr_or_DC)mask_img); | |
4ef0d4d0 KS |
6067 | |
6068 | x_put_x_image (f, mask_img, img->mask, img->width, img->height); | |
6069 | x_destroy_x_image (mask_img); | |
6070 | } | |
6071 | ||
6072 | UNGCPRO; | |
6073 | return 1; | |
6074 | } | |
6075 | ||
6076 | #else /* HAVE_PNG */ | |
6077 | ||
edfda783 AR |
6078 | #ifdef HAVE_NS |
6079 | static int | |
6080 | png_load (struct frame *f, struct image *img) | |
6081 | { | |
6082 | return ns_load_image(f, img, | |
6083 | image_spec_value (img->spec, QCfile, NULL), | |
6084 | image_spec_value (img->spec, QCdata, NULL)); | |
6085 | } | |
6086 | #endif /* HAVE_NS */ | |
6087 | ||
6088 | ||
4ef0d4d0 KS |
6089 | #endif /* !HAVE_PNG */ |
6090 | ||
6091 | ||
6092 | \f | |
6093 | /*********************************************************************** | |
6094 | JPEG | |
6095 | ***********************************************************************/ | |
6096 | ||
9e2a2647 | 6097 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
4ef0d4d0 KS |
6098 | |
6099 | static int jpeg_image_p P_ ((Lisp_Object object)); | |
6100 | static int jpeg_load P_ ((struct frame *f, struct image *img)); | |
6101 | ||
6102 | /* The symbol `jpeg' identifying images of this type. */ | |
6103 | ||
6104 | Lisp_Object Qjpeg; | |
6105 | ||
6106 | /* Indices of image specification fields in gs_format, below. */ | |
6107 | ||
6108 | enum jpeg_keyword_index | |
6109 | { | |
6110 | JPEG_TYPE, | |
6111 | JPEG_DATA, | |
6112 | JPEG_FILE, | |
6113 | JPEG_ASCENT, | |
6114 | JPEG_MARGIN, | |
6115 | JPEG_RELIEF, | |
6116 | JPEG_ALGORITHM, | |
6117 | JPEG_HEURISTIC_MASK, | |
6118 | JPEG_MASK, | |
6119 | JPEG_BACKGROUND, | |
6120 | JPEG_LAST | |
6121 | }; | |
6122 | ||
6123 | /* Vector of image_keyword structures describing the format | |
6124 | of valid user-defined image specifications. */ | |
6125 | ||
91433552 | 6126 | static const struct image_keyword jpeg_format[JPEG_LAST] = |
4ef0d4d0 KS |
6127 | { |
6128 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6129 | {":data", IMAGE_STRING_VALUE, 0}, | |
6130 | {":file", IMAGE_STRING_VALUE, 0}, | |
6131 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
6132 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
6133 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
6134 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6135 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6136 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6137 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
6138 | }; | |
6139 | ||
6140 | /* Structure describing the image type `jpeg'. */ | |
6141 | ||
6142 | static struct image_type jpeg_type = | |
6143 | { | |
6144 | &Qjpeg, | |
6145 | jpeg_image_p, | |
6146 | jpeg_load, | |
6147 | x_clear_image, | |
6148 | NULL | |
6149 | }; | |
6150 | ||
6151 | /* Return non-zero if OBJECT is a valid JPEG image specification. */ | |
6152 | ||
6153 | static int | |
6154 | jpeg_image_p (object) | |
6155 | Lisp_Object object; | |
6156 | { | |
6157 | struct image_keyword fmt[JPEG_LAST]; | |
6158 | ||
6159 | bcopy (jpeg_format, fmt, sizeof fmt); | |
6160 | ||
6161 | if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)) | |
6162 | return 0; | |
6163 | ||
6164 | /* Must specify either the :data or :file keyword. */ | |
6165 | return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1; | |
6166 | } | |
6167 | ||
9e2a2647 | 6168 | #endif /* HAVE_JPEG || HAVE_NS */ |
4ef0d4d0 KS |
6169 | |
6170 | #ifdef HAVE_JPEG | |
6171 | ||
6172 | /* Work around a warning about HAVE_STDLIB_H being redefined in | |
6173 | jconfig.h. */ | |
6174 | #ifdef HAVE_STDLIB_H | |
6175 | #define HAVE_STDLIB_H_1 | |
6176 | #undef HAVE_STDLIB_H | |
6177 | #endif /* HAVE_STLIB_H */ | |
6178 | ||
66354420 | 6179 | #if defined (HAVE_NTGUI) && !defined (__WIN32__) |
28893db2 JB |
6180 | /* In older releases of the jpeg library, jpeglib.h will define boolean |
6181 | differently depending on __WIN32__, so make sure it is defined. */ | |
66354420 JR |
6182 | #define __WIN32__ 1 |
6183 | #endif | |
6184 | ||
4ef0d4d0 KS |
6185 | #include <jpeglib.h> |
6186 | #include <jerror.h> | |
4ef0d4d0 KS |
6187 | |
6188 | #ifdef HAVE_STLIB_H_1 | |
6189 | #define HAVE_STDLIB_H 1 | |
6190 | #endif | |
6191 | ||
6192 | #ifdef HAVE_NTGUI | |
6193 | ||
6194 | /* JPEG library details. */ | |
6195 | DEF_IMGLIB_FN (jpeg_CreateDecompress); | |
6196 | DEF_IMGLIB_FN (jpeg_start_decompress); | |
6197 | DEF_IMGLIB_FN (jpeg_finish_decompress); | |
6198 | DEF_IMGLIB_FN (jpeg_destroy_decompress); | |
6199 | DEF_IMGLIB_FN (jpeg_read_header); | |
6200 | DEF_IMGLIB_FN (jpeg_read_scanlines); | |
4ef0d4d0 KS |
6201 | DEF_IMGLIB_FN (jpeg_std_error); |
6202 | DEF_IMGLIB_FN (jpeg_resync_to_restart); | |
6203 | ||
6204 | static int | |
0855eb52 | 6205 | init_jpeg_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
6206 | { |
6207 | HMODULE library; | |
6208 | ||
0855eb52 | 6209 | if (!(library = w32_delayed_load (libraries, Qjpeg))) |
4ef0d4d0 KS |
6210 | return 0; |
6211 | ||
6212 | LOAD_IMGLIB_FN (library, jpeg_finish_decompress); | |
6213 | LOAD_IMGLIB_FN (library, jpeg_read_scanlines); | |
6214 | LOAD_IMGLIB_FN (library, jpeg_start_decompress); | |
6215 | LOAD_IMGLIB_FN (library, jpeg_read_header); | |
4ef0d4d0 KS |
6216 | LOAD_IMGLIB_FN (library, jpeg_CreateDecompress); |
6217 | LOAD_IMGLIB_FN (library, jpeg_destroy_decompress); | |
6218 | LOAD_IMGLIB_FN (library, jpeg_std_error); | |
6219 | LOAD_IMGLIB_FN (library, jpeg_resync_to_restart); | |
6220 | return 1; | |
6221 | } | |
6222 | ||
6223 | /* Wrapper since we can't directly assign the function pointer | |
6224 | to another function pointer that was declared more completely easily. */ | |
6225 | static boolean | |
5fc9fdeb | 6226 | jpeg_resync_to_restart_wrapper (cinfo, desired) |
4ef0d4d0 KS |
6227 | j_decompress_ptr cinfo; |
6228 | int desired; | |
6229 | { | |
6230 | return fn_jpeg_resync_to_restart (cinfo, desired); | |
6231 | } | |
6232 | ||
6233 | #else | |
6234 | ||
6235 | #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a) | |
6236 | #define fn_jpeg_start_decompress jpeg_start_decompress | |
6237 | #define fn_jpeg_finish_decompress jpeg_finish_decompress | |
6238 | #define fn_jpeg_destroy_decompress jpeg_destroy_decompress | |
6239 | #define fn_jpeg_read_header jpeg_read_header | |
6240 | #define fn_jpeg_read_scanlines jpeg_read_scanlines | |
4ef0d4d0 KS |
6241 | #define fn_jpeg_std_error jpeg_std_error |
6242 | #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart | |
6243 | ||
6244 | #endif /* HAVE_NTGUI */ | |
6245 | ||
6246 | struct my_jpeg_error_mgr | |
6247 | { | |
6248 | struct jpeg_error_mgr pub; | |
6249 | jmp_buf setjmp_buffer; | |
6250 | }; | |
6251 | ||
6252 | ||
6253 | static void | |
6254 | my_error_exit (cinfo) | |
6255 | j_common_ptr cinfo; | |
6256 | { | |
6257 | struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err; | |
6258 | longjmp (mgr->setjmp_buffer, 1); | |
6259 | } | |
6260 | ||
6261 | ||
6262 | /* Init source method for JPEG data source manager. Called by | |
6263 | jpeg_read_header() before any data is actually read. See | |
6264 | libjpeg.doc from the JPEG lib distribution. */ | |
6265 | ||
6266 | static void | |
b25a72ab JB |
6267 | our_common_init_source (cinfo) |
6268 | j_decompress_ptr cinfo; | |
6269 | { | |
6270 | } | |
6271 | ||
6272 | ||
6273 | /* Method to terminate data source. Called by | |
6274 | jpeg_finish_decompress() after all data has been processed. */ | |
6275 | ||
6276 | static void | |
6277 | our_common_term_source (cinfo) | |
4ef0d4d0 KS |
6278 | j_decompress_ptr cinfo; |
6279 | { | |
6280 | } | |
6281 | ||
6282 | ||
6283 | /* Fill input buffer method for JPEG data source manager. Called | |
6284 | whenever more data is needed. We read the whole image in one step, | |
6285 | so this only adds a fake end of input marker at the end. */ | |
6286 | ||
5a1539f3 | 6287 | static JOCTET our_memory_buffer[2]; |
aca946f3 | 6288 | |
4ef0d4d0 | 6289 | static boolean |
b25a72ab | 6290 | our_memory_fill_input_buffer (cinfo) |
4ef0d4d0 KS |
6291 | j_decompress_ptr cinfo; |
6292 | { | |
6293 | /* Insert a fake EOI marker. */ | |
6294 | struct jpeg_source_mgr *src = cinfo->src; | |
4ef0d4d0 | 6295 | |
5a1539f3 MB |
6296 | our_memory_buffer[0] = (JOCTET) 0xFF; |
6297 | our_memory_buffer[1] = (JOCTET) JPEG_EOI; | |
4ef0d4d0 | 6298 | |
5a1539f3 | 6299 | src->next_input_byte = our_memory_buffer; |
4ef0d4d0 | 6300 | src->bytes_in_buffer = 2; |
e0e30823 | 6301 | return 1; |
4ef0d4d0 KS |
6302 | } |
6303 | ||
6304 | ||
6305 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6306 | is the JPEG data source manager. */ | |
6307 | ||
6308 | static void | |
b25a72ab | 6309 | our_memory_skip_input_data (cinfo, num_bytes) |
4ef0d4d0 KS |
6310 | j_decompress_ptr cinfo; |
6311 | long num_bytes; | |
6312 | { | |
6313 | struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src; | |
6314 | ||
6315 | if (src) | |
6316 | { | |
6317 | if (num_bytes > src->bytes_in_buffer) | |
6318 | ERREXIT (cinfo, JERR_INPUT_EOF); | |
6319 | ||
6320 | src->bytes_in_buffer -= num_bytes; | |
6321 | src->next_input_byte += num_bytes; | |
6322 | } | |
6323 | } | |
6324 | ||
6325 | ||
4ef0d4d0 KS |
6326 | /* Set up the JPEG lib for reading an image from DATA which contains |
6327 | LEN bytes. CINFO is the decompression info structure created for | |
6328 | reading the image. */ | |
6329 | ||
6330 | static void | |
6331 | jpeg_memory_src (cinfo, data, len) | |
6332 | j_decompress_ptr cinfo; | |
6333 | JOCTET *data; | |
6334 | unsigned int len; | |
6335 | { | |
6336 | struct jpeg_source_mgr *src; | |
6337 | ||
6338 | if (cinfo->src == NULL) | |
6339 | { | |
6340 | /* First time for this JPEG object? */ | |
6341 | cinfo->src = (struct jpeg_source_mgr *) | |
6342 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6343 | sizeof (struct jpeg_source_mgr)); | |
6344 | src = (struct jpeg_source_mgr *) cinfo->src; | |
6345 | src->next_input_byte = data; | |
6346 | } | |
6347 | ||
6348 | src = (struct jpeg_source_mgr *) cinfo->src; | |
b25a72ab JB |
6349 | src->init_source = our_common_init_source; |
6350 | src->fill_input_buffer = our_memory_fill_input_buffer; | |
6351 | src->skip_input_data = our_memory_skip_input_data; | |
4ef0d4d0 | 6352 | src->resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ |
b25a72ab | 6353 | src->term_source = our_common_term_source; |
4ef0d4d0 KS |
6354 | src->bytes_in_buffer = len; |
6355 | src->next_input_byte = data; | |
6356 | } | |
6357 | ||
6358 | ||
b25a72ab JB |
6359 | struct jpeg_stdio_mgr |
6360 | { | |
6361 | struct jpeg_source_mgr mgr; | |
6362 | boolean finished; | |
6363 | FILE *file; | |
6364 | JOCTET *buffer; | |
6365 | }; | |
6366 | ||
6367 | ||
6368 | /* Size of buffer to read JPEG from file. | |
6369 | Not too big, as we want to use alloc_small. */ | |
6370 | #define JPEG_STDIO_BUFFER_SIZE 8192 | |
6371 | ||
6372 | ||
6373 | /* Fill input buffer method for JPEG data source manager. Called | |
6374 | whenever more data is needed. The data is read from a FILE *. */ | |
6375 | ||
6376 | static boolean | |
6377 | our_stdio_fill_input_buffer (cinfo) | |
6378 | j_decompress_ptr cinfo; | |
6379 | { | |
6380 | struct jpeg_stdio_mgr *src; | |
6381 | ||
6382 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6383 | if (!src->finished) | |
6384 | { | |
6385 | size_t bytes; | |
6386 | ||
6387 | bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file); | |
6388 | if (bytes > 0) | |
6389 | src->mgr.bytes_in_buffer = bytes; | |
6390 | else | |
6391 | { | |
6392 | WARNMS (cinfo, JWRN_JPEG_EOF); | |
6393 | src->finished = 1; | |
6394 | src->buffer[0] = (JOCTET) 0xFF; | |
6395 | src->buffer[1] = (JOCTET) JPEG_EOI; | |
6396 | src->mgr.bytes_in_buffer = 2; | |
6397 | } | |
6398 | src->mgr.next_input_byte = src->buffer; | |
6399 | } | |
6400 | ||
6401 | return 1; | |
6402 | } | |
6403 | ||
6404 | ||
6405 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6406 | is the JPEG data source manager. */ | |
6407 | ||
6408 | static void | |
6409 | our_stdio_skip_input_data (cinfo, num_bytes) | |
6410 | j_decompress_ptr cinfo; | |
6411 | long num_bytes; | |
6412 | { | |
6413 | struct jpeg_stdio_mgr *src; | |
6414 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6415 | ||
6416 | while (num_bytes > 0 && !src->finished) | |
6417 | { | |
6418 | if (num_bytes <= src->mgr.bytes_in_buffer) | |
6419 | { | |
6420 | src->mgr.bytes_in_buffer -= num_bytes; | |
6421 | src->mgr.next_input_byte += num_bytes; | |
6422 | break; | |
6423 | } | |
6424 | else | |
6425 | { | |
6426 | num_bytes -= src->mgr.bytes_in_buffer; | |
6427 | src->mgr.bytes_in_buffer = 0; | |
6428 | src->mgr.next_input_byte = NULL; | |
6429 | ||
6430 | our_stdio_fill_input_buffer (cinfo); | |
6431 | } | |
6432 | } | |
6433 | } | |
6434 | ||
6435 | ||
6436 | /* Set up the JPEG lib for reading an image from a FILE *. | |
6437 | CINFO is the decompression info structure created for | |
6438 | reading the image. */ | |
6439 | ||
6440 | static void | |
6441 | jpeg_file_src (cinfo, fp) | |
6442 | j_decompress_ptr cinfo; | |
6443 | FILE *fp; | |
6444 | { | |
6445 | struct jpeg_stdio_mgr *src; | |
6446 | ||
6447 | if (cinfo->src != NULL) | |
6448 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6449 | else | |
6450 | { | |
6451 | /* First time for this JPEG object? */ | |
6452 | cinfo->src = (struct jpeg_source_mgr *) | |
6453 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6454 | sizeof (struct jpeg_stdio_mgr)); | |
6455 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6456 | src->buffer = (JOCTET *) | |
6457 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6458 | JPEG_STDIO_BUFFER_SIZE); | |
6459 | } | |
6460 | ||
6461 | src->file = fp; | |
6462 | src->finished = 0; | |
6463 | src->mgr.init_source = our_common_init_source; | |
6464 | src->mgr.fill_input_buffer = our_stdio_fill_input_buffer; | |
6465 | src->mgr.skip_input_data = our_stdio_skip_input_data; | |
6466 | src->mgr.resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ | |
6467 | src->mgr.term_source = our_common_term_source; | |
6468 | src->mgr.bytes_in_buffer = 0; | |
6469 | src->mgr.next_input_byte = NULL; | |
6470 | } | |
6471 | ||
6472 | ||
4ef0d4d0 KS |
6473 | /* Load image IMG for use on frame F. Patterned after example.c |
6474 | from the JPEG lib. */ | |
6475 | ||
6476 | static int | |
6477 | jpeg_load (f, img) | |
6478 | struct frame *f; | |
6479 | struct image *img; | |
6480 | { | |
6481 | struct jpeg_decompress_struct cinfo; | |
6482 | struct my_jpeg_error_mgr mgr; | |
6483 | Lisp_Object file, specified_file; | |
6484 | Lisp_Object specified_data; | |
6485 | FILE * volatile fp = NULL; | |
6486 | JSAMPARRAY buffer; | |
6487 | int row_stride, x, y; | |
6488 | XImagePtr ximg = NULL; | |
6489 | int rc; | |
6490 | unsigned long *colors; | |
6491 | int width, height; | |
6492 | struct gcpro gcpro1; | |
6493 | ||
6494 | /* Open the JPEG file. */ | |
6495 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6496 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
6497 | file = Qnil; | |
6498 | GCPRO1 (file); | |
6499 | ||
6500 | if (NILP (specified_data)) | |
6501 | { | |
6502 | file = x_find_image_file (specified_file); | |
6503 | if (!STRINGP (file)) | |
6504 | { | |
6505 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
6506 | UNGCPRO; | |
6507 | return 0; | |
6508 | } | |
6509 | ||
6510 | fp = fopen (SDATA (file), "rb"); | |
6511 | if (fp == NULL) | |
6512 | { | |
6513 | image_error ("Cannot open `%s'", file, Qnil); | |
6514 | UNGCPRO; | |
6515 | return 0; | |
6516 | } | |
6517 | } | |
6518 | ||
6519 | /* Customize libjpeg's error handling to call my_error_exit when an | |
2e09fef1 EZ |
6520 | error is detected. This function will perform a longjmp. |
6521 | Casting return value avoids a GCC warning on W32. */ | |
6522 | cinfo.err = (struct jpeg_error_mgr *)fn_jpeg_std_error (&mgr.pub); | |
4ef0d4d0 KS |
6523 | mgr.pub.error_exit = my_error_exit; |
6524 | ||
6525 | if ((rc = setjmp (mgr.setjmp_buffer)) != 0) | |
6526 | { | |
6527 | if (rc == 1) | |
6528 | { | |
6529 | /* Called from my_error_exit. Display a JPEG error. */ | |
6530 | char buffer[JMSG_LENGTH_MAX]; | |
6531 | cinfo.err->format_message ((j_common_ptr) &cinfo, buffer); | |
6532 | image_error ("Error reading JPEG image `%s': %s", img->spec, | |
6533 | build_string (buffer)); | |
6534 | } | |
6535 | ||
6536 | /* Close the input file and destroy the JPEG object. */ | |
6537 | if (fp) | |
6538 | fclose ((FILE *) fp); | |
6539 | fn_jpeg_destroy_decompress (&cinfo); | |
6540 | ||
6541 | /* If we already have an XImage, free that. */ | |
6542 | x_destroy_x_image (ximg); | |
6543 | ||
6544 | /* Free pixmap and colors. */ | |
6545 | x_clear_image (f, img); | |
6546 | ||
6547 | UNGCPRO; | |
6548 | return 0; | |
6549 | } | |
6550 | ||
6551 | /* Create the JPEG decompression object. Let it read from fp. | |
6552 | Read the JPEG image header. */ | |
6553 | fn_jpeg_CreateDecompress (&cinfo, JPEG_LIB_VERSION, sizeof (cinfo)); | |
6554 | ||
6555 | if (NILP (specified_data)) | |
b25a72ab | 6556 | jpeg_file_src (&cinfo, (FILE *) fp); |
4ef0d4d0 KS |
6557 | else |
6558 | jpeg_memory_src (&cinfo, SDATA (specified_data), | |
6559 | SBYTES (specified_data)); | |
6560 | ||
e0e30823 | 6561 | fn_jpeg_read_header (&cinfo, 1); |
4ef0d4d0 KS |
6562 | |
6563 | /* Customize decompression so that color quantization will be used. | |
6564 | Start decompression. */ | |
e0e30823 | 6565 | cinfo.quantize_colors = 1; |
4ef0d4d0 KS |
6566 | fn_jpeg_start_decompress (&cinfo); |
6567 | width = img->width = cinfo.output_width; | |
6568 | height = img->height = cinfo.output_height; | |
6569 | ||
f1f25b99 CY |
6570 | if (!check_image_size (f, width, height)) |
6571 | { | |
10ea2b82 | 6572 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
6573 | longjmp (mgr.setjmp_buffer, 2); |
6574 | } | |
6575 | ||
4ef0d4d0 KS |
6576 | /* Create X image and pixmap. */ |
6577 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) | |
6578 | longjmp (mgr.setjmp_buffer, 2); | |
6579 | ||
6580 | /* Allocate colors. When color quantization is used, | |
6581 | cinfo.actual_number_of_colors has been set with the number of | |
6582 | colors generated, and cinfo.colormap is a two-dimensional array | |
6583 | of color indices in the range 0..cinfo.actual_number_of_colors. | |
6584 | No more than 255 colors will be generated. */ | |
6585 | { | |
6586 | int i, ir, ig, ib; | |
6587 | ||
6588 | if (cinfo.out_color_components > 2) | |
6589 | ir = 0, ig = 1, ib = 2; | |
6590 | else if (cinfo.out_color_components > 1) | |
6591 | ir = 0, ig = 1, ib = 0; | |
6592 | else | |
6593 | ir = 0, ig = 0, ib = 0; | |
6594 | ||
6595 | /* Use the color table mechanism because it handles colors that | |
6596 | cannot be allocated nicely. Such colors will be replaced with | |
6597 | a default color, and we don't have to care about which colors | |
6598 | can be freed safely, and which can't. */ | |
6599 | init_color_table (); | |
6600 | colors = (unsigned long *) alloca (cinfo.actual_number_of_colors | |
6601 | * sizeof *colors); | |
6602 | ||
6603 | for (i = 0; i < cinfo.actual_number_of_colors; ++i) | |
6604 | { | |
6605 | /* Multiply RGB values with 255 because X expects RGB values | |
6606 | in the range 0..0xffff. */ | |
6607 | int r = cinfo.colormap[ir][i] << 8; | |
6608 | int g = cinfo.colormap[ig][i] << 8; | |
6609 | int b = cinfo.colormap[ib][i] << 8; | |
6610 | colors[i] = lookup_rgb_color (f, r, g, b); | |
6611 | } | |
6612 | ||
6613 | #ifdef COLOR_TABLE_SUPPORT | |
6614 | /* Remember those colors actually allocated. */ | |
6615 | img->colors = colors_in_color_table (&img->ncolors); | |
6616 | free_color_table (); | |
6617 | #endif /* COLOR_TABLE_SUPPORT */ | |
6618 | } | |
6619 | ||
6620 | /* Read pixels. */ | |
6621 | row_stride = width * cinfo.output_components; | |
6622 | buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE, | |
6623 | row_stride, 1); | |
6624 | for (y = 0; y < height; ++y) | |
6625 | { | |
6626 | fn_jpeg_read_scanlines (&cinfo, buffer, 1); | |
6627 | for (x = 0; x < cinfo.output_width; ++x) | |
6628 | XPutPixel (ximg, x, y, colors[buffer[0][x]]); | |
6629 | } | |
6630 | ||
6631 | /* Clean up. */ | |
6632 | fn_jpeg_finish_decompress (&cinfo); | |
6633 | fn_jpeg_destroy_decompress (&cinfo); | |
6634 | if (fp) | |
6635 | fclose ((FILE *) fp); | |
6636 | ||
6637 | /* Maybe fill in the background field while we have ximg handy. */ | |
6638 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
6639 | /* Casting avoids a GCC warning. */ |
6640 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
6641 | |
6642 | /* Put the image into the pixmap. */ | |
6643 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
6644 | x_destroy_x_image (ximg); | |
6645 | UNGCPRO; | |
6646 | return 1; | |
6647 | } | |
6648 | ||
6649 | #else /* HAVE_JPEG */ | |
6650 | ||
edfda783 AR |
6651 | #ifdef HAVE_NS |
6652 | static int | |
6653 | jpeg_load (struct frame *f, struct image *img) | |
6654 | { | |
6655 | return ns_load_image(f, img, | |
6656 | image_spec_value (img->spec, QCfile, NULL), | |
6657 | image_spec_value (img->spec, QCdata, NULL)); | |
6658 | } | |
6659 | #endif /* HAVE_NS */ | |
6660 | ||
4ef0d4d0 KS |
6661 | #endif /* !HAVE_JPEG */ |
6662 | ||
6663 | ||
6664 | \f | |
6665 | /*********************************************************************** | |
6666 | TIFF | |
6667 | ***********************************************************************/ | |
6668 | ||
9e2a2647 | 6669 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
4ef0d4d0 KS |
6670 | |
6671 | static int tiff_image_p P_ ((Lisp_Object object)); | |
6672 | static int tiff_load P_ ((struct frame *f, struct image *img)); | |
6673 | ||
6674 | /* The symbol `tiff' identifying images of this type. */ | |
6675 | ||
6676 | Lisp_Object Qtiff; | |
6677 | ||
6678 | /* Indices of image specification fields in tiff_format, below. */ | |
6679 | ||
6680 | enum tiff_keyword_index | |
6681 | { | |
6682 | TIFF_TYPE, | |
6683 | TIFF_DATA, | |
6684 | TIFF_FILE, | |
6685 | TIFF_ASCENT, | |
6686 | TIFF_MARGIN, | |
6687 | TIFF_RELIEF, | |
6688 | TIFF_ALGORITHM, | |
6689 | TIFF_HEURISTIC_MASK, | |
6690 | TIFF_MASK, | |
6691 | TIFF_BACKGROUND, | |
58279503 | 6692 | TIFF_INDEX, |
4ef0d4d0 KS |
6693 | TIFF_LAST |
6694 | }; | |
6695 | ||
6696 | /* Vector of image_keyword structures describing the format | |
6697 | of valid user-defined image specifications. */ | |
6698 | ||
91433552 | 6699 | static const struct image_keyword tiff_format[TIFF_LAST] = |
4ef0d4d0 KS |
6700 | { |
6701 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6702 | {":data", IMAGE_STRING_VALUE, 0}, | |
6703 | {":file", IMAGE_STRING_VALUE, 0}, | |
6704 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
6705 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
6706 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
6707 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6708 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6709 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 JR |
6710 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, |
6711 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0} | |
4ef0d4d0 KS |
6712 | }; |
6713 | ||
6714 | /* Structure describing the image type `tiff'. */ | |
6715 | ||
6716 | static struct image_type tiff_type = | |
6717 | { | |
6718 | &Qtiff, | |
6719 | tiff_image_p, | |
6720 | tiff_load, | |
6721 | x_clear_image, | |
6722 | NULL | |
6723 | }; | |
6724 | ||
6725 | /* Return non-zero if OBJECT is a valid TIFF image specification. */ | |
6726 | ||
6727 | static int | |
6728 | tiff_image_p (object) | |
6729 | Lisp_Object object; | |
6730 | { | |
6731 | struct image_keyword fmt[TIFF_LAST]; | |
6732 | bcopy (tiff_format, fmt, sizeof fmt); | |
6733 | ||
6734 | if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff)) | |
6735 | return 0; | |
6736 | ||
6737 | /* Must specify either the :data or :file keyword. */ | |
6738 | return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1; | |
6739 | } | |
6740 | ||
9e2a2647 | 6741 | #endif /* HAVE_TIFF || HAVE_NS */ |
4ef0d4d0 KS |
6742 | |
6743 | #ifdef HAVE_TIFF | |
6744 | ||
6745 | #include <tiffio.h> | |
6746 | ||
6747 | #ifdef HAVE_NTGUI | |
6748 | ||
6749 | /* TIFF library details. */ | |
6750 | DEF_IMGLIB_FN (TIFFSetErrorHandler); | |
6751 | DEF_IMGLIB_FN (TIFFSetWarningHandler); | |
6752 | DEF_IMGLIB_FN (TIFFOpen); | |
6753 | DEF_IMGLIB_FN (TIFFClientOpen); | |
6754 | DEF_IMGLIB_FN (TIFFGetField); | |
6755 | DEF_IMGLIB_FN (TIFFReadRGBAImage); | |
6756 | DEF_IMGLIB_FN (TIFFClose); | |
58279503 | 6757 | DEF_IMGLIB_FN (TIFFSetDirectory); |
4ef0d4d0 KS |
6758 | |
6759 | static int | |
0855eb52 | 6760 | init_tiff_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
6761 | { |
6762 | HMODULE library; | |
6763 | ||
0855eb52 | 6764 | if (!(library = w32_delayed_load (libraries, Qtiff))) |
4ef0d4d0 KS |
6765 | return 0; |
6766 | ||
6767 | LOAD_IMGLIB_FN (library, TIFFSetErrorHandler); | |
6768 | LOAD_IMGLIB_FN (library, TIFFSetWarningHandler); | |
6769 | LOAD_IMGLIB_FN (library, TIFFOpen); | |
6770 | LOAD_IMGLIB_FN (library, TIFFClientOpen); | |
6771 | LOAD_IMGLIB_FN (library, TIFFGetField); | |
6772 | LOAD_IMGLIB_FN (library, TIFFReadRGBAImage); | |
6773 | LOAD_IMGLIB_FN (library, TIFFClose); | |
58279503 | 6774 | LOAD_IMGLIB_FN (library, TIFFSetDirectory); |
4ef0d4d0 KS |
6775 | return 1; |
6776 | } | |
6777 | ||
6778 | #else | |
6779 | ||
6780 | #define fn_TIFFSetErrorHandler TIFFSetErrorHandler | |
6781 | #define fn_TIFFSetWarningHandler TIFFSetWarningHandler | |
6782 | #define fn_TIFFOpen TIFFOpen | |
6783 | #define fn_TIFFClientOpen TIFFClientOpen | |
6784 | #define fn_TIFFGetField TIFFGetField | |
6785 | #define fn_TIFFReadRGBAImage TIFFReadRGBAImage | |
6786 | #define fn_TIFFClose TIFFClose | |
58279503 | 6787 | #define fn_TIFFSetDirectory TIFFSetDirectory |
4ef0d4d0 KS |
6788 | #endif /* HAVE_NTGUI */ |
6789 | ||
6790 | ||
6791 | /* Reading from a memory buffer for TIFF images Based on the PNG | |
6792 | memory source, but we have to provide a lot of extra functions. | |
6793 | Blah. | |
6794 | ||
6795 | We really only need to implement read and seek, but I am not | |
6796 | convinced that the TIFF library is smart enough not to destroy | |
6797 | itself if we only hand it the function pointers we need to | |
6798 | override. */ | |
6799 | ||
6800 | typedef struct | |
6801 | { | |
6802 | unsigned char *bytes; | |
6803 | size_t len; | |
6804 | int index; | |
6805 | } | |
6806 | tiff_memory_source; | |
6807 | ||
6808 | static size_t | |
6809 | tiff_read_from_memory (data, buf, size) | |
6810 | thandle_t data; | |
6811 | tdata_t buf; | |
6812 | tsize_t size; | |
6813 | { | |
6814 | tiff_memory_source *src = (tiff_memory_source *) data; | |
6815 | ||
6816 | if (size > src->len - src->index) | |
6817 | return (size_t) -1; | |
6818 | bcopy (src->bytes + src->index, buf, size); | |
6819 | src->index += size; | |
6820 | return size; | |
6821 | } | |
6822 | ||
6823 | static size_t | |
6824 | tiff_write_from_memory (data, buf, size) | |
6825 | thandle_t data; | |
6826 | tdata_t buf; | |
6827 | tsize_t size; | |
6828 | { | |
6829 | return (size_t) -1; | |
6830 | } | |
6831 | ||
6832 | static toff_t | |
6833 | tiff_seek_in_memory (data, off, whence) | |
6834 | thandle_t data; | |
6835 | toff_t off; | |
6836 | int whence; | |
6837 | { | |
6838 | tiff_memory_source *src = (tiff_memory_source *) data; | |
6839 | int idx; | |
6840 | ||
6841 | switch (whence) | |
6842 | { | |
6843 | case SEEK_SET: /* Go from beginning of source. */ | |
6844 | idx = off; | |
6845 | break; | |
6846 | ||
6847 | case SEEK_END: /* Go from end of source. */ | |
6848 | idx = src->len + off; | |
6849 | break; | |
6850 | ||
6851 | case SEEK_CUR: /* Go from current position. */ | |
6852 | idx = src->index + off; | |
6853 | break; | |
6854 | ||
6855 | default: /* Invalid `whence'. */ | |
6856 | return -1; | |
6857 | } | |
6858 | ||
6859 | if (idx > src->len || idx < 0) | |
6860 | return -1; | |
6861 | ||
6862 | src->index = idx; | |
6863 | return src->index; | |
6864 | } | |
6865 | ||
6866 | static int | |
6867 | tiff_close_memory (data) | |
6868 | thandle_t data; | |
6869 | { | |
6870 | /* NOOP */ | |
6871 | return 0; | |
6872 | } | |
6873 | ||
6874 | static int | |
6875 | tiff_mmap_memory (data, pbase, psize) | |
6876 | thandle_t data; | |
6877 | tdata_t *pbase; | |
6878 | toff_t *psize; | |
6879 | { | |
6880 | /* It is already _IN_ memory. */ | |
6881 | return 0; | |
6882 | } | |
6883 | ||
6884 | static void | |
6885 | tiff_unmap_memory (data, base, size) | |
6886 | thandle_t data; | |
6887 | tdata_t base; | |
6888 | toff_t size; | |
6889 | { | |
6890 | /* We don't need to do this. */ | |
6891 | } | |
6892 | ||
6893 | static toff_t | |
6894 | tiff_size_of_memory (data) | |
6895 | thandle_t data; | |
6896 | { | |
6897 | return ((tiff_memory_source *) data)->len; | |
6898 | } | |
6899 | ||
6900 | ||
6901 | static void | |
6902 | tiff_error_handler (title, format, ap) | |
6903 | const char *title, *format; | |
6904 | va_list ap; | |
6905 | { | |
6906 | char buf[512]; | |
6907 | int len; | |
6908 | ||
6909 | len = sprintf (buf, "TIFF error: %s ", title); | |
6910 | vsprintf (buf + len, format, ap); | |
6911 | add_to_log (buf, Qnil, Qnil); | |
6912 | } | |
6913 | ||
6914 | ||
6915 | static void | |
6916 | tiff_warning_handler (title, format, ap) | |
6917 | const char *title, *format; | |
6918 | va_list ap; | |
6919 | { | |
6920 | char buf[512]; | |
6921 | int len; | |
6922 | ||
6923 | len = sprintf (buf, "TIFF warning: %s ", title); | |
6924 | vsprintf (buf + len, format, ap); | |
6925 | add_to_log (buf, Qnil, Qnil); | |
6926 | } | |
6927 | ||
6928 | ||
6929 | /* Load TIFF image IMG for use on frame F. Value is non-zero if | |
6930 | successful. */ | |
6931 | ||
6932 | static int | |
6933 | tiff_load (f, img) | |
6934 | struct frame *f; | |
6935 | struct image *img; | |
6936 | { | |
6937 | Lisp_Object file, specified_file; | |
6938 | Lisp_Object specified_data; | |
6939 | TIFF *tiff; | |
58279503 | 6940 | int width, height, x, y, count; |
4ef0d4d0 | 6941 | uint32 *buf; |
58279503 | 6942 | int rc, rc2; |
4ef0d4d0 KS |
6943 | XImagePtr ximg; |
6944 | struct gcpro gcpro1; | |
6945 | tiff_memory_source memsrc; | |
58279503 | 6946 | Lisp_Object image; |
4ef0d4d0 KS |
6947 | |
6948 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6949 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
6950 | file = Qnil; | |
6951 | GCPRO1 (file); | |
6952 | ||
6953 | fn_TIFFSetErrorHandler (tiff_error_handler); | |
6954 | fn_TIFFSetWarningHandler (tiff_warning_handler); | |
6955 | ||
6956 | if (NILP (specified_data)) | |
6957 | { | |
6958 | /* Read from a file */ | |
6959 | file = x_find_image_file (specified_file); | |
6960 | if (!STRINGP (file)) | |
6961 | { | |
6962 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
6963 | UNGCPRO; | |
6964 | return 0; | |
6965 | } | |
6966 | ||
2e09fef1 EZ |
6967 | /* Try to open the image file. Casting return value avoids a |
6968 | GCC warning on W32. */ | |
6969 | tiff = (TIFF *)fn_TIFFOpen (SDATA (file), "r"); | |
4ef0d4d0 KS |
6970 | if (tiff == NULL) |
6971 | { | |
6972 | image_error ("Cannot open `%s'", file, Qnil); | |
6973 | UNGCPRO; | |
6974 | return 0; | |
6975 | } | |
6976 | } | |
6977 | else | |
6978 | { | |
6979 | /* Memory source! */ | |
6980 | memsrc.bytes = SDATA (specified_data); | |
6981 | memsrc.len = SBYTES (specified_data); | |
6982 | memsrc.index = 0; | |
6983 | ||
2e09fef1 EZ |
6984 | /* Casting return value avoids a GCC warning on W32. */ |
6985 | tiff = (TIFF *)fn_TIFFClientOpen ("memory_source", "r", &memsrc, | |
6986 | (TIFFReadWriteProc) tiff_read_from_memory, | |
6987 | (TIFFReadWriteProc) tiff_write_from_memory, | |
6988 | tiff_seek_in_memory, | |
6989 | tiff_close_memory, | |
6990 | tiff_size_of_memory, | |
6991 | tiff_mmap_memory, | |
6992 | tiff_unmap_memory); | |
4ef0d4d0 KS |
6993 | |
6994 | if (!tiff) | |
6995 | { | |
6996 | image_error ("Cannot open memory source for `%s'", img->spec, Qnil); | |
6997 | UNGCPRO; | |
6998 | return 0; | |
6999 | } | |
7000 | } | |
7001 | ||
58279503 JR |
7002 | image = image_spec_value (img->spec, QCindex, NULL); |
7003 | if (INTEGERP (image)) | |
7004 | { | |
7005 | int ino = XFASTINT (image); | |
7006 | if (!fn_TIFFSetDirectory (tiff, ino)) | |
7007 | { | |
7008 | image_error ("Invalid image number `%s' in image `%s'", | |
7009 | image, img->spec); | |
7010 | fn_TIFFClose (tiff); | |
7011 | UNGCPRO; | |
7012 | return 0; | |
7013 | } | |
7014 | } | |
7015 | ||
4ef0d4d0 KS |
7016 | /* Get width and height of the image, and allocate a raster buffer |
7017 | of width x height 32-bit values. */ | |
7018 | fn_TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width); | |
7019 | fn_TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height); | |
f1f25b99 CY |
7020 | |
7021 | if (!check_image_size (f, width, height)) | |
7022 | { | |
10ea2b82 | 7023 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
58279503 | 7024 | fn_TIFFClose (tiff); |
f1f25b99 CY |
7025 | UNGCPRO; |
7026 | return 0; | |
7027 | } | |
7028 | ||
4ef0d4d0 KS |
7029 | buf = (uint32 *) xmalloc (width * height * sizeof *buf); |
7030 | ||
7031 | rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0); | |
58279503 JR |
7032 | |
7033 | /* Count the number of images in the file. */ | |
7034 | for (count = 1, rc2 = 1; rc2; count++) | |
7035 | rc2 = fn_TIFFSetDirectory (tiff, count); | |
7036 | ||
7037 | if (count > 1) | |
7038 | img->data.lisp_val = Fcons (Qcount, | |
7039 | Fcons (make_number (count), | |
7040 | img->data.lisp_val)); | |
7041 | ||
4ef0d4d0 KS |
7042 | fn_TIFFClose (tiff); |
7043 | if (!rc) | |
7044 | { | |
7045 | image_error ("Error reading TIFF image `%s'", img->spec, Qnil); | |
7046 | xfree (buf); | |
7047 | UNGCPRO; | |
7048 | return 0; | |
7049 | } | |
7050 | ||
7051 | /* Create the X image and pixmap. */ | |
7052 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) | |
7053 | { | |
7054 | xfree (buf); | |
7055 | UNGCPRO; | |
7056 | return 0; | |
7057 | } | |
7058 | ||
7059 | /* Initialize the color table. */ | |
7060 | init_color_table (); | |
7061 | ||
7062 | /* Process the pixel raster. Origin is in the lower-left corner. */ | |
7063 | for (y = 0; y < height; ++y) | |
7064 | { | |
7065 | uint32 *row = buf + y * width; | |
7066 | ||
7067 | for (x = 0; x < width; ++x) | |
7068 | { | |
7069 | uint32 abgr = row[x]; | |
7070 | int r = TIFFGetR (abgr) << 8; | |
7071 | int g = TIFFGetG (abgr) << 8; | |
7072 | int b = TIFFGetB (abgr) << 8; | |
7073 | XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b)); | |
7074 | } | |
7075 | } | |
7076 | ||
7077 | #ifdef COLOR_TABLE_SUPPORT | |
7078 | /* Remember the colors allocated for the image. Free the color table. */ | |
7079 | img->colors = colors_in_color_table (&img->ncolors); | |
7080 | free_color_table (); | |
7081 | #endif /* COLOR_TABLE_SUPPORT */ | |
7082 | ||
7083 | img->width = width; | |
7084 | img->height = height; | |
7085 | ||
7086 | /* Maybe fill in the background field while we have ximg handy. */ | |
7087 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7088 | /* Casting avoids a GCC warning on W32. */ |
7089 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
7090 | |
7091 | /* Put the image into the pixmap, then free the X image and its buffer. */ | |
7092 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
7093 | x_destroy_x_image (ximg); | |
7094 | xfree (buf); | |
7095 | ||
7096 | UNGCPRO; | |
7097 | return 1; | |
7098 | } | |
7099 | ||
7100 | #else /* HAVE_TIFF */ | |
7101 | ||
edfda783 AR |
7102 | #ifdef HAVE_NS |
7103 | static int | |
7104 | tiff_load (struct frame *f, struct image *img) | |
7105 | { | |
7106 | return ns_load_image(f, img, | |
7107 | image_spec_value (img->spec, QCfile, NULL), | |
7108 | image_spec_value (img->spec, QCdata, NULL)); | |
7109 | } | |
7110 | #endif /* HAVE_NS */ | |
7111 | ||
4ef0d4d0 KS |
7112 | #endif /* !HAVE_TIFF */ |
7113 | ||
7114 | ||
7115 | \f | |
7116 | /*********************************************************************** | |
7117 | GIF | |
7118 | ***********************************************************************/ | |
7119 | ||
9e2a2647 | 7120 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
4ef0d4d0 KS |
7121 | |
7122 | static int gif_image_p P_ ((Lisp_Object object)); | |
7123 | static int gif_load P_ ((struct frame *f, struct image *img)); | |
6ac3c7bb | 7124 | static void gif_clear_image P_ ((struct frame *f, struct image *img)); |
4ef0d4d0 KS |
7125 | |
7126 | /* The symbol `gif' identifying images of this type. */ | |
7127 | ||
7128 | Lisp_Object Qgif; | |
7129 | ||
7130 | /* Indices of image specification fields in gif_format, below. */ | |
7131 | ||
7132 | enum gif_keyword_index | |
7133 | { | |
7134 | GIF_TYPE, | |
7135 | GIF_DATA, | |
7136 | GIF_FILE, | |
7137 | GIF_ASCENT, | |
7138 | GIF_MARGIN, | |
7139 | GIF_RELIEF, | |
7140 | GIF_ALGORITHM, | |
7141 | GIF_HEURISTIC_MASK, | |
7142 | GIF_MASK, | |
7143 | GIF_IMAGE, | |
7144 | GIF_BACKGROUND, | |
7145 | GIF_LAST | |
7146 | }; | |
7147 | ||
7148 | /* Vector of image_keyword structures describing the format | |
7149 | of valid user-defined image specifications. */ | |
7150 | ||
91433552 | 7151 | static const struct image_keyword gif_format[GIF_LAST] = |
4ef0d4d0 KS |
7152 | { |
7153 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7154 | {":data", IMAGE_STRING_VALUE, 0}, | |
7155 | {":file", IMAGE_STRING_VALUE, 0}, | |
7156 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
7157 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
7158 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
7159 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7160 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7161 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 | 7162 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}, |
4ef0d4d0 KS |
7163 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} |
7164 | }; | |
7165 | ||
7166 | /* Structure describing the image type `gif'. */ | |
7167 | ||
7168 | static struct image_type gif_type = | |
7169 | { | |
7170 | &Qgif, | |
7171 | gif_image_p, | |
7172 | gif_load, | |
6ac3c7bb | 7173 | gif_clear_image, |
4ef0d4d0 KS |
7174 | NULL |
7175 | }; | |
7176 | ||
6ac3c7bb KS |
7177 | /* Free X resources of GIF image IMG which is used on frame F. */ |
7178 | ||
7179 | static void | |
7180 | gif_clear_image (f, img) | |
7181 | struct frame *f; | |
7182 | struct image *img; | |
7183 | { | |
7184 | /* IMG->data.ptr_val may contain extension data. */ | |
7185 | img->data.lisp_val = Qnil; | |
7186 | x_clear_image (f, img); | |
7187 | } | |
7188 | ||
4ef0d4d0 KS |
7189 | /* Return non-zero if OBJECT is a valid GIF image specification. */ |
7190 | ||
7191 | static int | |
7192 | gif_image_p (object) | |
7193 | Lisp_Object object; | |
7194 | { | |
7195 | struct image_keyword fmt[GIF_LAST]; | |
7196 | bcopy (gif_format, fmt, sizeof fmt); | |
7197 | ||
7198 | if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)) | |
7199 | return 0; | |
7200 | ||
7201 | /* Must specify either the :data or :file keyword. */ | |
7202 | return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1; | |
7203 | } | |
7204 | ||
9e2a2647 | 7205 | #endif /* HAVE_GIF */ |
4ef0d4d0 KS |
7206 | |
7207 | #ifdef HAVE_GIF | |
7208 | ||
9e2a2647 | 7209 | #if defined (HAVE_NTGUI) |
2e09fef1 EZ |
7210 | /* winuser.h might define DrawText to DrawTextA or DrawTextW. |
7211 | Undefine before redefining to avoid a preprocessor warning. */ | |
7212 | #ifdef DrawText | |
7213 | #undef DrawText | |
7214 | #endif | |
4ef0d4d0 KS |
7215 | /* avoid conflict with QuickdrawText.h */ |
7216 | #define DrawText gif_DrawText | |
7217 | #include <gif_lib.h> | |
7218 | #undef DrawText | |
7219 | ||
9e2a2647 | 7220 | #else /* HAVE_NTGUI */ |
4ef0d4d0 KS |
7221 | |
7222 | #include <gif_lib.h> | |
7223 | ||
9e2a2647 | 7224 | #endif /* HAVE_NTGUI */ |
4ef0d4d0 KS |
7225 | |
7226 | ||
7227 | #ifdef HAVE_NTGUI | |
7228 | ||
7229 | /* GIF library details. */ | |
7230 | DEF_IMGLIB_FN (DGifCloseFile); | |
7231 | DEF_IMGLIB_FN (DGifSlurp); | |
7232 | DEF_IMGLIB_FN (DGifOpen); | |
7233 | DEF_IMGLIB_FN (DGifOpenFileName); | |
7234 | ||
7235 | static int | |
0855eb52 | 7236 | init_gif_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
7237 | { |
7238 | HMODULE library; | |
7239 | ||
0855eb52 | 7240 | if (!(library = w32_delayed_load (libraries, Qgif))) |
4ef0d4d0 KS |
7241 | return 0; |
7242 | ||
7243 | LOAD_IMGLIB_FN (library, DGifCloseFile); | |
7244 | LOAD_IMGLIB_FN (library, DGifSlurp); | |
7245 | LOAD_IMGLIB_FN (library, DGifOpen); | |
7246 | LOAD_IMGLIB_FN (library, DGifOpenFileName); | |
7247 | return 1; | |
7248 | } | |
7249 | ||
7250 | #else | |
7251 | ||
7252 | #define fn_DGifCloseFile DGifCloseFile | |
7253 | #define fn_DGifSlurp DGifSlurp | |
7254 | #define fn_DGifOpen DGifOpen | |
7255 | #define fn_DGifOpenFileName DGifOpenFileName | |
7256 | ||
7257 | #endif /* HAVE_NTGUI */ | |
7258 | ||
7259 | /* Reading a GIF image from memory | |
7260 | Based on the PNG memory stuff to a certain extent. */ | |
7261 | ||
7262 | typedef struct | |
7263 | { | |
7264 | unsigned char *bytes; | |
7265 | size_t len; | |
7266 | int index; | |
7267 | } | |
7268 | gif_memory_source; | |
7269 | ||
7270 | /* Make the current memory source available to gif_read_from_memory. | |
7271 | It's done this way because not all versions of libungif support | |
7272 | a UserData field in the GifFileType structure. */ | |
7273 | static gif_memory_source *current_gif_memory_src; | |
7274 | ||
7275 | static int | |
7276 | gif_read_from_memory (file, buf, len) | |
7277 | GifFileType *file; | |
7278 | GifByteType *buf; | |
7279 | int len; | |
7280 | { | |
7281 | gif_memory_source *src = current_gif_memory_src; | |
7282 | ||
7283 | if (len > src->len - src->index) | |
7284 | return -1; | |
7285 | ||
7286 | bcopy (src->bytes + src->index, buf, len); | |
7287 | src->index += len; | |
7288 | return len; | |
7289 | } | |
7290 | ||
7291 | ||
7292 | /* Load GIF image IMG for use on frame F. Value is non-zero if | |
7293 | successful. */ | |
7294 | ||
91433552 DN |
7295 | static const int interlace_start[] = {0, 4, 2, 1}; |
7296 | static const int interlace_increment[] = {8, 8, 4, 2}; | |
aca946f3 | 7297 | |
4ef0d4d0 KS |
7298 | static int |
7299 | gif_load (f, img) | |
7300 | struct frame *f; | |
7301 | struct image *img; | |
7302 | { | |
7303 | Lisp_Object file, specified_file; | |
7304 | Lisp_Object specified_data; | |
7305 | int rc, width, height, x, y, i; | |
7306 | XImagePtr ximg; | |
7307 | ColorMapObject *gif_color_map; | |
7308 | unsigned long pixel_colors[256]; | |
7309 | GifFileType *gif; | |
7310 | struct gcpro gcpro1; | |
7311 | Lisp_Object image; | |
6ac3c7bb | 7312 | int ino, image_height, image_width; |
4ef0d4d0 KS |
7313 | gif_memory_source memsrc; |
7314 | unsigned char *raster; | |
7315 | ||
7316 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
7317 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
7318 | file = Qnil; | |
7319 | GCPRO1 (file); | |
7320 | ||
7321 | if (NILP (specified_data)) | |
7322 | { | |
7323 | file = x_find_image_file (specified_file); | |
7324 | if (!STRINGP (file)) | |
7325 | { | |
7326 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
7327 | UNGCPRO; | |
7328 | return 0; | |
7329 | } | |
7330 | ||
2e09fef1 EZ |
7331 | /* Open the GIF file. Casting return value avoids a GCC warning |
7332 | on W32. */ | |
7333 | gif = (GifFileType *)fn_DGifOpenFileName (SDATA (file)); | |
4ef0d4d0 KS |
7334 | if (gif == NULL) |
7335 | { | |
7336 | image_error ("Cannot open `%s'", file, Qnil); | |
7337 | UNGCPRO; | |
7338 | return 0; | |
7339 | } | |
7340 | } | |
7341 | else | |
7342 | { | |
7343 | /* Read from memory! */ | |
7344 | current_gif_memory_src = &memsrc; | |
7345 | memsrc.bytes = SDATA (specified_data); | |
7346 | memsrc.len = SBYTES (specified_data); | |
7347 | memsrc.index = 0; | |
7348 | ||
2e09fef1 | 7349 | /* Casting return value avoids a GCC warning on W32. */ |
5fc9fdeb | 7350 | gif = (GifFileType *) fn_DGifOpen (&memsrc, gif_read_from_memory); |
4ef0d4d0 KS |
7351 | if (!gif) |
7352 | { | |
7353 | image_error ("Cannot open memory source `%s'", img->spec, Qnil); | |
7354 | UNGCPRO; | |
7355 | return 0; | |
7356 | } | |
7357 | } | |
7358 | ||
f1f25b99 CY |
7359 | /* Before reading entire contents, check the declared image size. */ |
7360 | if (!check_image_size (f, gif->SWidth, gif->SHeight)) | |
7361 | { | |
10ea2b82 | 7362 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
7363 | fn_DGifCloseFile (gif); |
7364 | UNGCPRO; | |
7365 | return 0; | |
7366 | } | |
7367 | ||
4ef0d4d0 KS |
7368 | /* Read entire contents. */ |
7369 | rc = fn_DGifSlurp (gif); | |
7370 | if (rc == GIF_ERROR) | |
7371 | { | |
7372 | image_error ("Error reading `%s'", img->spec, Qnil); | |
7373 | fn_DGifCloseFile (gif); | |
7374 | UNGCPRO; | |
7375 | return 0; | |
7376 | } | |
7377 | ||
7378 | image = image_spec_value (img->spec, QCindex, NULL); | |
7379 | ino = INTEGERP (image) ? XFASTINT (image) : 0; | |
7380 | if (ino >= gif->ImageCount) | |
7381 | { | |
7382 | image_error ("Invalid image number `%s' in image `%s'", | |
7383 | image, img->spec); | |
7384 | fn_DGifCloseFile (gif); | |
7385 | UNGCPRO; | |
7386 | return 0; | |
7387 | } | |
7388 | ||
6ac3c7bb KS |
7389 | img->corners[TOP_CORNER] = gif->SavedImages[ino].ImageDesc.Top; |
7390 | img->corners[LEFT_CORNER] = gif->SavedImages[ino].ImageDesc.Left; | |
0f4aca46 | 7391 | image_height = gif->SavedImages[ino].ImageDesc.Height; |
6ac3c7bb KS |
7392 | img->corners[BOT_CORNER] = img->corners[TOP_CORNER] + image_height; |
7393 | image_width = gif->SavedImages[ino].ImageDesc.Width; | |
7394 | img->corners[RIGHT_CORNER] = img->corners[LEFT_CORNER] + image_width; | |
0f4aca46 KS |
7395 | |
7396 | width = img->width = max (gif->SWidth, | |
7397 | max (gif->Image.Left + gif->Image.Width, | |
6ac3c7bb | 7398 | img->corners[RIGHT_CORNER])); |
0f4aca46 KS |
7399 | height = img->height = max (gif->SHeight, |
7400 | max (gif->Image.Top + gif->Image.Height, | |
6ac3c7bb | 7401 | img->corners[BOT_CORNER])); |
4ef0d4d0 | 7402 | |
f1f25b99 CY |
7403 | if (!check_image_size (f, width, height)) |
7404 | { | |
10ea2b82 | 7405 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
7406 | fn_DGifCloseFile (gif); |
7407 | UNGCPRO; | |
7408 | return 0; | |
7409 | } | |
7410 | ||
4ef0d4d0 KS |
7411 | /* Create the X image and pixmap. */ |
7412 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) | |
7413 | { | |
7414 | fn_DGifCloseFile (gif); | |
7415 | UNGCPRO; | |
7416 | return 0; | |
7417 | } | |
7418 | ||
7419 | /* Allocate colors. */ | |
7420 | gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap; | |
7421 | if (!gif_color_map) | |
7422 | gif_color_map = gif->SColorMap; | |
7423 | init_color_table (); | |
7424 | bzero (pixel_colors, sizeof pixel_colors); | |
7425 | ||
1ab2cd43 TTN |
7426 | if (gif_color_map) |
7427 | for (i = 0; i < gif_color_map->ColorCount; ++i) | |
7428 | { | |
7429 | int r = gif_color_map->Colors[i].Red << 8; | |
7430 | int g = gif_color_map->Colors[i].Green << 8; | |
7431 | int b = gif_color_map->Colors[i].Blue << 8; | |
7432 | pixel_colors[i] = lookup_rgb_color (f, r, g, b); | |
7433 | } | |
4ef0d4d0 KS |
7434 | |
7435 | #ifdef COLOR_TABLE_SUPPORT | |
7436 | img->colors = colors_in_color_table (&img->ncolors); | |
7437 | free_color_table (); | |
7438 | #endif /* COLOR_TABLE_SUPPORT */ | |
7439 | ||
7440 | /* Clear the part of the screen image that are not covered by | |
7441 | the image from the GIF file. Full animated GIF support | |
7442 | requires more than can be done here (see the gif89 spec, | |
7443 | disposal methods). Let's simply assume that the part | |
7444 | not covered by a sub-image is in the frame's background color. */ | |
6ac3c7bb | 7445 | for (y = 0; y < img->corners[TOP_CORNER]; ++y) |
4ef0d4d0 KS |
7446 | for (x = 0; x < width; ++x) |
7447 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7448 | ||
6ac3c7bb | 7449 | for (y = img->corners[BOT_CORNER]; y < height; ++y) |
4ef0d4d0 KS |
7450 | for (x = 0; x < width; ++x) |
7451 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7452 | ||
6ac3c7bb | 7453 | for (y = img->corners[TOP_CORNER]; y < img->corners[BOT_CORNER]; ++y) |
4ef0d4d0 | 7454 | { |
6ac3c7bb | 7455 | for (x = 0; x < img->corners[LEFT_CORNER]; ++x) |
4ef0d4d0 | 7456 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
6ac3c7bb | 7457 | for (x = img->corners[RIGHT_CORNER]; x < width; ++x) |
4ef0d4d0 KS |
7458 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
7459 | } | |
7460 | ||
7461 | /* Read the GIF image into the X image. We use a local variable | |
7462 | `raster' here because RasterBits below is a char *, and invites | |
7463 | problems with bytes >= 0x80. */ | |
7464 | raster = (unsigned char *) gif->SavedImages[ino].RasterBits; | |
7465 | ||
7466 | if (gif->SavedImages[ino].ImageDesc.Interlace) | |
7467 | { | |
4ef0d4d0 KS |
7468 | int pass; |
7469 | int row = interlace_start[0]; | |
7470 | ||
7471 | pass = 0; | |
7472 | ||
7473 | for (y = 0; y < image_height; y++) | |
7474 | { | |
7475 | if (row >= image_height) | |
7476 | { | |
7477 | row = interlace_start[++pass]; | |
7478 | while (row >= image_height) | |
7479 | row = interlace_start[++pass]; | |
7480 | } | |
7481 | ||
7482 | for (x = 0; x < image_width; x++) | |
7483 | { | |
7484 | int i = raster[(y * image_width) + x]; | |
6ac3c7bb KS |
7485 | XPutPixel (ximg, x + img->corners[LEFT_CORNER], |
7486 | row + img->corners[TOP_CORNER], pixel_colors[i]); | |
4ef0d4d0 KS |
7487 | } |
7488 | ||
7489 | row += interlace_increment[pass]; | |
7490 | } | |
7491 | } | |
7492 | else | |
7493 | { | |
7494 | for (y = 0; y < image_height; ++y) | |
7495 | for (x = 0; x < image_width; ++x) | |
7496 | { | |
7497 | int i = raster[y * image_width + x]; | |
6ac3c7bb KS |
7498 | XPutPixel (ximg, x + img->corners[LEFT_CORNER], |
7499 | y + img->corners[TOP_CORNER], pixel_colors[i]); | |
4ef0d4d0 KS |
7500 | } |
7501 | } | |
7502 | ||
6ac3c7bb KS |
7503 | /* Save GIF image extension data for `image-extension-data'. |
7504 | Format is (count IMAGES FUNCTION "BYTES" ...). */ | |
7505 | img->data.lisp_val = Qnil; | |
7506 | if (gif->SavedImages[ino].ExtensionBlockCount > 0) | |
7507 | { | |
7508 | ExtensionBlock *ext = gif->SavedImages[ino].ExtensionBlocks; | |
7509 | for (i = 0; i < gif->SavedImages[ino].ExtensionBlockCount; i++, ext++) | |
7510 | /* Append (... FUNCTION "BYTES") */ | |
7511 | img->data.lisp_val = Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount), | |
7512 | Fcons (make_number (ext->Function), | |
7513 | img->data.lisp_val)); | |
7514 | img->data.lisp_val = Fnreverse (img->data.lisp_val); | |
7515 | } | |
7516 | if (gif->ImageCount > 1) | |
7517 | img->data.lisp_val = Fcons (Qcount, | |
7518 | Fcons (make_number (gif->ImageCount), | |
7519 | img->data.lisp_val)); | |
7520 | ||
4ef0d4d0 KS |
7521 | fn_DGifCloseFile (gif); |
7522 | ||
7523 | /* Maybe fill in the background field while we have ximg handy. */ | |
7524 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7525 | /* Casting avoids a GCC warning. */ |
7526 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
7527 | |
7528 | /* Put the image into the pixmap, then free the X image and its buffer. */ | |
7529 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
7530 | x_destroy_x_image (ximg); | |
7531 | ||
7532 | UNGCPRO; | |
7533 | return 1; | |
7534 | } | |
7535 | ||
2e09fef1 | 7536 | #else /* !HAVE_GIF */ |
4ef0d4d0 | 7537 | |
edfda783 AR |
7538 | #ifdef HAVE_NS |
7539 | static int | |
7540 | gif_load (struct frame *f, struct image *img) | |
7541 | { | |
7542 | return ns_load_image(f, img, | |
7543 | image_spec_value (img->spec, QCfile, NULL), | |
7544 | image_spec_value (img->spec, QCdata, NULL)); | |
7545 | } | |
7546 | #endif /* HAVE_NS */ | |
7547 | ||
4ef0d4d0 KS |
7548 | #endif /* HAVE_GIF */ |
7549 | ||
7550 | ||
7551 | \f | |
4ab27a43 GM |
7552 | /*********************************************************************** |
7553 | SVG | |
7554 | ***********************************************************************/ | |
7555 | ||
7556 | #if defined (HAVE_RSVG) | |
7557 | ||
7558 | /* Function prototypes. */ | |
7559 | ||
7560 | static int svg_image_p P_ ((Lisp_Object object)); | |
7561 | static int svg_load P_ ((struct frame *f, struct image *img)); | |
7562 | ||
7563 | static int svg_load_image P_ ((struct frame *, struct image *, | |
7564 | unsigned char *, unsigned int)); | |
7565 | ||
7566 | /* The symbol `svg' identifying images of this type. */ | |
7567 | ||
7568 | Lisp_Object Qsvg; | |
7569 | ||
7570 | /* Indices of image specification fields in svg_format, below. */ | |
7571 | ||
7572 | enum svg_keyword_index | |
7573 | { | |
7574 | SVG_TYPE, | |
7575 | SVG_DATA, | |
7576 | SVG_FILE, | |
7577 | SVG_ASCENT, | |
7578 | SVG_MARGIN, | |
7579 | SVG_RELIEF, | |
7580 | SVG_ALGORITHM, | |
7581 | SVG_HEURISTIC_MASK, | |
7582 | SVG_MASK, | |
7583 | SVG_BACKGROUND, | |
7584 | SVG_LAST | |
7585 | }; | |
7586 | ||
7587 | /* Vector of image_keyword structures describing the format | |
7588 | of valid user-defined image specifications. */ | |
7589 | ||
91433552 | 7590 | static const struct image_keyword svg_format[SVG_LAST] = |
4ab27a43 GM |
7591 | { |
7592 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7593 | {":data", IMAGE_STRING_VALUE, 0}, | |
7594 | {":file", IMAGE_STRING_VALUE, 0}, | |
7595 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
7596 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
7597 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
7598 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7599 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7600 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7601 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
7602 | }; | |
7603 | ||
7604 | /* Structure describing the image type `svg'. Its the same type of | |
7605 | structure defined for all image formats, handled by emacs image | |
7606 | functions. See struct image_type in dispextern.h. */ | |
7607 | ||
7608 | static struct image_type svg_type = | |
7609 | { | |
7610 | /* An identifier showing that this is an image structure for the SVG format. */ | |
7611 | &Qsvg, | |
7612 | /* Handle to a function that can be used to identify a SVG file. */ | |
7613 | svg_image_p, | |
7614 | /* Handle to function used to load a SVG file. */ | |
7615 | svg_load, | |
7616 | /* Handle to function to free sresources for SVG. */ | |
7617 | x_clear_image, | |
7618 | /* An internal field to link to the next image type in a list of | |
7619 | image types, will be filled in when registering the format. */ | |
c21e6151 | 7620 | NULL |
4ab27a43 GM |
7621 | }; |
7622 | ||
7623 | ||
7624 | /* Return non-zero if OBJECT is a valid SVG image specification. Do | |
7625 | this by calling parse_image_spec and supplying the keywords that | |
7626 | identify the SVG format. */ | |
7627 | ||
7628 | static int | |
7629 | svg_image_p (object) | |
7630 | Lisp_Object object; | |
7631 | { | |
7632 | struct image_keyword fmt[SVG_LAST]; | |
7633 | bcopy (svg_format, fmt, sizeof fmt); | |
7634 | ||
7635 | if (!parse_image_spec (object, fmt, SVG_LAST, Qsvg)) | |
7636 | return 0; | |
7637 | ||
7638 | /* Must specify either the :data or :file keyword. */ | |
7639 | return fmt[SVG_FILE].count + fmt[SVG_DATA].count == 1; | |
7640 | } | |
7641 | ||
7642 | #include <librsvg/rsvg.h> | |
7643 | ||
5fc9fdeb JR |
7644 | #ifdef HAVE_NTGUI |
7645 | ||
7646 | /* SVG library functions. */ | |
7647 | DEF_IMGLIB_FN (rsvg_handle_new); | |
e025c9c2 | 7648 | DEF_IMGLIB_FN (rsvg_handle_get_dimensions); |
5fc9fdeb JR |
7649 | DEF_IMGLIB_FN (rsvg_handle_write); |
7650 | DEF_IMGLIB_FN (rsvg_handle_close); | |
7651 | DEF_IMGLIB_FN (rsvg_handle_get_pixbuf); | |
7652 | DEF_IMGLIB_FN (rsvg_handle_free); | |
7653 | ||
7654 | DEF_IMGLIB_FN (gdk_pixbuf_get_width); | |
7655 | DEF_IMGLIB_FN (gdk_pixbuf_get_height); | |
7656 | DEF_IMGLIB_FN (gdk_pixbuf_get_pixels); | |
7657 | DEF_IMGLIB_FN (gdk_pixbuf_get_rowstride); | |
7658 | DEF_IMGLIB_FN (gdk_pixbuf_get_colorspace); | |
7659 | DEF_IMGLIB_FN (gdk_pixbuf_get_n_channels); | |
7660 | DEF_IMGLIB_FN (gdk_pixbuf_get_has_alpha); | |
7661 | DEF_IMGLIB_FN (gdk_pixbuf_get_bits_per_sample); | |
7662 | ||
7663 | DEF_IMGLIB_FN (g_type_init); | |
7664 | DEF_IMGLIB_FN (g_object_unref); | |
7665 | DEF_IMGLIB_FN (g_error_free); | |
7666 | ||
df23bf08 | 7667 | Lisp_Object Qgdk_pixbuf, Qglib, Qgobject; |
5fc9fdeb JR |
7668 | |
7669 | static int | |
7670 | init_svg_functions (Lisp_Object libraries) | |
7671 | { | |
df23bf08 | 7672 | HMODULE library, gdklib, glib, gobject; |
5fc9fdeb JR |
7673 | |
7674 | if (!(glib = w32_delayed_load (libraries, Qglib)) | |
df23bf08 | 7675 | || !(gobject = w32_delayed_load (libraries, Qgobject)) |
5fc9fdeb JR |
7676 | || !(gdklib = w32_delayed_load (libraries, Qgdk_pixbuf)) |
7677 | || !(library = w32_delayed_load (libraries, Qsvg))) | |
7678 | return 0; | |
7679 | ||
7680 | LOAD_IMGLIB_FN (library, rsvg_handle_new); | |
e025c9c2 | 7681 | LOAD_IMGLIB_FN (library, rsvg_handle_get_dimensions); |
5fc9fdeb JR |
7682 | LOAD_IMGLIB_FN (library, rsvg_handle_write); |
7683 | LOAD_IMGLIB_FN (library, rsvg_handle_close); | |
7684 | LOAD_IMGLIB_FN (library, rsvg_handle_get_pixbuf); | |
7685 | LOAD_IMGLIB_FN (library, rsvg_handle_free); | |
7686 | ||
7687 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_width); | |
7688 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_height); | |
7689 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_pixels); | |
7690 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_rowstride); | |
7691 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_colorspace); | |
7692 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_n_channels); | |
7693 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_has_alpha); | |
7694 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_bits_per_sample); | |
7695 | ||
df23bf08 JR |
7696 | LOAD_IMGLIB_FN (gobject, g_type_init); |
7697 | LOAD_IMGLIB_FN (gobject, g_object_unref); | |
5fc9fdeb | 7698 | LOAD_IMGLIB_FN (glib, g_error_free); |
df23bf08 | 7699 | |
5fc9fdeb JR |
7700 | return 1; |
7701 | } | |
4ab27a43 | 7702 | |
5fc9fdeb JR |
7703 | #else |
7704 | /* The following aliases for library functions allow dynamic loading | |
7705 | to be used on some platforms. */ | |
4ab27a43 | 7706 | #define fn_rsvg_handle_new rsvg_handle_new |
e025c9c2 | 7707 | #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions |
4ab27a43 GM |
7708 | #define fn_rsvg_handle_write rsvg_handle_write |
7709 | #define fn_rsvg_handle_close rsvg_handle_close | |
7710 | #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf | |
7711 | #define fn_rsvg_handle_free rsvg_handle_free | |
7712 | ||
5fc9fdeb JR |
7713 | #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width |
7714 | #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height | |
7715 | #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels | |
7716 | #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride | |
7717 | #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace | |
7718 | #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels | |
7719 | #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha | |
4ab27a43 GM |
7720 | #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample |
7721 | ||
5fc9fdeb JR |
7722 | #define fn_g_type_init g_type_init |
7723 | #define fn_g_object_unref g_object_unref | |
7724 | #define fn_g_error_free g_error_free | |
7725 | #endif /* !HAVE_NTGUI */ | |
4ab27a43 GM |
7726 | |
7727 | /* Load SVG image IMG for use on frame F. Value is non-zero if | |
7728 | successful. this function will go into the svg_type structure, and | |
7729 | the prototype thus needs to be compatible with that structure. */ | |
7730 | ||
7731 | static int | |
7732 | svg_load (f, img) | |
7733 | struct frame *f; | |
7734 | struct image *img; | |
7735 | { | |
7736 | int success_p = 0; | |
7737 | Lisp_Object file_name; | |
7738 | ||
7739 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
7740 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
7741 | if (STRINGP (file_name)) | |
7742 | { | |
7743 | Lisp_Object file; | |
7744 | unsigned char *contents; | |
7745 | int size; | |
7746 | struct gcpro gcpro1; | |
7747 | ||
7748 | file = x_find_image_file (file_name); | |
7749 | GCPRO1 (file); | |
7750 | if (!STRINGP (file)) | |
c21e6151 YM |
7751 | { |
7752 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
7753 | UNGCPRO; | |
7754 | return 0; | |
7755 | } | |
7756 | ||
4ab27a43 GM |
7757 | /* Read the entire file into memory. */ |
7758 | contents = slurp_file (SDATA (file), &size); | |
7759 | if (contents == NULL) | |
c21e6151 YM |
7760 | { |
7761 | image_error ("Error loading SVG image `%s'", img->spec, Qnil); | |
7762 | UNGCPRO; | |
7763 | return 0; | |
7764 | } | |
4ab27a43 | 7765 | /* If the file was slurped into memory properly, parse it. */ |
c21e6151 | 7766 | success_p = svg_load_image (f, img, contents, size); |
4ab27a43 GM |
7767 | xfree (contents); |
7768 | UNGCPRO; | |
7769 | } | |
7770 | /* Else its not a file, its a lisp object. Load the image from a | |
7771 | lisp object rather than a file. */ | |
7772 | else | |
7773 | { | |
7774 | Lisp_Object data; | |
7775 | ||
7776 | data = image_spec_value (img->spec, QCdata, NULL); | |
7777 | success_p = svg_load_image (f, img, SDATA (data), SBYTES (data)); | |
7778 | } | |
7779 | ||
7780 | return success_p; | |
7781 | } | |
7782 | ||
bbe6f2aa JB |
7783 | /* svg_load_image is a helper function for svg_load, which does the |
7784 | actual loading given contents and size, apart from frame and image | |
7785 | structures, passed from svg_load. | |
4ab27a43 | 7786 | |
bbe6f2aa | 7787 | Uses librsvg to do most of the image processing. |
c21e6151 | 7788 | |
bbe6f2aa | 7789 | Returns non-zero when successful. */ |
4ab27a43 GM |
7790 | static int |
7791 | svg_load_image (f, img, contents, size) | |
bbe6f2aa | 7792 | /* Pointer to emacs frame structure. */ |
4ab27a43 GM |
7793 | struct frame *f; |
7794 | /* Pointer to emacs image structure. */ | |
7795 | struct image *img; | |
7796 | /* String containing the SVG XML data to be parsed. */ | |
7797 | unsigned char *contents; | |
7798 | /* Size of data in bytes. */ | |
7799 | unsigned int size; | |
7800 | { | |
7801 | RsvgHandle *rsvg_handle; | |
e025c9c2 | 7802 | RsvgDimensionData dimension_data; |
4ab27a43 GM |
7803 | GError *error = NULL; |
7804 | GdkPixbuf *pixbuf; | |
7805 | int width; | |
7806 | int height; | |
7807 | const guint8 *pixels; | |
7808 | int rowstride; | |
7809 | XImagePtr ximg; | |
c21e6151 | 7810 | Lisp_Object specified_bg; |
4ab27a43 GM |
7811 | XColor background; |
7812 | int x; | |
7813 | int y; | |
7814 | ||
7815 | /* g_type_init is a glib function that must be called prior to using | |
7816 | gnome type library functions. */ | |
5fc9fdeb | 7817 | fn_g_type_init (); |
4ab27a43 | 7818 | /* Make a handle to a new rsvg object. */ |
8b7d0a16 | 7819 | rsvg_handle = (RsvgHandle *) fn_rsvg_handle_new (); |
4ab27a43 GM |
7820 | |
7821 | /* Parse the contents argument and fill in the rsvg_handle. */ | |
7822 | fn_rsvg_handle_write (rsvg_handle, contents, size, &error); | |
e025c9c2 | 7823 | if (error) goto rsvg_error; |
4ab27a43 GM |
7824 | |
7825 | /* The parsing is complete, rsvg_handle is ready to used, close it | |
7826 | for further writes. */ | |
7827 | fn_rsvg_handle_close (rsvg_handle, &error); | |
e025c9c2 CY |
7828 | if (error) goto rsvg_error; |
7829 | ||
7830 | fn_rsvg_handle_get_dimensions (rsvg_handle, &dimension_data); | |
7831 | if (! check_image_size (f, dimension_data.width, dimension_data.height)) | |
10ea2b82 JR |
7832 | { |
7833 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
7834 | goto rsvg_error; | |
7835 | } | |
b2996e57 | 7836 | |
4ab27a43 GM |
7837 | /* We can now get a valid pixel buffer from the svg file, if all |
7838 | went ok. */ | |
8b7d0a16 | 7839 | pixbuf = (GdkPixbuf *) fn_rsvg_handle_get_pixbuf (rsvg_handle); |
e025c9c2 CY |
7840 | if (!pixbuf) goto rsvg_error; |
7841 | fn_g_object_unref (rsvg_handle); | |
4ab27a43 GM |
7842 | |
7843 | /* Extract some meta data from the svg handle. */ | |
7844 | width = fn_gdk_pixbuf_get_width (pixbuf); | |
7845 | height = fn_gdk_pixbuf_get_height (pixbuf); | |
8b7d0a16 | 7846 | pixels = (const guint8 *) fn_gdk_pixbuf_get_pixels (pixbuf); |
4ab27a43 GM |
7847 | rowstride = fn_gdk_pixbuf_get_rowstride (pixbuf); |
7848 | ||
7849 | /* Validate the svg meta data. */ | |
7850 | eassert (fn_gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB); | |
7851 | eassert (fn_gdk_pixbuf_get_n_channels (pixbuf) == 4); | |
7852 | eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf)); | |
7853 | eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf) == 8); | |
7854 | ||
7855 | /* Try to create a x pixmap to hold the svg pixmap. */ | |
c21e6151 YM |
7856 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) |
7857 | { | |
5fc9fdeb | 7858 | fn_g_object_unref (pixbuf); |
c21e6151 YM |
7859 | return 0; |
7860 | } | |
4ab27a43 GM |
7861 | |
7862 | init_color_table (); | |
7863 | ||
c21e6151 YM |
7864 | /* Handle alpha channel by combining the image with a background |
7865 | color. */ | |
7866 | specified_bg = image_spec_value (img->spec, QCbackground, NULL); | |
8d3c4e79 CY |
7867 | if (!STRINGP (specified_bg) |
7868 | || !x_defined_color (f, SDATA (specified_bg), &background, 0)) | |
c21e6151 | 7869 | { |
d7e008d0 | 7870 | #ifndef HAVE_NS |
c21e6151 YM |
7871 | background.pixel = FRAME_BACKGROUND_PIXEL (f); |
7872 | x_query_color (f, &background); | |
d7e008d0 AR |
7873 | #else |
7874 | ns_query_color(FRAME_BACKGROUND_COLOR (f), &background, 1); | |
7875 | #endif | |
c21e6151 | 7876 | } |
4ab27a43 | 7877 | |
8d3c4e79 CY |
7878 | /* SVG pixmaps specify transparency in the last byte, so right |
7879 | shift 8 bits to get rid of it, since emacs doesn't support | |
7880 | transparency. */ | |
7881 | background.red >>= 8; | |
7882 | background.green >>= 8; | |
7883 | background.blue >>= 8; | |
7884 | ||
4ab27a43 GM |
7885 | /* This loop handles opacity values, since Emacs assumes |
7886 | non-transparent images. Each pixel must be "flattened" by | |
bbe6f2aa JB |
7887 | calculating the resulting color, given the transparency of the |
7888 | pixel, and the image background color. */ | |
4ab27a43 GM |
7889 | for (y = 0; y < height; ++y) |
7890 | { | |
7891 | for (x = 0; x < width; ++x) | |
7892 | { | |
7893 | unsigned red; | |
7894 | unsigned green; | |
7895 | unsigned blue; | |
7896 | unsigned opacity; | |
7897 | ||
7898 | red = *pixels++; | |
7899 | green = *pixels++; | |
7900 | blue = *pixels++; | |
7901 | opacity = *pixels++; | |
7902 | ||
7903 | red = ((red * opacity) | |
7904 | + (background.red * ((1 << 8) - opacity))); | |
7905 | green = ((green * opacity) | |
7906 | + (background.green * ((1 << 8) - opacity))); | |
7907 | blue = ((blue * opacity) | |
7908 | + (background.blue * ((1 << 8) - opacity))); | |
7909 | ||
7910 | XPutPixel (ximg, x, y, lookup_rgb_color (f, red, green, blue)); | |
7911 | } | |
7912 | ||
7913 | pixels += rowstride - 4 * width; | |
7914 | } | |
7915 | ||
7916 | #ifdef COLOR_TABLE_SUPPORT | |
7917 | /* Remember colors allocated for this image. */ | |
7918 | img->colors = colors_in_color_table (&img->ncolors); | |
7919 | free_color_table (); | |
7920 | #endif /* COLOR_TABLE_SUPPORT */ | |
7921 | ||
5fc9fdeb | 7922 | fn_g_object_unref (pixbuf); |
4ab27a43 | 7923 | |
c21e6151 YM |
7924 | img->width = width; |
7925 | img->height = height; | |
7926 | ||
7927 | /* Maybe fill in the background field while we have ximg handy. | |
7928 | Casting avoids a GCC warning. */ | |
7929 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
7930 | ||
4ab27a43 GM |
7931 | /* Put the image into the pixmap, then free the X image and its |
7932 | buffer. */ | |
7933 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
7934 | x_destroy_x_image (ximg); | |
7935 | ||
4ab27a43 GM |
7936 | return 1; |
7937 | ||
7938 | rsvg_error: | |
e025c9c2 | 7939 | fn_g_object_unref (rsvg_handle); |
4ab27a43 GM |
7940 | /* FIXME: Use error->message so the user knows what is the actual |
7941 | problem with the image. */ | |
7942 | image_error ("Error parsing SVG image `%s'", img->spec, Qnil); | |
5fc9fdeb | 7943 | fn_g_error_free (error); |
4ab27a43 GM |
7944 | return 0; |
7945 | } | |
7946 | ||
7947 | #endif /* defined (HAVE_RSVG) */ | |
7948 | ||
7949 | ||
7950 | ||
4ef0d4d0 KS |
7951 | \f |
7952 | /*********************************************************************** | |
7953 | Ghostscript | |
7954 | ***********************************************************************/ | |
7955 | ||
7956 | #ifdef HAVE_X_WINDOWS | |
7957 | #define HAVE_GHOSTSCRIPT 1 | |
7958 | #endif /* HAVE_X_WINDOWS */ | |
7959 | ||
7960 | /* The symbol `postscript' identifying images of this type. */ | |
7961 | ||
7962 | Lisp_Object Qpostscript; | |
7963 | ||
7964 | #ifdef HAVE_GHOSTSCRIPT | |
7965 | ||
7966 | static int gs_image_p P_ ((Lisp_Object object)); | |
7967 | static int gs_load P_ ((struct frame *f, struct image *img)); | |
7968 | static void gs_clear_image P_ ((struct frame *f, struct image *img)); | |
7969 | ||
7970 | /* Keyword symbols. */ | |
7971 | ||
7972 | Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height; | |
7973 | ||
7974 | /* Indices of image specification fields in gs_format, below. */ | |
7975 | ||
7976 | enum gs_keyword_index | |
7977 | { | |
7978 | GS_TYPE, | |
7979 | GS_PT_WIDTH, | |
7980 | GS_PT_HEIGHT, | |
7981 | GS_FILE, | |
7982 | GS_LOADER, | |
7983 | GS_BOUNDING_BOX, | |
7984 | GS_ASCENT, | |
7985 | GS_MARGIN, | |
7986 | GS_RELIEF, | |
7987 | GS_ALGORITHM, | |
7988 | GS_HEURISTIC_MASK, | |
7989 | GS_MASK, | |
7990 | GS_BACKGROUND, | |
7991 | GS_LAST | |
7992 | }; | |
7993 | ||
7994 | /* Vector of image_keyword structures describing the format | |
7995 | of valid user-defined image specifications. */ | |
7996 | ||
91433552 | 7997 | static const struct image_keyword gs_format[GS_LAST] = |
4ef0d4d0 KS |
7998 | { |
7999 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8000 | {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8001 | {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8002 | {":file", IMAGE_STRING_VALUE, 1}, | |
8003 | {":loader", IMAGE_FUNCTION_VALUE, 0}, | |
8004 | {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1}, | |
8005 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
8006 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
8007 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
8008 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8009 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8010 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8011 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8012 | }; | |
8013 | ||
8014 | /* Structure describing the image type `ghostscript'. */ | |
8015 | ||
8016 | static struct image_type gs_type = | |
8017 | { | |
8018 | &Qpostscript, | |
8019 | gs_image_p, | |
8020 | gs_load, | |
8021 | gs_clear_image, | |
8022 | NULL | |
8023 | }; | |
8024 | ||
8025 | ||
8026 | /* Free X resources of Ghostscript image IMG which is used on frame F. */ | |
8027 | ||
8028 | static void | |
8029 | gs_clear_image (f, img) | |
8030 | struct frame *f; | |
8031 | struct image *img; | |
8032 | { | |
8033 | /* IMG->data.ptr_val may contain a recorded colormap. */ | |
8034 | xfree (img->data.ptr_val); | |
8035 | x_clear_image (f, img); | |
8036 | } | |
8037 | ||
8038 | ||
8039 | /* Return non-zero if OBJECT is a valid Ghostscript image | |
8040 | specification. */ | |
8041 | ||
8042 | static int | |
8043 | gs_image_p (object) | |
8044 | Lisp_Object object; | |
8045 | { | |
8046 | struct image_keyword fmt[GS_LAST]; | |
8047 | Lisp_Object tem; | |
8048 | int i; | |
8049 | ||
8050 | bcopy (gs_format, fmt, sizeof fmt); | |
8051 | ||
8052 | if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)) | |
8053 | return 0; | |
8054 | ||
8055 | /* Bounding box must be a list or vector containing 4 integers. */ | |
8056 | tem = fmt[GS_BOUNDING_BOX].value; | |
8057 | if (CONSP (tem)) | |
8058 | { | |
8059 | for (i = 0; i < 4; ++i, tem = XCDR (tem)) | |
8060 | if (!CONSP (tem) || !INTEGERP (XCAR (tem))) | |
8061 | return 0; | |
8062 | if (!NILP (tem)) | |
8063 | return 0; | |
8064 | } | |
8065 | else if (VECTORP (tem)) | |
8066 | { | |
8067 | if (XVECTOR (tem)->size != 4) | |
8068 | return 0; | |
8069 | for (i = 0; i < 4; ++i) | |
8070 | if (!INTEGERP (XVECTOR (tem)->contents[i])) | |
8071 | return 0; | |
8072 | } | |
8073 | else | |
8074 | return 0; | |
8075 | ||
8076 | return 1; | |
8077 | } | |
8078 | ||
8079 | ||
8080 | /* Load Ghostscript image IMG for use on frame F. Value is non-zero | |
8081 | if successful. */ | |
8082 | ||
8083 | static int | |
8084 | gs_load (f, img) | |
8085 | struct frame *f; | |
8086 | struct image *img; | |
8087 | { | |
8088 | char buffer[100]; | |
8089 | Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width; | |
8090 | struct gcpro gcpro1, gcpro2; | |
8091 | Lisp_Object frame; | |
8092 | double in_width, in_height; | |
8093 | Lisp_Object pixel_colors = Qnil; | |
8094 | ||
8095 | /* Compute pixel size of pixmap needed from the given size in the | |
8096 | image specification. Sizes in the specification are in pt. 1 pt | |
8097 | = 1/72 in, xdpi and ydpi are stored in the frame's X display | |
8098 | info. */ | |
8099 | pt_width = image_spec_value (img->spec, QCpt_width, NULL); | |
8100 | in_width = XFASTINT (pt_width) / 72.0; | |
8101 | img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx; | |
8102 | pt_height = image_spec_value (img->spec, QCpt_height, NULL); | |
8103 | in_height = XFASTINT (pt_height) / 72.0; | |
8104 | img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy; | |
8105 | ||
f1f25b99 CY |
8106 | if (!check_image_size (f, img->width, img->height)) |
8107 | { | |
10ea2b82 | 8108 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
8109 | return 0; |
8110 | } | |
8111 | ||
4ef0d4d0 KS |
8112 | /* Create the pixmap. */ |
8113 | xassert (img->pixmap == NO_PIXMAP); | |
8114 | ||
8115 | /* Only W32 version did BLOCK_INPUT here. ++kfs */ | |
8116 | BLOCK_INPUT; | |
8117 | img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
8118 | img->width, img->height, | |
8119 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))); | |
8120 | UNBLOCK_INPUT; | |
8121 | ||
8122 | if (!img->pixmap) | |
8123 | { | |
8124 | image_error ("Unable to create pixmap for `%s'", img->spec, Qnil); | |
8125 | return 0; | |
8126 | } | |
8127 | ||
8128 | /* Call the loader to fill the pixmap. It returns a process object | |
8129 | if successful. We do not record_unwind_protect here because | |
8130 | other places in redisplay like calling window scroll functions | |
8131 | don't either. Let the Lisp loader use `unwind-protect' instead. */ | |
8132 | GCPRO2 (window_and_pixmap_id, pixel_colors); | |
8133 | ||
8134 | sprintf (buffer, "%lu %lu", | |
8135 | (unsigned long) FRAME_X_WINDOW (f), | |
8136 | (unsigned long) img->pixmap); | |
8137 | window_and_pixmap_id = build_string (buffer); | |
8138 | ||
8139 | sprintf (buffer, "%lu %lu", | |
8140 | FRAME_FOREGROUND_PIXEL (f), | |
8141 | FRAME_BACKGROUND_PIXEL (f)); | |
8142 | pixel_colors = build_string (buffer); | |
8143 | ||
8144 | XSETFRAME (frame, f); | |
8145 | loader = image_spec_value (img->spec, QCloader, NULL); | |
8146 | if (NILP (loader)) | |
8147 | loader = intern ("gs-load-image"); | |
8148 | ||
8149 | img->data.lisp_val = call6 (loader, frame, img->spec, | |
8150 | make_number (img->width), | |
8151 | make_number (img->height), | |
8152 | window_and_pixmap_id, | |
8153 | pixel_colors); | |
8154 | UNGCPRO; | |
8155 | return PROCESSP (img->data.lisp_val); | |
8156 | } | |
8157 | ||
8158 | ||
8159 | /* Kill the Ghostscript process that was started to fill PIXMAP on | |
8160 | frame F. Called from XTread_socket when receiving an event | |
8161 | telling Emacs that Ghostscript has finished drawing. */ | |
8162 | ||
8163 | void | |
8164 | x_kill_gs_process (pixmap, f) | |
8165 | Pixmap pixmap; | |
8166 | struct frame *f; | |
8167 | { | |
354884c4 | 8168 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
8169 | int class, i; |
8170 | struct image *img; | |
8171 | ||
8172 | /* Find the image containing PIXMAP. */ | |
8173 | for (i = 0; i < c->used; ++i) | |
8174 | if (c->images[i]->pixmap == pixmap) | |
8175 | break; | |
8176 | ||
8177 | /* Should someone in between have cleared the image cache, for | |
8178 | instance, give up. */ | |
8179 | if (i == c->used) | |
8180 | return; | |
8181 | ||
8182 | /* Kill the GS process. We should have found PIXMAP in the image | |
8183 | cache and its image should contain a process object. */ | |
8184 | img = c->images[i]; | |
8185 | xassert (PROCESSP (img->data.lisp_val)); | |
8186 | Fkill_process (img->data.lisp_val, Qnil); | |
8187 | img->data.lisp_val = Qnil; | |
8188 | ||
8189 | #if defined (HAVE_X_WINDOWS) | |
8190 | ||
8191 | /* On displays with a mutable colormap, figure out the colors | |
8192 | allocated for the image by looking at the pixels of an XImage for | |
8193 | img->pixmap. */ | |
8194 | class = FRAME_X_VISUAL (f)->class; | |
8195 | if (class != StaticColor && class != StaticGray && class != TrueColor) | |
8196 | { | |
8197 | XImagePtr ximg; | |
8198 | ||
8199 | BLOCK_INPUT; | |
8200 | ||
8201 | /* Try to get an XImage for img->pixmep. */ | |
8202 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, | |
8203 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
8204 | if (ximg) | |
8205 | { | |
8206 | int x, y; | |
8207 | ||
8208 | /* Initialize the color table. */ | |
8209 | init_color_table (); | |
8210 | ||
8211 | /* For each pixel of the image, look its color up in the | |
8212 | color table. After having done so, the color table will | |
8213 | contain an entry for each color used by the image. */ | |
8214 | for (y = 0; y < img->height; ++y) | |
8215 | for (x = 0; x < img->width; ++x) | |
8216 | { | |
8217 | unsigned long pixel = XGetPixel (ximg, x, y); | |
8218 | lookup_pixel_color (f, pixel); | |
8219 | } | |
8220 | ||
8221 | /* Record colors in the image. Free color table and XImage. */ | |
8222 | #ifdef COLOR_TABLE_SUPPORT | |
8223 | img->colors = colors_in_color_table (&img->ncolors); | |
8224 | free_color_table (); | |
8225 | #endif | |
8226 | XDestroyImage (ximg); | |
8227 | ||
8228 | #if 0 /* This doesn't seem to be the case. If we free the colors | |
8229 | here, we get a BadAccess later in x_clear_image when | |
8230 | freeing the colors. */ | |
8231 | /* We have allocated colors once, but Ghostscript has also | |
8232 | allocated colors on behalf of us. So, to get the | |
8233 | reference counts right, free them once. */ | |
8234 | if (img->ncolors) | |
8235 | x_free_colors (f, img->colors, img->ncolors); | |
8236 | #endif | |
8237 | } | |
8238 | else | |
8239 | image_error ("Cannot get X image of `%s'; colors will not be freed", | |
8240 | img->spec, Qnil); | |
8241 | ||
8242 | UNBLOCK_INPUT; | |
8243 | } | |
8244 | #endif /* HAVE_X_WINDOWS */ | |
8245 | ||
8246 | /* Now that we have the pixmap, compute mask and transform the | |
8247 | image if requested. */ | |
8248 | BLOCK_INPUT; | |
8249 | postprocess_image (f, img); | |
8250 | UNBLOCK_INPUT; | |
8251 | } | |
8252 | ||
8253 | #endif /* HAVE_GHOSTSCRIPT */ | |
8254 | ||
8255 | \f | |
8256 | /*********************************************************************** | |
8257 | Tests | |
8258 | ***********************************************************************/ | |
8259 | ||
8260 | #if GLYPH_DEBUG | |
8261 | ||
8262 | DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0, | |
8263 | doc: /* Value is non-nil if SPEC is a valid image specification. */) | |
8264 | (spec) | |
8265 | Lisp_Object spec; | |
8266 | { | |
8267 | return valid_image_p (spec) ? Qt : Qnil; | |
8268 | } | |
8269 | ||
8270 | ||
8271 | DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "") | |
8272 | (spec) | |
8273 | Lisp_Object spec; | |
8274 | { | |
8275 | int id = -1; | |
8276 | ||
8277 | if (valid_image_p (spec)) | |
8278 | id = lookup_image (SELECTED_FRAME (), spec); | |
8279 | ||
8280 | debug_print (spec); | |
8281 | return make_number (id); | |
8282 | } | |
8283 | ||
8284 | #endif /* GLYPH_DEBUG != 0 */ | |
8285 | ||
8286 | ||
8287 | /*********************************************************************** | |
8288 | Initialization | |
8289 | ***********************************************************************/ | |
8290 | ||
0855eb52 JB |
8291 | #ifdef HAVE_NTGUI |
8292 | /* Image types that rely on external libraries are loaded dynamically | |
8293 | if the library is available. */ | |
adac86db | 8294 | #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \ |
0855eb52 JB |
8295 | define_image_type (image_type, init_lib_fn (libraries)) |
8296 | #else | |
adac86db | 8297 | #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \ |
e0e30823 | 8298 | define_image_type (image_type, 1) |
0855eb52 JB |
8299 | #endif /* HAVE_NTGUI */ |
8300 | ||
8301 | DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 2, 2, 0, | |
8302 | doc: /* Initialize image library implementing image type TYPE. | |
8303 | Return non-nil if TYPE is a supported image type. | |
8304 | ||
8305 | Image types pbm and xbm are prebuilt; other types are loaded here. | |
8306 | Libraries to load are specified in alist LIBRARIES (usually, the value | |
adac86db | 8307 | of `image-library-alist', which see). */) |
0855eb52 | 8308 | (type, libraries) |
adac86db | 8309 | Lisp_Object type, libraries; |
0855eb52 JB |
8310 | { |
8311 | Lisp_Object tested; | |
8312 | ||
8313 | /* Don't try to reload the library. */ | |
8314 | tested = Fassq (type, Vimage_type_cache); | |
8315 | if (CONSP (tested)) | |
8316 | return XCDR (tested); | |
8317 | ||
9e2a2647 | 8318 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
0855eb52 | 8319 | if (EQ (type, Qxpm)) |
adac86db | 8320 | return CHECK_LIB_AVAILABLE (&xpm_type, init_xpm_functions, libraries); |
0855eb52 JB |
8321 | #endif |
8322 | ||
9e2a2647 | 8323 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
0855eb52 | 8324 | if (EQ (type, Qjpeg)) |
adac86db | 8325 | return CHECK_LIB_AVAILABLE (&jpeg_type, init_jpeg_functions, libraries); |
0855eb52 JB |
8326 | #endif |
8327 | ||
9e2a2647 | 8328 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
0855eb52 | 8329 | if (EQ (type, Qtiff)) |
adac86db | 8330 | return CHECK_LIB_AVAILABLE (&tiff_type, init_tiff_functions, libraries); |
0855eb52 JB |
8331 | #endif |
8332 | ||
9e2a2647 | 8333 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
0855eb52 | 8334 | if (EQ (type, Qgif)) |
adac86db | 8335 | return CHECK_LIB_AVAILABLE (&gif_type, init_gif_functions, libraries); |
0855eb52 JB |
8336 | #endif |
8337 | ||
9e2a2647 | 8338 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
0855eb52 | 8339 | if (EQ (type, Qpng)) |
adac86db | 8340 | return CHECK_LIB_AVAILABLE (&png_type, init_png_functions, libraries); |
0855eb52 JB |
8341 | #endif |
8342 | ||
4ab27a43 GM |
8343 | #if defined (HAVE_RSVG) |
8344 | if (EQ (type, Qsvg)) | |
8345 | return CHECK_LIB_AVAILABLE (&svg_type, init_svg_functions, libraries); | |
8346 | #endif | |
c21e6151 | 8347 | |
0855eb52 JB |
8348 | #ifdef HAVE_GHOSTSCRIPT |
8349 | if (EQ (type, Qpostscript)) | |
adac86db | 8350 | return CHECK_LIB_AVAILABLE (&gs_type, init_gs_functions, libraries); |
0855eb52 JB |
8351 | #endif |
8352 | ||
8353 | /* If the type is not recognized, avoid testing it ever again. */ | |
adac86db | 8354 | CACHE_IMAGE_TYPE (type, Qnil); |
0855eb52 JB |
8355 | return Qnil; |
8356 | } | |
8357 | ||
4ef0d4d0 KS |
8358 | void |
8359 | syms_of_image () | |
8360 | { | |
2a611c1a JB |
8361 | extern Lisp_Object Qrisky_local_variable; /* Syms_of_xdisp has already run. */ |
8362 | ||
6e6fc3fd RS |
8363 | /* Initialize this only once, since that's what we do with Vimage_types |
8364 | and they are supposed to be in sync. Initializing here gives correct | |
8365 | operation on GNU/Linux of calling dump-emacs after loading some images. */ | |
8366 | image_types = NULL; | |
8367 | ||
0855eb52 JB |
8368 | /* Must be defined now becase we're going to update it below, while |
8369 | defining the supported image types. */ | |
8370 | DEFVAR_LISP ("image-types", &Vimage_types, | |
8371 | doc: /* List of potentially supported image types. | |
bbe6f2aa | 8372 | Each element of the list is a symbol for an image type, like 'jpeg or 'png. |
0855eb52 JB |
8373 | To check whether it is really supported, use `image-type-available-p'. */); |
8374 | Vimage_types = Qnil; | |
8375 | ||
2a611c1a JB |
8376 | DEFVAR_LISP ("image-library-alist", &Vimage_library_alist, |
8377 | doc: /* Alist of image types vs external libraries needed to display them. | |
8378 | ||
8379 | Each element is a list (IMAGE-TYPE LIBRARY...), where the car is a symbol | |
8380 | representing a supported image type, and the rest are strings giving | |
8381 | alternate filenames for the corresponding external libraries. | |
8382 | ||
8383 | Emacs tries to load the libraries in the order they appear on the | |
8384 | list; if none is loaded, the running session of Emacs won't | |
8385 | support the image type. Types 'pbm and 'xbm don't need to be | |
3046a84b | 8386 | listed; they are always supported. */); |
2a611c1a | 8387 | Vimage_library_alist = Qnil; |
d67b4f80 | 8388 | Fput (intern_c_string ("image-library-alist"), Qrisky_local_variable, Qt); |
2a611c1a | 8389 | |
f1f25b99 | 8390 | DEFVAR_LISP ("max-image-size", &Vmax_image_size, |
7df4765a KS |
8391 | doc: /* Maximum size of images. |
8392 | Emacs will not load an image into memory if its pixel width or | |
8393 | pixel height exceeds this limit. | |
8394 | ||
8395 | If the value is an integer, it directly specifies the maximum | |
8396 | image height and width, measured in pixels. If it is a floating | |
8397 | point number, it specifies the maximum image height and width | |
8398 | as a ratio to the frame height and width. If the value is | |
8399 | non-numeric, there is no explicit limit on the size of images. */); | |
f1f25b99 CY |
8400 | Vmax_image_size = make_float (MAX_IMAGE_SIZE); |
8401 | ||
0855eb52 JB |
8402 | Vimage_type_cache = Qnil; |
8403 | staticpro (&Vimage_type_cache); | |
8404 | ||
d67b4f80 | 8405 | Qpbm = intern_c_string ("pbm"); |
7c4e3a32 | 8406 | staticpro (&Qpbm); |
5fc9fdeb | 8407 | ADD_IMAGE_TYPE (Qpbm); |
7c4e3a32 | 8408 | |
d67b4f80 | 8409 | Qxbm = intern_c_string ("xbm"); |
7c4e3a32 | 8410 | staticpro (&Qxbm); |
5fc9fdeb | 8411 | ADD_IMAGE_TYPE (Qxbm); |
7c4e3a32 RS |
8412 | |
8413 | define_image_type (&xbm_type, 1); | |
8414 | define_image_type (&pbm_type, 1); | |
8415 | ||
d67b4f80 | 8416 | Qcount = intern_c_string ("count"); |
e047f448 SM |
8417 | staticpro (&Qcount); |
8418 | ||
d67b4f80 | 8419 | QCascent = intern_c_string (":ascent"); |
4ef0d4d0 | 8420 | staticpro (&QCascent); |
d67b4f80 | 8421 | QCmargin = intern_c_string (":margin"); |
4ef0d4d0 | 8422 | staticpro (&QCmargin); |
d67b4f80 | 8423 | QCrelief = intern_c_string (":relief"); |
4ef0d4d0 | 8424 | staticpro (&QCrelief); |
d67b4f80 | 8425 | QCconversion = intern_c_string (":conversion"); |
4ef0d4d0 | 8426 | staticpro (&QCconversion); |
d67b4f80 | 8427 | QCcolor_symbols = intern_c_string (":color-symbols"); |
4ef0d4d0 | 8428 | staticpro (&QCcolor_symbols); |
d67b4f80 | 8429 | QCheuristic_mask = intern_c_string (":heuristic-mask"); |
4ef0d4d0 | 8430 | staticpro (&QCheuristic_mask); |
d67b4f80 | 8431 | QCindex = intern_c_string (":index"); |
4ef0d4d0 | 8432 | staticpro (&QCindex); |
d67b4f80 | 8433 | QCmatrix = intern_c_string (":matrix"); |
4ef0d4d0 | 8434 | staticpro (&QCmatrix); |
d67b4f80 | 8435 | QCcolor_adjustment = intern_c_string (":color-adjustment"); |
4ef0d4d0 | 8436 | staticpro (&QCcolor_adjustment); |
d67b4f80 | 8437 | QCmask = intern_c_string (":mask"); |
4ef0d4d0 KS |
8438 | staticpro (&QCmask); |
8439 | ||
d67b4f80 | 8440 | Qlaplace = intern_c_string ("laplace"); |
4ef0d4d0 | 8441 | staticpro (&Qlaplace); |
d67b4f80 | 8442 | Qemboss = intern_c_string ("emboss"); |
4ef0d4d0 | 8443 | staticpro (&Qemboss); |
d67b4f80 | 8444 | Qedge_detection = intern_c_string ("edge-detection"); |
4ef0d4d0 | 8445 | staticpro (&Qedge_detection); |
d67b4f80 | 8446 | Qheuristic = intern_c_string ("heuristic"); |
4ef0d4d0 | 8447 | staticpro (&Qheuristic); |
4ef0d4d0 | 8448 | |
d67b4f80 | 8449 | Qpostscript = intern_c_string ("postscript"); |
4ef0d4d0 KS |
8450 | staticpro (&Qpostscript); |
8451 | #ifdef HAVE_GHOSTSCRIPT | |
5fc9fdeb | 8452 | ADD_IMAGE_TYPE (Qpostscript); |
d67b4f80 | 8453 | QCloader = intern_c_string (":loader"); |
4ef0d4d0 | 8454 | staticpro (&QCloader); |
d67b4f80 | 8455 | QCbounding_box = intern_c_string (":bounding-box"); |
4ef0d4d0 | 8456 | staticpro (&QCbounding_box); |
d67b4f80 | 8457 | QCpt_width = intern_c_string (":pt-width"); |
4ef0d4d0 | 8458 | staticpro (&QCpt_width); |
d67b4f80 | 8459 | QCpt_height = intern_c_string (":pt-height"); |
4ef0d4d0 KS |
8460 | staticpro (&QCpt_height); |
8461 | #endif /* HAVE_GHOSTSCRIPT */ | |
8462 | ||
5be1c984 EZ |
8463 | #ifdef HAVE_NTGUI |
8464 | Qlibpng_version = intern_c_string ("libpng-version"); | |
8465 | staticpro (&Qlibpng_version); | |
8466 | #if HAVE_PNG | |
e7f50e8f | 8467 | XSYMBOL (Qlibpng_version)->value = make_number (PNG_LIBPNG_VER); |
5be1c984 | 8468 | #else |
e7f50e8f | 8469 | XSYMBOL (Qlibpng_version)->value = make_number (-1)); |
5be1c984 EZ |
8470 | #endif |
8471 | #endif | |
8472 | ||
9e2a2647 | 8473 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
d67b4f80 | 8474 | Qxpm = intern_c_string ("xpm"); |
4ef0d4d0 | 8475 | staticpro (&Qxpm); |
5fc9fdeb | 8476 | ADD_IMAGE_TYPE (Qxpm); |
4ef0d4d0 KS |
8477 | #endif |
8478 | ||
9e2a2647 | 8479 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
d67b4f80 | 8480 | Qjpeg = intern_c_string ("jpeg"); |
4ef0d4d0 | 8481 | staticpro (&Qjpeg); |
5fc9fdeb | 8482 | ADD_IMAGE_TYPE (Qjpeg); |
4ef0d4d0 KS |
8483 | #endif |
8484 | ||
9e2a2647 | 8485 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
d67b4f80 | 8486 | Qtiff = intern_c_string ("tiff"); |
4ef0d4d0 | 8487 | staticpro (&Qtiff); |
5fc9fdeb | 8488 | ADD_IMAGE_TYPE (Qtiff); |
4ef0d4d0 KS |
8489 | #endif |
8490 | ||
9e2a2647 | 8491 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
d67b4f80 | 8492 | Qgif = intern_c_string ("gif"); |
4ef0d4d0 | 8493 | staticpro (&Qgif); |
5fc9fdeb | 8494 | ADD_IMAGE_TYPE (Qgif); |
4ef0d4d0 KS |
8495 | #endif |
8496 | ||
9e2a2647 | 8497 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
d67b4f80 | 8498 | Qpng = intern_c_string ("png"); |
4ef0d4d0 | 8499 | staticpro (&Qpng); |
5fc9fdeb | 8500 | ADD_IMAGE_TYPE (Qpng); |
4ef0d4d0 KS |
8501 | #endif |
8502 | ||
4ab27a43 | 8503 | #if defined (HAVE_RSVG) |
d67b4f80 | 8504 | Qsvg = intern_c_string ("svg"); |
4ab27a43 | 8505 | staticpro (&Qsvg); |
5fc9fdeb JR |
8506 | ADD_IMAGE_TYPE (Qsvg); |
8507 | #ifdef HAVE_NTGUI | |
df23bf08 | 8508 | /* Other libraries used directly by svg code. */ |
d67b4f80 | 8509 | Qgdk_pixbuf = intern_c_string ("gdk-pixbuf"); |
5fc9fdeb | 8510 | staticpro (&Qgdk_pixbuf); |
d67b4f80 | 8511 | Qglib = intern_c_string ("glib"); |
5fc9fdeb | 8512 | staticpro (&Qglib); |
d67b4f80 | 8513 | Qgobject = intern_c_string ("gobject"); |
df23bf08 | 8514 | staticpro (&Qgobject); |
5fc9fdeb JR |
8515 | #endif /* HAVE_NTGUI */ |
8516 | #endif /* HAVE_RSVG */ | |
4ab27a43 | 8517 | |
0855eb52 | 8518 | defsubr (&Sinit_image_library); |
4ef0d4d0 | 8519 | defsubr (&Sclear_image_cache); |
483e7ae0 | 8520 | defsubr (&Simage_refresh); |
4ef0d4d0 KS |
8521 | defsubr (&Simage_size); |
8522 | defsubr (&Simage_mask_p); | |
6ac3c7bb | 8523 | defsubr (&Simage_extension_data); |
4ef0d4d0 KS |
8524 | |
8525 | #if GLYPH_DEBUG | |
8526 | defsubr (&Simagep); | |
8527 | defsubr (&Slookup_image); | |
8528 | #endif | |
8529 | ||
8530 | DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images, | |
8531 | doc: /* Non-nil means always draw a cross over disabled images. | |
3046a84b | 8532 | Disabled images are those having a `:conversion disabled' property. |
4ef0d4d0 KS |
8533 | A cross is always drawn on black & white displays. */); |
8534 | cross_disabled_images = 0; | |
8535 | ||
8536 | DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path, | |
8537 | doc: /* List of directories to search for window system bitmap files. */); | |
8538 | Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS); | |
8539 | ||
8540 | DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay, | |
8541 | doc: /* Time after which cached images are removed from the cache. | |
8542 | When an image has not been displayed this many seconds, remove it | |
8543 | from the image cache. Value must be an integer or nil with nil | |
8544 | meaning don't clear the cache. */); | |
8545 | Vimage_cache_eviction_delay = make_number (30 * 60); | |
8546 | } | |
8547 | ||
4ef0d4d0 KS |
8548 | void |
8549 | init_image () | |
8550 | { | |
4ef0d4d0 KS |
8551 | } |
8552 | ||
1b9f8cec MB |
8553 | /* arch-tag: 123c2a5e-14a8-4c53-ab95-af47d7db49b9 |
8554 | (do not change this comment) */ |