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, |
114f9c96 | 3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
6678dc11 | 4 | Free Software Foundation, Inc. |
4ef0d4d0 KS |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
4ef0d4d0 | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
4ef0d4d0 KS |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ef0d4d0 KS |
20 | |
21 | #include <config.h> | |
4ef0d4d0 KS |
22 | #include <stdio.h> |
23 | #include <math.h> | |
be8c4ce4 | 24 | #include <ctype.h> |
4ef0d4d0 KS |
25 | |
26 | #ifdef HAVE_UNISTD_H | |
27 | #include <unistd.h> | |
28 | #endif | |
29 | ||
d7306fe6 DN |
30 | #ifdef HAVE_PNG |
31 | #if defined HAVE_LIBPNG_PNG_H | |
32 | # include <libpng/png.h> | |
33 | #else | |
34 | # include <png.h> | |
35 | #endif | |
285d07e2 | 36 | #endif |
d7306fe6 | 37 | |
c3417a74 DN |
38 | #include <setjmp.h> |
39 | ||
4ef0d4d0 KS |
40 | /* This makes the fields of a Display accessible, in Xlib header files. */ |
41 | ||
42 | #define XLIB_ILLEGAL_ACCESS | |
43 | ||
44 | #include "lisp.h" | |
45 | #include "frame.h" | |
46 | #include "window.h" | |
47 | #include "dispextern.h" | |
48 | #include "blockinput.h" | |
49 | #include "systime.h" | |
50 | #include <epaths.h> | |
3dcc8b84 | 51 | #include "character.h" |
022af124 | 52 | #include "coding.h" |
6087292e | 53 | #include "termhooks.h" |
3dcc8b84 | 54 | #include "font.h" |
4ef0d4d0 KS |
55 | |
56 | #ifdef HAVE_X_WINDOWS | |
57 | #include "xterm.h" | |
58 | #include <sys/types.h> | |
59 | #include <sys/stat.h> | |
60 | ||
61 | #define COLOR_TABLE_SUPPORT 1 | |
62 | ||
63 | typedef struct x_bitmap_record Bitmap_Record; | |
4ef0d4d0 KS |
64 | #define GET_PIXEL(ximg, x, y) XGetPixel(ximg, x, y) |
65 | #define NO_PIXMAP None | |
4ef0d4d0 KS |
66 | |
67 | #define RGB_PIXEL_COLOR unsigned long | |
68 | ||
9ecf6403 YM |
69 | #define PIX_MASK_RETAIN 0 |
70 | #define PIX_MASK_DRAW 1 | |
4ef0d4d0 KS |
71 | #endif /* HAVE_X_WINDOWS */ |
72 | ||
73 | ||
74 | #ifdef HAVE_NTGUI | |
75 | #include "w32term.h" | |
76 | ||
77 | /* W32_TODO : Color tables on W32. */ | |
78 | #undef COLOR_TABLE_SUPPORT | |
79 | ||
80 | typedef struct w32_bitmap_record Bitmap_Record; | |
4ef0d4d0 KS |
81 | #define GET_PIXEL(ximg, x, y) GetPixel(ximg, x, y) |
82 | #define NO_PIXMAP 0 | |
4ef0d4d0 KS |
83 | |
84 | #define RGB_PIXEL_COLOR COLORREF | |
85 | ||
9ecf6403 YM |
86 | #define PIX_MASK_RETAIN 0 |
87 | #define PIX_MASK_DRAW 1 | |
4ef0d4d0 KS |
88 | |
89 | #define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual | |
90 | #define x_defined_color w32_defined_color | |
91 | #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits) | |
8b7d0a16 JR |
92 | |
93 | /* Functions from w32term.c that depend on XColor (so can't go in w32term.h | |
94 | without modifying lots of files). */ | |
95 | extern void x_query_colors (struct frame *f, XColor *colors, int ncolors); | |
96 | extern void x_query_color (struct frame *f, XColor *color); | |
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; |
1546c559 | 607 | Lisp_Object QCascent, QCmargin, QCrelief, Qcount, Qextension_data; |
4ef0d4d0 | 608 | Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask; |
3de25479 | 609 | Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask, QCgeometry, QCcrop, QCrotation; |
4ef0d4d0 KS |
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 | ||
1546c559 JL |
1014 | DEFUN ("image-metadata", Fimage_metadata, Simage_metadata, 1, 2, 0, |
1015 | doc: /* Return metadata for image SPEC. | |
6ac3c7bb KS |
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 | |
98fe5161 | 1585 | if (c) |
4ef0d4d0 | 1586 | { |
98fe5161 | 1587 | int i, nfreed = 0; |
4ef0d4d0 KS |
1588 | |
1589 | /* Block input so that we won't be interrupted by a SIGIO | |
1590 | while being in an inconsistent state. */ | |
1591 | BLOCK_INPUT; | |
1592 | ||
98fe5161 | 1593 | if (!NILP (filter)) |
4ef0d4d0 | 1594 | { |
98fe5161 CY |
1595 | /* Filter image cache. */ |
1596 | for (i = 0; i < c->used; ++i) | |
4ef0d4d0 | 1597 | { |
98fe5161 CY |
1598 | struct image *img = c->images[i]; |
1599 | if (img && (EQ (Qt, filter) | |
1600 | || !NILP (Fmember (filter, img->dependencies)))) | |
1601 | { | |
1602 | free_image (f, img); | |
1603 | ++nfreed; | |
1604 | } | |
1605 | } | |
1606 | } | |
1607 | else if (INTEGERP (Vimage_cache_eviction_delay)) | |
4ef0d4d0 | 1608 | { |
98fe5161 CY |
1609 | /* Free cache based on timestamp. */ |
1610 | EMACS_TIME t; | |
1611 | unsigned long old; | |
1612 | int delay, nimages = 0; | |
1613 | ||
1614 | for (i = 0; i < c->used; ++i) | |
1615 | if (c->images[i]) | |
1616 | nimages++; | |
1617 | ||
1618 | /* If the number of cached images has grown unusually large, | |
1619 | decrease the cache eviction delay (Bug#6230). */ | |
1620 | delay = XFASTINT (Vimage_cache_eviction_delay); | |
1621 | if (nimages > 40) | |
1622 | delay = max (1, 1600 * delay / (nimages*nimages)); | |
1623 | ||
1624 | EMACS_GET_TIME (t); | |
1625 | old = EMACS_SECS (t) - delay; | |
1626 | ||
1627 | for (i = 0; i < c->used; ++i) | |
4ef0d4d0 | 1628 | { |
98fe5161 CY |
1629 | struct image *img = c->images[i]; |
1630 | if (img && img->timestamp < old) | |
1631 | { | |
1632 | free_image (f, img); | |
1633 | ++nfreed; | |
1634 | } | |
4ef0d4d0 KS |
1635 | } |
1636 | } | |
1637 | ||
1638 | /* We may be clearing the image cache because, for example, | |
1639 | Emacs was iconified for a longer period of time. In that | |
1640 | case, current matrices may still contain references to | |
1641 | images freed above. So, clear these matrices. */ | |
1642 | if (nfreed) | |
1643 | { | |
1644 | Lisp_Object tail, frame; | |
1645 | ||
1646 | FOR_EACH_FRAME (tail, frame) | |
1647 | { | |
1648 | struct frame *f = XFRAME (frame); | |
354884c4 | 1649 | if (FRAME_IMAGE_CACHE (f) == c) |
4ef0d4d0 KS |
1650 | clear_current_matrices (f); |
1651 | } | |
1652 | ||
1653 | ++windows_or_buffers_changed; | |
1654 | } | |
1655 | ||
1656 | UNBLOCK_INPUT; | |
1657 | } | |
1658 | } | |
1659 | ||
354884c4 | 1660 | void |
a2bc5bdd | 1661 | clear_image_caches (Lisp_Object filter) |
354884c4 SM |
1662 | { |
1663 | /* FIXME: We want to do | |
1664 | * struct terminal *t; | |
1665 | * for (t = terminal_list; t; t = t->next_terminal) | |
1666 | * clear_image_cache (t, filter); */ | |
1667 | Lisp_Object tail, frame; | |
1668 | FOR_EACH_FRAME (tail, frame) | |
1669 | if (FRAME_WINDOW_P (XFRAME (frame))) | |
a2bc5bdd | 1670 | clear_image_cache (XFRAME (frame), filter); |
354884c4 | 1671 | } |
4ef0d4d0 KS |
1672 | |
1673 | DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache, | |
1674 | 0, 1, 0, | |
a2bc5bdd SM |
1675 | doc: /* Clear the image cache. |
1676 | FILTER nil or a frame means clear all images in the selected frame. | |
1677 | FILTER t means clear the image caches of all frames. | |
1678 | Anything else, means only clear those images which refer to FILTER, | |
1679 | which is then usually a filename. */) | |
1680 | (filter) | |
1681 | Lisp_Object filter; | |
4ef0d4d0 | 1682 | { |
a2bc5bdd SM |
1683 | if (!(EQ (filter, Qnil) || FRAMEP (filter))) |
1684 | clear_image_caches (filter); | |
4ef0d4d0 | 1685 | else |
a2bc5bdd | 1686 | clear_image_cache (check_x_frame (filter), Qt); |
4ef0d4d0 KS |
1687 | |
1688 | return Qnil; | |
1689 | } | |
1690 | ||
1691 | ||
110683ad | 1692 | DEFUN ("image-flush", Fimage_flush, Simage_flush, |
483e7ae0 | 1693 | 1, 2, 0, |
110683ad CY |
1694 | doc: /* Fush the image with specification SPEC on frame FRAME. |
1695 | This removes the image from the Emacs image cache. If SPEC specifies | |
1696 | an image file, the next redisplay of this image will read from the | |
1697 | current contents of that file. | |
1698 | ||
483e7ae0 CY |
1699 | FRAME nil or omitted means use the selected frame. |
1700 | FRAME t means refresh the image on all frames. */) | |
1701 | (spec, frame) | |
1702 | Lisp_Object spec, frame; | |
1703 | { | |
1704 | if (!valid_image_p (spec)) | |
1705 | error ("Invalid image specification"); | |
1706 | ||
1707 | if (EQ (frame, Qt)) | |
1708 | { | |
1709 | Lisp_Object tail; | |
1710 | FOR_EACH_FRAME (tail, frame) | |
1711 | { | |
1712 | struct frame *f = XFRAME (frame); | |
1713 | if (FRAME_WINDOW_P (f)) | |
1714 | uncache_image (f, spec); | |
1715 | } | |
1716 | } | |
1717 | else | |
1718 | uncache_image (check_x_frame (frame), spec); | |
1719 | ||
1720 | return Qnil; | |
1721 | } | |
1722 | ||
1723 | ||
4ef0d4d0 KS |
1724 | /* Compute masks and transform image IMG on frame F, as specified |
1725 | by the image's specification, */ | |
1726 | ||
1727 | static void | |
1728 | postprocess_image (f, img) | |
1729 | struct frame *f; | |
1730 | struct image *img; | |
1731 | { | |
1732 | /* Manipulation of the image's mask. */ | |
1733 | if (img->pixmap) | |
1734 | { | |
1735 | Lisp_Object conversion, spec; | |
1736 | Lisp_Object mask; | |
1737 | ||
1738 | spec = img->spec; | |
1739 | ||
1740 | /* `:heuristic-mask t' | |
1741 | `:mask heuristic' | |
1742 | means build a mask heuristically. | |
1743 | `:heuristic-mask (R G B)' | |
1744 | `:mask (heuristic (R G B))' | |
1745 | means build a mask from color (R G B) in the | |
1746 | image. | |
1747 | `:mask nil' | |
1748 | means remove a mask, if any. */ | |
1749 | ||
1750 | mask = image_spec_value (spec, QCheuristic_mask, NULL); | |
1751 | if (!NILP (mask)) | |
1752 | x_build_heuristic_mask (f, img, mask); | |
1753 | else | |
1754 | { | |
1755 | int found_p; | |
1756 | ||
1757 | mask = image_spec_value (spec, QCmask, &found_p); | |
1758 | ||
1759 | if (EQ (mask, Qheuristic)) | |
1760 | x_build_heuristic_mask (f, img, Qt); | |
1761 | else if (CONSP (mask) | |
1762 | && EQ (XCAR (mask), Qheuristic)) | |
1763 | { | |
1764 | if (CONSP (XCDR (mask))) | |
1765 | x_build_heuristic_mask (f, img, XCAR (XCDR (mask))); | |
1766 | else | |
1767 | x_build_heuristic_mask (f, img, XCDR (mask)); | |
1768 | } | |
1769 | else if (NILP (mask) && found_p && img->mask) | |
1770 | { | |
1771 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
1772 | img->mask = NO_PIXMAP; | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | ||
1777 | /* Should we apply an image transformation algorithm? */ | |
1778 | conversion = image_spec_value (spec, QCconversion, NULL); | |
1779 | if (EQ (conversion, Qdisabled)) | |
1780 | x_disable_image (f, img); | |
1781 | else if (EQ (conversion, Qlaplace)) | |
1782 | x_laplace (f, img); | |
1783 | else if (EQ (conversion, Qemboss)) | |
1784 | x_emboss (f, img); | |
1785 | else if (CONSP (conversion) | |
1786 | && EQ (XCAR (conversion), Qedge_detection)) | |
1787 | { | |
1788 | Lisp_Object tem; | |
1789 | tem = XCDR (conversion); | |
1790 | if (CONSP (tem)) | |
1791 | x_edge_detection (f, img, | |
1792 | Fplist_get (tem, QCmatrix), | |
1793 | Fplist_get (tem, QCcolor_adjustment)); | |
1794 | } | |
1795 | } | |
1796 | } | |
1797 | ||
1798 | ||
1799 | /* Return the id of image with Lisp specification SPEC on frame F. | |
1800 | SPEC must be a valid Lisp image specification (see valid_image_p). */ | |
1801 | ||
1802 | int | |
1803 | lookup_image (f, spec) | |
1804 | struct frame *f; | |
1805 | Lisp_Object spec; | |
1806 | { | |
4a0e3f08 | 1807 | struct image_cache *c; |
4ef0d4d0 | 1808 | struct image *img; |
4ef0d4d0 KS |
1809 | unsigned hash; |
1810 | struct gcpro gcpro1; | |
1811 | EMACS_TIME now; | |
1812 | ||
1813 | /* F must be a window-system frame, and SPEC must be a valid image | |
1814 | specification. */ | |
1815 | xassert (FRAME_WINDOW_P (f)); | |
1816 | xassert (valid_image_p (spec)); | |
1817 | ||
354884c4 | 1818 | c = FRAME_IMAGE_CACHE (f); |
4a0e3f08 | 1819 | |
4ef0d4d0 KS |
1820 | GCPRO1 (spec); |
1821 | ||
1822 | /* Look up SPEC in the hash table of the image cache. */ | |
1823 | hash = sxhash (spec, 0); | |
d3c35d17 | 1824 | img = search_image_cache (f, spec, hash); |
f1f25b99 CY |
1825 | if (img && img->load_failed_p) |
1826 | { | |
1827 | free_image (f, img); | |
1828 | img = NULL; | |
1829 | } | |
1830 | ||
4ef0d4d0 KS |
1831 | /* If not found, create a new image and cache it. */ |
1832 | if (img == NULL) | |
1833 | { | |
1834 | extern Lisp_Object Qpostscript; | |
1835 | ||
1836 | BLOCK_INPUT; | |
1837 | img = make_image (spec, hash); | |
1838 | cache_image (f, img); | |
1839 | img->load_failed_p = img->type->load (f, img) == 0; | |
c5b8e0ea YM |
1840 | img->frame_foreground = FRAME_FOREGROUND_PIXEL (f); |
1841 | img->frame_background = FRAME_BACKGROUND_PIXEL (f); | |
4ef0d4d0 KS |
1842 | |
1843 | /* If we can't load the image, and we don't have a width and | |
1844 | height, use some arbitrary width and height so that we can | |
1845 | draw a rectangle for it. */ | |
1846 | if (img->load_failed_p) | |
1847 | { | |
1848 | Lisp_Object value; | |
1849 | ||
1850 | value = image_spec_value (spec, QCwidth, NULL); | |
1851 | img->width = (INTEGERP (value) | |
1852 | ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH); | |
1853 | value = image_spec_value (spec, QCheight, NULL); | |
1854 | img->height = (INTEGERP (value) | |
1855 | ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT); | |
1856 | } | |
1857 | else | |
1858 | { | |
1859 | /* Handle image type independent image attributes | |
1860 | `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF', | |
1861 | `:background COLOR'. */ | |
1862 | Lisp_Object ascent, margin, relief, bg; | |
1863 | ||
1864 | ascent = image_spec_value (spec, QCascent, NULL); | |
1865 | if (INTEGERP (ascent)) | |
1866 | img->ascent = XFASTINT (ascent); | |
1867 | else if (EQ (ascent, Qcenter)) | |
1868 | img->ascent = CENTERED_IMAGE_ASCENT; | |
1869 | ||
1870 | margin = image_spec_value (spec, QCmargin, NULL); | |
1871 | if (INTEGERP (margin) && XINT (margin) >= 0) | |
1872 | img->vmargin = img->hmargin = XFASTINT (margin); | |
1873 | else if (CONSP (margin) && INTEGERP (XCAR (margin)) | |
1874 | && INTEGERP (XCDR (margin))) | |
1875 | { | |
1876 | if (XINT (XCAR (margin)) > 0) | |
1877 | img->hmargin = XFASTINT (XCAR (margin)); | |
1878 | if (XINT (XCDR (margin)) > 0) | |
1879 | img->vmargin = XFASTINT (XCDR (margin)); | |
1880 | } | |
1881 | ||
1882 | relief = image_spec_value (spec, QCrelief, NULL); | |
1883 | if (INTEGERP (relief)) | |
1884 | { | |
1885 | img->relief = XINT (relief); | |
1ea40aa2 EZ |
1886 | img->hmargin += eabs (img->relief); |
1887 | img->vmargin += eabs (img->relief); | |
4ef0d4d0 KS |
1888 | } |
1889 | ||
1890 | if (! img->background_valid) | |
1891 | { | |
1892 | bg = image_spec_value (img->spec, QCbackground, NULL); | |
1893 | if (!NILP (bg)) | |
1894 | { | |
1895 | img->background | |
1896 | = x_alloc_image_color (f, img, bg, | |
1897 | FRAME_BACKGROUND_PIXEL (f)); | |
1898 | img->background_valid = 1; | |
1899 | } | |
1900 | } | |
1901 | ||
1902 | /* Do image transformations and compute masks, unless we | |
1903 | don't have the image yet. */ | |
1904 | if (!EQ (*img->type->type, Qpostscript)) | |
1905 | postprocess_image (f, img); | |
1906 | } | |
1907 | ||
1908 | UNBLOCK_INPUT; | |
1909 | } | |
1910 | ||
1911 | /* We're using IMG, so set its timestamp to `now'. */ | |
1912 | EMACS_GET_TIME (now); | |
1913 | img->timestamp = EMACS_SECS (now); | |
1914 | ||
1915 | UNGCPRO; | |
1916 | ||
1917 | /* Value is the image id. */ | |
1918 | return img->id; | |
1919 | } | |
1920 | ||
1921 | ||
1922 | /* Cache image IMG in the image cache of frame F. */ | |
1923 | ||
1924 | static void | |
1925 | cache_image (f, img) | |
1926 | struct frame *f; | |
1927 | struct image *img; | |
1928 | { | |
354884c4 | 1929 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
1930 | int i; |
1931 | ||
1932 | /* Find a free slot in c->images. */ | |
1933 | for (i = 0; i < c->used; ++i) | |
1934 | if (c->images[i] == NULL) | |
1935 | break; | |
1936 | ||
1937 | /* If no free slot found, maybe enlarge c->images. */ | |
1938 | if (i == c->used && c->used == c->size) | |
1939 | { | |
1940 | c->size *= 2; | |
1941 | c->images = (struct image **) xrealloc (c->images, | |
1942 | c->size * sizeof *c->images); | |
1943 | } | |
1944 | ||
1945 | /* Add IMG to c->images, and assign IMG an id. */ | |
1946 | c->images[i] = img; | |
1947 | img->id = i; | |
1948 | if (i == c->used) | |
1949 | ++c->used; | |
1950 | ||
1951 | /* Add IMG to the cache's hash table. */ | |
1952 | i = img->hash % IMAGE_CACHE_BUCKETS_SIZE; | |
1953 | img->next = c->buckets[i]; | |
1954 | if (img->next) | |
1955 | img->next->prev = img; | |
1956 | img->prev = NULL; | |
1957 | c->buckets[i] = img; | |
1958 | } | |
1959 | ||
1960 | ||
1961 | /* Call FN on every image in the image cache of frame F. Used to mark | |
1962 | Lisp Objects in the image cache. */ | |
1963 | ||
354884c4 SM |
1964 | /* Mark Lisp objects in image IMG. */ |
1965 | ||
1966 | static void | |
1967 | mark_image (img) | |
1968 | struct image *img; | |
1969 | { | |
1970 | mark_object (img->spec); | |
a2bc5bdd | 1971 | mark_object (img->dependencies); |
354884c4 SM |
1972 | |
1973 | if (!NILP (img->data.lisp_val)) | |
1974 | mark_object (img->data.lisp_val); | |
1975 | } | |
1976 | ||
1977 | ||
4ef0d4d0 | 1978 | void |
354884c4 | 1979 | mark_image_cache (struct image_cache *c) |
4ef0d4d0 | 1980 | { |
354884c4 | 1981 | if (c) |
4ef0d4d0 | 1982 | { |
354884c4 SM |
1983 | int i; |
1984 | for (i = 0; i < c->used; ++i) | |
1985 | if (c->images[i]) | |
1986 | mark_image (c->images[i]); | |
4ef0d4d0 KS |
1987 | } |
1988 | } | |
1989 | ||
1990 | ||
1991 | \f | |
1992 | /*********************************************************************** | |
9e2a2647 | 1993 | X / NS / W32 support code |
4ef0d4d0 KS |
1994 | ***********************************************************************/ |
1995 | ||
1996 | #ifdef HAVE_NTGUI | |
1997 | ||
1998 | /* Macro for defining functions that will be loaded from image DLLs. */ | |
09e8eb5b | 1999 | #define DEF_IMGLIB_FN(func) int (FAR CDECL *fn_##func)() |
4ef0d4d0 KS |
2000 | |
2001 | /* Macro for loading those image functions from the library. */ | |
2002 | #define LOAD_IMGLIB_FN(lib,func) { \ | |
2003 | fn_##func = (void *) GetProcAddress (lib, #func); \ | |
2004 | if (!fn_##func) return 0; \ | |
2005 | } | |
2006 | ||
0855eb52 JB |
2007 | /* Load a DLL implementing an image type. |
2008 | The `image-library-alist' variable associates a symbol, | |
3046a84b | 2009 | identifying an image type, to a list of possible filenames. |
0855eb52 JB |
2010 | The function returns NULL if no library could be loaded for |
2011 | the given image type, or if the library was previously loaded; | |
2012 | else the handle of the DLL. */ | |
2013 | static HMODULE | |
2014 | w32_delayed_load (Lisp_Object libraries, Lisp_Object type) | |
2015 | { | |
2016 | HMODULE library = NULL; | |
2017 | ||
2018 | if (CONSP (libraries) && NILP (Fassq (type, Vimage_type_cache))) | |
2019 | { | |
2020 | Lisp_Object dlls = Fassq (type, libraries); | |
2021 | ||
2022 | if (CONSP (dlls)) | |
2023 | for (dlls = XCDR (dlls); CONSP (dlls); dlls = XCDR (dlls)) | |
2024 | { | |
2025 | CHECK_STRING_CAR (dlls); | |
2026 | if (library = LoadLibrary (SDATA (XCAR (dlls)))) | |
2027 | break; | |
2028 | } | |
2029 | } | |
2030 | ||
2031 | return library; | |
2032 | } | |
2033 | ||
4ef0d4d0 KS |
2034 | #endif /* HAVE_NTGUI */ |
2035 | ||
2036 | static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int, | |
2037 | XImagePtr *, Pixmap *)); | |
2038 | static void x_destroy_x_image P_ ((XImagePtr)); | |
2039 | static void x_put_x_image P_ ((struct frame *, XImagePtr, Pixmap, int, int)); | |
2040 | ||
2041 | ||
2042 | /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on | |
2043 | frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created. | |
2044 | Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated | |
2045 | via xmalloc. Print error messages via image_error if an error | |
2046 | occurs. Value is non-zero if successful. | |
2047 | ||
2048 | On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH | |
2049 | should indicate the bit depth of the image. */ | |
2050 | ||
2051 | static int | |
2052 | x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap) | |
2053 | struct frame *f; | |
2054 | int width, height, depth; | |
2055 | XImagePtr *ximg; | |
2056 | Pixmap *pixmap; | |
2057 | { | |
2058 | #ifdef HAVE_X_WINDOWS | |
2059 | Display *display = FRAME_X_DISPLAY (f); | |
2060 | Window window = FRAME_X_WINDOW (f); | |
2061 | Screen *screen = FRAME_X_SCREEN (f); | |
2062 | ||
2063 | xassert (interrupt_input_blocked); | |
2064 | ||
2065 | if (depth <= 0) | |
2066 | depth = DefaultDepthOfScreen (screen); | |
2067 | *ximg = XCreateImage (display, DefaultVisualOfScreen (screen), | |
2068 | depth, ZPixmap, 0, NULL, width, height, | |
2069 | depth > 16 ? 32 : depth > 8 ? 16 : 8, 0); | |
2070 | if (*ximg == NULL) | |
2071 | { | |
2072 | image_error ("Unable to allocate X image", Qnil, Qnil); | |
2073 | return 0; | |
2074 | } | |
2075 | ||
2076 | /* Allocate image raster. */ | |
2077 | (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height); | |
2078 | ||
2079 | /* Allocate a pixmap of the same size. */ | |
2080 | *pixmap = XCreatePixmap (display, window, width, height, depth); | |
2081 | if (*pixmap == NO_PIXMAP) | |
2082 | { | |
2083 | x_destroy_x_image (*ximg); | |
2084 | *ximg = NULL; | |
2085 | image_error ("Unable to create X pixmap", Qnil, Qnil); | |
2086 | return 0; | |
2087 | } | |
2088 | ||
2089 | return 1; | |
2090 | #endif /* HAVE_X_WINDOWS */ | |
2091 | ||
2092 | #ifdef HAVE_NTGUI | |
2093 | ||
2094 | BITMAPINFOHEADER *header; | |
2095 | HDC hdc; | |
2096 | int scanline_width_bits; | |
2097 | int remainder; | |
2098 | int palette_colors = 0; | |
2099 | ||
2100 | if (depth == 0) | |
2101 | depth = 24; | |
2102 | ||
2103 | if (depth != 1 && depth != 4 && depth != 8 | |
2104 | && depth != 16 && depth != 24 && depth != 32) | |
2105 | { | |
2106 | image_error ("Invalid image bit depth specified", Qnil, Qnil); | |
2107 | return 0; | |
2108 | } | |
2109 | ||
2110 | scanline_width_bits = width * depth; | |
2111 | remainder = scanline_width_bits % 32; | |
2112 | ||
2113 | if (remainder) | |
2114 | scanline_width_bits += 32 - remainder; | |
2115 | ||
2116 | /* Bitmaps with a depth less than 16 need a palette. */ | |
2117 | /* BITMAPINFO structure already contains the first RGBQUAD. */ | |
2118 | if (depth < 16) | |
2119 | palette_colors = 1 << depth - 1; | |
2120 | ||
2121 | *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD)); | |
2122 | if (*ximg == NULL) | |
2123 | { | |
2124 | image_error ("Unable to allocate memory for XImage", Qnil, Qnil); | |
2125 | return 0; | |
2126 | } | |
2127 | ||
2128 | header = &((*ximg)->info.bmiHeader); | |
2129 | bzero (&((*ximg)->info), sizeof (BITMAPINFO)); | |
2130 | header->biSize = sizeof (*header); | |
2131 | header->biWidth = width; | |
2132 | header->biHeight = -height; /* negative indicates a top-down bitmap. */ | |
2133 | header->biPlanes = 1; | |
2134 | header->biBitCount = depth; | |
2135 | header->biCompression = BI_RGB; | |
2136 | header->biClrUsed = palette_colors; | |
2137 | ||
2138 | /* TODO: fill in palette. */ | |
2139 | if (depth == 1) | |
2140 | { | |
2141 | (*ximg)->info.bmiColors[0].rgbBlue = 0; | |
2142 | (*ximg)->info.bmiColors[0].rgbGreen = 0; | |
2143 | (*ximg)->info.bmiColors[0].rgbRed = 0; | |
2144 | (*ximg)->info.bmiColors[0].rgbReserved = 0; | |
2145 | (*ximg)->info.bmiColors[1].rgbBlue = 255; | |
2146 | (*ximg)->info.bmiColors[1].rgbGreen = 255; | |
2147 | (*ximg)->info.bmiColors[1].rgbRed = 255; | |
2148 | (*ximg)->info.bmiColors[1].rgbReserved = 0; | |
2149 | } | |
2150 | ||
2151 | hdc = get_frame_dc (f); | |
2152 | ||
2153 | /* Create a DIBSection and raster array for the bitmap, | |
2154 | and store its handle in *pixmap. */ | |
2155 | *pixmap = CreateDIBSection (hdc, &((*ximg)->info), | |
2156 | (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS, | |
2e09fef1 EZ |
2157 | /* casting avoids a GCC warning */ |
2158 | (void **)&((*ximg)->data), NULL, 0); | |
4ef0d4d0 KS |
2159 | |
2160 | /* Realize display palette and garbage all frames. */ | |
2161 | release_frame_dc (f, hdc); | |
2162 | ||
2163 | if (*pixmap == NULL) | |
2164 | { | |
5fc9fdeb | 2165 | DWORD err = GetLastError (); |
4ef0d4d0 KS |
2166 | Lisp_Object errcode; |
2167 | /* All system errors are < 10000, so the following is safe. */ | |
2168 | XSETINT (errcode, (int) err); | |
2169 | image_error ("Unable to create bitmap, error code %d", errcode, Qnil); | |
2170 | x_destroy_x_image (*ximg); | |
2171 | return 0; | |
2172 | } | |
2173 | ||
2174 | return 1; | |
2175 | ||
2176 | #endif /* HAVE_NTGUI */ | |
2177 | ||
edfda783 AR |
2178 | #ifdef HAVE_NS |
2179 | *pixmap = ns_image_for_XPM(width, height, depth); | |
2180 | if (*pixmap == 0) | |
2181 | { | |
2182 | *ximg = NULL; | |
2183 | image_error ("Unable to allocate NSImage for XPM pixmap", Qnil, Qnil); | |
2184 | return 0; | |
2185 | } | |
2186 | *ximg = *pixmap; | |
2187 | return 1; | |
2188 | #endif | |
4ef0d4d0 KS |
2189 | } |
2190 | ||
2191 | ||
2192 | /* Destroy XImage XIMG. Free XIMG->data. */ | |
2193 | ||
2194 | static void | |
2195 | x_destroy_x_image (ximg) | |
2196 | XImagePtr ximg; | |
2197 | { | |
2198 | xassert (interrupt_input_blocked); | |
2199 | if (ximg) | |
2200 | { | |
2201 | #ifdef HAVE_X_WINDOWS | |
2202 | xfree (ximg->data); | |
2203 | ximg->data = NULL; | |
2204 | XDestroyImage (ximg); | |
2205 | #endif /* HAVE_X_WINDOWS */ | |
2206 | #ifdef HAVE_NTGUI | |
2207 | /* Data will be freed by DestroyObject. */ | |
2208 | ximg->data = NULL; | |
2209 | xfree (ximg); | |
2210 | #endif /* HAVE_NTGUI */ | |
edfda783 AR |
2211 | #ifdef HAVE_NS |
2212 | ns_release_object(ximg); | |
2213 | #endif /* HAVE_NS */ | |
4ef0d4d0 KS |
2214 | } |
2215 | } | |
2216 | ||
2217 | ||
2218 | /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT | |
2219 | are width and height of both the image and pixmap. */ | |
2220 | ||
2221 | static void | |
2222 | x_put_x_image (f, ximg, pixmap, width, height) | |
2223 | struct frame *f; | |
2224 | XImagePtr ximg; | |
2225 | Pixmap pixmap; | |
2226 | int width, height; | |
2227 | { | |
2228 | #ifdef HAVE_X_WINDOWS | |
2229 | GC gc; | |
2230 | ||
2231 | xassert (interrupt_input_blocked); | |
2232 | gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL); | |
2233 | XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height); | |
2234 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
2235 | #endif /* HAVE_X_WINDOWS */ | |
2236 | ||
2237 | #ifdef HAVE_NTGUI | |
2238 | #if 0 /* I don't think this is necessary looking at where it is used. */ | |
2239 | HDC hdc = get_frame_dc (f); | |
2240 | SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS); | |
2241 | release_frame_dc (f, hdc); | |
2242 | #endif | |
2243 | #endif /* HAVE_NTGUI */ | |
2244 | ||
edfda783 AR |
2245 | #ifdef HAVE_NS |
2246 | xassert (ximg == pixmap); | |
2247 | ns_retain_object(ximg); | |
2248 | #endif | |
4ef0d4d0 KS |
2249 | } |
2250 | ||
2251 | \f | |
2252 | /*********************************************************************** | |
2253 | File Handling | |
2254 | ***********************************************************************/ | |
2255 | ||
4ef0d4d0 KS |
2256 | static unsigned char *slurp_file P_ ((char *, int *)); |
2257 | ||
2258 | ||
6678dc11 | 2259 | /* Find image file FILE. Look in data-directory/images, then |
022af124 KH |
2260 | x-bitmap-file-path. Value is the encoded full name of the file |
2261 | found, or nil if not found. */ | |
4ef0d4d0 | 2262 | |
c640aa9b | 2263 | Lisp_Object |
4ef0d4d0 KS |
2264 | x_find_image_file (file) |
2265 | Lisp_Object file; | |
2266 | { | |
2267 | Lisp_Object file_found, search_path; | |
2268 | struct gcpro gcpro1, gcpro2; | |
2269 | int fd; | |
2270 | ||
2271 | file_found = Qnil; | |
6678dc11 GM |
2272 | /* TODO I think this should use something like image-load-path |
2273 | instead. Unfortunately, that can contain non-string elements. */ | |
2274 | search_path = Fcons (Fexpand_file_name (build_string ("images"), | |
2275 | Vdata_directory), | |
2276 | Vx_bitmap_file_path); | |
4ef0d4d0 KS |
2277 | GCPRO2 (file_found, search_path); |
2278 | ||
050b82f6 | 2279 | /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */ |
4ef0d4d0 KS |
2280 | fd = openp (search_path, file, Qnil, &file_found, Qnil); |
2281 | ||
2282 | if (fd == -1) | |
2283 | file_found = Qnil; | |
2284 | else | |
022af124 KH |
2285 | { |
2286 | file_found = ENCODE_FILE (file_found); | |
2287 | close (fd); | |
2288 | } | |
4ef0d4d0 KS |
2289 | |
2290 | UNGCPRO; | |
2291 | return file_found; | |
2292 | } | |
2293 | ||
2294 | ||
2295 | /* Read FILE into memory. Value is a pointer to a buffer allocated | |
2296 | with xmalloc holding FILE's contents. Value is null if an error | |
2297 | occurred. *SIZE is set to the size of the file. */ | |
2298 | ||
2299 | static unsigned char * | |
2300 | slurp_file (file, size) | |
2301 | char *file; | |
2302 | int *size; | |
2303 | { | |
2304 | FILE *fp = NULL; | |
2305 | unsigned char *buf = NULL; | |
2306 | struct stat st; | |
2307 | ||
2308 | if (stat (file, &st) == 0 | |
2309 | && (fp = fopen (file, "rb")) != NULL | |
fa8459a3 | 2310 | && (buf = (unsigned char *) xmalloc (st.st_size), |
4ef0d4d0 KS |
2311 | fread (buf, 1, st.st_size, fp) == st.st_size)) |
2312 | { | |
2313 | *size = st.st_size; | |
2314 | fclose (fp); | |
2315 | } | |
2316 | else | |
2317 | { | |
2318 | if (fp) | |
2319 | fclose (fp); | |
2320 | if (buf) | |
2321 | { | |
2322 | xfree (buf); | |
2323 | buf = NULL; | |
2324 | } | |
2325 | } | |
2326 | ||
2327 | return buf; | |
2328 | } | |
2329 | ||
2330 | ||
2331 | \f | |
4ef0d4d0 KS |
2332 | /*********************************************************************** |
2333 | XBM images | |
2334 | ***********************************************************************/ | |
2335 | ||
2336 | static int xbm_scan P_ ((unsigned char **, unsigned char *, char *, int *)); | |
2337 | static int xbm_load P_ ((struct frame *f, struct image *img)); | |
2338 | static int xbm_load_image P_ ((struct frame *f, struct image *img, | |
2339 | unsigned char *, unsigned char *)); | |
2340 | static int xbm_image_p P_ ((Lisp_Object object)); | |
f1f25b99 CY |
2341 | static int xbm_read_bitmap_data P_ ((struct frame *f, |
2342 | unsigned char *, unsigned char *, | |
ce959360 | 2343 | int *, int *, unsigned char **, int)); |
4ef0d4d0 KS |
2344 | static int xbm_file_p P_ ((Lisp_Object)); |
2345 | ||
2346 | ||
2347 | /* Indices of image specification fields in xbm_format, below. */ | |
2348 | ||
2349 | enum xbm_keyword_index | |
2350 | { | |
2351 | XBM_TYPE, | |
2352 | XBM_FILE, | |
2353 | XBM_WIDTH, | |
2354 | XBM_HEIGHT, | |
2355 | XBM_DATA, | |
2356 | XBM_FOREGROUND, | |
2357 | XBM_BACKGROUND, | |
2358 | XBM_ASCENT, | |
2359 | XBM_MARGIN, | |
2360 | XBM_RELIEF, | |
2361 | XBM_ALGORITHM, | |
2362 | XBM_HEURISTIC_MASK, | |
2363 | XBM_MASK, | |
2364 | XBM_LAST | |
2365 | }; | |
2366 | ||
2367 | /* Vector of image_keyword structures describing the format | |
2368 | of valid XBM image specifications. */ | |
2369 | ||
91433552 | 2370 | static const struct image_keyword xbm_format[XBM_LAST] = |
4ef0d4d0 KS |
2371 | { |
2372 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
2373 | {":file", IMAGE_STRING_VALUE, 0}, | |
2374 | {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2375 | {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2376 | {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2377 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2378 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2379 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
2380 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
2381 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
2382 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2383 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2384 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0} | |
2385 | }; | |
2386 | ||
2387 | /* Structure describing the image type XBM. */ | |
2388 | ||
2389 | static struct image_type xbm_type = | |
2390 | { | |
2391 | &Qxbm, | |
2392 | xbm_image_p, | |
2393 | xbm_load, | |
2394 | x_clear_image, | |
2395 | NULL | |
2396 | }; | |
2397 | ||
2398 | /* Tokens returned from xbm_scan. */ | |
2399 | ||
2400 | enum xbm_token | |
2401 | { | |
2402 | XBM_TK_IDENT = 256, | |
2403 | XBM_TK_NUMBER | |
2404 | }; | |
2405 | ||
2406 | ||
2407 | /* Return non-zero if OBJECT is a valid XBM-type image specification. | |
2408 | A valid specification is a list starting with the symbol `image' | |
2409 | The rest of the list is a property list which must contain an | |
2410 | entry `:type xbm.. | |
2411 | ||
2412 | If the specification specifies a file to load, it must contain | |
2413 | an entry `:file FILENAME' where FILENAME is a string. | |
2414 | ||
2415 | If the specification is for a bitmap loaded from memory it must | |
2416 | contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where | |
2417 | WIDTH and HEIGHT are integers > 0. DATA may be: | |
2418 | ||
2419 | 1. a string large enough to hold the bitmap data, i.e. it must | |
2420 | have a size >= (WIDTH + 7) / 8 * HEIGHT | |
2421 | ||
2422 | 2. a bool-vector of size >= WIDTH * HEIGHT | |
2423 | ||
2424 | 3. a vector of strings or bool-vectors, one for each line of the | |
2425 | bitmap. | |
2426 | ||
bbe6f2aa | 2427 | 4. a string containing an in-memory XBM file. WIDTH and HEIGHT |
4ef0d4d0 KS |
2428 | may not be specified in this case because they are defined in the |
2429 | XBM file. | |
2430 | ||
2431 | Both the file and data forms may contain the additional entries | |
2432 | `:background COLOR' and `:foreground COLOR'. If not present, | |
2433 | foreground and background of the frame on which the image is | |
2434 | displayed is used. */ | |
2435 | ||
2436 | static int | |
2437 | xbm_image_p (object) | |
2438 | Lisp_Object object; | |
2439 | { | |
2440 | struct image_keyword kw[XBM_LAST]; | |
2441 | ||
2442 | bcopy (xbm_format, kw, sizeof kw); | |
2443 | if (!parse_image_spec (object, kw, XBM_LAST, Qxbm)) | |
2444 | return 0; | |
2445 | ||
2446 | xassert (EQ (kw[XBM_TYPE].value, Qxbm)); | |
2447 | ||
2448 | if (kw[XBM_FILE].count) | |
2449 | { | |
2450 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count) | |
2451 | return 0; | |
2452 | } | |
2453 | else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value)) | |
2454 | { | |
2455 | /* In-memory XBM file. */ | |
2456 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count) | |
2457 | return 0; | |
2458 | } | |
2459 | else | |
2460 | { | |
2461 | Lisp_Object data; | |
2462 | int width, height; | |
2463 | ||
2464 | /* Entries for `:width', `:height' and `:data' must be present. */ | |
2465 | if (!kw[XBM_WIDTH].count | |
2466 | || !kw[XBM_HEIGHT].count | |
2467 | || !kw[XBM_DATA].count) | |
2468 | return 0; | |
2469 | ||
2470 | data = kw[XBM_DATA].value; | |
2471 | width = XFASTINT (kw[XBM_WIDTH].value); | |
2472 | height = XFASTINT (kw[XBM_HEIGHT].value); | |
2473 | ||
2474 | /* Check type of data, and width and height against contents of | |
2475 | data. */ | |
2476 | if (VECTORP (data)) | |
2477 | { | |
2478 | int i; | |
2479 | ||
2480 | /* Number of elements of the vector must be >= height. */ | |
2481 | if (XVECTOR (data)->size < height) | |
2482 | return 0; | |
2483 | ||
2484 | /* Each string or bool-vector in data must be large enough | |
2485 | for one line of the image. */ | |
2486 | for (i = 0; i < height; ++i) | |
2487 | { | |
2488 | Lisp_Object elt = XVECTOR (data)->contents[i]; | |
2489 | ||
2490 | if (STRINGP (elt)) | |
2491 | { | |
2492 | if (SCHARS (elt) | |
2493 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR) | |
2494 | return 0; | |
2495 | } | |
2496 | else if (BOOL_VECTOR_P (elt)) | |
2497 | { | |
2498 | if (XBOOL_VECTOR (elt)->size < width) | |
2499 | return 0; | |
2500 | } | |
2501 | else | |
2502 | return 0; | |
2503 | } | |
2504 | } | |
2505 | else if (STRINGP (data)) | |
2506 | { | |
2507 | if (SCHARS (data) | |
2508 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height) | |
2509 | return 0; | |
2510 | } | |
2511 | else if (BOOL_VECTOR_P (data)) | |
2512 | { | |
2513 | if (XBOOL_VECTOR (data)->size < width * height) | |
2514 | return 0; | |
2515 | } | |
2516 | else | |
2517 | return 0; | |
2518 | } | |
2519 | ||
2520 | return 1; | |
2521 | } | |
2522 | ||
2523 | ||
2524 | /* Scan a bitmap file. FP is the stream to read from. Value is | |
2525 | either an enumerator from enum xbm_token, or a character for a | |
2526 | single-character token, or 0 at end of file. If scanning an | |
2527 | identifier, store the lexeme of the identifier in SVAL. If | |
2528 | scanning a number, store its value in *IVAL. */ | |
2529 | ||
2530 | static int | |
2531 | xbm_scan (s, end, sval, ival) | |
2532 | unsigned char **s, *end; | |
2533 | char *sval; | |
2534 | int *ival; | |
2535 | { | |
2536 | unsigned int c; | |
2537 | ||
2538 | loop: | |
2539 | ||
2540 | /* Skip white space. */ | |
2541 | while (*s < end && (c = *(*s)++, isspace (c))) | |
2542 | ; | |
2543 | ||
2544 | if (*s >= end) | |
2545 | c = 0; | |
2546 | else if (isdigit (c)) | |
2547 | { | |
2548 | int value = 0, digit; | |
2549 | ||
2550 | if (c == '0' && *s < end) | |
2551 | { | |
2552 | c = *(*s)++; | |
2553 | if (c == 'x' || c == 'X') | |
2554 | { | |
2555 | while (*s < end) | |
2556 | { | |
2557 | c = *(*s)++; | |
2558 | if (isdigit (c)) | |
2559 | digit = c - '0'; | |
2560 | else if (c >= 'a' && c <= 'f') | |
2561 | digit = c - 'a' + 10; | |
2562 | else if (c >= 'A' && c <= 'F') | |
2563 | digit = c - 'A' + 10; | |
2564 | else | |
2565 | break; | |
2566 | value = 16 * value + digit; | |
2567 | } | |
2568 | } | |
2569 | else if (isdigit (c)) | |
2570 | { | |
2571 | value = c - '0'; | |
2572 | while (*s < end | |
2573 | && (c = *(*s)++, isdigit (c))) | |
2574 | value = 8 * value + c - '0'; | |
2575 | } | |
2576 | } | |
2577 | else | |
2578 | { | |
2579 | value = c - '0'; | |
2580 | while (*s < end | |
2581 | && (c = *(*s)++, isdigit (c))) | |
2582 | value = 10 * value + c - '0'; | |
2583 | } | |
2584 | ||
2585 | if (*s < end) | |
2586 | *s = *s - 1; | |
2587 | *ival = value; | |
2588 | c = XBM_TK_NUMBER; | |
2589 | } | |
2590 | else if (isalpha (c) || c == '_') | |
2591 | { | |
2592 | *sval++ = c; | |
2593 | while (*s < end | |
2594 | && (c = *(*s)++, (isalnum (c) || c == '_'))) | |
2595 | *sval++ = c; | |
2596 | *sval = 0; | |
2597 | if (*s < end) | |
2598 | *s = *s - 1; | |
2599 | c = XBM_TK_IDENT; | |
2600 | } | |
2601 | else if (c == '/' && **s == '*') | |
2602 | { | |
2603 | /* C-style comment. */ | |
2604 | ++*s; | |
2605 | while (**s && (**s != '*' || *(*s + 1) != '/')) | |
2606 | ++*s; | |
2607 | if (**s) | |
2608 | { | |
2609 | *s += 2; | |
2610 | goto loop; | |
2611 | } | |
2612 | } | |
2613 | ||
2614 | return c; | |
2615 | } | |
2616 | ||
2617 | #ifdef HAVE_NTGUI | |
2618 | ||
2619 | /* Create a Windows bitmap from X bitmap data. */ | |
2620 | static HBITMAP | |
2621 | w32_create_pixmap_from_bitmap_data (int width, int height, char *data) | |
2622 | { | |
2623 | static unsigned char swap_nibble[16] | |
2624 | = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */ | |
2625 | 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */ | |
2626 | 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */ | |
2627 | 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */ | |
2628 | int i, j, w1, w2; | |
2629 | unsigned char *bits, *p; | |
2630 | HBITMAP bmp; | |
2631 | ||
2632 | w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */ | |
2633 | w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */ | |
2634 | bits = (unsigned char *) alloca (height * w2); | |
2635 | bzero (bits, height * w2); | |
2636 | for (i = 0; i < height; i++) | |
2637 | { | |
2638 | p = bits + i*w2; | |
2639 | for (j = 0; j < w1; j++) | |
2640 | { | |
2641 | /* Bitswap XBM bytes to match how Windows does things. */ | |
2642 | unsigned char c = *data++; | |
2643 | *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4) | |
2644 | | (swap_nibble[(c>>4) & 0xf])); | |
2645 | } | |
2646 | } | |
2647 | bmp = CreateBitmap (width, height, 1, 1, (char *) bits); | |
2648 | ||
2649 | return bmp; | |
2650 | } | |
2651 | ||
3046a84b JB |
2652 | static void |
2653 | convert_mono_to_color_image (f, img, foreground, background) | |
4ef0d4d0 KS |
2654 | struct frame *f; |
2655 | struct image *img; | |
2656 | COLORREF foreground, background; | |
2657 | { | |
2658 | HDC hdc, old_img_dc, new_img_dc; | |
2659 | HGDIOBJ old_prev, new_prev; | |
2660 | HBITMAP new_pixmap; | |
2661 | ||
2662 | hdc = get_frame_dc (f); | |
2663 | old_img_dc = CreateCompatibleDC (hdc); | |
2664 | new_img_dc = CreateCompatibleDC (hdc); | |
2665 | new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height); | |
2666 | release_frame_dc (f, hdc); | |
2667 | old_prev = SelectObject (old_img_dc, img->pixmap); | |
2668 | new_prev = SelectObject (new_img_dc, new_pixmap); | |
44b1dc2e JR |
2669 | /* Windows convention for mono bitmaps is black = background, |
2670 | white = foreground. */ | |
2386b1f1 JR |
2671 | SetTextColor (new_img_dc, background); |
2672 | SetBkColor (new_img_dc, foreground); | |
4ef0d4d0 KS |
2673 | |
2674 | BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc, | |
2675 | 0, 0, SRCCOPY); | |
2676 | ||
2677 | SelectObject (old_img_dc, old_prev); | |
2678 | SelectObject (new_img_dc, new_prev); | |
2679 | DeleteDC (old_img_dc); | |
2680 | DeleteDC (new_img_dc); | |
2681 | DeleteObject (img->pixmap); | |
2682 | if (new_pixmap == 0) | |
2683 | fprintf (stderr, "Failed to convert image to color.\n"); | |
2684 | else | |
2685 | img->pixmap = new_pixmap; | |
2686 | } | |
2687 | ||
2688 | #define XBM_BIT_SHUFFLE(b) (~(b)) | |
2689 | ||
2690 | #else | |
2691 | ||
2692 | #define XBM_BIT_SHUFFLE(b) (b) | |
2693 | ||
2694 | #endif /* HAVE_NTGUI */ | |
2695 | ||
2696 | ||
2697 | static void | |
3046a84b | 2698 | Create_Pixmap_From_Bitmap_Data (f, img, data, fg, bg, non_default_colors) |
4ef0d4d0 KS |
2699 | struct frame *f; |
2700 | struct image *img; | |
2701 | char *data; | |
2702 | RGB_PIXEL_COLOR fg, bg; | |
2703 | int non_default_colors; | |
2704 | { | |
2705 | #ifdef HAVE_NTGUI | |
2706 | img->pixmap | |
2707 | = w32_create_pixmap_from_bitmap_data (img->width, img->height, data); | |
2708 | ||
2709 | /* If colors were specified, transfer the bitmap to a color one. */ | |
2710 | if (non_default_colors) | |
2711 | convert_mono_to_color_image (f, img, fg, bg); | |
edfda783 AR |
2712 | |
2713 | #elif defined (HAVE_NS) | |
2714 | img->pixmap = ns_image_from_XBM(data, img->width, img->height); | |
2715 | ||
4ef0d4d0 KS |
2716 | #else |
2717 | img->pixmap | |
2718 | = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f), | |
2719 | FRAME_X_WINDOW (f), | |
2720 | data, | |
2721 | img->width, img->height, | |
2722 | fg, bg, | |
2723 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))); | |
edfda783 | 2724 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
2725 | } |
2726 | ||
2727 | ||
2728 | ||
2729 | /* Replacement for XReadBitmapFileData which isn't available under old | |
2730 | X versions. CONTENTS is a pointer to a buffer to parse; END is the | |
2731 | buffer's end. Set *WIDTH and *HEIGHT to the width and height of | |
2732 | the image. Return in *DATA the bitmap data allocated with xmalloc. | |
2733 | Value is non-zero if successful. DATA null means just test if | |
ce959360 CY |
2734 | CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR |
2735 | is non-zero, inhibit the call to image_error when the image size is | |
2736 | invalid (the bitmap remains unread). */ | |
4ef0d4d0 KS |
2737 | |
2738 | static int | |
ce959360 | 2739 | xbm_read_bitmap_data (f, contents, end, width, height, data, inhibit_image_error) |
f1f25b99 | 2740 | struct frame *f; |
4ef0d4d0 KS |
2741 | unsigned char *contents, *end; |
2742 | int *width, *height; | |
2743 | unsigned char **data; | |
ce959360 | 2744 | int inhibit_image_error; |
4ef0d4d0 KS |
2745 | { |
2746 | unsigned char *s = contents; | |
2747 | char buffer[BUFSIZ]; | |
2748 | int padding_p = 0; | |
2749 | int v10 = 0; | |
2750 | int bytes_per_line, i, nbytes; | |
2751 | unsigned char *p; | |
2752 | int value; | |
2753 | int LA1; | |
2754 | ||
2755 | #define match() \ | |
2756 | LA1 = xbm_scan (&s, end, buffer, &value) | |
2757 | ||
2758 | #define expect(TOKEN) \ | |
2759 | if (LA1 != (TOKEN)) \ | |
2760 | goto failure; \ | |
2761 | else \ | |
2762 | match () | |
2763 | ||
2764 | #define expect_ident(IDENT) \ | |
2765 | if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \ | |
2766 | match (); \ | |
2767 | else \ | |
2768 | goto failure | |
2769 | ||
2770 | *width = *height = -1; | |
2771 | if (data) | |
2772 | *data = NULL; | |
2773 | LA1 = xbm_scan (&s, end, buffer, &value); | |
2774 | ||
2775 | /* Parse defines for width, height and hot-spots. */ | |
2776 | while (LA1 == '#') | |
2777 | { | |
2778 | match (); | |
2779 | expect_ident ("define"); | |
2780 | expect (XBM_TK_IDENT); | |
2781 | ||
431feaf6 | 2782 | if (LA1 == XBM_TK_NUMBER) |
4ef0d4d0 KS |
2783 | { |
2784 | char *p = strrchr (buffer, '_'); | |
2785 | p = p ? p + 1 : buffer; | |
2786 | if (strcmp (p, "width") == 0) | |
2787 | *width = value; | |
2788 | else if (strcmp (p, "height") == 0) | |
2789 | *height = value; | |
2790 | } | |
2791 | expect (XBM_TK_NUMBER); | |
2792 | } | |
2793 | ||
f1f25b99 | 2794 | if (!check_image_size (f, *width, *height)) |
10ea2b82 | 2795 | { |
ce959360 CY |
2796 | if (!inhibit_image_error) |
2797 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
10ea2b82 JR |
2798 | goto failure; |
2799 | } | |
4ef0d4d0 KS |
2800 | else if (data == NULL) |
2801 | goto success; | |
2802 | ||
2803 | /* Parse bits. Must start with `static'. */ | |
2804 | expect_ident ("static"); | |
2805 | if (LA1 == XBM_TK_IDENT) | |
2806 | { | |
2807 | if (strcmp (buffer, "unsigned") == 0) | |
2808 | { | |
2809 | match (); | |
2810 | expect_ident ("char"); | |
2811 | } | |
2812 | else if (strcmp (buffer, "short") == 0) | |
2813 | { | |
2814 | match (); | |
2815 | v10 = 1; | |
2816 | if (*width % 16 && *width % 16 < 9) | |
2817 | padding_p = 1; | |
2818 | } | |
2819 | else if (strcmp (buffer, "char") == 0) | |
2820 | match (); | |
2821 | else | |
2822 | goto failure; | |
2823 | } | |
2824 | else | |
2825 | goto failure; | |
2826 | ||
2827 | expect (XBM_TK_IDENT); | |
2828 | expect ('['); | |
2829 | expect (']'); | |
2830 | expect ('='); | |
2831 | expect ('{'); | |
2832 | ||
2833 | bytes_per_line = (*width + 7) / 8 + padding_p; | |
2834 | nbytes = bytes_per_line * *height; | |
fa8459a3 | 2835 | p = *data = (unsigned char *) xmalloc (nbytes); |
4ef0d4d0 KS |
2836 | |
2837 | if (v10) | |
2838 | { | |
2839 | for (i = 0; i < nbytes; i += 2) | |
2840 | { | |
2841 | int val = value; | |
2842 | expect (XBM_TK_NUMBER); | |
2843 | ||
2844 | *p++ = XBM_BIT_SHUFFLE (val); | |
2845 | if (!padding_p || ((i + 2) % bytes_per_line)) | |
2846 | *p++ = XBM_BIT_SHUFFLE (value >> 8); | |
2847 | ||
2848 | if (LA1 == ',' || LA1 == '}') | |
2849 | match (); | |
2850 | else | |
2851 | goto failure; | |
2852 | } | |
2853 | } | |
2854 | else | |
2855 | { | |
2856 | for (i = 0; i < nbytes; ++i) | |
2857 | { | |
2858 | int val = value; | |
2859 | expect (XBM_TK_NUMBER); | |
2860 | ||
2861 | *p++ = XBM_BIT_SHUFFLE (val); | |
2862 | ||
2863 | if (LA1 == ',' || LA1 == '}') | |
2864 | match (); | |
2865 | else | |
2866 | goto failure; | |
2867 | } | |
2868 | } | |
2869 | ||
2870 | success: | |
2871 | return 1; | |
2872 | ||
2873 | failure: | |
2874 | ||
2875 | if (data && *data) | |
2876 | { | |
2877 | xfree (*data); | |
2878 | *data = NULL; | |
2879 | } | |
2880 | return 0; | |
2881 | ||
2882 | #undef match | |
2883 | #undef expect | |
2884 | #undef expect_ident | |
2885 | } | |
2886 | ||
2887 | ||
2888 | /* Load XBM image IMG which will be displayed on frame F from buffer | |
2889 | CONTENTS. END is the end of the buffer. Value is non-zero if | |
2890 | successful. */ | |
2891 | ||
2892 | static int | |
2893 | xbm_load_image (f, img, contents, end) | |
2894 | struct frame *f; | |
2895 | struct image *img; | |
2896 | unsigned char *contents, *end; | |
2897 | { | |
2898 | int rc; | |
2899 | unsigned char *data; | |
2900 | int success_p = 0; | |
2901 | ||
ce959360 CY |
2902 | rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height, |
2903 | &data, 0); | |
4ef0d4d0 KS |
2904 | if (rc) |
2905 | { | |
2906 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2907 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
2908 | int non_default_colors = 0; | |
2909 | Lisp_Object value; | |
2910 | ||
2911 | xassert (img->width > 0 && img->height > 0); | |
2912 | ||
2913 | /* Get foreground and background colors, maybe allocate colors. */ | |
2914 | value = image_spec_value (img->spec, QCforeground, NULL); | |
2915 | if (!NILP (value)) | |
2916 | { | |
2917 | foreground = x_alloc_image_color (f, img, value, foreground); | |
2918 | non_default_colors = 1; | |
2919 | } | |
2920 | value = image_spec_value (img->spec, QCbackground, NULL); | |
2921 | if (!NILP (value)) | |
2922 | { | |
2923 | background = x_alloc_image_color (f, img, value, background); | |
2924 | img->background = background; | |
2925 | img->background_valid = 1; | |
2926 | non_default_colors = 1; | |
2927 | } | |
2928 | ||
1f026899 | 2929 | Create_Pixmap_From_Bitmap_Data (f, img, data, |
4ef0d4d0 KS |
2930 | foreground, background, |
2931 | non_default_colors); | |
2932 | xfree (data); | |
2933 | ||
2934 | if (img->pixmap == NO_PIXMAP) | |
2935 | { | |
2936 | x_clear_image (f, img); | |
2937 | image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil); | |
2938 | } | |
2939 | else | |
2940 | success_p = 1; | |
2941 | } | |
2942 | else | |
2943 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
2944 | ||
2945 | return success_p; | |
2946 | } | |
2947 | ||
2948 | ||
2949 | /* Value is non-zero if DATA looks like an in-memory XBM file. */ | |
2950 | ||
2951 | static int | |
2952 | xbm_file_p (data) | |
2953 | Lisp_Object data; | |
2954 | { | |
2955 | int w, h; | |
2956 | return (STRINGP (data) | |
f1f25b99 | 2957 | && xbm_read_bitmap_data (NULL, SDATA (data), |
ce959360 CY |
2958 | (SDATA (data) + SBYTES (data)), |
2959 | &w, &h, NULL, 1)); | |
4ef0d4d0 KS |
2960 | } |
2961 | ||
2962 | ||
2963 | /* Fill image IMG which is used on frame F with pixmap data. Value is | |
2964 | non-zero if successful. */ | |
2965 | ||
2966 | static int | |
2967 | xbm_load (f, img) | |
2968 | struct frame *f; | |
2969 | struct image *img; | |
2970 | { | |
2971 | int success_p = 0; | |
2972 | Lisp_Object file_name; | |
2973 | ||
2974 | xassert (xbm_image_p (img->spec)); | |
2975 | ||
2976 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
2977 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
2978 | if (STRINGP (file_name)) | |
2979 | { | |
2980 | Lisp_Object file; | |
2981 | unsigned char *contents; | |
2982 | int size; | |
2983 | struct gcpro gcpro1; | |
2984 | ||
2985 | file = x_find_image_file (file_name); | |
2986 | GCPRO1 (file); | |
2987 | if (!STRINGP (file)) | |
2988 | { | |
2989 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
2990 | UNGCPRO; | |
2991 | return 0; | |
2992 | } | |
2993 | ||
2994 | contents = slurp_file (SDATA (file), &size); | |
2995 | if (contents == NULL) | |
2996 | { | |
2997 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
2998 | UNGCPRO; | |
2999 | return 0; | |
3000 | } | |
3001 | ||
3002 | success_p = xbm_load_image (f, img, contents, contents + size); | |
3003 | UNGCPRO; | |
3004 | } | |
3005 | else | |
3006 | { | |
3007 | struct image_keyword fmt[XBM_LAST]; | |
3008 | Lisp_Object data; | |
3009 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
3010 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
3011 | int non_default_colors = 0; | |
3012 | char *bits; | |
3013 | int parsed_p; | |
3014 | int in_memory_file_p = 0; | |
3015 | ||
3016 | /* See if data looks like an in-memory XBM file. */ | |
3017 | data = image_spec_value (img->spec, QCdata, NULL); | |
3018 | in_memory_file_p = xbm_file_p (data); | |
3019 | ||
3020 | /* Parse the image specification. */ | |
3021 | bcopy (xbm_format, fmt, sizeof fmt); | |
3022 | parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm); | |
3023 | xassert (parsed_p); | |
3024 | ||
3025 | /* Get specified width, and height. */ | |
3026 | if (!in_memory_file_p) | |
3027 | { | |
3028 | img->width = XFASTINT (fmt[XBM_WIDTH].value); | |
3029 | img->height = XFASTINT (fmt[XBM_HEIGHT].value); | |
3030 | xassert (img->width > 0 && img->height > 0); | |
3031 | } | |
3032 | ||
3033 | /* Get foreground and background colors, maybe allocate colors. */ | |
3034 | if (fmt[XBM_FOREGROUND].count | |
3035 | && STRINGP (fmt[XBM_FOREGROUND].value)) | |
3036 | { | |
3037 | foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value, | |
3038 | foreground); | |
3039 | non_default_colors = 1; | |
3040 | } | |
3041 | ||
3042 | if (fmt[XBM_BACKGROUND].count | |
3043 | && STRINGP (fmt[XBM_BACKGROUND].value)) | |
3044 | { | |
3045 | background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value, | |
3046 | background); | |
3047 | non_default_colors = 1; | |
3048 | } | |
3049 | ||
3050 | if (in_memory_file_p) | |
3051 | success_p = xbm_load_image (f, img, SDATA (data), | |
3052 | (SDATA (data) | |
3053 | + SBYTES (data))); | |
3054 | else | |
3055 | { | |
3056 | if (VECTORP (data)) | |
3057 | { | |
3058 | int i; | |
3059 | char *p; | |
3060 | int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR; | |
3061 | ||
3062 | p = bits = (char *) alloca (nbytes * img->height); | |
3063 | for (i = 0; i < img->height; ++i, p += nbytes) | |
3064 | { | |
3065 | Lisp_Object line = XVECTOR (data)->contents[i]; | |
3066 | if (STRINGP (line)) | |
3067 | bcopy (SDATA (line), p, nbytes); | |
3068 | else | |
3069 | bcopy (XBOOL_VECTOR (line)->data, p, nbytes); | |
3070 | } | |
3071 | } | |
3072 | else if (STRINGP (data)) | |
3073 | bits = SDATA (data); | |
3074 | else | |
3075 | bits = XBOOL_VECTOR (data)->data; | |
3076 | ||
44b1dc2e JR |
3077 | #ifdef WINDOWSNT |
3078 | { | |
3079 | char *invertedBits; | |
3080 | int nbytes, i; | |
3081 | /* Windows mono bitmaps are reversed compared with X. */ | |
3082 | invertedBits = bits; | |
285d07e2 | 3083 | nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR |
44b1dc2e JR |
3084 | * img->height; |
3085 | bits = (char *) alloca(nbytes); | |
3086 | for (i = 0; i < nbytes; i++) | |
3087 | bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]); | |
3088 | } | |
3089 | #endif | |
4ef0d4d0 KS |
3090 | /* Create the pixmap. */ |
3091 | ||
3092 | Create_Pixmap_From_Bitmap_Data (f, img, bits, | |
3093 | foreground, background, | |
3094 | non_default_colors); | |
3095 | if (img->pixmap) | |
3096 | success_p = 1; | |
3097 | else | |
3098 | { | |
3099 | image_error ("Unable to create pixmap for XBM image `%s'", | |
3100 | img->spec, Qnil); | |
3101 | x_clear_image (f, img); | |
3102 | } | |
3103 | } | |
3104 | } | |
3105 | ||
3106 | return success_p; | |
3107 | } | |
3108 | ||
3109 | ||
3110 | \f | |
3111 | /*********************************************************************** | |
3112 | XPM images | |
3113 | ***********************************************************************/ | |
3114 | ||
9e2a2647 | 3115 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 KS |
3116 | |
3117 | static int xpm_image_p P_ ((Lisp_Object object)); | |
3118 | static int xpm_load P_ ((struct frame *f, struct image *img)); | |
3119 | static int xpm_valid_color_symbols_p P_ ((Lisp_Object)); | |
3120 | ||
9e2a2647 | 3121 | #endif /* HAVE_XPM || HAVE_NS */ |
ea1aaa6f ST |
3122 | |
3123 | #ifdef HAVE_XPM | |
4ef0d4d0 KS |
3124 | #ifdef HAVE_NTGUI |
3125 | /* Indicate to xpm.h that we don't have Xlib. */ | |
3126 | #define FOR_MSW | |
3127 | /* simx.h in xpm defines XColor and XImage differently than Emacs. */ | |
279d3293 | 3128 | /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */ |
4ef0d4d0 KS |
3129 | #define XColor xpm_XColor |
3130 | #define XImage xpm_XImage | |
279d3293 | 3131 | #define Display xpm_Display |
4ef0d4d0 KS |
3132 | #define PIXEL_ALREADY_TYPEDEFED |
3133 | #include "X11/xpm.h" | |
3134 | #undef FOR_MSW | |
3135 | #undef XColor | |
3136 | #undef XImage | |
279d3293 | 3137 | #undef Display |
4ef0d4d0 KS |
3138 | #undef PIXEL_ALREADY_TYPEDEFED |
3139 | #else | |
3140 | #include "X11/xpm.h" | |
3141 | #endif /* HAVE_NTGUI */ | |
ea1aaa6f | 3142 | #endif /* HAVE_XPM */ |
4ef0d4d0 | 3143 | |
9e2a2647 | 3144 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 KS |
3145 | /* The symbol `xpm' identifying XPM-format images. */ |
3146 | ||
3147 | Lisp_Object Qxpm; | |
3148 | ||
3149 | /* Indices of image specification fields in xpm_format, below. */ | |
3150 | ||
3151 | enum xpm_keyword_index | |
3152 | { | |
3153 | XPM_TYPE, | |
3154 | XPM_FILE, | |
3155 | XPM_DATA, | |
3156 | XPM_ASCENT, | |
3157 | XPM_MARGIN, | |
3158 | XPM_RELIEF, | |
3159 | XPM_ALGORITHM, | |
3160 | XPM_HEURISTIC_MASK, | |
3161 | XPM_MASK, | |
3162 | XPM_COLOR_SYMBOLS, | |
3163 | XPM_BACKGROUND, | |
3164 | XPM_LAST | |
3165 | }; | |
3166 | ||
3167 | /* Vector of image_keyword structures describing the format | |
3168 | of valid XPM image specifications. */ | |
3169 | ||
91433552 | 3170 | static const struct image_keyword xpm_format[XPM_LAST] = |
4ef0d4d0 KS |
3171 | { |
3172 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
3173 | {":file", IMAGE_STRING_VALUE, 0}, | |
3174 | {":data", IMAGE_STRING_VALUE, 0}, | |
3175 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
3176 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
3177 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
3178 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3179 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3180 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3181 | {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3182 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
3183 | }; | |
3184 | ||
3185 | /* Structure describing the image type XPM. */ | |
3186 | ||
3187 | static struct image_type xpm_type = | |
3188 | { | |
3189 | &Qxpm, | |
3190 | xpm_image_p, | |
3191 | xpm_load, | |
3192 | x_clear_image, | |
3193 | NULL | |
3194 | }; | |
3195 | ||
3196 | #ifdef HAVE_X_WINDOWS | |
3197 | ||
3198 | /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation | |
3199 | functions for allocating image colors. Our own functions handle | |
3200 | color allocation failures more gracefully than the ones on the XPM | |
3201 | lib. */ | |
3202 | ||
3203 | #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure | |
3204 | #define ALLOC_XPM_COLORS | |
3205 | #endif | |
3206 | #endif /* HAVE_X_WINDOWS */ | |
3207 | ||
3208 | #ifdef ALLOC_XPM_COLORS | |
3209 | ||
3210 | static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *)); | |
3211 | static void xpm_free_color_cache P_ ((void)); | |
3212 | static int xpm_lookup_color P_ ((struct frame *, char *, XColor *)); | |
3213 | static int xpm_color_bucket P_ ((char *)); | |
3214 | static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *, | |
3215 | XColor *, int)); | |
3216 | ||
3217 | /* An entry in a hash table used to cache color definitions of named | |
3218 | colors. This cache is necessary to speed up XPM image loading in | |
3219 | case we do color allocations ourselves. Without it, we would need | |
3220 | a call to XParseColor per pixel in the image. */ | |
3221 | ||
3222 | struct xpm_cached_color | |
3223 | { | |
3224 | /* Next in collision chain. */ | |
3225 | struct xpm_cached_color *next; | |
3226 | ||
3227 | /* Color definition (RGB and pixel color). */ | |
3228 | XColor color; | |
3229 | ||
3230 | /* Color name. */ | |
3231 | char name[1]; | |
3232 | }; | |
3233 | ||
3234 | /* The hash table used for the color cache, and its bucket vector | |
3235 | size. */ | |
3236 | ||
3237 | #define XPM_COLOR_CACHE_BUCKETS 1001 | |
3238 | struct xpm_cached_color **xpm_color_cache; | |
3239 | ||
3240 | /* Initialize the color cache. */ | |
3241 | ||
3242 | static void | |
3243 | xpm_init_color_cache (f, attrs) | |
3244 | struct frame *f; | |
3245 | XpmAttributes *attrs; | |
3246 | { | |
3247 | size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache; | |
3248 | xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes); | |
3249 | memset (xpm_color_cache, 0, nbytes); | |
3250 | init_color_table (); | |
3251 | ||
3252 | if (attrs->valuemask & XpmColorSymbols) | |
3253 | { | |
3254 | int i; | |
3255 | XColor color; | |
3256 | ||
3257 | for (i = 0; i < attrs->numsymbols; ++i) | |
3258 | if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3259 | attrs->colorsymbols[i].value, &color)) | |
3260 | { | |
3261 | color.pixel = lookup_rgb_color (f, color.red, color.green, | |
3262 | color.blue); | |
3263 | xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1); | |
3264 | } | |
3265 | } | |
3266 | } | |
3267 | ||
3268 | /* Free the color cache. */ | |
3269 | ||
3270 | static void | |
3271 | xpm_free_color_cache () | |
3272 | { | |
3273 | struct xpm_cached_color *p, *next; | |
3274 | int i; | |
3275 | ||
3276 | for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i) | |
3277 | for (p = xpm_color_cache[i]; p; p = next) | |
3278 | { | |
3279 | next = p->next; | |
3280 | xfree (p); | |
3281 | } | |
3282 | ||
3283 | xfree (xpm_color_cache); | |
3284 | xpm_color_cache = NULL; | |
3285 | free_color_table (); | |
3286 | } | |
3287 | ||
3288 | /* Return the bucket index for color named COLOR_NAME in the color | |
3289 | cache. */ | |
3290 | ||
3291 | static int | |
3292 | xpm_color_bucket (color_name) | |
3293 | char *color_name; | |
3294 | { | |
3295 | unsigned h = 0; | |
3296 | char *s; | |
3297 | ||
3298 | for (s = color_name; *s; ++s) | |
3299 | h = (h << 2) ^ *s; | |
3300 | return h %= XPM_COLOR_CACHE_BUCKETS; | |
3301 | } | |
3302 | ||
3303 | ||
3304 | /* On frame F, cache values COLOR for color with name COLOR_NAME. | |
3305 | BUCKET, if >= 0, is a precomputed bucket index. Value is the cache | |
3306 | entry added. */ | |
3307 | ||
3308 | static struct xpm_cached_color * | |
3309 | xpm_cache_color (f, color_name, color, bucket) | |
3310 | struct frame *f; | |
3311 | char *color_name; | |
3312 | XColor *color; | |
3313 | int bucket; | |
3314 | { | |
3315 | size_t nbytes; | |
3316 | struct xpm_cached_color *p; | |
3317 | ||
3318 | if (bucket < 0) | |
3319 | bucket = xpm_color_bucket (color_name); | |
3320 | ||
3321 | nbytes = sizeof *p + strlen (color_name); | |
3322 | p = (struct xpm_cached_color *) xmalloc (nbytes); | |
3323 | strcpy (p->name, color_name); | |
3324 | p->color = *color; | |
3325 | p->next = xpm_color_cache[bucket]; | |
3326 | xpm_color_cache[bucket] = p; | |
3327 | return p; | |
3328 | } | |
3329 | ||
3330 | /* Look up color COLOR_NAME for frame F in the color cache. If found, | |
3331 | return the cached definition in *COLOR. Otherwise, make a new | |
3332 | entry in the cache and allocate the color. Value is zero if color | |
3333 | allocation failed. */ | |
3334 | ||
3335 | static int | |
3336 | xpm_lookup_color (f, color_name, color) | |
3337 | struct frame *f; | |
3338 | char *color_name; | |
3339 | XColor *color; | |
3340 | { | |
3341 | struct xpm_cached_color *p; | |
3342 | int h = xpm_color_bucket (color_name); | |
3343 | ||
3344 | for (p = xpm_color_cache[h]; p; p = p->next) | |
3345 | if (strcmp (p->name, color_name) == 0) | |
3346 | break; | |
3347 | ||
3348 | if (p != NULL) | |
3349 | *color = p->color; | |
3350 | else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3351 | color_name, color)) | |
3352 | { | |
3353 | color->pixel = lookup_rgb_color (f, color->red, color->green, | |
3354 | color->blue); | |
3355 | p = xpm_cache_color (f, color_name, color, h); | |
3356 | } | |
3357 | /* You get `opaque' at least from ImageMagick converting pbm to xpm | |
3358 | with transparency, and it's useful. */ | |
3359 | else if (strcmp ("opaque", color_name) == 0) | |
3360 | { | |
3361 | bzero (color, sizeof (XColor)); /* Is this necessary/correct? */ | |
3362 | color->pixel = FRAME_FOREGROUND_PIXEL (f); | |
3363 | p = xpm_cache_color (f, color_name, color, h); | |
3364 | } | |
3365 | ||
3366 | return p != NULL; | |
3367 | } | |
3368 | ||
3369 | ||
3370 | /* Callback for allocating color COLOR_NAME. Called from the XPM lib. | |
3371 | CLOSURE is a pointer to the frame on which we allocate the | |
3372 | color. Return in *COLOR the allocated color. Value is non-zero | |
3373 | if successful. */ | |
3374 | ||
3375 | static int | |
3376 | xpm_alloc_color (dpy, cmap, color_name, color, closure) | |
3377 | Display *dpy; | |
3378 | Colormap cmap; | |
3379 | char *color_name; | |
3380 | XColor *color; | |
3381 | void *closure; | |
3382 | { | |
3383 | return xpm_lookup_color ((struct frame *) closure, color_name, color); | |
3384 | } | |
3385 | ||
3386 | ||
3387 | /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE | |
3388 | is a pointer to the frame on which we allocate the color. Value is | |
3389 | non-zero if successful. */ | |
3390 | ||
3391 | static int | |
3392 | xpm_free_colors (dpy, cmap, pixels, npixels, closure) | |
3393 | Display *dpy; | |
3394 | Colormap cmap; | |
3395 | Pixel *pixels; | |
3396 | int npixels; | |
3397 | void *closure; | |
3398 | { | |
3399 | return 1; | |
3400 | } | |
3401 | ||
3402 | #endif /* ALLOC_XPM_COLORS */ | |
3403 | ||
3404 | ||
3405 | #ifdef HAVE_NTGUI | |
3406 | ||
3407 | /* XPM library details. */ | |
3408 | ||
3409 | DEF_IMGLIB_FN (XpmFreeAttributes); | |
3410 | DEF_IMGLIB_FN (XpmCreateImageFromBuffer); | |
3411 | DEF_IMGLIB_FN (XpmReadFileToImage); | |
3412 | DEF_IMGLIB_FN (XImageFree); | |
3413 | ||
4ef0d4d0 | 3414 | static int |
0855eb52 | 3415 | init_xpm_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
3416 | { |
3417 | HMODULE library; | |
3418 | ||
0855eb52 | 3419 | if (!(library = w32_delayed_load (libraries, Qxpm))) |
4ef0d4d0 KS |
3420 | return 0; |
3421 | ||
3422 | LOAD_IMGLIB_FN (library, XpmFreeAttributes); | |
3423 | LOAD_IMGLIB_FN (library, XpmCreateImageFromBuffer); | |
3424 | LOAD_IMGLIB_FN (library, XpmReadFileToImage); | |
3425 | LOAD_IMGLIB_FN (library, XImageFree); | |
3426 | return 1; | |
3427 | } | |
3428 | ||
3429 | #endif /* HAVE_NTGUI */ | |
3430 | ||
3431 | ||
3432 | /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list | |
3433 | for XPM images. Such a list must consist of conses whose car and | |
3434 | cdr are strings. */ | |
3435 | ||
3436 | static int | |
3437 | xpm_valid_color_symbols_p (color_symbols) | |
3438 | Lisp_Object color_symbols; | |
3439 | { | |
3440 | while (CONSP (color_symbols)) | |
3441 | { | |
3442 | Lisp_Object sym = XCAR (color_symbols); | |
3443 | if (!CONSP (sym) | |
3444 | || !STRINGP (XCAR (sym)) | |
3445 | || !STRINGP (XCDR (sym))) | |
3446 | break; | |
3447 | color_symbols = XCDR (color_symbols); | |
3448 | } | |
3449 | ||
3450 | return NILP (color_symbols); | |
3451 | } | |
3452 | ||
3453 | ||
3454 | /* Value is non-zero if OBJECT is a valid XPM image specification. */ | |
3455 | ||
3456 | static int | |
3457 | xpm_image_p (object) | |
3458 | Lisp_Object object; | |
3459 | { | |
3460 | struct image_keyword fmt[XPM_LAST]; | |
3461 | bcopy (xpm_format, fmt, sizeof fmt); | |
3462 | return (parse_image_spec (object, fmt, XPM_LAST, Qxpm) | |
3463 | /* Either `:file' or `:data' must be present. */ | |
3464 | && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1 | |
3465 | /* Either no `:color-symbols' or it's a list of conses | |
3466 | whose car and cdr are strings. */ | |
3467 | && (fmt[XPM_COLOR_SYMBOLS].count == 0 | |
3468 | || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))); | |
3469 | } | |
3470 | ||
9e2a2647 | 3471 | #endif /* HAVE_XPM || HAVE_NS */ |
4ef0d4d0 | 3472 | |
786a43d6 CY |
3473 | #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) |
3474 | int | |
3475 | x_create_bitmap_from_xpm_data (f, bits) | |
3476 | struct frame *f; | |
3477 | char **bits; | |
3478 | { | |
3479 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
3480 | int id, rc; | |
3481 | XpmAttributes attrs; | |
3482 | Pixmap bitmap, mask; | |
3483 | ||
3484 | bzero (&attrs, sizeof attrs); | |
3485 | ||
54188d8f CY |
3486 | attrs.visual = FRAME_X_VISUAL (f); |
3487 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3488 | attrs.valuemask |= XpmVisual; | |
3489 | attrs.valuemask |= XpmColormap; | |
3490 | ||
786a43d6 CY |
3491 | rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
3492 | bits, &bitmap, &mask, &attrs); | |
3493 | if (rc != XpmSuccess) | |
fe45ad15 CY |
3494 | { |
3495 | XpmFreeAttributes (&attrs); | |
3496 | return -1; | |
3497 | } | |
786a43d6 CY |
3498 | |
3499 | id = x_allocate_bitmap_record (f); | |
786a43d6 CY |
3500 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; |
3501 | dpyinfo->bitmaps[id - 1].have_mask = 1; | |
3502 | dpyinfo->bitmaps[id - 1].mask = mask; | |
3503 | dpyinfo->bitmaps[id - 1].file = NULL; | |
3504 | dpyinfo->bitmaps[id - 1].height = attrs.height; | |
3505 | dpyinfo->bitmaps[id - 1].width = attrs.width; | |
3506 | dpyinfo->bitmaps[id - 1].depth = attrs.depth; | |
3507 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
3508 | ||
3509 | XpmFreeAttributes (&attrs); | |
3510 | return id; | |
3511 | } | |
0d876a14 | 3512 | #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */ |
786a43d6 | 3513 | |
4ef0d4d0 KS |
3514 | /* Load image IMG which will be displayed on frame F. Value is |
3515 | non-zero if successful. */ | |
3516 | ||
ea1aaa6f ST |
3517 | #ifdef HAVE_XPM |
3518 | ||
4ef0d4d0 KS |
3519 | static int |
3520 | xpm_load (f, img) | |
3521 | struct frame *f; | |
3522 | struct image *img; | |
3523 | { | |
3524 | int rc; | |
3525 | XpmAttributes attrs; | |
3526 | Lisp_Object specified_file, color_symbols; | |
3527 | #ifdef HAVE_NTGUI | |
3528 | HDC hdc; | |
3529 | xpm_XImage * xpm_image = NULL, * xpm_mask = NULL; | |
3530 | #endif /* HAVE_NTGUI */ | |
3531 | ||
3532 | /* Configure the XPM lib. Use the visual of frame F. Allocate | |
3533 | close colors. Return colors allocated. */ | |
3534 | bzero (&attrs, sizeof attrs); | |
3535 | ||
3536 | #ifndef HAVE_NTGUI | |
3537 | attrs.visual = FRAME_X_VISUAL (f); | |
3538 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3539 | attrs.valuemask |= XpmVisual; | |
3540 | attrs.valuemask |= XpmColormap; | |
3541 | #endif /* HAVE_NTGUI */ | |
3542 | ||
3543 | #ifdef ALLOC_XPM_COLORS | |
3544 | /* Allocate colors with our own functions which handle | |
3545 | failing color allocation more gracefully. */ | |
3546 | attrs.color_closure = f; | |
3547 | attrs.alloc_color = xpm_alloc_color; | |
3548 | attrs.free_colors = xpm_free_colors; | |
3549 | attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure; | |
3550 | #else /* not ALLOC_XPM_COLORS */ | |
3551 | /* Let the XPM lib allocate colors. */ | |
3552 | attrs.valuemask |= XpmReturnAllocPixels; | |
3553 | #ifdef XpmAllocCloseColors | |
3554 | attrs.alloc_close_colors = 1; | |
3555 | attrs.valuemask |= XpmAllocCloseColors; | |
3556 | #else /* not XpmAllocCloseColors */ | |
3557 | attrs.closeness = 600; | |
3558 | attrs.valuemask |= XpmCloseness; | |
3559 | #endif /* not XpmAllocCloseColors */ | |
3560 | #endif /* ALLOC_XPM_COLORS */ | |
3561 | ||
3562 | /* If image specification contains symbolic color definitions, add | |
3563 | these to `attrs'. */ | |
3564 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
3565 | if (CONSP (color_symbols)) | |
3566 | { | |
3567 | Lisp_Object tail; | |
3568 | XpmColorSymbol *xpm_syms; | |
3569 | int i, size; | |
3570 | ||
3571 | attrs.valuemask |= XpmColorSymbols; | |
3572 | ||
3573 | /* Count number of symbols. */ | |
3574 | attrs.numsymbols = 0; | |
3575 | for (tail = color_symbols; CONSP (tail); tail = XCDR (tail)) | |
3576 | ++attrs.numsymbols; | |
3577 | ||
3578 | /* Allocate an XpmColorSymbol array. */ | |
3579 | size = attrs.numsymbols * sizeof *xpm_syms; | |
3580 | xpm_syms = (XpmColorSymbol *) alloca (size); | |
3581 | bzero (xpm_syms, size); | |
3582 | attrs.colorsymbols = xpm_syms; | |
3583 | ||
3584 | /* Fill the color symbol array. */ | |
3585 | for (tail = color_symbols, i = 0; | |
3586 | CONSP (tail); | |
3587 | ++i, tail = XCDR (tail)) | |
3588 | { | |
3589 | Lisp_Object name = XCAR (XCAR (tail)); | |
3590 | Lisp_Object color = XCDR (XCAR (tail)); | |
3591 | xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1); | |
3592 | strcpy (xpm_syms[i].name, SDATA (name)); | |
3593 | xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1); | |
3594 | strcpy (xpm_syms[i].value, SDATA (color)); | |
3595 | } | |
3596 | } | |
3597 | ||
3598 | /* Create a pixmap for the image, either from a file, or from a | |
3599 | string buffer containing data in the same format as an XPM file. */ | |
3600 | #ifdef ALLOC_XPM_COLORS | |
3601 | xpm_init_color_cache (f, &attrs); | |
3602 | #endif | |
3603 | ||
3604 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
3605 | ||
3606 | #ifdef HAVE_NTGUI | |
3607 | { | |
3608 | HDC frame_dc = get_frame_dc (f); | |
3609 | hdc = CreateCompatibleDC (frame_dc); | |
3610 | release_frame_dc (f, frame_dc); | |
3611 | } | |
3612 | #endif /* HAVE_NTGUI */ | |
3613 | ||
3614 | if (STRINGP (specified_file)) | |
3615 | { | |
3616 | Lisp_Object file = x_find_image_file (specified_file); | |
3617 | if (!STRINGP (file)) | |
3618 | { | |
3619 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
3620 | return 0; | |
3621 | } | |
3622 | ||
3623 | #ifdef HAVE_NTGUI | |
3624 | /* XpmReadFileToPixmap is not available in the Windows port of | |
3625 | libxpm. But XpmReadFileToImage almost does what we want. */ | |
3626 | rc = fn_XpmReadFileToImage (&hdc, SDATA (file), | |
3627 | &xpm_image, &xpm_mask, | |
3628 | &attrs); | |
3629 | #else | |
3630 | rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3631 | SDATA (file), &img->pixmap, &img->mask, | |
3632 | &attrs); | |
3633 | #endif /* HAVE_NTGUI */ | |
3634 | } | |
3635 | else | |
3636 | { | |
3637 | Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL); | |
3638 | #ifdef HAVE_NTGUI | |
3639 | /* XpmCreatePixmapFromBuffer is not available in the Windows port | |
3640 | of libxpm. But XpmCreateImageFromBuffer almost does what we want. */ | |
3641 | rc = fn_XpmCreateImageFromBuffer (&hdc, SDATA (buffer), | |
3642 | &xpm_image, &xpm_mask, | |
3643 | &attrs); | |
3644 | #else | |
3645 | rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3646 | SDATA (buffer), | |
3647 | &img->pixmap, &img->mask, | |
3648 | &attrs); | |
3649 | #endif /* HAVE_NTGUI */ | |
3650 | } | |
3651 | ||
3652 | if (rc == XpmSuccess) | |
3653 | { | |
3654 | #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS) | |
3655 | img->colors = colors_in_color_table (&img->ncolors); | |
3656 | #else /* not ALLOC_XPM_COLORS */ | |
3657 | int i; | |
3658 | ||
3659 | #ifdef HAVE_NTGUI | |
3660 | /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap, | |
3661 | plus some duplicate attributes. */ | |
3662 | if (xpm_image && xpm_image->bitmap) | |
3663 | { | |
3664 | img->pixmap = xpm_image->bitmap; | |
3665 | /* XImageFree in libXpm frees XImage struct without destroying | |
3666 | the bitmap, which is what we want. */ | |
3667 | fn_XImageFree (xpm_image); | |
3668 | } | |
3669 | if (xpm_mask && xpm_mask->bitmap) | |
3670 | { | |
3671 | /* The mask appears to be inverted compared with what we expect. | |
3672 | TODO: invert our expectations. See other places where we | |
3673 | have to invert bits because our idea of masks is backwards. */ | |
3674 | HGDIOBJ old_obj; | |
3675 | old_obj = SelectObject (hdc, xpm_mask->bitmap); | |
3676 | ||
3677 | PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT); | |
3678 | SelectObject (hdc, old_obj); | |
3679 | ||
3680 | img->mask = xpm_mask->bitmap; | |
3681 | fn_XImageFree (xpm_mask); | |
3682 | DeleteDC (hdc); | |
3683 | } | |
3684 | ||
3685 | DeleteDC (hdc); | |
3686 | #endif /* HAVE_NTGUI */ | |
3687 | ||
3688 | /* Remember allocated colors. */ | |
3689 | img->ncolors = attrs.nalloc_pixels; | |
3690 | img->colors = (unsigned long *) xmalloc (img->ncolors | |
3691 | * sizeof *img->colors); | |
3692 | for (i = 0; i < attrs.nalloc_pixels; ++i) | |
3693 | { | |
3694 | img->colors[i] = attrs.alloc_pixels[i]; | |
3695 | #ifdef DEBUG_X_COLORS | |
3696 | register_color (img->colors[i]); | |
3697 | #endif | |
3698 | } | |
3699 | #endif /* not ALLOC_XPM_COLORS */ | |
3700 | ||
3701 | img->width = attrs.width; | |
3702 | img->height = attrs.height; | |
3703 | xassert (img->width > 0 && img->height > 0); | |
3704 | ||
3705 | /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */ | |
3706 | #ifdef HAVE_NTGUI | |
3707 | fn_XpmFreeAttributes (&attrs); | |
3708 | #else | |
3709 | XpmFreeAttributes (&attrs); | |
3710 | #endif /* HAVE_NTGUI */ | |
3711 | } | |
3712 | else | |
3713 | { | |
3714 | #ifdef HAVE_NTGUI | |
3715 | DeleteDC (hdc); | |
3716 | #endif /* HAVE_NTGUI */ | |
3717 | ||
3718 | switch (rc) | |
3719 | { | |
3720 | case XpmOpenFailed: | |
3721 | image_error ("Error opening XPM file (%s)", img->spec, Qnil); | |
3722 | break; | |
3723 | ||
3724 | case XpmFileInvalid: | |
3725 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
3726 | break; | |
3727 | ||
3728 | case XpmNoMemory: | |
3729 | image_error ("Out of memory (%s)", img->spec, Qnil); | |
3730 | break; | |
3731 | ||
3732 | case XpmColorFailed: | |
3733 | image_error ("Color allocation error (%s)", img->spec, Qnil); | |
3734 | break; | |
3735 | ||
3736 | default: | |
3737 | image_error ("Unknown error (%s)", img->spec, Qnil); | |
3738 | break; | |
3739 | } | |
3740 | } | |
3741 | ||
3742 | #ifdef ALLOC_XPM_COLORS | |
3743 | xpm_free_color_cache (); | |
3744 | #endif | |
3745 | return rc == XpmSuccess; | |
3746 | } | |
3747 | ||
3748 | #endif /* HAVE_XPM */ | |
3749 | ||
9e2a2647 | 3750 | #if defined (HAVE_NS) && !defined (HAVE_XPM) |
ea1aaa6f | 3751 | |
9e2a2647 | 3752 | /* XPM support functions for NS where libxpm is not available. |
ea1aaa6f ST |
3753 | Only XPM version 3 (without any extensions) is supported. */ |
3754 | ||
46accb8f YM |
3755 | static int xpm_scan P_ ((const unsigned char **, const unsigned char *, |
3756 | const unsigned char **, int *)); | |
ea1aaa6f | 3757 | static Lisp_Object xpm_make_color_table_v |
46accb8f YM |
3758 | P_ ((void (**) (Lisp_Object, const unsigned char *, int, Lisp_Object), |
3759 | Lisp_Object (**) (Lisp_Object, const unsigned char *, int))); | |
3760 | static void xpm_put_color_table_v P_ ((Lisp_Object, const unsigned char *, | |
9ecf6403 | 3761 | int, Lisp_Object)); |
ea1aaa6f | 3762 | static Lisp_Object xpm_get_color_table_v P_ ((Lisp_Object, |
46accb8f | 3763 | const unsigned char *, int)); |
ea1aaa6f | 3764 | static Lisp_Object xpm_make_color_table_h |
46accb8f YM |
3765 | P_ ((void (**) (Lisp_Object, const unsigned char *, int, Lisp_Object), |
3766 | Lisp_Object (**) (Lisp_Object, const unsigned char *, int))); | |
3767 | static void xpm_put_color_table_h P_ ((Lisp_Object, const unsigned char *, | |
9ecf6403 | 3768 | int, Lisp_Object)); |
ea1aaa6f | 3769 | static Lisp_Object xpm_get_color_table_h P_ ((Lisp_Object, |
46accb8f YM |
3770 | const unsigned char *, int)); |
3771 | static int xpm_str_to_color_key P_ ((const char *)); | |
ea1aaa6f | 3772 | static int xpm_load_image P_ ((struct frame *, struct image *, |
46accb8f | 3773 | const unsigned char *, const unsigned char *)); |
ea1aaa6f ST |
3774 | |
3775 | /* Tokens returned from xpm_scan. */ | |
3776 | ||
3777 | enum xpm_token | |
3778 | { | |
3779 | XPM_TK_IDENT = 256, | |
3780 | XPM_TK_STRING, | |
3781 | XPM_TK_EOF | |
3782 | }; | |
3783 | ||
3784 | /* Scan an XPM data and return a character (< 256) or a token defined | |
3785 | by enum xpm_token above. *S and END are the start (inclusive) and | |
3786 | the end (exclusive) addresses of the data, respectively. Advance | |
3787 | *S while scanning. If token is either XPM_TK_IDENT or | |
3788 | XPM_TK_STRING, *BEG and *LEN are set to the start address and the | |
3789 | length of the corresponding token, respectively. */ | |
3790 | ||
3791 | static int | |
3792 | xpm_scan (s, end, beg, len) | |
46accb8f | 3793 | const unsigned char **s, *end, **beg; |
ea1aaa6f ST |
3794 | int *len; |
3795 | { | |
3796 | int c; | |
3797 | ||
3798 | while (*s < end) | |
3799 | { | |
3800 | /* Skip white-space. */ | |
3801 | while (*s < end && (c = *(*s)++, isspace (c))) | |
9ecf6403 | 3802 | ; |
ea1aaa6f ST |
3803 | |
3804 | /* gnus-pointer.xpm uses '-' in its identifier. | |
9ecf6403 | 3805 | sb-dir-plus.xpm uses '+' in its identifier. */ |
ea1aaa6f | 3806 | if (isalpha (c) || c == '_' || c == '-' || c == '+') |
9ecf6403 YM |
3807 | { |
3808 | *beg = *s - 1; | |
2ff0845e YM |
3809 | while (*s < end |
3810 | && (c = **s, isalnum (c) || c == '_' || c == '-' || c == '+')) | |
9ecf6403 YM |
3811 | ++*s; |
3812 | *len = *s - *beg; | |
3813 | return XPM_TK_IDENT; | |
3814 | } | |
ea1aaa6f | 3815 | else if (c == '"') |
9ecf6403 YM |
3816 | { |
3817 | *beg = *s; | |
3818 | while (*s < end && **s != '"') | |
3819 | ++*s; | |
3820 | *len = *s - *beg; | |
3821 | if (*s < end) | |
3822 | ++*s; | |
3823 | return XPM_TK_STRING; | |
3824 | } | |
ea1aaa6f | 3825 | else if (c == '/') |
9ecf6403 YM |
3826 | { |
3827 | if (*s < end && **s == '*') | |
3828 | { | |
3829 | /* C-style comment. */ | |
3830 | ++*s; | |
3831 | do | |
3832 | { | |
3833 | while (*s < end && *(*s)++ != '*') | |
3834 | ; | |
3835 | } | |
3836 | while (*s < end && **s != '/'); | |
3837 | if (*s < end) | |
3838 | ++*s; | |
3839 | } | |
3840 | else | |
3841 | return c; | |
3842 | } | |
ea1aaa6f | 3843 | else |
9ecf6403 | 3844 | return c; |
ea1aaa6f ST |
3845 | } |
3846 | ||
3847 | return XPM_TK_EOF; | |
3848 | } | |
3849 | ||
bbe6f2aa | 3850 | /* Functions for color table lookup in XPM data. A key is a string |
ea1aaa6f ST |
3851 | specifying the color of each pixel in XPM data. A value is either |
3852 | an integer that specifies a pixel color, Qt that specifies | |
3853 | transparency, or Qnil for the unspecified color. If the length of | |
3854 | the key string is one, a vector is used as a table. Otherwise, a | |
3855 | hash table is used. */ | |
3856 | ||
3857 | static Lisp_Object | |
3858 | xpm_make_color_table_v (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_v; | |
3863 | *get_func = xpm_get_color_table_v; | |
3864 | return Fmake_vector (make_number (256), Qnil); | |
3865 | } | |
3866 | ||
3867 | static void | |
3868 | xpm_put_color_table_v (color_table, chars_start, chars_len, color) | |
3869 | Lisp_Object color_table; | |
46accb8f | 3870 | const unsigned char *chars_start; |
ea1aaa6f ST |
3871 | int chars_len; |
3872 | Lisp_Object color; | |
3873 | { | |
3874 | XVECTOR (color_table)->contents[*chars_start] = color; | |
3875 | } | |
3876 | ||
3877 | static Lisp_Object | |
3878 | xpm_get_color_table_v (color_table, chars_start, chars_len) | |
3879 | Lisp_Object color_table; | |
46accb8f | 3880 | const unsigned char *chars_start; |
ea1aaa6f ST |
3881 | int chars_len; |
3882 | { | |
3883 | return XVECTOR (color_table)->contents[*chars_start]; | |
3884 | } | |
3885 | ||
3886 | static Lisp_Object | |
3887 | xpm_make_color_table_h (put_func, get_func) | |
46accb8f YM |
3888 | void (**put_func) (Lisp_Object, const unsigned char *, int, Lisp_Object); |
3889 | Lisp_Object (**get_func) (Lisp_Object, const unsigned char *, int); | |
ea1aaa6f ST |
3890 | { |
3891 | *put_func = xpm_put_color_table_h; | |
3892 | *get_func = xpm_get_color_table_h; | |
3893 | return make_hash_table (Qequal, make_number (DEFAULT_HASH_SIZE), | |
9ecf6403 YM |
3894 | make_float (DEFAULT_REHASH_SIZE), |
3895 | make_float (DEFAULT_REHASH_THRESHOLD), | |
3896 | Qnil, Qnil, Qnil); | |
ea1aaa6f ST |
3897 | } |
3898 | ||
3899 | static void | |
3900 | xpm_put_color_table_h (color_table, chars_start, chars_len, color) | |
3901 | Lisp_Object color_table; | |
46accb8f | 3902 | const unsigned char *chars_start; |
ea1aaa6f ST |
3903 | int chars_len; |
3904 | Lisp_Object color; | |
3905 | { | |
3906 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
3907 | unsigned hash_code; | |
3908 | Lisp_Object chars = make_unibyte_string (chars_start, chars_len); | |
3909 | ||
3910 | hash_lookup (table, chars, &hash_code); | |
3911 | hash_put (table, chars, color, hash_code); | |
3912 | } | |
3913 | ||
3914 | static Lisp_Object | |
3915 | xpm_get_color_table_h (color_table, chars_start, chars_len) | |
3916 | Lisp_Object color_table; | |
46accb8f | 3917 | const unsigned char *chars_start; |
ea1aaa6f ST |
3918 | int chars_len; |
3919 | { | |
3920 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
3921 | int i = hash_lookup (table, make_unibyte_string (chars_start, chars_len), | |
9ecf6403 | 3922 | NULL); |
ea1aaa6f ST |
3923 | |
3924 | return i >= 0 ? HASH_VALUE (table, i) : Qnil; | |
3925 | } | |
3926 | ||
3927 | enum xpm_color_key { | |
3928 | XPM_COLOR_KEY_S, | |
3929 | XPM_COLOR_KEY_M, | |
3930 | XPM_COLOR_KEY_G4, | |
3931 | XPM_COLOR_KEY_G, | |
3932 | XPM_COLOR_KEY_C | |
3933 | }; | |
3934 | ||
46accb8f | 3935 | static const char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"}; |
ea1aaa6f ST |
3936 | |
3937 | static int | |
3938 | xpm_str_to_color_key (s) | |
46accb8f | 3939 | const char *s; |
ea1aaa6f ST |
3940 | { |
3941 | int i; | |
3942 | ||
3943 | for (i = 0; | |
3944 | i < sizeof xpm_color_key_strings / sizeof xpm_color_key_strings[0]; | |
3945 | i++) | |
3946 | if (strcmp (xpm_color_key_strings[i], s) == 0) | |
3947 | return i; | |
3948 | return -1; | |
3949 | } | |
3950 | ||
3951 | static int | |
3952 | xpm_load_image (f, img, contents, end) | |
3953 | struct frame *f; | |
3954 | struct image *img; | |
46accb8f | 3955 | const unsigned char *contents, *end; |
ea1aaa6f | 3956 | { |
46accb8f | 3957 | const unsigned char *s = contents, *beg, *str; |
ea1aaa6f ST |
3958 | unsigned char buffer[BUFSIZ]; |
3959 | int width, height, x, y; | |
3960 | int num_colors, chars_per_pixel; | |
3961 | int len, LA1; | |
46accb8f YM |
3962 | void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object); |
3963 | Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int); | |
ea1aaa6f ST |
3964 | Lisp_Object frame, color_symbols, color_table; |
3965 | int best_key, have_mask = 0; | |
3966 | XImagePtr ximg = NULL, mask_img = NULL; | |
3967 | ||
3968 | #define match() \ | |
3969 | LA1 = xpm_scan (&s, end, &beg, &len) | |
3970 | ||
9ecf6403 YM |
3971 | #define expect(TOKEN) \ |
3972 | if (LA1 != (TOKEN)) \ | |
3973 | goto failure; \ | |
3974 | else \ | |
ea1aaa6f ST |
3975 | match () |
3976 | ||
9ecf6403 | 3977 | #define expect_ident(IDENT) \ |
ea1aaa6f | 3978 | if (LA1 == XPM_TK_IDENT \ |
9ecf6403 YM |
3979 | && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \ |
3980 | match (); \ | |
3981 | else \ | |
ea1aaa6f ST |
3982 | goto failure |
3983 | ||
3984 | if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0)) | |
3985 | goto failure; | |
3986 | s += 9; | |
3987 | match(); | |
3988 | expect_ident ("static"); | |
3989 | expect_ident ("char"); | |
3990 | expect ('*'); | |
3991 | expect (XPM_TK_IDENT); | |
3992 | expect ('['); | |
3993 | expect (']'); | |
3994 | expect ('='); | |
3995 | expect ('{'); | |
3996 | expect (XPM_TK_STRING); | |
3997 | if (len >= BUFSIZ) | |
3998 | goto failure; | |
3999 | memcpy (buffer, beg, len); | |
4000 | buffer[len] = '\0'; | |
4001 | if (sscanf (buffer, "%d %d %d %d", &width, &height, | |
9ecf6403 | 4002 | &num_colors, &chars_per_pixel) != 4 |
ea1aaa6f ST |
4003 | || width <= 0 || height <= 0 |
4004 | || num_colors <= 0 || chars_per_pixel <= 0) | |
4005 | goto failure; | |
de297941 YM |
4006 | |
4007 | if (!check_image_size (f, width, height)) | |
4008 | { | |
10ea2b82 | 4009 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
de297941 YM |
4010 | goto failure; |
4011 | } | |
4012 | ||
ea1aaa6f ST |
4013 | expect (','); |
4014 | ||
4015 | XSETFRAME (frame, f); | |
4016 | if (!NILP (Fxw_display_color_p (frame))) | |
4017 | best_key = XPM_COLOR_KEY_C; | |
4018 | else if (!NILP (Fx_display_grayscale_p (frame))) | |
4019 | best_key = (XFASTINT (Fx_display_planes (frame)) > 2 | |
9ecf6403 | 4020 | ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4); |
ea1aaa6f ST |
4021 | else |
4022 | best_key = XPM_COLOR_KEY_M; | |
4023 | ||
4024 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
4025 | if (chars_per_pixel == 1) | |
4026 | color_table = xpm_make_color_table_v (&put_color_table, | |
9ecf6403 | 4027 | &get_color_table); |
ea1aaa6f ST |
4028 | else |
4029 | color_table = xpm_make_color_table_h (&put_color_table, | |
9ecf6403 | 4030 | &get_color_table); |
ea1aaa6f ST |
4031 | |
4032 | while (num_colors-- > 0) | |
4033 | { | |
4034 | unsigned char *color, *max_color; | |
4035 | int key, next_key, max_key = 0; | |
4036 | Lisp_Object symbol_color = Qnil, color_val; | |
4037 | XColor cdef; | |
4038 | ||
4039 | expect (XPM_TK_STRING); | |
4040 | if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel) | |
9ecf6403 | 4041 | goto failure; |
ea1aaa6f ST |
4042 | memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel); |
4043 | buffer[len - chars_per_pixel] = '\0'; | |
4044 | ||
4045 | str = strtok (buffer, " \t"); | |
4046 | if (str == NULL) | |
9ecf6403 | 4047 | goto failure; |
ea1aaa6f ST |
4048 | key = xpm_str_to_color_key (str); |
4049 | if (key < 0) | |
9ecf6403 | 4050 | goto failure; |
ea1aaa6f | 4051 | do |
9ecf6403 YM |
4052 | { |
4053 | color = strtok (NULL, " \t"); | |
4054 | if (color == NULL) | |
4055 | goto failure; | |
ea1aaa6f | 4056 | |
9db94f82 | 4057 | while ((str = strtok (NULL, " \t")) != NULL) |
9ecf6403 YM |
4058 | { |
4059 | next_key = xpm_str_to_color_key (str); | |
4060 | if (next_key >= 0) | |
4061 | break; | |
4062 | color[strlen (color)] = ' '; | |
4063 | } | |
ea1aaa6f | 4064 | |
9ecf6403 YM |
4065 | if (key == XPM_COLOR_KEY_S) |
4066 | { | |
4067 | if (NILP (symbol_color)) | |
4068 | symbol_color = build_string (color); | |
4069 | } | |
4070 | else if (max_key < key && key <= best_key) | |
4071 | { | |
4072 | max_key = key; | |
4073 | max_color = color; | |
4074 | } | |
4075 | key = next_key; | |
4076 | } | |
ea1aaa6f ST |
4077 | while (str); |
4078 | ||
4079 | color_val = Qnil; | |
4080 | if (!NILP (color_symbols) && !NILP (symbol_color)) | |
9ecf6403 YM |
4081 | { |
4082 | Lisp_Object specified_color = Fassoc (symbol_color, color_symbols); | |
4083 | ||
4084 | if (CONSP (specified_color) && STRINGP (XCDR (specified_color))) | |
9db94f82 | 4085 | { |
05131107 | 4086 | if (xstrcasecmp (SDATA (XCDR (specified_color)), "None") == 0) |
9db94f82 YM |
4087 | color_val = Qt; |
4088 | else if (x_defined_color (f, SDATA (XCDR (specified_color)), | |
4089 | &cdef, 0)) | |
4090 | color_val = make_number (cdef.pixel); | |
4091 | } | |
9ecf6403 | 4092 | } |
ea1aaa6f | 4093 | if (NILP (color_val) && max_key > 0) |
9db94f82 | 4094 | { |
05131107 | 4095 | if (xstrcasecmp (max_color, "None") == 0) |
9db94f82 YM |
4096 | color_val = Qt; |
4097 | else if (x_defined_color (f, max_color, &cdef, 0)) | |
4098 | color_val = make_number (cdef.pixel); | |
4099 | } | |
ea1aaa6f | 4100 | if (!NILP (color_val)) |
9ecf6403 | 4101 | (*put_color_table) (color_table, beg, chars_per_pixel, color_val); |
ea1aaa6f ST |
4102 | |
4103 | expect (','); | |
4104 | } | |
4105 | ||
4106 | if (!x_create_x_image_and_pixmap (f, width, height, 0, | |
9ecf6403 | 4107 | &ximg, &img->pixmap) |
edfda783 | 4108 | #ifndef HAVE_NS |
ea1aaa6f | 4109 | || !x_create_x_image_and_pixmap (f, width, height, 1, |
edfda783 AR |
4110 | &mask_img, &img->mask) |
4111 | #endif | |
4112 | ) | |
ea1aaa6f ST |
4113 | { |
4114 | image_error ("Out of memory (%s)", img->spec, Qnil); | |
4115 | goto error; | |
4116 | } | |
4117 | ||
4118 | for (y = 0; y < height; y++) | |
4119 | { | |
4120 | expect (XPM_TK_STRING); | |
4121 | str = beg; | |
4122 | if (len < width * chars_per_pixel) | |
9ecf6403 | 4123 | goto failure; |
ea1aaa6f | 4124 | for (x = 0; x < width; x++, str += chars_per_pixel) |
9ecf6403 YM |
4125 | { |
4126 | Lisp_Object color_val = | |
4127 | (*get_color_table) (color_table, str, chars_per_pixel); | |
4128 | ||
4129 | XPutPixel (ximg, x, y, | |
4130 | (INTEGERP (color_val) ? XINT (color_val) | |
4131 | : FRAME_FOREGROUND_PIXEL (f))); | |
edfda783 | 4132 | #ifndef HAVE_NS |
9ecf6403 YM |
4133 | XPutPixel (mask_img, x, y, |
4134 | (!EQ (color_val, Qt) ? PIX_MASK_DRAW | |
4135 | : (have_mask = 1, PIX_MASK_RETAIN))); | |
edfda783 AR |
4136 | #else |
4137 | if (EQ(color_val, Qt)) | |
4138 | ns_set_alpha(ximg, x, y, 0); | |
4139 | #endif | |
9ecf6403 | 4140 | } |
ea1aaa6f | 4141 | if (y + 1 < height) |
9ecf6403 | 4142 | expect (','); |
ea1aaa6f ST |
4143 | } |
4144 | ||
4145 | img->width = width; | |
4146 | img->height = height; | |
4147 | ||
0b8ac842 YM |
4148 | /* Maybe fill in the background field while we have ximg handy. */ |
4149 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
4150 | IMAGE_BACKGROUND (img, f, ximg); | |
4151 | ||
ea1aaa6f ST |
4152 | x_put_x_image (f, ximg, img->pixmap, width, height); |
4153 | x_destroy_x_image (ximg); | |
edfda783 | 4154 | #ifndef HAVE_NS |
ea1aaa6f ST |
4155 | if (have_mask) |
4156 | { | |
9ecf6403 YM |
4157 | /* Fill in the background_transparent field while we have the |
4158 | mask handy. */ | |
4159 | image_background_transparent (img, f, mask_img); | |
4160 | ||
ea1aaa6f ST |
4161 | x_put_x_image (f, mask_img, img->mask, width, height); |
4162 | x_destroy_x_image (mask_img); | |
4163 | } | |
4164 | else | |
4165 | { | |
4166 | x_destroy_x_image (mask_img); | |
4167 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
4168 | img->mask = NO_PIXMAP; | |
4169 | } | |
edfda783 | 4170 | #endif |
ea1aaa6f ST |
4171 | return 1; |
4172 | ||
4173 | failure: | |
4174 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
4175 | error: | |
4176 | x_destroy_x_image (ximg); | |
4177 | x_destroy_x_image (mask_img); | |
4178 | x_clear_image (f, img); | |
4179 | return 0; | |
4180 | ||
4181 | #undef match | |
4182 | #undef expect | |
4183 | #undef expect_ident | |
4184 | } | |
4185 | ||
4186 | static int | |
4187 | xpm_load (f, img) | |
4188 | struct frame *f; | |
4189 | struct image *img; | |
4190 | { | |
4191 | int success_p = 0; | |
4192 | Lisp_Object file_name; | |
4193 | ||
4194 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
4195 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
4196 | if (STRINGP (file_name)) | |
4197 | { | |
4198 | Lisp_Object file; | |
4199 | unsigned char *contents; | |
4200 | int size; | |
4201 | struct gcpro gcpro1; | |
4202 | ||
4203 | file = x_find_image_file (file_name); | |
4204 | GCPRO1 (file); | |
4205 | if (!STRINGP (file)) | |
9ecf6403 YM |
4206 | { |
4207 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
4208 | UNGCPRO; | |
4209 | return 0; | |
4210 | } | |
ea1aaa6f ST |
4211 | |
4212 | contents = slurp_file (SDATA (file), &size); | |
4213 | if (contents == NULL) | |
9ecf6403 YM |
4214 | { |
4215 | image_error ("Error loading XPM image `%s'", img->spec, Qnil); | |
4216 | UNGCPRO; | |
4217 | return 0; | |
4218 | } | |
ea1aaa6f ST |
4219 | |
4220 | success_p = xpm_load_image (f, img, contents, contents + size); | |
4221 | xfree (contents); | |
4222 | UNGCPRO; | |
4223 | } | |
4224 | else | |
4225 | { | |
4226 | Lisp_Object data; | |
4227 | ||
4228 | data = image_spec_value (img->spec, QCdata, NULL); | |
4229 | success_p = xpm_load_image (f, img, SDATA (data), | |
9ecf6403 | 4230 | SDATA (data) + SBYTES (data)); |
ea1aaa6f ST |
4231 | } |
4232 | ||
4233 | return success_p; | |
4234 | } | |
4235 | ||
9e2a2647 | 4236 | #endif /* HAVE_NS && !HAVE_XPM */ |
b0da69a7 | 4237 | |
ea1aaa6f | 4238 | |
4ef0d4d0 KS |
4239 | \f |
4240 | /*********************************************************************** | |
4241 | Color table | |
4242 | ***********************************************************************/ | |
4243 | ||
4244 | #ifdef COLOR_TABLE_SUPPORT | |
4245 | ||
4246 | /* An entry in the color table mapping an RGB color to a pixel color. */ | |
4247 | ||
4248 | struct ct_color | |
4249 | { | |
4250 | int r, g, b; | |
4251 | unsigned long pixel; | |
4252 | ||
4253 | /* Next in color table collision list. */ | |
4254 | struct ct_color *next; | |
4255 | }; | |
4256 | ||
4257 | /* The bucket vector size to use. Must be prime. */ | |
4258 | ||
4259 | #define CT_SIZE 101 | |
4260 | ||
4261 | /* Value is a hash of the RGB color given by R, G, and B. */ | |
4262 | ||
4263 | #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B)) | |
4264 | ||
4265 | /* The color hash table. */ | |
4266 | ||
4267 | struct ct_color **ct_table; | |
4268 | ||
4269 | /* Number of entries in the color table. */ | |
4270 | ||
4271 | int ct_colors_allocated; | |
4272 | ||
4273 | /* Initialize the color table. */ | |
4274 | ||
4275 | static void | |
4276 | init_color_table () | |
4277 | { | |
4278 | int size = CT_SIZE * sizeof (*ct_table); | |
4279 | ct_table = (struct ct_color **) xmalloc (size); | |
4280 | bzero (ct_table, size); | |
4281 | ct_colors_allocated = 0; | |
4282 | } | |
4283 | ||
4284 | ||
4285 | /* Free memory associated with the color table. */ | |
4286 | ||
4287 | static void | |
4288 | free_color_table () | |
4289 | { | |
4290 | int i; | |
4291 | struct ct_color *p, *next; | |
4292 | ||
4293 | for (i = 0; i < CT_SIZE; ++i) | |
4294 | for (p = ct_table[i]; p; p = next) | |
4295 | { | |
4296 | next = p->next; | |
4297 | xfree (p); | |
4298 | } | |
4299 | ||
4300 | xfree (ct_table); | |
4301 | ct_table = NULL; | |
4302 | } | |
4303 | ||
4304 | ||
4305 | /* Value is a pixel color for RGB color R, G, B on frame F. If an | |
4306 | entry for that color already is in the color table, return the | |
4307 | pixel color of that entry. Otherwise, allocate a new color for R, | |
4308 | G, B, and make an entry in the color table. */ | |
4309 | ||
4310 | static unsigned long | |
4311 | lookup_rgb_color (f, r, g, b) | |
4312 | struct frame *f; | |
4313 | int r, g, b; | |
4314 | { | |
4315 | unsigned hash = CT_HASH_RGB (r, g, b); | |
4316 | int i = hash % CT_SIZE; | |
4317 | struct ct_color *p; | |
4318 | Display_Info *dpyinfo; | |
4319 | ||
4320 | /* Handle TrueColor visuals specially, which improves performance by | |
4321 | two orders of magnitude. Freeing colors on TrueColor visuals is | |
4322 | a nop, and pixel colors specify RGB values directly. See also | |
4323 | the Xlib spec, chapter 3.1. */ | |
4324 | dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
4325 | if (dpyinfo->red_bits > 0) | |
4326 | { | |
4327 | unsigned long pr, pg, pb; | |
4328 | ||
4329 | /* Apply gamma-correction like normal color allocation does. */ | |
4330 | if (f->gamma) | |
4331 | { | |
4332 | XColor color; | |
4333 | color.red = r, color.green = g, color.blue = b; | |
4334 | gamma_correct (f, &color); | |
4335 | r = color.red, g = color.green, b = color.blue; | |
4336 | } | |
4337 | ||
4338 | /* Scale down RGB values to the visual's bits per RGB, and shift | |
4339 | them to the right position in the pixel color. Note that the | |
4340 | original RGB values are 16-bit values, as usual in X. */ | |
4341 | pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset; | |
4342 | pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset; | |
4343 | pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset; | |
4344 | ||
4345 | /* Assemble the pixel color. */ | |
4346 | return pr | pg | pb; | |
4347 | } | |
1f026899 | 4348 | |
4ef0d4d0 KS |
4349 | for (p = ct_table[i]; p; p = p->next) |
4350 | if (p->r == r && p->g == g && p->b == b) | |
4351 | break; | |
4352 | ||
4353 | if (p == NULL) | |
4354 | { | |
4355 | ||
4356 | #ifdef HAVE_X_WINDOWS | |
4357 | XColor color; | |
4358 | Colormap cmap; | |
4359 | int rc; | |
4360 | ||
4361 | color.red = r; | |
4362 | color.green = g; | |
4363 | color.blue = b; | |
4364 | ||
4365 | cmap = FRAME_X_COLORMAP (f); | |
4366 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4367 | if (rc) | |
4368 | { | |
4369 | ++ct_colors_allocated; | |
4370 | p = (struct ct_color *) xmalloc (sizeof *p); | |
4371 | p->r = r; | |
4372 | p->g = g; | |
4373 | p->b = b; | |
4374 | p->pixel = color.pixel; | |
4375 | p->next = ct_table[i]; | |
4376 | ct_table[i] = p; | |
4377 | } | |
4378 | else | |
4379 | return FRAME_FOREGROUND_PIXEL (f); | |
4380 | ||
4381 | #else | |
4382 | COLORREF color; | |
4383 | #ifdef HAVE_NTGUI | |
4384 | color = PALETTERGB (r, g, b); | |
4385 | #else | |
4386 | color = RGB_TO_ULONG (r, g, b); | |
4387 | #endif /* HAVE_NTGUI */ | |
4388 | ++ct_colors_allocated; | |
4389 | p = (struct ct_color *) xmalloc (sizeof *p); | |
4390 | p->r = r; | |
4391 | p->g = g; | |
4392 | p->b = b; | |
4393 | p->pixel = color; | |
4394 | p->next = ct_table[i]; | |
4395 | ct_table[i] = p; | |
4396 | #endif /* HAVE_X_WINDOWS */ | |
4397 | ||
4398 | } | |
4399 | ||
4400 | return p->pixel; | |
4401 | } | |
4402 | ||
4403 | ||
4404 | /* Look up pixel color PIXEL which is used on frame F in the color | |
4405 | table. If not already present, allocate it. Value is PIXEL. */ | |
4406 | ||
4407 | static unsigned long | |
4408 | lookup_pixel_color (f, pixel) | |
4409 | struct frame *f; | |
4410 | unsigned long pixel; | |
4411 | { | |
4412 | int i = pixel % CT_SIZE; | |
4413 | struct ct_color *p; | |
4414 | ||
4415 | for (p = ct_table[i]; p; p = p->next) | |
4416 | if (p->pixel == pixel) | |
4417 | break; | |
4418 | ||
4419 | if (p == NULL) | |
4420 | { | |
4421 | XColor color; | |
4422 | Colormap cmap; | |
4423 | int rc; | |
4424 | ||
4425 | #ifdef HAVE_X_WINDOWS | |
4426 | cmap = FRAME_X_COLORMAP (f); | |
4427 | color.pixel = pixel; | |
4428 | x_query_color (f, &color); | |
4429 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4430 | #else | |
4431 | BLOCK_INPUT; | |
4432 | cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f)); | |
4433 | color.pixel = pixel; | |
4434 | XQueryColor (NULL, cmap, &color); | |
4435 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4436 | UNBLOCK_INPUT; | |
4437 | #endif /* HAVE_X_WINDOWS */ | |
4438 | ||
4439 | if (rc) | |
4440 | { | |
4441 | ++ct_colors_allocated; | |
4442 | ||
4443 | p = (struct ct_color *) xmalloc (sizeof *p); | |
4444 | p->r = color.red; | |
4445 | p->g = color.green; | |
4446 | p->b = color.blue; | |
4447 | p->pixel = pixel; | |
4448 | p->next = ct_table[i]; | |
4449 | ct_table[i] = p; | |
4450 | } | |
4451 | else | |
4452 | return FRAME_FOREGROUND_PIXEL (f); | |
4453 | } | |
4454 | return p->pixel; | |
4455 | } | |
4456 | ||
4457 | ||
4458 | /* Value is a vector of all pixel colors contained in the color table, | |
4459 | allocated via xmalloc. Set *N to the number of colors. */ | |
4460 | ||
4461 | static unsigned long * | |
4462 | colors_in_color_table (n) | |
4463 | int *n; | |
4464 | { | |
4465 | int i, j; | |
4466 | struct ct_color *p; | |
4467 | unsigned long *colors; | |
4468 | ||
4469 | if (ct_colors_allocated == 0) | |
4470 | { | |
4471 | *n = 0; | |
4472 | colors = NULL; | |
4473 | } | |
4474 | else | |
4475 | { | |
4476 | colors = (unsigned long *) xmalloc (ct_colors_allocated | |
4477 | * sizeof *colors); | |
4478 | *n = ct_colors_allocated; | |
4479 | ||
4480 | for (i = j = 0; i < CT_SIZE; ++i) | |
4481 | for (p = ct_table[i]; p; p = p->next) | |
4482 | colors[j++] = p->pixel; | |
4483 | } | |
4484 | ||
4485 | return colors; | |
4486 | } | |
4487 | ||
4488 | #else /* COLOR_TABLE_SUPPORT */ | |
4489 | ||
4490 | static unsigned long | |
4491 | lookup_rgb_color (f, r, g, b) | |
4492 | struct frame *f; | |
4493 | int r, g, b; | |
4494 | { | |
4495 | unsigned long pixel; | |
4496 | ||
4ef0d4d0 KS |
4497 | #ifdef HAVE_NTGUI |
4498 | pixel = PALETTERGB (r >> 8, g >> 8, b >> 8); | |
4499 | #endif /* HAVE_NTGUI */ | |
4500 | ||
edfda783 AR |
4501 | #ifdef HAVE_NS |
4502 | pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8); | |
4503 | #endif /* HAVE_NS */ | |
4ef0d4d0 KS |
4504 | return pixel; |
4505 | } | |
4506 | ||
4507 | static void | |
4508 | init_color_table () | |
4509 | { | |
4510 | } | |
4511 | #endif /* COLOR_TABLE_SUPPORT */ | |
4512 | ||
4513 | \f | |
4514 | /*********************************************************************** | |
4515 | Algorithms | |
4516 | ***********************************************************************/ | |
4517 | ||
4518 | static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int)); | |
4519 | static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *)); | |
4520 | static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int)); | |
4521 | ||
4522 | #ifdef HAVE_NTGUI | |
4523 | static void XPutPixel (XImagePtr , int, int, COLORREF); | |
4524 | #endif /* HAVE_NTGUI */ | |
4525 | ||
4526 | /* Non-zero means draw a cross on images having `:conversion | |
4527 | disabled'. */ | |
4528 | ||
4529 | int cross_disabled_images; | |
4530 | ||
4531 | /* Edge detection matrices for different edge-detection | |
4532 | strategies. */ | |
4533 | ||
4534 | static int emboss_matrix[9] = { | |
4535 | /* x - 1 x x + 1 */ | |
4536 | 2, -1, 0, /* y - 1 */ | |
4537 | -1, 0, 1, /* y */ | |
4538 | 0, 1, -2 /* y + 1 */ | |
4539 | }; | |
4540 | ||
4541 | static int laplace_matrix[9] = { | |
4542 | /* x - 1 x x + 1 */ | |
4543 | 1, 0, 0, /* y - 1 */ | |
4544 | 0, 0, 0, /* y */ | |
4545 | 0, 0, -1 /* y + 1 */ | |
4546 | }; | |
4547 | ||
4548 | /* Value is the intensity of the color whose red/green/blue values | |
4549 | are R, G, and B. */ | |
4550 | ||
4551 | #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6) | |
4552 | ||
4553 | ||
4554 | /* On frame F, return an array of XColor structures describing image | |
4555 | IMG->pixmap. Each XColor structure has its pixel color set. RGB_P | |
4556 | non-zero means also fill the red/green/blue members of the XColor | |
4557 | structures. Value is a pointer to the array of XColors structures, | |
4558 | allocated with xmalloc; it must be freed by the caller. */ | |
4559 | ||
4560 | static XColor * | |
4561 | x_to_xcolors (f, img, rgb_p) | |
4562 | struct frame *f; | |
4563 | struct image *img; | |
4564 | int rgb_p; | |
4565 | { | |
4566 | int x, y; | |
4567 | XColor *colors, *p; | |
4568 | XImagePtr_or_DC ximg; | |
4569 | #ifdef HAVE_NTGUI | |
4570 | HDC hdc; | |
4571 | HGDIOBJ prev; | |
4572 | #endif /* HAVE_NTGUI */ | |
4573 | ||
4574 | colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors); | |
4575 | ||
4576 | #ifndef HAVE_NTGUI | |
4577 | /* Get the X image IMG->pixmap. */ | |
4578 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, | |
4579 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
4580 | #else | |
4581 | /* Load the image into a memory device context. */ | |
4582 | hdc = get_frame_dc (f); | |
4583 | ximg = CreateCompatibleDC (hdc); | |
4584 | release_frame_dc (f, hdc); | |
4585 | prev = SelectObject (ximg, img->pixmap); | |
4586 | #endif /* HAVE_NTGUI */ | |
4587 | ||
4588 | /* Fill the `pixel' members of the XColor array. I wished there | |
4589 | were an easy and portable way to circumvent XGetPixel. */ | |
4590 | p = colors; | |
4591 | for (y = 0; y < img->height; ++y) | |
4592 | { | |
4593 | XColor *row = p; | |
4594 | ||
8b7d0a16 | 4595 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
4ef0d4d0 | 4596 | for (x = 0; x < img->width; ++x, ++p) |
8b7d0a16 | 4597 | p->pixel = GET_PIXEL (ximg, x, y); |
4ef0d4d0 KS |
4598 | if (rgb_p) |
4599 | x_query_colors (f, row, img->width); | |
4600 | ||
4601 | #else | |
4602 | ||
4603 | for (x = 0; x < img->width; ++x, ++p) | |
4604 | { | |
4605 | /* W32_TODO: palette support needed here? */ | |
4606 | p->pixel = GET_PIXEL (ximg, x, y); | |
4607 | if (rgb_p) | |
4608 | { | |
4ef0d4d0 KS |
4609 | p->red = RED16_FROM_ULONG (p->pixel); |
4610 | p->green = GREEN16_FROM_ULONG (p->pixel); | |
4611 | p->blue = BLUE16_FROM_ULONG (p->pixel); | |
4ef0d4d0 KS |
4612 | } |
4613 | } | |
4614 | #endif /* HAVE_X_WINDOWS */ | |
4615 | } | |
4616 | ||
4617 | Destroy_Image (ximg, prev); | |
4618 | ||
4619 | return colors; | |
4620 | } | |
4621 | ||
4622 | #ifdef HAVE_NTGUI | |
4623 | ||
4624 | /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been | |
4625 | created with CreateDIBSection, with the pointer to the bit values | |
4626 | stored in ximg->data. */ | |
4627 | ||
3046a84b JB |
4628 | static void |
4629 | XPutPixel (ximg, x, y, color) | |
4ef0d4d0 KS |
4630 | XImagePtr ximg; |
4631 | int x, y; | |
4632 | COLORREF color; | |
4633 | { | |
4634 | int width = ximg->info.bmiHeader.biWidth; | |
4635 | int height = ximg->info.bmiHeader.biHeight; | |
4636 | unsigned char * pixel; | |
4637 | ||
4638 | /* True color images. */ | |
4639 | if (ximg->info.bmiHeader.biBitCount == 24) | |
4640 | { | |
4641 | int rowbytes = width * 3; | |
4642 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4643 | if (rowbytes % 4) | |
4644 | rowbytes += 4 - (rowbytes % 4); | |
4645 | ||
4646 | pixel = ximg->data + y * rowbytes + x * 3; | |
4647 | /* Windows bitmaps are in BGR order. */ | |
4648 | *pixel = GetBValue (color); | |
4649 | *(pixel + 1) = GetGValue (color); | |
4650 | *(pixel + 2) = GetRValue (color); | |
4651 | } | |
4652 | /* Monochrome images. */ | |
4653 | else if (ximg->info.bmiHeader.biBitCount == 1) | |
4654 | { | |
4655 | int rowbytes = width / 8; | |
4656 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4657 | if (rowbytes % 4) | |
4658 | rowbytes += 4 - (rowbytes % 4); | |
4659 | pixel = ximg->data + y * rowbytes + x / 8; | |
4660 | /* Filter out palette info. */ | |
4661 | if (color & 0x00ffffff) | |
4662 | *pixel = *pixel | (1 << x % 8); | |
4663 | else | |
4664 | *pixel = *pixel & ~(1 << x % 8); | |
4665 | } | |
4666 | else | |
4667 | image_error ("XPutPixel: palette image not supported", Qnil, Qnil); | |
4668 | } | |
4669 | ||
4670 | #endif /* HAVE_NTGUI */ | |
4671 | ||
4672 | /* Create IMG->pixmap from an array COLORS of XColor structures, whose | |
4673 | RGB members are set. F is the frame on which this all happens. | |
4674 | COLORS will be freed; an existing IMG->pixmap will be freed, too. */ | |
4675 | ||
4676 | static void | |
4677 | x_from_xcolors (f, img, colors) | |
4678 | struct frame *f; | |
4679 | struct image *img; | |
4680 | XColor *colors; | |
4681 | { | |
4682 | int x, y; | |
edfda783 | 4683 | XImagePtr oimg = NULL; |
9d7112ed | 4684 | Pixmap pixmap; |
4ef0d4d0 KS |
4685 | XColor *p; |
4686 | ||
4687 | init_color_table (); | |
4688 | ||
4689 | x_create_x_image_and_pixmap (f, img->width, img->height, 0, | |
4690 | &oimg, &pixmap); | |
4691 | p = colors; | |
4692 | for (y = 0; y < img->height; ++y) | |
4693 | for (x = 0; x < img->width; ++x, ++p) | |
4694 | { | |
4695 | unsigned long pixel; | |
4696 | pixel = lookup_rgb_color (f, p->red, p->green, p->blue); | |
4697 | XPutPixel (oimg, x, y, pixel); | |
4698 | } | |
4699 | ||
4700 | xfree (colors); | |
4701 | x_clear_image_1 (f, img, 1, 0, 1); | |
4702 | ||
4703 | x_put_x_image (f, oimg, pixmap, img->width, img->height); | |
4704 | x_destroy_x_image (oimg); | |
4705 | img->pixmap = pixmap; | |
4706 | #ifdef COLOR_TABLE_SUPPORT | |
4707 | img->colors = colors_in_color_table (&img->ncolors); | |
4708 | free_color_table (); | |
4709 | #endif /* COLOR_TABLE_SUPPORT */ | |
4710 | } | |
4711 | ||
4712 | ||
4713 | /* On frame F, perform edge-detection on image IMG. | |
4714 | ||
4715 | MATRIX is a nine-element array specifying the transformation | |
4716 | matrix. See emboss_matrix for an example. | |
4717 | ||
4718 | COLOR_ADJUST is a color adjustment added to each pixel of the | |
4719 | outgoing image. */ | |
4720 | ||
4721 | static void | |
4722 | x_detect_edges (f, img, matrix, color_adjust) | |
4723 | struct frame *f; | |
4724 | struct image *img; | |
4725 | int matrix[9], color_adjust; | |
4726 | { | |
4727 | XColor *colors = x_to_xcolors (f, img, 1); | |
4728 | XColor *new, *p; | |
4729 | int x, y, i, sum; | |
4730 | ||
4731 | for (i = sum = 0; i < 9; ++i) | |
1ea40aa2 | 4732 | sum += eabs (matrix[i]); |
4ef0d4d0 KS |
4733 | |
4734 | #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X)) | |
4735 | ||
4736 | new = (XColor *) xmalloc (img->width * img->height * sizeof *new); | |
4737 | ||
4738 | for (y = 0; y < img->height; ++y) | |
4739 | { | |
4740 | p = COLOR (new, 0, y); | |
4741 | p->red = p->green = p->blue = 0xffff/2; | |
4742 | p = COLOR (new, img->width - 1, y); | |
4743 | p->red = p->green = p->blue = 0xffff/2; | |
4744 | } | |
4745 | ||
4746 | for (x = 1; x < img->width - 1; ++x) | |
4747 | { | |
4748 | p = COLOR (new, x, 0); | |
4749 | p->red = p->green = p->blue = 0xffff/2; | |
4750 | p = COLOR (new, x, img->height - 1); | |
4751 | p->red = p->green = p->blue = 0xffff/2; | |
4752 | } | |
4753 | ||
4754 | for (y = 1; y < img->height - 1; ++y) | |
4755 | { | |
4756 | p = COLOR (new, 1, y); | |
4757 | ||
4758 | for (x = 1; x < img->width - 1; ++x, ++p) | |
4759 | { | |
4760 | int r, g, b, y1, x1; | |
4761 | ||
4762 | r = g = b = i = 0; | |
4763 | for (y1 = y - 1; y1 < y + 2; ++y1) | |
4764 | for (x1 = x - 1; x1 < x + 2; ++x1, ++i) | |
4765 | if (matrix[i]) | |
4766 | { | |
4767 | XColor *t = COLOR (colors, x1, y1); | |
4768 | r += matrix[i] * t->red; | |
4769 | g += matrix[i] * t->green; | |
4770 | b += matrix[i] * t->blue; | |
4771 | } | |
4772 | ||
4773 | r = (r / sum + color_adjust) & 0xffff; | |
4774 | g = (g / sum + color_adjust) & 0xffff; | |
4775 | b = (b / sum + color_adjust) & 0xffff; | |
4776 | p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b); | |
4777 | } | |
4778 | } | |
4779 | ||
4780 | xfree (colors); | |
4781 | x_from_xcolors (f, img, new); | |
4782 | ||
4783 | #undef COLOR | |
4784 | } | |
4785 | ||
4786 | ||
4787 | /* Perform the pre-defined `emboss' edge-detection on image IMG | |
4788 | on frame F. */ | |
4789 | ||
4790 | static void | |
4791 | x_emboss (f, img) | |
4792 | struct frame *f; | |
4793 | struct image *img; | |
4794 | { | |
4795 | x_detect_edges (f, img, emboss_matrix, 0xffff / 2); | |
4796 | } | |
4797 | ||
4798 | ||
4799 | /* Transform image IMG which is used on frame F with a Laplace | |
4800 | edge-detection algorithm. The result is an image that can be used | |
4801 | to draw disabled buttons, for example. */ | |
4802 | ||
4803 | static void | |
4804 | x_laplace (f, img) | |
4805 | struct frame *f; | |
4806 | struct image *img; | |
4807 | { | |
4808 | x_detect_edges (f, img, laplace_matrix, 45000); | |
4809 | } | |
4810 | ||
4811 | ||
4812 | /* Perform edge-detection on image IMG on frame F, with specified | |
4813 | transformation matrix MATRIX and color-adjustment COLOR_ADJUST. | |
4814 | ||
4815 | MATRIX must be either | |
4816 | ||
4817 | - a list of at least 9 numbers in row-major form | |
4818 | - a vector of at least 9 numbers | |
4819 | ||
4820 | COLOR_ADJUST nil means use a default; otherwise it must be a | |
4821 | number. */ | |
4822 | ||
4823 | static void | |
4824 | x_edge_detection (f, img, matrix, color_adjust) | |
4825 | struct frame *f; | |
4826 | struct image *img; | |
4827 | Lisp_Object matrix, color_adjust; | |
4828 | { | |
4829 | int i = 0; | |
4830 | int trans[9]; | |
4831 | ||
4832 | if (CONSP (matrix)) | |
4833 | { | |
4834 | for (i = 0; | |
4835 | i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix)); | |
4836 | ++i, matrix = XCDR (matrix)) | |
4837 | trans[i] = XFLOATINT (XCAR (matrix)); | |
4838 | } | |
4839 | else if (VECTORP (matrix) && ASIZE (matrix) >= 9) | |
4840 | { | |
4841 | for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i) | |
4842 | trans[i] = XFLOATINT (AREF (matrix, i)); | |
4843 | } | |
4844 | ||
4845 | if (NILP (color_adjust)) | |
4846 | color_adjust = make_number (0xffff / 2); | |
4847 | ||
4848 | if (i == 9 && NUMBERP (color_adjust)) | |
4849 | x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust)); | |
4850 | } | |
4851 | ||
4852 | ||
4853 | /* Transform image IMG on frame F so that it looks disabled. */ | |
4854 | ||
4855 | static void | |
4856 | x_disable_image (f, img) | |
4857 | struct frame *f; | |
4858 | struct image *img; | |
4859 | { | |
4860 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
4861 | #ifdef HAVE_NTGUI | |
4862 | int n_planes = dpyinfo->n_planes * dpyinfo->n_cbits; | |
4863 | #else | |
4864 | int n_planes = dpyinfo->n_planes; | |
4865 | #endif /* HAVE_NTGUI */ | |
4866 | ||
4867 | if (n_planes >= 2) | |
4868 | { | |
4869 | /* Color (or grayscale). Convert to gray, and equalize. Just | |
4870 | drawing such images with a stipple can look very odd, so | |
4871 | we're using this method instead. */ | |
4872 | XColor *colors = x_to_xcolors (f, img, 1); | |
4873 | XColor *p, *end; | |
4874 | const int h = 15000; | |
4875 | const int l = 30000; | |
4876 | ||
4877 | for (p = colors, end = colors + img->width * img->height; | |
4878 | p < end; | |
4879 | ++p) | |
4880 | { | |
4881 | int i = COLOR_INTENSITY (p->red, p->green, p->blue); | |
4882 | int i2 = (0xffff - h - l) * i / 0xffff + l; | |
4883 | p->red = p->green = p->blue = i2; | |
4884 | } | |
4885 | ||
4886 | x_from_xcolors (f, img, colors); | |
4887 | } | |
4888 | ||
4889 | /* Draw a cross over the disabled image, if we must or if we | |
4890 | should. */ | |
4891 | if (n_planes < 2 || cross_disabled_images) | |
4892 | { | |
4893 | #ifndef HAVE_NTGUI | |
4894 | Display *dpy = FRAME_X_DISPLAY (f); | |
4895 | GC gc; | |
4896 | ||
5afa3a81 | 4897 | #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */ |
edfda783 | 4898 | |
4ef0d4d0 | 4899 | #define MaskForeground(f) WHITE_PIX_DEFAULT (f) |
4ef0d4d0 | 4900 | |
ae8279db | 4901 | gc = XCreateGC (dpy, img->pixmap, 0, NULL); |
4ef0d4d0 KS |
4902 | XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f)); |
4903 | XDrawLine (dpy, img->pixmap, gc, 0, 0, | |
4904 | img->width - 1, img->height - 1); | |
4905 | XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1, | |
4906 | img->width - 1, 0); | |
4907 | XFreeGC (dpy, gc); | |
4908 | ||
4909 | if (img->mask) | |
4910 | { | |
ae8279db | 4911 | gc = XCreateGC (dpy, img->mask, 0, NULL); |
4ef0d4d0 KS |
4912 | XSetForeground (dpy, gc, MaskForeground (f)); |
4913 | XDrawLine (dpy, img->mask, gc, 0, 0, | |
4914 | img->width - 1, img->height - 1); | |
4915 | XDrawLine (dpy, img->mask, gc, 0, img->height - 1, | |
4916 | img->width - 1, 0); | |
4917 | XFreeGC (dpy, gc); | |
4918 | } | |
edfda783 | 4919 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4920 | #else |
4921 | HDC hdc, bmpdc; | |
4922 | HGDIOBJ prev; | |
4923 | ||
4924 | hdc = get_frame_dc (f); | |
4925 | bmpdc = CreateCompatibleDC (hdc); | |
4926 | release_frame_dc (f, hdc); | |
4927 | ||
4928 | prev = SelectObject (bmpdc, img->pixmap); | |
4929 | ||
4930 | SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f)); | |
4931 | MoveToEx (bmpdc, 0, 0, NULL); | |
4932 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4933 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4934 | LineTo (bmpdc, img->width - 1, 0); | |
4935 | ||
4936 | if (img->mask) | |
4937 | { | |
4938 | SelectObject (bmpdc, img->mask); | |
4939 | SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f)); | |
4940 | MoveToEx (bmpdc, 0, 0, NULL); | |
4941 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4942 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4943 | LineTo (bmpdc, img->width - 1, 0); | |
4944 | } | |
4945 | SelectObject (bmpdc, prev); | |
4946 | DeleteDC (bmpdc); | |
4947 | #endif /* HAVE_NTGUI */ | |
4948 | } | |
4949 | } | |
4950 | ||
4951 | ||
4952 | /* Build a mask for image IMG which is used on frame F. FILE is the | |
4953 | name of an image file, for error messages. HOW determines how to | |
4954 | determine the background color of IMG. If it is a list '(R G B)', | |
4955 | with R, G, and B being integers >= 0, take that as the color of the | |
4956 | background. Otherwise, determine the background color of IMG | |
4957 | heuristically. Value is non-zero if successful. */ | |
4958 | ||
4959 | static int | |
4960 | x_build_heuristic_mask (f, img, how) | |
4961 | struct frame *f; | |
4962 | struct image *img; | |
4963 | Lisp_Object how; | |
4964 | { | |
4965 | XImagePtr_or_DC ximg; | |
4966 | #ifndef HAVE_NTGUI | |
4967 | XImagePtr mask_img; | |
4968 | #else | |
4969 | HDC frame_dc; | |
4970 | HGDIOBJ prev; | |
4971 | char *mask_img; | |
4972 | int row_width; | |
4973 | #endif /* HAVE_NTGUI */ | |
4974 | int x, y, rc, use_img_background; | |
4975 | unsigned long bg = 0; | |
4976 | ||
4977 | if (img->mask) | |
4978 | { | |
4979 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
4980 | img->mask = NO_PIXMAP; | |
4981 | img->background_transparent_valid = 0; | |
4982 | } | |
4983 | ||
4984 | #ifndef HAVE_NTGUI | |
edfda783 | 4985 | #ifndef HAVE_NS |
4ef0d4d0 KS |
4986 | /* Create an image and pixmap serving as mask. */ |
4987 | rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1, | |
4988 | &mask_img, &img->mask); | |
4989 | if (!rc) | |
4990 | return 0; | |
edfda783 | 4991 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4992 | |
4993 | /* Get the X image of IMG->pixmap. */ | |
4994 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, 0, 0, | |
4995 | img->width, img->height, | |
4996 | ~0, ZPixmap); | |
4997 | #else | |
4998 | /* Create the bit array serving as mask. */ | |
4999 | row_width = (img->width + 7) / 8; | |
5000 | mask_img = xmalloc (row_width * img->height); | |
5001 | bzero (mask_img, row_width * img->height); | |
5002 | ||
5003 | /* Create a memory device context for IMG->pixmap. */ | |
5004 | frame_dc = get_frame_dc (f); | |
5005 | ximg = CreateCompatibleDC (frame_dc); | |
5006 | release_frame_dc (f, frame_dc); | |
5007 | prev = SelectObject (ximg, img->pixmap); | |
5008 | #endif /* HAVE_NTGUI */ | |
5009 | ||
5010 | /* Determine the background color of ximg. If HOW is `(R G B)' | |
5011 | take that as color. Otherwise, use the image's background color. */ | |
5012 | use_img_background = 1; | |
5013 | ||
5014 | if (CONSP (how)) | |
5015 | { | |
5016 | int rgb[3], i; | |
5017 | ||
5018 | for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i) | |
5019 | { | |
5020 | rgb[i] = XFASTINT (XCAR (how)) & 0xffff; | |
5021 | how = XCDR (how); | |
5022 | } | |
5023 | ||
5024 | if (i == 3 && NILP (how)) | |
5025 | { | |
5026 | char color_name[30]; | |
5027 | sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]); | |
5028 | bg = ( | |
5029 | #ifdef HAVE_NTGUI | |
5030 | 0x00ffffff & /* Filter out palette info. */ | |
5031 | #endif /* HAVE_NTGUI */ | |
5032 | x_alloc_image_color (f, img, build_string (color_name), 0)); | |
5033 | use_img_background = 0; | |
5034 | } | |
5035 | } | |
5036 | ||
5037 | if (use_img_background) | |
6ac3c7bb | 5038 | bg = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
5039 | |
5040 | /* Set all bits in mask_img to 1 whose color in ximg is different | |
5041 | from the background color bg. */ | |
5042 | #ifndef HAVE_NTGUI | |
5043 | for (y = 0; y < img->height; ++y) | |
5044 | for (x = 0; x < img->width; ++x) | |
edfda783 | 5045 | #ifndef HAVE_NS |
4ef0d4d0 | 5046 | XPutPixel (mask_img, x, y, (XGetPixel (ximg, x, y) != bg |
9ecf6403 | 5047 | ? PIX_MASK_DRAW : PIX_MASK_RETAIN)); |
edfda783 AR |
5048 | #else |
5049 | if (XGetPixel (ximg, x, y) == bg) | |
5050 | ns_set_alpha(ximg, x, y, 0); | |
5051 | #endif /* HAVE_NS */ | |
5052 | #ifndef HAVE_NS | |
4ef0d4d0 KS |
5053 | /* Fill in the background_transparent field while we have the mask handy. */ |
5054 | image_background_transparent (img, f, mask_img); | |
5055 | ||
5056 | /* Put mask_img into img->mask. */ | |
5057 | x_put_x_image (f, mask_img, img->mask, img->width, img->height); | |
5058 | x_destroy_x_image (mask_img); | |
edfda783 | 5059 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
5060 | #else |
5061 | for (y = 0; y < img->height; ++y) | |
5062 | for (x = 0; x < img->width; ++x) | |
5063 | { | |
5064 | COLORREF p = GetPixel (ximg, x, y); | |
5065 | if (p != bg) | |
5066 | mask_img[y * row_width + x / 8] |= 1 << (x % 8); | |
5067 | } | |
5068 | ||
5069 | /* Create the mask image. */ | |
5070 | img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height, | |
5071 | mask_img); | |
5072 | /* Fill in the background_transparent field while we have the mask handy. */ | |
5073 | SelectObject (ximg, img->mask); | |
5074 | image_background_transparent (img, f, ximg); | |
5075 | ||
5076 | /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */ | |
5077 | xfree (mask_img); | |
5078 | #endif /* HAVE_NTGUI */ | |
5079 | ||
5080 | Destroy_Image (ximg, prev); | |
5081 | ||
5082 | return 1; | |
5083 | } | |
5084 | ||
5085 | \f | |
5086 | /*********************************************************************** | |
5087 | PBM (mono, gray, color) | |
5088 | ***********************************************************************/ | |
5089 | ||
5090 | static int pbm_image_p P_ ((Lisp_Object object)); | |
5091 | static int pbm_load P_ ((struct frame *f, struct image *img)); | |
5092 | static int pbm_scan_number P_ ((unsigned char **, unsigned char *)); | |
5093 | ||
5094 | /* The symbol `pbm' identifying images of this type. */ | |
5095 | ||
5096 | Lisp_Object Qpbm; | |
5097 | ||
5098 | /* Indices of image specification fields in gs_format, below. */ | |
5099 | ||
5100 | enum pbm_keyword_index | |
5101 | { | |
5102 | PBM_TYPE, | |
5103 | PBM_FILE, | |
5104 | PBM_DATA, | |
5105 | PBM_ASCENT, | |
5106 | PBM_MARGIN, | |
5107 | PBM_RELIEF, | |
5108 | PBM_ALGORITHM, | |
5109 | PBM_HEURISTIC_MASK, | |
5110 | PBM_MASK, | |
5111 | PBM_FOREGROUND, | |
5112 | PBM_BACKGROUND, | |
5113 | PBM_LAST | |
5114 | }; | |
5115 | ||
5116 | /* Vector of image_keyword structures describing the format | |
5117 | of valid user-defined image specifications. */ | |
5118 | ||
91433552 | 5119 | static const struct image_keyword pbm_format[PBM_LAST] = |
4ef0d4d0 KS |
5120 | { |
5121 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5122 | {":file", IMAGE_STRING_VALUE, 0}, | |
5123 | {":data", IMAGE_STRING_VALUE, 0}, | |
5124 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
5125 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
5126 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
5127 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5128 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5129 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5130 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
5131 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5132 | }; | |
5133 | ||
5134 | /* Structure describing the image type `pbm'. */ | |
5135 | ||
5136 | static struct image_type pbm_type = | |
5137 | { | |
5138 | &Qpbm, | |
5139 | pbm_image_p, | |
5140 | pbm_load, | |
5141 | x_clear_image, | |
5142 | NULL | |
5143 | }; | |
5144 | ||
5145 | ||
5146 | /* Return non-zero if OBJECT is a valid PBM image specification. */ | |
5147 | ||
5148 | static int | |
5149 | pbm_image_p (object) | |
5150 | Lisp_Object object; | |
5151 | { | |
5152 | struct image_keyword fmt[PBM_LAST]; | |
5153 | ||
5154 | bcopy (pbm_format, fmt, sizeof fmt); | |
5155 | ||
5156 | if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)) | |
5157 | return 0; | |
5158 | ||
5159 | /* Must specify either :data or :file. */ | |
5160 | return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1; | |
5161 | } | |
5162 | ||
5163 | ||
5164 | /* Scan a decimal number from *S and return it. Advance *S while | |
5165 | reading the number. END is the end of the string. Value is -1 at | |
5166 | end of input. */ | |
5167 | ||
5168 | static int | |
5169 | pbm_scan_number (s, end) | |
5170 | unsigned char **s, *end; | |
5171 | { | |
5172 | int c = 0, val = -1; | |
5173 | ||
5174 | while (*s < end) | |
5175 | { | |
5176 | /* Skip white-space. */ | |
5177 | while (*s < end && (c = *(*s)++, isspace (c))) | |
5178 | ; | |
5179 | ||
5180 | if (c == '#') | |
5181 | { | |
5182 | /* Skip comment to end of line. */ | |
5183 | while (*s < end && (c = *(*s)++, c != '\n')) | |
5184 | ; | |
5185 | } | |
5186 | else if (isdigit (c)) | |
5187 | { | |
5188 | /* Read decimal number. */ | |
5189 | val = c - '0'; | |
5190 | while (*s < end && (c = *(*s)++, isdigit (c))) | |
5191 | val = 10 * val + c - '0'; | |
5192 | break; | |
5193 | } | |
5194 | else | |
5195 | break; | |
5196 | } | |
5197 | ||
5198 | return val; | |
5199 | } | |
5200 | ||
5201 | ||
5202 | #ifdef HAVE_NTGUI | |
5203 | #if 0 /* Unused. ++kfs */ | |
5204 | ||
5205 | /* Read FILE into memory. Value is a pointer to a buffer allocated | |
5206 | with xmalloc holding FILE's contents. Value is null if an error | |
5207 | occurred. *SIZE is set to the size of the file. */ | |
5208 | ||
5209 | static char * | |
5210 | pbm_read_file (file, size) | |
5211 | Lisp_Object file; | |
5212 | int *size; | |
5213 | { | |
5214 | FILE *fp = NULL; | |
5215 | char *buf = NULL; | |
5216 | struct stat st; | |
5217 | ||
5218 | if (stat (SDATA (file), &st) == 0 | |
5219 | && (fp = fopen (SDATA (file), "rb")) != NULL | |
5220 | && (buf = (char *) xmalloc (st.st_size), | |
5221 | fread (buf, 1, st.st_size, fp) == st.st_size)) | |
5222 | { | |
5223 | *size = st.st_size; | |
5224 | fclose (fp); | |
5225 | } | |
5226 | else | |
5227 | { | |
5228 | if (fp) | |
5229 | fclose (fp); | |
5230 | if (buf) | |
5231 | { | |
5232 | xfree (buf); | |
5233 | buf = NULL; | |
5234 | } | |
5235 | } | |
5236 | ||
5237 | return buf; | |
5238 | } | |
5239 | #endif | |
5240 | #endif /* HAVE_NTGUI */ | |
5241 | ||
5242 | /* Load PBM image IMG for use on frame F. */ | |
5243 | ||
5244 | static int | |
5245 | pbm_load (f, img) | |
5246 | struct frame *f; | |
5247 | struct image *img; | |
5248 | { | |
5249 | int raw_p, x, y; | |
5250 | int width, height, max_color_idx = 0; | |
5251 | XImagePtr ximg; | |
5252 | Lisp_Object file, specified_file; | |
5253 | enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type; | |
5254 | struct gcpro gcpro1; | |
5255 | unsigned char *contents = NULL; | |
5256 | unsigned char *end, *p; | |
5257 | int size; | |
5258 | ||
5259 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
5260 | file = Qnil; | |
5261 | GCPRO1 (file); | |
5262 | ||
5263 | if (STRINGP (specified_file)) | |
5264 | { | |
5265 | file = x_find_image_file (specified_file); | |
5266 | if (!STRINGP (file)) | |
5267 | { | |
5268 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
5269 | UNGCPRO; | |
5270 | return 0; | |
5271 | } | |
5272 | ||
5273 | contents = slurp_file (SDATA (file), &size); | |
5274 | if (contents == NULL) | |
5275 | { | |
5276 | image_error ("Error reading `%s'", file, Qnil); | |
5277 | UNGCPRO; | |
5278 | return 0; | |
5279 | } | |
5280 | ||
5281 | p = contents; | |
5282 | end = contents + size; | |
5283 | } | |
5284 | else | |
5285 | { | |
5286 | Lisp_Object data; | |
5287 | data = image_spec_value (img->spec, QCdata, NULL); | |
5288 | p = SDATA (data); | |
5289 | end = p + SBYTES (data); | |
5290 | } | |
5291 | ||
5292 | /* Check magic number. */ | |
5293 | if (end - p < 2 || *p++ != 'P') | |
5294 | { | |
5295 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5296 | error: | |
5297 | xfree (contents); | |
5298 | UNGCPRO; | |
5299 | return 0; | |
5300 | } | |
5301 | ||
5302 | switch (*p++) | |
5303 | { | |
5304 | case '1': | |
5305 | raw_p = 0, type = PBM_MONO; | |
5306 | break; | |
5307 | ||
5308 | case '2': | |
5309 | raw_p = 0, type = PBM_GRAY; | |
5310 | break; | |
5311 | ||
5312 | case '3': | |
5313 | raw_p = 0, type = PBM_COLOR; | |
5314 | break; | |
5315 | ||
5316 | case '4': | |
5317 | raw_p = 1, type = PBM_MONO; | |
5318 | break; | |
5319 | ||
5320 | case '5': | |
5321 | raw_p = 1, type = PBM_GRAY; | |
5322 | break; | |
5323 | ||
5324 | case '6': | |
5325 | raw_p = 1, type = PBM_COLOR; | |
5326 | break; | |
5327 | ||
5328 | default: | |
5329 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5330 | goto error; | |
5331 | } | |
5332 | ||
5333 | /* Read width, height, maximum color-component. Characters | |
5334 | starting with `#' up to the end of a line are ignored. */ | |
5335 | width = pbm_scan_number (&p, end); | |
5336 | height = pbm_scan_number (&p, end); | |
5337 | ||
5338 | if (type != PBM_MONO) | |
5339 | { | |
5340 | max_color_idx = pbm_scan_number (&p, end); | |
b9c89e11 JR |
5341 | if (max_color_idx > 65535 || max_color_idx < 0) |
5342 | { | |
5343 | image_error ("Unsupported maximum PBM color value", Qnil, Qnil); | |
5344 | goto error; | |
5345 | } | |
4ef0d4d0 KS |
5346 | } |
5347 | ||
b9c89e11 JR |
5348 | if (!check_image_size (f, width, height)) |
5349 | { | |
10ea2b82 | 5350 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
b9c89e11 JR |
5351 | goto error; |
5352 | } | |
4ef0d4d0 KS |
5353 | |
5354 | if (!x_create_x_image_and_pixmap (f, width, height, 0, | |
5355 | &ximg, &img->pixmap)) | |
5356 | goto error; | |
5357 | ||
5358 | /* Initialize the color hash table. */ | |
5359 | init_color_table (); | |
5360 | ||
5361 | if (type == PBM_MONO) | |
5362 | { | |
5363 | int c = 0, g; | |
5364 | struct image_keyword fmt[PBM_LAST]; | |
5365 | unsigned long fg = FRAME_FOREGROUND_PIXEL (f); | |
5366 | unsigned long bg = FRAME_BACKGROUND_PIXEL (f); | |
5367 | ||
5368 | /* Parse the image specification. */ | |
5369 | bcopy (pbm_format, fmt, sizeof fmt); | |
5370 | parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm); | |
5371 | ||
5372 | /* Get foreground and background colors, maybe allocate colors. */ | |
5373 | if (fmt[PBM_FOREGROUND].count | |
5374 | && STRINGP (fmt[PBM_FOREGROUND].value)) | |
5375 | fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg); | |
5376 | if (fmt[PBM_BACKGROUND].count | |
5377 | && STRINGP (fmt[PBM_BACKGROUND].value)) | |
5378 | { | |
5379 | bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg); | |
5380 | img->background = bg; | |
5381 | img->background_valid = 1; | |
5382 | } | |
5383 | ||
5384 | for (y = 0; y < height; ++y) | |
5385 | for (x = 0; x < width; ++x) | |
5386 | { | |
5387 | if (raw_p) | |
5388 | { | |
5389 | if ((x & 7) == 0) | |
c295e710 CY |
5390 | { |
5391 | if (p >= end) | |
5392 | { | |
5393 | x_destroy_x_image (ximg); | |
5394 | x_clear_image (f, img); | |
5395 | image_error ("Invalid image size in image `%s'", | |
5396 | img->spec, Qnil); | |
5397 | goto error; | |
5398 | } | |
5399 | c = *p++; | |
5400 | } | |
4ef0d4d0 KS |
5401 | g = c & 0x80; |
5402 | c <<= 1; | |
5403 | } | |
5404 | else | |
5405 | g = pbm_scan_number (&p, end); | |
5406 | ||
5407 | XPutPixel (ximg, x, y, g ? fg : bg); | |
5408 | } | |
5409 | } | |
5410 | else | |
5411 | { | |
b9c89e11 JR |
5412 | int expected_size = height * width; |
5413 | if (max_color_idx > 255) | |
5414 | expected_size *= 2; | |
5415 | if (type == PBM_COLOR) | |
5416 | expected_size *= 3; | |
5417 | ||
5418 | if (raw_p && p + expected_size > end) | |
39e653ea CY |
5419 | { |
5420 | x_destroy_x_image (ximg); | |
fb0b0862 | 5421 | x_clear_image (f, img); |
39e653ea CY |
5422 | image_error ("Invalid image size in image `%s'", |
5423 | img->spec, Qnil); | |
5424 | goto error; | |
5425 | } | |
5426 | ||
4ef0d4d0 KS |
5427 | for (y = 0; y < height; ++y) |
5428 | for (x = 0; x < width; ++x) | |
5429 | { | |
5430 | int r, g, b; | |
5431 | ||
b9c89e11 JR |
5432 | if (type == PBM_GRAY && raw_p) |
5433 | { | |
5434 | r = g = b = *p++; | |
5435 | if (max_color_idx > 255) | |
5436 | r = g = b = r * 256 + *p++; | |
5437 | } | |
5438 | else if (type == PBM_GRAY) | |
5439 | r = g = b = pbm_scan_number (&p, end); | |
4ef0d4d0 KS |
5440 | else if (raw_p) |
5441 | { | |
5442 | r = *p++; | |
b9c89e11 JR |
5443 | if (max_color_idx > 255) |
5444 | r = r * 256 + *p++; | |
4ef0d4d0 | 5445 | g = *p++; |
b9c89e11 JR |
5446 | if (max_color_idx > 255) |
5447 | g = g * 256 + *p++; | |
4ef0d4d0 | 5448 | b = *p++; |
b9c89e11 JR |
5449 | if (max_color_idx > 255) |
5450 | b = b * 256 + *p++; | |
4ef0d4d0 KS |
5451 | } |
5452 | else | |
5453 | { | |
5454 | r = pbm_scan_number (&p, end); | |
5455 | g = pbm_scan_number (&p, end); | |
5456 | b = pbm_scan_number (&p, end); | |
5457 | } | |
5458 | ||
5459 | if (r < 0 || g < 0 || b < 0) | |
5460 | { | |
5461 | x_destroy_x_image (ximg); | |
5462 | image_error ("Invalid pixel value in image `%s'", | |
5463 | img->spec, Qnil); | |
5464 | goto error; | |
5465 | } | |
5466 | ||
5467 | /* RGB values are now in the range 0..max_color_idx. | |
5468 | Scale this to the range 0..0xffff supported by X. */ | |
5469 | r = (double) r * 65535 / max_color_idx; | |
5470 | g = (double) g * 65535 / max_color_idx; | |
5471 | b = (double) b * 65535 / max_color_idx; | |
5472 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
5473 | } | |
5474 | } | |
5475 | ||
5476 | #ifdef COLOR_TABLE_SUPPORT | |
5477 | /* Store in IMG->colors the colors allocated for the image, and | |
5478 | free the color table. */ | |
5479 | img->colors = colors_in_color_table (&img->ncolors); | |
5480 | free_color_table (); | |
5481 | #endif /* COLOR_TABLE_SUPPORT */ | |
5482 | ||
5483 | img->width = width; | |
5484 | img->height = height; | |
5485 | ||
5486 | /* Maybe fill in the background field while we have ximg handy. */ | |
5487 | ||
5488 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
5489 | /* Casting avoids a GCC warning. */ |
5490 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
5491 | |
5492 | /* Put the image into a pixmap. */ | |
5493 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
5494 | x_destroy_x_image (ximg); | |
5495 | ||
5496 | /* X and W32 versions did it here, MAC version above. ++kfs | |
1f026899 | 5497 | img->width = width; |
4ef0d4d0 KS |
5498 | img->height = height; */ |
5499 | ||
5500 | UNGCPRO; | |
5501 | xfree (contents); | |
5502 | return 1; | |
5503 | } | |
5504 | ||
5505 | \f | |
5506 | /*********************************************************************** | |
5507 | PNG | |
5508 | ***********************************************************************/ | |
5509 | ||
9e2a2647 | 5510 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
4ef0d4d0 KS |
5511 | |
5512 | /* Function prototypes. */ | |
5513 | ||
5514 | static int png_image_p P_ ((Lisp_Object object)); | |
5515 | static int png_load P_ ((struct frame *f, struct image *img)); | |
5516 | ||
5517 | /* The symbol `png' identifying images of this type. */ | |
5518 | ||
5519 | Lisp_Object Qpng; | |
5520 | ||
5521 | /* Indices of image specification fields in png_format, below. */ | |
5522 | ||
5523 | enum png_keyword_index | |
5524 | { | |
5525 | PNG_TYPE, | |
5526 | PNG_DATA, | |
5527 | PNG_FILE, | |
5528 | PNG_ASCENT, | |
5529 | PNG_MARGIN, | |
5530 | PNG_RELIEF, | |
5531 | PNG_ALGORITHM, | |
5532 | PNG_HEURISTIC_MASK, | |
5533 | PNG_MASK, | |
5534 | PNG_BACKGROUND, | |
5535 | PNG_LAST | |
5536 | }; | |
5537 | ||
5538 | /* Vector of image_keyword structures describing the format | |
5539 | of valid user-defined image specifications. */ | |
5540 | ||
91433552 | 5541 | static const struct image_keyword png_format[PNG_LAST] = |
4ef0d4d0 KS |
5542 | { |
5543 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5544 | {":data", IMAGE_STRING_VALUE, 0}, | |
5545 | {":file", IMAGE_STRING_VALUE, 0}, | |
5546 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
5547 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
5548 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
5549 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5550 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5551 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5552 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5553 | }; | |
5554 | ||
5555 | /* Structure describing the image type `png'. */ | |
5556 | ||
5557 | static struct image_type png_type = | |
5558 | { | |
5559 | &Qpng, | |
5560 | png_image_p, | |
5561 | png_load, | |
5562 | x_clear_image, | |
5563 | NULL | |
5564 | }; | |
5565 | ||
5566 | /* Return non-zero if OBJECT is a valid PNG image specification. */ | |
5567 | ||
5568 | static int | |
5569 | png_image_p (object) | |
5570 | Lisp_Object object; | |
5571 | { | |
5572 | struct image_keyword fmt[PNG_LAST]; | |
5573 | bcopy (png_format, fmt, sizeof fmt); | |
5574 | ||
5575 | if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)) | |
5576 | return 0; | |
5577 | ||
5578 | /* Must specify either the :data or :file keyword. */ | |
5579 | return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1; | |
5580 | } | |
5581 | ||
9e2a2647 | 5582 | #endif /* HAVE_PNG || HAVE_NS */ |
4ef0d4d0 KS |
5583 | |
5584 | ||
5585 | #ifdef HAVE_PNG | |
5586 | ||
4ef0d4d0 KS |
5587 | #ifdef HAVE_NTGUI |
5588 | /* PNG library details. */ | |
5589 | ||
5590 | DEF_IMGLIB_FN (png_get_io_ptr); | |
285d07e2 | 5591 | DEF_IMGLIB_FN (png_sig_cmp); |
4ef0d4d0 KS |
5592 | DEF_IMGLIB_FN (png_create_read_struct); |
5593 | DEF_IMGLIB_FN (png_create_info_struct); | |
5594 | DEF_IMGLIB_FN (png_destroy_read_struct); | |
5595 | DEF_IMGLIB_FN (png_set_read_fn); | |
4ef0d4d0 KS |
5596 | DEF_IMGLIB_FN (png_set_sig_bytes); |
5597 | DEF_IMGLIB_FN (png_read_info); | |
5598 | DEF_IMGLIB_FN (png_get_IHDR); | |
5599 | DEF_IMGLIB_FN (png_get_valid); | |
5600 | DEF_IMGLIB_FN (png_set_strip_16); | |
5601 | DEF_IMGLIB_FN (png_set_expand); | |
5602 | DEF_IMGLIB_FN (png_set_gray_to_rgb); | |
5603 | DEF_IMGLIB_FN (png_set_background); | |
5604 | DEF_IMGLIB_FN (png_get_bKGD); | |
5605 | DEF_IMGLIB_FN (png_read_update_info); | |
5606 | DEF_IMGLIB_FN (png_get_channels); | |
5607 | DEF_IMGLIB_FN (png_get_rowbytes); | |
5608 | DEF_IMGLIB_FN (png_read_image); | |
5609 | DEF_IMGLIB_FN (png_read_end); | |
5610 | DEF_IMGLIB_FN (png_error); | |
5611 | ||
5612 | static int | |
0855eb52 | 5613 | init_png_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
5614 | { |
5615 | HMODULE library; | |
5616 | ||
4ef0d4d0 | 5617 | /* Try loading libpng under probable names. */ |
0855eb52 | 5618 | if (!(library = w32_delayed_load (libraries, Qpng))) |
4ef0d4d0 KS |
5619 | return 0; |
5620 | ||
5621 | LOAD_IMGLIB_FN (library, png_get_io_ptr); | |
285d07e2 | 5622 | LOAD_IMGLIB_FN (library, png_sig_cmp); |
4ef0d4d0 KS |
5623 | LOAD_IMGLIB_FN (library, png_create_read_struct); |
5624 | LOAD_IMGLIB_FN (library, png_create_info_struct); | |
5625 | LOAD_IMGLIB_FN (library, png_destroy_read_struct); | |
5626 | LOAD_IMGLIB_FN (library, png_set_read_fn); | |
4ef0d4d0 KS |
5627 | LOAD_IMGLIB_FN (library, png_set_sig_bytes); |
5628 | LOAD_IMGLIB_FN (library, png_read_info); | |
5629 | LOAD_IMGLIB_FN (library, png_get_IHDR); | |
5630 | LOAD_IMGLIB_FN (library, png_get_valid); | |
5631 | LOAD_IMGLIB_FN (library, png_set_strip_16); | |
5632 | LOAD_IMGLIB_FN (library, png_set_expand); | |
5633 | LOAD_IMGLIB_FN (library, png_set_gray_to_rgb); | |
5634 | LOAD_IMGLIB_FN (library, png_set_background); | |
5635 | LOAD_IMGLIB_FN (library, png_get_bKGD); | |
5636 | LOAD_IMGLIB_FN (library, png_read_update_info); | |
5637 | LOAD_IMGLIB_FN (library, png_get_channels); | |
5638 | LOAD_IMGLIB_FN (library, png_get_rowbytes); | |
5639 | LOAD_IMGLIB_FN (library, png_read_image); | |
5640 | LOAD_IMGLIB_FN (library, png_read_end); | |
5641 | LOAD_IMGLIB_FN (library, png_error); | |
5642 | return 1; | |
5643 | } | |
5644 | #else | |
5645 | ||
5646 | #define fn_png_get_io_ptr png_get_io_ptr | |
285d07e2 | 5647 | #define fn_png_sig_cmp png_sig_cmp |
4ef0d4d0 KS |
5648 | #define fn_png_create_read_struct png_create_read_struct |
5649 | #define fn_png_create_info_struct png_create_info_struct | |
5650 | #define fn_png_destroy_read_struct png_destroy_read_struct | |
5651 | #define fn_png_set_read_fn png_set_read_fn | |
4ef0d4d0 KS |
5652 | #define fn_png_set_sig_bytes png_set_sig_bytes |
5653 | #define fn_png_read_info png_read_info | |
5654 | #define fn_png_get_IHDR png_get_IHDR | |
5655 | #define fn_png_get_valid png_get_valid | |
5656 | #define fn_png_set_strip_16 png_set_strip_16 | |
5657 | #define fn_png_set_expand png_set_expand | |
5658 | #define fn_png_set_gray_to_rgb png_set_gray_to_rgb | |
5659 | #define fn_png_set_background png_set_background | |
5660 | #define fn_png_get_bKGD png_get_bKGD | |
5661 | #define fn_png_read_update_info png_read_update_info | |
5662 | #define fn_png_get_channels png_get_channels | |
5663 | #define fn_png_get_rowbytes png_get_rowbytes | |
5664 | #define fn_png_read_image png_read_image | |
5665 | #define fn_png_read_end png_read_end | |
5666 | #define fn_png_error png_error | |
5667 | ||
5668 | #endif /* HAVE_NTGUI */ | |
5669 | ||
5670 | /* Error and warning handlers installed when the PNG library | |
5671 | is initialized. */ | |
5672 | ||
5673 | static void | |
5674 | my_png_error (png_ptr, msg) | |
5675 | png_struct *png_ptr; | |
5676 | char *msg; | |
5677 | { | |
5678 | xassert (png_ptr != NULL); | |
5679 | image_error ("PNG error: %s", build_string (msg), Qnil); | |
5680 | longjmp (png_ptr->jmpbuf, 1); | |
5681 | } | |
5682 | ||
5683 | ||
5684 | static void | |
5685 | my_png_warning (png_ptr, msg) | |
5686 | png_struct *png_ptr; | |
5687 | char *msg; | |
5688 | { | |
5689 | xassert (png_ptr != NULL); | |
5690 | image_error ("PNG warning: %s", build_string (msg), Qnil); | |
5691 | } | |
5692 | ||
5693 | /* Memory source for PNG decoding. */ | |
5694 | ||
5695 | struct png_memory_storage | |
5696 | { | |
5697 | unsigned char *bytes; /* The data */ | |
5698 | size_t len; /* How big is it? */ | |
5699 | int index; /* Where are we? */ | |
5700 | }; | |
5701 | ||
5702 | ||
5703 | /* Function set as reader function when reading PNG image from memory. | |
5704 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5705 | bytes from the input to DATA. */ | |
5706 | ||
5707 | static void | |
5708 | png_read_from_memory (png_ptr, data, length) | |
5709 | png_structp png_ptr; | |
5710 | png_bytep data; | |
5711 | png_size_t length; | |
5712 | { | |
5713 | struct png_memory_storage *tbr | |
5714 | = (struct png_memory_storage *) fn_png_get_io_ptr (png_ptr); | |
5715 | ||
5716 | if (length > tbr->len - tbr->index) | |
5717 | fn_png_error (png_ptr, "Read error"); | |
5718 | ||
5719 | bcopy (tbr->bytes + tbr->index, data, length); | |
5720 | tbr->index = tbr->index + length; | |
5721 | } | |
5722 | ||
b0da69a7 | 5723 | |
9a22a004 JB |
5724 | /* Function set as reader function when reading PNG image from a file. |
5725 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5726 | bytes from the input to DATA. */ | |
5727 | ||
5728 | static void | |
5729 | png_read_from_file (png_ptr, data, length) | |
5730 | png_structp png_ptr; | |
5731 | png_bytep data; | |
5732 | png_size_t length; | |
5733 | { | |
5734 | FILE *fp = (FILE *) fn_png_get_io_ptr (png_ptr); | |
5735 | ||
5736 | if (fread (data, 1, length, fp) < length) | |
5737 | fn_png_error (png_ptr, "Read error"); | |
5738 | } | |
5739 | ||
5740 | ||
4ef0d4d0 KS |
5741 | /* Load PNG image IMG for use on frame F. Value is non-zero if |
5742 | successful. */ | |
5743 | ||
5744 | static int | |
5745 | png_load (f, img) | |
5746 | struct frame *f; | |
5747 | struct image *img; | |
5748 | { | |
5749 | Lisp_Object file, specified_file; | |
5750 | Lisp_Object specified_data; | |
5751 | int x, y, i; | |
5752 | XImagePtr ximg, mask_img = NULL; | |
5753 | struct gcpro gcpro1; | |
5754 | png_struct *png_ptr = NULL; | |
5755 | png_info *info_ptr = NULL, *end_info = NULL; | |
5756 | FILE *volatile fp = NULL; | |
5757 | png_byte sig[8]; | |
5758 | png_byte * volatile pixels = NULL; | |
5759 | png_byte ** volatile rows = NULL; | |
5760 | png_uint_32 width, height; | |
5761 | int bit_depth, color_type, interlace_type; | |
5762 | png_byte channels; | |
5763 | png_uint_32 row_bytes; | |
5764 | int transparent_p; | |
4ef0d4d0 KS |
5765 | struct png_memory_storage tbr; /* Data to be read */ |
5766 | ||
5767 | /* Find out what file to load. */ | |
5768 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
5769 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
5770 | file = Qnil; | |
5771 | GCPRO1 (file); | |
5772 | ||
5773 | if (NILP (specified_data)) | |
5774 | { | |
5775 | file = x_find_image_file (specified_file); | |
5776 | if (!STRINGP (file)) | |
5777 | { | |
5778 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
5779 | UNGCPRO; | |
5780 | return 0; | |
5781 | } | |
5782 | ||
5783 | /* Open the image file. */ | |
5784 | fp = fopen (SDATA (file), "rb"); | |
5785 | if (!fp) | |
5786 | { | |
5787 | image_error ("Cannot open image file `%s'", file, Qnil); | |
5788 | UNGCPRO; | |
4ef0d4d0 KS |
5789 | return 0; |
5790 | } | |
5791 | ||
5792 | /* Check PNG signature. */ | |
5793 | if (fread (sig, 1, sizeof sig, fp) != sizeof sig | |
285d07e2 | 5794 | || fn_png_sig_cmp (sig, 0, sizeof sig)) |
4ef0d4d0 KS |
5795 | { |
5796 | image_error ("Not a PNG file: `%s'", file, Qnil); | |
5797 | UNGCPRO; | |
5798 | fclose (fp); | |
5799 | return 0; | |
5800 | } | |
5801 | } | |
5802 | else | |
5803 | { | |
5804 | /* Read from memory. */ | |
5805 | tbr.bytes = SDATA (specified_data); | |
5806 | tbr.len = SBYTES (specified_data); | |
5807 | tbr.index = 0; | |
5808 | ||
5809 | /* Check PNG signature. */ | |
5810 | if (tbr.len < sizeof sig | |
285d07e2 | 5811 | || fn_png_sig_cmp (tbr.bytes, 0, sizeof sig)) |
4ef0d4d0 KS |
5812 | { |
5813 | image_error ("Not a PNG image: `%s'", img->spec, Qnil); | |
5814 | UNGCPRO; | |
5815 | return 0; | |
5816 | } | |
5817 | ||
5818 | /* Need to skip past the signature. */ | |
5819 | tbr.bytes += sizeof (sig); | |
5820 | } | |
5821 | ||
2e09fef1 EZ |
5822 | /* Initialize read and info structs for PNG lib. Casting return |
5823 | value avoids a GCC warning on W32. */ | |
5824 | png_ptr = (png_structp)fn_png_create_read_struct (PNG_LIBPNG_VER_STRING, | |
5825 | NULL, my_png_error, | |
5826 | my_png_warning); | |
4ef0d4d0 KS |
5827 | if (!png_ptr) |
5828 | { | |
5829 | if (fp) fclose (fp); | |
5830 | UNGCPRO; | |
5831 | return 0; | |
5832 | } | |
5833 | ||
2e09fef1 EZ |
5834 | /* Casting return value avoids a GCC warning on W32. */ |
5835 | info_ptr = (png_infop)fn_png_create_info_struct (png_ptr); | |
4ef0d4d0 KS |
5836 | if (!info_ptr) |
5837 | { | |
5838 | fn_png_destroy_read_struct (&png_ptr, NULL, NULL); | |
5839 | if (fp) fclose (fp); | |
5840 | UNGCPRO; | |
5841 | return 0; | |
5842 | } | |
5843 | ||
2e09fef1 EZ |
5844 | /* Casting return value avoids a GCC warning on W32. */ |
5845 | end_info = (png_infop)fn_png_create_info_struct (png_ptr); | |
4ef0d4d0 KS |
5846 | if (!end_info) |
5847 | { | |
5848 | fn_png_destroy_read_struct (&png_ptr, &info_ptr, NULL); | |
5849 | if (fp) fclose (fp); | |
5850 | UNGCPRO; | |
5851 | return 0; | |
5852 | } | |
5853 | ||
5854 | /* Set error jump-back. We come back here when the PNG library | |
5855 | detects an error. */ | |
5856 | if (setjmp (png_ptr->jmpbuf)) | |
5857 | { | |
5858 | error: | |
5859 | if (png_ptr) | |
5860 | fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); | |
5861 | xfree (pixels); | |
5862 | xfree (rows); | |
5863 | if (fp) fclose (fp); | |
5864 | UNGCPRO; | |
5865 | return 0; | |
5866 | } | |
5867 | ||
5868 | /* Read image info. */ | |
5869 | if (!NILP (specified_data)) | |
5870 | fn_png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory); | |
5871 | else | |
9a22a004 | 5872 | fn_png_set_read_fn (png_ptr, (void *) fp, png_read_from_file); |
4ef0d4d0 KS |
5873 | |
5874 | fn_png_set_sig_bytes (png_ptr, sizeof sig); | |
5875 | fn_png_read_info (png_ptr, info_ptr); | |
5876 | fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, | |
5877 | &interlace_type, NULL, NULL); | |
5878 | ||
f1f25b99 | 5879 | if (!check_image_size (f, width, height)) |
10ea2b82 JR |
5880 | { |
5881 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
5882 | goto error; | |
5883 | } | |
4ef0d4d0 KS |
5884 | /* If image contains simply transparency data, we prefer to |
5885 | construct a clipping mask. */ | |
5886 | if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) | |
5887 | transparent_p = 1; | |
5888 | else | |
5889 | transparent_p = 0; | |
5890 | ||
5891 | /* This function is easier to write if we only have to handle | |
5892 | one data format: RGB or RGBA with 8 bits per channel. Let's | |
5893 | transform other formats into that format. */ | |
5894 | ||
5895 | /* Strip more than 8 bits per channel. */ | |
5896 | if (bit_depth == 16) | |
5897 | fn_png_set_strip_16 (png_ptr); | |
5898 | ||
5899 | /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel | |
5900 | if available. */ | |
5901 | fn_png_set_expand (png_ptr); | |
5902 | ||
5903 | /* Convert grayscale images to RGB. */ | |
5904 | if (color_type == PNG_COLOR_TYPE_GRAY | |
5905 | || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | |
5906 | fn_png_set_gray_to_rgb (png_ptr); | |
5907 | ||
4ef0d4d0 KS |
5908 | /* Handle alpha channel by combining the image with a background |
5909 | color. Do this only if a real alpha channel is supplied. For | |
5910 | simple transparency, we prefer a clipping mask. */ | |
5911 | if (!transparent_p) | |
5912 | { | |
917ac1a9 | 5913 | /* png_color_16 *image_bg; */ |
4ef0d4d0 KS |
5914 | Lisp_Object specified_bg |
5915 | = image_spec_value (img->spec, QCbackground, NULL); | |
c4f13f44 | 5916 | int shift = (bit_depth == 16) ? 0 : 8; |
4ef0d4d0 KS |
5917 | |
5918 | if (STRINGP (specified_bg)) | |
5919 | /* The user specified `:background', use that. */ | |
5920 | { | |
4ef0d4d0 KS |
5921 | XColor color; |
5922 | if (x_defined_color (f, SDATA (specified_bg), &color, 0)) | |
5923 | { | |
5924 | png_color_16 user_bg; | |
5925 | ||
5926 | bzero (&user_bg, sizeof user_bg); | |
c4f13f44 CY |
5927 | user_bg.red = color.red >> shift; |
5928 | user_bg.green = color.green >> shift; | |
5929 | user_bg.blue = color.blue >> shift; | |
4ef0d4d0 KS |
5930 | |
5931 | fn_png_set_background (png_ptr, &user_bg, | |
5932 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
5933 | } | |
5934 | } | |
4ef0d4d0 KS |
5935 | else |
5936 | { | |
c4f13f44 CY |
5937 | /* We use the current frame background, ignoring any default |
5938 | background color set by the image. */ | |
8b7d0a16 | 5939 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
4ef0d4d0 KS |
5940 | XColor color; |
5941 | png_color_16 frame_background; | |
5942 | ||
5943 | color.pixel = FRAME_BACKGROUND_PIXEL (f); | |
5944 | x_query_color (f, &color); | |
5945 | ||
5946 | bzero (&frame_background, sizeof frame_background); | |
c4f13f44 CY |
5947 | frame_background.red = color.red >> shift; |
5948 | frame_background.green = color.green >> shift; | |
5949 | frame_background.blue = color.blue >> shift; | |
4ef0d4d0 KS |
5950 | #endif /* HAVE_X_WINDOWS */ |
5951 | ||
4ef0d4d0 KS |
5952 | fn_png_set_background (png_ptr, &frame_background, |
5953 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
5954 | } | |
5955 | } | |
5956 | ||
5957 | /* Update info structure. */ | |
5958 | fn_png_read_update_info (png_ptr, info_ptr); | |
5959 | ||
5960 | /* Get number of channels. Valid values are 1 for grayscale images | |
5961 | and images with a palette, 2 for grayscale images with transparency | |
5962 | information (alpha channel), 3 for RGB images, and 4 for RGB | |
5963 | images with alpha channel, i.e. RGBA. If conversions above were | |
5964 | sufficient we should only have 3 or 4 channels here. */ | |
5965 | channels = fn_png_get_channels (png_ptr, info_ptr); | |
5966 | xassert (channels == 3 || channels == 4); | |
5967 | ||
5968 | /* Number of bytes needed for one row of the image. */ | |
5969 | row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr); | |
5970 | ||
5971 | /* Allocate memory for the image. */ | |
5972 | pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels); | |
5973 | rows = (png_byte **) xmalloc (height * sizeof *rows); | |
5974 | for (i = 0; i < height; ++i) | |
5975 | rows[i] = pixels + i * row_bytes; | |
5976 | ||
5977 | /* Read the entire image. */ | |
5978 | fn_png_read_image (png_ptr, rows); | |
5979 | fn_png_read_end (png_ptr, info_ptr); | |
5980 | if (fp) | |
5981 | { | |
5982 | fclose (fp); | |
5983 | fp = NULL; | |
5984 | } | |
5985 | ||
5986 | /* Create the X image and pixmap. */ | |
5987 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, | |
5988 | &img->pixmap)) | |
5989 | goto error; | |
5990 | ||
5991 | /* Create an image and pixmap serving as mask if the PNG image | |
5992 | contains an alpha channel. */ | |
5993 | if (channels == 4 | |
5994 | && !transparent_p | |
5995 | && !x_create_x_image_and_pixmap (f, width, height, 1, | |
5996 | &mask_img, &img->mask)) | |
5997 | { | |
5998 | x_destroy_x_image (ximg); | |
5999 | Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap); | |
6000 | img->pixmap = NO_PIXMAP; | |
6001 | goto error; | |
6002 | } | |
6003 | ||
6004 | /* Fill the X image and mask from PNG data. */ | |
6005 | init_color_table (); | |
6006 | ||
6007 | for (y = 0; y < height; ++y) | |
6008 | { | |
6009 | png_byte *p = rows[y]; | |
6010 | ||
6011 | for (x = 0; x < width; ++x) | |
6012 | { | |
6013 | unsigned r, g, b; | |
6014 | ||
6015 | r = *p++ << 8; | |
6016 | g = *p++ << 8; | |
6017 | b = *p++ << 8; | |
6018 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
6019 | /* An alpha channel, aka mask channel, associates variable | |
6020 | transparency with an image. Where other image formats | |
6021 | support binary transparency---fully transparent or fully | |
6022 | opaque---PNG allows up to 254 levels of partial transparency. | |
6023 | The PNG library implements partial transparency by combining | |
6024 | the image with a specified background color. | |
6025 | ||
6026 | I'm not sure how to handle this here nicely: because the | |
6027 | background on which the image is displayed may change, for | |
6028 | real alpha channel support, it would be necessary to create | |
6029 | a new image for each possible background. | |
6030 | ||
6031 | What I'm doing now is that a mask is created if we have | |
6032 | boolean transparency information. Otherwise I'm using | |
6033 | the frame's background color to combine the image with. */ | |
6034 | ||
6035 | if (channels == 4) | |
6036 | { | |
6037 | if (mask_img) | |
9ecf6403 | 6038 | XPutPixel (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN); |
4ef0d4d0 KS |
6039 | ++p; |
6040 | } | |
6041 | } | |
6042 | } | |
6043 | ||
6044 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
6045 | /* Set IMG's background color from the PNG image, unless the user | |
6046 | overrode it. */ | |
6047 | { | |
6048 | png_color_16 *bg; | |
6049 | if (fn_png_get_bKGD (png_ptr, info_ptr, &bg)) | |
6050 | { | |
6051 | img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue); | |
6052 | img->background_valid = 1; | |
6053 | } | |
6054 | } | |
6055 | ||
6056 | #ifdef COLOR_TABLE_SUPPORT | |
6057 | /* Remember colors allocated for this image. */ | |
6058 | img->colors = colors_in_color_table (&img->ncolors); | |
6059 | free_color_table (); | |
6060 | #endif /* COLOR_TABLE_SUPPORT */ | |
6061 | ||
6062 | /* Clean up. */ | |
6063 | fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); | |
6064 | xfree (rows); | |
6065 | xfree (pixels); | |
6066 | ||
6067 | img->width = width; | |
6068 | img->height = height; | |
6069 | ||
2e09fef1 EZ |
6070 | /* Maybe fill in the background field while we have ximg handy. |
6071 | Casting avoids a GCC warning. */ | |
6072 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
6073 | |
6074 | /* Put the image into the pixmap, then free the X image and its buffer. */ | |
6075 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
6076 | x_destroy_x_image (ximg); | |
6077 | ||
6078 | /* Same for the mask. */ | |
6079 | if (mask_img) | |
6080 | { | |
2e09fef1 EZ |
6081 | /* Fill in the background_transparent field while we have the |
6082 | mask handy. Casting avoids a GCC warning. */ | |
6083 | image_background_transparent (img, f, (XImagePtr_or_DC)mask_img); | |
4ef0d4d0 KS |
6084 | |
6085 | x_put_x_image (f, mask_img, img->mask, img->width, img->height); | |
6086 | x_destroy_x_image (mask_img); | |
6087 | } | |
6088 | ||
6089 | UNGCPRO; | |
6090 | return 1; | |
6091 | } | |
6092 | ||
6093 | #else /* HAVE_PNG */ | |
6094 | ||
edfda783 AR |
6095 | #ifdef HAVE_NS |
6096 | static int | |
6097 | png_load (struct frame *f, struct image *img) | |
6098 | { | |
6099 | return ns_load_image(f, img, | |
6100 | image_spec_value (img->spec, QCfile, NULL), | |
6101 | image_spec_value (img->spec, QCdata, NULL)); | |
6102 | } | |
6103 | #endif /* HAVE_NS */ | |
6104 | ||
6105 | ||
4ef0d4d0 KS |
6106 | #endif /* !HAVE_PNG */ |
6107 | ||
6108 | ||
6109 | \f | |
6110 | /*********************************************************************** | |
6111 | JPEG | |
6112 | ***********************************************************************/ | |
6113 | ||
9e2a2647 | 6114 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
4ef0d4d0 KS |
6115 | |
6116 | static int jpeg_image_p P_ ((Lisp_Object object)); | |
6117 | static int jpeg_load P_ ((struct frame *f, struct image *img)); | |
6118 | ||
6119 | /* The symbol `jpeg' identifying images of this type. */ | |
6120 | ||
6121 | Lisp_Object Qjpeg; | |
6122 | ||
6123 | /* Indices of image specification fields in gs_format, below. */ | |
6124 | ||
6125 | enum jpeg_keyword_index | |
6126 | { | |
6127 | JPEG_TYPE, | |
6128 | JPEG_DATA, | |
6129 | JPEG_FILE, | |
6130 | JPEG_ASCENT, | |
6131 | JPEG_MARGIN, | |
6132 | JPEG_RELIEF, | |
6133 | JPEG_ALGORITHM, | |
6134 | JPEG_HEURISTIC_MASK, | |
6135 | JPEG_MASK, | |
6136 | JPEG_BACKGROUND, | |
6137 | JPEG_LAST | |
6138 | }; | |
6139 | ||
6140 | /* Vector of image_keyword structures describing the format | |
6141 | of valid user-defined image specifications. */ | |
6142 | ||
91433552 | 6143 | static const struct image_keyword jpeg_format[JPEG_LAST] = |
4ef0d4d0 KS |
6144 | { |
6145 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6146 | {":data", IMAGE_STRING_VALUE, 0}, | |
6147 | {":file", IMAGE_STRING_VALUE, 0}, | |
6148 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
6149 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
6150 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
6151 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6152 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6153 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6154 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
6155 | }; | |
6156 | ||
6157 | /* Structure describing the image type `jpeg'. */ | |
6158 | ||
6159 | static struct image_type jpeg_type = | |
6160 | { | |
6161 | &Qjpeg, | |
6162 | jpeg_image_p, | |
6163 | jpeg_load, | |
6164 | x_clear_image, | |
6165 | NULL | |
6166 | }; | |
6167 | ||
6168 | /* Return non-zero if OBJECT is a valid JPEG image specification. */ | |
6169 | ||
6170 | static int | |
6171 | jpeg_image_p (object) | |
6172 | Lisp_Object object; | |
6173 | { | |
6174 | struct image_keyword fmt[JPEG_LAST]; | |
6175 | ||
6176 | bcopy (jpeg_format, fmt, sizeof fmt); | |
6177 | ||
6178 | if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)) | |
6179 | return 0; | |
6180 | ||
6181 | /* Must specify either the :data or :file keyword. */ | |
6182 | return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1; | |
6183 | } | |
6184 | ||
9e2a2647 | 6185 | #endif /* HAVE_JPEG || HAVE_NS */ |
4ef0d4d0 KS |
6186 | |
6187 | #ifdef HAVE_JPEG | |
6188 | ||
6189 | /* Work around a warning about HAVE_STDLIB_H being redefined in | |
6190 | jconfig.h. */ | |
6191 | #ifdef HAVE_STDLIB_H | |
6192 | #define HAVE_STDLIB_H_1 | |
6193 | #undef HAVE_STDLIB_H | |
6194 | #endif /* HAVE_STLIB_H */ | |
6195 | ||
66354420 | 6196 | #if defined (HAVE_NTGUI) && !defined (__WIN32__) |
28893db2 JB |
6197 | /* In older releases of the jpeg library, jpeglib.h will define boolean |
6198 | differently depending on __WIN32__, so make sure it is defined. */ | |
66354420 JR |
6199 | #define __WIN32__ 1 |
6200 | #endif | |
6201 | ||
4ef0d4d0 KS |
6202 | #include <jpeglib.h> |
6203 | #include <jerror.h> | |
4ef0d4d0 KS |
6204 | |
6205 | #ifdef HAVE_STLIB_H_1 | |
6206 | #define HAVE_STDLIB_H 1 | |
6207 | #endif | |
6208 | ||
6209 | #ifdef HAVE_NTGUI | |
6210 | ||
6211 | /* JPEG library details. */ | |
6212 | DEF_IMGLIB_FN (jpeg_CreateDecompress); | |
6213 | DEF_IMGLIB_FN (jpeg_start_decompress); | |
6214 | DEF_IMGLIB_FN (jpeg_finish_decompress); | |
6215 | DEF_IMGLIB_FN (jpeg_destroy_decompress); | |
6216 | DEF_IMGLIB_FN (jpeg_read_header); | |
6217 | DEF_IMGLIB_FN (jpeg_read_scanlines); | |
4ef0d4d0 KS |
6218 | DEF_IMGLIB_FN (jpeg_std_error); |
6219 | DEF_IMGLIB_FN (jpeg_resync_to_restart); | |
6220 | ||
6221 | static int | |
0855eb52 | 6222 | init_jpeg_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
6223 | { |
6224 | HMODULE library; | |
6225 | ||
0855eb52 | 6226 | if (!(library = w32_delayed_load (libraries, Qjpeg))) |
4ef0d4d0 KS |
6227 | return 0; |
6228 | ||
6229 | LOAD_IMGLIB_FN (library, jpeg_finish_decompress); | |
6230 | LOAD_IMGLIB_FN (library, jpeg_read_scanlines); | |
6231 | LOAD_IMGLIB_FN (library, jpeg_start_decompress); | |
6232 | LOAD_IMGLIB_FN (library, jpeg_read_header); | |
4ef0d4d0 KS |
6233 | LOAD_IMGLIB_FN (library, jpeg_CreateDecompress); |
6234 | LOAD_IMGLIB_FN (library, jpeg_destroy_decompress); | |
6235 | LOAD_IMGLIB_FN (library, jpeg_std_error); | |
6236 | LOAD_IMGLIB_FN (library, jpeg_resync_to_restart); | |
6237 | return 1; | |
6238 | } | |
6239 | ||
6240 | /* Wrapper since we can't directly assign the function pointer | |
6241 | to another function pointer that was declared more completely easily. */ | |
6242 | static boolean | |
5fc9fdeb | 6243 | jpeg_resync_to_restart_wrapper (cinfo, desired) |
4ef0d4d0 KS |
6244 | j_decompress_ptr cinfo; |
6245 | int desired; | |
6246 | { | |
6247 | return fn_jpeg_resync_to_restart (cinfo, desired); | |
6248 | } | |
6249 | ||
6250 | #else | |
6251 | ||
6252 | #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress(a) | |
6253 | #define fn_jpeg_start_decompress jpeg_start_decompress | |
6254 | #define fn_jpeg_finish_decompress jpeg_finish_decompress | |
6255 | #define fn_jpeg_destroy_decompress jpeg_destroy_decompress | |
6256 | #define fn_jpeg_read_header jpeg_read_header | |
6257 | #define fn_jpeg_read_scanlines jpeg_read_scanlines | |
4ef0d4d0 KS |
6258 | #define fn_jpeg_std_error jpeg_std_error |
6259 | #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart | |
6260 | ||
6261 | #endif /* HAVE_NTGUI */ | |
6262 | ||
6263 | struct my_jpeg_error_mgr | |
6264 | { | |
6265 | struct jpeg_error_mgr pub; | |
6266 | jmp_buf setjmp_buffer; | |
6267 | }; | |
6268 | ||
6269 | ||
6270 | static void | |
6271 | my_error_exit (cinfo) | |
6272 | j_common_ptr cinfo; | |
6273 | { | |
6274 | struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err; | |
6275 | longjmp (mgr->setjmp_buffer, 1); | |
6276 | } | |
6277 | ||
6278 | ||
6279 | /* Init source method for JPEG data source manager. Called by | |
6280 | jpeg_read_header() before any data is actually read. See | |
6281 | libjpeg.doc from the JPEG lib distribution. */ | |
6282 | ||
6283 | static void | |
b25a72ab JB |
6284 | our_common_init_source (cinfo) |
6285 | j_decompress_ptr cinfo; | |
6286 | { | |
6287 | } | |
6288 | ||
6289 | ||
6290 | /* Method to terminate data source. Called by | |
6291 | jpeg_finish_decompress() after all data has been processed. */ | |
6292 | ||
6293 | static void | |
6294 | our_common_term_source (cinfo) | |
4ef0d4d0 KS |
6295 | j_decompress_ptr cinfo; |
6296 | { | |
6297 | } | |
6298 | ||
6299 | ||
6300 | /* Fill input buffer method for JPEG data source manager. Called | |
6301 | whenever more data is needed. We read the whole image in one step, | |
6302 | so this only adds a fake end of input marker at the end. */ | |
6303 | ||
5a1539f3 | 6304 | static JOCTET our_memory_buffer[2]; |
aca946f3 | 6305 | |
4ef0d4d0 | 6306 | static boolean |
b25a72ab | 6307 | our_memory_fill_input_buffer (cinfo) |
4ef0d4d0 KS |
6308 | j_decompress_ptr cinfo; |
6309 | { | |
6310 | /* Insert a fake EOI marker. */ | |
6311 | struct jpeg_source_mgr *src = cinfo->src; | |
4ef0d4d0 | 6312 | |
5a1539f3 MB |
6313 | our_memory_buffer[0] = (JOCTET) 0xFF; |
6314 | our_memory_buffer[1] = (JOCTET) JPEG_EOI; | |
4ef0d4d0 | 6315 | |
5a1539f3 | 6316 | src->next_input_byte = our_memory_buffer; |
4ef0d4d0 | 6317 | src->bytes_in_buffer = 2; |
e0e30823 | 6318 | return 1; |
4ef0d4d0 KS |
6319 | } |
6320 | ||
6321 | ||
6322 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6323 | is the JPEG data source manager. */ | |
6324 | ||
6325 | static void | |
b25a72ab | 6326 | our_memory_skip_input_data (cinfo, num_bytes) |
4ef0d4d0 KS |
6327 | j_decompress_ptr cinfo; |
6328 | long num_bytes; | |
6329 | { | |
6330 | struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src; | |
6331 | ||
6332 | if (src) | |
6333 | { | |
6334 | if (num_bytes > src->bytes_in_buffer) | |
6335 | ERREXIT (cinfo, JERR_INPUT_EOF); | |
6336 | ||
6337 | src->bytes_in_buffer -= num_bytes; | |
6338 | src->next_input_byte += num_bytes; | |
6339 | } | |
6340 | } | |
6341 | ||
6342 | ||
4ef0d4d0 KS |
6343 | /* Set up the JPEG lib for reading an image from DATA which contains |
6344 | LEN bytes. CINFO is the decompression info structure created for | |
6345 | reading the image. */ | |
6346 | ||
6347 | static void | |
6348 | jpeg_memory_src (cinfo, data, len) | |
6349 | j_decompress_ptr cinfo; | |
6350 | JOCTET *data; | |
6351 | unsigned int len; | |
6352 | { | |
6353 | struct jpeg_source_mgr *src; | |
6354 | ||
6355 | if (cinfo->src == NULL) | |
6356 | { | |
6357 | /* First time for this JPEG object? */ | |
6358 | cinfo->src = (struct jpeg_source_mgr *) | |
6359 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6360 | sizeof (struct jpeg_source_mgr)); | |
6361 | src = (struct jpeg_source_mgr *) cinfo->src; | |
6362 | src->next_input_byte = data; | |
6363 | } | |
6364 | ||
6365 | src = (struct jpeg_source_mgr *) cinfo->src; | |
b25a72ab JB |
6366 | src->init_source = our_common_init_source; |
6367 | src->fill_input_buffer = our_memory_fill_input_buffer; | |
6368 | src->skip_input_data = our_memory_skip_input_data; | |
4ef0d4d0 | 6369 | src->resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ |
b25a72ab | 6370 | src->term_source = our_common_term_source; |
4ef0d4d0 KS |
6371 | src->bytes_in_buffer = len; |
6372 | src->next_input_byte = data; | |
6373 | } | |
6374 | ||
6375 | ||
b25a72ab JB |
6376 | struct jpeg_stdio_mgr |
6377 | { | |
6378 | struct jpeg_source_mgr mgr; | |
6379 | boolean finished; | |
6380 | FILE *file; | |
6381 | JOCTET *buffer; | |
6382 | }; | |
6383 | ||
6384 | ||
6385 | /* Size of buffer to read JPEG from file. | |
6386 | Not too big, as we want to use alloc_small. */ | |
6387 | #define JPEG_STDIO_BUFFER_SIZE 8192 | |
6388 | ||
6389 | ||
6390 | /* Fill input buffer method for JPEG data source manager. Called | |
6391 | whenever more data is needed. The data is read from a FILE *. */ | |
6392 | ||
6393 | static boolean | |
6394 | our_stdio_fill_input_buffer (cinfo) | |
6395 | j_decompress_ptr cinfo; | |
6396 | { | |
6397 | struct jpeg_stdio_mgr *src; | |
6398 | ||
6399 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6400 | if (!src->finished) | |
6401 | { | |
6402 | size_t bytes; | |
6403 | ||
6404 | bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file); | |
6405 | if (bytes > 0) | |
6406 | src->mgr.bytes_in_buffer = bytes; | |
6407 | else | |
6408 | { | |
6409 | WARNMS (cinfo, JWRN_JPEG_EOF); | |
6410 | src->finished = 1; | |
6411 | src->buffer[0] = (JOCTET) 0xFF; | |
6412 | src->buffer[1] = (JOCTET) JPEG_EOI; | |
6413 | src->mgr.bytes_in_buffer = 2; | |
6414 | } | |
6415 | src->mgr.next_input_byte = src->buffer; | |
6416 | } | |
6417 | ||
6418 | return 1; | |
6419 | } | |
6420 | ||
6421 | ||
6422 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6423 | is the JPEG data source manager. */ | |
6424 | ||
6425 | static void | |
6426 | our_stdio_skip_input_data (cinfo, num_bytes) | |
6427 | j_decompress_ptr cinfo; | |
6428 | long num_bytes; | |
6429 | { | |
6430 | struct jpeg_stdio_mgr *src; | |
6431 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6432 | ||
6433 | while (num_bytes > 0 && !src->finished) | |
6434 | { | |
6435 | if (num_bytes <= src->mgr.bytes_in_buffer) | |
6436 | { | |
6437 | src->mgr.bytes_in_buffer -= num_bytes; | |
6438 | src->mgr.next_input_byte += num_bytes; | |
6439 | break; | |
6440 | } | |
6441 | else | |
6442 | { | |
6443 | num_bytes -= src->mgr.bytes_in_buffer; | |
6444 | src->mgr.bytes_in_buffer = 0; | |
6445 | src->mgr.next_input_byte = NULL; | |
6446 | ||
6447 | our_stdio_fill_input_buffer (cinfo); | |
6448 | } | |
6449 | } | |
6450 | } | |
6451 | ||
6452 | ||
6453 | /* Set up the JPEG lib for reading an image from a FILE *. | |
6454 | CINFO is the decompression info structure created for | |
6455 | reading the image. */ | |
6456 | ||
6457 | static void | |
6458 | jpeg_file_src (cinfo, fp) | |
6459 | j_decompress_ptr cinfo; | |
6460 | FILE *fp; | |
6461 | { | |
6462 | struct jpeg_stdio_mgr *src; | |
6463 | ||
6464 | if (cinfo->src != NULL) | |
6465 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6466 | else | |
6467 | { | |
6468 | /* First time for this JPEG object? */ | |
6469 | cinfo->src = (struct jpeg_source_mgr *) | |
6470 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6471 | sizeof (struct jpeg_stdio_mgr)); | |
6472 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6473 | src->buffer = (JOCTET *) | |
6474 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6475 | JPEG_STDIO_BUFFER_SIZE); | |
6476 | } | |
6477 | ||
6478 | src->file = fp; | |
6479 | src->finished = 0; | |
6480 | src->mgr.init_source = our_common_init_source; | |
6481 | src->mgr.fill_input_buffer = our_stdio_fill_input_buffer; | |
6482 | src->mgr.skip_input_data = our_stdio_skip_input_data; | |
6483 | src->mgr.resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ | |
6484 | src->mgr.term_source = our_common_term_source; | |
6485 | src->mgr.bytes_in_buffer = 0; | |
6486 | src->mgr.next_input_byte = NULL; | |
6487 | } | |
6488 | ||
6489 | ||
4ef0d4d0 KS |
6490 | /* Load image IMG for use on frame F. Patterned after example.c |
6491 | from the JPEG lib. */ | |
6492 | ||
6493 | static int | |
6494 | jpeg_load (f, img) | |
6495 | struct frame *f; | |
6496 | struct image *img; | |
6497 | { | |
6498 | struct jpeg_decompress_struct cinfo; | |
6499 | struct my_jpeg_error_mgr mgr; | |
6500 | Lisp_Object file, specified_file; | |
6501 | Lisp_Object specified_data; | |
6502 | FILE * volatile fp = NULL; | |
6503 | JSAMPARRAY buffer; | |
6504 | int row_stride, x, y; | |
6505 | XImagePtr ximg = NULL; | |
6506 | int rc; | |
6507 | unsigned long *colors; | |
6508 | int width, height; | |
6509 | struct gcpro gcpro1; | |
6510 | ||
6511 | /* Open the JPEG file. */ | |
6512 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6513 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
6514 | file = Qnil; | |
6515 | GCPRO1 (file); | |
6516 | ||
6517 | if (NILP (specified_data)) | |
6518 | { | |
6519 | file = x_find_image_file (specified_file); | |
6520 | if (!STRINGP (file)) | |
6521 | { | |
6522 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
6523 | UNGCPRO; | |
6524 | return 0; | |
6525 | } | |
6526 | ||
6527 | fp = fopen (SDATA (file), "rb"); | |
6528 | if (fp == NULL) | |
6529 | { | |
6530 | image_error ("Cannot open `%s'", file, Qnil); | |
6531 | UNGCPRO; | |
6532 | return 0; | |
6533 | } | |
6534 | } | |
6535 | ||
6536 | /* Customize libjpeg's error handling to call my_error_exit when an | |
2e09fef1 EZ |
6537 | error is detected. This function will perform a longjmp. |
6538 | Casting return value avoids a GCC warning on W32. */ | |
6539 | cinfo.err = (struct jpeg_error_mgr *)fn_jpeg_std_error (&mgr.pub); | |
4ef0d4d0 KS |
6540 | mgr.pub.error_exit = my_error_exit; |
6541 | ||
6542 | if ((rc = setjmp (mgr.setjmp_buffer)) != 0) | |
6543 | { | |
6544 | if (rc == 1) | |
6545 | { | |
6546 | /* Called from my_error_exit. Display a JPEG error. */ | |
6547 | char buffer[JMSG_LENGTH_MAX]; | |
6548 | cinfo.err->format_message ((j_common_ptr) &cinfo, buffer); | |
6549 | image_error ("Error reading JPEG image `%s': %s", img->spec, | |
6550 | build_string (buffer)); | |
6551 | } | |
6552 | ||
6553 | /* Close the input file and destroy the JPEG object. */ | |
6554 | if (fp) | |
6555 | fclose ((FILE *) fp); | |
6556 | fn_jpeg_destroy_decompress (&cinfo); | |
6557 | ||
6558 | /* If we already have an XImage, free that. */ | |
6559 | x_destroy_x_image (ximg); | |
6560 | ||
6561 | /* Free pixmap and colors. */ | |
6562 | x_clear_image (f, img); | |
6563 | ||
6564 | UNGCPRO; | |
6565 | return 0; | |
6566 | } | |
6567 | ||
6568 | /* Create the JPEG decompression object. Let it read from fp. | |
6569 | Read the JPEG image header. */ | |
6570 | fn_jpeg_CreateDecompress (&cinfo, JPEG_LIB_VERSION, sizeof (cinfo)); | |
6571 | ||
6572 | if (NILP (specified_data)) | |
b25a72ab | 6573 | jpeg_file_src (&cinfo, (FILE *) fp); |
4ef0d4d0 KS |
6574 | else |
6575 | jpeg_memory_src (&cinfo, SDATA (specified_data), | |
6576 | SBYTES (specified_data)); | |
6577 | ||
e0e30823 | 6578 | fn_jpeg_read_header (&cinfo, 1); |
4ef0d4d0 KS |
6579 | |
6580 | /* Customize decompression so that color quantization will be used. | |
6581 | Start decompression. */ | |
e0e30823 | 6582 | cinfo.quantize_colors = 1; |
4ef0d4d0 KS |
6583 | fn_jpeg_start_decompress (&cinfo); |
6584 | width = img->width = cinfo.output_width; | |
6585 | height = img->height = cinfo.output_height; | |
6586 | ||
f1f25b99 CY |
6587 | if (!check_image_size (f, width, height)) |
6588 | { | |
10ea2b82 | 6589 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
6590 | longjmp (mgr.setjmp_buffer, 2); |
6591 | } | |
6592 | ||
4ef0d4d0 KS |
6593 | /* Create X image and pixmap. */ |
6594 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) | |
6595 | longjmp (mgr.setjmp_buffer, 2); | |
6596 | ||
6597 | /* Allocate colors. When color quantization is used, | |
6598 | cinfo.actual_number_of_colors has been set with the number of | |
6599 | colors generated, and cinfo.colormap is a two-dimensional array | |
6600 | of color indices in the range 0..cinfo.actual_number_of_colors. | |
6601 | No more than 255 colors will be generated. */ | |
6602 | { | |
6603 | int i, ir, ig, ib; | |
6604 | ||
6605 | if (cinfo.out_color_components > 2) | |
6606 | ir = 0, ig = 1, ib = 2; | |
6607 | else if (cinfo.out_color_components > 1) | |
6608 | ir = 0, ig = 1, ib = 0; | |
6609 | else | |
6610 | ir = 0, ig = 0, ib = 0; | |
6611 | ||
6612 | /* Use the color table mechanism because it handles colors that | |
6613 | cannot be allocated nicely. Such colors will be replaced with | |
6614 | a default color, and we don't have to care about which colors | |
6615 | can be freed safely, and which can't. */ | |
6616 | init_color_table (); | |
6617 | colors = (unsigned long *) alloca (cinfo.actual_number_of_colors | |
6618 | * sizeof *colors); | |
6619 | ||
6620 | for (i = 0; i < cinfo.actual_number_of_colors; ++i) | |
6621 | { | |
6622 | /* Multiply RGB values with 255 because X expects RGB values | |
6623 | in the range 0..0xffff. */ | |
6624 | int r = cinfo.colormap[ir][i] << 8; | |
6625 | int g = cinfo.colormap[ig][i] << 8; | |
6626 | int b = cinfo.colormap[ib][i] << 8; | |
6627 | colors[i] = lookup_rgb_color (f, r, g, b); | |
6628 | } | |
6629 | ||
6630 | #ifdef COLOR_TABLE_SUPPORT | |
6631 | /* Remember those colors actually allocated. */ | |
6632 | img->colors = colors_in_color_table (&img->ncolors); | |
6633 | free_color_table (); | |
6634 | #endif /* COLOR_TABLE_SUPPORT */ | |
6635 | } | |
6636 | ||
6637 | /* Read pixels. */ | |
6638 | row_stride = width * cinfo.output_components; | |
6639 | buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE, | |
6640 | row_stride, 1); | |
6641 | for (y = 0; y < height; ++y) | |
6642 | { | |
6643 | fn_jpeg_read_scanlines (&cinfo, buffer, 1); | |
6644 | for (x = 0; x < cinfo.output_width; ++x) | |
6645 | XPutPixel (ximg, x, y, colors[buffer[0][x]]); | |
6646 | } | |
6647 | ||
6648 | /* Clean up. */ | |
6649 | fn_jpeg_finish_decompress (&cinfo); | |
6650 | fn_jpeg_destroy_decompress (&cinfo); | |
6651 | if (fp) | |
6652 | fclose ((FILE *) fp); | |
6653 | ||
6654 | /* Maybe fill in the background field while we have ximg handy. */ | |
6655 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
6656 | /* Casting avoids a GCC warning. */ |
6657 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
6658 | |
6659 | /* Put the image into the pixmap. */ | |
6660 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
6661 | x_destroy_x_image (ximg); | |
6662 | UNGCPRO; | |
6663 | return 1; | |
6664 | } | |
6665 | ||
6666 | #else /* HAVE_JPEG */ | |
6667 | ||
edfda783 AR |
6668 | #ifdef HAVE_NS |
6669 | static int | |
6670 | jpeg_load (struct frame *f, struct image *img) | |
6671 | { | |
6672 | return ns_load_image(f, img, | |
6673 | image_spec_value (img->spec, QCfile, NULL), | |
6674 | image_spec_value (img->spec, QCdata, NULL)); | |
6675 | } | |
6676 | #endif /* HAVE_NS */ | |
6677 | ||
4ef0d4d0 KS |
6678 | #endif /* !HAVE_JPEG */ |
6679 | ||
6680 | ||
6681 | \f | |
6682 | /*********************************************************************** | |
6683 | TIFF | |
6684 | ***********************************************************************/ | |
6685 | ||
9e2a2647 | 6686 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
4ef0d4d0 KS |
6687 | |
6688 | static int tiff_image_p P_ ((Lisp_Object object)); | |
6689 | static int tiff_load P_ ((struct frame *f, struct image *img)); | |
6690 | ||
6691 | /* The symbol `tiff' identifying images of this type. */ | |
6692 | ||
6693 | Lisp_Object Qtiff; | |
6694 | ||
6695 | /* Indices of image specification fields in tiff_format, below. */ | |
6696 | ||
6697 | enum tiff_keyword_index | |
6698 | { | |
6699 | TIFF_TYPE, | |
6700 | TIFF_DATA, | |
6701 | TIFF_FILE, | |
6702 | TIFF_ASCENT, | |
6703 | TIFF_MARGIN, | |
6704 | TIFF_RELIEF, | |
6705 | TIFF_ALGORITHM, | |
6706 | TIFF_HEURISTIC_MASK, | |
6707 | TIFF_MASK, | |
6708 | TIFF_BACKGROUND, | |
58279503 | 6709 | TIFF_INDEX, |
4ef0d4d0 KS |
6710 | TIFF_LAST |
6711 | }; | |
6712 | ||
6713 | /* Vector of image_keyword structures describing the format | |
6714 | of valid user-defined image specifications. */ | |
6715 | ||
91433552 | 6716 | static const struct image_keyword tiff_format[TIFF_LAST] = |
4ef0d4d0 KS |
6717 | { |
6718 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6719 | {":data", IMAGE_STRING_VALUE, 0}, | |
6720 | {":file", IMAGE_STRING_VALUE, 0}, | |
6721 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
6722 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
6723 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
6724 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6725 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6726 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 JR |
6727 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, |
6728 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0} | |
4ef0d4d0 KS |
6729 | }; |
6730 | ||
6731 | /* Structure describing the image type `tiff'. */ | |
6732 | ||
6733 | static struct image_type tiff_type = | |
6734 | { | |
6735 | &Qtiff, | |
6736 | tiff_image_p, | |
6737 | tiff_load, | |
6738 | x_clear_image, | |
6739 | NULL | |
6740 | }; | |
6741 | ||
6742 | /* Return non-zero if OBJECT is a valid TIFF image specification. */ | |
6743 | ||
6744 | static int | |
6745 | tiff_image_p (object) | |
6746 | Lisp_Object object; | |
6747 | { | |
6748 | struct image_keyword fmt[TIFF_LAST]; | |
6749 | bcopy (tiff_format, fmt, sizeof fmt); | |
6750 | ||
6751 | if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff)) | |
6752 | return 0; | |
6753 | ||
6754 | /* Must specify either the :data or :file keyword. */ | |
6755 | return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1; | |
6756 | } | |
6757 | ||
9e2a2647 | 6758 | #endif /* HAVE_TIFF || HAVE_NS */ |
4ef0d4d0 KS |
6759 | |
6760 | #ifdef HAVE_TIFF | |
6761 | ||
6762 | #include <tiffio.h> | |
6763 | ||
6764 | #ifdef HAVE_NTGUI | |
6765 | ||
6766 | /* TIFF library details. */ | |
6767 | DEF_IMGLIB_FN (TIFFSetErrorHandler); | |
6768 | DEF_IMGLIB_FN (TIFFSetWarningHandler); | |
6769 | DEF_IMGLIB_FN (TIFFOpen); | |
6770 | DEF_IMGLIB_FN (TIFFClientOpen); | |
6771 | DEF_IMGLIB_FN (TIFFGetField); | |
6772 | DEF_IMGLIB_FN (TIFFReadRGBAImage); | |
6773 | DEF_IMGLIB_FN (TIFFClose); | |
58279503 | 6774 | DEF_IMGLIB_FN (TIFFSetDirectory); |
4ef0d4d0 KS |
6775 | |
6776 | static int | |
0855eb52 | 6777 | init_tiff_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
6778 | { |
6779 | HMODULE library; | |
6780 | ||
0855eb52 | 6781 | if (!(library = w32_delayed_load (libraries, Qtiff))) |
4ef0d4d0 KS |
6782 | return 0; |
6783 | ||
6784 | LOAD_IMGLIB_FN (library, TIFFSetErrorHandler); | |
6785 | LOAD_IMGLIB_FN (library, TIFFSetWarningHandler); | |
6786 | LOAD_IMGLIB_FN (library, TIFFOpen); | |
6787 | LOAD_IMGLIB_FN (library, TIFFClientOpen); | |
6788 | LOAD_IMGLIB_FN (library, TIFFGetField); | |
6789 | LOAD_IMGLIB_FN (library, TIFFReadRGBAImage); | |
6790 | LOAD_IMGLIB_FN (library, TIFFClose); | |
58279503 | 6791 | LOAD_IMGLIB_FN (library, TIFFSetDirectory); |
4ef0d4d0 KS |
6792 | return 1; |
6793 | } | |
6794 | ||
6795 | #else | |
6796 | ||
6797 | #define fn_TIFFSetErrorHandler TIFFSetErrorHandler | |
6798 | #define fn_TIFFSetWarningHandler TIFFSetWarningHandler | |
6799 | #define fn_TIFFOpen TIFFOpen | |
6800 | #define fn_TIFFClientOpen TIFFClientOpen | |
6801 | #define fn_TIFFGetField TIFFGetField | |
6802 | #define fn_TIFFReadRGBAImage TIFFReadRGBAImage | |
6803 | #define fn_TIFFClose TIFFClose | |
58279503 | 6804 | #define fn_TIFFSetDirectory TIFFSetDirectory |
4ef0d4d0 KS |
6805 | #endif /* HAVE_NTGUI */ |
6806 | ||
6807 | ||
6808 | /* Reading from a memory buffer for TIFF images Based on the PNG | |
6809 | memory source, but we have to provide a lot of extra functions. | |
6810 | Blah. | |
6811 | ||
6812 | We really only need to implement read and seek, but I am not | |
6813 | convinced that the TIFF library is smart enough not to destroy | |
6814 | itself if we only hand it the function pointers we need to | |
6815 | override. */ | |
6816 | ||
6817 | typedef struct | |
6818 | { | |
6819 | unsigned char *bytes; | |
6820 | size_t len; | |
6821 | int index; | |
6822 | } | |
6823 | tiff_memory_source; | |
6824 | ||
6825 | static size_t | |
6826 | tiff_read_from_memory (data, buf, size) | |
6827 | thandle_t data; | |
6828 | tdata_t buf; | |
6829 | tsize_t size; | |
6830 | { | |
6831 | tiff_memory_source *src = (tiff_memory_source *) data; | |
6832 | ||
6833 | if (size > src->len - src->index) | |
6834 | return (size_t) -1; | |
6835 | bcopy (src->bytes + src->index, buf, size); | |
6836 | src->index += size; | |
6837 | return size; | |
6838 | } | |
6839 | ||
6840 | static size_t | |
6841 | tiff_write_from_memory (data, buf, size) | |
6842 | thandle_t data; | |
6843 | tdata_t buf; | |
6844 | tsize_t size; | |
6845 | { | |
6846 | return (size_t) -1; | |
6847 | } | |
6848 | ||
6849 | static toff_t | |
6850 | tiff_seek_in_memory (data, off, whence) | |
6851 | thandle_t data; | |
6852 | toff_t off; | |
6853 | int whence; | |
6854 | { | |
6855 | tiff_memory_source *src = (tiff_memory_source *) data; | |
6856 | int idx; | |
6857 | ||
6858 | switch (whence) | |
6859 | { | |
6860 | case SEEK_SET: /* Go from beginning of source. */ | |
6861 | idx = off; | |
6862 | break; | |
6863 | ||
6864 | case SEEK_END: /* Go from end of source. */ | |
6865 | idx = src->len + off; | |
6866 | break; | |
6867 | ||
6868 | case SEEK_CUR: /* Go from current position. */ | |
6869 | idx = src->index + off; | |
6870 | break; | |
6871 | ||
6872 | default: /* Invalid `whence'. */ | |
6873 | return -1; | |
6874 | } | |
6875 | ||
6876 | if (idx > src->len || idx < 0) | |
6877 | return -1; | |
6878 | ||
6879 | src->index = idx; | |
6880 | return src->index; | |
6881 | } | |
6882 | ||
6883 | static int | |
6884 | tiff_close_memory (data) | |
6885 | thandle_t data; | |
6886 | { | |
6887 | /* NOOP */ | |
6888 | return 0; | |
6889 | } | |
6890 | ||
6891 | static int | |
6892 | tiff_mmap_memory (data, pbase, psize) | |
6893 | thandle_t data; | |
6894 | tdata_t *pbase; | |
6895 | toff_t *psize; | |
6896 | { | |
6897 | /* It is already _IN_ memory. */ | |
6898 | return 0; | |
6899 | } | |
6900 | ||
6901 | static void | |
6902 | tiff_unmap_memory (data, base, size) | |
6903 | thandle_t data; | |
6904 | tdata_t base; | |
6905 | toff_t size; | |
6906 | { | |
6907 | /* We don't need to do this. */ | |
6908 | } | |
6909 | ||
6910 | static toff_t | |
6911 | tiff_size_of_memory (data) | |
6912 | thandle_t data; | |
6913 | { | |
6914 | return ((tiff_memory_source *) data)->len; | |
6915 | } | |
6916 | ||
6917 | ||
6918 | static void | |
6919 | tiff_error_handler (title, format, ap) | |
6920 | const char *title, *format; | |
6921 | va_list ap; | |
6922 | { | |
6923 | char buf[512]; | |
6924 | int len; | |
6925 | ||
6926 | len = sprintf (buf, "TIFF error: %s ", title); | |
6927 | vsprintf (buf + len, format, ap); | |
6928 | add_to_log (buf, Qnil, Qnil); | |
6929 | } | |
6930 | ||
6931 | ||
6932 | static void | |
6933 | tiff_warning_handler (title, format, ap) | |
6934 | const char *title, *format; | |
6935 | va_list ap; | |
6936 | { | |
6937 | char buf[512]; | |
6938 | int len; | |
6939 | ||
6940 | len = sprintf (buf, "TIFF warning: %s ", title); | |
6941 | vsprintf (buf + len, format, ap); | |
6942 | add_to_log (buf, Qnil, Qnil); | |
6943 | } | |
6944 | ||
6945 | ||
6946 | /* Load TIFF image IMG for use on frame F. Value is non-zero if | |
6947 | successful. */ | |
6948 | ||
6949 | static int | |
6950 | tiff_load (f, img) | |
6951 | struct frame *f; | |
6952 | struct image *img; | |
6953 | { | |
6954 | Lisp_Object file, specified_file; | |
6955 | Lisp_Object specified_data; | |
6956 | TIFF *tiff; | |
58279503 | 6957 | int width, height, x, y, count; |
4ef0d4d0 | 6958 | uint32 *buf; |
58279503 | 6959 | int rc, rc2; |
4ef0d4d0 KS |
6960 | XImagePtr ximg; |
6961 | struct gcpro gcpro1; | |
6962 | tiff_memory_source memsrc; | |
58279503 | 6963 | Lisp_Object image; |
4ef0d4d0 KS |
6964 | |
6965 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6966 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
6967 | file = Qnil; | |
6968 | GCPRO1 (file); | |
6969 | ||
6970 | fn_TIFFSetErrorHandler (tiff_error_handler); | |
6971 | fn_TIFFSetWarningHandler (tiff_warning_handler); | |
6972 | ||
6973 | if (NILP (specified_data)) | |
6974 | { | |
6975 | /* Read from a file */ | |
6976 | file = x_find_image_file (specified_file); | |
6977 | if (!STRINGP (file)) | |
6978 | { | |
6979 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
6980 | UNGCPRO; | |
6981 | return 0; | |
6982 | } | |
6983 | ||
2e09fef1 EZ |
6984 | /* Try to open the image file. Casting return value avoids a |
6985 | GCC warning on W32. */ | |
6986 | tiff = (TIFF *)fn_TIFFOpen (SDATA (file), "r"); | |
4ef0d4d0 KS |
6987 | if (tiff == NULL) |
6988 | { | |
6989 | image_error ("Cannot open `%s'", file, Qnil); | |
6990 | UNGCPRO; | |
6991 | return 0; | |
6992 | } | |
6993 | } | |
6994 | else | |
6995 | { | |
6996 | /* Memory source! */ | |
6997 | memsrc.bytes = SDATA (specified_data); | |
6998 | memsrc.len = SBYTES (specified_data); | |
6999 | memsrc.index = 0; | |
7000 | ||
2e09fef1 EZ |
7001 | /* Casting return value avoids a GCC warning on W32. */ |
7002 | tiff = (TIFF *)fn_TIFFClientOpen ("memory_source", "r", &memsrc, | |
7003 | (TIFFReadWriteProc) tiff_read_from_memory, | |
7004 | (TIFFReadWriteProc) tiff_write_from_memory, | |
7005 | tiff_seek_in_memory, | |
7006 | tiff_close_memory, | |
7007 | tiff_size_of_memory, | |
7008 | tiff_mmap_memory, | |
7009 | tiff_unmap_memory); | |
4ef0d4d0 KS |
7010 | |
7011 | if (!tiff) | |
7012 | { | |
7013 | image_error ("Cannot open memory source for `%s'", img->spec, Qnil); | |
7014 | UNGCPRO; | |
7015 | return 0; | |
7016 | } | |
7017 | } | |
7018 | ||
58279503 JR |
7019 | image = image_spec_value (img->spec, QCindex, NULL); |
7020 | if (INTEGERP (image)) | |
7021 | { | |
7022 | int ino = XFASTINT (image); | |
7023 | if (!fn_TIFFSetDirectory (tiff, ino)) | |
7024 | { | |
7025 | image_error ("Invalid image number `%s' in image `%s'", | |
7026 | image, img->spec); | |
7027 | fn_TIFFClose (tiff); | |
7028 | UNGCPRO; | |
7029 | return 0; | |
7030 | } | |
7031 | } | |
7032 | ||
4ef0d4d0 KS |
7033 | /* Get width and height of the image, and allocate a raster buffer |
7034 | of width x height 32-bit values. */ | |
7035 | fn_TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width); | |
7036 | fn_TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height); | |
f1f25b99 CY |
7037 | |
7038 | if (!check_image_size (f, width, height)) | |
7039 | { | |
10ea2b82 | 7040 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
58279503 | 7041 | fn_TIFFClose (tiff); |
f1f25b99 CY |
7042 | UNGCPRO; |
7043 | return 0; | |
7044 | } | |
7045 | ||
4ef0d4d0 KS |
7046 | buf = (uint32 *) xmalloc (width * height * sizeof *buf); |
7047 | ||
7048 | rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0); | |
58279503 JR |
7049 | |
7050 | /* Count the number of images in the file. */ | |
7051 | for (count = 1, rc2 = 1; rc2; count++) | |
7052 | rc2 = fn_TIFFSetDirectory (tiff, count); | |
7053 | ||
7054 | if (count > 1) | |
7055 | img->data.lisp_val = Fcons (Qcount, | |
7056 | Fcons (make_number (count), | |
7057 | img->data.lisp_val)); | |
7058 | ||
4ef0d4d0 KS |
7059 | fn_TIFFClose (tiff); |
7060 | if (!rc) | |
7061 | { | |
7062 | image_error ("Error reading TIFF image `%s'", img->spec, Qnil); | |
7063 | xfree (buf); | |
7064 | UNGCPRO; | |
7065 | return 0; | |
7066 | } | |
7067 | ||
7068 | /* Create the X image and pixmap. */ | |
7069 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) | |
7070 | { | |
7071 | xfree (buf); | |
7072 | UNGCPRO; | |
7073 | return 0; | |
7074 | } | |
7075 | ||
7076 | /* Initialize the color table. */ | |
7077 | init_color_table (); | |
7078 | ||
7079 | /* Process the pixel raster. Origin is in the lower-left corner. */ | |
7080 | for (y = 0; y < height; ++y) | |
7081 | { | |
7082 | uint32 *row = buf + y * width; | |
7083 | ||
7084 | for (x = 0; x < width; ++x) | |
7085 | { | |
7086 | uint32 abgr = row[x]; | |
7087 | int r = TIFFGetR (abgr) << 8; | |
7088 | int g = TIFFGetG (abgr) << 8; | |
7089 | int b = TIFFGetB (abgr) << 8; | |
7090 | XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b)); | |
7091 | } | |
7092 | } | |
7093 | ||
7094 | #ifdef COLOR_TABLE_SUPPORT | |
7095 | /* Remember the colors allocated for the image. Free the color table. */ | |
7096 | img->colors = colors_in_color_table (&img->ncolors); | |
7097 | free_color_table (); | |
7098 | #endif /* COLOR_TABLE_SUPPORT */ | |
7099 | ||
7100 | img->width = width; | |
7101 | img->height = height; | |
7102 | ||
7103 | /* Maybe fill in the background field while we have ximg handy. */ | |
7104 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7105 | /* Casting avoids a GCC warning on W32. */ |
7106 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
7107 | |
7108 | /* Put the image into the pixmap, then free the X image and its buffer. */ | |
7109 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
7110 | x_destroy_x_image (ximg); | |
7111 | xfree (buf); | |
7112 | ||
7113 | UNGCPRO; | |
7114 | return 1; | |
7115 | } | |
7116 | ||
7117 | #else /* HAVE_TIFF */ | |
7118 | ||
edfda783 AR |
7119 | #ifdef HAVE_NS |
7120 | static int | |
7121 | tiff_load (struct frame *f, struct image *img) | |
7122 | { | |
7123 | return ns_load_image(f, img, | |
7124 | image_spec_value (img->spec, QCfile, NULL), | |
7125 | image_spec_value (img->spec, QCdata, NULL)); | |
7126 | } | |
7127 | #endif /* HAVE_NS */ | |
7128 | ||
4ef0d4d0 KS |
7129 | #endif /* !HAVE_TIFF */ |
7130 | ||
7131 | ||
7132 | \f | |
7133 | /*********************************************************************** | |
7134 | GIF | |
7135 | ***********************************************************************/ | |
7136 | ||
9e2a2647 | 7137 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
4ef0d4d0 KS |
7138 | |
7139 | static int gif_image_p P_ ((Lisp_Object object)); | |
7140 | static int gif_load P_ ((struct frame *f, struct image *img)); | |
6ac3c7bb | 7141 | static void gif_clear_image P_ ((struct frame *f, struct image *img)); |
4ef0d4d0 KS |
7142 | |
7143 | /* The symbol `gif' identifying images of this type. */ | |
7144 | ||
7145 | Lisp_Object Qgif; | |
7146 | ||
7147 | /* Indices of image specification fields in gif_format, below. */ | |
7148 | ||
7149 | enum gif_keyword_index | |
7150 | { | |
7151 | GIF_TYPE, | |
7152 | GIF_DATA, | |
7153 | GIF_FILE, | |
7154 | GIF_ASCENT, | |
7155 | GIF_MARGIN, | |
7156 | GIF_RELIEF, | |
7157 | GIF_ALGORITHM, | |
7158 | GIF_HEURISTIC_MASK, | |
7159 | GIF_MASK, | |
7160 | GIF_IMAGE, | |
7161 | GIF_BACKGROUND, | |
7162 | GIF_LAST | |
7163 | }; | |
7164 | ||
7165 | /* Vector of image_keyword structures describing the format | |
7166 | of valid user-defined image specifications. */ | |
7167 | ||
91433552 | 7168 | static const struct image_keyword gif_format[GIF_LAST] = |
4ef0d4d0 KS |
7169 | { |
7170 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7171 | {":data", IMAGE_STRING_VALUE, 0}, | |
7172 | {":file", IMAGE_STRING_VALUE, 0}, | |
7173 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
7174 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
7175 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
7176 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7177 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7178 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 | 7179 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}, |
4ef0d4d0 KS |
7180 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} |
7181 | }; | |
7182 | ||
7183 | /* Structure describing the image type `gif'. */ | |
7184 | ||
7185 | static struct image_type gif_type = | |
7186 | { | |
7187 | &Qgif, | |
7188 | gif_image_p, | |
7189 | gif_load, | |
6ac3c7bb | 7190 | gif_clear_image, |
4ef0d4d0 KS |
7191 | NULL |
7192 | }; | |
7193 | ||
6ac3c7bb KS |
7194 | /* Free X resources of GIF image IMG which is used on frame F. */ |
7195 | ||
7196 | static void | |
7197 | gif_clear_image (f, img) | |
7198 | struct frame *f; | |
7199 | struct image *img; | |
7200 | { | |
1546c559 | 7201 | /* IMG->data.ptr_val may contain metadata with extension data. */ |
6ac3c7bb KS |
7202 | img->data.lisp_val = Qnil; |
7203 | x_clear_image (f, img); | |
7204 | } | |
7205 | ||
4ef0d4d0 KS |
7206 | /* Return non-zero if OBJECT is a valid GIF image specification. */ |
7207 | ||
7208 | static int | |
7209 | gif_image_p (object) | |
7210 | Lisp_Object object; | |
7211 | { | |
7212 | struct image_keyword fmt[GIF_LAST]; | |
7213 | bcopy (gif_format, fmt, sizeof fmt); | |
7214 | ||
7215 | if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)) | |
7216 | return 0; | |
7217 | ||
7218 | /* Must specify either the :data or :file keyword. */ | |
7219 | return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1; | |
7220 | } | |
7221 | ||
9e2a2647 | 7222 | #endif /* HAVE_GIF */ |
4ef0d4d0 KS |
7223 | |
7224 | #ifdef HAVE_GIF | |
7225 | ||
9e2a2647 | 7226 | #if defined (HAVE_NTGUI) |
2e09fef1 EZ |
7227 | /* winuser.h might define DrawText to DrawTextA or DrawTextW. |
7228 | Undefine before redefining to avoid a preprocessor warning. */ | |
7229 | #ifdef DrawText | |
7230 | #undef DrawText | |
7231 | #endif | |
4ef0d4d0 KS |
7232 | /* avoid conflict with QuickdrawText.h */ |
7233 | #define DrawText gif_DrawText | |
7234 | #include <gif_lib.h> | |
7235 | #undef DrawText | |
7236 | ||
9e2a2647 | 7237 | #else /* HAVE_NTGUI */ |
4ef0d4d0 KS |
7238 | |
7239 | #include <gif_lib.h> | |
7240 | ||
9e2a2647 | 7241 | #endif /* HAVE_NTGUI */ |
4ef0d4d0 KS |
7242 | |
7243 | ||
7244 | #ifdef HAVE_NTGUI | |
7245 | ||
7246 | /* GIF library details. */ | |
7247 | DEF_IMGLIB_FN (DGifCloseFile); | |
7248 | DEF_IMGLIB_FN (DGifSlurp); | |
7249 | DEF_IMGLIB_FN (DGifOpen); | |
7250 | DEF_IMGLIB_FN (DGifOpenFileName); | |
7251 | ||
7252 | static int | |
0855eb52 | 7253 | init_gif_functions (Lisp_Object libraries) |
4ef0d4d0 KS |
7254 | { |
7255 | HMODULE library; | |
7256 | ||
0855eb52 | 7257 | if (!(library = w32_delayed_load (libraries, Qgif))) |
4ef0d4d0 KS |
7258 | return 0; |
7259 | ||
7260 | LOAD_IMGLIB_FN (library, DGifCloseFile); | |
7261 | LOAD_IMGLIB_FN (library, DGifSlurp); | |
7262 | LOAD_IMGLIB_FN (library, DGifOpen); | |
7263 | LOAD_IMGLIB_FN (library, DGifOpenFileName); | |
7264 | return 1; | |
7265 | } | |
7266 | ||
7267 | #else | |
7268 | ||
7269 | #define fn_DGifCloseFile DGifCloseFile | |
7270 | #define fn_DGifSlurp DGifSlurp | |
7271 | #define fn_DGifOpen DGifOpen | |
7272 | #define fn_DGifOpenFileName DGifOpenFileName | |
7273 | ||
7274 | #endif /* HAVE_NTGUI */ | |
7275 | ||
7276 | /* Reading a GIF image from memory | |
7277 | Based on the PNG memory stuff to a certain extent. */ | |
7278 | ||
7279 | typedef struct | |
7280 | { | |
7281 | unsigned char *bytes; | |
7282 | size_t len; | |
7283 | int index; | |
7284 | } | |
7285 | gif_memory_source; | |
7286 | ||
7287 | /* Make the current memory source available to gif_read_from_memory. | |
7288 | It's done this way because not all versions of libungif support | |
7289 | a UserData field in the GifFileType structure. */ | |
7290 | static gif_memory_source *current_gif_memory_src; | |
7291 | ||
7292 | static int | |
7293 | gif_read_from_memory (file, buf, len) | |
7294 | GifFileType *file; | |
7295 | GifByteType *buf; | |
7296 | int len; | |
7297 | { | |
7298 | gif_memory_source *src = current_gif_memory_src; | |
7299 | ||
7300 | if (len > src->len - src->index) | |
7301 | return -1; | |
7302 | ||
7303 | bcopy (src->bytes + src->index, buf, len); | |
7304 | src->index += len; | |
7305 | return len; | |
7306 | } | |
7307 | ||
7308 | ||
7309 | /* Load GIF image IMG for use on frame F. Value is non-zero if | |
7310 | successful. */ | |
7311 | ||
91433552 DN |
7312 | static const int interlace_start[] = {0, 4, 2, 1}; |
7313 | static const int interlace_increment[] = {8, 8, 4, 2}; | |
aca946f3 | 7314 | |
4ef0d4d0 KS |
7315 | static int |
7316 | gif_load (f, img) | |
7317 | struct frame *f; | |
7318 | struct image *img; | |
7319 | { | |
7320 | Lisp_Object file, specified_file; | |
7321 | Lisp_Object specified_data; | |
7322 | int rc, width, height, x, y, i; | |
7323 | XImagePtr ximg; | |
7324 | ColorMapObject *gif_color_map; | |
7325 | unsigned long pixel_colors[256]; | |
7326 | GifFileType *gif; | |
7327 | struct gcpro gcpro1; | |
7328 | Lisp_Object image; | |
6ac3c7bb | 7329 | int ino, image_height, image_width; |
4ef0d4d0 KS |
7330 | gif_memory_source memsrc; |
7331 | unsigned char *raster; | |
7332 | ||
7333 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
7334 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
7335 | file = Qnil; | |
7336 | GCPRO1 (file); | |
7337 | ||
7338 | if (NILP (specified_data)) | |
7339 | { | |
7340 | file = x_find_image_file (specified_file); | |
7341 | if (!STRINGP (file)) | |
7342 | { | |
7343 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
7344 | UNGCPRO; | |
7345 | return 0; | |
7346 | } | |
7347 | ||
2e09fef1 EZ |
7348 | /* Open the GIF file. Casting return value avoids a GCC warning |
7349 | on W32. */ | |
7350 | gif = (GifFileType *)fn_DGifOpenFileName (SDATA (file)); | |
4ef0d4d0 KS |
7351 | if (gif == NULL) |
7352 | { | |
7353 | image_error ("Cannot open `%s'", file, Qnil); | |
7354 | UNGCPRO; | |
7355 | return 0; | |
7356 | } | |
7357 | } | |
7358 | else | |
7359 | { | |
7360 | /* Read from memory! */ | |
7361 | current_gif_memory_src = &memsrc; | |
7362 | memsrc.bytes = SDATA (specified_data); | |
7363 | memsrc.len = SBYTES (specified_data); | |
7364 | memsrc.index = 0; | |
7365 | ||
2e09fef1 | 7366 | /* Casting return value avoids a GCC warning on W32. */ |
5fc9fdeb | 7367 | gif = (GifFileType *) fn_DGifOpen (&memsrc, gif_read_from_memory); |
4ef0d4d0 KS |
7368 | if (!gif) |
7369 | { | |
7370 | image_error ("Cannot open memory source `%s'", img->spec, Qnil); | |
7371 | UNGCPRO; | |
7372 | return 0; | |
7373 | } | |
7374 | } | |
7375 | ||
f1f25b99 CY |
7376 | /* Before reading entire contents, check the declared image size. */ |
7377 | if (!check_image_size (f, gif->SWidth, gif->SHeight)) | |
7378 | { | |
10ea2b82 | 7379 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
7380 | fn_DGifCloseFile (gif); |
7381 | UNGCPRO; | |
7382 | return 0; | |
7383 | } | |
7384 | ||
4ef0d4d0 KS |
7385 | /* Read entire contents. */ |
7386 | rc = fn_DGifSlurp (gif); | |
7387 | if (rc == GIF_ERROR) | |
7388 | { | |
7389 | image_error ("Error reading `%s'", img->spec, Qnil); | |
7390 | fn_DGifCloseFile (gif); | |
7391 | UNGCPRO; | |
7392 | return 0; | |
7393 | } | |
7394 | ||
7395 | image = image_spec_value (img->spec, QCindex, NULL); | |
7396 | ino = INTEGERP (image) ? XFASTINT (image) : 0; | |
7397 | if (ino >= gif->ImageCount) | |
7398 | { | |
7399 | image_error ("Invalid image number `%s' in image `%s'", | |
7400 | image, img->spec); | |
7401 | fn_DGifCloseFile (gif); | |
7402 | UNGCPRO; | |
7403 | return 0; | |
7404 | } | |
7405 | ||
6ac3c7bb KS |
7406 | img->corners[TOP_CORNER] = gif->SavedImages[ino].ImageDesc.Top; |
7407 | img->corners[LEFT_CORNER] = gif->SavedImages[ino].ImageDesc.Left; | |
0f4aca46 | 7408 | image_height = gif->SavedImages[ino].ImageDesc.Height; |
6ac3c7bb KS |
7409 | img->corners[BOT_CORNER] = img->corners[TOP_CORNER] + image_height; |
7410 | image_width = gif->SavedImages[ino].ImageDesc.Width; | |
7411 | img->corners[RIGHT_CORNER] = img->corners[LEFT_CORNER] + image_width; | |
0f4aca46 KS |
7412 | |
7413 | width = img->width = max (gif->SWidth, | |
7414 | max (gif->Image.Left + gif->Image.Width, | |
6ac3c7bb | 7415 | img->corners[RIGHT_CORNER])); |
0f4aca46 KS |
7416 | height = img->height = max (gif->SHeight, |
7417 | max (gif->Image.Top + gif->Image.Height, | |
6ac3c7bb | 7418 | img->corners[BOT_CORNER])); |
4ef0d4d0 | 7419 | |
f1f25b99 CY |
7420 | if (!check_image_size (f, width, height)) |
7421 | { | |
10ea2b82 | 7422 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
7423 | fn_DGifCloseFile (gif); |
7424 | UNGCPRO; | |
7425 | return 0; | |
7426 | } | |
7427 | ||
4ef0d4d0 KS |
7428 | /* Create the X image and pixmap. */ |
7429 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) | |
7430 | { | |
7431 | fn_DGifCloseFile (gif); | |
7432 | UNGCPRO; | |
7433 | return 0; | |
7434 | } | |
7435 | ||
7436 | /* Allocate colors. */ | |
7437 | gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap; | |
7438 | if (!gif_color_map) | |
7439 | gif_color_map = gif->SColorMap; | |
7440 | init_color_table (); | |
7441 | bzero (pixel_colors, sizeof pixel_colors); | |
7442 | ||
1ab2cd43 TTN |
7443 | if (gif_color_map) |
7444 | for (i = 0; i < gif_color_map->ColorCount; ++i) | |
7445 | { | |
7446 | int r = gif_color_map->Colors[i].Red << 8; | |
7447 | int g = gif_color_map->Colors[i].Green << 8; | |
7448 | int b = gif_color_map->Colors[i].Blue << 8; | |
7449 | pixel_colors[i] = lookup_rgb_color (f, r, g, b); | |
7450 | } | |
4ef0d4d0 KS |
7451 | |
7452 | #ifdef COLOR_TABLE_SUPPORT | |
7453 | img->colors = colors_in_color_table (&img->ncolors); | |
7454 | free_color_table (); | |
7455 | #endif /* COLOR_TABLE_SUPPORT */ | |
7456 | ||
7457 | /* Clear the part of the screen image that are not covered by | |
7458 | the image from the GIF file. Full animated GIF support | |
7459 | requires more than can be done here (see the gif89 spec, | |
7460 | disposal methods). Let's simply assume that the part | |
7461 | not covered by a sub-image is in the frame's background color. */ | |
6ac3c7bb | 7462 | for (y = 0; y < img->corners[TOP_CORNER]; ++y) |
4ef0d4d0 KS |
7463 | for (x = 0; x < width; ++x) |
7464 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7465 | ||
6ac3c7bb | 7466 | for (y = img->corners[BOT_CORNER]; y < height; ++y) |
4ef0d4d0 KS |
7467 | for (x = 0; x < width; ++x) |
7468 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7469 | ||
6ac3c7bb | 7470 | for (y = img->corners[TOP_CORNER]; y < img->corners[BOT_CORNER]; ++y) |
4ef0d4d0 | 7471 | { |
6ac3c7bb | 7472 | for (x = 0; x < img->corners[LEFT_CORNER]; ++x) |
4ef0d4d0 | 7473 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
6ac3c7bb | 7474 | for (x = img->corners[RIGHT_CORNER]; x < width; ++x) |
4ef0d4d0 KS |
7475 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
7476 | } | |
7477 | ||
7478 | /* Read the GIF image into the X image. We use a local variable | |
7479 | `raster' here because RasterBits below is a char *, and invites | |
7480 | problems with bytes >= 0x80. */ | |
7481 | raster = (unsigned char *) gif->SavedImages[ino].RasterBits; | |
7482 | ||
7483 | if (gif->SavedImages[ino].ImageDesc.Interlace) | |
7484 | { | |
4ef0d4d0 KS |
7485 | int pass; |
7486 | int row = interlace_start[0]; | |
7487 | ||
7488 | pass = 0; | |
7489 | ||
7490 | for (y = 0; y < image_height; y++) | |
7491 | { | |
7492 | if (row >= image_height) | |
7493 | { | |
7494 | row = interlace_start[++pass]; | |
7495 | while (row >= image_height) | |
7496 | row = interlace_start[++pass]; | |
7497 | } | |
7498 | ||
7499 | for (x = 0; x < image_width; x++) | |
7500 | { | |
7501 | int i = raster[(y * image_width) + x]; | |
6ac3c7bb KS |
7502 | XPutPixel (ximg, x + img->corners[LEFT_CORNER], |
7503 | row + img->corners[TOP_CORNER], pixel_colors[i]); | |
4ef0d4d0 KS |
7504 | } |
7505 | ||
7506 | row += interlace_increment[pass]; | |
7507 | } | |
7508 | } | |
7509 | else | |
7510 | { | |
7511 | for (y = 0; y < image_height; ++y) | |
7512 | for (x = 0; x < image_width; ++x) | |
7513 | { | |
7514 | int i = raster[y * image_width + x]; | |
6ac3c7bb KS |
7515 | XPutPixel (ximg, x + img->corners[LEFT_CORNER], |
7516 | y + img->corners[TOP_CORNER], pixel_colors[i]); | |
4ef0d4d0 KS |
7517 | } |
7518 | } | |
7519 | ||
1546c559 JL |
7520 | /* Save GIF image extension data for `image-metadata'. |
7521 | Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */ | |
6ac3c7bb KS |
7522 | img->data.lisp_val = Qnil; |
7523 | if (gif->SavedImages[ino].ExtensionBlockCount > 0) | |
7524 | { | |
7525 | ExtensionBlock *ext = gif->SavedImages[ino].ExtensionBlocks; | |
7526 | for (i = 0; i < gif->SavedImages[ino].ExtensionBlockCount; i++, ext++) | |
7527 | /* Append (... FUNCTION "BYTES") */ | |
7528 | img->data.lisp_val = Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount), | |
7529 | Fcons (make_number (ext->Function), | |
7530 | img->data.lisp_val)); | |
1546c559 JL |
7531 | img->data.lisp_val = Fcons (Qextension_data, |
7532 | Fcons (Fnreverse (img->data.lisp_val), | |
7533 | Qnil)); | |
6ac3c7bb KS |
7534 | } |
7535 | if (gif->ImageCount > 1) | |
7536 | img->data.lisp_val = Fcons (Qcount, | |
7537 | Fcons (make_number (gif->ImageCount), | |
7538 | img->data.lisp_val)); | |
7539 | ||
4ef0d4d0 KS |
7540 | fn_DGifCloseFile (gif); |
7541 | ||
7542 | /* Maybe fill in the background field while we have ximg handy. */ | |
7543 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7544 | /* Casting avoids a GCC warning. */ |
7545 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 KS |
7546 | |
7547 | /* Put the image into the pixmap, then free the X image and its buffer. */ | |
7548 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
7549 | x_destroy_x_image (ximg); | |
7550 | ||
7551 | UNGCPRO; | |
7552 | return 1; | |
7553 | } | |
7554 | ||
2e09fef1 | 7555 | #else /* !HAVE_GIF */ |
4ef0d4d0 | 7556 | |
edfda783 AR |
7557 | #ifdef HAVE_NS |
7558 | static int | |
7559 | gif_load (struct frame *f, struct image *img) | |
7560 | { | |
7561 | return ns_load_image(f, img, | |
7562 | image_spec_value (img->spec, QCfile, NULL), | |
7563 | image_spec_value (img->spec, QCdata, NULL)); | |
7564 | } | |
7565 | #endif /* HAVE_NS */ | |
7566 | ||
4ef0d4d0 KS |
7567 | #endif /* HAVE_GIF */ |
7568 | ||
7569 | ||
3de25479 JV |
7570 | /*********************************************************************** |
7571 | imagemagick | |
4917006b | 7572 | ***********************************************************************/ |
3de25479 JV |
7573 | #if defined (HAVE_IMAGEMAGICK) |
7574 | Lisp_Object Vimagemagick_render_type; | |
3de25479 JV |
7575 | |
7576 | /* The symbol `imagemagick' identifying images of this type. */ | |
7577 | ||
7578 | Lisp_Object Qimagemagick; | |
7579 | Lisp_Object Vimagemagick_render_type; | |
7580 | ||
7581 | /* Indices of image specification fields in imagemagick_format, below. */ | |
7582 | ||
7583 | enum imagemagick_keyword_index | |
4917006b JV |
7584 | { |
7585 | IMAGEMAGICK_TYPE, | |
7586 | IMAGEMAGICK_DATA, | |
7587 | IMAGEMAGICK_FILE, | |
7588 | IMAGEMAGICK_ASCENT, | |
7589 | IMAGEMAGICK_MARGIN, | |
7590 | IMAGEMAGICK_RELIEF, | |
7591 | IMAGEMAGICK_ALGORITHM, | |
7592 | IMAGEMAGICK_HEURISTIC_MASK, | |
7593 | IMAGEMAGICK_MASK, | |
7594 | IMAGEMAGICK_BACKGROUND, | |
7595 | IMAGEMAGICK_LAST | |
7596 | }; | |
3de25479 JV |
7597 | |
7598 | /* Vector of image_keyword structures describing the format | |
7599 | of valid user-defined image specifications. */ | |
7600 | ||
7601 | static struct image_keyword imagemagick_format[IMAGEMAGICK_LAST] = | |
4917006b JV |
7602 | { |
7603 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7604 | {":data", IMAGE_STRING_VALUE, 0}, | |
7605 | {":file", IMAGE_STRING_VALUE, 0}, | |
7606 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
7607 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
7608 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
7609 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7610 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7611 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7612 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
7613 | }; | |
3de25479 JV |
7614 | /* Free X resources of imagemagick image IMG which is used on frame F. */ |
7615 | ||
7616 | static void | |
4917006b JV |
7617 | imagemagick_clear_image (struct frame *f, |
7618 | struct image *img) | |
3de25479 JV |
7619 | { |
7620 | printf("clearing imagemagick image\n"); | |
7621 | x_clear_image (f, img); | |
7622 | } | |
7623 | ||
3de25479 JV |
7624 | |
7625 | ||
7626 | /* Return non-zero if OBJECT is a valid IMAGEMAGICK image specification. Do | |
7627 | this by calling parse_image_spec and supplying the keywords that | |
7628 | identify the IMAGEMAGICK format. */ | |
7629 | ||
7630 | static int | |
4917006b | 7631 | imagemagick_image_p (Lisp_Object object) |
3de25479 JV |
7632 | { |
7633 | struct image_keyword fmt[IMAGEMAGICK_LAST]; | |
7634 | bcopy (imagemagick_format, fmt, sizeof fmt); | |
7635 | ||
7636 | if (!parse_image_spec (object, fmt, IMAGEMAGICK_LAST, Qimagemagick)) | |
7637 | return 0; | |
7638 | ||
7639 | /* Must specify either the :data or :file keyword. */ | |
7640 | return fmt[IMAGEMAGICK_FILE].count + fmt[IMAGEMAGICK_DATA].count == 1; | |
7641 | } | |
7642 | ||
7643 | /* The GIF library also defines DrawRectangle, but its never used in Emacs. | |
7644 | Therefore rename the function so it doesnt collide with ImageMagick. */ | |
7645 | #define DrawRectangle DrawRectangleGif | |
7646 | #include <wand/MagickWand.h> | |
7647 | ||
d2a2d277 JV |
7648 | /* imagemagick_load_image is a helper function for imagemagick_load, |
7649 | which does the actual loading given contents and size, apart from | |
7650 | frame and image structures, passed from imagemagick_load. | |
3de25479 JV |
7651 | |
7652 | Uses librimagemagick to do most of the image processing. | |
7653 | ||
d2a2d277 | 7654 | non-zero when successful. |
3de25479 JV |
7655 | */ |
7656 | ||
7657 | static int | |
d2a2d277 JV |
7658 | imagemagick_load_image (/* Pointer to emacs frame structure. */ |
7659 | struct frame *f, | |
7660 | /* Pointer to emacs image structure. */ | |
7661 | struct image *img, | |
7662 | /* String containing the IMAGEMAGICK data to | |
7663 | be parsed. */ | |
7664 | unsigned char *contents, | |
7665 | /* Size of data in bytes. */ | |
7666 | unsigned int size, | |
7667 | /* Filename, either pass filename or | |
7668 | contents/size. */ | |
7669 | unsigned char *filename) | |
3de25479 JV |
7670 | { |
7671 | long unsigned int width; | |
7672 | long unsigned int height; | |
7673 | ||
7674 | MagickBooleanType | |
7675 | status; | |
7676 | ||
7677 | XImagePtr ximg; | |
7678 | Lisp_Object specified_bg; | |
7679 | XColor background; | |
7680 | int x; | |
7681 | int y; | |
7682 | ||
7683 | MagickWand *image_wand; | |
7684 | PixelIterator *iterator; | |
7685 | PixelWand **pixels; | |
7686 | MagickPixelPacket pixel; | |
4917006b JV |
7687 | Lisp_Object image; |
7688 | Lisp_Object value; | |
7689 | Lisp_Object crop, geometry; | |
7690 | long ino; | |
7691 | int desired_width, desired_height; | |
7692 | double rotation; | |
7693 | int imagemagick_rendermethod; | |
7694 | int pixelwidth; | |
3de25479 | 7695 | |
4917006b | 7696 | |
3de25479 JV |
7697 | /* image_wand will contain the image. */ |
7698 | image_wand = NewMagickWand(); | |
7699 | ||
7700 | /* Parse the contents argument and initialize image_wand. */ | |
4917006b JV |
7701 | if(filename != NULL) |
7702 | status = MagickReadImage(image_wand, filename); | |
3de25479 | 7703 | else |
4917006b | 7704 | status = MagickReadImageBlob(image_wand, contents, size); |
3de25479 JV |
7705 | image_error ("im read failed", Qnil, Qnil); |
7706 | if (status == MagickFalse) goto imagemagick_error; | |
7707 | ||
7708 | /* Handle image index for image types who can contain more than one image. | |
4917006b JV |
7709 | Interface :index is same as for GIF. */ |
7710 | ||
3de25479 JV |
7711 | image = image_spec_value (img->spec, QCindex, NULL); |
7712 | ino = INTEGERP (image) ? XFASTINT (image) : 0; | |
3de25479 | 7713 | |
431dab04 JV |
7714 | |
7715 | ||
7716 | if (ino >= MagickGetNumberImages(image_wand)) | |
7717 | { | |
7718 | image_error ("Invalid image number `%s' in image `%s'", | |
7719 | image, img->spec); | |
7720 | UNGCPRO; | |
7721 | return 0; | |
7722 | } | |
7723 | ||
7724 | ||
7725 | if (MagickGetNumberImages(image_wand) > 1) | |
d2a2d277 JV |
7726 | img->data.lisp_val = |
7727 | Fcons (Qcount, | |
7728 | Fcons (make_number (MagickGetNumberImages(image_wand)), | |
7729 | img->data.lisp_val)); | |
4917006b | 7730 | if(ino == 0) |
3de25479 JV |
7731 | MagickSetFirstIterator(image_wand); |
7732 | else | |
7733 | MagickSetIteratorIndex(image_wand, ino); | |
7734 | ||
d2a2d277 JV |
7735 | /* If width and/or height is set in the display spec assume we want |
7736 | to scale to those. */ | |
3de25479 | 7737 | |
3de25479 JV |
7738 | value = image_spec_value (img->spec, QCwidth, NULL); |
7739 | desired_width = (INTEGERP (value) ? XFASTINT (value) : -1); | |
7740 | value = image_spec_value (img->spec, QCheight, NULL); | |
7741 | desired_height = (INTEGERP (value) ? XFASTINT (value) : -1); | |
4917006b JV |
7742 | if(desired_width != -1 && desired_height != -1) |
7743 | { | |
7744 | printf("MagickScaleImage %d %d\n", desired_width, desired_height); | |
7745 | status = MagickScaleImage(image_wand, desired_width, desired_height); | |
7746 | if (status == MagickFalse) { | |
7747 | image_error ("Imagemagick scale failed", Qnil, Qnil); | |
7748 | goto imagemagick_error; | |
7749 | } | |
3de25479 | 7750 | } |
3de25479 | 7751 | |
d2a2d277 JV |
7752 | /* Also support :geometry and :crop which are imagemagick specific |
7753 | descriptors. */ | |
3de25479 | 7754 | |
3de25479 JV |
7755 | crop = image_spec_value (img->spec, QCcrop, NULL); |
7756 | geometry = image_spec_value (img->spec, QCgeometry, NULL); | |
4917006b JV |
7757 | if (STRINGP (crop) && STRINGP (geometry)) |
7758 | { | |
7759 | printf("MagickTransformImage %s %s\n", SDATA(crop), SDATA(geometry)); | |
d2a2d277 JV |
7760 | image_wand = MagickTransformImage (image_wand, SDATA (crop), |
7761 | SDATA (geometry)); | |
4917006b JV |
7762 | /* TODO differ between image_wand and transform_wand. */ |
7763 | } | |
3de25479 | 7764 | |
d2a2d277 JV |
7765 | /* Furthermore :rotation. we need background color and angle for |
7766 | rotation. */ | |
3de25479 | 7767 | /* |
d2a2d277 JV |
7768 | TODO background handling for rotation specified_bg = |
7769 | image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP | |
7770 | (specified_bg) | |
3de25479 | 7771 | */ |
4917006b JV |
7772 | value = image_spec_value (img->spec, QCrotation, NULL); |
7773 | if (FLOATP (value)) | |
7774 | { | |
3de25479 JV |
7775 | PixelWand* background = NewPixelWand(); |
7776 | PixelSetColor (background, "#ffffff");/*TODO remove hardcode*/ | |
4917006b | 7777 | |
3de25479 | 7778 | rotation = extract_float (value); |
4917006b JV |
7779 | printf ("MagickRotateImage %f\n", rotation); |
7780 | ||
7781 | status = MagickRotateImage (image_wand, background, rotation); | |
3de25479 | 7782 | DestroyPixelWand (background); |
4917006b JV |
7783 | if (status == MagickFalse) |
7784 | { | |
7785 | image_error ("Imagemagick image rotate failed", Qnil, Qnil); | |
7786 | goto imagemagick_error; | |
7787 | } | |
3de25479 JV |
7788 | } |
7789 | ||
7790 | /* Finaly we are done manipulating the image, | |
7791 | figure out resulting width, height, and then transfer ownerwship to Emacs. | |
4917006b JV |
7792 | */ |
7793 | height = MagickGetImageHeight (image_wand); | |
7794 | width = MagickGetImageWidth (image_wand); | |
7795 | if (status == MagickFalse) | |
7796 | { | |
3de25479 JV |
7797 | image_error ("Imagemagick image get size failed", Qnil, Qnil); |
7798 | goto imagemagick_error; | |
4917006b | 7799 | } |
3de25479 JV |
7800 | |
7801 | if (! check_image_size (f, width, height)) | |
7802 | { | |
7803 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
7804 | goto imagemagick_error; | |
7805 | } | |
7806 | ||
7807 | /* We can now get a valid pixel buffer from the imagemagick file, if all | |
7808 | went ok. */ | |
7809 | ||
7810 | ||
7811 | init_color_table (); | |
d2a2d277 JV |
7812 | imagemagick_rendermethod = (INTEGERP (Vimagemagick_render_type) |
7813 | ? XFASTINT (Vimagemagick_render_type) : 0); | |
4917006b JV |
7814 | if (imagemagick_rendermethod == 0) |
7815 | { | |
7816 | /* Try to create a x pixmap to hold the imagemagick pixmap. */ | |
d2a2d277 JV |
7817 | if (!x_create_x_image_and_pixmap (f, width, height, 0, |
7818 | &ximg, &img->pixmap)) | |
4917006b JV |
7819 | { |
7820 | image_error("Imagemagick X bitmap allocation failure", Qnil, Qnil); | |
7821 | goto imagemagick_error; | |
7822 | } | |
3de25479 | 7823 | |
4917006b JV |
7824 | /* Copy imagegmagick image to x with primitive yet robust pixel |
7825 | pusher loop. This has been tested a lot with many different | |
7826 | images, it doesnt work too well with image archive formats though! | |
3de25479 | 7827 | |
4917006b JV |
7828 | Also seems slow. |
7829 | */ | |
3de25479 | 7830 | |
4917006b JV |
7831 | /* Copy pixels from the imagemagick image structure to the x image map. */ |
7832 | iterator = NewPixelIterator (image_wand); | |
7833 | if ((iterator == (PixelIterator *) NULL)) | |
7834 | { | |
d2a2d277 JV |
7835 | image_error ("Imagemagick pixel iterator creation failed", |
7836 | Qnil, Qnil); | |
3de25479 | 7837 | goto imagemagick_error; |
4917006b JV |
7838 | } |
7839 | ||
7840 | for (y = 0; y < (long) MagickGetImageHeight(image_wand); y++) | |
7841 | { | |
7842 | pixels = PixelGetNextIteratorRow (iterator, &width); | |
7843 | if ((pixels == (PixelWand **) NULL)) | |
7844 | break; | |
7845 | for (x = 0; x < (long) width; x++) | |
7846 | { | |
7847 | PixelGetMagickColor (pixels[x], &pixel); | |
d2a2d277 JV |
7848 | XPutPixel (ximg, x, y, |
7849 | lookup_rgb_color (f, | |
7850 | pixel.red, | |
7851 | pixel.green, | |
7852 | pixel.blue)); | |
4917006b JV |
7853 | } |
7854 | } | |
7855 | DestroyPixelIterator (iterator); | |
3de25479 JV |
7856 | } |
7857 | ||
4917006b JV |
7858 | if (imagemagick_rendermethod == 1) |
7859 | { | |
7860 | /* Try if magicexportimage is any faster than pixelpushing. */ | |
d2a2d277 JV |
7861 | /* printf("ximg: bitmap_unit:%d format:%d byte_order:%d depth:%d |
7862 | bits_per_pixel:%d\n", */ | |
7863 | /* ximg->bitmap_unit,ximg->format,ximg->byte_order, | |
7864 | ximg->depth,ximg->bits_per_pixel); */ | |
4917006b JV |
7865 | int imagedepth = 24;/*MagickGetImageDepth(image_wand);*/ |
7866 | char* exportdepth = imagedepth <= 8 ? "I" : "BGRP";/*"RGBP";*/ | |
7867 | /* Try to create a x pixmap to hold the imagemagick pixmap. */ | |
7868 | printf("imagedepth:%d exportdepth:%s\n", imagedepth, exportdepth); | |
d2a2d277 JV |
7869 | if (!x_create_x_image_and_pixmap (f, width, height, imagedepth, |
7870 | &ximg, &img->pixmap)){ | |
4917006b JV |
7871 | image_error("Imagemagick X bitmap allocation failure", Qnil, Qnil); |
7872 | goto imagemagick_error; | |
3de25479 | 7873 | } |
3de25479 JV |
7874 | |
7875 | ||
4917006b JV |
7876 | /* Oddly, the below code doesnt seem to work:*/ |
7877 | /* switch(ximg->bitmap_unit){ */ | |
7878 | /* case 8: */ | |
7879 | /* pixelwidth=CharPixel; */ | |
7880 | /* break; */ | |
7881 | /* case 16: */ | |
7882 | /* pixelwidth=ShortPixel; */ | |
7883 | /* break; */ | |
7884 | /* case 32: */ | |
7885 | /* pixelwidth=LongPixel; */ | |
7886 | /* break; */ | |
7887 | /* } */ | |
7888 | /* | |
7889 | Here im just guessing the format of the bitmap. | |
7890 | happens to work fine for: | |
7891 | - bw djvu images | |
7892 | on rgb display. | |
7893 | seems about 3 times as fast as pixel pushing(not carefully measured) | |
7894 | with color djvu, the bitplanes are mapped to wrong color(seems fixed). | |
7895 | ||
7896 | */ | |
7897 | pixelwidth = CharPixel;/*??? TODO figure out*/ | |
431dab04 | 7898 | #ifdef HAVE_MAGICKEXPORTIMAGEPIXELS |
4917006b JV |
7899 | MagickExportImagePixels(image_wand, |
7900 | 0, 0, | |
7901 | width, height, | |
7902 | exportdepth, | |
7903 | pixelwidth, | |
7904 | /*&(img->pixmap));*/ | |
7905 | ximg->data); | |
431dab04 | 7906 | #else |
d2a2d277 | 7907 | image_error("You dont have MagickExportImagePixels, upgrade ImageMagick!", |
4917006b | 7908 | Qnil, Qnil); |
431dab04 | 7909 | #endif |
4917006b | 7910 | } |
3de25479 | 7911 | |
3ed42149 | 7912 | |
3de25479 JV |
7913 | #ifdef COLOR_TABLE_SUPPORT |
7914 | /* Remember colors allocated for this image. */ | |
7915 | img->colors = colors_in_color_table (&img->ncolors); | |
7916 | free_color_table (); | |
7917 | #endif /* COLOR_TABLE_SUPPORT */ | |
7918 | ||
7919 | ||
7920 | img->width = width; | |
7921 | img->height = height; | |
7922 | ||
7923 | /* Maybe fill in the background field while we have ximg handy. | |
7924 | Casting avoids a GCC warning. */ | |
7925 | /* IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg);*/ | |
7926 | ||
7927 | /* Put the image into the pixmap, then free the X image and its | |
7928 | buffer. */ | |
7929 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
7930 | ||
7931 | x_destroy_x_image (ximg); | |
7932 | ||
7933 | ||
7934 | /* JAVE TODO more cleanup. */ | |
7935 | DestroyMagickWand (image_wand); | |
7936 | ||
7937 | return 1; | |
7938 | ||
7939 | imagemagick_error: | |
7940 | /* TODO more cleanup. */ | |
7941 | image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec, Qnil); | |
7942 | printf("Imagemagick error, see *Messages*\n"); | |
7943 | return 0; | |
7944 | } | |
7945 | ||
4917006b JV |
7946 | |
7947 | /* Load IMAGEMAGICK image IMG for use on frame F. Value is non-zero if | |
7948 | successful. this function will go into the imagemagick_type structure, and | |
7949 | the prototype thus needs to be compatible with that structure. */ | |
7950 | ||
7951 | static int | |
7952 | imagemagick_load (struct frame *f, | |
7953 | struct image *img) | |
7954 | { | |
7955 | int success_p = 0; | |
7956 | Lisp_Object file_name; | |
7957 | ||
7958 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
7959 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
7960 | if (STRINGP (file_name)) | |
7961 | { | |
7962 | Lisp_Object file; | |
7963 | unsigned char *contents; | |
7964 | int size; | |
7965 | struct gcpro gcpro1; | |
7966 | ||
7967 | file = x_find_image_file (file_name); | |
7968 | GCPRO1 (file); | |
7969 | if (!STRINGP (file)) | |
7970 | { | |
7971 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
7972 | UNGCPRO; | |
7973 | return 0; | |
7974 | } | |
7975 | ||
7976 | /* Read the entire file into memory. */ | |
7977 | /* contents = slurp_file (SDATA (file), &size); */ | |
7978 | /* if (contents == NULL) */ | |
7979 | /* { */ | |
d2a2d277 JV |
7980 | /* image_error ("Error loading IMAGEMAGICK image `%s'", |
7981 | img->spec, Qnil); */ | |
4917006b JV |
7982 | /* UNGCPRO; */ |
7983 | /* return 0; */ | |
7984 | /* } */ | |
7985 | /* If the file was slurped into memory properly, parse it. */ | |
7986 | success_p = imagemagick_load_image (f, img, 0, 0, SDATA(file_name)); | |
7987 | UNGCPRO; | |
7988 | } | |
7989 | /* Else its not a file, its a lisp object. Load the image from a | |
7990 | lisp object rather than a file. */ | |
7991 | else | |
7992 | { | |
7993 | Lisp_Object data; | |
7994 | ||
7995 | data = image_spec_value (img->spec, QCdata, NULL); | |
d2a2d277 JV |
7996 | success_p = imagemagick_load_image (f, img, SDATA (data), |
7997 | SBYTES (data), NULL); | |
4917006b JV |
7998 | } |
7999 | ||
8000 | return success_p; | |
8001 | } | |
8002 | ||
d2a2d277 JV |
8003 | /* Structure describing the image type `imagemagick'. Its the same |
8004 | type of structure defined for all image formats, handled by Emacs | |
8005 | image functions. See struct image_type in dispextern.h. */ | |
4917006b JV |
8006 | |
8007 | static struct image_type imagemagick_type = | |
8008 | { | |
d2a2d277 JV |
8009 | /* An identifier showing that this is an image structure for the |
8010 | IMAGEMAGICK format. */ | |
4917006b | 8011 | &Qimagemagick, |
d2a2d277 JV |
8012 | /* Handle to a function that can be used to identify a IMAGEMAGICK |
8013 | file. */ | |
4917006b JV |
8014 | imagemagick_image_p, |
8015 | /* Handle to function used to load a IMAGEMAGICK file. */ | |
8016 | imagemagick_load, | |
8017 | /* Handle to function to free resources for IMAGEMAGICK. */ | |
8018 | imagemagick_clear_image, | |
8019 | /* An internal field to link to the next image type in a list of | |
8020 | image types, will be filled in when registering the format. */ | |
8021 | NULL | |
8022 | }; | |
8023 | ||
8024 | ||
8025 | ||
8026 | ||
8027 | DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0, | |
3de25479 | 8028 | doc: /* Return image file types supported by ImageMagick. |
d2a2d277 JV |
8029 | Since ImageMagick recognizes a lot of file-types that clash with Emacs, |
8030 | such as .c, we want to be able to alter the list at the lisp level. */) | |
3de25479 JV |
8031 | () |
8032 | { | |
8033 | Lisp_Object typelist = Qnil; | |
8034 | unsigned long numf; | |
8035 | ExceptionInfo ex; | |
8036 | char** imtypes = GetMagickList ("*", &numf, &ex); | |
8037 | int i; | |
8038 | Lisp_Object Qimagemagicktype; | |
4917006b JV |
8039 | for (i = 0; i < numf; i++) |
8040 | { | |
8041 | Qimagemagicktype = intern (imtypes[i]); | |
8042 | typelist = Fcons (Qimagemagicktype, typelist); | |
8043 | } | |
8044 | return typelist; | |
3de25479 JV |
8045 | } |
8046 | ||
8047 | #endif /* defined (HAVE_IMAGEMAGICK) */ | |
8048 | ||
8049 | ||
4ef0d4d0 | 8050 | \f |
4ab27a43 GM |
8051 | /*********************************************************************** |
8052 | SVG | |
8053 | ***********************************************************************/ | |
8054 | ||
8055 | #if defined (HAVE_RSVG) | |
8056 | ||
8057 | /* Function prototypes. */ | |
8058 | ||
8059 | static int svg_image_p P_ ((Lisp_Object object)); | |
8060 | static int svg_load P_ ((struct frame *f, struct image *img)); | |
8061 | ||
8062 | static int svg_load_image P_ ((struct frame *, struct image *, | |
8063 | unsigned char *, unsigned int)); | |
8064 | ||
8065 | /* The symbol `svg' identifying images of this type. */ | |
8066 | ||
8067 | Lisp_Object Qsvg; | |
8068 | ||
8069 | /* Indices of image specification fields in svg_format, below. */ | |
8070 | ||
8071 | enum svg_keyword_index | |
8072 | { | |
8073 | SVG_TYPE, | |
8074 | SVG_DATA, | |
8075 | SVG_FILE, | |
8076 | SVG_ASCENT, | |
8077 | SVG_MARGIN, | |
8078 | SVG_RELIEF, | |
8079 | SVG_ALGORITHM, | |
8080 | SVG_HEURISTIC_MASK, | |
8081 | SVG_MASK, | |
8082 | SVG_BACKGROUND, | |
8083 | SVG_LAST | |
8084 | }; | |
8085 | ||
8086 | /* Vector of image_keyword structures describing the format | |
8087 | of valid user-defined image specifications. */ | |
8088 | ||
91433552 | 8089 | static const struct image_keyword svg_format[SVG_LAST] = |
4ab27a43 GM |
8090 | { |
8091 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8092 | {":data", IMAGE_STRING_VALUE, 0}, | |
8093 | {":file", IMAGE_STRING_VALUE, 0}, | |
8094 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
8095 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
8096 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
8097 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8098 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8099 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8100 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8101 | }; | |
8102 | ||
8103 | /* Structure describing the image type `svg'. Its the same type of | |
8104 | structure defined for all image formats, handled by emacs image | |
8105 | functions. See struct image_type in dispextern.h. */ | |
8106 | ||
8107 | static struct image_type svg_type = | |
8108 | { | |
8109 | /* An identifier showing that this is an image structure for the SVG format. */ | |
8110 | &Qsvg, | |
8111 | /* Handle to a function that can be used to identify a SVG file. */ | |
8112 | svg_image_p, | |
8113 | /* Handle to function used to load a SVG file. */ | |
8114 | svg_load, | |
8115 | /* Handle to function to free sresources for SVG. */ | |
8116 | x_clear_image, | |
8117 | /* An internal field to link to the next image type in a list of | |
8118 | image types, will be filled in when registering the format. */ | |
c21e6151 | 8119 | NULL |
4ab27a43 GM |
8120 | }; |
8121 | ||
8122 | ||
8123 | /* Return non-zero if OBJECT is a valid SVG image specification. Do | |
8124 | this by calling parse_image_spec and supplying the keywords that | |
8125 | identify the SVG format. */ | |
8126 | ||
8127 | static int | |
8128 | svg_image_p (object) | |
8129 | Lisp_Object object; | |
8130 | { | |
8131 | struct image_keyword fmt[SVG_LAST]; | |
8132 | bcopy (svg_format, fmt, sizeof fmt); | |
8133 | ||
8134 | if (!parse_image_spec (object, fmt, SVG_LAST, Qsvg)) | |
8135 | return 0; | |
8136 | ||
8137 | /* Must specify either the :data or :file keyword. */ | |
8138 | return fmt[SVG_FILE].count + fmt[SVG_DATA].count == 1; | |
8139 | } | |
8140 | ||
8141 | #include <librsvg/rsvg.h> | |
8142 | ||
5fc9fdeb JR |
8143 | #ifdef HAVE_NTGUI |
8144 | ||
8145 | /* SVG library functions. */ | |
8146 | DEF_IMGLIB_FN (rsvg_handle_new); | |
e025c9c2 | 8147 | DEF_IMGLIB_FN (rsvg_handle_get_dimensions); |
5fc9fdeb JR |
8148 | DEF_IMGLIB_FN (rsvg_handle_write); |
8149 | DEF_IMGLIB_FN (rsvg_handle_close); | |
8150 | DEF_IMGLIB_FN (rsvg_handle_get_pixbuf); | |
8151 | DEF_IMGLIB_FN (rsvg_handle_free); | |
8152 | ||
8153 | DEF_IMGLIB_FN (gdk_pixbuf_get_width); | |
8154 | DEF_IMGLIB_FN (gdk_pixbuf_get_height); | |
8155 | DEF_IMGLIB_FN (gdk_pixbuf_get_pixels); | |
8156 | DEF_IMGLIB_FN (gdk_pixbuf_get_rowstride); | |
8157 | DEF_IMGLIB_FN (gdk_pixbuf_get_colorspace); | |
8158 | DEF_IMGLIB_FN (gdk_pixbuf_get_n_channels); | |
8159 | DEF_IMGLIB_FN (gdk_pixbuf_get_has_alpha); | |
8160 | DEF_IMGLIB_FN (gdk_pixbuf_get_bits_per_sample); | |
8161 | ||
8162 | DEF_IMGLIB_FN (g_type_init); | |
8163 | DEF_IMGLIB_FN (g_object_unref); | |
8164 | DEF_IMGLIB_FN (g_error_free); | |
8165 | ||
df23bf08 | 8166 | Lisp_Object Qgdk_pixbuf, Qglib, Qgobject; |
5fc9fdeb JR |
8167 | |
8168 | static int | |
8169 | init_svg_functions (Lisp_Object libraries) | |
8170 | { | |
df23bf08 | 8171 | HMODULE library, gdklib, glib, gobject; |
5fc9fdeb JR |
8172 | |
8173 | if (!(glib = w32_delayed_load (libraries, Qglib)) | |
df23bf08 | 8174 | || !(gobject = w32_delayed_load (libraries, Qgobject)) |
5fc9fdeb JR |
8175 | || !(gdklib = w32_delayed_load (libraries, Qgdk_pixbuf)) |
8176 | || !(library = w32_delayed_load (libraries, Qsvg))) | |
8177 | return 0; | |
8178 | ||
8179 | LOAD_IMGLIB_FN (library, rsvg_handle_new); | |
e025c9c2 | 8180 | LOAD_IMGLIB_FN (library, rsvg_handle_get_dimensions); |
5fc9fdeb JR |
8181 | LOAD_IMGLIB_FN (library, rsvg_handle_write); |
8182 | LOAD_IMGLIB_FN (library, rsvg_handle_close); | |
8183 | LOAD_IMGLIB_FN (library, rsvg_handle_get_pixbuf); | |
8184 | LOAD_IMGLIB_FN (library, rsvg_handle_free); | |
8185 | ||
8186 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_width); | |
8187 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_height); | |
8188 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_pixels); | |
8189 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_rowstride); | |
8190 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_colorspace); | |
8191 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_n_channels); | |
8192 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_has_alpha); | |
8193 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_bits_per_sample); | |
8194 | ||
df23bf08 JR |
8195 | LOAD_IMGLIB_FN (gobject, g_type_init); |
8196 | LOAD_IMGLIB_FN (gobject, g_object_unref); | |
5fc9fdeb | 8197 | LOAD_IMGLIB_FN (glib, g_error_free); |
df23bf08 | 8198 | |
5fc9fdeb JR |
8199 | return 1; |
8200 | } | |
4ab27a43 | 8201 | |
5fc9fdeb JR |
8202 | #else |
8203 | /* The following aliases for library functions allow dynamic loading | |
8204 | to be used on some platforms. */ | |
4ab27a43 | 8205 | #define fn_rsvg_handle_new rsvg_handle_new |
e025c9c2 | 8206 | #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions |
4ab27a43 GM |
8207 | #define fn_rsvg_handle_write rsvg_handle_write |
8208 | #define fn_rsvg_handle_close rsvg_handle_close | |
8209 | #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf | |
8210 | #define fn_rsvg_handle_free rsvg_handle_free | |
8211 | ||
5fc9fdeb JR |
8212 | #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width |
8213 | #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height | |
8214 | #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels | |
8215 | #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride | |
8216 | #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace | |
8217 | #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels | |
8218 | #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha | |
4ab27a43 GM |
8219 | #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample |
8220 | ||
5fc9fdeb JR |
8221 | #define fn_g_type_init g_type_init |
8222 | #define fn_g_object_unref g_object_unref | |
8223 | #define fn_g_error_free g_error_free | |
8224 | #endif /* !HAVE_NTGUI */ | |
4ab27a43 GM |
8225 | |
8226 | /* Load SVG image IMG for use on frame F. Value is non-zero if | |
8227 | successful. this function will go into the svg_type structure, and | |
8228 | the prototype thus needs to be compatible with that structure. */ | |
8229 | ||
8230 | static int | |
8231 | svg_load (f, img) | |
8232 | struct frame *f; | |
8233 | struct image *img; | |
8234 | { | |
8235 | int success_p = 0; | |
8236 | Lisp_Object file_name; | |
8237 | ||
8238 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
8239 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
8240 | if (STRINGP (file_name)) | |
8241 | { | |
8242 | Lisp_Object file; | |
8243 | unsigned char *contents; | |
8244 | int size; | |
8245 | struct gcpro gcpro1; | |
8246 | ||
8247 | file = x_find_image_file (file_name); | |
8248 | GCPRO1 (file); | |
8249 | if (!STRINGP (file)) | |
c21e6151 YM |
8250 | { |
8251 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
8252 | UNGCPRO; | |
8253 | return 0; | |
8254 | } | |
8255 | ||
4ab27a43 GM |
8256 | /* Read the entire file into memory. */ |
8257 | contents = slurp_file (SDATA (file), &size); | |
8258 | if (contents == NULL) | |
c21e6151 YM |
8259 | { |
8260 | image_error ("Error loading SVG image `%s'", img->spec, Qnil); | |
8261 | UNGCPRO; | |
8262 | return 0; | |
8263 | } | |
4ab27a43 | 8264 | /* If the file was slurped into memory properly, parse it. */ |
c21e6151 | 8265 | success_p = svg_load_image (f, img, contents, size); |
4ab27a43 GM |
8266 | xfree (contents); |
8267 | UNGCPRO; | |
8268 | } | |
8269 | /* Else its not a file, its a lisp object. Load the image from a | |
8270 | lisp object rather than a file. */ | |
8271 | else | |
8272 | { | |
8273 | Lisp_Object data; | |
8274 | ||
8275 | data = image_spec_value (img->spec, QCdata, NULL); | |
8276 | success_p = svg_load_image (f, img, SDATA (data), SBYTES (data)); | |
8277 | } | |
8278 | ||
8279 | return success_p; | |
8280 | } | |
8281 | ||
bbe6f2aa JB |
8282 | /* svg_load_image is a helper function for svg_load, which does the |
8283 | actual loading given contents and size, apart from frame and image | |
8284 | structures, passed from svg_load. | |
4ab27a43 | 8285 | |
bbe6f2aa | 8286 | Uses librsvg to do most of the image processing. |
c21e6151 | 8287 | |
bbe6f2aa | 8288 | Returns non-zero when successful. */ |
4ab27a43 GM |
8289 | static int |
8290 | svg_load_image (f, img, contents, size) | |
bbe6f2aa | 8291 | /* Pointer to emacs frame structure. */ |
4ab27a43 GM |
8292 | struct frame *f; |
8293 | /* Pointer to emacs image structure. */ | |
8294 | struct image *img; | |
8295 | /* String containing the SVG XML data to be parsed. */ | |
8296 | unsigned char *contents; | |
8297 | /* Size of data in bytes. */ | |
8298 | unsigned int size; | |
8299 | { | |
8300 | RsvgHandle *rsvg_handle; | |
e025c9c2 | 8301 | RsvgDimensionData dimension_data; |
4ab27a43 GM |
8302 | GError *error = NULL; |
8303 | GdkPixbuf *pixbuf; | |
8304 | int width; | |
8305 | int height; | |
8306 | const guint8 *pixels; | |
8307 | int rowstride; | |
8308 | XImagePtr ximg; | |
c21e6151 | 8309 | Lisp_Object specified_bg; |
4ab27a43 GM |
8310 | XColor background; |
8311 | int x; | |
8312 | int y; | |
8313 | ||
8314 | /* g_type_init is a glib function that must be called prior to using | |
8315 | gnome type library functions. */ | |
5fc9fdeb | 8316 | fn_g_type_init (); |
4ab27a43 | 8317 | /* Make a handle to a new rsvg object. */ |
8b7d0a16 | 8318 | rsvg_handle = (RsvgHandle *) fn_rsvg_handle_new (); |
4ab27a43 GM |
8319 | |
8320 | /* Parse the contents argument and fill in the rsvg_handle. */ | |
8321 | fn_rsvg_handle_write (rsvg_handle, contents, size, &error); | |
e025c9c2 | 8322 | if (error) goto rsvg_error; |
4ab27a43 GM |
8323 | |
8324 | /* The parsing is complete, rsvg_handle is ready to used, close it | |
8325 | for further writes. */ | |
8326 | fn_rsvg_handle_close (rsvg_handle, &error); | |
e025c9c2 CY |
8327 | if (error) goto rsvg_error; |
8328 | ||
8329 | fn_rsvg_handle_get_dimensions (rsvg_handle, &dimension_data); | |
8330 | if (! check_image_size (f, dimension_data.width, dimension_data.height)) | |
10ea2b82 JR |
8331 | { |
8332 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
8333 | goto rsvg_error; | |
8334 | } | |
b2996e57 | 8335 | |
4ab27a43 GM |
8336 | /* We can now get a valid pixel buffer from the svg file, if all |
8337 | went ok. */ | |
8b7d0a16 | 8338 | pixbuf = (GdkPixbuf *) fn_rsvg_handle_get_pixbuf (rsvg_handle); |
e025c9c2 CY |
8339 | if (!pixbuf) goto rsvg_error; |
8340 | fn_g_object_unref (rsvg_handle); | |
4ab27a43 GM |
8341 | |
8342 | /* Extract some meta data from the svg handle. */ | |
8343 | width = fn_gdk_pixbuf_get_width (pixbuf); | |
8344 | height = fn_gdk_pixbuf_get_height (pixbuf); | |
8b7d0a16 | 8345 | pixels = (const guint8 *) fn_gdk_pixbuf_get_pixels (pixbuf); |
4ab27a43 GM |
8346 | rowstride = fn_gdk_pixbuf_get_rowstride (pixbuf); |
8347 | ||
8348 | /* Validate the svg meta data. */ | |
8349 | eassert (fn_gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB); | |
8350 | eassert (fn_gdk_pixbuf_get_n_channels (pixbuf) == 4); | |
8351 | eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf)); | |
8352 | eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf) == 8); | |
8353 | ||
8354 | /* Try to create a x pixmap to hold the svg pixmap. */ | |
c21e6151 YM |
8355 | if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap)) |
8356 | { | |
5fc9fdeb | 8357 | fn_g_object_unref (pixbuf); |
c21e6151 YM |
8358 | return 0; |
8359 | } | |
4ab27a43 GM |
8360 | |
8361 | init_color_table (); | |
8362 | ||
c21e6151 YM |
8363 | /* Handle alpha channel by combining the image with a background |
8364 | color. */ | |
8365 | specified_bg = image_spec_value (img->spec, QCbackground, NULL); | |
8d3c4e79 CY |
8366 | if (!STRINGP (specified_bg) |
8367 | || !x_defined_color (f, SDATA (specified_bg), &background, 0)) | |
c21e6151 | 8368 | { |
d7e008d0 | 8369 | #ifndef HAVE_NS |
c21e6151 YM |
8370 | background.pixel = FRAME_BACKGROUND_PIXEL (f); |
8371 | x_query_color (f, &background); | |
d7e008d0 AR |
8372 | #else |
8373 | ns_query_color(FRAME_BACKGROUND_COLOR (f), &background, 1); | |
8374 | #endif | |
c21e6151 | 8375 | } |
4ab27a43 | 8376 | |
8d3c4e79 CY |
8377 | /* SVG pixmaps specify transparency in the last byte, so right |
8378 | shift 8 bits to get rid of it, since emacs doesn't support | |
8379 | transparency. */ | |
8380 | background.red >>= 8; | |
8381 | background.green >>= 8; | |
8382 | background.blue >>= 8; | |
8383 | ||
4ab27a43 GM |
8384 | /* This loop handles opacity values, since Emacs assumes |
8385 | non-transparent images. Each pixel must be "flattened" by | |
bbe6f2aa JB |
8386 | calculating the resulting color, given the transparency of the |
8387 | pixel, and the image background color. */ | |
4ab27a43 GM |
8388 | for (y = 0; y < height; ++y) |
8389 | { | |
8390 | for (x = 0; x < width; ++x) | |
8391 | { | |
8392 | unsigned red; | |
8393 | unsigned green; | |
8394 | unsigned blue; | |
8395 | unsigned opacity; | |
8396 | ||
8397 | red = *pixels++; | |
8398 | green = *pixels++; | |
8399 | blue = *pixels++; | |
8400 | opacity = *pixels++; | |
8401 | ||
8402 | red = ((red * opacity) | |
8403 | + (background.red * ((1 << 8) - opacity))); | |
8404 | green = ((green * opacity) | |
8405 | + (background.green * ((1 << 8) - opacity))); | |
8406 | blue = ((blue * opacity) | |
8407 | + (background.blue * ((1 << 8) - opacity))); | |
8408 | ||
8409 | XPutPixel (ximg, x, y, lookup_rgb_color (f, red, green, blue)); | |
8410 | } | |
8411 | ||
8412 | pixels += rowstride - 4 * width; | |
8413 | } | |
8414 | ||
8415 | #ifdef COLOR_TABLE_SUPPORT | |
8416 | /* Remember colors allocated for this image. */ | |
8417 | img->colors = colors_in_color_table (&img->ncolors); | |
8418 | free_color_table (); | |
8419 | #endif /* COLOR_TABLE_SUPPORT */ | |
8420 | ||
5fc9fdeb | 8421 | fn_g_object_unref (pixbuf); |
4ab27a43 | 8422 | |
c21e6151 YM |
8423 | img->width = width; |
8424 | img->height = height; | |
8425 | ||
8426 | /* Maybe fill in the background field while we have ximg handy. | |
8427 | Casting avoids a GCC warning. */ | |
8428 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
8429 | ||
4ab27a43 GM |
8430 | /* Put the image into the pixmap, then free the X image and its |
8431 | buffer. */ | |
8432 | x_put_x_image (f, ximg, img->pixmap, width, height); | |
8433 | x_destroy_x_image (ximg); | |
8434 | ||
4ab27a43 GM |
8435 | return 1; |
8436 | ||
8437 | rsvg_error: | |
e025c9c2 | 8438 | fn_g_object_unref (rsvg_handle); |
4ab27a43 GM |
8439 | /* FIXME: Use error->message so the user knows what is the actual |
8440 | problem with the image. */ | |
8441 | image_error ("Error parsing SVG image `%s'", img->spec, Qnil); | |
5fc9fdeb | 8442 | fn_g_error_free (error); |
4ab27a43 GM |
8443 | return 0; |
8444 | } | |
8445 | ||
8446 | #endif /* defined (HAVE_RSVG) */ | |
8447 | ||
8448 | ||
8449 | ||
4ef0d4d0 KS |
8450 | \f |
8451 | /*********************************************************************** | |
8452 | Ghostscript | |
8453 | ***********************************************************************/ | |
8454 | ||
8455 | #ifdef HAVE_X_WINDOWS | |
8456 | #define HAVE_GHOSTSCRIPT 1 | |
8457 | #endif /* HAVE_X_WINDOWS */ | |
8458 | ||
8459 | /* The symbol `postscript' identifying images of this type. */ | |
8460 | ||
8461 | Lisp_Object Qpostscript; | |
8462 | ||
8463 | #ifdef HAVE_GHOSTSCRIPT | |
8464 | ||
8465 | static int gs_image_p P_ ((Lisp_Object object)); | |
8466 | static int gs_load P_ ((struct frame *f, struct image *img)); | |
8467 | static void gs_clear_image P_ ((struct frame *f, struct image *img)); | |
8468 | ||
8469 | /* Keyword symbols. */ | |
8470 | ||
8471 | Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height; | |
8472 | ||
8473 | /* Indices of image specification fields in gs_format, below. */ | |
8474 | ||
8475 | enum gs_keyword_index | |
8476 | { | |
8477 | GS_TYPE, | |
8478 | GS_PT_WIDTH, | |
8479 | GS_PT_HEIGHT, | |
8480 | GS_FILE, | |
8481 | GS_LOADER, | |
8482 | GS_BOUNDING_BOX, | |
8483 | GS_ASCENT, | |
8484 | GS_MARGIN, | |
8485 | GS_RELIEF, | |
8486 | GS_ALGORITHM, | |
8487 | GS_HEURISTIC_MASK, | |
8488 | GS_MASK, | |
8489 | GS_BACKGROUND, | |
8490 | GS_LAST | |
8491 | }; | |
8492 | ||
8493 | /* Vector of image_keyword structures describing the format | |
8494 | of valid user-defined image specifications. */ | |
8495 | ||
91433552 | 8496 | static const struct image_keyword gs_format[GS_LAST] = |
4ef0d4d0 KS |
8497 | { |
8498 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8499 | {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8500 | {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8501 | {":file", IMAGE_STRING_VALUE, 1}, | |
8502 | {":loader", IMAGE_FUNCTION_VALUE, 0}, | |
8503 | {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1}, | |
8504 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
8505 | {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0}, | |
8506 | {":relief", IMAGE_INTEGER_VALUE, 0}, | |
8507 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8508 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8509 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8510 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8511 | }; | |
8512 | ||
8513 | /* Structure describing the image type `ghostscript'. */ | |
8514 | ||
8515 | static struct image_type gs_type = | |
8516 | { | |
8517 | &Qpostscript, | |
8518 | gs_image_p, | |
8519 | gs_load, | |
8520 | gs_clear_image, | |
8521 | NULL | |
8522 | }; | |
8523 | ||
8524 | ||
8525 | /* Free X resources of Ghostscript image IMG which is used on frame F. */ | |
8526 | ||
8527 | static void | |
8528 | gs_clear_image (f, img) | |
8529 | struct frame *f; | |
8530 | struct image *img; | |
8531 | { | |
8532 | /* IMG->data.ptr_val may contain a recorded colormap. */ | |
8533 | xfree (img->data.ptr_val); | |
8534 | x_clear_image (f, img); | |
8535 | } | |
8536 | ||
8537 | ||
8538 | /* Return non-zero if OBJECT is a valid Ghostscript image | |
8539 | specification. */ | |
8540 | ||
8541 | static int | |
8542 | gs_image_p (object) | |
8543 | Lisp_Object object; | |
8544 | { | |
8545 | struct image_keyword fmt[GS_LAST]; | |
8546 | Lisp_Object tem; | |
8547 | int i; | |
8548 | ||
8549 | bcopy (gs_format, fmt, sizeof fmt); | |
8550 | ||
8551 | if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)) | |
8552 | return 0; | |
8553 | ||
8554 | /* Bounding box must be a list or vector containing 4 integers. */ | |
8555 | tem = fmt[GS_BOUNDING_BOX].value; | |
8556 | if (CONSP (tem)) | |
8557 | { | |
8558 | for (i = 0; i < 4; ++i, tem = XCDR (tem)) | |
8559 | if (!CONSP (tem) || !INTEGERP (XCAR (tem))) | |
8560 | return 0; | |
8561 | if (!NILP (tem)) | |
8562 | return 0; | |
8563 | } | |
8564 | else if (VECTORP (tem)) | |
8565 | { | |
8566 | if (XVECTOR (tem)->size != 4) | |
8567 | return 0; | |
8568 | for (i = 0; i < 4; ++i) | |
8569 | if (!INTEGERP (XVECTOR (tem)->contents[i])) | |
8570 | return 0; | |
8571 | } | |
8572 | else | |
8573 | return 0; | |
8574 | ||
8575 | return 1; | |
8576 | } | |
8577 | ||
8578 | ||
8579 | /* Load Ghostscript image IMG for use on frame F. Value is non-zero | |
8580 | if successful. */ | |
8581 | ||
8582 | static int | |
8583 | gs_load (f, img) | |
8584 | struct frame *f; | |
8585 | struct image *img; | |
8586 | { | |
8587 | char buffer[100]; | |
8588 | Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width; | |
8589 | struct gcpro gcpro1, gcpro2; | |
8590 | Lisp_Object frame; | |
8591 | double in_width, in_height; | |
8592 | Lisp_Object pixel_colors = Qnil; | |
8593 | ||
8594 | /* Compute pixel size of pixmap needed from the given size in the | |
8595 | image specification. Sizes in the specification are in pt. 1 pt | |
8596 | = 1/72 in, xdpi and ydpi are stored in the frame's X display | |
8597 | info. */ | |
8598 | pt_width = image_spec_value (img->spec, QCpt_width, NULL); | |
8599 | in_width = XFASTINT (pt_width) / 72.0; | |
8600 | img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx; | |
8601 | pt_height = image_spec_value (img->spec, QCpt_height, NULL); | |
8602 | in_height = XFASTINT (pt_height) / 72.0; | |
8603 | img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy; | |
8604 | ||
f1f25b99 CY |
8605 | if (!check_image_size (f, img->width, img->height)) |
8606 | { | |
10ea2b82 | 8607 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
8608 | return 0; |
8609 | } | |
8610 | ||
4ef0d4d0 KS |
8611 | /* Create the pixmap. */ |
8612 | xassert (img->pixmap == NO_PIXMAP); | |
8613 | ||
8614 | /* Only W32 version did BLOCK_INPUT here. ++kfs */ | |
8615 | BLOCK_INPUT; | |
8616 | img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
8617 | img->width, img->height, | |
8618 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))); | |
8619 | UNBLOCK_INPUT; | |
8620 | ||
8621 | if (!img->pixmap) | |
8622 | { | |
8623 | image_error ("Unable to create pixmap for `%s'", img->spec, Qnil); | |
8624 | return 0; | |
8625 | } | |
8626 | ||
8627 | /* Call the loader to fill the pixmap. It returns a process object | |
8628 | if successful. We do not record_unwind_protect here because | |
8629 | other places in redisplay like calling window scroll functions | |
8630 | don't either. Let the Lisp loader use `unwind-protect' instead. */ | |
8631 | GCPRO2 (window_and_pixmap_id, pixel_colors); | |
8632 | ||
8633 | sprintf (buffer, "%lu %lu", | |
8634 | (unsigned long) FRAME_X_WINDOW (f), | |
8635 | (unsigned long) img->pixmap); | |
8636 | window_and_pixmap_id = build_string (buffer); | |
8637 | ||
8638 | sprintf (buffer, "%lu %lu", | |
8639 | FRAME_FOREGROUND_PIXEL (f), | |
8640 | FRAME_BACKGROUND_PIXEL (f)); | |
8641 | pixel_colors = build_string (buffer); | |
8642 | ||
8643 | XSETFRAME (frame, f); | |
8644 | loader = image_spec_value (img->spec, QCloader, NULL); | |
8645 | if (NILP (loader)) | |
8646 | loader = intern ("gs-load-image"); | |
8647 | ||
8648 | img->data.lisp_val = call6 (loader, frame, img->spec, | |
8649 | make_number (img->width), | |
8650 | make_number (img->height), | |
8651 | window_and_pixmap_id, | |
8652 | pixel_colors); | |
8653 | UNGCPRO; | |
8654 | return PROCESSP (img->data.lisp_val); | |
8655 | } | |
8656 | ||
8657 | ||
8658 | /* Kill the Ghostscript process that was started to fill PIXMAP on | |
8659 | frame F. Called from XTread_socket when receiving an event | |
8660 | telling Emacs that Ghostscript has finished drawing. */ | |
8661 | ||
8662 | void | |
8663 | x_kill_gs_process (pixmap, f) | |
8664 | Pixmap pixmap; | |
8665 | struct frame *f; | |
8666 | { | |
354884c4 | 8667 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
8668 | int class, i; |
8669 | struct image *img; | |
8670 | ||
8671 | /* Find the image containing PIXMAP. */ | |
8672 | for (i = 0; i < c->used; ++i) | |
8673 | if (c->images[i]->pixmap == pixmap) | |
8674 | break; | |
8675 | ||
8676 | /* Should someone in between have cleared the image cache, for | |
8677 | instance, give up. */ | |
8678 | if (i == c->used) | |
8679 | return; | |
8680 | ||
8681 | /* Kill the GS process. We should have found PIXMAP in the image | |
8682 | cache and its image should contain a process object. */ | |
8683 | img = c->images[i]; | |
8684 | xassert (PROCESSP (img->data.lisp_val)); | |
8685 | Fkill_process (img->data.lisp_val, Qnil); | |
8686 | img->data.lisp_val = Qnil; | |
8687 | ||
8688 | #if defined (HAVE_X_WINDOWS) | |
8689 | ||
8690 | /* On displays with a mutable colormap, figure out the colors | |
8691 | allocated for the image by looking at the pixels of an XImage for | |
8692 | img->pixmap. */ | |
8693 | class = FRAME_X_VISUAL (f)->class; | |
8694 | if (class != StaticColor && class != StaticGray && class != TrueColor) | |
8695 | { | |
8696 | XImagePtr ximg; | |
8697 | ||
8698 | BLOCK_INPUT; | |
8699 | ||
8700 | /* Try to get an XImage for img->pixmep. */ | |
8701 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, | |
8702 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
8703 | if (ximg) | |
8704 | { | |
8705 | int x, y; | |
8706 | ||
8707 | /* Initialize the color table. */ | |
8708 | init_color_table (); | |
8709 | ||
8710 | /* For each pixel of the image, look its color up in the | |
8711 | color table. After having done so, the color table will | |
8712 | contain an entry for each color used by the image. */ | |
8713 | for (y = 0; y < img->height; ++y) | |
8714 | for (x = 0; x < img->width; ++x) | |
8715 | { | |
8716 | unsigned long pixel = XGetPixel (ximg, x, y); | |
8717 | lookup_pixel_color (f, pixel); | |
8718 | } | |
8719 | ||
8720 | /* Record colors in the image. Free color table and XImage. */ | |
8721 | #ifdef COLOR_TABLE_SUPPORT | |
8722 | img->colors = colors_in_color_table (&img->ncolors); | |
8723 | free_color_table (); | |
8724 | #endif | |
8725 | XDestroyImage (ximg); | |
8726 | ||
8727 | #if 0 /* This doesn't seem to be the case. If we free the colors | |
8728 | here, we get a BadAccess later in x_clear_image when | |
8729 | freeing the colors. */ | |
8730 | /* We have allocated colors once, but Ghostscript has also | |
8731 | allocated colors on behalf of us. So, to get the | |
8732 | reference counts right, free them once. */ | |
8733 | if (img->ncolors) | |
8734 | x_free_colors (f, img->colors, img->ncolors); | |
8735 | #endif | |
8736 | } | |
8737 | else | |
8738 | image_error ("Cannot get X image of `%s'; colors will not be freed", | |
8739 | img->spec, Qnil); | |
8740 | ||
8741 | UNBLOCK_INPUT; | |
8742 | } | |
8743 | #endif /* HAVE_X_WINDOWS */ | |
8744 | ||
8745 | /* Now that we have the pixmap, compute mask and transform the | |
8746 | image if requested. */ | |
8747 | BLOCK_INPUT; | |
8748 | postprocess_image (f, img); | |
8749 | UNBLOCK_INPUT; | |
8750 | } | |
8751 | ||
8752 | #endif /* HAVE_GHOSTSCRIPT */ | |
8753 | ||
8754 | \f | |
8755 | /*********************************************************************** | |
8756 | Tests | |
8757 | ***********************************************************************/ | |
8758 | ||
8759 | #if GLYPH_DEBUG | |
8760 | ||
8761 | DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0, | |
8762 | doc: /* Value is non-nil if SPEC is a valid image specification. */) | |
8763 | (spec) | |
8764 | Lisp_Object spec; | |
8765 | { | |
8766 | return valid_image_p (spec) ? Qt : Qnil; | |
8767 | } | |
8768 | ||
8769 | ||
8770 | DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "") | |
8771 | (spec) | |
8772 | Lisp_Object spec; | |
8773 | { | |
8774 | int id = -1; | |
8775 | ||
8776 | if (valid_image_p (spec)) | |
8777 | id = lookup_image (SELECTED_FRAME (), spec); | |
8778 | ||
8779 | debug_print (spec); | |
8780 | return make_number (id); | |
8781 | } | |
8782 | ||
8783 | #endif /* GLYPH_DEBUG != 0 */ | |
8784 | ||
8785 | ||
8786 | /*********************************************************************** | |
8787 | Initialization | |
8788 | ***********************************************************************/ | |
8789 | ||
0855eb52 JB |
8790 | #ifdef HAVE_NTGUI |
8791 | /* Image types that rely on external libraries are loaded dynamically | |
8792 | if the library is available. */ | |
adac86db | 8793 | #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \ |
0855eb52 JB |
8794 | define_image_type (image_type, init_lib_fn (libraries)) |
8795 | #else | |
adac86db | 8796 | #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \ |
e0e30823 | 8797 | define_image_type (image_type, 1) |
0855eb52 JB |
8798 | #endif /* HAVE_NTGUI */ |
8799 | ||
8800 | DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 2, 2, 0, | |
8801 | doc: /* Initialize image library implementing image type TYPE. | |
8802 | Return non-nil if TYPE is a supported image type. | |
8803 | ||
8804 | Image types pbm and xbm are prebuilt; other types are loaded here. | |
8805 | Libraries to load are specified in alist LIBRARIES (usually, the value | |
adac86db | 8806 | of `image-library-alist', which see). */) |
0855eb52 | 8807 | (type, libraries) |
adac86db | 8808 | Lisp_Object type, libraries; |
0855eb52 JB |
8809 | { |
8810 | Lisp_Object tested; | |
8811 | ||
8812 | /* Don't try to reload the library. */ | |
8813 | tested = Fassq (type, Vimage_type_cache); | |
8814 | if (CONSP (tested)) | |
8815 | return XCDR (tested); | |
8816 | ||
9e2a2647 | 8817 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
0855eb52 | 8818 | if (EQ (type, Qxpm)) |
adac86db | 8819 | return CHECK_LIB_AVAILABLE (&xpm_type, init_xpm_functions, libraries); |
0855eb52 JB |
8820 | #endif |
8821 | ||
9e2a2647 | 8822 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
0855eb52 | 8823 | if (EQ (type, Qjpeg)) |
adac86db | 8824 | return CHECK_LIB_AVAILABLE (&jpeg_type, init_jpeg_functions, libraries); |
0855eb52 JB |
8825 | #endif |
8826 | ||
9e2a2647 | 8827 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
0855eb52 | 8828 | if (EQ (type, Qtiff)) |
adac86db | 8829 | return CHECK_LIB_AVAILABLE (&tiff_type, init_tiff_functions, libraries); |
0855eb52 JB |
8830 | #endif |
8831 | ||
9e2a2647 | 8832 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
0855eb52 | 8833 | if (EQ (type, Qgif)) |
adac86db | 8834 | return CHECK_LIB_AVAILABLE (&gif_type, init_gif_functions, libraries); |
0855eb52 JB |
8835 | #endif |
8836 | ||
9e2a2647 | 8837 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
0855eb52 | 8838 | if (EQ (type, Qpng)) |
adac86db | 8839 | return CHECK_LIB_AVAILABLE (&png_type, init_png_functions, libraries); |
0855eb52 JB |
8840 | #endif |
8841 | ||
4ab27a43 GM |
8842 | #if defined (HAVE_RSVG) |
8843 | if (EQ (type, Qsvg)) | |
8844 | return CHECK_LIB_AVAILABLE (&svg_type, init_svg_functions, libraries); | |
8845 | #endif | |
c21e6151 | 8846 | |
3de25479 JV |
8847 | #if defined (HAVE_IMAGEMAGICK) |
8848 | if (EQ (type, Qimagemagick)){ | |
8849 | /* MagickWandGenesis() initalizes the imagemagick library. */ | |
8850 | MagickWandGenesis(); | |
d2a2d277 JV |
8851 | return CHECK_LIB_AVAILABLE (&imagemagick_type, init_imagemagick_functions, |
8852 | libraries); | |
3de25479 JV |
8853 | } |
8854 | #endif | |
8855 | ||
0855eb52 JB |
8856 | #ifdef HAVE_GHOSTSCRIPT |
8857 | if (EQ (type, Qpostscript)) | |
adac86db | 8858 | return CHECK_LIB_AVAILABLE (&gs_type, init_gs_functions, libraries); |
0855eb52 JB |
8859 | #endif |
8860 | ||
8861 | /* If the type is not recognized, avoid testing it ever again. */ | |
adac86db | 8862 | CACHE_IMAGE_TYPE (type, Qnil); |
0855eb52 JB |
8863 | return Qnil; |
8864 | } | |
8865 | ||
3de25479 | 8866 | |
4ef0d4d0 KS |
8867 | void |
8868 | syms_of_image () | |
8869 | { | |
2a611c1a JB |
8870 | extern Lisp_Object Qrisky_local_variable; /* Syms_of_xdisp has already run. */ |
8871 | ||
6e6fc3fd RS |
8872 | /* Initialize this only once, since that's what we do with Vimage_types |
8873 | and they are supposed to be in sync. Initializing here gives correct | |
8874 | operation on GNU/Linux of calling dump-emacs after loading some images. */ | |
8875 | image_types = NULL; | |
8876 | ||
0855eb52 JB |
8877 | /* Must be defined now becase we're going to update it below, while |
8878 | defining the supported image types. */ | |
8879 | DEFVAR_LISP ("image-types", &Vimage_types, | |
8880 | doc: /* List of potentially supported image types. | |
bbe6f2aa | 8881 | Each element of the list is a symbol for an image type, like 'jpeg or 'png. |
0855eb52 JB |
8882 | To check whether it is really supported, use `image-type-available-p'. */); |
8883 | Vimage_types = Qnil; | |
8884 | ||
2a611c1a JB |
8885 | DEFVAR_LISP ("image-library-alist", &Vimage_library_alist, |
8886 | doc: /* Alist of image types vs external libraries needed to display them. | |
8887 | ||
8888 | Each element is a list (IMAGE-TYPE LIBRARY...), where the car is a symbol | |
8889 | representing a supported image type, and the rest are strings giving | |
8890 | alternate filenames for the corresponding external libraries. | |
8891 | ||
8892 | Emacs tries to load the libraries in the order they appear on the | |
8893 | list; if none is loaded, the running session of Emacs won't | |
8894 | support the image type. Types 'pbm and 'xbm don't need to be | |
3046a84b | 8895 | listed; they are always supported. */); |
2a611c1a | 8896 | Vimage_library_alist = Qnil; |
d67b4f80 | 8897 | Fput (intern_c_string ("image-library-alist"), Qrisky_local_variable, Qt); |
2a611c1a | 8898 | |
f1f25b99 | 8899 | DEFVAR_LISP ("max-image-size", &Vmax_image_size, |
7df4765a KS |
8900 | doc: /* Maximum size of images. |
8901 | Emacs will not load an image into memory if its pixel width or | |
8902 | pixel height exceeds this limit. | |
8903 | ||
8904 | If the value is an integer, it directly specifies the maximum | |
8905 | image height and width, measured in pixels. If it is a floating | |
8906 | point number, it specifies the maximum image height and width | |
8907 | as a ratio to the frame height and width. If the value is | |
8908 | non-numeric, there is no explicit limit on the size of images. */); | |
f1f25b99 CY |
8909 | Vmax_image_size = make_float (MAX_IMAGE_SIZE); |
8910 | ||
0855eb52 JB |
8911 | Vimage_type_cache = Qnil; |
8912 | staticpro (&Vimage_type_cache); | |
8913 | ||
d67b4f80 | 8914 | Qpbm = intern_c_string ("pbm"); |
7c4e3a32 | 8915 | staticpro (&Qpbm); |
5fc9fdeb | 8916 | ADD_IMAGE_TYPE (Qpbm); |
7c4e3a32 | 8917 | |
d67b4f80 | 8918 | Qxbm = intern_c_string ("xbm"); |
7c4e3a32 | 8919 | staticpro (&Qxbm); |
5fc9fdeb | 8920 | ADD_IMAGE_TYPE (Qxbm); |
7c4e3a32 RS |
8921 | |
8922 | define_image_type (&xbm_type, 1); | |
8923 | define_image_type (&pbm_type, 1); | |
8924 | ||
d67b4f80 | 8925 | Qcount = intern_c_string ("count"); |
e047f448 | 8926 | staticpro (&Qcount); |
1546c559 JL |
8927 | Qextension_data = intern_c_string ("extension-data"); |
8928 | staticpro (&Qextension_data); | |
e047f448 | 8929 | |
d67b4f80 | 8930 | QCascent = intern_c_string (":ascent"); |
4ef0d4d0 | 8931 | staticpro (&QCascent); |
d67b4f80 | 8932 | QCmargin = intern_c_string (":margin"); |
4ef0d4d0 | 8933 | staticpro (&QCmargin); |
d67b4f80 | 8934 | QCrelief = intern_c_string (":relief"); |
4ef0d4d0 | 8935 | staticpro (&QCrelief); |
d67b4f80 | 8936 | QCconversion = intern_c_string (":conversion"); |
4ef0d4d0 | 8937 | staticpro (&QCconversion); |
d67b4f80 | 8938 | QCcolor_symbols = intern_c_string (":color-symbols"); |
4ef0d4d0 | 8939 | staticpro (&QCcolor_symbols); |
d67b4f80 | 8940 | QCheuristic_mask = intern_c_string (":heuristic-mask"); |
4ef0d4d0 | 8941 | staticpro (&QCheuristic_mask); |
d67b4f80 | 8942 | QCindex = intern_c_string (":index"); |
4ef0d4d0 | 8943 | staticpro (&QCindex); |
3de25479 JV |
8944 | QCgeometry = intern (":geometry"); |
8945 | staticpro (&QCgeometry); | |
8946 | QCcrop = intern (":crop"); | |
8947 | staticpro (&QCcrop); | |
8948 | QCrotation = intern (":rotation"); | |
8949 | staticpro (&QCrotation); | |
d67b4f80 | 8950 | QCmatrix = intern_c_string (":matrix"); |
3de25479 | 8951 | |
4ef0d4d0 | 8952 | staticpro (&QCmatrix); |
d67b4f80 | 8953 | QCcolor_adjustment = intern_c_string (":color-adjustment"); |
4ef0d4d0 | 8954 | staticpro (&QCcolor_adjustment); |
d67b4f80 | 8955 | QCmask = intern_c_string (":mask"); |
4ef0d4d0 KS |
8956 | staticpro (&QCmask); |
8957 | ||
d67b4f80 | 8958 | Qlaplace = intern_c_string ("laplace"); |
4ef0d4d0 | 8959 | staticpro (&Qlaplace); |
d67b4f80 | 8960 | Qemboss = intern_c_string ("emboss"); |
4ef0d4d0 | 8961 | staticpro (&Qemboss); |
d67b4f80 | 8962 | Qedge_detection = intern_c_string ("edge-detection"); |
4ef0d4d0 | 8963 | staticpro (&Qedge_detection); |
d67b4f80 | 8964 | Qheuristic = intern_c_string ("heuristic"); |
4ef0d4d0 | 8965 | staticpro (&Qheuristic); |
4ef0d4d0 | 8966 | |
d67b4f80 | 8967 | Qpostscript = intern_c_string ("postscript"); |
4ef0d4d0 KS |
8968 | staticpro (&Qpostscript); |
8969 | #ifdef HAVE_GHOSTSCRIPT | |
5fc9fdeb | 8970 | ADD_IMAGE_TYPE (Qpostscript); |
d67b4f80 | 8971 | QCloader = intern_c_string (":loader"); |
4ef0d4d0 | 8972 | staticpro (&QCloader); |
d67b4f80 | 8973 | QCbounding_box = intern_c_string (":bounding-box"); |
4ef0d4d0 | 8974 | staticpro (&QCbounding_box); |
d67b4f80 | 8975 | QCpt_width = intern_c_string (":pt-width"); |
4ef0d4d0 | 8976 | staticpro (&QCpt_width); |
d67b4f80 | 8977 | QCpt_height = intern_c_string (":pt-height"); |
4ef0d4d0 KS |
8978 | staticpro (&QCpt_height); |
8979 | #endif /* HAVE_GHOSTSCRIPT */ | |
8980 | ||
9e2a2647 | 8981 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
d67b4f80 | 8982 | Qxpm = intern_c_string ("xpm"); |
4ef0d4d0 | 8983 | staticpro (&Qxpm); |
5fc9fdeb | 8984 | ADD_IMAGE_TYPE (Qxpm); |
4ef0d4d0 KS |
8985 | #endif |
8986 | ||
9e2a2647 | 8987 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
d67b4f80 | 8988 | Qjpeg = intern_c_string ("jpeg"); |
4ef0d4d0 | 8989 | staticpro (&Qjpeg); |
5fc9fdeb | 8990 | ADD_IMAGE_TYPE (Qjpeg); |
4ef0d4d0 KS |
8991 | #endif |
8992 | ||
9e2a2647 | 8993 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
d67b4f80 | 8994 | Qtiff = intern_c_string ("tiff"); |
4ef0d4d0 | 8995 | staticpro (&Qtiff); |
5fc9fdeb | 8996 | ADD_IMAGE_TYPE (Qtiff); |
4ef0d4d0 KS |
8997 | #endif |
8998 | ||
9e2a2647 | 8999 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
d67b4f80 | 9000 | Qgif = intern_c_string ("gif"); |
4ef0d4d0 | 9001 | staticpro (&Qgif); |
5fc9fdeb | 9002 | ADD_IMAGE_TYPE (Qgif); |
4ef0d4d0 KS |
9003 | #endif |
9004 | ||
9e2a2647 | 9005 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
d67b4f80 | 9006 | Qpng = intern_c_string ("png"); |
4ef0d4d0 | 9007 | staticpro (&Qpng); |
5fc9fdeb | 9008 | ADD_IMAGE_TYPE (Qpng); |
4ef0d4d0 KS |
9009 | #endif |
9010 | ||
3de25479 JV |
9011 | #if defined (HAVE_IMAGEMAGICK) |
9012 | Qimagemagick = intern ("imagemagick"); | |
9013 | staticpro (&Qimagemagick); | |
9014 | ADD_IMAGE_TYPE (Qimagemagick); | |
9015 | #endif | |
9016 | ||
4ab27a43 | 9017 | #if defined (HAVE_RSVG) |
d67b4f80 | 9018 | Qsvg = intern_c_string ("svg"); |
4ab27a43 | 9019 | staticpro (&Qsvg); |
5fc9fdeb JR |
9020 | ADD_IMAGE_TYPE (Qsvg); |
9021 | #ifdef HAVE_NTGUI | |
df23bf08 | 9022 | /* Other libraries used directly by svg code. */ |
d67b4f80 | 9023 | Qgdk_pixbuf = intern_c_string ("gdk-pixbuf"); |
5fc9fdeb | 9024 | staticpro (&Qgdk_pixbuf); |
d67b4f80 | 9025 | Qglib = intern_c_string ("glib"); |
5fc9fdeb | 9026 | staticpro (&Qglib); |
d67b4f80 | 9027 | Qgobject = intern_c_string ("gobject"); |
df23bf08 | 9028 | staticpro (&Qgobject); |
5fc9fdeb JR |
9029 | #endif /* HAVE_NTGUI */ |
9030 | #endif /* HAVE_RSVG */ | |
4ab27a43 | 9031 | |
0855eb52 | 9032 | defsubr (&Sinit_image_library); |
3de25479 JV |
9033 | #ifdef HAVE_IMAGEMAGICK |
9034 | defsubr (&Simagemagick_types); | |
9035 | #endif | |
4ef0d4d0 | 9036 | defsubr (&Sclear_image_cache); |
110683ad | 9037 | defsubr (&Simage_flush); |
4ef0d4d0 KS |
9038 | defsubr (&Simage_size); |
9039 | defsubr (&Simage_mask_p); | |
1546c559 | 9040 | defsubr (&Simage_metadata); |
4ef0d4d0 KS |
9041 | |
9042 | #if GLYPH_DEBUG | |
9043 | defsubr (&Simagep); | |
9044 | defsubr (&Slookup_image); | |
9045 | #endif | |
9046 | ||
9047 | DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images, | |
9048 | doc: /* Non-nil means always draw a cross over disabled images. | |
3046a84b | 9049 | Disabled images are those having a `:conversion disabled' property. |
4ef0d4d0 KS |
9050 | A cross is always drawn on black & white displays. */); |
9051 | cross_disabled_images = 0; | |
9052 | ||
9053 | DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path, | |
9054 | doc: /* List of directories to search for window system bitmap files. */); | |
9055 | Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS); | |
9056 | ||
9057 | DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay, | |
98fe5161 CY |
9058 | doc: /* Maximum time after which images are removed from the cache. |
9059 | When an image has not been displayed this many seconds, Emacs | |
9060 | automatically removes it from the image cache. If the cache contains | |
9061 | a large number of images, the actual eviction time may be shorter. | |
9062 | The value can also be nil, meaning the cache is never cleared. | |
98fe5161 CY |
9063 | The function `clear-image-cache' disregards this variable. */); |
9064 | Vimage_cache_eviction_delay = make_number (300); | |
3de25479 JV |
9065 | #ifdef HAVE_IMAGEMAGICK |
9066 | DEFVAR_LISP ("imagemagick-render-type", &Vimagemagick_render_type, | |
4917006b | 9067 | doc: /* Choose between ImageMagick render methods. */); |
3de25479 | 9068 | #endif |
4ef0d4d0 | 9069 | |
4917006b JV |
9070 | } |
9071 | ||
9072 | ||
4ef0d4d0 KS |
9073 | void |
9074 | init_image () | |
9075 | { | |
3de25479 | 9076 | |
4ef0d4d0 KS |
9077 | } |
9078 | ||
3de25479 JV |
9079 | |
9080 | ||
1b9f8cec MB |
9081 | /* arch-tag: 123c2a5e-14a8-4c53-ab95-af47d7db49b9 |
9082 | (do not change this comment) */ |