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