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