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