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