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