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