Commit | Line | Data |
---|---|---|
4ef0d4d0 | 1 | /* Functions for image support on window system. |
32d72c2f | 2 | |
ab422c4d | 3 | Copyright (C) 1989, 1992-2013 Free Software Foundation, Inc. |
4ef0d4d0 KS |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
4ef0d4d0 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
4ef0d4d0 KS |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ef0d4d0 KS |
19 | |
20 | #include <config.h> | |
406af475 | 21 | #include "sysstdio.h" |
4ef0d4d0 | 22 | #include <unistd.h> |
4ef0d4d0 | 23 | |
d7306fe6 DN |
24 | #ifdef HAVE_PNG |
25 | #if defined HAVE_LIBPNG_PNG_H | |
26 | # include <libpng/png.h> | |
27 | #else | |
28 | # include <png.h> | |
29 | #endif | |
285d07e2 | 30 | #endif |
d7306fe6 | 31 | |
c3417a74 DN |
32 | #include <setjmp.h> |
33 | ||
620f13b0 PE |
34 | #include <c-ctype.h> |
35 | ||
4ef0d4d0 KS |
36 | /* This makes the fields of a Display accessible, in Xlib header files. */ |
37 | ||
38 | #define XLIB_ILLEGAL_ACCESS | |
39 | ||
40 | #include "lisp.h" | |
41 | #include "frame.h" | |
42 | #include "window.h" | |
43 | #include "dispextern.h" | |
44 | #include "blockinput.h" | |
45 | #include "systime.h" | |
46 | #include <epaths.h> | |
3dcc8b84 | 47 | #include "character.h" |
022af124 | 48 | #include "coding.h" |
6087292e | 49 | #include "termhooks.h" |
3dcc8b84 | 50 | #include "font.h" |
4ef0d4d0 | 51 | |
17a2cbbd | 52 | #ifdef HAVE_SYS_STAT_H |
4ef0d4d0 | 53 | #include <sys/stat.h> |
17a2cbbd DC |
54 | #endif /* HAVE_SYS_STAT_H */ |
55 | ||
56 | #ifdef HAVE_SYS_TYPES_H | |
57 | #include <sys/types.h> | |
58 | #endif /* HAVE_SYS_TYPES_H */ | |
59 | ||
60 | #ifdef HAVE_WINDOW_SYSTEM | |
61 | #include TERM_HEADER | |
62 | #endif /* HAVE_WINDOW_SYSTEM */ | |
4ef0d4d0 | 63 | |
17a2cbbd | 64 | #ifdef HAVE_X_WINDOWS |
4ef0d4d0 KS |
65 | #define COLOR_TABLE_SUPPORT 1 |
66 | ||
67 | typedef struct x_bitmap_record Bitmap_Record; | |
5e617bc2 | 68 | #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y) |
4ef0d4d0 | 69 | #define NO_PIXMAP None |
4ef0d4d0 | 70 | |
9ecf6403 YM |
71 | #define PIX_MASK_RETAIN 0 |
72 | #define PIX_MASK_DRAW 1 | |
4ef0d4d0 KS |
73 | #endif /* HAVE_X_WINDOWS */ |
74 | ||
4ef0d4d0 | 75 | #ifdef HAVE_NTGUI |
a9e7a9d5 PE |
76 | |
77 | /* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */ | |
78 | #ifdef WINDOWSNT | |
0fda9b75 | 79 | # include "w32.h" |
a9e7a9d5 PE |
80 | #endif |
81 | ||
4ef0d4d0 KS |
82 | /* W32_TODO : Color tables on W32. */ |
83 | #undef COLOR_TABLE_SUPPORT | |
84 | ||
85 | typedef struct w32_bitmap_record Bitmap_Record; | |
5e617bc2 | 86 | #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y) |
4ef0d4d0 | 87 | #define NO_PIXMAP 0 |
4ef0d4d0 | 88 | |
9ecf6403 YM |
89 | #define PIX_MASK_RETAIN 0 |
90 | #define PIX_MASK_DRAW 1 | |
4ef0d4d0 | 91 | |
4ef0d4d0 KS |
92 | #define x_defined_color w32_defined_color |
93 | #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits) | |
8b7d0a16 | 94 | |
5be1c984 EZ |
95 | /* Version of libpng that we were compiled with, or -1 if no PNG |
96 | support was compiled in. This is tested by w32-win.el to correctly | |
97 | set up the alist used to search for PNG libraries. */ | |
98 | Lisp_Object Qlibpng_version; | |
4ef0d4d0 KS |
99 | #endif /* HAVE_NTGUI */ |
100 | ||
edfda783 | 101 | #ifdef HAVE_NS |
edfda783 AR |
102 | #undef COLOR_TABLE_SUPPORT |
103 | ||
104 | typedef struct ns_bitmap_record Bitmap_Record; | |
105 | ||
5e617bc2 | 106 | #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y) |
edfda783 AR |
107 | #define NO_PIXMAP 0 |
108 | ||
edfda783 AR |
109 | #define PIX_MASK_RETAIN 0 |
110 | #define PIX_MASK_DRAW 1 | |
111 | ||
edfda783 AR |
112 | #define x_defined_color(f, name, color_def, alloc) \ |
113 | ns_defined_color (f, name, color_def, alloc, 0) | |
9e50ff0c | 114 | #define DefaultDepthOfScreen(screen) x_display_list->n_planes |
edfda783 AR |
115 | #endif /* HAVE_NS */ |
116 | ||
117 | ||
97ec208f DN |
118 | /* The symbol `postscript' identifying images of this type. */ |
119 | ||
955cbe7b | 120 | static Lisp_Object Qpostscript; |
4ef0d4d0 | 121 | |
f57e2426 J |
122 | static void x_disable_image (struct frame *, struct image *); |
123 | static void x_edge_detection (struct frame *, struct image *, Lisp_Object, | |
124 | Lisp_Object); | |
4ef0d4d0 | 125 | |
f57e2426 J |
126 | static void init_color_table (void); |
127 | static unsigned long lookup_rgb_color (struct frame *f, int r, int g, int b); | |
4ef0d4d0 | 128 | #ifdef COLOR_TABLE_SUPPORT |
f57e2426 J |
129 | static void free_color_table (void); |
130 | static unsigned long *colors_in_color_table (int *n); | |
4ef0d4d0 KS |
131 | #endif |
132 | ||
9c90cc06 | 133 | static Lisp_Object QCmax_width, QCmax_height; |
f3f9606c | 134 | |
4ef0d4d0 KS |
135 | /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap |
136 | id, which is just an int that this section returns. Bitmaps are | |
137 | reference counted so they can be shared among frames. | |
138 | ||
139 | Bitmap indices are guaranteed to be > 0, so a negative number can | |
140 | be used to indicate no bitmap. | |
141 | ||
142 | If you use x_create_bitmap_from_data, then you must keep track of | |
143 | the bitmaps yourself. That is, creating a bitmap from the same | |
144 | data more than once will not be caught. */ | |
145 | ||
edfda783 | 146 | #ifdef HAVE_NS |
bad98418 | 147 | /* Use with images created by ns_image_for_XPM. */ |
edfda783 AR |
148 | unsigned long |
149 | XGetPixel (XImagePtr ximage, int x, int y) | |
150 | { | |
ed3751c8 | 151 | return ns_get_pixel (ximage, x, y); |
edfda783 AR |
152 | } |
153 | ||
bad98418 PE |
154 | /* Use with images created by ns_image_for_XPM; alpha set to 1; |
155 | pixel is assumed to be in RGB form. */ | |
edfda783 AR |
156 | void |
157 | XPutPixel (XImagePtr ximage, int x, int y, unsigned long pixel) | |
158 | { | |
ed3751c8 | 159 | ns_put_pixel (ximage, x, y, pixel); |
edfda783 AR |
160 | } |
161 | #endif /* HAVE_NS */ | |
162 | ||
4ef0d4d0 KS |
163 | |
164 | /* Functions to access the contents of a bitmap, given an id. */ | |
165 | ||
166 | int | |
0766b489 | 167 | x_bitmap_height (FRAME_PTR f, ptrdiff_t id) |
4ef0d4d0 KS |
168 | { |
169 | return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height; | |
170 | } | |
171 | ||
172 | int | |
0766b489 | 173 | x_bitmap_width (FRAME_PTR f, ptrdiff_t id) |
4ef0d4d0 KS |
174 | { |
175 | return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width; | |
176 | } | |
177 | ||
178 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) | |
62aba0d4 | 179 | ptrdiff_t |
0766b489 | 180 | x_bitmap_pixmap (FRAME_PTR f, ptrdiff_t id) |
4ef0d4d0 | 181 | { |
f6cfbd8f | 182 | /* HAVE_NTGUI needs the explicit cast here. */ |
62aba0d4 | 183 | return (ptrdiff_t) FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap; |
4ef0d4d0 KS |
184 | } |
185 | #endif | |
186 | ||
187 | #ifdef HAVE_X_WINDOWS | |
188 | int | |
0766b489 | 189 | x_bitmap_mask (FRAME_PTR f, ptrdiff_t id) |
4ef0d4d0 KS |
190 | { |
191 | return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].mask; | |
192 | } | |
193 | #endif | |
194 | ||
195 | /* Allocate a new bitmap record. Returns index of new record. */ | |
196 | ||
0766b489 | 197 | static ptrdiff_t |
d3da34e0 | 198 | x_allocate_bitmap_record (FRAME_PTR f) |
4ef0d4d0 KS |
199 | { |
200 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
0766b489 | 201 | ptrdiff_t i; |
4ef0d4d0 | 202 | |
4ef0d4d0 KS |
203 | if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size) |
204 | return ++dpyinfo->bitmaps_last; | |
205 | ||
206 | for (i = 0; i < dpyinfo->bitmaps_size; ++i) | |
207 | if (dpyinfo->bitmaps[i].refcount == 0) | |
208 | return i + 1; | |
209 | ||
a69fbedb PE |
210 | dpyinfo->bitmaps = |
211 | xpalloc (dpyinfo->bitmaps, &dpyinfo->bitmaps_size, | |
212 | 10, -1, sizeof *dpyinfo->bitmaps); | |
4ef0d4d0 KS |
213 | return ++dpyinfo->bitmaps_last; |
214 | } | |
215 | ||
216 | /* Add one reference to the reference count of the bitmap with id ID. */ | |
217 | ||
218 | void | |
13464394 | 219 | x_reference_bitmap (FRAME_PTR f, ptrdiff_t id) |
4ef0d4d0 KS |
220 | { |
221 | ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount; | |
222 | } | |
223 | ||
224 | /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */ | |
225 | ||
0766b489 | 226 | ptrdiff_t |
d3da34e0 | 227 | x_create_bitmap_from_data (struct frame *f, char *bits, unsigned int width, unsigned int height) |
4ef0d4d0 KS |
228 | { |
229 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
0766b489 | 230 | ptrdiff_t id; |
4ef0d4d0 KS |
231 | |
232 | #ifdef HAVE_X_WINDOWS | |
233 | Pixmap bitmap; | |
234 | bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
235 | bits, width, height); | |
236 | if (! bitmap) | |
237 | return -1; | |
238 | #endif /* HAVE_X_WINDOWS */ | |
239 | ||
240 | #ifdef HAVE_NTGUI | |
241 | Pixmap bitmap; | |
242 | bitmap = CreateBitmap (width, height, | |
243 | FRAME_X_DISPLAY_INFO (XFRAME (frame))->n_planes, | |
244 | FRAME_X_DISPLAY_INFO (XFRAME (frame))->n_cbits, | |
245 | bits); | |
246 | if (! bitmap) | |
247 | return -1; | |
248 | #endif /* HAVE_NTGUI */ | |
249 | ||
edfda783 | 250 | #ifdef HAVE_NS |
ed3751c8 | 251 | void *bitmap = ns_image_from_XBM (bits, width, height); |
edfda783 AR |
252 | if (!bitmap) |
253 | return -1; | |
254 | #endif | |
255 | ||
4ef0d4d0 | 256 | id = x_allocate_bitmap_record (f); |
4ef0d4d0 | 257 | |
edfda783 AR |
258 | #ifdef HAVE_NS |
259 | dpyinfo->bitmaps[id - 1].img = bitmap; | |
260 | dpyinfo->bitmaps[id - 1].depth = 1; | |
261 | #endif | |
262 | ||
4ef0d4d0 KS |
263 | dpyinfo->bitmaps[id - 1].file = NULL; |
264 | dpyinfo->bitmaps[id - 1].height = height; | |
265 | dpyinfo->bitmaps[id - 1].width = width; | |
266 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
267 | ||
268 | #ifdef HAVE_X_WINDOWS | |
269 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
270 | dpyinfo->bitmaps[id - 1].have_mask = 0; | |
271 | dpyinfo->bitmaps[id - 1].depth = 1; | |
272 | #endif /* HAVE_X_WINDOWS */ | |
273 | ||
274 | #ifdef HAVE_NTGUI | |
275 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
276 | dpyinfo->bitmaps[id - 1].hinst = NULL; | |
277 | dpyinfo->bitmaps[id - 1].depth = 1; | |
278 | #endif /* HAVE_NTGUI */ | |
279 | ||
280 | return id; | |
281 | } | |
282 | ||
283 | /* Create bitmap from file FILE for frame F. */ | |
284 | ||
0766b489 | 285 | ptrdiff_t |
d3da34e0 | 286 | x_create_bitmap_from_file (struct frame *f, Lisp_Object file) |
4ef0d4d0 | 287 | { |
edfda783 AR |
288 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); |
289 | ||
4ef0d4d0 KS |
290 | #ifdef HAVE_NTGUI |
291 | return -1; /* W32_TODO : bitmap support */ | |
292 | #endif /* HAVE_NTGUI */ | |
293 | ||
edfda783 | 294 | #ifdef HAVE_NS |
0766b489 | 295 | ptrdiff_t id; |
ed3751c8 | 296 | void *bitmap = ns_image_from_file (file); |
edfda783 AR |
297 | |
298 | if (!bitmap) | |
299 | return -1; | |
300 | ||
301 | ||
302 | id = x_allocate_bitmap_record (f); | |
303 | dpyinfo->bitmaps[id - 1].img = bitmap; | |
304 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
23f86fce | 305 | dpyinfo->bitmaps[id - 1].file = xmalloc (SBYTES (file) + 1); |
edfda783 | 306 | dpyinfo->bitmaps[id - 1].depth = 1; |
ed3751c8 JB |
307 | dpyinfo->bitmaps[id - 1].height = ns_image_width (bitmap); |
308 | dpyinfo->bitmaps[id - 1].width = ns_image_height (bitmap); | |
6045c4fd | 309 | strcpy (dpyinfo->bitmaps[id - 1].file, SSDATA (file)); |
edfda783 AR |
310 | return id; |
311 | #endif | |
312 | ||
4ef0d4d0 | 313 | #ifdef HAVE_X_WINDOWS |
4ef0d4d0 KS |
314 | unsigned int width, height; |
315 | Pixmap bitmap; | |
0766b489 PE |
316 | int xhot, yhot, result; |
317 | ptrdiff_t id; | |
4ef0d4d0 KS |
318 | Lisp_Object found; |
319 | int fd; | |
320 | char *filename; | |
321 | ||
322 | /* Look for an existing bitmap with the same name. */ | |
323 | for (id = 0; id < dpyinfo->bitmaps_last; ++id) | |
324 | { | |
325 | if (dpyinfo->bitmaps[id].refcount | |
326 | && dpyinfo->bitmaps[id].file | |
51b59d79 | 327 | && !strcmp (dpyinfo->bitmaps[id].file, SSDATA (file))) |
4ef0d4d0 KS |
328 | { |
329 | ++dpyinfo->bitmaps[id].refcount; | |
330 | return id + 1; | |
331 | } | |
332 | } | |
333 | ||
334 | /* Search bitmap-file-path for the file, if appropriate. */ | |
335 | fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil); | |
336 | if (fd < 0) | |
337 | return -1; | |
338 | emacs_close (fd); | |
339 | ||
51b59d79 | 340 | filename = SSDATA (found); |
4ef0d4d0 KS |
341 | |
342 | result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
343 | filename, &width, &height, &bitmap, &xhot, &yhot); | |
344 | if (result != BitmapSuccess) | |
345 | return -1; | |
346 | ||
347 | id = x_allocate_bitmap_record (f); | |
348 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
349 | dpyinfo->bitmaps[id - 1].have_mask = 0; | |
350 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
23f86fce | 351 | dpyinfo->bitmaps[id - 1].file = xmalloc (SBYTES (file) + 1); |
4ef0d4d0 KS |
352 | dpyinfo->bitmaps[id - 1].depth = 1; |
353 | dpyinfo->bitmaps[id - 1].height = height; | |
354 | dpyinfo->bitmaps[id - 1].width = width; | |
42a5b22f | 355 | strcpy (dpyinfo->bitmaps[id - 1].file, SSDATA (file)); |
4ef0d4d0 KS |
356 | |
357 | return id; | |
358 | #endif /* HAVE_X_WINDOWS */ | |
359 | } | |
360 | ||
361 | /* Free bitmap B. */ | |
362 | ||
363 | static void | |
d3da34e0 | 364 | free_bitmap_record (Display_Info *dpyinfo, Bitmap_Record *bm) |
4ef0d4d0 KS |
365 | { |
366 | #ifdef HAVE_X_WINDOWS | |
367 | XFreePixmap (dpyinfo->display, bm->pixmap); | |
368 | if (bm->have_mask) | |
369 | XFreePixmap (dpyinfo->display, bm->mask); | |
370 | #endif /* HAVE_X_WINDOWS */ | |
371 | ||
372 | #ifdef HAVE_NTGUI | |
373 | DeleteObject (bm->pixmap); | |
374 | #endif /* HAVE_NTGUI */ | |
375 | ||
edfda783 | 376 | #ifdef HAVE_NS |
ed3751c8 | 377 | ns_release_object (bm->img); |
edfda783 AR |
378 | #endif |
379 | ||
4ef0d4d0 KS |
380 | if (bm->file) |
381 | { | |
382 | xfree (bm->file); | |
383 | bm->file = NULL; | |
384 | } | |
385 | } | |
386 | ||
387 | /* Remove reference to bitmap with id number ID. */ | |
388 | ||
389 | void | |
0766b489 | 390 | x_destroy_bitmap (FRAME_PTR f, ptrdiff_t id) |
4ef0d4d0 KS |
391 | { |
392 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
393 | ||
394 | if (id > 0) | |
395 | { | |
396 | Bitmap_Record *bm = &dpyinfo->bitmaps[id - 1]; | |
397 | ||
398 | if (--bm->refcount == 0) | |
399 | { | |
4d7e6e51 | 400 | block_input (); |
65342ae3 | 401 | free_bitmap_record (dpyinfo, bm); |
4d7e6e51 | 402 | unblock_input (); |
4ef0d4d0 KS |
403 | } |
404 | } | |
405 | } | |
406 | ||
407 | /* Free all the bitmaps for the display specified by DPYINFO. */ | |
408 | ||
409 | void | |
d3da34e0 | 410 | x_destroy_all_bitmaps (Display_Info *dpyinfo) |
4ef0d4d0 | 411 | { |
0766b489 | 412 | ptrdiff_t i; |
4ef0d4d0 KS |
413 | Bitmap_Record *bm = dpyinfo->bitmaps; |
414 | ||
415 | for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++) | |
416 | if (bm->refcount > 0) | |
65342ae3 | 417 | free_bitmap_record (dpyinfo, bm); |
4ef0d4d0 KS |
418 | |
419 | dpyinfo->bitmaps_last = 0; | |
420 | } | |
421 | ||
578098f3 PE |
422 | static bool x_create_x_image_and_pixmap (struct frame *, int, int, int, |
423 | XImagePtr *, Pixmap *); | |
424 | static void x_destroy_x_image (XImagePtr ximg); | |
4ef0d4d0 | 425 | |
547c9269 YM |
426 | #ifdef HAVE_NTGUI |
427 | static XImagePtr_or_DC image_get_x_image_or_dc (struct frame *, struct image *, | |
428 | bool, HGDIOBJ *); | |
429 | static void image_unget_x_image_or_dc (struct image *, bool, XImagePtr_or_DC, | |
430 | HGDIOBJ); | |
431 | #else | |
432 | static XImagePtr image_get_x_image (struct frame *, struct image *, bool); | |
433 | static void image_unget_x_image (struct image *, bool, XImagePtr); | |
434 | #define image_get_x_image_or_dc(f, img, mask_p, dummy) \ | |
435 | image_get_x_image (f, img, mask_p) | |
436 | #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \ | |
437 | image_unget_x_image (img, mask_p, ximg) | |
438 | #endif | |
439 | ||
4ef0d4d0 KS |
440 | #ifdef HAVE_X_WINDOWS |
441 | ||
547c9269 YM |
442 | static void image_sync_to_pixmaps (struct frame *, struct image *); |
443 | ||
4ef0d4d0 KS |
444 | /* Useful functions defined in the section |
445 | `Image type independent image structures' below. */ | |
446 | ||
f57e2426 J |
447 | static unsigned long four_corners_best (XImagePtr ximg, |
448 | int *corners, | |
449 | unsigned long width, | |
450 | unsigned long height); | |
4ef0d4d0 | 451 | |
4ef0d4d0 KS |
452 | |
453 | /* Create a mask of a bitmap. Note is this not a perfect mask. | |
454 | It's nicer with some borders in this context */ | |
455 | ||
578098f3 | 456 | void |
0766b489 | 457 | x_create_bitmap_mask (struct frame *f, ptrdiff_t id) |
4ef0d4d0 KS |
458 | { |
459 | Pixmap pixmap, mask; | |
460 | XImagePtr ximg, mask_img; | |
461 | unsigned long width, height; | |
578098f3 | 462 | bool result; |
4ef0d4d0 KS |
463 | unsigned long bg; |
464 | unsigned long x, y, xp, xm, yp, ym; | |
465 | GC gc; | |
466 | ||
467 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
468 | ||
469 | if (!(id > 0)) | |
578098f3 | 470 | return; |
4ef0d4d0 KS |
471 | |
472 | pixmap = x_bitmap_pixmap (f, id); | |
473 | width = x_bitmap_width (f, id); | |
474 | height = x_bitmap_height (f, id); | |
475 | ||
4d7e6e51 | 476 | block_input (); |
4ef0d4d0 KS |
477 | ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height, |
478 | ~0, ZPixmap); | |
479 | ||
480 | if (!ximg) | |
481 | { | |
4d7e6e51 | 482 | unblock_input (); |
578098f3 | 483 | return; |
4ef0d4d0 KS |
484 | } |
485 | ||
486 | result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask); | |
487 | ||
4d7e6e51 | 488 | unblock_input (); |
4ef0d4d0 KS |
489 | if (!result) |
490 | { | |
491 | XDestroyImage (ximg); | |
578098f3 | 492 | return; |
4ef0d4d0 KS |
493 | } |
494 | ||
6ac3c7bb | 495 | bg = four_corners_best (ximg, NULL, width, height); |
4ef0d4d0 KS |
496 | |
497 | for (y = 0; y < ximg->height; ++y) | |
498 | { | |
499 | for (x = 0; x < ximg->width; ++x) | |
500 | { | |
501 | xp = x != ximg->width - 1 ? x + 1 : 0; | |
502 | xm = x != 0 ? x - 1 : ximg->width - 1; | |
503 | yp = y != ximg->height - 1 ? y + 1 : 0; | |
504 | ym = y != 0 ? y - 1 : ximg->height - 1; | |
505 | if (XGetPixel (ximg, x, y) == bg | |
506 | && XGetPixel (ximg, x, yp) == bg | |
507 | && XGetPixel (ximg, x, ym) == bg | |
508 | && XGetPixel (ximg, xp, y) == bg | |
509 | && XGetPixel (ximg, xp, yp) == bg | |
510 | && XGetPixel (ximg, xp, ym) == bg | |
511 | && XGetPixel (ximg, xm, y) == bg | |
512 | && XGetPixel (ximg, xm, yp) == bg | |
513 | && XGetPixel (ximg, xm, ym) == bg) | |
514 | XPutPixel (mask_img, x, y, 0); | |
515 | else | |
516 | XPutPixel (mask_img, x, y, 1); | |
517 | } | |
518 | } | |
519 | ||
4d7e6e51 | 520 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
521 | gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL); |
522 | XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0, | |
523 | width, height); | |
524 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
525 | ||
526 | dpyinfo->bitmaps[id - 1].have_mask = 1; | |
527 | dpyinfo->bitmaps[id - 1].mask = mask; | |
528 | ||
529 | XDestroyImage (ximg); | |
530 | x_destroy_x_image (mask_img); | |
4ef0d4d0 KS |
531 | } |
532 | ||
533 | #endif /* HAVE_X_WINDOWS */ | |
534 | ||
535 | ||
536 | /*********************************************************************** | |
537 | Image types | |
538 | ***********************************************************************/ | |
539 | ||
4ef0d4d0 KS |
540 | /* List of supported image types. Use define_image_type to add new |
541 | types. Use lookup_image_type to find a type for a given symbol. */ | |
542 | ||
543 | static struct image_type *image_types; | |
544 | ||
545 | /* The symbol `xbm' which is used as the type symbol for XBM images. */ | |
546 | ||
955cbe7b | 547 | static Lisp_Object Qxbm; |
4ef0d4d0 KS |
548 | |
549 | /* Keywords. */ | |
550 | ||
955cbe7b | 551 | Lisp_Object QCascent, QCmargin, QCrelief; |
955cbe7b PE |
552 | Lisp_Object QCconversion; |
553 | static Lisp_Object QCheuristic_mask; | |
554 | static Lisp_Object QCcolor_symbols; | |
555 | static Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask, QCgeometry; | |
556 | static Lisp_Object QCcrop, QCrotation; | |
4ef0d4d0 KS |
557 | |
558 | /* Other symbols. */ | |
559 | ||
1100a63c | 560 | static Lisp_Object Qcount, Qextension_data, Qdelay; |
955cbe7b | 561 | static Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic; |
4ef0d4d0 | 562 | |
578098f3 | 563 | /* Forward function prototypes. */ |
4ef0d4d0 | 564 | |
641cfd14 | 565 | static struct image_type *lookup_image_type (Lisp_Object); |
f57e2426 J |
566 | static void x_laplace (struct frame *, struct image *); |
567 | static void x_emboss (struct frame *, struct image *); | |
578098f3 | 568 | static void x_build_heuristic_mask (struct frame *, struct image *, |
36a305a7 | 569 | Lisp_Object); |
0fda9b75 DC |
570 | #ifdef WINDOWSNT |
571 | extern Lisp_Object Vlibrary_cache; | |
0855eb52 | 572 | #define CACHE_IMAGE_TYPE(type, status) \ |
0898ca10 | 573 | do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0) |
84d358f0 JB |
574 | #else |
575 | #define CACHE_IMAGE_TYPE(type, status) | |
a9e7a9d5 | 576 | #endif |
0855eb52 JB |
577 | |
578 | #define ADD_IMAGE_TYPE(type) \ | |
579 | do { Vimage_types = Fcons (type, Vimage_types); } while (0) | |
4ef0d4d0 KS |
580 | |
581 | /* Define a new image type from TYPE. This adds a copy of TYPE to | |
0855eb52 | 582 | image_types and caches the loading status of TYPE. */ |
4ef0d4d0 | 583 | |
bb4d86b4 | 584 | static struct image_type * |
d07ff9db | 585 | define_image_type (struct image_type *type) |
4ef0d4d0 | 586 | { |
bb4d86b4 CY |
587 | struct image_type *p = NULL; |
588 | Lisp_Object target_type = *type->type; | |
578098f3 | 589 | bool type_valid = 1; |
0855eb52 | 590 | |
4d7e6e51 | 591 | block_input (); |
bb4d86b4 CY |
592 | |
593 | for (p = image_types; p; p = p->next) | |
594 | if (EQ (*p->type, target_type)) | |
595 | goto done; | |
596 | ||
597 | if (type->init) | |
0855eb52 | 598 | { |
a9e7a9d5 | 599 | #if defined HAVE_NTGUI && defined WINDOWSNT |
bb4d86b4 CY |
600 | /* If we failed to load the library before, don't try again. */ |
601 | Lisp_Object tested = Fassq (target_type, Vlibrary_cache); | |
602 | if (CONSP (tested) && NILP (XCDR (tested))) | |
603 | type_valid = 0; | |
604 | else | |
605 | #endif | |
606 | { | |
d07ff9db | 607 | type_valid = type->init (); |
bb4d86b4 CY |
608 | CACHE_IMAGE_TYPE (target_type, type_valid ? Qt : Qnil); |
609 | } | |
610 | } | |
09c01941 | 611 | |
bb4d86b4 | 612 | if (type_valid) |
0855eb52 JB |
613 | { |
614 | /* Make a copy of TYPE to avoid a bus error in a dumped Emacs. | |
615 | The initialized data segment is read-only. */ | |
09c01941 | 616 | p = xmalloc (sizeof *p); |
ae1d87e2 | 617 | *p = *type; |
0855eb52 JB |
618 | p->next = image_types; |
619 | image_types = p; | |
0855eb52 JB |
620 | } |
621 | ||
bb4d86b4 | 622 | done: |
4d7e6e51 | 623 | unblock_input (); |
bb4d86b4 | 624 | return p; |
4ef0d4d0 KS |
625 | } |
626 | ||
627 | ||
578098f3 | 628 | /* Value is true if OBJECT is a valid Lisp image specification. A |
4ef0d4d0 KS |
629 | valid image specification is a list whose car is the symbol |
630 | `image', and whose rest is a property list. The property list must | |
631 | contain a value for key `:type'. That value must be the name of a | |
632 | supported image type. The rest of the property list depends on the | |
633 | image type. */ | |
634 | ||
578098f3 | 635 | bool |
d3da34e0 | 636 | valid_image_p (Lisp_Object object) |
4ef0d4d0 | 637 | { |
578098f3 | 638 | bool valid_p = 0; |
4ef0d4d0 KS |
639 | |
640 | if (IMAGEP (object)) | |
641 | { | |
642 | Lisp_Object tem; | |
643 | ||
644 | for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem)) | |
645 | if (EQ (XCAR (tem), QCtype)) | |
646 | { | |
647 | tem = XCDR (tem); | |
648 | if (CONSP (tem) && SYMBOLP (XCAR (tem))) | |
649 | { | |
650 | struct image_type *type; | |
651 | type = lookup_image_type (XCAR (tem)); | |
652 | if (type) | |
653 | valid_p = type->valid_p (object); | |
654 | } | |
655 | ||
656 | break; | |
657 | } | |
658 | } | |
659 | ||
660 | return valid_p; | |
661 | } | |
662 | ||
663 | ||
664 | /* Log error message with format string FORMAT and argument ARG. | |
665 | Signaling an error, e.g. when an image cannot be loaded, is not a | |
666 | good idea because this would interrupt redisplay, and the error | |
667 | message display would lead to another redisplay. This function | |
668 | therefore simply displays a message. */ | |
669 | ||
670 | static void | |
675e2c69 | 671 | image_error (const char *format, Lisp_Object arg1, Lisp_Object arg2) |
4ef0d4d0 KS |
672 | { |
673 | add_to_log (format, arg1, arg2); | |
674 | } | |
675 | ||
676 | ||
677 | \f | |
678 | /*********************************************************************** | |
679 | Image specifications | |
680 | ***********************************************************************/ | |
681 | ||
682 | enum image_value_type | |
683 | { | |
684 | IMAGE_DONT_CHECK_VALUE_TYPE, | |
685 | IMAGE_STRING_VALUE, | |
686 | IMAGE_STRING_OR_NIL_VALUE, | |
687 | IMAGE_SYMBOL_VALUE, | |
688 | IMAGE_POSITIVE_INTEGER_VALUE, | |
c4a07a4c | 689 | IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, |
4ef0d4d0 KS |
690 | IMAGE_NON_NEGATIVE_INTEGER_VALUE, |
691 | IMAGE_ASCENT_VALUE, | |
692 | IMAGE_INTEGER_VALUE, | |
693 | IMAGE_FUNCTION_VALUE, | |
694 | IMAGE_NUMBER_VALUE, | |
695 | IMAGE_BOOL_VALUE | |
696 | }; | |
697 | ||
698 | /* Structure used when parsing image specifications. */ | |
699 | ||
700 | struct image_keyword | |
701 | { | |
702 | /* Name of keyword. */ | |
675e2c69 | 703 | const char *name; |
4ef0d4d0 KS |
704 | |
705 | /* The type of value allowed. */ | |
706 | enum image_value_type type; | |
707 | ||
578098f3 PE |
708 | /* True means key must be present. */ |
709 | bool mandatory_p; | |
4ef0d4d0 KS |
710 | |
711 | /* Used to recognize duplicate keywords in a property list. */ | |
712 | int count; | |
713 | ||
714 | /* The value that was found. */ | |
715 | Lisp_Object value; | |
716 | }; | |
717 | ||
718 | ||
4ef0d4d0 KS |
719 | /* Parse image spec SPEC according to KEYWORDS. A valid image spec |
720 | has the format (image KEYWORD VALUE ...). One of the keyword/ | |
721 | value pairs must be `:type TYPE'. KEYWORDS is a vector of | |
722 | image_keywords structures of size NKEYWORDS describing other | |
578098f3 | 723 | allowed keyword/value pairs. Value is true if SPEC is valid. */ |
4ef0d4d0 | 724 | |
578098f3 | 725 | static bool |
d3da34e0 JB |
726 | parse_image_spec (Lisp_Object spec, struct image_keyword *keywords, |
727 | int nkeywords, Lisp_Object type) | |
4ef0d4d0 KS |
728 | { |
729 | int i; | |
730 | Lisp_Object plist; | |
731 | ||
732 | if (!IMAGEP (spec)) | |
733 | return 0; | |
734 | ||
735 | plist = XCDR (spec); | |
736 | while (CONSP (plist)) | |
737 | { | |
738 | Lisp_Object key, value; | |
739 | ||
740 | /* First element of a pair must be a symbol. */ | |
741 | key = XCAR (plist); | |
742 | plist = XCDR (plist); | |
743 | if (!SYMBOLP (key)) | |
744 | return 0; | |
745 | ||
746 | /* There must follow a value. */ | |
747 | if (!CONSP (plist)) | |
748 | return 0; | |
749 | value = XCAR (plist); | |
750 | plist = XCDR (plist); | |
751 | ||
752 | /* Find key in KEYWORDS. Error if not found. */ | |
753 | for (i = 0; i < nkeywords; ++i) | |
42a5b22f | 754 | if (strcmp (keywords[i].name, SSDATA (SYMBOL_NAME (key))) == 0) |
4ef0d4d0 KS |
755 | break; |
756 | ||
757 | if (i == nkeywords) | |
758 | continue; | |
759 | ||
760 | /* Record that we recognized the keyword. If a keywords | |
761 | was found more than once, it's an error. */ | |
762 | keywords[i].value = value; | |
4ef0d4d0 KS |
763 | if (keywords[i].count > 1) |
764 | return 0; | |
d311d28c | 765 | ++keywords[i].count; |
4ef0d4d0 KS |
766 | |
767 | /* Check type of value against allowed type. */ | |
768 | switch (keywords[i].type) | |
769 | { | |
770 | case IMAGE_STRING_VALUE: | |
771 | if (!STRINGP (value)) | |
772 | return 0; | |
773 | break; | |
774 | ||
775 | case IMAGE_STRING_OR_NIL_VALUE: | |
776 | if (!STRINGP (value) && !NILP (value)) | |
777 | return 0; | |
778 | break; | |
779 | ||
780 | case IMAGE_SYMBOL_VALUE: | |
781 | if (!SYMBOLP (value)) | |
782 | return 0; | |
783 | break; | |
784 | ||
785 | case IMAGE_POSITIVE_INTEGER_VALUE: | |
13464394 | 786 | if (! RANGED_INTEGERP (1, value, INT_MAX)) |
4ef0d4d0 KS |
787 | return 0; |
788 | break; | |
789 | ||
c4a07a4c PE |
790 | case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR: |
791 | if (RANGED_INTEGERP (0, value, INT_MAX)) | |
4ef0d4d0 KS |
792 | break; |
793 | if (CONSP (value) | |
c4a07a4c PE |
794 | && RANGED_INTEGERP (0, XCAR (value), INT_MAX) |
795 | && RANGED_INTEGERP (0, XCDR (value), INT_MAX)) | |
4ef0d4d0 KS |
796 | break; |
797 | return 0; | |
798 | ||
799 | case IMAGE_ASCENT_VALUE: | |
800 | if (SYMBOLP (value) && EQ (value, Qcenter)) | |
801 | break; | |
13464394 | 802 | else if (RANGED_INTEGERP (0, value, 100)) |
4ef0d4d0 KS |
803 | break; |
804 | return 0; | |
805 | ||
806 | case IMAGE_NON_NEGATIVE_INTEGER_VALUE: | |
13464394 PE |
807 | /* Unlike the other integer-related cases, this one does not |
808 | verify that VALUE fits in 'int'. This is because callers | |
809 | want EMACS_INT. */ | |
4ef0d4d0 KS |
810 | if (!INTEGERP (value) || XINT (value) < 0) |
811 | return 0; | |
812 | break; | |
813 | ||
814 | case IMAGE_DONT_CHECK_VALUE_TYPE: | |
815 | break; | |
816 | ||
817 | case IMAGE_FUNCTION_VALUE: | |
818 | value = indirect_function (value); | |
ca105506 | 819 | if (!NILP (Ffunctionp (value))) |
4ef0d4d0 KS |
820 | break; |
821 | return 0; | |
822 | ||
823 | case IMAGE_NUMBER_VALUE: | |
824 | if (!INTEGERP (value) && !FLOATP (value)) | |
825 | return 0; | |
826 | break; | |
827 | ||
828 | case IMAGE_INTEGER_VALUE: | |
13464394 | 829 | if (! TYPE_RANGED_INTEGERP (int, value)) |
4ef0d4d0 KS |
830 | return 0; |
831 | break; | |
832 | ||
833 | case IMAGE_BOOL_VALUE: | |
834 | if (!NILP (value) && !EQ (value, Qt)) | |
835 | return 0; | |
836 | break; | |
837 | ||
838 | default: | |
1088b922 | 839 | emacs_abort (); |
4ef0d4d0 KS |
840 | break; |
841 | } | |
842 | ||
843 | if (EQ (key, QCtype) && !EQ (type, value)) | |
844 | return 0; | |
845 | } | |
846 | ||
847 | /* Check that all mandatory fields are present. */ | |
848 | for (i = 0; i < nkeywords; ++i) | |
849 | if (keywords[i].mandatory_p && keywords[i].count == 0) | |
850 | return 0; | |
851 | ||
852 | return NILP (plist); | |
853 | } | |
854 | ||
855 | ||
856 | /* Return the value of KEY in image specification SPEC. Value is nil | |
578098f3 PE |
857 | if KEY is not present in SPEC. Set *FOUND depending on whether KEY |
858 | was found in SPEC. */ | |
4ef0d4d0 KS |
859 | |
860 | static Lisp_Object | |
578098f3 | 861 | image_spec_value (Lisp_Object spec, Lisp_Object key, bool *found) |
4ef0d4d0 KS |
862 | { |
863 | Lisp_Object tail; | |
864 | ||
a54e2c05 | 865 | eassert (valid_image_p (spec)); |
4ef0d4d0 KS |
866 | |
867 | for (tail = XCDR (spec); | |
868 | CONSP (tail) && CONSP (XCDR (tail)); | |
869 | tail = XCDR (XCDR (tail))) | |
870 | { | |
871 | if (EQ (XCAR (tail), key)) | |
872 | { | |
873 | if (found) | |
874 | *found = 1; | |
875 | return XCAR (XCDR (tail)); | |
876 | } | |
877 | } | |
878 | ||
879 | if (found) | |
880 | *found = 0; | |
881 | return Qnil; | |
882 | } | |
883 | ||
884 | ||
885 | DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0, | |
886 | doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT). | |
887 | PIXELS non-nil means return the size in pixels, otherwise return the | |
888 | size in canonical character units. | |
889 | FRAME is the frame on which the image will be displayed. FRAME nil | |
890 | or omitted means use the selected frame. */) | |
5842a27b | 891 | (Lisp_Object spec, Lisp_Object pixels, Lisp_Object frame) |
4ef0d4d0 KS |
892 | { |
893 | Lisp_Object size; | |
894 | ||
895 | size = Qnil; | |
896 | if (valid_image_p (spec)) | |
897 | { | |
7452b7bd | 898 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 899 | ptrdiff_t id = lookup_image (f, spec); |
4ef0d4d0 KS |
900 | struct image *img = IMAGE_FROM_ID (f, id); |
901 | int width = img->width + 2 * img->hmargin; | |
902 | int height = img->height + 2 * img->vmargin; | |
903 | ||
904 | if (NILP (pixels)) | |
905 | size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)), | |
906 | make_float ((double) height / FRAME_LINE_HEIGHT (f))); | |
907 | else | |
908 | size = Fcons (make_number (width), make_number (height)); | |
909 | } | |
910 | else | |
911 | error ("Invalid image specification"); | |
912 | ||
913 | return size; | |
914 | } | |
915 | ||
916 | ||
917 | DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0, | |
918 | doc: /* Return t if image SPEC has a mask bitmap. | |
919 | FRAME is the frame on which the image will be displayed. FRAME nil | |
920 | or omitted means use the selected frame. */) | |
5842a27b | 921 | (Lisp_Object spec, Lisp_Object frame) |
4ef0d4d0 KS |
922 | { |
923 | Lisp_Object mask; | |
924 | ||
925 | mask = Qnil; | |
926 | if (valid_image_p (spec)) | |
927 | { | |
7452b7bd | 928 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 929 | ptrdiff_t id = lookup_image (f, spec); |
4ef0d4d0 KS |
930 | struct image *img = IMAGE_FROM_ID (f, id); |
931 | if (img->mask) | |
932 | mask = Qt; | |
933 | } | |
934 | else | |
935 | error ("Invalid image specification"); | |
936 | ||
937 | return mask; | |
938 | } | |
939 | ||
1546c559 JL |
940 | DEFUN ("image-metadata", Fimage_metadata, Simage_metadata, 1, 2, 0, |
941 | doc: /* Return metadata for image SPEC. | |
6ac3c7bb KS |
942 | FRAME is the frame on which the image will be displayed. FRAME nil |
943 | or omitted means use the selected frame. */) | |
5842a27b | 944 | (Lisp_Object spec, Lisp_Object frame) |
6ac3c7bb KS |
945 | { |
946 | Lisp_Object ext; | |
947 | ||
948 | ext = Qnil; | |
949 | if (valid_image_p (spec)) | |
950 | { | |
7452b7bd | 951 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 952 | ptrdiff_t id = lookup_image (f, spec); |
6ac3c7bb | 953 | struct image *img = IMAGE_FROM_ID (f, id); |
b50691aa | 954 | ext = img->lisp_data; |
6ac3c7bb KS |
955 | } |
956 | ||
957 | return ext; | |
958 | } | |
959 | ||
4ef0d4d0 KS |
960 | \f |
961 | /*********************************************************************** | |
962 | Image type independent image structures | |
963 | ***********************************************************************/ | |
964 | ||
11273115 | 965 | #define MAX_IMAGE_SIZE 10.0 |
4ef0d4d0 KS |
966 | /* Allocate and return a new image structure for image specification |
967 | SPEC. SPEC has a hash value of HASH. */ | |
968 | ||
969 | static struct image * | |
0de4bb68 | 970 | make_image (Lisp_Object spec, EMACS_UINT hash) |
4ef0d4d0 | 971 | { |
23f86fce | 972 | struct image *img = xzalloc (sizeof *img); |
a2bc5bdd | 973 | Lisp_Object file = image_spec_value (spec, QCfile, NULL); |
4ef0d4d0 | 974 | |
a54e2c05 | 975 | eassert (valid_image_p (spec)); |
a2bc5bdd | 976 | img->dependencies = NILP (file) ? Qnil : list1 (file); |
4ef0d4d0 | 977 | img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL)); |
a54e2c05 | 978 | eassert (img->type != NULL); |
4ef0d4d0 | 979 | img->spec = spec; |
b50691aa | 980 | img->lisp_data = Qnil; |
4ef0d4d0 KS |
981 | img->ascent = DEFAULT_IMAGE_ASCENT; |
982 | img->hash = hash; | |
6ac3c7bb | 983 | img->corners[BOT_CORNER] = -1; /* Full image */ |
4ef0d4d0 KS |
984 | return img; |
985 | } | |
986 | ||
987 | ||
988 | /* Free image IMG which was used on frame F, including its resources. */ | |
989 | ||
990 | static void | |
d3da34e0 | 991 | free_image (struct frame *f, struct image *img) |
4ef0d4d0 KS |
992 | { |
993 | if (img) | |
994 | { | |
354884c4 | 995 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
996 | |
997 | /* Remove IMG from the hash table of its cache. */ | |
998 | if (img->prev) | |
999 | img->prev->next = img->next; | |
1000 | else | |
1001 | c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next; | |
1002 | ||
1003 | if (img->next) | |
1004 | img->next->prev = img->prev; | |
1005 | ||
1006 | c->images[img->id] = NULL; | |
1007 | ||
1008 | /* Free resources, then free IMG. */ | |
1009 | img->type->free (f, img); | |
1010 | xfree (img); | |
1011 | } | |
1012 | } | |
1013 | ||
578098f3 | 1014 | /* Return true if the given widths and heights are valid for display. */ |
f1f25b99 | 1015 | |
578098f3 | 1016 | static bool |
d3da34e0 | 1017 | check_image_size (struct frame *f, int width, int height) |
f1f25b99 | 1018 | { |
7df4765a | 1019 | int w, h; |
f1f25b99 | 1020 | |
7df4765a | 1021 | if (width <= 0 || height <= 0) |
f1f25b99 CY |
1022 | return 0; |
1023 | ||
7df4765a | 1024 | if (INTEGERP (Vmax_image_size)) |
3f791afe PE |
1025 | return (width <= XINT (Vmax_image_size) |
1026 | && height <= XINT (Vmax_image_size)); | |
7df4765a KS |
1027 | else if (FLOATP (Vmax_image_size)) |
1028 | { | |
e5d76069 PE |
1029 | if (f != NULL) |
1030 | { | |
1031 | w = FRAME_PIXEL_WIDTH (f); | |
1032 | h = FRAME_PIXEL_HEIGHT (f); | |
1033 | } | |
1034 | else | |
1035 | w = h = 1024; /* Arbitrary size for unknown frame. */ | |
3f791afe PE |
1036 | return (width <= XFLOAT_DATA (Vmax_image_size) * w |
1037 | && height <= XFLOAT_DATA (Vmax_image_size) * h); | |
7df4765a KS |
1038 | } |
1039 | else | |
1040 | return 1; | |
f1f25b99 | 1041 | } |
4ef0d4d0 KS |
1042 | |
1043 | /* Prepare image IMG for display on frame F. Must be called before | |
1044 | drawing an image. */ | |
1045 | ||
1046 | void | |
d3da34e0 | 1047 | prepare_image_for_display (struct frame *f, struct image *img) |
4ef0d4d0 | 1048 | { |
4ef0d4d0 | 1049 | /* We're about to display IMG, so set its timestamp to `now'. */ |
e9a9ae03 | 1050 | img->timestamp = current_emacs_time (); |
4ef0d4d0 KS |
1051 | |
1052 | /* If IMG doesn't have a pixmap yet, load it now, using the image | |
1053 | type dependent loader function. */ | |
1054 | if (img->pixmap == NO_PIXMAP && !img->load_failed_p) | |
578098f3 | 1055 | img->load_failed_p = ! img->type->load (f, img); |
c7fea325 | 1056 | |
547c9269 YM |
1057 | #ifdef HAVE_X_WINDOWS |
1058 | if (!img->load_failed_p) | |
1059 | { | |
1060 | block_input (); | |
1061 | image_sync_to_pixmaps (f, img); | |
1062 | unblock_input (); | |
1063 | } | |
1064 | #endif | |
4ef0d4d0 KS |
1065 | } |
1066 | ||
1067 | ||
1068 | /* Value is the number of pixels for the ascent of image IMG when | |
1069 | drawn in face FACE. */ | |
1070 | ||
1071 | int | |
d3da34e0 | 1072 | image_ascent (struct image *img, struct face *face, struct glyph_slice *slice) |
4ef0d4d0 | 1073 | { |
1938bc36 | 1074 | int height; |
4ef0d4d0 KS |
1075 | int ascent; |
1076 | ||
1938bc36 KS |
1077 | if (slice->height == img->height) |
1078 | height = img->height + img->vmargin; | |
1079 | else if (slice->y == 0) | |
1080 | height = slice->height + img->vmargin; | |
1081 | else | |
1082 | height = slice->height; | |
1083 | ||
4ef0d4d0 KS |
1084 | if (img->ascent == CENTERED_IMAGE_ASCENT) |
1085 | { | |
1086 | if (face->font) | |
1087 | { | |
1088 | #ifdef HAVE_NTGUI | |
1089 | /* W32 specific version. Why?. ++kfs */ | |
5fc9fdeb JR |
1090 | ascent = height / 2 - (FONT_DESCENT (face->font) |
1091 | - FONT_BASE (face->font)) / 2; | |
4ef0d4d0 KS |
1092 | #else |
1093 | /* This expression is arranged so that if the image can't be | |
1094 | exactly centered, it will be moved slightly up. This is | |
1095 | because a typical font is `top-heavy' (due to the presence | |
1096 | uppercase letters), so the image placement should err towards | |
1097 | being top-heavy too. It also just generally looks better. */ | |
5e617bc2 JB |
1098 | ascent = (height + FONT_BASE (face->font) |
1099 | - FONT_DESCENT (face->font) + 1) / 2; | |
4ef0d4d0 KS |
1100 | #endif /* HAVE_NTGUI */ |
1101 | } | |
1102 | else | |
1103 | ascent = height / 2; | |
1104 | } | |
1105 | else | |
13464394 | 1106 | ascent = height * (img->ascent / 100.0); |
4ef0d4d0 KS |
1107 | |
1108 | return ascent; | |
1109 | } | |
1110 | ||
1111 | \f | |
1112 | /* Image background colors. */ | |
1113 | ||
1114 | /* Find the "best" corner color of a bitmap. | |
1115 | On W32, XIMG is assumed to a device context with the bitmap selected. */ | |
1116 | ||
1117 | static RGB_PIXEL_COLOR | |
d3da34e0 JB |
1118 | four_corners_best (XImagePtr_or_DC ximg, int *corners, |
1119 | unsigned long width, unsigned long height) | |
4ef0d4d0 | 1120 | { |
f0c77cd1 | 1121 | RGB_PIXEL_COLOR corner_pixels[4], best IF_LINT (= 0); |
4ef0d4d0 KS |
1122 | int i, best_count; |
1123 | ||
6ac3c7bb KS |
1124 | if (corners && corners[BOT_CORNER] >= 0) |
1125 | { | |
1126 | /* Get the colors at the corner_pixels of ximg. */ | |
1127 | corner_pixels[0] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[TOP_CORNER]); | |
1128 | corner_pixels[1] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[TOP_CORNER]); | |
1129 | corner_pixels[2] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[BOT_CORNER] - 1); | |
1130 | corner_pixels[3] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[BOT_CORNER] - 1); | |
1131 | } | |
1132 | else | |
1133 | { | |
1134 | /* Get the colors at the corner_pixels of ximg. */ | |
1135 | corner_pixels[0] = GET_PIXEL (ximg, 0, 0); | |
1136 | corner_pixels[1] = GET_PIXEL (ximg, width - 1, 0); | |
1137 | corner_pixels[2] = GET_PIXEL (ximg, width - 1, height - 1); | |
1138 | corner_pixels[3] = GET_PIXEL (ximg, 0, height - 1); | |
1139 | } | |
4ef0d4d0 KS |
1140 | /* Choose the most frequently found color as background. */ |
1141 | for (i = best_count = 0; i < 4; ++i) | |
1142 | { | |
1143 | int j, n; | |
1144 | ||
1145 | for (j = n = 0; j < 4; ++j) | |
6ac3c7bb | 1146 | if (corner_pixels[i] == corner_pixels[j]) |
4ef0d4d0 KS |
1147 | ++n; |
1148 | ||
1149 | if (n > best_count) | |
6ac3c7bb | 1150 | best = corner_pixels[i], best_count = n; |
4ef0d4d0 KS |
1151 | } |
1152 | ||
1153 | return best; | |
1154 | } | |
1155 | ||
1156 | /* Portability macros */ | |
1157 | ||
1158 | #ifdef HAVE_NTGUI | |
1159 | ||
4ef0d4d0 KS |
1160 | #define Free_Pixmap(display, pixmap) \ |
1161 | DeleteObject (pixmap) | |
1162 | ||
edfda783 AR |
1163 | #elif defined (HAVE_NS) |
1164 | ||
edfda783 | 1165 | #define Free_Pixmap(display, pixmap) \ |
ed3751c8 | 1166 | ns_release_object (pixmap) |
edfda783 | 1167 | |
4ef0d4d0 KS |
1168 | #else |
1169 | ||
4ef0d4d0 KS |
1170 | #define Free_Pixmap(display, pixmap) \ |
1171 | XFreePixmap (display, pixmap) | |
1172 | ||
edfda783 | 1173 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
1174 | |
1175 | ||
1176 | /* Return the `background' field of IMG. If IMG doesn't have one yet, | |
1177 | it is guessed heuristically. If non-zero, XIMG is an existing | |
1178 | XImage object (or device context with the image selected on W32) to | |
1f026899 | 1179 | use for the heuristic. */ |
4ef0d4d0 KS |
1180 | |
1181 | RGB_PIXEL_COLOR | |
d3da34e0 | 1182 | image_background (struct image *img, struct frame *f, XImagePtr_or_DC ximg) |
4ef0d4d0 KS |
1183 | { |
1184 | if (! img->background_valid) | |
1185 | /* IMG doesn't have a background yet, try to guess a reasonable value. */ | |
1186 | { | |
578098f3 | 1187 | bool free_ximg = !ximg; |
4ef0d4d0 KS |
1188 | #ifdef HAVE_NTGUI |
1189 | HGDIOBJ prev; | |
1190 | #endif /* HAVE_NTGUI */ | |
1191 | ||
1192 | if (free_ximg) | |
547c9269 | 1193 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); |
4ef0d4d0 | 1194 | |
6ac3c7bb | 1195 | img->background = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
1196 | |
1197 | if (free_ximg) | |
547c9269 | 1198 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
1f026899 | 1199 | |
4ef0d4d0 KS |
1200 | img->background_valid = 1; |
1201 | } | |
1202 | ||
1203 | return img->background; | |
1204 | } | |
1205 | ||
1206 | /* Return the `background_transparent' field of IMG. If IMG doesn't | |
1207 | have one yet, it is guessed heuristically. If non-zero, MASK is an | |
1208 | existing XImage object to use for the heuristic. */ | |
1209 | ||
1210 | int | |
d3da34e0 | 1211 | image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_DC mask) |
4ef0d4d0 KS |
1212 | { |
1213 | if (! img->background_transparent_valid) | |
1214 | /* IMG doesn't have a background yet, try to guess a reasonable value. */ | |
1215 | { | |
1216 | if (img->mask) | |
1217 | { | |
578098f3 | 1218 | bool free_mask = !mask; |
4ef0d4d0 KS |
1219 | #ifdef HAVE_NTGUI |
1220 | HGDIOBJ prev; | |
1221 | #endif /* HAVE_NTGUI */ | |
1222 | ||
1223 | if (free_mask) | |
547c9269 | 1224 | mask = image_get_x_image_or_dc (f, img, 1, &prev); |
4ef0d4d0 KS |
1225 | |
1226 | img->background_transparent | |
6ac3c7bb | 1227 | = (four_corners_best (mask, img->corners, img->width, img->height) == PIX_MASK_RETAIN); |
4ef0d4d0 KS |
1228 | |
1229 | if (free_mask) | |
547c9269 | 1230 | image_unget_x_image_or_dc (img, 1, mask, prev); |
4ef0d4d0 KS |
1231 | } |
1232 | else | |
1233 | img->background_transparent = 0; | |
1234 | ||
1235 | img->background_transparent_valid = 1; | |
1236 | } | |
1237 | ||
1238 | return img->background_transparent; | |
1239 | } | |
1240 | ||
1241 | \f | |
1242 | /*********************************************************************** | |
1243 | Helper functions for X image types | |
1244 | ***********************************************************************/ | |
1245 | ||
547c9269 YM |
1246 | /* Clear X resources of image IMG on frame F according to FLAGS. |
1247 | FLAGS is bitwise-or of the following masks: | |
1248 | CLEAR_IMAGE_PIXMAP free the pixmap if any. | |
1249 | CLEAR_IMAGE_MASK means clear the mask pixmap if any. | |
1250 | CLEAR_IMAGE_COLORS means free colors allocated for the image, if | |
1251 | any. */ | |
1252 | ||
1253 | #define CLEAR_IMAGE_PIXMAP (1 << 0) | |
1254 | #define CLEAR_IMAGE_MASK (1 << 1) | |
1255 | #define CLEAR_IMAGE_COLORS (1 << 2) | |
4ef0d4d0 KS |
1256 | |
1257 | static void | |
547c9269 | 1258 | x_clear_image_1 (struct frame *f, struct image *img, int flags) |
4ef0d4d0 | 1259 | { |
547c9269 | 1260 | if (flags & CLEAR_IMAGE_PIXMAP) |
4ef0d4d0 | 1261 | { |
547c9269 YM |
1262 | if (img->pixmap) |
1263 | { | |
1264 | Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap); | |
1265 | img->pixmap = NO_PIXMAP; | |
1266 | /* NOTE (HAVE_NS): background color is NOT an indexed color! */ | |
1267 | img->background_valid = 0; | |
1268 | } | |
1269 | #ifdef HAVE_X_WINDOWS | |
1270 | if (img->ximg) | |
1271 | { | |
1272 | x_destroy_x_image (img->ximg); | |
1273 | img->ximg = NULL; | |
1274 | img->background_valid = 0; | |
1275 | } | |
1276 | #endif | |
4ef0d4d0 KS |
1277 | } |
1278 | ||
547c9269 | 1279 | if (flags & CLEAR_IMAGE_MASK) |
4ef0d4d0 | 1280 | { |
547c9269 YM |
1281 | if (img->mask) |
1282 | { | |
1283 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
1284 | img->mask = NO_PIXMAP; | |
1285 | img->background_transparent_valid = 0; | |
1286 | } | |
1287 | #ifdef HAVE_X_WINDOWS | |
1288 | if (img->mask_img) | |
1289 | { | |
1290 | x_destroy_x_image (img->mask_img); | |
1291 | img->mask_img = NULL; | |
1292 | img->background_transparent_valid = 0; | |
1293 | } | |
1294 | #endif | |
4ef0d4d0 KS |
1295 | } |
1296 | ||
547c9269 | 1297 | if ((flags & CLEAR_IMAGE_COLORS) && img->ncolors) |
4ef0d4d0 | 1298 | { |
4ef0d4d0 KS |
1299 | /* W32_TODO: color table support. */ |
1300 | #ifdef HAVE_X_WINDOWS | |
1301 | x_free_colors (f, img->colors, img->ncolors); | |
1302 | #endif /* HAVE_X_WINDOWS */ | |
1303 | xfree (img->colors); | |
1304 | img->colors = NULL; | |
1305 | img->ncolors = 0; | |
1306 | } | |
c7fea325 | 1307 | |
4ef0d4d0 KS |
1308 | } |
1309 | ||
1310 | /* Free X resources of image IMG which is used on frame F. */ | |
1311 | ||
1312 | static void | |
d3da34e0 | 1313 | x_clear_image (struct frame *f, struct image *img) |
4ef0d4d0 | 1314 | { |
4d7e6e51 | 1315 | block_input (); |
547c9269 YM |
1316 | x_clear_image_1 (f, img, |
1317 | CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_MASK | CLEAR_IMAGE_COLORS); | |
4d7e6e51 | 1318 | unblock_input (); |
4ef0d4d0 KS |
1319 | } |
1320 | ||
1321 | ||
1322 | /* Allocate color COLOR_NAME for image IMG on frame F. If color | |
1323 | cannot be allocated, use DFLT. Add a newly allocated color to | |
1324 | IMG->colors, so that it can be freed again. Value is the pixel | |
1325 | color. */ | |
1326 | ||
1327 | static unsigned long | |
d3da34e0 JB |
1328 | x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name, |
1329 | unsigned long dflt) | |
4ef0d4d0 KS |
1330 | { |
1331 | XColor color; | |
1332 | unsigned long result; | |
1333 | ||
a54e2c05 | 1334 | eassert (STRINGP (color_name)); |
4ef0d4d0 | 1335 | |
3f791afe PE |
1336 | if (x_defined_color (f, SSDATA (color_name), &color, 1) |
1337 | && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors, | |
1338 | INT_MAX)) | |
4ef0d4d0 KS |
1339 | { |
1340 | /* This isn't called frequently so we get away with simply | |
1341 | reallocating the color vector to the needed size, here. */ | |
ddff3151 | 1342 | ptrdiff_t ncolors = img->ncolors + 1; |
38182d90 | 1343 | img->colors = xrealloc (img->colors, ncolors * sizeof *img->colors); |
ddff3151 PE |
1344 | img->colors[ncolors - 1] = color.pixel; |
1345 | img->ncolors = ncolors; | |
4ef0d4d0 KS |
1346 | result = color.pixel; |
1347 | } | |
1348 | else | |
1349 | result = dflt; | |
1350 | ||
1351 | return result; | |
1352 | } | |
1353 | ||
1354 | ||
1355 | \f | |
1356 | /*********************************************************************** | |
1357 | Image Cache | |
1358 | ***********************************************************************/ | |
1359 | ||
f57e2426 | 1360 | static void cache_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
1361 | |
1362 | /* Return a new, initialized image cache that is allocated from the | |
1363 | heap. Call free_image_cache to free an image cache. */ | |
1364 | ||
1365 | struct image_cache * | |
d3da34e0 | 1366 | make_image_cache (void) |
4ef0d4d0 | 1367 | { |
23f86fce | 1368 | struct image_cache *c = xzalloc (sizeof *c); |
4ef0d4d0 KS |
1369 | int size; |
1370 | ||
ddff3151 | 1371 | size = 50; |
23f86fce | 1372 | c->images = xmalloc (size * sizeof *c->images); |
ddff3151 | 1373 | c->size = size; |
4ef0d4d0 | 1374 | size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets; |
23f86fce | 1375 | c->buckets = xzalloc (size); |
4ef0d4d0 KS |
1376 | return c; |
1377 | } | |
1378 | ||
1379 | ||
d3c35d17 CY |
1380 | /* Find an image matching SPEC in the cache, and return it. If no |
1381 | image is found, return NULL. */ | |
1382 | static struct image * | |
0de4bb68 | 1383 | search_image_cache (struct frame *f, Lisp_Object spec, EMACS_UINT hash) |
483e7ae0 | 1384 | { |
91be6b66 | 1385 | struct image *img; |
354884c4 | 1386 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
483e7ae0 CY |
1387 | int i = hash % IMAGE_CACHE_BUCKETS_SIZE; |
1388 | ||
8198352b CY |
1389 | if (!c) return NULL; |
1390 | ||
d3c35d17 CY |
1391 | /* If the image spec does not specify a background color, the cached |
1392 | image must have the same background color as the current frame. | |
cf26ebe8 CY |
1393 | The foreground color must also match, for the sake of monochrome |
1394 | images. | |
1395 | ||
1396 | In fact, we could ignore the foreground color matching condition | |
1397 | for color images, or if the image spec specifies :foreground; | |
1398 | similarly we could ignore the background color matching condition | |
1399 | for formats that don't use transparency (such as jpeg), or if the | |
1400 | image spec specifies :background. However, the extra memory | |
1401 | usage is probably negligible in practice, so we don't bother. */ | |
d3c35d17 | 1402 | |
483e7ae0 | 1403 | for (img = c->buckets[i]; img; img = img->next) |
d3c35d17 CY |
1404 | if (img->hash == hash |
1405 | && !NILP (Fequal (img->spec, spec)) | |
c5b8e0ea YM |
1406 | && img->frame_foreground == FRAME_FOREGROUND_PIXEL (f) |
1407 | && img->frame_background == FRAME_BACKGROUND_PIXEL (f)) | |
d3c35d17 CY |
1408 | break; |
1409 | return img; | |
1410 | } | |
1411 | ||
1412 | ||
1413 | /* Search frame F for an image with spec SPEC, and free it. */ | |
1414 | ||
1415 | static void | |
d3da34e0 | 1416 | uncache_image (struct frame *f, Lisp_Object spec) |
d3c35d17 CY |
1417 | { |
1418 | struct image *img = search_image_cache (f, spec, sxhash (spec, 0)); | |
1419 | if (img) | |
be3faa80 CY |
1420 | { |
1421 | free_image (f, img); | |
1422 | /* As display glyphs may still be referring to the image ID, we | |
1423 | must garbage the frame (Bug#6426). */ | |
1424 | SET_FRAME_GARBAGED (f); | |
1425 | } | |
483e7ae0 CY |
1426 | } |
1427 | ||
1428 | ||
4ef0d4d0 KS |
1429 | /* Free image cache of frame F. Be aware that X frames share images |
1430 | caches. */ | |
1431 | ||
1432 | void | |
d3da34e0 | 1433 | free_image_cache (struct frame *f) |
4ef0d4d0 | 1434 | { |
354884c4 | 1435 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
1436 | if (c) |
1437 | { | |
13464394 | 1438 | ptrdiff_t i; |
4ef0d4d0 KS |
1439 | |
1440 | /* Cache should not be referenced by any frame when freed. */ | |
a54e2c05 | 1441 | eassert (c->refcount == 0); |
4ef0d4d0 KS |
1442 | |
1443 | for (i = 0; i < c->used; ++i) | |
1444 | free_image (f, c->images[i]); | |
1445 | xfree (c->images); | |
1446 | xfree (c->buckets); | |
1447 | xfree (c); | |
354884c4 | 1448 | FRAME_IMAGE_CACHE (f) = NULL; |
4ef0d4d0 KS |
1449 | } |
1450 | } | |
1451 | ||
1452 | ||
a2bc5bdd SM |
1453 | /* Clear image cache of frame F. FILTER=t means free all images. |
1454 | FILTER=nil means clear only images that haven't been | |
1455 | displayed for some time. | |
1456 | Else, only free the images which have FILTER in their `dependencies'. | |
1457 | Should be called from time to time to reduce the number of loaded images. | |
1458 | If image-cache-eviction-delay is non-nil, this frees images in the cache | |
1459 | which weren't displayed for at least that many seconds. */ | |
4ef0d4d0 | 1460 | |
ce0ad53d | 1461 | static void |
a2bc5bdd | 1462 | clear_image_cache (struct frame *f, Lisp_Object filter) |
4ef0d4d0 | 1463 | { |
354884c4 | 1464 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 | 1465 | |
98fe5161 | 1466 | if (c) |
4ef0d4d0 | 1467 | { |
13464394 | 1468 | ptrdiff_t i, nfreed = 0; |
4ef0d4d0 KS |
1469 | |
1470 | /* Block input so that we won't be interrupted by a SIGIO | |
1471 | while being in an inconsistent state. */ | |
4d7e6e51 | 1472 | block_input (); |
4ef0d4d0 | 1473 | |
98fe5161 CY |
1474 | if (!NILP (filter)) |
1475 | { | |
1476 | /* Filter image cache. */ | |
1477 | for (i = 0; i < c->used; ++i) | |
1478 | { | |
1479 | struct image *img = c->images[i]; | |
1480 | if (img && (EQ (Qt, filter) | |
1481 | || !NILP (Fmember (filter, img->dependencies)))) | |
1482 | { | |
1483 | free_image (f, img); | |
1484 | ++nfreed; | |
1485 | } | |
1486 | } | |
1487 | } | |
1488 | else if (INTEGERP (Vimage_cache_eviction_delay)) | |
4ef0d4d0 | 1489 | { |
98fe5161 | 1490 | /* Free cache based on timestamp. */ |
d35af63c PE |
1491 | EMACS_TIME old, t; |
1492 | double delay; | |
13464394 | 1493 | ptrdiff_t nimages = 0; |
98fe5161 CY |
1494 | |
1495 | for (i = 0; i < c->used; ++i) | |
1496 | if (c->images[i]) | |
1497 | nimages++; | |
1498 | ||
1499 | /* If the number of cached images has grown unusually large, | |
1500 | decrease the cache eviction delay (Bug#6230). */ | |
13464394 | 1501 | delay = XINT (Vimage_cache_eviction_delay); |
98fe5161 | 1502 | if (nimages > 40) |
13464394 PE |
1503 | delay = 1600 * delay / nimages / nimages; |
1504 | delay = max (delay, 1); | |
98fe5161 | 1505 | |
e9a9ae03 PE |
1506 | t = current_emacs_time (); |
1507 | old = sub_emacs_time (t, EMACS_TIME_FROM_DOUBLE (delay)); | |
98fe5161 CY |
1508 | |
1509 | for (i = 0; i < c->used; ++i) | |
4ef0d4d0 | 1510 | { |
98fe5161 | 1511 | struct image *img = c->images[i]; |
d35af63c | 1512 | if (img && EMACS_TIME_LT (img->timestamp, old)) |
98fe5161 CY |
1513 | { |
1514 | free_image (f, img); | |
1515 | ++nfreed; | |
1516 | } | |
4ef0d4d0 KS |
1517 | } |
1518 | } | |
1519 | ||
1520 | /* We may be clearing the image cache because, for example, | |
1521 | Emacs was iconified for a longer period of time. In that | |
1522 | case, current matrices may still contain references to | |
1523 | images freed above. So, clear these matrices. */ | |
1524 | if (nfreed) | |
1525 | { | |
1526 | Lisp_Object tail, frame; | |
1527 | ||
1528 | FOR_EACH_FRAME (tail, frame) | |
1529 | { | |
6ae141d6 PE |
1530 | struct frame *fr = XFRAME (frame); |
1531 | if (FRAME_IMAGE_CACHE (fr) == c) | |
1532 | clear_current_matrices (fr); | |
4ef0d4d0 KS |
1533 | } |
1534 | ||
1535 | ++windows_or_buffers_changed; | |
1536 | } | |
1537 | ||
4d7e6e51 | 1538 | unblock_input (); |
4ef0d4d0 KS |
1539 | } |
1540 | } | |
1541 | ||
354884c4 | 1542 | void |
a2bc5bdd | 1543 | clear_image_caches (Lisp_Object filter) |
354884c4 SM |
1544 | { |
1545 | /* FIXME: We want to do | |
1546 | * struct terminal *t; | |
1547 | * for (t = terminal_list; t; t = t->next_terminal) | |
1548 | * clear_image_cache (t, filter); */ | |
1549 | Lisp_Object tail, frame; | |
1550 | FOR_EACH_FRAME (tail, frame) | |
1551 | if (FRAME_WINDOW_P (XFRAME (frame))) | |
a2bc5bdd | 1552 | clear_image_cache (XFRAME (frame), filter); |
354884c4 | 1553 | } |
4ef0d4d0 KS |
1554 | |
1555 | DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache, | |
1556 | 0, 1, 0, | |
a2bc5bdd SM |
1557 | doc: /* Clear the image cache. |
1558 | FILTER nil or a frame means clear all images in the selected frame. | |
1559 | FILTER t means clear the image caches of all frames. | |
1560 | Anything else, means only clear those images which refer to FILTER, | |
1561 | which is then usually a filename. */) | |
5842a27b | 1562 | (Lisp_Object filter) |
4ef0d4d0 | 1563 | { |
a2bc5bdd SM |
1564 | if (!(EQ (filter, Qnil) || FRAMEP (filter))) |
1565 | clear_image_caches (filter); | |
4ef0d4d0 | 1566 | else |
7452b7bd | 1567 | clear_image_cache (decode_window_system_frame (filter), Qt); |
4ef0d4d0 KS |
1568 | |
1569 | return Qnil; | |
1570 | } | |
1571 | ||
1572 | ||
110683ad | 1573 | DEFUN ("image-flush", Fimage_flush, Simage_flush, |
483e7ae0 | 1574 | 1, 2, 0, |
110683ad CY |
1575 | doc: /* Fush the image with specification SPEC on frame FRAME. |
1576 | This removes the image from the Emacs image cache. If SPEC specifies | |
1577 | an image file, the next redisplay of this image will read from the | |
1578 | current contents of that file. | |
1579 | ||
483e7ae0 CY |
1580 | FRAME nil or omitted means use the selected frame. |
1581 | FRAME t means refresh the image on all frames. */) | |
5842a27b | 1582 | (Lisp_Object spec, Lisp_Object frame) |
483e7ae0 CY |
1583 | { |
1584 | if (!valid_image_p (spec)) | |
1585 | error ("Invalid image specification"); | |
1586 | ||
1587 | if (EQ (frame, Qt)) | |
1588 | { | |
1589 | Lisp_Object tail; | |
1590 | FOR_EACH_FRAME (tail, frame) | |
1591 | { | |
1592 | struct frame *f = XFRAME (frame); | |
1593 | if (FRAME_WINDOW_P (f)) | |
1594 | uncache_image (f, spec); | |
1595 | } | |
1596 | } | |
1597 | else | |
7452b7bd | 1598 | uncache_image (decode_window_system_frame (frame), spec); |
483e7ae0 CY |
1599 | |
1600 | return Qnil; | |
1601 | } | |
1602 | ||
1603 | ||
4ef0d4d0 KS |
1604 | /* Compute masks and transform image IMG on frame F, as specified |
1605 | by the image's specification, */ | |
1606 | ||
1607 | static void | |
d3da34e0 | 1608 | postprocess_image (struct frame *f, struct image *img) |
4ef0d4d0 KS |
1609 | { |
1610 | /* Manipulation of the image's mask. */ | |
1611 | if (img->pixmap) | |
1612 | { | |
1613 | Lisp_Object conversion, spec; | |
1614 | Lisp_Object mask; | |
1615 | ||
1616 | spec = img->spec; | |
1617 | ||
1618 | /* `:heuristic-mask t' | |
1619 | `:mask heuristic' | |
1620 | means build a mask heuristically. | |
1621 | `:heuristic-mask (R G B)' | |
1622 | `:mask (heuristic (R G B))' | |
1623 | means build a mask from color (R G B) in the | |
1624 | image. | |
1625 | `:mask nil' | |
1626 | means remove a mask, if any. */ | |
1627 | ||
1628 | mask = image_spec_value (spec, QCheuristic_mask, NULL); | |
1629 | if (!NILP (mask)) | |
1630 | x_build_heuristic_mask (f, img, mask); | |
1631 | else | |
1632 | { | |
578098f3 | 1633 | bool found_p; |
4ef0d4d0 KS |
1634 | |
1635 | mask = image_spec_value (spec, QCmask, &found_p); | |
1636 | ||
1637 | if (EQ (mask, Qheuristic)) | |
1638 | x_build_heuristic_mask (f, img, Qt); | |
1639 | else if (CONSP (mask) | |
1640 | && EQ (XCAR (mask), Qheuristic)) | |
1641 | { | |
1642 | if (CONSP (XCDR (mask))) | |
1643 | x_build_heuristic_mask (f, img, XCAR (XCDR (mask))); | |
1644 | else | |
1645 | x_build_heuristic_mask (f, img, XCDR (mask)); | |
1646 | } | |
1647 | else if (NILP (mask) && found_p && img->mask) | |
547c9269 | 1648 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
4ef0d4d0 KS |
1649 | } |
1650 | ||
1651 | ||
1652 | /* Should we apply an image transformation algorithm? */ | |
1653 | conversion = image_spec_value (spec, QCconversion, NULL); | |
1654 | if (EQ (conversion, Qdisabled)) | |
1655 | x_disable_image (f, img); | |
1656 | else if (EQ (conversion, Qlaplace)) | |
1657 | x_laplace (f, img); | |
1658 | else if (EQ (conversion, Qemboss)) | |
1659 | x_emboss (f, img); | |
1660 | else if (CONSP (conversion) | |
1661 | && EQ (XCAR (conversion), Qedge_detection)) | |
1662 | { | |
1663 | Lisp_Object tem; | |
1664 | tem = XCDR (conversion); | |
1665 | if (CONSP (tem)) | |
1666 | x_edge_detection (f, img, | |
1667 | Fplist_get (tem, QCmatrix), | |
1668 | Fplist_get (tem, QCcolor_adjustment)); | |
1669 | } | |
1670 | } | |
1671 | } | |
1672 | ||
1673 | ||
1674 | /* Return the id of image with Lisp specification SPEC on frame F. | |
1675 | SPEC must be a valid Lisp image specification (see valid_image_p). */ | |
1676 | ||
13464394 | 1677 | ptrdiff_t |
d3da34e0 | 1678 | lookup_image (struct frame *f, Lisp_Object spec) |
4ef0d4d0 | 1679 | { |
4ef0d4d0 | 1680 | struct image *img; |
0de4bb68 | 1681 | EMACS_UINT hash; |
4ef0d4d0 KS |
1682 | |
1683 | /* F must be a window-system frame, and SPEC must be a valid image | |
1684 | specification. */ | |
a54e2c05 DA |
1685 | eassert (FRAME_WINDOW_P (f)); |
1686 | eassert (valid_image_p (spec)); | |
4ef0d4d0 | 1687 | |
4ef0d4d0 KS |
1688 | /* Look up SPEC in the hash table of the image cache. */ |
1689 | hash = sxhash (spec, 0); | |
d3c35d17 | 1690 | img = search_image_cache (f, spec, hash); |
f1f25b99 CY |
1691 | if (img && img->load_failed_p) |
1692 | { | |
1693 | free_image (f, img); | |
1694 | img = NULL; | |
1695 | } | |
1696 | ||
4ef0d4d0 KS |
1697 | /* If not found, create a new image and cache it. */ |
1698 | if (img == NULL) | |
1699 | { | |
4d7e6e51 | 1700 | block_input (); |
4ef0d4d0 KS |
1701 | img = make_image (spec, hash); |
1702 | cache_image (f, img); | |
578098f3 | 1703 | img->load_failed_p = ! img->type->load (f, img); |
c5b8e0ea YM |
1704 | img->frame_foreground = FRAME_FOREGROUND_PIXEL (f); |
1705 | img->frame_background = FRAME_BACKGROUND_PIXEL (f); | |
4ef0d4d0 KS |
1706 | |
1707 | /* If we can't load the image, and we don't have a width and | |
1708 | height, use some arbitrary width and height so that we can | |
1709 | draw a rectangle for it. */ | |
1710 | if (img->load_failed_p) | |
1711 | { | |
1712 | Lisp_Object value; | |
1713 | ||
1714 | value = image_spec_value (spec, QCwidth, NULL); | |
1715 | img->width = (INTEGERP (value) | |
1716 | ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH); | |
1717 | value = image_spec_value (spec, QCheight, NULL); | |
1718 | img->height = (INTEGERP (value) | |
1719 | ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT); | |
1720 | } | |
1721 | else | |
1722 | { | |
1723 | /* Handle image type independent image attributes | |
1724 | `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF', | |
1725 | `:background COLOR'. */ | |
1726 | Lisp_Object ascent, margin, relief, bg; | |
d311d28c | 1727 | int relief_bound; |
4ef0d4d0 KS |
1728 | |
1729 | ascent = image_spec_value (spec, QCascent, NULL); | |
1730 | if (INTEGERP (ascent)) | |
1731 | img->ascent = XFASTINT (ascent); | |
1732 | else if (EQ (ascent, Qcenter)) | |
1733 | img->ascent = CENTERED_IMAGE_ASCENT; | |
1734 | ||
1735 | margin = image_spec_value (spec, QCmargin, NULL); | |
13464394 | 1736 | if (INTEGERP (margin)) |
4ef0d4d0 | 1737 | img->vmargin = img->hmargin = XFASTINT (margin); |
13464394 | 1738 | else if (CONSP (margin)) |
4ef0d4d0 | 1739 | { |
13464394 PE |
1740 | img->hmargin = XFASTINT (XCAR (margin)); |
1741 | img->vmargin = XFASTINT (XCDR (margin)); | |
4ef0d4d0 KS |
1742 | } |
1743 | ||
1744 | relief = image_spec_value (spec, QCrelief, NULL); | |
d311d28c PE |
1745 | relief_bound = INT_MAX - max (img->hmargin, img->vmargin); |
1746 | if (RANGED_INTEGERP (- relief_bound, relief, relief_bound)) | |
4ef0d4d0 KS |
1747 | { |
1748 | img->relief = XINT (relief); | |
1ea40aa2 EZ |
1749 | img->hmargin += eabs (img->relief); |
1750 | img->vmargin += eabs (img->relief); | |
4ef0d4d0 KS |
1751 | } |
1752 | ||
1753 | if (! img->background_valid) | |
1754 | { | |
1755 | bg = image_spec_value (img->spec, QCbackground, NULL); | |
1756 | if (!NILP (bg)) | |
1757 | { | |
1758 | img->background | |
1759 | = x_alloc_image_color (f, img, bg, | |
1760 | FRAME_BACKGROUND_PIXEL (f)); | |
1761 | img->background_valid = 1; | |
1762 | } | |
1763 | } | |
1764 | ||
1765 | /* Do image transformations and compute masks, unless we | |
1766 | don't have the image yet. */ | |
1767 | if (!EQ (*img->type->type, Qpostscript)) | |
1768 | postprocess_image (f, img); | |
1769 | } | |
1770 | ||
4d7e6e51 | 1771 | unblock_input (); |
4ef0d4d0 KS |
1772 | } |
1773 | ||
1774 | /* We're using IMG, so set its timestamp to `now'. */ | |
e9a9ae03 | 1775 | img->timestamp = current_emacs_time (); |
4ef0d4d0 | 1776 | |
4ef0d4d0 KS |
1777 | /* Value is the image id. */ |
1778 | return img->id; | |
1779 | } | |
1780 | ||
1781 | ||
1782 | /* Cache image IMG in the image cache of frame F. */ | |
1783 | ||
1784 | static void | |
d3da34e0 | 1785 | cache_image (struct frame *f, struct image *img) |
4ef0d4d0 | 1786 | { |
354884c4 | 1787 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
13464394 | 1788 | ptrdiff_t i; |
4ef0d4d0 KS |
1789 | |
1790 | /* Find a free slot in c->images. */ | |
1791 | for (i = 0; i < c->used; ++i) | |
1792 | if (c->images[i] == NULL) | |
1793 | break; | |
1794 | ||
1795 | /* If no free slot found, maybe enlarge c->images. */ | |
1796 | if (i == c->used && c->used == c->size) | |
a69fbedb | 1797 | c->images = xpalloc (c->images, &c->size, 1, -1, sizeof *c->images); |
4ef0d4d0 KS |
1798 | |
1799 | /* Add IMG to c->images, and assign IMG an id. */ | |
1800 | c->images[i] = img; | |
1801 | img->id = i; | |
1802 | if (i == c->used) | |
1803 | ++c->used; | |
1804 | ||
1805 | /* Add IMG to the cache's hash table. */ | |
1806 | i = img->hash % IMAGE_CACHE_BUCKETS_SIZE; | |
1807 | img->next = c->buckets[i]; | |
1808 | if (img->next) | |
1809 | img->next->prev = img; | |
1810 | img->prev = NULL; | |
1811 | c->buckets[i] = img; | |
1812 | } | |
1813 | ||
1814 | ||
1815 | /* Call FN on every image in the image cache of frame F. Used to mark | |
1816 | Lisp Objects in the image cache. */ | |
1817 | ||
354884c4 SM |
1818 | /* Mark Lisp objects in image IMG. */ |
1819 | ||
1820 | static void | |
d3da34e0 | 1821 | mark_image (struct image *img) |
354884c4 SM |
1822 | { |
1823 | mark_object (img->spec); | |
a2bc5bdd | 1824 | mark_object (img->dependencies); |
354884c4 | 1825 | |
b50691aa CY |
1826 | if (!NILP (img->lisp_data)) |
1827 | mark_object (img->lisp_data); | |
354884c4 SM |
1828 | } |
1829 | ||
1830 | ||
4ef0d4d0 | 1831 | void |
354884c4 | 1832 | mark_image_cache (struct image_cache *c) |
4ef0d4d0 | 1833 | { |
354884c4 | 1834 | if (c) |
4ef0d4d0 | 1835 | { |
13464394 | 1836 | ptrdiff_t i; |
354884c4 SM |
1837 | for (i = 0; i < c->used; ++i) |
1838 | if (c->images[i]) | |
1839 | mark_image (c->images[i]); | |
4ef0d4d0 KS |
1840 | } |
1841 | } | |
1842 | ||
1843 | ||
1844 | \f | |
1845 | /*********************************************************************** | |
9e2a2647 | 1846 | X / NS / W32 support code |
4ef0d4d0 KS |
1847 | ***********************************************************************/ |
1848 | ||
0fda9b75 | 1849 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
1850 | |
1851 | /* Macro for defining functions that will be loaded from image DLLs. */ | |
dbdb9a7c | 1852 | #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args |
4ef0d4d0 KS |
1853 | |
1854 | /* Macro for loading those image functions from the library. */ | |
1855 | #define LOAD_IMGLIB_FN(lib,func) { \ | |
1856 | fn_##func = (void *) GetProcAddress (lib, #func); \ | |
1857 | if (!fn_##func) return 0; \ | |
1858 | } | |
1859 | ||
0fda9b75 | 1860 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 1861 | |
578098f3 | 1862 | /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the |
476371c4 | 1863 | windowing system. |
ca4aa935 PE |
1864 | WIDTH and HEIGHT must both be positive. |
1865 | If XIMG is null, assume it is a bitmap. */ | |
578098f3 | 1866 | static bool |
ca4aa935 PE |
1867 | x_check_image_size (XImagePtr ximg, int width, int height) |
1868 | { | |
476371c4 | 1869 | #ifdef HAVE_X_WINDOWS |
ca4aa935 PE |
1870 | /* Respect Xlib's limits: it cannot deal with images that have more |
1871 | than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits | |
476371c4 | 1872 | of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */ |
ca4aa935 PE |
1873 | enum |
1874 | { | |
1875 | XLIB_BYTES_MAX = min (INT_MAX, UINT_MAX), | |
1876 | X_IMAGE_BYTES_MAX = min (XLIB_BYTES_MAX, min (PTRDIFF_MAX, SIZE_MAX)) | |
1877 | }; | |
1878 | ||
1879 | int bitmap_pad, depth, bytes_per_line; | |
1880 | if (ximg) | |
1881 | { | |
1882 | bitmap_pad = ximg->bitmap_pad; | |
1883 | depth = ximg->depth; | |
1884 | bytes_per_line = ximg->bytes_per_line; | |
1885 | } | |
1886 | else | |
1887 | { | |
1888 | bitmap_pad = 8; | |
1889 | depth = 1; | |
1890 | bytes_per_line = (width >> 3) + ((width & 7) != 0); | |
1891 | } | |
1892 | return (width <= (INT_MAX - (bitmap_pad - 1)) / depth | |
1893 | && height <= X_IMAGE_BYTES_MAX / bytes_per_line); | |
476371c4 PE |
1894 | #else |
1895 | /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases. | |
1896 | For now, assume that every image size is allowed on these systems. */ | |
1897 | return 1; | |
1898 | #endif | |
ca4aa935 | 1899 | } |
4ef0d4d0 KS |
1900 | |
1901 | /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on | |
1902 | frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created. | |
1903 | Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated | |
1904 | via xmalloc. Print error messages via image_error if an error | |
578098f3 | 1905 | occurs. Value is true if successful. |
4ef0d4d0 KS |
1906 | |
1907 | On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH | |
1908 | should indicate the bit depth of the image. */ | |
1909 | ||
578098f3 | 1910 | static bool |
d3da34e0 JB |
1911 | x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth, |
1912 | XImagePtr *ximg, Pixmap *pixmap) | |
4ef0d4d0 KS |
1913 | { |
1914 | #ifdef HAVE_X_WINDOWS | |
1915 | Display *display = FRAME_X_DISPLAY (f); | |
1916 | Window window = FRAME_X_WINDOW (f); | |
1917 | Screen *screen = FRAME_X_SCREEN (f); | |
1918 | ||
4d7e6e51 | 1919 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
1920 | |
1921 | if (depth <= 0) | |
1922 | depth = DefaultDepthOfScreen (screen); | |
1923 | *ximg = XCreateImage (display, DefaultVisualOfScreen (screen), | |
1924 | depth, ZPixmap, 0, NULL, width, height, | |
1925 | depth > 16 ? 32 : depth > 8 ? 16 : 8, 0); | |
1926 | if (*ximg == NULL) | |
1927 | { | |
1928 | image_error ("Unable to allocate X image", Qnil, Qnil); | |
1929 | return 0; | |
1930 | } | |
1931 | ||
ca4aa935 PE |
1932 | if (! x_check_image_size (*ximg, width, height)) |
1933 | { | |
1934 | x_destroy_x_image (*ximg); | |
1935 | *ximg = NULL; | |
1936 | image_error ("Image too large (%dx%d)", | |
1937 | make_number (width), make_number (height)); | |
1938 | return 0; | |
1939 | } | |
1940 | ||
4ef0d4d0 | 1941 | /* Allocate image raster. */ |
23f86fce | 1942 | (*ximg)->data = xmalloc ((*ximg)->bytes_per_line * height); |
4ef0d4d0 KS |
1943 | |
1944 | /* Allocate a pixmap of the same size. */ | |
1945 | *pixmap = XCreatePixmap (display, window, width, height, depth); | |
1946 | if (*pixmap == NO_PIXMAP) | |
1947 | { | |
1948 | x_destroy_x_image (*ximg); | |
1949 | *ximg = NULL; | |
1950 | image_error ("Unable to create X pixmap", Qnil, Qnil); | |
1951 | return 0; | |
1952 | } | |
1953 | ||
1954 | return 1; | |
1955 | #endif /* HAVE_X_WINDOWS */ | |
1956 | ||
1957 | #ifdef HAVE_NTGUI | |
1958 | ||
1959 | BITMAPINFOHEADER *header; | |
1960 | HDC hdc; | |
1961 | int scanline_width_bits; | |
1962 | int remainder; | |
1963 | int palette_colors = 0; | |
1964 | ||
1965 | if (depth == 0) | |
1966 | depth = 24; | |
1967 | ||
1968 | if (depth != 1 && depth != 4 && depth != 8 | |
1969 | && depth != 16 && depth != 24 && depth != 32) | |
1970 | { | |
1971 | image_error ("Invalid image bit depth specified", Qnil, Qnil); | |
1972 | return 0; | |
1973 | } | |
1974 | ||
1975 | scanline_width_bits = width * depth; | |
1976 | remainder = scanline_width_bits % 32; | |
1977 | ||
1978 | if (remainder) | |
1979 | scanline_width_bits += 32 - remainder; | |
1980 | ||
1981 | /* Bitmaps with a depth less than 16 need a palette. */ | |
1982 | /* BITMAPINFO structure already contains the first RGBQUAD. */ | |
1983 | if (depth < 16) | |
657d08d3 | 1984 | palette_colors = 1 << (depth - 1); |
4ef0d4d0 KS |
1985 | |
1986 | *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD)); | |
4ef0d4d0 | 1987 | |
72af86bd AS |
1988 | header = &(*ximg)->info.bmiHeader; |
1989 | memset (&(*ximg)->info, 0, sizeof (BITMAPINFO)); | |
4ef0d4d0 KS |
1990 | header->biSize = sizeof (*header); |
1991 | header->biWidth = width; | |
1992 | header->biHeight = -height; /* negative indicates a top-down bitmap. */ | |
1993 | header->biPlanes = 1; | |
1994 | header->biBitCount = depth; | |
1995 | header->biCompression = BI_RGB; | |
1996 | header->biClrUsed = palette_colors; | |
1997 | ||
1998 | /* TODO: fill in palette. */ | |
1999 | if (depth == 1) | |
2000 | { | |
2001 | (*ximg)->info.bmiColors[0].rgbBlue = 0; | |
2002 | (*ximg)->info.bmiColors[0].rgbGreen = 0; | |
2003 | (*ximg)->info.bmiColors[0].rgbRed = 0; | |
2004 | (*ximg)->info.bmiColors[0].rgbReserved = 0; | |
2005 | (*ximg)->info.bmiColors[1].rgbBlue = 255; | |
2006 | (*ximg)->info.bmiColors[1].rgbGreen = 255; | |
2007 | (*ximg)->info.bmiColors[1].rgbRed = 255; | |
2008 | (*ximg)->info.bmiColors[1].rgbReserved = 0; | |
2009 | } | |
2010 | ||
2011 | hdc = get_frame_dc (f); | |
2012 | ||
2013 | /* Create a DIBSection and raster array for the bitmap, | |
2014 | and store its handle in *pixmap. */ | |
2015 | *pixmap = CreateDIBSection (hdc, &((*ximg)->info), | |
2016 | (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS, | |
2e09fef1 EZ |
2017 | /* casting avoids a GCC warning */ |
2018 | (void **)&((*ximg)->data), NULL, 0); | |
4ef0d4d0 KS |
2019 | |
2020 | /* Realize display palette and garbage all frames. */ | |
2021 | release_frame_dc (f, hdc); | |
2022 | ||
2023 | if (*pixmap == NULL) | |
2024 | { | |
5fc9fdeb | 2025 | DWORD err = GetLastError (); |
4ef0d4d0 KS |
2026 | Lisp_Object errcode; |
2027 | /* All system errors are < 10000, so the following is safe. */ | |
13464394 | 2028 | XSETINT (errcode, err); |
4ef0d4d0 KS |
2029 | image_error ("Unable to create bitmap, error code %d", errcode, Qnil); |
2030 | x_destroy_x_image (*ximg); | |
2031 | return 0; | |
2032 | } | |
2033 | ||
2034 | return 1; | |
2035 | ||
2036 | #endif /* HAVE_NTGUI */ | |
2037 | ||
edfda783 | 2038 | #ifdef HAVE_NS |
ed3751c8 | 2039 | *pixmap = ns_image_for_XPM (width, height, depth); |
edfda783 AR |
2040 | if (*pixmap == 0) |
2041 | { | |
2042 | *ximg = NULL; | |
2043 | image_error ("Unable to allocate NSImage for XPM pixmap", Qnil, Qnil); | |
2044 | return 0; | |
2045 | } | |
2046 | *ximg = *pixmap; | |
2047 | return 1; | |
2048 | #endif | |
4ef0d4d0 KS |
2049 | } |
2050 | ||
2051 | ||
2052 | /* Destroy XImage XIMG. Free XIMG->data. */ | |
2053 | ||
2054 | static void | |
d3da34e0 | 2055 | x_destroy_x_image (XImagePtr ximg) |
4ef0d4d0 | 2056 | { |
4d7e6e51 | 2057 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
2058 | if (ximg) |
2059 | { | |
2060 | #ifdef HAVE_X_WINDOWS | |
2061 | xfree (ximg->data); | |
2062 | ximg->data = NULL; | |
2063 | XDestroyImage (ximg); | |
2064 | #endif /* HAVE_X_WINDOWS */ | |
2065 | #ifdef HAVE_NTGUI | |
2066 | /* Data will be freed by DestroyObject. */ | |
2067 | ximg->data = NULL; | |
2068 | xfree (ximg); | |
2069 | #endif /* HAVE_NTGUI */ | |
edfda783 | 2070 | #ifdef HAVE_NS |
ed3751c8 | 2071 | ns_release_object (ximg); |
edfda783 | 2072 | #endif /* HAVE_NS */ |
4ef0d4d0 KS |
2073 | } |
2074 | } | |
2075 | ||
2076 | ||
2077 | /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT | |
2078 | are width and height of both the image and pixmap. */ | |
2079 | ||
2080 | static void | |
d3da34e0 | 2081 | x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int height) |
4ef0d4d0 KS |
2082 | { |
2083 | #ifdef HAVE_X_WINDOWS | |
2084 | GC gc; | |
2085 | ||
4d7e6e51 | 2086 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
2087 | gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL); |
2088 | XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height); | |
2089 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
2090 | #endif /* HAVE_X_WINDOWS */ | |
2091 | ||
2092 | #ifdef HAVE_NTGUI | |
2093 | #if 0 /* I don't think this is necessary looking at where it is used. */ | |
2094 | HDC hdc = get_frame_dc (f); | |
2095 | SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS); | |
2096 | release_frame_dc (f, hdc); | |
2097 | #endif | |
2098 | #endif /* HAVE_NTGUI */ | |
2099 | ||
edfda783 | 2100 | #ifdef HAVE_NS |
a54e2c05 | 2101 | eassert (ximg == pixmap); |
ed3751c8 | 2102 | ns_retain_object (ximg); |
edfda783 | 2103 | #endif |
4ef0d4d0 KS |
2104 | } |
2105 | ||
547c9269 YM |
2106 | /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches |
2107 | with image_put_x_image. */ | |
2108 | ||
2109 | static bool | |
2110 | image_create_x_image_and_pixmap (struct frame *f, struct image *img, | |
2111 | int width, int height, int depth, | |
2112 | XImagePtr *ximg, bool mask_p) | |
2113 | { | |
2114 | eassert ((!mask_p ? img->pixmap : img->mask) == NO_PIXMAP); | |
2115 | ||
2116 | return x_create_x_image_and_pixmap (f, width, height, depth, ximg, | |
2117 | !mask_p ? &img->pixmap : &img->mask); | |
2118 | } | |
2119 | ||
2120 | /* Put X image XIMG into image IMG on frame F, as a mask if and only | |
2121 | if MASK_P. On X, this simply records XIMG on a member of IMG, so | |
2122 | it can be put into the pixmap afterwards via image_sync_to_pixmaps. | |
2123 | On the other platforms, it puts XIMG into the pixmap, then frees | |
2124 | the X image and its buffer. */ | |
2125 | ||
2126 | static void | |
2127 | image_put_x_image (struct frame *f, struct image *img, XImagePtr ximg, | |
2128 | bool mask_p) | |
2129 | { | |
2130 | #ifdef HAVE_X_WINDOWS | |
2131 | if (!mask_p) | |
2132 | { | |
2133 | eassert (img->ximg == NULL); | |
2134 | img->ximg = ximg; | |
2135 | } | |
2136 | else | |
2137 | { | |
2138 | eassert (img->mask_img == NULL); | |
2139 | img->mask_img = ximg; | |
2140 | } | |
2141 | #else | |
2142 | x_put_x_image (f, ximg, !mask_p ? img->pixmap : img->mask, | |
2143 | img->width, img->height); | |
2144 | x_destroy_x_image (ximg); | |
2145 | #endif | |
2146 | } | |
2147 | ||
2148 | #ifdef HAVE_X_WINDOWS | |
2149 | /* Put the X images recorded in IMG on frame F into pixmaps, then free | |
2150 | the X images and their buffers. */ | |
2151 | ||
2152 | static void | |
2153 | image_sync_to_pixmaps (struct frame *f, struct image *img) | |
2154 | { | |
2155 | if (img->ximg) | |
2156 | { | |
2157 | x_put_x_image (f, img->ximg, img->pixmap, img->width, img->height); | |
2158 | x_destroy_x_image (img->ximg); | |
2159 | img->ximg = NULL; | |
2160 | } | |
2161 | if (img->mask_img) | |
2162 | { | |
2163 | x_put_x_image (f, img->mask_img, img->mask, img->width, img->height); | |
2164 | x_destroy_x_image (img->mask_img); | |
2165 | img->mask_img = NULL; | |
2166 | } | |
2167 | } | |
2168 | #endif | |
2169 | ||
2170 | #ifdef HAVE_NTGUI | |
2171 | /* Create a memory device context for IMG on frame F. It stores the | |
2172 | currently selected GDI object into *PREV for future restoration by | |
2173 | image_unget_x_image_or_dc. */ | |
2174 | ||
2175 | static XImagePtr_or_DC | |
2176 | image_get_x_image_or_dc (struct frame *f, struct image *img, bool mask_p, | |
2177 | HGDIOBJ *prev) | |
2178 | { | |
2179 | HDC frame_dc = get_frame_dc (f); | |
2180 | XImagePtr_or_DC ximg = CreateCompatibleDC (frame_dc); | |
2181 | ||
2182 | release_frame_dc (f, frame_dc); | |
2183 | *prev = SelectObject (ximg, !mask_p ? img->pixmap : img->mask); | |
b8075603 YM |
2184 | |
2185 | return ximg; | |
547c9269 YM |
2186 | } |
2187 | ||
b8075603 YM |
2188 | static void |
2189 | image_unget_x_image_or_dc (struct image *img, bool mask_p, | |
2190 | XImagePtr_or_DC ximg, HGDIOBJ prev) | |
547c9269 YM |
2191 | { |
2192 | SelectObject (ximg, prev); | |
2193 | DeleteDC (ximg); | |
2194 | } | |
2195 | #else /* !HAVE_NTGUI */ | |
2196 | /* Get the X image for IMG on frame F. The resulting X image data | |
2197 | should be treated as read-only at least on X. */ | |
2198 | ||
2199 | static XImagePtr | |
2200 | image_get_x_image (struct frame *f, struct image *img, bool mask_p) | |
2201 | { | |
2202 | #ifdef HAVE_X_WINDOWS | |
2203 | XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img; | |
2204 | ||
2205 | if (ximg_in_img) | |
2206 | return ximg_in_img; | |
2207 | else | |
2208 | return XGetImage (FRAME_X_DISPLAY (f), !mask_p ? img->pixmap : img->mask, | |
2209 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
2210 | #elif defined (HAVE_NS) | |
2211 | XImagePtr pixmap = !mask_p ? img->pixmap : img->mask; | |
2212 | ||
2213 | ns_retain_object (pixmap); | |
2214 | return pixmap; | |
2215 | #endif | |
2216 | } | |
2217 | ||
df03dc8a YM |
2218 | static void |
2219 | image_unget_x_image (struct image *img, bool mask_p, XImagePtr ximg) | |
547c9269 YM |
2220 | { |
2221 | #ifdef HAVE_X_WINDOWS | |
2222 | XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img; | |
2223 | ||
2224 | if (ximg_in_img) | |
2225 | eassert (ximg == ximg_in_img); | |
2226 | else | |
2227 | XDestroyImage (ximg); | |
2228 | #elif defined (HAVE_NS) | |
2229 | ns_release_object (ximg); | |
2230 | #endif | |
2231 | } | |
2232 | #endif /* !HAVE_NTGUI */ | |
2233 | ||
4ef0d4d0 KS |
2234 | \f |
2235 | /*********************************************************************** | |
2236 | File Handling | |
2237 | ***********************************************************************/ | |
2238 | ||
6678dc11 | 2239 | /* Find image file FILE. Look in data-directory/images, then |
022af124 KH |
2240 | x-bitmap-file-path. Value is the encoded full name of the file |
2241 | found, or nil if not found. */ | |
4ef0d4d0 | 2242 | |
c640aa9b | 2243 | Lisp_Object |
d3da34e0 | 2244 | x_find_image_file (Lisp_Object file) |
4ef0d4d0 KS |
2245 | { |
2246 | Lisp_Object file_found, search_path; | |
4ef0d4d0 KS |
2247 | int fd; |
2248 | ||
6678dc11 GM |
2249 | /* TODO I think this should use something like image-load-path |
2250 | instead. Unfortunately, that can contain non-string elements. */ | |
2251 | search_path = Fcons (Fexpand_file_name (build_string ("images"), | |
2252 | Vdata_directory), | |
2253 | Vx_bitmap_file_path); | |
4ef0d4d0 | 2254 | |
050b82f6 | 2255 | /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */ |
4ef0d4d0 KS |
2256 | fd = openp (search_path, file, Qnil, &file_found, Qnil); |
2257 | ||
2258 | if (fd == -1) | |
2259 | file_found = Qnil; | |
2260 | else | |
022af124 KH |
2261 | { |
2262 | file_found = ENCODE_FILE (file_found); | |
f6774c1a AS |
2263 | if (fd != -2) |
2264 | emacs_close (fd); | |
022af124 | 2265 | } |
4ef0d4d0 | 2266 | |
4ef0d4d0 KS |
2267 | return file_found; |
2268 | } | |
2269 | ||
2270 | ||
2271 | /* Read FILE into memory. Value is a pointer to a buffer allocated | |
2272 | with xmalloc holding FILE's contents. Value is null if an error | |
2273 | occurred. *SIZE is set to the size of the file. */ | |
2274 | ||
2275 | static unsigned char * | |
dd52fcea | 2276 | slurp_file (char *file, ptrdiff_t *size) |
4ef0d4d0 | 2277 | { |
406af475 | 2278 | FILE *fp = emacs_fopen (file, "rb"); |
4ef0d4d0 KS |
2279 | unsigned char *buf = NULL; |
2280 | struct stat st; | |
2281 | ||
d6453ce4 | 2282 | if (fp && fstat (fileno (fp), &st) == 0 |
dd52fcea | 2283 | && 0 <= st.st_size && st.st_size <= min (PTRDIFF_MAX, SIZE_MAX) |
23f86fce | 2284 | && (buf = xmalloc (st.st_size), |
4ef0d4d0 KS |
2285 | fread (buf, 1, st.st_size, fp) == st.st_size)) |
2286 | { | |
2287 | *size = st.st_size; | |
2288 | fclose (fp); | |
2289 | } | |
2290 | else | |
2291 | { | |
2292 | if (fp) | |
2293 | fclose (fp); | |
2294 | if (buf) | |
2295 | { | |
2296 | xfree (buf); | |
2297 | buf = NULL; | |
2298 | } | |
2299 | } | |
2300 | ||
2301 | return buf; | |
2302 | } | |
2303 | ||
2304 | ||
2305 | \f | |
4ef0d4d0 KS |
2306 | /*********************************************************************** |
2307 | XBM images | |
2308 | ***********************************************************************/ | |
2309 | ||
578098f3 PE |
2310 | static bool xbm_load (struct frame *f, struct image *img); |
2311 | static bool xbm_image_p (Lisp_Object object); | |
2312 | static bool xbm_file_p (Lisp_Object); | |
4ef0d4d0 KS |
2313 | |
2314 | ||
2315 | /* Indices of image specification fields in xbm_format, below. */ | |
2316 | ||
2317 | enum xbm_keyword_index | |
2318 | { | |
2319 | XBM_TYPE, | |
2320 | XBM_FILE, | |
2321 | XBM_WIDTH, | |
2322 | XBM_HEIGHT, | |
2323 | XBM_DATA, | |
2324 | XBM_FOREGROUND, | |
2325 | XBM_BACKGROUND, | |
2326 | XBM_ASCENT, | |
2327 | XBM_MARGIN, | |
2328 | XBM_RELIEF, | |
2329 | XBM_ALGORITHM, | |
2330 | XBM_HEURISTIC_MASK, | |
2331 | XBM_MASK, | |
2332 | XBM_LAST | |
2333 | }; | |
2334 | ||
2335 | /* Vector of image_keyword structures describing the format | |
2336 | of valid XBM image specifications. */ | |
2337 | ||
91433552 | 2338 | static const struct image_keyword xbm_format[XBM_LAST] = |
4ef0d4d0 KS |
2339 | { |
2340 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
2341 | {":file", IMAGE_STRING_VALUE, 0}, | |
2342 | {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2343 | {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2344 | {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2345 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2346 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2347 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 2348 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
2349 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
2350 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2351 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2352 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0} | |
2353 | }; | |
2354 | ||
2355 | /* Structure describing the image type XBM. */ | |
2356 | ||
2357 | static struct image_type xbm_type = | |
2358 | { | |
2359 | &Qxbm, | |
2360 | xbm_image_p, | |
2361 | xbm_load, | |
2362 | x_clear_image, | |
bb4d86b4 | 2363 | NULL, |
4ef0d4d0 KS |
2364 | NULL |
2365 | }; | |
2366 | ||
2367 | /* Tokens returned from xbm_scan. */ | |
2368 | ||
2369 | enum xbm_token | |
2370 | { | |
2371 | XBM_TK_IDENT = 256, | |
2372 | XBM_TK_NUMBER | |
2373 | }; | |
2374 | ||
2375 | ||
578098f3 | 2376 | /* Return true if OBJECT is a valid XBM-type image specification. |
4ef0d4d0 KS |
2377 | A valid specification is a list starting with the symbol `image' |
2378 | The rest of the list is a property list which must contain an | |
578098f3 | 2379 | entry `:type xbm'. |
4ef0d4d0 KS |
2380 | |
2381 | If the specification specifies a file to load, it must contain | |
2382 | an entry `:file FILENAME' where FILENAME is a string. | |
2383 | ||
2384 | If the specification is for a bitmap loaded from memory it must | |
2385 | contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where | |
2386 | WIDTH and HEIGHT are integers > 0. DATA may be: | |
2387 | ||
2388 | 1. a string large enough to hold the bitmap data, i.e. it must | |
2389 | have a size >= (WIDTH + 7) / 8 * HEIGHT | |
2390 | ||
2391 | 2. a bool-vector of size >= WIDTH * HEIGHT | |
2392 | ||
2393 | 3. a vector of strings or bool-vectors, one for each line of the | |
2394 | bitmap. | |
2395 | ||
bbe6f2aa | 2396 | 4. a string containing an in-memory XBM file. WIDTH and HEIGHT |
4ef0d4d0 KS |
2397 | may not be specified in this case because they are defined in the |
2398 | XBM file. | |
2399 | ||
2400 | Both the file and data forms may contain the additional entries | |
2401 | `:background COLOR' and `:foreground COLOR'. If not present, | |
2402 | foreground and background of the frame on which the image is | |
2403 | displayed is used. */ | |
2404 | ||
578098f3 | 2405 | static bool |
d3da34e0 | 2406 | xbm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
2407 | { |
2408 | struct image_keyword kw[XBM_LAST]; | |
2409 | ||
72af86bd | 2410 | memcpy (kw, xbm_format, sizeof kw); |
4ef0d4d0 KS |
2411 | if (!parse_image_spec (object, kw, XBM_LAST, Qxbm)) |
2412 | return 0; | |
2413 | ||
a54e2c05 | 2414 | eassert (EQ (kw[XBM_TYPE].value, Qxbm)); |
4ef0d4d0 KS |
2415 | |
2416 | if (kw[XBM_FILE].count) | |
2417 | { | |
2418 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count) | |
2419 | return 0; | |
2420 | } | |
2421 | else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value)) | |
2422 | { | |
2423 | /* In-memory XBM file. */ | |
2424 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count) | |
2425 | return 0; | |
2426 | } | |
2427 | else | |
2428 | { | |
2429 | Lisp_Object data; | |
13464394 | 2430 | int width, height; |
4ef0d4d0 KS |
2431 | |
2432 | /* Entries for `:width', `:height' and `:data' must be present. */ | |
2433 | if (!kw[XBM_WIDTH].count | |
2434 | || !kw[XBM_HEIGHT].count | |
2435 | || !kw[XBM_DATA].count) | |
2436 | return 0; | |
2437 | ||
2438 | data = kw[XBM_DATA].value; | |
2439 | width = XFASTINT (kw[XBM_WIDTH].value); | |
2440 | height = XFASTINT (kw[XBM_HEIGHT].value); | |
2441 | ||
2442 | /* Check type of data, and width and height against contents of | |
2443 | data. */ | |
2444 | if (VECTORP (data)) | |
2445 | { | |
45aebb64 | 2446 | EMACS_INT i; |
4ef0d4d0 KS |
2447 | |
2448 | /* Number of elements of the vector must be >= height. */ | |
77b37c05 | 2449 | if (ASIZE (data) < height) |
4ef0d4d0 KS |
2450 | return 0; |
2451 | ||
2452 | /* Each string or bool-vector in data must be large enough | |
2453 | for one line of the image. */ | |
2454 | for (i = 0; i < height; ++i) | |
2455 | { | |
28be1ada | 2456 | Lisp_Object elt = AREF (data, i); |
4ef0d4d0 KS |
2457 | |
2458 | if (STRINGP (elt)) | |
2459 | { | |
2460 | if (SCHARS (elt) | |
2461 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR) | |
2462 | return 0; | |
2463 | } | |
2464 | else if (BOOL_VECTOR_P (elt)) | |
2465 | { | |
2466 | if (XBOOL_VECTOR (elt)->size < width) | |
2467 | return 0; | |
2468 | } | |
2469 | else | |
2470 | return 0; | |
2471 | } | |
2472 | } | |
2473 | else if (STRINGP (data)) | |
2474 | { | |
2475 | if (SCHARS (data) | |
2476 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height) | |
2477 | return 0; | |
2478 | } | |
2479 | else if (BOOL_VECTOR_P (data)) | |
2480 | { | |
ca4aa935 | 2481 | if (XBOOL_VECTOR (data)->size / height < width) |
4ef0d4d0 KS |
2482 | return 0; |
2483 | } | |
2484 | else | |
2485 | return 0; | |
2486 | } | |
2487 | ||
2488 | return 1; | |
2489 | } | |
2490 | ||
2491 | ||
2492 | /* Scan a bitmap file. FP is the stream to read from. Value is | |
2493 | either an enumerator from enum xbm_token, or a character for a | |
2494 | single-character token, or 0 at end of file. If scanning an | |
2495 | identifier, store the lexeme of the identifier in SVAL. If | |
2496 | scanning a number, store its value in *IVAL. */ | |
2497 | ||
2498 | static int | |
d3da34e0 | 2499 | xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival) |
4ef0d4d0 KS |
2500 | { |
2501 | unsigned int c; | |
2502 | ||
2503 | loop: | |
2504 | ||
2505 | /* Skip white space. */ | |
620f13b0 | 2506 | while (*s < end && (c = *(*s)++, c_isspace (c))) |
4ef0d4d0 KS |
2507 | ; |
2508 | ||
2509 | if (*s >= end) | |
2510 | c = 0; | |
620f13b0 | 2511 | else if (c_isdigit (c)) |
4ef0d4d0 KS |
2512 | { |
2513 | int value = 0, digit; | |
2514 | ||
2515 | if (c == '0' && *s < end) | |
2516 | { | |
2517 | c = *(*s)++; | |
2518 | if (c == 'x' || c == 'X') | |
2519 | { | |
2520 | while (*s < end) | |
2521 | { | |
2522 | c = *(*s)++; | |
620f13b0 | 2523 | if (c_isdigit (c)) |
4ef0d4d0 KS |
2524 | digit = c - '0'; |
2525 | else if (c >= 'a' && c <= 'f') | |
2526 | digit = c - 'a' + 10; | |
2527 | else if (c >= 'A' && c <= 'F') | |
2528 | digit = c - 'A' + 10; | |
2529 | else | |
2530 | break; | |
2531 | value = 16 * value + digit; | |
2532 | } | |
2533 | } | |
620f13b0 | 2534 | else if (c_isdigit (c)) |
4ef0d4d0 KS |
2535 | { |
2536 | value = c - '0'; | |
2537 | while (*s < end | |
620f13b0 | 2538 | && (c = *(*s)++, c_isdigit (c))) |
4ef0d4d0 KS |
2539 | value = 8 * value + c - '0'; |
2540 | } | |
2541 | } | |
2542 | else | |
2543 | { | |
2544 | value = c - '0'; | |
2545 | while (*s < end | |
620f13b0 | 2546 | && (c = *(*s)++, c_isdigit (c))) |
4ef0d4d0 KS |
2547 | value = 10 * value + c - '0'; |
2548 | } | |
2549 | ||
2550 | if (*s < end) | |
2551 | *s = *s - 1; | |
2552 | *ival = value; | |
2553 | c = XBM_TK_NUMBER; | |
2554 | } | |
620f13b0 | 2555 | else if (c_isalpha (c) || c == '_') |
4ef0d4d0 KS |
2556 | { |
2557 | *sval++ = c; | |
2558 | while (*s < end | |
620f13b0 | 2559 | && (c = *(*s)++, (c_isalnum (c) || c == '_'))) |
4ef0d4d0 KS |
2560 | *sval++ = c; |
2561 | *sval = 0; | |
2562 | if (*s < end) | |
2563 | *s = *s - 1; | |
2564 | c = XBM_TK_IDENT; | |
2565 | } | |
2566 | else if (c == '/' && **s == '*') | |
2567 | { | |
2568 | /* C-style comment. */ | |
2569 | ++*s; | |
2570 | while (**s && (**s != '*' || *(*s + 1) != '/')) | |
2571 | ++*s; | |
2572 | if (**s) | |
2573 | { | |
2574 | *s += 2; | |
2575 | goto loop; | |
2576 | } | |
2577 | } | |
2578 | ||
2579 | return c; | |
2580 | } | |
2581 | ||
2582 | #ifdef HAVE_NTGUI | |
2583 | ||
2584 | /* Create a Windows bitmap from X bitmap data. */ | |
2585 | static HBITMAP | |
2586 | w32_create_pixmap_from_bitmap_data (int width, int height, char *data) | |
2587 | { | |
2588 | static unsigned char swap_nibble[16] | |
2589 | = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */ | |
2590 | 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */ | |
2591 | 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */ | |
2592 | 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */ | |
2593 | int i, j, w1, w2; | |
2594 | unsigned char *bits, *p; | |
2595 | HBITMAP bmp; | |
2596 | ||
2597 | w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */ | |
2598 | w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */ | |
38182d90 | 2599 | bits = alloca (height * w2); |
72af86bd | 2600 | memset (bits, 0, height * w2); |
4ef0d4d0 KS |
2601 | for (i = 0; i < height; i++) |
2602 | { | |
2603 | p = bits + i*w2; | |
2604 | for (j = 0; j < w1; j++) | |
2605 | { | |
2606 | /* Bitswap XBM bytes to match how Windows does things. */ | |
2607 | unsigned char c = *data++; | |
2608 | *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4) | |
2609 | | (swap_nibble[(c>>4) & 0xf])); | |
2610 | } | |
2611 | } | |
2612 | bmp = CreateBitmap (width, height, 1, 1, (char *) bits); | |
2613 | ||
2614 | return bmp; | |
2615 | } | |
2616 | ||
3046a84b | 2617 | static void |
7c3320d8 JB |
2618 | convert_mono_to_color_image (struct frame *f, struct image *img, |
2619 | COLORREF foreground, COLORREF background) | |
4ef0d4d0 KS |
2620 | { |
2621 | HDC hdc, old_img_dc, new_img_dc; | |
2622 | HGDIOBJ old_prev, new_prev; | |
2623 | HBITMAP new_pixmap; | |
2624 | ||
2625 | hdc = get_frame_dc (f); | |
2626 | old_img_dc = CreateCompatibleDC (hdc); | |
2627 | new_img_dc = CreateCompatibleDC (hdc); | |
2628 | new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height); | |
2629 | release_frame_dc (f, hdc); | |
2630 | old_prev = SelectObject (old_img_dc, img->pixmap); | |
2631 | new_prev = SelectObject (new_img_dc, new_pixmap); | |
44b1dc2e JR |
2632 | /* Windows convention for mono bitmaps is black = background, |
2633 | white = foreground. */ | |
2386b1f1 JR |
2634 | SetTextColor (new_img_dc, background); |
2635 | SetBkColor (new_img_dc, foreground); | |
4ef0d4d0 KS |
2636 | |
2637 | BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc, | |
2638 | 0, 0, SRCCOPY); | |
2639 | ||
2640 | SelectObject (old_img_dc, old_prev); | |
2641 | SelectObject (new_img_dc, new_prev); | |
2642 | DeleteDC (old_img_dc); | |
2643 | DeleteDC (new_img_dc); | |
2644 | DeleteObject (img->pixmap); | |
2645 | if (new_pixmap == 0) | |
2646 | fprintf (stderr, "Failed to convert image to color.\n"); | |
2647 | else | |
2648 | img->pixmap = new_pixmap; | |
2649 | } | |
2650 | ||
2651 | #define XBM_BIT_SHUFFLE(b) (~(b)) | |
2652 | ||
2653 | #else | |
2654 | ||
2655 | #define XBM_BIT_SHUFFLE(b) (b) | |
2656 | ||
2657 | #endif /* HAVE_NTGUI */ | |
2658 | ||
2659 | ||
2660 | static void | |
d3da34e0 JB |
2661 | Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data, |
2662 | RGB_PIXEL_COLOR fg, RGB_PIXEL_COLOR bg, | |
578098f3 | 2663 | bool non_default_colors) |
4ef0d4d0 KS |
2664 | { |
2665 | #ifdef HAVE_NTGUI | |
2666 | img->pixmap | |
2667 | = w32_create_pixmap_from_bitmap_data (img->width, img->height, data); | |
2668 | ||
2669 | /* If colors were specified, transfer the bitmap to a color one. */ | |
2670 | if (non_default_colors) | |
2671 | convert_mono_to_color_image (f, img, fg, bg); | |
edfda783 AR |
2672 | |
2673 | #elif defined (HAVE_NS) | |
ed3751c8 | 2674 | img->pixmap = ns_image_from_XBM (data, img->width, img->height); |
edfda783 | 2675 | |
4ef0d4d0 | 2676 | #else |
ca4aa935 PE |
2677 | img->pixmap = |
2678 | (x_check_image_size (0, img->width, img->height) | |
2679 | ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f), | |
4ef0d4d0 KS |
2680 | FRAME_X_WINDOW (f), |
2681 | data, | |
2682 | img->width, img->height, | |
2683 | fg, bg, | |
ca4aa935 PE |
2684 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))) |
2685 | : NO_PIXMAP); | |
edfda783 | 2686 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
2687 | } |
2688 | ||
2689 | ||
2690 | ||
2691 | /* Replacement for XReadBitmapFileData which isn't available under old | |
2692 | X versions. CONTENTS is a pointer to a buffer to parse; END is the | |
2693 | buffer's end. Set *WIDTH and *HEIGHT to the width and height of | |
2694 | the image. Return in *DATA the bitmap data allocated with xmalloc. | |
578098f3 PE |
2695 | Value is true if successful. DATA null means just test if |
2696 | CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR, | |
2697 | inhibit the call to image_error when the image size is invalid (the | |
2698 | bitmap remains unread). */ | |
4ef0d4d0 | 2699 | |
578098f3 | 2700 | static bool |
d3da34e0 | 2701 | xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *end, |
b8dc29e9 | 2702 | int *width, int *height, char **data, |
578098f3 | 2703 | bool inhibit_image_error) |
4ef0d4d0 KS |
2704 | { |
2705 | unsigned char *s = contents; | |
2706 | char buffer[BUFSIZ]; | |
578098f3 PE |
2707 | bool padding_p = 0; |
2708 | bool v10 = 0; | |
ca4aa935 | 2709 | int bytes_per_line, i, nbytes; |
b8dc29e9 | 2710 | char *p; |
4ef0d4d0 KS |
2711 | int value; |
2712 | int LA1; | |
2713 | ||
2714 | #define match() \ | |
2715 | LA1 = xbm_scan (&s, end, buffer, &value) | |
2716 | ||
2717 | #define expect(TOKEN) \ | |
2718 | if (LA1 != (TOKEN)) \ | |
2719 | goto failure; \ | |
2720 | else \ | |
2721 | match () | |
2722 | ||
2723 | #define expect_ident(IDENT) \ | |
2724 | if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \ | |
2725 | match (); \ | |
2726 | else \ | |
2727 | goto failure | |
2728 | ||
2729 | *width = *height = -1; | |
2730 | if (data) | |
2731 | *data = NULL; | |
2732 | LA1 = xbm_scan (&s, end, buffer, &value); | |
2733 | ||
2734 | /* Parse defines for width, height and hot-spots. */ | |
2735 | while (LA1 == '#') | |
2736 | { | |
2737 | match (); | |
2738 | expect_ident ("define"); | |
2739 | expect (XBM_TK_IDENT); | |
2740 | ||
431feaf6 | 2741 | if (LA1 == XBM_TK_NUMBER) |
4ef0d4d0 | 2742 | { |
6ae141d6 PE |
2743 | char *q = strrchr (buffer, '_'); |
2744 | q = q ? q + 1 : buffer; | |
2745 | if (strcmp (q, "width") == 0) | |
4ef0d4d0 | 2746 | *width = value; |
6ae141d6 | 2747 | else if (strcmp (q, "height") == 0) |
4ef0d4d0 KS |
2748 | *height = value; |
2749 | } | |
2750 | expect (XBM_TK_NUMBER); | |
2751 | } | |
2752 | ||
f1f25b99 | 2753 | if (!check_image_size (f, *width, *height)) |
10ea2b82 | 2754 | { |
ce959360 CY |
2755 | if (!inhibit_image_error) |
2756 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
10ea2b82 JR |
2757 | goto failure; |
2758 | } | |
4ef0d4d0 KS |
2759 | else if (data == NULL) |
2760 | goto success; | |
2761 | ||
2762 | /* Parse bits. Must start with `static'. */ | |
2763 | expect_ident ("static"); | |
2764 | if (LA1 == XBM_TK_IDENT) | |
2765 | { | |
2766 | if (strcmp (buffer, "unsigned") == 0) | |
2767 | { | |
2768 | match (); | |
2769 | expect_ident ("char"); | |
2770 | } | |
2771 | else if (strcmp (buffer, "short") == 0) | |
2772 | { | |
2773 | match (); | |
2774 | v10 = 1; | |
2775 | if (*width % 16 && *width % 16 < 9) | |
2776 | padding_p = 1; | |
2777 | } | |
2778 | else if (strcmp (buffer, "char") == 0) | |
2779 | match (); | |
2780 | else | |
2781 | goto failure; | |
2782 | } | |
2783 | else | |
2784 | goto failure; | |
2785 | ||
2786 | expect (XBM_TK_IDENT); | |
2787 | expect ('['); | |
2788 | expect (']'); | |
2789 | expect ('='); | |
2790 | expect ('{'); | |
2791 | ||
ca4aa935 PE |
2792 | if (! x_check_image_size (0, *width, *height)) |
2793 | { | |
2794 | if (!inhibit_image_error) | |
2795 | image_error ("Image too large (%dx%d)", | |
2796 | make_number (*width), make_number (*height)); | |
2797 | goto failure; | |
2798 | } | |
4ef0d4d0 KS |
2799 | bytes_per_line = (*width + 7) / 8 + padding_p; |
2800 | nbytes = bytes_per_line * *height; | |
23f86fce | 2801 | p = *data = xmalloc (nbytes); |
4ef0d4d0 KS |
2802 | |
2803 | if (v10) | |
2804 | { | |
2805 | for (i = 0; i < nbytes; i += 2) | |
2806 | { | |
2807 | int val = value; | |
2808 | expect (XBM_TK_NUMBER); | |
2809 | ||
2810 | *p++ = XBM_BIT_SHUFFLE (val); | |
2811 | if (!padding_p || ((i + 2) % bytes_per_line)) | |
2812 | *p++ = XBM_BIT_SHUFFLE (value >> 8); | |
2813 | ||
2814 | if (LA1 == ',' || LA1 == '}') | |
2815 | match (); | |
2816 | else | |
2817 | goto failure; | |
2818 | } | |
2819 | } | |
2820 | else | |
2821 | { | |
2822 | for (i = 0; i < nbytes; ++i) | |
2823 | { | |
2824 | int val = value; | |
2825 | expect (XBM_TK_NUMBER); | |
2826 | ||
2827 | *p++ = XBM_BIT_SHUFFLE (val); | |
2828 | ||
2829 | if (LA1 == ',' || LA1 == '}') | |
2830 | match (); | |
2831 | else | |
2832 | goto failure; | |
2833 | } | |
2834 | } | |
2835 | ||
2836 | success: | |
2837 | return 1; | |
2838 | ||
2839 | failure: | |
2840 | ||
2841 | if (data && *data) | |
2842 | { | |
2843 | xfree (*data); | |
2844 | *data = NULL; | |
2845 | } | |
2846 | return 0; | |
2847 | ||
2848 | #undef match | |
2849 | #undef expect | |
2850 | #undef expect_ident | |
2851 | } | |
2852 | ||
2853 | ||
2854 | /* Load XBM image IMG which will be displayed on frame F from buffer | |
578098f3 | 2855 | CONTENTS. END is the end of the buffer. Value is true if |
4ef0d4d0 KS |
2856 | successful. */ |
2857 | ||
578098f3 | 2858 | static bool |
d3da34e0 JB |
2859 | xbm_load_image (struct frame *f, struct image *img, unsigned char *contents, |
2860 | unsigned char *end) | |
4ef0d4d0 | 2861 | { |
578098f3 | 2862 | bool rc; |
b8dc29e9 | 2863 | char *data; |
578098f3 | 2864 | bool success_p = 0; |
4ef0d4d0 | 2865 | |
ce959360 CY |
2866 | rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height, |
2867 | &data, 0); | |
4ef0d4d0 KS |
2868 | if (rc) |
2869 | { | |
2870 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2871 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
578098f3 | 2872 | bool non_default_colors = 0; |
4ef0d4d0 KS |
2873 | Lisp_Object value; |
2874 | ||
a54e2c05 | 2875 | eassert (img->width > 0 && img->height > 0); |
4ef0d4d0 KS |
2876 | |
2877 | /* Get foreground and background colors, maybe allocate colors. */ | |
2878 | value = image_spec_value (img->spec, QCforeground, NULL); | |
2879 | if (!NILP (value)) | |
2880 | { | |
2881 | foreground = x_alloc_image_color (f, img, value, foreground); | |
2882 | non_default_colors = 1; | |
2883 | } | |
2884 | value = image_spec_value (img->spec, QCbackground, NULL); | |
2885 | if (!NILP (value)) | |
2886 | { | |
2887 | background = x_alloc_image_color (f, img, value, background); | |
2888 | img->background = background; | |
2889 | img->background_valid = 1; | |
2890 | non_default_colors = 1; | |
2891 | } | |
2892 | ||
1f026899 | 2893 | Create_Pixmap_From_Bitmap_Data (f, img, data, |
4ef0d4d0 KS |
2894 | foreground, background, |
2895 | non_default_colors); | |
2896 | xfree (data); | |
2897 | ||
2898 | if (img->pixmap == NO_PIXMAP) | |
2899 | { | |
2900 | x_clear_image (f, img); | |
2901 | image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil); | |
2902 | } | |
2903 | else | |
2904 | success_p = 1; | |
2905 | } | |
2906 | else | |
2907 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
2908 | ||
2909 | return success_p; | |
2910 | } | |
2911 | ||
2912 | ||
578098f3 | 2913 | /* Value is true if DATA looks like an in-memory XBM file. */ |
4ef0d4d0 | 2914 | |
578098f3 | 2915 | static bool |
d3da34e0 | 2916 | xbm_file_p (Lisp_Object data) |
4ef0d4d0 KS |
2917 | { |
2918 | int w, h; | |
2919 | return (STRINGP (data) | |
f1f25b99 | 2920 | && xbm_read_bitmap_data (NULL, SDATA (data), |
ce959360 CY |
2921 | (SDATA (data) + SBYTES (data)), |
2922 | &w, &h, NULL, 1)); | |
4ef0d4d0 KS |
2923 | } |
2924 | ||
2925 | ||
2926 | /* Fill image IMG which is used on frame F with pixmap data. Value is | |
578098f3 | 2927 | true if successful. */ |
4ef0d4d0 | 2928 | |
578098f3 | 2929 | static bool |
d3da34e0 | 2930 | xbm_load (struct frame *f, struct image *img) |
4ef0d4d0 | 2931 | { |
578098f3 | 2932 | bool success_p = 0; |
4ef0d4d0 KS |
2933 | Lisp_Object file_name; |
2934 | ||
a54e2c05 | 2935 | eassert (xbm_image_p (img->spec)); |
4ef0d4d0 KS |
2936 | |
2937 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
2938 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
2939 | if (STRINGP (file_name)) | |
2940 | { | |
2941 | Lisp_Object file; | |
2942 | unsigned char *contents; | |
dd52fcea | 2943 | ptrdiff_t size; |
4ef0d4d0 KS |
2944 | |
2945 | file = x_find_image_file (file_name); | |
4ef0d4d0 KS |
2946 | if (!STRINGP (file)) |
2947 | { | |
2948 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
4ef0d4d0 KS |
2949 | return 0; |
2950 | } | |
2951 | ||
42a5b22f | 2952 | contents = slurp_file (SSDATA (file), &size); |
4ef0d4d0 KS |
2953 | if (contents == NULL) |
2954 | { | |
2955 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
2956 | return 0; |
2957 | } | |
2958 | ||
2959 | success_p = xbm_load_image (f, img, contents, contents + size); | |
d5a19415 | 2960 | xfree (contents); |
4ef0d4d0 KS |
2961 | } |
2962 | else | |
2963 | { | |
2964 | struct image_keyword fmt[XBM_LAST]; | |
2965 | Lisp_Object data; | |
2966 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2967 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
578098f3 | 2968 | bool non_default_colors = 0; |
4ef0d4d0 | 2969 | char *bits; |
578098f3 PE |
2970 | bool parsed_p; |
2971 | bool in_memory_file_p = 0; | |
4ef0d4d0 KS |
2972 | |
2973 | /* See if data looks like an in-memory XBM file. */ | |
2974 | data = image_spec_value (img->spec, QCdata, NULL); | |
2975 | in_memory_file_p = xbm_file_p (data); | |
2976 | ||
2977 | /* Parse the image specification. */ | |
72af86bd | 2978 | memcpy (fmt, xbm_format, sizeof fmt); |
4ef0d4d0 | 2979 | parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm); |
a54e2c05 | 2980 | eassert (parsed_p); |
4ef0d4d0 KS |
2981 | |
2982 | /* Get specified width, and height. */ | |
2983 | if (!in_memory_file_p) | |
2984 | { | |
2985 | img->width = XFASTINT (fmt[XBM_WIDTH].value); | |
2986 | img->height = XFASTINT (fmt[XBM_HEIGHT].value); | |
a54e2c05 | 2987 | eassert (img->width > 0 && img->height > 0); |
ca4aa935 PE |
2988 | if (!check_image_size (f, img->width, img->height)) |
2989 | { | |
2990 | image_error ("Invalid image size (see `max-image-size')", | |
2991 | Qnil, Qnil); | |
2992 | return 0; | |
2993 | } | |
4ef0d4d0 KS |
2994 | } |
2995 | ||
2996 | /* Get foreground and background colors, maybe allocate colors. */ | |
2997 | if (fmt[XBM_FOREGROUND].count | |
2998 | && STRINGP (fmt[XBM_FOREGROUND].value)) | |
2999 | { | |
3000 | foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value, | |
3001 | foreground); | |
3002 | non_default_colors = 1; | |
3003 | } | |
3004 | ||
3005 | if (fmt[XBM_BACKGROUND].count | |
3006 | && STRINGP (fmt[XBM_BACKGROUND].value)) | |
3007 | { | |
3008 | background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value, | |
3009 | background); | |
3010 | non_default_colors = 1; | |
3011 | } | |
3012 | ||
3013 | if (in_memory_file_p) | |
3014 | success_p = xbm_load_image (f, img, SDATA (data), | |
3015 | (SDATA (data) | |
3016 | + SBYTES (data))); | |
3017 | else | |
3018 | { | |
3019 | if (VECTORP (data)) | |
3020 | { | |
3021 | int i; | |
3022 | char *p; | |
3023 | int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR; | |
3024 | ||
38182d90 | 3025 | p = bits = alloca (nbytes * img->height); |
4ef0d4d0 KS |
3026 | for (i = 0; i < img->height; ++i, p += nbytes) |
3027 | { | |
28be1ada | 3028 | Lisp_Object line = AREF (data, i); |
4ef0d4d0 | 3029 | if (STRINGP (line)) |
72af86bd | 3030 | memcpy (p, SDATA (line), nbytes); |
4ef0d4d0 | 3031 | else |
72af86bd | 3032 | memcpy (p, XBOOL_VECTOR (line)->data, nbytes); |
4ef0d4d0 KS |
3033 | } |
3034 | } | |
3035 | else if (STRINGP (data)) | |
42a5b22f | 3036 | bits = SSDATA (data); |
4ef0d4d0 | 3037 | else |
b8dc29e9 | 3038 | bits = (char *) XBOOL_VECTOR (data)->data; |
4ef0d4d0 | 3039 | |
0fda9b75 | 3040 | #ifdef HAVE_NTGUI |
44b1dc2e JR |
3041 | { |
3042 | char *invertedBits; | |
3043 | int nbytes, i; | |
3044 | /* Windows mono bitmaps are reversed compared with X. */ | |
3045 | invertedBits = bits; | |
285d07e2 | 3046 | nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR |
44b1dc2e | 3047 | * img->height; |
38182d90 | 3048 | bits = alloca (nbytes); |
44b1dc2e JR |
3049 | for (i = 0; i < nbytes; i++) |
3050 | bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]); | |
3051 | } | |
3052 | #endif | |
4ef0d4d0 KS |
3053 | /* Create the pixmap. */ |
3054 | ||
ca4aa935 PE |
3055 | if (x_check_image_size (0, img->width, img->height)) |
3056 | Create_Pixmap_From_Bitmap_Data (f, img, bits, | |
3057 | foreground, background, | |
3058 | non_default_colors); | |
3059 | else | |
3060 | img->pixmap = NO_PIXMAP; | |
3061 | ||
4ef0d4d0 KS |
3062 | if (img->pixmap) |
3063 | success_p = 1; | |
3064 | else | |
3065 | { | |
3066 | image_error ("Unable to create pixmap for XBM image `%s'", | |
3067 | img->spec, Qnil); | |
3068 | x_clear_image (f, img); | |
3069 | } | |
3070 | } | |
3071 | } | |
3072 | ||
3073 | return success_p; | |
3074 | } | |
3075 | ||
3076 | ||
3077 | \f | |
3078 | /*********************************************************************** | |
3079 | XPM images | |
3080 | ***********************************************************************/ | |
3081 | ||
9e2a2647 | 3082 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 | 3083 | |
578098f3 PE |
3084 | static bool xpm_image_p (Lisp_Object object); |
3085 | static bool xpm_load (struct frame *f, struct image *img); | |
4ef0d4d0 | 3086 | |
9e2a2647 | 3087 | #endif /* HAVE_XPM || HAVE_NS */ |
ea1aaa6f ST |
3088 | |
3089 | #ifdef HAVE_XPM | |
4ef0d4d0 KS |
3090 | #ifdef HAVE_NTGUI |
3091 | /* Indicate to xpm.h that we don't have Xlib. */ | |
3092 | #define FOR_MSW | |
3093 | /* simx.h in xpm defines XColor and XImage differently than Emacs. */ | |
279d3293 | 3094 | /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */ |
4ef0d4d0 KS |
3095 | #define XColor xpm_XColor |
3096 | #define XImage xpm_XImage | |
279d3293 | 3097 | #define Display xpm_Display |
4ef0d4d0 KS |
3098 | #define PIXEL_ALREADY_TYPEDEFED |
3099 | #include "X11/xpm.h" | |
3100 | #undef FOR_MSW | |
3101 | #undef XColor | |
3102 | #undef XImage | |
279d3293 | 3103 | #undef Display |
4ef0d4d0 KS |
3104 | #undef PIXEL_ALREADY_TYPEDEFED |
3105 | #else | |
3106 | #include "X11/xpm.h" | |
3107 | #endif /* HAVE_NTGUI */ | |
ea1aaa6f | 3108 | #endif /* HAVE_XPM */ |
4ef0d4d0 | 3109 | |
9e2a2647 | 3110 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 KS |
3111 | /* The symbol `xpm' identifying XPM-format images. */ |
3112 | ||
955cbe7b | 3113 | static Lisp_Object Qxpm; |
4ef0d4d0 KS |
3114 | |
3115 | /* Indices of image specification fields in xpm_format, below. */ | |
3116 | ||
3117 | enum xpm_keyword_index | |
3118 | { | |
3119 | XPM_TYPE, | |
3120 | XPM_FILE, | |
3121 | XPM_DATA, | |
3122 | XPM_ASCENT, | |
3123 | XPM_MARGIN, | |
3124 | XPM_RELIEF, | |
3125 | XPM_ALGORITHM, | |
3126 | XPM_HEURISTIC_MASK, | |
3127 | XPM_MASK, | |
3128 | XPM_COLOR_SYMBOLS, | |
3129 | XPM_BACKGROUND, | |
3130 | XPM_LAST | |
3131 | }; | |
3132 | ||
3133 | /* Vector of image_keyword structures describing the format | |
3134 | of valid XPM image specifications. */ | |
3135 | ||
91433552 | 3136 | static const struct image_keyword xpm_format[XPM_LAST] = |
4ef0d4d0 KS |
3137 | { |
3138 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
3139 | {":file", IMAGE_STRING_VALUE, 0}, | |
3140 | {":data", IMAGE_STRING_VALUE, 0}, | |
3141 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 3142 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
3143 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
3144 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3145 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3146 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3147 | {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3148 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
3149 | }; | |
3150 | ||
a9e7a9d5 | 3151 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 3152 | static bool init_xpm_functions (void); |
bb4d86b4 CY |
3153 | #else |
3154 | #define init_xpm_functions NULL | |
3155 | #endif | |
3156 | ||
4ef0d4d0 KS |
3157 | /* Structure describing the image type XPM. */ |
3158 | ||
3159 | static struct image_type xpm_type = | |
3160 | { | |
3161 | &Qxpm, | |
3162 | xpm_image_p, | |
3163 | xpm_load, | |
3164 | x_clear_image, | |
bb4d86b4 | 3165 | init_xpm_functions, |
4ef0d4d0 KS |
3166 | NULL |
3167 | }; | |
3168 | ||
3169 | #ifdef HAVE_X_WINDOWS | |
3170 | ||
3171 | /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation | |
3172 | functions for allocating image colors. Our own functions handle | |
3173 | color allocation failures more gracefully than the ones on the XPM | |
3174 | lib. */ | |
3175 | ||
3176 | #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure | |
3177 | #define ALLOC_XPM_COLORS | |
3178 | #endif | |
3179 | #endif /* HAVE_X_WINDOWS */ | |
3180 | ||
3181 | #ifdef ALLOC_XPM_COLORS | |
3182 | ||
f57e2426 J |
3183 | static struct xpm_cached_color *xpm_cache_color (struct frame *, char *, |
3184 | XColor *, int); | |
4ef0d4d0 KS |
3185 | |
3186 | /* An entry in a hash table used to cache color definitions of named | |
3187 | colors. This cache is necessary to speed up XPM image loading in | |
3188 | case we do color allocations ourselves. Without it, we would need | |
3189 | a call to XParseColor per pixel in the image. */ | |
3190 | ||
3191 | struct xpm_cached_color | |
3192 | { | |
3193 | /* Next in collision chain. */ | |
3194 | struct xpm_cached_color *next; | |
3195 | ||
3196 | /* Color definition (RGB and pixel color). */ | |
3197 | XColor color; | |
3198 | ||
3199 | /* Color name. */ | |
fbe9e0b9 | 3200 | char name[FLEXIBLE_ARRAY_MEMBER]; |
4ef0d4d0 KS |
3201 | }; |
3202 | ||
3203 | /* The hash table used for the color cache, and its bucket vector | |
3204 | size. */ | |
3205 | ||
3206 | #define XPM_COLOR_CACHE_BUCKETS 1001 | |
cd44d2eb | 3207 | static struct xpm_cached_color **xpm_color_cache; |
4ef0d4d0 KS |
3208 | |
3209 | /* Initialize the color cache. */ | |
3210 | ||
3211 | static void | |
d3da34e0 | 3212 | xpm_init_color_cache (struct frame *f, XpmAttributes *attrs) |
4ef0d4d0 KS |
3213 | { |
3214 | size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache; | |
23f86fce | 3215 | xpm_color_cache = xzalloc (nbytes); |
4ef0d4d0 KS |
3216 | init_color_table (); |
3217 | ||
3218 | if (attrs->valuemask & XpmColorSymbols) | |
3219 | { | |
3220 | int i; | |
3221 | XColor color; | |
3222 | ||
3223 | for (i = 0; i < attrs->numsymbols; ++i) | |
3224 | if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3225 | attrs->colorsymbols[i].value, &color)) | |
3226 | { | |
3227 | color.pixel = lookup_rgb_color (f, color.red, color.green, | |
3228 | color.blue); | |
3229 | xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1); | |
3230 | } | |
3231 | } | |
3232 | } | |
3233 | ||
3234 | /* Free the color cache. */ | |
3235 | ||
3236 | static void | |
d3da34e0 | 3237 | xpm_free_color_cache (void) |
4ef0d4d0 KS |
3238 | { |
3239 | struct xpm_cached_color *p, *next; | |
3240 | int i; | |
3241 | ||
3242 | for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i) | |
3243 | for (p = xpm_color_cache[i]; p; p = next) | |
3244 | { | |
3245 | next = p->next; | |
3246 | xfree (p); | |
3247 | } | |
3248 | ||
3249 | xfree (xpm_color_cache); | |
3250 | xpm_color_cache = NULL; | |
3251 | free_color_table (); | |
3252 | } | |
3253 | ||
3254 | /* Return the bucket index for color named COLOR_NAME in the color | |
3255 | cache. */ | |
3256 | ||
3257 | static int | |
d3da34e0 | 3258 | xpm_color_bucket (char *color_name) |
4ef0d4d0 | 3259 | { |
3f791afe PE |
3260 | EMACS_UINT hash = hash_string (color_name, strlen (color_name)); |
3261 | return hash % XPM_COLOR_CACHE_BUCKETS; | |
4ef0d4d0 KS |
3262 | } |
3263 | ||
3264 | ||
3265 | /* On frame F, cache values COLOR for color with name COLOR_NAME. | |
3266 | BUCKET, if >= 0, is a precomputed bucket index. Value is the cache | |
3267 | entry added. */ | |
3268 | ||
3269 | static struct xpm_cached_color * | |
d3da34e0 | 3270 | xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket) |
4ef0d4d0 KS |
3271 | { |
3272 | size_t nbytes; | |
3273 | struct xpm_cached_color *p; | |
3274 | ||
3275 | if (bucket < 0) | |
3276 | bucket = xpm_color_bucket (color_name); | |
3277 | ||
3f791afe | 3278 | nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1; |
23f86fce | 3279 | p = xmalloc (nbytes); |
4ef0d4d0 KS |
3280 | strcpy (p->name, color_name); |
3281 | p->color = *color; | |
3282 | p->next = xpm_color_cache[bucket]; | |
3283 | xpm_color_cache[bucket] = p; | |
3284 | return p; | |
3285 | } | |
3286 | ||
3287 | /* Look up color COLOR_NAME for frame F in the color cache. If found, | |
3288 | return the cached definition in *COLOR. Otherwise, make a new | |
578098f3 | 3289 | entry in the cache and allocate the color. Value is false if color |
4ef0d4d0 KS |
3290 | allocation failed. */ |
3291 | ||
578098f3 | 3292 | static bool |
d3da34e0 | 3293 | xpm_lookup_color (struct frame *f, char *color_name, XColor *color) |
4ef0d4d0 KS |
3294 | { |
3295 | struct xpm_cached_color *p; | |
3296 | int h = xpm_color_bucket (color_name); | |
3297 | ||
3298 | for (p = xpm_color_cache[h]; p; p = p->next) | |
3299 | if (strcmp (p->name, color_name) == 0) | |
3300 | break; | |
3301 | ||
3302 | if (p != NULL) | |
3303 | *color = p->color; | |
3304 | else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3305 | color_name, color)) | |
3306 | { | |
3307 | color->pixel = lookup_rgb_color (f, color->red, color->green, | |
3308 | color->blue); | |
3309 | p = xpm_cache_color (f, color_name, color, h); | |
3310 | } | |
3311 | /* You get `opaque' at least from ImageMagick converting pbm to xpm | |
3312 | with transparency, and it's useful. */ | |
3313 | else if (strcmp ("opaque", color_name) == 0) | |
3314 | { | |
72af86bd | 3315 | memset (color, 0, sizeof (XColor)); /* Is this necessary/correct? */ |
4ef0d4d0 KS |
3316 | color->pixel = FRAME_FOREGROUND_PIXEL (f); |
3317 | p = xpm_cache_color (f, color_name, color, h); | |
3318 | } | |
3319 | ||
3320 | return p != NULL; | |
3321 | } | |
3322 | ||
3323 | ||
3324 | /* Callback for allocating color COLOR_NAME. Called from the XPM lib. | |
3325 | CLOSURE is a pointer to the frame on which we allocate the | |
3326 | color. Return in *COLOR the allocated color. Value is non-zero | |
3327 | if successful. */ | |
3328 | ||
3329 | static int | |
d3da34e0 JB |
3330 | xpm_alloc_color (Display *dpy, Colormap cmap, char *color_name, XColor *color, |
3331 | void *closure) | |
4ef0d4d0 KS |
3332 | { |
3333 | return xpm_lookup_color ((struct frame *) closure, color_name, color); | |
3334 | } | |
3335 | ||
3336 | ||
3337 | /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE | |
3338 | is a pointer to the frame on which we allocate the color. Value is | |
3339 | non-zero if successful. */ | |
3340 | ||
3341 | static int | |
d3da34e0 | 3342 | xpm_free_colors (Display *dpy, Colormap cmap, Pixel *pixels, int npixels, void *closure) |
4ef0d4d0 KS |
3343 | { |
3344 | return 1; | |
3345 | } | |
3346 | ||
3347 | #endif /* ALLOC_XPM_COLORS */ | |
3348 | ||
3349 | ||
0fda9b75 | 3350 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
3351 | |
3352 | /* XPM library details. */ | |
3353 | ||
14beddf4 CY |
3354 | DEF_IMGLIB_FN (void, XpmFreeAttributes, (XpmAttributes *)); |
3355 | DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer, (Display *, char *, xpm_XImage **, | |
850690cc | 3356 | xpm_XImage **, XpmAttributes *)); |
14beddf4 | 3357 | DEF_IMGLIB_FN (int, XpmReadFileToImage, (Display *, char *, xpm_XImage **, |
850690cc | 3358 | xpm_XImage **, XpmAttributes *)); |
14beddf4 | 3359 | DEF_IMGLIB_FN (void, XImageFree, (xpm_XImage *)); |
4ef0d4d0 | 3360 | |
578098f3 | 3361 | static bool |
d07ff9db | 3362 | init_xpm_functions (void) |
4ef0d4d0 KS |
3363 | { |
3364 | HMODULE library; | |
3365 | ||
d07ff9db | 3366 | if (!(library = w32_delayed_load (Qxpm))) |
4ef0d4d0 KS |
3367 | return 0; |
3368 | ||
3369 | LOAD_IMGLIB_FN (library, XpmFreeAttributes); | |
3370 | LOAD_IMGLIB_FN (library, XpmCreateImageFromBuffer); | |
3371 | LOAD_IMGLIB_FN (library, XpmReadFileToImage); | |
3372 | LOAD_IMGLIB_FN (library, XImageFree); | |
3373 | return 1; | |
3374 | } | |
3375 | ||
0fda9b75 | 3376 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 3377 | |
a9e7a9d5 | 3378 | #if defined HAVE_NTGUI && !defined WINDOWSNT |
0fda9b75 DC |
3379 | /* Glue for code below */ |
3380 | #define fn_XpmReadFileToImage XpmReadFileToImage | |
3381 | #define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer | |
3382 | #define fn_XImageFree XImageFree | |
3383 | #define fn_XpmFreeAttributes XpmFreeAttributes | |
2ae3a8a7 | 3384 | #endif /* HAVE_NTGUI && !WINDOWSNT */ |
4ef0d4d0 | 3385 | |
578098f3 | 3386 | /* Value is true if COLOR_SYMBOLS is a valid color symbols list |
4ef0d4d0 KS |
3387 | for XPM images. Such a list must consist of conses whose car and |
3388 | cdr are strings. */ | |
3389 | ||
578098f3 | 3390 | static bool |
d3da34e0 | 3391 | xpm_valid_color_symbols_p (Lisp_Object color_symbols) |
4ef0d4d0 KS |
3392 | { |
3393 | while (CONSP (color_symbols)) | |
3394 | { | |
3395 | Lisp_Object sym = XCAR (color_symbols); | |
3396 | if (!CONSP (sym) | |
3397 | || !STRINGP (XCAR (sym)) | |
3398 | || !STRINGP (XCDR (sym))) | |
3399 | break; | |
3400 | color_symbols = XCDR (color_symbols); | |
3401 | } | |
3402 | ||
3403 | return NILP (color_symbols); | |
3404 | } | |
3405 | ||
3406 | ||
578098f3 | 3407 | /* Value is true if OBJECT is a valid XPM image specification. */ |
4ef0d4d0 | 3408 | |
578098f3 | 3409 | static bool |
d3da34e0 | 3410 | xpm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
3411 | { |
3412 | struct image_keyword fmt[XPM_LAST]; | |
72af86bd | 3413 | memcpy (fmt, xpm_format, sizeof fmt); |
4ef0d4d0 KS |
3414 | return (parse_image_spec (object, fmt, XPM_LAST, Qxpm) |
3415 | /* Either `:file' or `:data' must be present. */ | |
3416 | && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1 | |
3417 | /* Either no `:color-symbols' or it's a list of conses | |
3418 | whose car and cdr are strings. */ | |
3419 | && (fmt[XPM_COLOR_SYMBOLS].count == 0 | |
3420 | || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))); | |
3421 | } | |
3422 | ||
9e2a2647 | 3423 | #endif /* HAVE_XPM || HAVE_NS */ |
4ef0d4d0 | 3424 | |
cd44d2eb | 3425 | #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK |
0766b489 | 3426 | ptrdiff_t |
ef1b0ba7 | 3427 | x_create_bitmap_from_xpm_data (struct frame *f, const char **bits) |
786a43d6 CY |
3428 | { |
3429 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
0766b489 PE |
3430 | ptrdiff_t id; |
3431 | int rc; | |
786a43d6 CY |
3432 | XpmAttributes attrs; |
3433 | Pixmap bitmap, mask; | |
3434 | ||
72af86bd | 3435 | memset (&attrs, 0, sizeof attrs); |
786a43d6 | 3436 | |
54188d8f CY |
3437 | attrs.visual = FRAME_X_VISUAL (f); |
3438 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3439 | attrs.valuemask |= XpmVisual; | |
3440 | attrs.valuemask |= XpmColormap; | |
3441 | ||
786a43d6 | 3442 | rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
f77fabaf | 3443 | (char **) bits, &bitmap, &mask, &attrs); |
786a43d6 | 3444 | if (rc != XpmSuccess) |
fe45ad15 CY |
3445 | { |
3446 | XpmFreeAttributes (&attrs); | |
3447 | return -1; | |
3448 | } | |
786a43d6 CY |
3449 | |
3450 | id = x_allocate_bitmap_record (f); | |
786a43d6 CY |
3451 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; |
3452 | dpyinfo->bitmaps[id - 1].have_mask = 1; | |
3453 | dpyinfo->bitmaps[id - 1].mask = mask; | |
3454 | dpyinfo->bitmaps[id - 1].file = NULL; | |
3455 | dpyinfo->bitmaps[id - 1].height = attrs.height; | |
3456 | dpyinfo->bitmaps[id - 1].width = attrs.width; | |
3457 | dpyinfo->bitmaps[id - 1].depth = attrs.depth; | |
3458 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
3459 | ||
3460 | XpmFreeAttributes (&attrs); | |
3461 | return id; | |
3462 | } | |
0d876a14 | 3463 | #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */ |
786a43d6 | 3464 | |
4ef0d4d0 | 3465 | /* Load image IMG which will be displayed on frame F. Value is |
578098f3 | 3466 | true if successful. */ |
4ef0d4d0 | 3467 | |
ea1aaa6f ST |
3468 | #ifdef HAVE_XPM |
3469 | ||
578098f3 | 3470 | static bool |
d3da34e0 | 3471 | xpm_load (struct frame *f, struct image *img) |
4ef0d4d0 KS |
3472 | { |
3473 | int rc; | |
3474 | XpmAttributes attrs; | |
3475 | Lisp_Object specified_file, color_symbols; | |
3476 | #ifdef HAVE_NTGUI | |
3477 | HDC hdc; | |
3478 | xpm_XImage * xpm_image = NULL, * xpm_mask = NULL; | |
3479 | #endif /* HAVE_NTGUI */ | |
3480 | ||
3481 | /* Configure the XPM lib. Use the visual of frame F. Allocate | |
3482 | close colors. Return colors allocated. */ | |
72af86bd | 3483 | memset (&attrs, 0, sizeof attrs); |
4ef0d4d0 KS |
3484 | |
3485 | #ifndef HAVE_NTGUI | |
3486 | attrs.visual = FRAME_X_VISUAL (f); | |
3487 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3488 | attrs.valuemask |= XpmVisual; | |
3489 | attrs.valuemask |= XpmColormap; | |
3490 | #endif /* HAVE_NTGUI */ | |
3491 | ||
3492 | #ifdef ALLOC_XPM_COLORS | |
3493 | /* Allocate colors with our own functions which handle | |
3494 | failing color allocation more gracefully. */ | |
3495 | attrs.color_closure = f; | |
3496 | attrs.alloc_color = xpm_alloc_color; | |
3497 | attrs.free_colors = xpm_free_colors; | |
3498 | attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure; | |
3499 | #else /* not ALLOC_XPM_COLORS */ | |
3500 | /* Let the XPM lib allocate colors. */ | |
3501 | attrs.valuemask |= XpmReturnAllocPixels; | |
3502 | #ifdef XpmAllocCloseColors | |
3503 | attrs.alloc_close_colors = 1; | |
3504 | attrs.valuemask |= XpmAllocCloseColors; | |
3505 | #else /* not XpmAllocCloseColors */ | |
3506 | attrs.closeness = 600; | |
3507 | attrs.valuemask |= XpmCloseness; | |
3508 | #endif /* not XpmAllocCloseColors */ | |
3509 | #endif /* ALLOC_XPM_COLORS */ | |
3510 | ||
3511 | /* If image specification contains symbolic color definitions, add | |
3512 | these to `attrs'. */ | |
3513 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
3514 | if (CONSP (color_symbols)) | |
3515 | { | |
3516 | Lisp_Object tail; | |
3517 | XpmColorSymbol *xpm_syms; | |
3518 | int i, size; | |
3519 | ||
3520 | attrs.valuemask |= XpmColorSymbols; | |
3521 | ||
3522 | /* Count number of symbols. */ | |
3523 | attrs.numsymbols = 0; | |
3524 | for (tail = color_symbols; CONSP (tail); tail = XCDR (tail)) | |
3525 | ++attrs.numsymbols; | |
3526 | ||
3527 | /* Allocate an XpmColorSymbol array. */ | |
3528 | size = attrs.numsymbols * sizeof *xpm_syms; | |
38182d90 | 3529 | xpm_syms = alloca (size); |
72af86bd | 3530 | memset (xpm_syms, 0, size); |
4ef0d4d0 KS |
3531 | attrs.colorsymbols = xpm_syms; |
3532 | ||
3533 | /* Fill the color symbol array. */ | |
3534 | for (tail = color_symbols, i = 0; | |
3535 | CONSP (tail); | |
3536 | ++i, tail = XCDR (tail)) | |
3537 | { | |
7574650a AS |
3538 | Lisp_Object name; |
3539 | Lisp_Object color; | |
e22cffbc | 3540 | char *empty_string = (char *) ""; |
7574650a AS |
3541 | |
3542 | if (!CONSP (XCAR (tail))) | |
3543 | { | |
e22cffbc PE |
3544 | xpm_syms[i].name = empty_string; |
3545 | xpm_syms[i].value = empty_string; | |
7574650a AS |
3546 | continue; |
3547 | } | |
3548 | name = XCAR (XCAR (tail)); | |
3549 | color = XCDR (XCAR (tail)); | |
3550 | if (STRINGP (name)) | |
3551 | { | |
38182d90 | 3552 | xpm_syms[i].name = alloca (SCHARS (name) + 1); |
42a5b22f | 3553 | strcpy (xpm_syms[i].name, SSDATA (name)); |
7574650a AS |
3554 | } |
3555 | else | |
e22cffbc | 3556 | xpm_syms[i].name = empty_string; |
7574650a AS |
3557 | if (STRINGP (color)) |
3558 | { | |
38182d90 | 3559 | xpm_syms[i].value = alloca (SCHARS (color) + 1); |
42a5b22f | 3560 | strcpy (xpm_syms[i].value, SSDATA (color)); |
7574650a AS |
3561 | } |
3562 | else | |
e22cffbc | 3563 | xpm_syms[i].value = empty_string; |
4ef0d4d0 KS |
3564 | } |
3565 | } | |
3566 | ||
3567 | /* Create a pixmap for the image, either from a file, or from a | |
3568 | string buffer containing data in the same format as an XPM file. */ | |
3569 | #ifdef ALLOC_XPM_COLORS | |
3570 | xpm_init_color_cache (f, &attrs); | |
3571 | #endif | |
3572 | ||
3573 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
3574 | ||
3575 | #ifdef HAVE_NTGUI | |
3576 | { | |
3577 | HDC frame_dc = get_frame_dc (f); | |
3578 | hdc = CreateCompatibleDC (frame_dc); | |
3579 | release_frame_dc (f, frame_dc); | |
3580 | } | |
3581 | #endif /* HAVE_NTGUI */ | |
3582 | ||
3583 | if (STRINGP (specified_file)) | |
3584 | { | |
3585 | Lisp_Object file = x_find_image_file (specified_file); | |
3586 | if (!STRINGP (file)) | |
3587 | { | |
3588 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
7574650a AS |
3589 | #ifdef ALLOC_XPM_COLORS |
3590 | xpm_free_color_cache (); | |
3591 | #endif | |
4ef0d4d0 KS |
3592 | return 0; |
3593 | } | |
3594 | ||
3595 | #ifdef HAVE_NTGUI | |
3596 | /* XpmReadFileToPixmap is not available in the Windows port of | |
3597 | libxpm. But XpmReadFileToImage almost does what we want. */ | |
3598 | rc = fn_XpmReadFileToImage (&hdc, SDATA (file), | |
3599 | &xpm_image, &xpm_mask, | |
3600 | &attrs); | |
3601 | #else | |
547c9269 YM |
3602 | rc = XpmReadFileToImage (FRAME_X_DISPLAY (f), SSDATA (file), |
3603 | &img->ximg, &img->mask_img, | |
3604 | &attrs); | |
4ef0d4d0 KS |
3605 | #endif /* HAVE_NTGUI */ |
3606 | } | |
3607 | else | |
3608 | { | |
3609 | Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
3610 | if (!STRINGP (buffer)) |
3611 | { | |
3612 | image_error ("Invalid image data `%s'", buffer, Qnil); | |
3613 | #ifdef ALLOC_XPM_COLORS | |
3614 | xpm_free_color_cache (); | |
3615 | #endif | |
3616 | return 0; | |
3617 | } | |
4ef0d4d0 KS |
3618 | #ifdef HAVE_NTGUI |
3619 | /* XpmCreatePixmapFromBuffer is not available in the Windows port | |
3620 | of libxpm. But XpmCreateImageFromBuffer almost does what we want. */ | |
3621 | rc = fn_XpmCreateImageFromBuffer (&hdc, SDATA (buffer), | |
3622 | &xpm_image, &xpm_mask, | |
3623 | &attrs); | |
3624 | #else | |
547c9269 YM |
3625 | rc = XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f), SSDATA (buffer), |
3626 | &img->ximg, &img->mask_img, | |
3627 | &attrs); | |
4ef0d4d0 KS |
3628 | #endif /* HAVE_NTGUI */ |
3629 | } | |
3630 | ||
547c9269 YM |
3631 | #ifdef HAVE_X_WINDOWS |
3632 | if (rc == XpmSuccess) | |
3633 | { | |
3634 | img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3635 | img->ximg->width, img->ximg->height, | |
3636 | img->ximg->depth); | |
3637 | if (img->pixmap == NO_PIXMAP) | |
3638 | { | |
3639 | x_clear_image (f, img); | |
3640 | rc = XpmNoMemory; | |
3641 | } | |
3642 | else if (img->mask_img) | |
3643 | { | |
3644 | img->mask = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3645 | img->mask_img->width, | |
3646 | img->mask_img->height, | |
3647 | img->mask_img->depth); | |
3648 | if (img->mask == NO_PIXMAP) | |
3649 | { | |
3650 | x_clear_image (f, img); | |
3651 | rc = XpmNoMemory; | |
3652 | } | |
3653 | } | |
3654 | } | |
3655 | #endif | |
3656 | ||
4ef0d4d0 KS |
3657 | if (rc == XpmSuccess) |
3658 | { | |
3659 | #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS) | |
3660 | img->colors = colors_in_color_table (&img->ncolors); | |
3661 | #else /* not ALLOC_XPM_COLORS */ | |
3662 | int i; | |
3663 | ||
3664 | #ifdef HAVE_NTGUI | |
3665 | /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap, | |
3666 | plus some duplicate attributes. */ | |
3667 | if (xpm_image && xpm_image->bitmap) | |
3668 | { | |
3669 | img->pixmap = xpm_image->bitmap; | |
3670 | /* XImageFree in libXpm frees XImage struct without destroying | |
3671 | the bitmap, which is what we want. */ | |
3672 | fn_XImageFree (xpm_image); | |
3673 | } | |
3674 | if (xpm_mask && xpm_mask->bitmap) | |
3675 | { | |
3676 | /* The mask appears to be inverted compared with what we expect. | |
3677 | TODO: invert our expectations. See other places where we | |
3678 | have to invert bits because our idea of masks is backwards. */ | |
3679 | HGDIOBJ old_obj; | |
3680 | old_obj = SelectObject (hdc, xpm_mask->bitmap); | |
3681 | ||
3682 | PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT); | |
3683 | SelectObject (hdc, old_obj); | |
3684 | ||
3685 | img->mask = xpm_mask->bitmap; | |
3686 | fn_XImageFree (xpm_mask); | |
3687 | DeleteDC (hdc); | |
3688 | } | |
3689 | ||
3690 | DeleteDC (hdc); | |
3691 | #endif /* HAVE_NTGUI */ | |
3692 | ||
3693 | /* Remember allocated colors. */ | |
0065d054 | 3694 | img->colors = xnmalloc (attrs.nalloc_pixels, sizeof *img->colors); |
ddff3151 | 3695 | img->ncolors = attrs.nalloc_pixels; |
4ef0d4d0 KS |
3696 | for (i = 0; i < attrs.nalloc_pixels; ++i) |
3697 | { | |
3698 | img->colors[i] = attrs.alloc_pixels[i]; | |
3699 | #ifdef DEBUG_X_COLORS | |
3700 | register_color (img->colors[i]); | |
3701 | #endif | |
3702 | } | |
3703 | #endif /* not ALLOC_XPM_COLORS */ | |
3704 | ||
3705 | img->width = attrs.width; | |
3706 | img->height = attrs.height; | |
a54e2c05 | 3707 | eassert (img->width > 0 && img->height > 0); |
4ef0d4d0 KS |
3708 | |
3709 | /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */ | |
3710 | #ifdef HAVE_NTGUI | |
3711 | fn_XpmFreeAttributes (&attrs); | |
3712 | #else | |
3713 | XpmFreeAttributes (&attrs); | |
3714 | #endif /* HAVE_NTGUI */ | |
547c9269 YM |
3715 | |
3716 | #ifdef HAVE_X_WINDOWS | |
3717 | /* Maybe fill in the background field while we have ximg handy. */ | |
3718 | IMAGE_BACKGROUND (img, f, img->ximg); | |
3719 | if (img->mask_img) | |
3720 | /* Fill in the background_transparent field while we have the | |
3721 | mask handy. */ | |
3722 | image_background_transparent (img, f, img->mask_img); | |
3723 | #endif | |
4ef0d4d0 KS |
3724 | } |
3725 | else | |
3726 | { | |
3727 | #ifdef HAVE_NTGUI | |
3728 | DeleteDC (hdc); | |
3729 | #endif /* HAVE_NTGUI */ | |
3730 | ||
3731 | switch (rc) | |
3732 | { | |
3733 | case XpmOpenFailed: | |
3734 | image_error ("Error opening XPM file (%s)", img->spec, Qnil); | |
3735 | break; | |
3736 | ||
3737 | case XpmFileInvalid: | |
3738 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
3739 | break; | |
3740 | ||
3741 | case XpmNoMemory: | |
3742 | image_error ("Out of memory (%s)", img->spec, Qnil); | |
3743 | break; | |
3744 | ||
3745 | case XpmColorFailed: | |
3746 | image_error ("Color allocation error (%s)", img->spec, Qnil); | |
3747 | break; | |
3748 | ||
3749 | default: | |
3750 | image_error ("Unknown error (%s)", img->spec, Qnil); | |
3751 | break; | |
3752 | } | |
3753 | } | |
3754 | ||
3755 | #ifdef ALLOC_XPM_COLORS | |
3756 | xpm_free_color_cache (); | |
3757 | #endif | |
3758 | return rc == XpmSuccess; | |
3759 | } | |
3760 | ||
3761 | #endif /* HAVE_XPM */ | |
3762 | ||
9e2a2647 | 3763 | #if defined (HAVE_NS) && !defined (HAVE_XPM) |
ea1aaa6f | 3764 | |
9e2a2647 | 3765 | /* XPM support functions for NS where libxpm is not available. |
ea1aaa6f ST |
3766 | Only XPM version 3 (without any extensions) is supported. */ |
3767 | ||
f57e2426 J |
3768 | static void xpm_put_color_table_v (Lisp_Object, const unsigned char *, |
3769 | int, Lisp_Object); | |
3770 | static Lisp_Object xpm_get_color_table_v (Lisp_Object, | |
3771 | const unsigned char *, int); | |
f57e2426 J |
3772 | static void xpm_put_color_table_h (Lisp_Object, const unsigned char *, |
3773 | int, Lisp_Object); | |
3774 | static Lisp_Object xpm_get_color_table_h (Lisp_Object, | |
3775 | const unsigned char *, int); | |
ea1aaa6f ST |
3776 | |
3777 | /* Tokens returned from xpm_scan. */ | |
3778 | ||
3779 | enum xpm_token | |
3780 | { | |
3781 | XPM_TK_IDENT = 256, | |
3782 | XPM_TK_STRING, | |
3783 | XPM_TK_EOF | |
3784 | }; | |
3785 | ||
3786 | /* Scan an XPM data and return a character (< 256) or a token defined | |
3787 | by enum xpm_token above. *S and END are the start (inclusive) and | |
3788 | the end (exclusive) addresses of the data, respectively. Advance | |
3789 | *S while scanning. If token is either XPM_TK_IDENT or | |
3790 | XPM_TK_STRING, *BEG and *LEN are set to the start address and the | |
3791 | length of the corresponding token, respectively. */ | |
3792 | ||
3793 | static int | |
3d608a86 J |
3794 | xpm_scan (const unsigned char **s, |
3795 | const unsigned char *end, | |
3796 | const unsigned char **beg, | |
74ca2eb3 | 3797 | ptrdiff_t *len) |
ea1aaa6f ST |
3798 | { |
3799 | int c; | |
3800 | ||
3801 | while (*s < end) | |
3802 | { | |
3803 | /* Skip white-space. */ | |
620f13b0 | 3804 | while (*s < end && (c = *(*s)++, c_isspace (c))) |
9ecf6403 | 3805 | ; |
ea1aaa6f ST |
3806 | |
3807 | /* gnus-pointer.xpm uses '-' in its identifier. | |
9ecf6403 | 3808 | sb-dir-plus.xpm uses '+' in its identifier. */ |
620f13b0 | 3809 | if (c_isalpha (c) || c == '_' || c == '-' || c == '+') |
9ecf6403 YM |
3810 | { |
3811 | *beg = *s - 1; | |
2ff0845e | 3812 | while (*s < end |
620f13b0 PE |
3813 | && (c = **s, c_isalnum (c) |
3814 | || c == '_' || c == '-' || c == '+')) | |
9ecf6403 YM |
3815 | ++*s; |
3816 | *len = *s - *beg; | |
3817 | return XPM_TK_IDENT; | |
3818 | } | |
ea1aaa6f | 3819 | else if (c == '"') |
9ecf6403 YM |
3820 | { |
3821 | *beg = *s; | |
3822 | while (*s < end && **s != '"') | |
3823 | ++*s; | |
3824 | *len = *s - *beg; | |
3825 | if (*s < end) | |
3826 | ++*s; | |
3827 | return XPM_TK_STRING; | |
3828 | } | |
ea1aaa6f | 3829 | else if (c == '/') |
9ecf6403 YM |
3830 | { |
3831 | if (*s < end && **s == '*') | |
3832 | { | |
3833 | /* C-style comment. */ | |
3834 | ++*s; | |
3835 | do | |
3836 | { | |
3837 | while (*s < end && *(*s)++ != '*') | |
3838 | ; | |
3839 | } | |
3840 | while (*s < end && **s != '/'); | |
3841 | if (*s < end) | |
3842 | ++*s; | |
3843 | } | |
3844 | else | |
3845 | return c; | |
3846 | } | |
ea1aaa6f | 3847 | else |
9ecf6403 | 3848 | return c; |
ea1aaa6f ST |
3849 | } |
3850 | ||
3851 | return XPM_TK_EOF; | |
3852 | } | |
3853 | ||
bbe6f2aa | 3854 | /* Functions for color table lookup in XPM data. A key is a string |
ea1aaa6f ST |
3855 | specifying the color of each pixel in XPM data. A value is either |
3856 | an integer that specifies a pixel color, Qt that specifies | |
3857 | transparency, or Qnil for the unspecified color. If the length of | |
3858 | the key string is one, a vector is used as a table. Otherwise, a | |
3859 | hash table is used. */ | |
3860 | ||
3861 | static Lisp_Object | |
3d608a86 J |
3862 | xpm_make_color_table_v (void (**put_func) (Lisp_Object, |
3863 | const unsigned char *, | |
3864 | int, | |
3865 | Lisp_Object), | |
3866 | Lisp_Object (**get_func) (Lisp_Object, | |
3867 | const unsigned char *, | |
3868 | int)) | |
ea1aaa6f ST |
3869 | { |
3870 | *put_func = xpm_put_color_table_v; | |
3871 | *get_func = xpm_get_color_table_v; | |
3872 | return Fmake_vector (make_number (256), Qnil); | |
3873 | } | |
3874 | ||
3875 | static void | |
3d608a86 J |
3876 | xpm_put_color_table_v (Lisp_Object color_table, |
3877 | const unsigned char *chars_start, | |
3878 | int chars_len, | |
3879 | Lisp_Object color) | |
ea1aaa6f | 3880 | { |
28be1ada | 3881 | ASET (color_table, *chars_start, color); |
ea1aaa6f ST |
3882 | } |
3883 | ||
3884 | static Lisp_Object | |
3d608a86 J |
3885 | xpm_get_color_table_v (Lisp_Object color_table, |
3886 | const unsigned char *chars_start, | |
3887 | int chars_len) | |
ea1aaa6f | 3888 | { |
28be1ada | 3889 | return AREF (color_table, *chars_start); |
ea1aaa6f ST |
3890 | } |
3891 | ||
3892 | static Lisp_Object | |
3d608a86 J |
3893 | xpm_make_color_table_h (void (**put_func) (Lisp_Object, |
3894 | const unsigned char *, | |
3895 | int, | |
3896 | Lisp_Object), | |
3897 | Lisp_Object (**get_func) (Lisp_Object, | |
3898 | const unsigned char *, | |
3899 | int)) | |
ea1aaa6f ST |
3900 | { |
3901 | *put_func = xpm_put_color_table_h; | |
3902 | *get_func = xpm_get_color_table_h; | |
4ab15c3e | 3903 | return make_hash_table (hashtest_equal, make_number (DEFAULT_HASH_SIZE), |
9ecf6403 YM |
3904 | make_float (DEFAULT_REHASH_SIZE), |
3905 | make_float (DEFAULT_REHASH_THRESHOLD), | |
a23c4171 | 3906 | Qnil); |
ea1aaa6f ST |
3907 | } |
3908 | ||
3909 | static void | |
3d608a86 J |
3910 | xpm_put_color_table_h (Lisp_Object color_table, |
3911 | const unsigned char *chars_start, | |
3912 | int chars_len, | |
3913 | Lisp_Object color) | |
ea1aaa6f ST |
3914 | { |
3915 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
0de4bb68 | 3916 | EMACS_UINT hash_code; |
ea1aaa6f ST |
3917 | Lisp_Object chars = make_unibyte_string (chars_start, chars_len); |
3918 | ||
3919 | hash_lookup (table, chars, &hash_code); | |
3920 | hash_put (table, chars, color, hash_code); | |
3921 | } | |
3922 | ||
3923 | static Lisp_Object | |
3d608a86 J |
3924 | xpm_get_color_table_h (Lisp_Object color_table, |
3925 | const unsigned char *chars_start, | |
3926 | int chars_len) | |
ea1aaa6f ST |
3927 | { |
3928 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
d3411f89 PE |
3929 | ptrdiff_t i = |
3930 | hash_lookup (table, make_unibyte_string (chars_start, chars_len), NULL); | |
ea1aaa6f ST |
3931 | |
3932 | return i >= 0 ? HASH_VALUE (table, i) : Qnil; | |
3933 | } | |
3934 | ||
3935 | enum xpm_color_key { | |
3936 | XPM_COLOR_KEY_S, | |
3937 | XPM_COLOR_KEY_M, | |
3938 | XPM_COLOR_KEY_G4, | |
3939 | XPM_COLOR_KEY_G, | |
3940 | XPM_COLOR_KEY_C | |
3941 | }; | |
3942 | ||
46accb8f | 3943 | static const char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"}; |
ea1aaa6f ST |
3944 | |
3945 | static int | |
3d608a86 | 3946 | xpm_str_to_color_key (const char *s) |
ea1aaa6f ST |
3947 | { |
3948 | int i; | |
3949 | ||
3950 | for (i = 0; | |
3951 | i < sizeof xpm_color_key_strings / sizeof xpm_color_key_strings[0]; | |
3952 | i++) | |
3953 | if (strcmp (xpm_color_key_strings[i], s) == 0) | |
3954 | return i; | |
3955 | return -1; | |
3956 | } | |
3957 | ||
578098f3 | 3958 | static bool |
3d608a86 J |
3959 | xpm_load_image (struct frame *f, |
3960 | struct image *img, | |
3961 | const unsigned char *contents, | |
3962 | const unsigned char *end) | |
ea1aaa6f | 3963 | { |
46accb8f | 3964 | const unsigned char *s = contents, *beg, *str; |
ea1aaa6f ST |
3965 | unsigned char buffer[BUFSIZ]; |
3966 | int width, height, x, y; | |
3967 | int num_colors, chars_per_pixel; | |
74ca2eb3 PE |
3968 | ptrdiff_t len; |
3969 | int LA1; | |
46accb8f YM |
3970 | void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object); |
3971 | Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int); | |
ea1aaa6f | 3972 | Lisp_Object frame, color_symbols, color_table; |
578098f3 PE |
3973 | int best_key; |
3974 | bool have_mask = 0; | |
ea1aaa6f ST |
3975 | XImagePtr ximg = NULL, mask_img = NULL; |
3976 | ||
3977 | #define match() \ | |
3978 | LA1 = xpm_scan (&s, end, &beg, &len) | |
3979 | ||
9ecf6403 YM |
3980 | #define expect(TOKEN) \ |
3981 | if (LA1 != (TOKEN)) \ | |
3982 | goto failure; \ | |
3983 | else \ | |
ea1aaa6f ST |
3984 | match () |
3985 | ||
9ecf6403 | 3986 | #define expect_ident(IDENT) \ |
ea1aaa6f | 3987 | if (LA1 == XPM_TK_IDENT \ |
9ecf6403 YM |
3988 | && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \ |
3989 | match (); \ | |
3990 | else \ | |
ea1aaa6f ST |
3991 | goto failure |
3992 | ||
3993 | if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0)) | |
3994 | goto failure; | |
3995 | s += 9; | |
ed3751c8 | 3996 | match (); |
ea1aaa6f ST |
3997 | expect_ident ("static"); |
3998 | expect_ident ("char"); | |
3999 | expect ('*'); | |
4000 | expect (XPM_TK_IDENT); | |
4001 | expect ('['); | |
4002 | expect (']'); | |
4003 | expect ('='); | |
4004 | expect ('{'); | |
4005 | expect (XPM_TK_STRING); | |
4006 | if (len >= BUFSIZ) | |
4007 | goto failure; | |
4008 | memcpy (buffer, beg, len); | |
4009 | buffer[len] = '\0'; | |
4010 | if (sscanf (buffer, "%d %d %d %d", &width, &height, | |
9ecf6403 | 4011 | &num_colors, &chars_per_pixel) != 4 |
ea1aaa6f ST |
4012 | || width <= 0 || height <= 0 |
4013 | || num_colors <= 0 || chars_per_pixel <= 0) | |
4014 | goto failure; | |
de297941 YM |
4015 | |
4016 | if (!check_image_size (f, width, height)) | |
4017 | { | |
10ea2b82 | 4018 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
de297941 YM |
4019 | goto failure; |
4020 | } | |
4021 | ||
547c9269 | 4022 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0) |
ca4aa935 | 4023 | #ifndef HAVE_NS |
547c9269 YM |
4024 | || !image_create_x_image_and_pixmap (f, img, width, height, 1, |
4025 | &mask_img, 1) | |
ca4aa935 PE |
4026 | #endif |
4027 | ) | |
4028 | { | |
4029 | image_error ("Image too large", Qnil, Qnil); | |
4030 | goto failure; | |
4031 | } | |
4032 | ||
ea1aaa6f ST |
4033 | expect (','); |
4034 | ||
4035 | XSETFRAME (frame, f); | |
4036 | if (!NILP (Fxw_display_color_p (frame))) | |
4037 | best_key = XPM_COLOR_KEY_C; | |
4038 | else if (!NILP (Fx_display_grayscale_p (frame))) | |
4039 | best_key = (XFASTINT (Fx_display_planes (frame)) > 2 | |
9ecf6403 | 4040 | ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4); |
ea1aaa6f ST |
4041 | else |
4042 | best_key = XPM_COLOR_KEY_M; | |
4043 | ||
4044 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
4045 | if (chars_per_pixel == 1) | |
4046 | color_table = xpm_make_color_table_v (&put_color_table, | |
9ecf6403 | 4047 | &get_color_table); |
ea1aaa6f ST |
4048 | else |
4049 | color_table = xpm_make_color_table_h (&put_color_table, | |
9ecf6403 | 4050 | &get_color_table); |
ea1aaa6f ST |
4051 | |
4052 | while (num_colors-- > 0) | |
4053 | { | |
25a48bd0 | 4054 | char *color, *max_color; |
ea1aaa6f ST |
4055 | int key, next_key, max_key = 0; |
4056 | Lisp_Object symbol_color = Qnil, color_val; | |
4057 | XColor cdef; | |
4058 | ||
4059 | expect (XPM_TK_STRING); | |
4060 | if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel) | |
9ecf6403 | 4061 | goto failure; |
ea1aaa6f ST |
4062 | memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel); |
4063 | buffer[len - chars_per_pixel] = '\0'; | |
4064 | ||
4065 | str = strtok (buffer, " \t"); | |
4066 | if (str == NULL) | |
9ecf6403 | 4067 | goto failure; |
ea1aaa6f ST |
4068 | key = xpm_str_to_color_key (str); |
4069 | if (key < 0) | |
9ecf6403 | 4070 | goto failure; |
ea1aaa6f | 4071 | do |
9ecf6403 YM |
4072 | { |
4073 | color = strtok (NULL, " \t"); | |
4074 | if (color == NULL) | |
4075 | goto failure; | |
ea1aaa6f | 4076 | |
9db94f82 | 4077 | while ((str = strtok (NULL, " \t")) != NULL) |
9ecf6403 YM |
4078 | { |
4079 | next_key = xpm_str_to_color_key (str); | |
4080 | if (next_key >= 0) | |
4081 | break; | |
4082 | color[strlen (color)] = ' '; | |
4083 | } | |
ea1aaa6f | 4084 | |
9ecf6403 YM |
4085 | if (key == XPM_COLOR_KEY_S) |
4086 | { | |
4087 | if (NILP (symbol_color)) | |
4088 | symbol_color = build_string (color); | |
4089 | } | |
4090 | else if (max_key < key && key <= best_key) | |
4091 | { | |
4092 | max_key = key; | |
4093 | max_color = color; | |
4094 | } | |
4095 | key = next_key; | |
4096 | } | |
ea1aaa6f ST |
4097 | while (str); |
4098 | ||
4099 | color_val = Qnil; | |
4100 | if (!NILP (color_symbols) && !NILP (symbol_color)) | |
9ecf6403 YM |
4101 | { |
4102 | Lisp_Object specified_color = Fassoc (symbol_color, color_symbols); | |
4103 | ||
4104 | if (CONSP (specified_color) && STRINGP (XCDR (specified_color))) | |
9db94f82 | 4105 | { |
25a48bd0 | 4106 | if (xstrcasecmp (SSDATA (XCDR (specified_color)), "None") == 0) |
9db94f82 | 4107 | color_val = Qt; |
6045c4fd | 4108 | else if (x_defined_color (f, SSDATA (XCDR (specified_color)), |
9db94f82 YM |
4109 | &cdef, 0)) |
4110 | color_val = make_number (cdef.pixel); | |
4111 | } | |
9ecf6403 | 4112 | } |
ea1aaa6f | 4113 | if (NILP (color_val) && max_key > 0) |
9db94f82 | 4114 | { |
05131107 | 4115 | if (xstrcasecmp (max_color, "None") == 0) |
9db94f82 YM |
4116 | color_val = Qt; |
4117 | else if (x_defined_color (f, max_color, &cdef, 0)) | |
4118 | color_val = make_number (cdef.pixel); | |
4119 | } | |
ea1aaa6f | 4120 | if (!NILP (color_val)) |
9ecf6403 | 4121 | (*put_color_table) (color_table, beg, chars_per_pixel, color_val); |
ea1aaa6f ST |
4122 | |
4123 | expect (','); | |
4124 | } | |
4125 | ||
ea1aaa6f ST |
4126 | for (y = 0; y < height; y++) |
4127 | { | |
4128 | expect (XPM_TK_STRING); | |
4129 | str = beg; | |
4130 | if (len < width * chars_per_pixel) | |
9ecf6403 | 4131 | goto failure; |
ea1aaa6f | 4132 | for (x = 0; x < width; x++, str += chars_per_pixel) |
9ecf6403 YM |
4133 | { |
4134 | Lisp_Object color_val = | |
4135 | (*get_color_table) (color_table, str, chars_per_pixel); | |
4136 | ||
4137 | XPutPixel (ximg, x, y, | |
4138 | (INTEGERP (color_val) ? XINT (color_val) | |
4139 | : FRAME_FOREGROUND_PIXEL (f))); | |
edfda783 | 4140 | #ifndef HAVE_NS |
9ecf6403 YM |
4141 | XPutPixel (mask_img, x, y, |
4142 | (!EQ (color_val, Qt) ? PIX_MASK_DRAW | |
4143 | : (have_mask = 1, PIX_MASK_RETAIN))); | |
edfda783 | 4144 | #else |
ed3751c8 JB |
4145 | if (EQ (color_val, Qt)) |
4146 | ns_set_alpha (ximg, x, y, 0); | |
edfda783 | 4147 | #endif |
9ecf6403 | 4148 | } |
ea1aaa6f | 4149 | if (y + 1 < height) |
9ecf6403 | 4150 | expect (','); |
ea1aaa6f ST |
4151 | } |
4152 | ||
4153 | img->width = width; | |
4154 | img->height = height; | |
4155 | ||
0b8ac842 YM |
4156 | /* Maybe fill in the background field while we have ximg handy. */ |
4157 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
4158 | IMAGE_BACKGROUND (img, f, ximg); | |
4159 | ||
547c9269 | 4160 | image_put_x_image (f, img, ximg, 0); |
edfda783 | 4161 | #ifndef HAVE_NS |
ea1aaa6f ST |
4162 | if (have_mask) |
4163 | { | |
9ecf6403 YM |
4164 | /* Fill in the background_transparent field while we have the |
4165 | mask handy. */ | |
4166 | image_background_transparent (img, f, mask_img); | |
4167 | ||
547c9269 | 4168 | image_put_x_image (f, img, mask_img, 1); |
ea1aaa6f ST |
4169 | } |
4170 | else | |
4171 | { | |
4172 | x_destroy_x_image (mask_img); | |
547c9269 | 4173 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
ea1aaa6f | 4174 | } |
edfda783 | 4175 | #endif |
ea1aaa6f ST |
4176 | return 1; |
4177 | ||
4178 | failure: | |
4179 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
ea1aaa6f ST |
4180 | x_destroy_x_image (ximg); |
4181 | x_destroy_x_image (mask_img); | |
4182 | x_clear_image (f, img); | |
4183 | return 0; | |
4184 | ||
4185 | #undef match | |
4186 | #undef expect | |
4187 | #undef expect_ident | |
4188 | } | |
4189 | ||
578098f3 | 4190 | static bool |
3d608a86 J |
4191 | xpm_load (struct frame *f, |
4192 | struct image *img) | |
ea1aaa6f | 4193 | { |
578098f3 | 4194 | bool success_p = 0; |
ea1aaa6f ST |
4195 | Lisp_Object file_name; |
4196 | ||
4197 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
4198 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
4199 | if (STRINGP (file_name)) | |
4200 | { | |
4201 | Lisp_Object file; | |
4202 | unsigned char *contents; | |
dd52fcea | 4203 | ptrdiff_t size; |
ea1aaa6f ST |
4204 | |
4205 | file = x_find_image_file (file_name); | |
ea1aaa6f | 4206 | if (!STRINGP (file)) |
9ecf6403 YM |
4207 | { |
4208 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
9ecf6403 YM |
4209 | return 0; |
4210 | } | |
ea1aaa6f | 4211 | |
6045c4fd | 4212 | contents = slurp_file (SSDATA (file), &size); |
ea1aaa6f | 4213 | if (contents == NULL) |
9ecf6403 YM |
4214 | { |
4215 | image_error ("Error loading XPM image `%s'", img->spec, Qnil); | |
9ecf6403 YM |
4216 | return 0; |
4217 | } | |
ea1aaa6f ST |
4218 | |
4219 | success_p = xpm_load_image (f, img, contents, contents + size); | |
4220 | xfree (contents); | |
ea1aaa6f ST |
4221 | } |
4222 | else | |
4223 | { | |
4224 | Lisp_Object data; | |
4225 | ||
4226 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
4227 | if (!STRINGP (data)) |
4228 | { | |
4229 | image_error ("Invalid image data `%s'", data, Qnil); | |
4230 | return 0; | |
4231 | } | |
ea1aaa6f | 4232 | success_p = xpm_load_image (f, img, SDATA (data), |
9ecf6403 | 4233 | SDATA (data) + SBYTES (data)); |
ea1aaa6f ST |
4234 | } |
4235 | ||
4236 | return success_p; | |
4237 | } | |
4238 | ||
9e2a2647 | 4239 | #endif /* HAVE_NS && !HAVE_XPM */ |
b0da69a7 | 4240 | |
ea1aaa6f | 4241 | |
4ef0d4d0 KS |
4242 | \f |
4243 | /*********************************************************************** | |
4244 | Color table | |
4245 | ***********************************************************************/ | |
4246 | ||
4247 | #ifdef COLOR_TABLE_SUPPORT | |
4248 | ||
4249 | /* An entry in the color table mapping an RGB color to a pixel color. */ | |
4250 | ||
4251 | struct ct_color | |
4252 | { | |
4253 | int r, g, b; | |
4254 | unsigned long pixel; | |
4255 | ||
4256 | /* Next in color table collision list. */ | |
4257 | struct ct_color *next; | |
4258 | }; | |
4259 | ||
4260 | /* The bucket vector size to use. Must be prime. */ | |
4261 | ||
4262 | #define CT_SIZE 101 | |
4263 | ||
4264 | /* Value is a hash of the RGB color given by R, G, and B. */ | |
4265 | ||
4266 | #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B)) | |
4267 | ||
4268 | /* The color hash table. */ | |
4269 | ||
cd44d2eb | 4270 | static struct ct_color **ct_table; |
4ef0d4d0 KS |
4271 | |
4272 | /* Number of entries in the color table. */ | |
4273 | ||
cd44d2eb | 4274 | static int ct_colors_allocated; |
ddff3151 PE |
4275 | enum |
4276 | { | |
4277 | ct_colors_allocated_max = | |
4278 | min (INT_MAX, | |
4279 | min (PTRDIFF_MAX, SIZE_MAX) / sizeof (unsigned long)) | |
4280 | }; | |
4ef0d4d0 KS |
4281 | |
4282 | /* Initialize the color table. */ | |
4283 | ||
4284 | static void | |
d3da34e0 | 4285 | init_color_table (void) |
4ef0d4d0 KS |
4286 | { |
4287 | int size = CT_SIZE * sizeof (*ct_table); | |
23f86fce | 4288 | ct_table = xzalloc (size); |
4ef0d4d0 KS |
4289 | ct_colors_allocated = 0; |
4290 | } | |
4291 | ||
4292 | ||
4293 | /* Free memory associated with the color table. */ | |
4294 | ||
4295 | static void | |
d3da34e0 | 4296 | free_color_table (void) |
4ef0d4d0 KS |
4297 | { |
4298 | int i; | |
4299 | struct ct_color *p, *next; | |
4300 | ||
4301 | for (i = 0; i < CT_SIZE; ++i) | |
4302 | for (p = ct_table[i]; p; p = next) | |
4303 | { | |
4304 | next = p->next; | |
4305 | xfree (p); | |
4306 | } | |
4307 | ||
4308 | xfree (ct_table); | |
4309 | ct_table = NULL; | |
4310 | } | |
4311 | ||
4312 | ||
4313 | /* Value is a pixel color for RGB color R, G, B on frame F. If an | |
4314 | entry for that color already is in the color table, return the | |
4315 | pixel color of that entry. Otherwise, allocate a new color for R, | |
4316 | G, B, and make an entry in the color table. */ | |
4317 | ||
4318 | static unsigned long | |
d3da34e0 | 4319 | lookup_rgb_color (struct frame *f, int r, int g, int b) |
4ef0d4d0 KS |
4320 | { |
4321 | unsigned hash = CT_HASH_RGB (r, g, b); | |
4322 | int i = hash % CT_SIZE; | |
4323 | struct ct_color *p; | |
4324 | Display_Info *dpyinfo; | |
4325 | ||
4326 | /* Handle TrueColor visuals specially, which improves performance by | |
4327 | two orders of magnitude. Freeing colors on TrueColor visuals is | |
4328 | a nop, and pixel colors specify RGB values directly. See also | |
4329 | the Xlib spec, chapter 3.1. */ | |
4330 | dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
4331 | if (dpyinfo->red_bits > 0) | |
4332 | { | |
4333 | unsigned long pr, pg, pb; | |
4334 | ||
4335 | /* Apply gamma-correction like normal color allocation does. */ | |
4336 | if (f->gamma) | |
4337 | { | |
4338 | XColor color; | |
4339 | color.red = r, color.green = g, color.blue = b; | |
4340 | gamma_correct (f, &color); | |
4341 | r = color.red, g = color.green, b = color.blue; | |
4342 | } | |
4343 | ||
4344 | /* Scale down RGB values to the visual's bits per RGB, and shift | |
4345 | them to the right position in the pixel color. Note that the | |
4346 | original RGB values are 16-bit values, as usual in X. */ | |
4347 | pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset; | |
4348 | pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset; | |
4349 | pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset; | |
4350 | ||
4351 | /* Assemble the pixel color. */ | |
4352 | return pr | pg | pb; | |
4353 | } | |
1f026899 | 4354 | |
4ef0d4d0 KS |
4355 | for (p = ct_table[i]; p; p = p->next) |
4356 | if (p->r == r && p->g == g && p->b == b) | |
4357 | break; | |
4358 | ||
4359 | if (p == NULL) | |
4360 | { | |
4361 | ||
4362 | #ifdef HAVE_X_WINDOWS | |
4363 | XColor color; | |
4364 | Colormap cmap; | |
578098f3 | 4365 | bool rc; |
ddff3151 PE |
4366 | #else |
4367 | COLORREF color; | |
4368 | #endif | |
4ef0d4d0 | 4369 | |
ddff3151 PE |
4370 | if (ct_colors_allocated_max <= ct_colors_allocated) |
4371 | return FRAME_FOREGROUND_PIXEL (f); | |
4372 | ||
4373 | #ifdef HAVE_X_WINDOWS | |
4ef0d4d0 KS |
4374 | color.red = r; |
4375 | color.green = g; | |
4376 | color.blue = b; | |
4377 | ||
4378 | cmap = FRAME_X_COLORMAP (f); | |
4379 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4380 | if (rc) | |
4381 | { | |
4382 | ++ct_colors_allocated; | |
23f86fce | 4383 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4384 | p->r = r; |
4385 | p->g = g; | |
4386 | p->b = b; | |
4387 | p->pixel = color.pixel; | |
4388 | p->next = ct_table[i]; | |
4389 | ct_table[i] = p; | |
4390 | } | |
4391 | else | |
4392 | return FRAME_FOREGROUND_PIXEL (f); | |
4393 | ||
4394 | #else | |
4ef0d4d0 KS |
4395 | #ifdef HAVE_NTGUI |
4396 | color = PALETTERGB (r, g, b); | |
4397 | #else | |
4398 | color = RGB_TO_ULONG (r, g, b); | |
4399 | #endif /* HAVE_NTGUI */ | |
4400 | ++ct_colors_allocated; | |
23f86fce | 4401 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4402 | p->r = r; |
4403 | p->g = g; | |
4404 | p->b = b; | |
4405 | p->pixel = color; | |
4406 | p->next = ct_table[i]; | |
4407 | ct_table[i] = p; | |
4408 | #endif /* HAVE_X_WINDOWS */ | |
4409 | ||
4410 | } | |
4411 | ||
4412 | return p->pixel; | |
4413 | } | |
4414 | ||
4415 | ||
4416 | /* Look up pixel color PIXEL which is used on frame F in the color | |
4417 | table. If not already present, allocate it. Value is PIXEL. */ | |
4418 | ||
4419 | static unsigned long | |
d3da34e0 | 4420 | lookup_pixel_color (struct frame *f, unsigned long pixel) |
4ef0d4d0 KS |
4421 | { |
4422 | int i = pixel % CT_SIZE; | |
4423 | struct ct_color *p; | |
4424 | ||
4425 | for (p = ct_table[i]; p; p = p->next) | |
4426 | if (p->pixel == pixel) | |
4427 | break; | |
4428 | ||
4429 | if (p == NULL) | |
4430 | { | |
4431 | XColor color; | |
4432 | Colormap cmap; | |
578098f3 | 4433 | bool rc; |
4ef0d4d0 | 4434 | |
ddff3151 PE |
4435 | if (ct_colors_allocated_max <= ct_colors_allocated) |
4436 | return FRAME_FOREGROUND_PIXEL (f); | |
4437 | ||
4ef0d4d0 KS |
4438 | #ifdef HAVE_X_WINDOWS |
4439 | cmap = FRAME_X_COLORMAP (f); | |
4440 | color.pixel = pixel; | |
4441 | x_query_color (f, &color); | |
4442 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4443 | #else | |
4d7e6e51 | 4444 | block_input (); |
4ef0d4d0 KS |
4445 | cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f)); |
4446 | color.pixel = pixel; | |
4447 | XQueryColor (NULL, cmap, &color); | |
4448 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4d7e6e51 | 4449 | unblock_input (); |
4ef0d4d0 KS |
4450 | #endif /* HAVE_X_WINDOWS */ |
4451 | ||
4452 | if (rc) | |
4453 | { | |
4454 | ++ct_colors_allocated; | |
4455 | ||
23f86fce | 4456 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4457 | p->r = color.red; |
4458 | p->g = color.green; | |
4459 | p->b = color.blue; | |
4460 | p->pixel = pixel; | |
4461 | p->next = ct_table[i]; | |
4462 | ct_table[i] = p; | |
4463 | } | |
4464 | else | |
4465 | return FRAME_FOREGROUND_PIXEL (f); | |
4466 | } | |
4467 | return p->pixel; | |
4468 | } | |
4469 | ||
4470 | ||
4471 | /* Value is a vector of all pixel colors contained in the color table, | |
4472 | allocated via xmalloc. Set *N to the number of colors. */ | |
4473 | ||
4474 | static unsigned long * | |
d3da34e0 | 4475 | colors_in_color_table (int *n) |
4ef0d4d0 KS |
4476 | { |
4477 | int i, j; | |
4478 | struct ct_color *p; | |
4479 | unsigned long *colors; | |
4480 | ||
4481 | if (ct_colors_allocated == 0) | |
4482 | { | |
4483 | *n = 0; | |
4484 | colors = NULL; | |
4485 | } | |
4486 | else | |
4487 | { | |
23f86fce | 4488 | colors = xmalloc (ct_colors_allocated * sizeof *colors); |
4ef0d4d0 KS |
4489 | *n = ct_colors_allocated; |
4490 | ||
4491 | for (i = j = 0; i < CT_SIZE; ++i) | |
4492 | for (p = ct_table[i]; p; p = p->next) | |
4493 | colors[j++] = p->pixel; | |
4494 | } | |
4495 | ||
4496 | return colors; | |
4497 | } | |
4498 | ||
4499 | #else /* COLOR_TABLE_SUPPORT */ | |
4500 | ||
4501 | static unsigned long | |
7c3320d8 | 4502 | lookup_rgb_color (struct frame *f, int r, int g, int b) |
4ef0d4d0 KS |
4503 | { |
4504 | unsigned long pixel; | |
4505 | ||
4ef0d4d0 KS |
4506 | #ifdef HAVE_NTGUI |
4507 | pixel = PALETTERGB (r >> 8, g >> 8, b >> 8); | |
4508 | #endif /* HAVE_NTGUI */ | |
4509 | ||
edfda783 AR |
4510 | #ifdef HAVE_NS |
4511 | pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8); | |
4512 | #endif /* HAVE_NS */ | |
4ef0d4d0 KS |
4513 | return pixel; |
4514 | } | |
4515 | ||
4516 | static void | |
7c3320d8 | 4517 | init_color_table (void) |
4ef0d4d0 KS |
4518 | { |
4519 | } | |
4520 | #endif /* COLOR_TABLE_SUPPORT */ | |
4521 | ||
4522 | \f | |
4523 | /*********************************************************************** | |
4524 | Algorithms | |
4525 | ***********************************************************************/ | |
4526 | ||
4ef0d4d0 KS |
4527 | /* Edge detection matrices for different edge-detection |
4528 | strategies. */ | |
4529 | ||
4530 | static int emboss_matrix[9] = { | |
4531 | /* x - 1 x x + 1 */ | |
4532 | 2, -1, 0, /* y - 1 */ | |
4533 | -1, 0, 1, /* y */ | |
4534 | 0, 1, -2 /* y + 1 */ | |
4535 | }; | |
4536 | ||
4537 | static int laplace_matrix[9] = { | |
4538 | /* x - 1 x x + 1 */ | |
4539 | 1, 0, 0, /* y - 1 */ | |
4540 | 0, 0, 0, /* y */ | |
4541 | 0, 0, -1 /* y + 1 */ | |
4542 | }; | |
4543 | ||
4544 | /* Value is the intensity of the color whose red/green/blue values | |
4545 | are R, G, and B. */ | |
4546 | ||
4547 | #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6) | |
4548 | ||
4549 | ||
4550 | /* On frame F, return an array of XColor structures describing image | |
4551 | IMG->pixmap. Each XColor structure has its pixel color set. RGB_P | |
578098f3 | 4552 | means also fill the red/green/blue members of the XColor |
4ef0d4d0 KS |
4553 | structures. Value is a pointer to the array of XColors structures, |
4554 | allocated with xmalloc; it must be freed by the caller. */ | |
4555 | ||
4556 | static XColor * | |
578098f3 | 4557 | x_to_xcolors (struct frame *f, struct image *img, bool rgb_p) |
4ef0d4d0 KS |
4558 | { |
4559 | int x, y; | |
4560 | XColor *colors, *p; | |
4561 | XImagePtr_or_DC ximg; | |
4562 | #ifdef HAVE_NTGUI | |
4ef0d4d0 KS |
4563 | HGDIOBJ prev; |
4564 | #endif /* HAVE_NTGUI */ | |
4565 | ||
ddff3151 PE |
4566 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width < img->height) |
4567 | memory_full (SIZE_MAX); | |
23f86fce | 4568 | colors = xmalloc (sizeof *colors * img->width * img->height); |
4ef0d4d0 | 4569 | |
547c9269 YM |
4570 | /* Get the X image or create a memory device context for IMG. */ |
4571 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); | |
4ef0d4d0 KS |
4572 | |
4573 | /* Fill the `pixel' members of the XColor array. I wished there | |
4574 | were an easy and portable way to circumvent XGetPixel. */ | |
4575 | p = colors; | |
4576 | for (y = 0; y < img->height; ++y) | |
4577 | { | |
8b7d0a16 | 4578 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
6045c4fd | 4579 | XColor *row = p; |
4ef0d4d0 | 4580 | for (x = 0; x < img->width; ++x, ++p) |
8b7d0a16 | 4581 | p->pixel = GET_PIXEL (ximg, x, y); |
4ef0d4d0 KS |
4582 | if (rgb_p) |
4583 | x_query_colors (f, row, img->width); | |
4584 | ||
4585 | #else | |
4586 | ||
4587 | for (x = 0; x < img->width; ++x, ++p) | |
4588 | { | |
4589 | /* W32_TODO: palette support needed here? */ | |
4590 | p->pixel = GET_PIXEL (ximg, x, y); | |
4591 | if (rgb_p) | |
4592 | { | |
4ef0d4d0 KS |
4593 | p->red = RED16_FROM_ULONG (p->pixel); |
4594 | p->green = GREEN16_FROM_ULONG (p->pixel); | |
4595 | p->blue = BLUE16_FROM_ULONG (p->pixel); | |
4ef0d4d0 KS |
4596 | } |
4597 | } | |
4598 | #endif /* HAVE_X_WINDOWS */ | |
4599 | } | |
4600 | ||
547c9269 | 4601 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
4ef0d4d0 KS |
4602 | |
4603 | return colors; | |
4604 | } | |
4605 | ||
4606 | #ifdef HAVE_NTGUI | |
4607 | ||
4608 | /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been | |
4609 | created with CreateDIBSection, with the pointer to the bit values | |
4610 | stored in ximg->data. */ | |
4611 | ||
3046a84b | 4612 | static void |
7c3320d8 | 4613 | XPutPixel (XImagePtr ximg, int x, int y, COLORREF color) |
4ef0d4d0 KS |
4614 | { |
4615 | int width = ximg->info.bmiHeader.biWidth; | |
4ef0d4d0 KS |
4616 | unsigned char * pixel; |
4617 | ||
4618 | /* True color images. */ | |
4619 | if (ximg->info.bmiHeader.biBitCount == 24) | |
4620 | { | |
4621 | int rowbytes = width * 3; | |
4622 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4623 | if (rowbytes % 4) | |
4624 | rowbytes += 4 - (rowbytes % 4); | |
4625 | ||
4626 | pixel = ximg->data + y * rowbytes + x * 3; | |
4627 | /* Windows bitmaps are in BGR order. */ | |
4628 | *pixel = GetBValue (color); | |
4629 | *(pixel + 1) = GetGValue (color); | |
4630 | *(pixel + 2) = GetRValue (color); | |
4631 | } | |
4632 | /* Monochrome images. */ | |
4633 | else if (ximg->info.bmiHeader.biBitCount == 1) | |
4634 | { | |
4635 | int rowbytes = width / 8; | |
4636 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4637 | if (rowbytes % 4) | |
4638 | rowbytes += 4 - (rowbytes % 4); | |
4639 | pixel = ximg->data + y * rowbytes + x / 8; | |
4640 | /* Filter out palette info. */ | |
4641 | if (color & 0x00ffffff) | |
4642 | *pixel = *pixel | (1 << x % 8); | |
4643 | else | |
4644 | *pixel = *pixel & ~(1 << x % 8); | |
4645 | } | |
4646 | else | |
4647 | image_error ("XPutPixel: palette image not supported", Qnil, Qnil); | |
4648 | } | |
4649 | ||
4650 | #endif /* HAVE_NTGUI */ | |
4651 | ||
4652 | /* Create IMG->pixmap from an array COLORS of XColor structures, whose | |
4653 | RGB members are set. F is the frame on which this all happens. | |
4654 | COLORS will be freed; an existing IMG->pixmap will be freed, too. */ | |
4655 | ||
4656 | static void | |
d3da34e0 | 4657 | x_from_xcolors (struct frame *f, struct image *img, XColor *colors) |
4ef0d4d0 KS |
4658 | { |
4659 | int x, y; | |
edfda783 | 4660 | XImagePtr oimg = NULL; |
4ef0d4d0 KS |
4661 | XColor *p; |
4662 | ||
4663 | init_color_table (); | |
4664 | ||
547c9269 YM |
4665 | x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_COLORS); |
4666 | image_create_x_image_and_pixmap (f, img, img->width, img->height, 0, | |
4667 | &oimg, 0); | |
4ef0d4d0 KS |
4668 | p = colors; |
4669 | for (y = 0; y < img->height; ++y) | |
4670 | for (x = 0; x < img->width; ++x, ++p) | |
4671 | { | |
4672 | unsigned long pixel; | |
4673 | pixel = lookup_rgb_color (f, p->red, p->green, p->blue); | |
4674 | XPutPixel (oimg, x, y, pixel); | |
4675 | } | |
4676 | ||
4677 | xfree (colors); | |
4ef0d4d0 | 4678 | |
547c9269 | 4679 | image_put_x_image (f, img, oimg, 0); |
4ef0d4d0 KS |
4680 | #ifdef COLOR_TABLE_SUPPORT |
4681 | img->colors = colors_in_color_table (&img->ncolors); | |
4682 | free_color_table (); | |
4683 | #endif /* COLOR_TABLE_SUPPORT */ | |
4684 | } | |
4685 | ||
4686 | ||
4687 | /* On frame F, perform edge-detection on image IMG. | |
4688 | ||
4689 | MATRIX is a nine-element array specifying the transformation | |
4690 | matrix. See emboss_matrix for an example. | |
4691 | ||
4692 | COLOR_ADJUST is a color adjustment added to each pixel of the | |
4693 | outgoing image. */ | |
4694 | ||
4695 | static void | |
d3da34e0 | 4696 | x_detect_edges (struct frame *f, struct image *img, int *matrix, int color_adjust) |
4ef0d4d0 KS |
4697 | { |
4698 | XColor *colors = x_to_xcolors (f, img, 1); | |
4699 | XColor *new, *p; | |
4700 | int x, y, i, sum; | |
4701 | ||
4702 | for (i = sum = 0; i < 9; ++i) | |
1ea40aa2 | 4703 | sum += eabs (matrix[i]); |
4ef0d4d0 KS |
4704 | |
4705 | #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X)) | |
4706 | ||
ddff3151 PE |
4707 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width < img->height) |
4708 | memory_full (SIZE_MAX); | |
23f86fce | 4709 | new = xmalloc (sizeof *new * img->width * img->height); |
4ef0d4d0 KS |
4710 | |
4711 | for (y = 0; y < img->height; ++y) | |
4712 | { | |
4713 | p = COLOR (new, 0, y); | |
4714 | p->red = p->green = p->blue = 0xffff/2; | |
4715 | p = COLOR (new, img->width - 1, y); | |
4716 | p->red = p->green = p->blue = 0xffff/2; | |
4717 | } | |
4718 | ||
4719 | for (x = 1; x < img->width - 1; ++x) | |
4720 | { | |
4721 | p = COLOR (new, x, 0); | |
4722 | p->red = p->green = p->blue = 0xffff/2; | |
4723 | p = COLOR (new, x, img->height - 1); | |
4724 | p->red = p->green = p->blue = 0xffff/2; | |
4725 | } | |
4726 | ||
4727 | for (y = 1; y < img->height - 1; ++y) | |
4728 | { | |
4729 | p = COLOR (new, 1, y); | |
4730 | ||
4731 | for (x = 1; x < img->width - 1; ++x, ++p) | |
4732 | { | |
6ae141d6 | 4733 | int r, g, b, yy, xx; |
4ef0d4d0 KS |
4734 | |
4735 | r = g = b = i = 0; | |
6ae141d6 PE |
4736 | for (yy = y - 1; yy < y + 2; ++yy) |
4737 | for (xx = x - 1; xx < x + 2; ++xx, ++i) | |
4ef0d4d0 KS |
4738 | if (matrix[i]) |
4739 | { | |
6ae141d6 | 4740 | XColor *t = COLOR (colors, xx, yy); |
4ef0d4d0 KS |
4741 | r += matrix[i] * t->red; |
4742 | g += matrix[i] * t->green; | |
4743 | b += matrix[i] * t->blue; | |
4744 | } | |
4745 | ||
4746 | r = (r / sum + color_adjust) & 0xffff; | |
4747 | g = (g / sum + color_adjust) & 0xffff; | |
4748 | b = (b / sum + color_adjust) & 0xffff; | |
4749 | p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b); | |
4750 | } | |
4751 | } | |
4752 | ||
4753 | xfree (colors); | |
4754 | x_from_xcolors (f, img, new); | |
4755 | ||
4756 | #undef COLOR | |
4757 | } | |
4758 | ||
4759 | ||
4760 | /* Perform the pre-defined `emboss' edge-detection on image IMG | |
4761 | on frame F. */ | |
4762 | ||
4763 | static void | |
d3da34e0 | 4764 | x_emboss (struct frame *f, struct image *img) |
4ef0d4d0 KS |
4765 | { |
4766 | x_detect_edges (f, img, emboss_matrix, 0xffff / 2); | |
4767 | } | |
4768 | ||
4769 | ||
4770 | /* Transform image IMG which is used on frame F with a Laplace | |
4771 | edge-detection algorithm. The result is an image that can be used | |
4772 | to draw disabled buttons, for example. */ | |
4773 | ||
4774 | static void | |
d3da34e0 | 4775 | x_laplace (struct frame *f, struct image *img) |
4ef0d4d0 KS |
4776 | { |
4777 | x_detect_edges (f, img, laplace_matrix, 45000); | |
4778 | } | |
4779 | ||
4780 | ||
4781 | /* Perform edge-detection on image IMG on frame F, with specified | |
4782 | transformation matrix MATRIX and color-adjustment COLOR_ADJUST. | |
4783 | ||
4784 | MATRIX must be either | |
4785 | ||
4786 | - a list of at least 9 numbers in row-major form | |
4787 | - a vector of at least 9 numbers | |
4788 | ||
4789 | COLOR_ADJUST nil means use a default; otherwise it must be a | |
4790 | number. */ | |
4791 | ||
4792 | static void | |
d3da34e0 JB |
4793 | x_edge_detection (struct frame *f, struct image *img, Lisp_Object matrix, |
4794 | Lisp_Object color_adjust) | |
4ef0d4d0 KS |
4795 | { |
4796 | int i = 0; | |
4797 | int trans[9]; | |
4798 | ||
4799 | if (CONSP (matrix)) | |
4800 | { | |
4801 | for (i = 0; | |
4802 | i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix)); | |
4803 | ++i, matrix = XCDR (matrix)) | |
4804 | trans[i] = XFLOATINT (XCAR (matrix)); | |
4805 | } | |
4806 | else if (VECTORP (matrix) && ASIZE (matrix) >= 9) | |
4807 | { | |
4808 | for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i) | |
4809 | trans[i] = XFLOATINT (AREF (matrix, i)); | |
4810 | } | |
4811 | ||
4812 | if (NILP (color_adjust)) | |
4813 | color_adjust = make_number (0xffff / 2); | |
4814 | ||
4815 | if (i == 9 && NUMBERP (color_adjust)) | |
77a765fd | 4816 | x_detect_edges (f, img, trans, XFLOATINT (color_adjust)); |
4ef0d4d0 KS |
4817 | } |
4818 | ||
4819 | ||
4820 | /* Transform image IMG on frame F so that it looks disabled. */ | |
4821 | ||
4822 | static void | |
d3da34e0 | 4823 | x_disable_image (struct frame *f, struct image *img) |
4ef0d4d0 KS |
4824 | { |
4825 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
4826 | #ifdef HAVE_NTGUI | |
4827 | int n_planes = dpyinfo->n_planes * dpyinfo->n_cbits; | |
4828 | #else | |
4829 | int n_planes = dpyinfo->n_planes; | |
4830 | #endif /* HAVE_NTGUI */ | |
4831 | ||
4832 | if (n_planes >= 2) | |
4833 | { | |
4834 | /* Color (or grayscale). Convert to gray, and equalize. Just | |
4835 | drawing such images with a stipple can look very odd, so | |
4836 | we're using this method instead. */ | |
4837 | XColor *colors = x_to_xcolors (f, img, 1); | |
4838 | XColor *p, *end; | |
4839 | const int h = 15000; | |
4840 | const int l = 30000; | |
4841 | ||
4842 | for (p = colors, end = colors + img->width * img->height; | |
4843 | p < end; | |
4844 | ++p) | |
4845 | { | |
4846 | int i = COLOR_INTENSITY (p->red, p->green, p->blue); | |
4847 | int i2 = (0xffff - h - l) * i / 0xffff + l; | |
4848 | p->red = p->green = p->blue = i2; | |
4849 | } | |
4850 | ||
4851 | x_from_xcolors (f, img, colors); | |
4852 | } | |
4853 | ||
4854 | /* Draw a cross over the disabled image, if we must or if we | |
4855 | should. */ | |
4856 | if (n_planes < 2 || cross_disabled_images) | |
4857 | { | |
4858 | #ifndef HAVE_NTGUI | |
5afa3a81 | 4859 | #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */ |
edfda783 | 4860 | |
4ef0d4d0 | 4861 | #define MaskForeground(f) WHITE_PIX_DEFAULT (f) |
4ef0d4d0 | 4862 | |
6045c4fd | 4863 | Display *dpy = FRAME_X_DISPLAY (f); |
547c9269 YM |
4864 | GC gc; |
4865 | ||
4866 | image_sync_to_pixmaps (f, img); | |
4867 | gc = XCreateGC (dpy, img->pixmap, 0, NULL); | |
4ef0d4d0 KS |
4868 | XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f)); |
4869 | XDrawLine (dpy, img->pixmap, gc, 0, 0, | |
4870 | img->width - 1, img->height - 1); | |
4871 | XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1, | |
4872 | img->width - 1, 0); | |
4873 | XFreeGC (dpy, gc); | |
4874 | ||
4875 | if (img->mask) | |
4876 | { | |
ae8279db | 4877 | gc = XCreateGC (dpy, img->mask, 0, NULL); |
4ef0d4d0 KS |
4878 | XSetForeground (dpy, gc, MaskForeground (f)); |
4879 | XDrawLine (dpy, img->mask, gc, 0, 0, | |
4880 | img->width - 1, img->height - 1); | |
4881 | XDrawLine (dpy, img->mask, gc, 0, img->height - 1, | |
4882 | img->width - 1, 0); | |
4883 | XFreeGC (dpy, gc); | |
4884 | } | |
edfda783 | 4885 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4886 | #else |
4887 | HDC hdc, bmpdc; | |
4888 | HGDIOBJ prev; | |
4889 | ||
4890 | hdc = get_frame_dc (f); | |
4891 | bmpdc = CreateCompatibleDC (hdc); | |
4892 | release_frame_dc (f, hdc); | |
4893 | ||
4894 | prev = SelectObject (bmpdc, img->pixmap); | |
4895 | ||
4896 | SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f)); | |
4897 | MoveToEx (bmpdc, 0, 0, NULL); | |
4898 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4899 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4900 | LineTo (bmpdc, img->width - 1, 0); | |
4901 | ||
4902 | if (img->mask) | |
4903 | { | |
4904 | SelectObject (bmpdc, img->mask); | |
4905 | SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f)); | |
4906 | MoveToEx (bmpdc, 0, 0, NULL); | |
4907 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4908 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4909 | LineTo (bmpdc, img->width - 1, 0); | |
4910 | } | |
4911 | SelectObject (bmpdc, prev); | |
4912 | DeleteDC (bmpdc); | |
4913 | #endif /* HAVE_NTGUI */ | |
4914 | } | |
4915 | } | |
4916 | ||
4917 | ||
4918 | /* Build a mask for image IMG which is used on frame F. FILE is the | |
4919 | name of an image file, for error messages. HOW determines how to | |
4920 | determine the background color of IMG. If it is a list '(R G B)', | |
4921 | with R, G, and B being integers >= 0, take that as the color of the | |
4922 | background. Otherwise, determine the background color of IMG | |
578098f3 | 4923 | heuristically. */ |
4ef0d4d0 | 4924 | |
578098f3 | 4925 | static void |
d3da34e0 | 4926 | x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) |
4ef0d4d0 KS |
4927 | { |
4928 | XImagePtr_or_DC ximg; | |
4929 | #ifndef HAVE_NTGUI | |
4930 | XImagePtr mask_img; | |
4931 | #else | |
4ef0d4d0 KS |
4932 | HGDIOBJ prev; |
4933 | char *mask_img; | |
4934 | int row_width; | |
4935 | #endif /* HAVE_NTGUI */ | |
578098f3 PE |
4936 | int x, y; |
4937 | bool rc, use_img_background; | |
4ef0d4d0 KS |
4938 | unsigned long bg = 0; |
4939 | ||
4940 | if (img->mask) | |
547c9269 | 4941 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
4ef0d4d0 KS |
4942 | |
4943 | #ifndef HAVE_NTGUI | |
edfda783 | 4944 | #ifndef HAVE_NS |
4ef0d4d0 | 4945 | /* Create an image and pixmap serving as mask. */ |
547c9269 YM |
4946 | rc = image_create_x_image_and_pixmap (f, img, img->width, img->height, 1, |
4947 | &mask_img, 1); | |
4ef0d4d0 | 4948 | if (!rc) |
578098f3 | 4949 | return; |
edfda783 | 4950 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4951 | #else |
4952 | /* Create the bit array serving as mask. */ | |
4953 | row_width = (img->width + 7) / 8; | |
23f86fce | 4954 | mask_img = xzalloc (row_width * img->height); |
4ef0d4d0 KS |
4955 | #endif /* HAVE_NTGUI */ |
4956 | ||
547c9269 YM |
4957 | /* Get the X image or create a memory device context for IMG. */ |
4958 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); | |
4959 | ||
4ef0d4d0 KS |
4960 | /* Determine the background color of ximg. If HOW is `(R G B)' |
4961 | take that as color. Otherwise, use the image's background color. */ | |
4962 | use_img_background = 1; | |
4963 | ||
4964 | if (CONSP (how)) | |
4965 | { | |
4966 | int rgb[3], i; | |
4967 | ||
4968 | for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i) | |
4969 | { | |
4970 | rgb[i] = XFASTINT (XCAR (how)) & 0xffff; | |
4971 | how = XCDR (how); | |
4972 | } | |
4973 | ||
4974 | if (i == 3 && NILP (how)) | |
4975 | { | |
4976 | char color_name[30]; | |
4977 | sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]); | |
4978 | bg = ( | |
4979 | #ifdef HAVE_NTGUI | |
4980 | 0x00ffffff & /* Filter out palette info. */ | |
4981 | #endif /* HAVE_NTGUI */ | |
4982 | x_alloc_image_color (f, img, build_string (color_name), 0)); | |
4983 | use_img_background = 0; | |
4984 | } | |
4985 | } | |
4986 | ||
4987 | if (use_img_background) | |
6ac3c7bb | 4988 | bg = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
4989 | |
4990 | /* Set all bits in mask_img to 1 whose color in ximg is different | |
4991 | from the background color bg. */ | |
4992 | #ifndef HAVE_NTGUI | |
4993 | for (y = 0; y < img->height; ++y) | |
4994 | for (x = 0; x < img->width; ++x) | |
edfda783 | 4995 | #ifndef HAVE_NS |
4ef0d4d0 | 4996 | XPutPixel (mask_img, x, y, (XGetPixel (ximg, x, y) != bg |
9ecf6403 | 4997 | ? PIX_MASK_DRAW : PIX_MASK_RETAIN)); |
edfda783 AR |
4998 | #else |
4999 | if (XGetPixel (ximg, x, y) == bg) | |
ed3751c8 | 5000 | ns_set_alpha (ximg, x, y, 0); |
edfda783 AR |
5001 | #endif /* HAVE_NS */ |
5002 | #ifndef HAVE_NS | |
4ef0d4d0 KS |
5003 | /* Fill in the background_transparent field while we have the mask handy. */ |
5004 | image_background_transparent (img, f, mask_img); | |
5005 | ||
547c9269 YM |
5006 | /* Put mask_img into the image. */ |
5007 | image_put_x_image (f, img, mask_img, 1); | |
edfda783 | 5008 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
5009 | #else |
5010 | for (y = 0; y < img->height; ++y) | |
5011 | for (x = 0; x < img->width; ++x) | |
5012 | { | |
5013 | COLORREF p = GetPixel (ximg, x, y); | |
5014 | if (p != bg) | |
5015 | mask_img[y * row_width + x / 8] |= 1 << (x % 8); | |
5016 | } | |
5017 | ||
5018 | /* Create the mask image. */ | |
5019 | img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height, | |
5020 | mask_img); | |
5021 | /* Fill in the background_transparent field while we have the mask handy. */ | |
5022 | SelectObject (ximg, img->mask); | |
5023 | image_background_transparent (img, f, ximg); | |
5024 | ||
5025 | /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */ | |
5026 | xfree (mask_img); | |
5027 | #endif /* HAVE_NTGUI */ | |
5028 | ||
547c9269 | 5029 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
4ef0d4d0 KS |
5030 | } |
5031 | ||
5032 | \f | |
5033 | /*********************************************************************** | |
5034 | PBM (mono, gray, color) | |
5035 | ***********************************************************************/ | |
5036 | ||
578098f3 PE |
5037 | static bool pbm_image_p (Lisp_Object object); |
5038 | static bool pbm_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
5039 | |
5040 | /* The symbol `pbm' identifying images of this type. */ | |
5041 | ||
955cbe7b | 5042 | static Lisp_Object Qpbm; |
4ef0d4d0 KS |
5043 | |
5044 | /* Indices of image specification fields in gs_format, below. */ | |
5045 | ||
5046 | enum pbm_keyword_index | |
5047 | { | |
5048 | PBM_TYPE, | |
5049 | PBM_FILE, | |
5050 | PBM_DATA, | |
5051 | PBM_ASCENT, | |
5052 | PBM_MARGIN, | |
5053 | PBM_RELIEF, | |
5054 | PBM_ALGORITHM, | |
5055 | PBM_HEURISTIC_MASK, | |
5056 | PBM_MASK, | |
5057 | PBM_FOREGROUND, | |
5058 | PBM_BACKGROUND, | |
5059 | PBM_LAST | |
5060 | }; | |
5061 | ||
5062 | /* Vector of image_keyword structures describing the format | |
5063 | of valid user-defined image specifications. */ | |
5064 | ||
91433552 | 5065 | static const struct image_keyword pbm_format[PBM_LAST] = |
4ef0d4d0 KS |
5066 | { |
5067 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5068 | {":file", IMAGE_STRING_VALUE, 0}, | |
5069 | {":data", IMAGE_STRING_VALUE, 0}, | |
5070 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 5071 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
5072 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
5073 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5074 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5075 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5076 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
5077 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5078 | }; | |
5079 | ||
5080 | /* Structure describing the image type `pbm'. */ | |
5081 | ||
5082 | static struct image_type pbm_type = | |
5083 | { | |
5084 | &Qpbm, | |
5085 | pbm_image_p, | |
5086 | pbm_load, | |
5087 | x_clear_image, | |
bb4d86b4 | 5088 | NULL, |
4ef0d4d0 KS |
5089 | NULL |
5090 | }; | |
5091 | ||
5092 | ||
578098f3 | 5093 | /* Return true if OBJECT is a valid PBM image specification. */ |
4ef0d4d0 | 5094 | |
578098f3 | 5095 | static bool |
d3da34e0 | 5096 | pbm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
5097 | { |
5098 | struct image_keyword fmt[PBM_LAST]; | |
5099 | ||
72af86bd | 5100 | memcpy (fmt, pbm_format, sizeof fmt); |
4ef0d4d0 KS |
5101 | |
5102 | if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)) | |
5103 | return 0; | |
5104 | ||
5105 | /* Must specify either :data or :file. */ | |
5106 | return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1; | |
5107 | } | |
5108 | ||
5109 | ||
5110 | /* Scan a decimal number from *S and return it. Advance *S while | |
5111 | reading the number. END is the end of the string. Value is -1 at | |
5112 | end of input. */ | |
5113 | ||
5114 | static int | |
d3da34e0 | 5115 | pbm_scan_number (unsigned char **s, unsigned char *end) |
4ef0d4d0 KS |
5116 | { |
5117 | int c = 0, val = -1; | |
5118 | ||
5119 | while (*s < end) | |
5120 | { | |
5121 | /* Skip white-space. */ | |
620f13b0 | 5122 | while (*s < end && (c = *(*s)++, c_isspace (c))) |
4ef0d4d0 KS |
5123 | ; |
5124 | ||
5125 | if (c == '#') | |
5126 | { | |
5127 | /* Skip comment to end of line. */ | |
5128 | while (*s < end && (c = *(*s)++, c != '\n')) | |
5129 | ; | |
5130 | } | |
620f13b0 | 5131 | else if (c_isdigit (c)) |
4ef0d4d0 KS |
5132 | { |
5133 | /* Read decimal number. */ | |
5134 | val = c - '0'; | |
620f13b0 | 5135 | while (*s < end && (c = *(*s)++, c_isdigit (c))) |
4ef0d4d0 KS |
5136 | val = 10 * val + c - '0'; |
5137 | break; | |
5138 | } | |
5139 | else | |
5140 | break; | |
5141 | } | |
5142 | ||
5143 | return val; | |
5144 | } | |
5145 | ||
5146 | ||
4ef0d4d0 KS |
5147 | /* Load PBM image IMG for use on frame F. */ |
5148 | ||
578098f3 | 5149 | static bool |
d3da34e0 | 5150 | pbm_load (struct frame *f, struct image *img) |
4ef0d4d0 | 5151 | { |
578098f3 PE |
5152 | bool raw_p; |
5153 | int x, y; | |
4ef0d4d0 KS |
5154 | int width, height, max_color_idx = 0; |
5155 | XImagePtr ximg; | |
5156 | Lisp_Object file, specified_file; | |
5157 | enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type; | |
4ef0d4d0 KS |
5158 | unsigned char *contents = NULL; |
5159 | unsigned char *end, *p; | |
dd52fcea | 5160 | ptrdiff_t size; |
4ef0d4d0 KS |
5161 | |
5162 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
4ef0d4d0 KS |
5163 | |
5164 | if (STRINGP (specified_file)) | |
5165 | { | |
5166 | file = x_find_image_file (specified_file); | |
5167 | if (!STRINGP (file)) | |
5168 | { | |
5169 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
5170 | return 0; |
5171 | } | |
5172 | ||
42a5b22f | 5173 | contents = slurp_file (SSDATA (file), &size); |
4ef0d4d0 KS |
5174 | if (contents == NULL) |
5175 | { | |
5176 | image_error ("Error reading `%s'", file, Qnil); | |
4ef0d4d0 KS |
5177 | return 0; |
5178 | } | |
5179 | ||
5180 | p = contents; | |
5181 | end = contents + size; | |
5182 | } | |
5183 | else | |
5184 | { | |
5185 | Lisp_Object data; | |
5186 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
5187 | if (!STRINGP (data)) |
5188 | { | |
5189 | image_error ("Invalid image data `%s'", data, Qnil); | |
5190 | return 0; | |
5191 | } | |
4ef0d4d0 KS |
5192 | p = SDATA (data); |
5193 | end = p + SBYTES (data); | |
5194 | } | |
5195 | ||
5196 | /* Check magic number. */ | |
5197 | if (end - p < 2 || *p++ != 'P') | |
5198 | { | |
5199 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5200 | error: | |
5201 | xfree (contents); | |
4ef0d4d0 KS |
5202 | return 0; |
5203 | } | |
5204 | ||
5205 | switch (*p++) | |
5206 | { | |
5207 | case '1': | |
5208 | raw_p = 0, type = PBM_MONO; | |
5209 | break; | |
5210 | ||
5211 | case '2': | |
5212 | raw_p = 0, type = PBM_GRAY; | |
5213 | break; | |
5214 | ||
5215 | case '3': | |
5216 | raw_p = 0, type = PBM_COLOR; | |
5217 | break; | |
5218 | ||
5219 | case '4': | |
5220 | raw_p = 1, type = PBM_MONO; | |
5221 | break; | |
5222 | ||
5223 | case '5': | |
5224 | raw_p = 1, type = PBM_GRAY; | |
5225 | break; | |
5226 | ||
5227 | case '6': | |
5228 | raw_p = 1, type = PBM_COLOR; | |
5229 | break; | |
5230 | ||
5231 | default: | |
5232 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5233 | goto error; | |
5234 | } | |
5235 | ||
5236 | /* Read width, height, maximum color-component. Characters | |
5237 | starting with `#' up to the end of a line are ignored. */ | |
5238 | width = pbm_scan_number (&p, end); | |
5239 | height = pbm_scan_number (&p, end); | |
5240 | ||
5241 | if (type != PBM_MONO) | |
5242 | { | |
5243 | max_color_idx = pbm_scan_number (&p, end); | |
b9c89e11 JR |
5244 | if (max_color_idx > 65535 || max_color_idx < 0) |
5245 | { | |
5246 | image_error ("Unsupported maximum PBM color value", Qnil, Qnil); | |
5247 | goto error; | |
5248 | } | |
4ef0d4d0 KS |
5249 | } |
5250 | ||
b9c89e11 JR |
5251 | if (!check_image_size (f, width, height)) |
5252 | { | |
10ea2b82 | 5253 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
b9c89e11 JR |
5254 | goto error; |
5255 | } | |
4ef0d4d0 | 5256 | |
547c9269 | 5257 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
4ef0d4d0 KS |
5258 | goto error; |
5259 | ||
5260 | /* Initialize the color hash table. */ | |
5261 | init_color_table (); | |
5262 | ||
5263 | if (type == PBM_MONO) | |
5264 | { | |
5265 | int c = 0, g; | |
5266 | struct image_keyword fmt[PBM_LAST]; | |
5267 | unsigned long fg = FRAME_FOREGROUND_PIXEL (f); | |
5268 | unsigned long bg = FRAME_BACKGROUND_PIXEL (f); | |
5269 | ||
5270 | /* Parse the image specification. */ | |
72af86bd | 5271 | memcpy (fmt, pbm_format, sizeof fmt); |
4ef0d4d0 KS |
5272 | parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm); |
5273 | ||
5274 | /* Get foreground and background colors, maybe allocate colors. */ | |
5275 | if (fmt[PBM_FOREGROUND].count | |
5276 | && STRINGP (fmt[PBM_FOREGROUND].value)) | |
5277 | fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg); | |
5278 | if (fmt[PBM_BACKGROUND].count | |
5279 | && STRINGP (fmt[PBM_BACKGROUND].value)) | |
5280 | { | |
5281 | bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg); | |
5282 | img->background = bg; | |
5283 | img->background_valid = 1; | |
5284 | } | |
5285 | ||
5286 | for (y = 0; y < height; ++y) | |
5287 | for (x = 0; x < width; ++x) | |
5288 | { | |
5289 | if (raw_p) | |
5290 | { | |
5291 | if ((x & 7) == 0) | |
c295e710 CY |
5292 | { |
5293 | if (p >= end) | |
5294 | { | |
5295 | x_destroy_x_image (ximg); | |
5296 | x_clear_image (f, img); | |
5297 | image_error ("Invalid image size in image `%s'", | |
5298 | img->spec, Qnil); | |
5299 | goto error; | |
5300 | } | |
5301 | c = *p++; | |
5302 | } | |
4ef0d4d0 KS |
5303 | g = c & 0x80; |
5304 | c <<= 1; | |
5305 | } | |
5306 | else | |
5307 | g = pbm_scan_number (&p, end); | |
5308 | ||
5309 | XPutPixel (ximg, x, y, g ? fg : bg); | |
5310 | } | |
5311 | } | |
5312 | else | |
5313 | { | |
b9c89e11 JR |
5314 | int expected_size = height * width; |
5315 | if (max_color_idx > 255) | |
5316 | expected_size *= 2; | |
5317 | if (type == PBM_COLOR) | |
5318 | expected_size *= 3; | |
5319 | ||
5320 | if (raw_p && p + expected_size > end) | |
39e653ea CY |
5321 | { |
5322 | x_destroy_x_image (ximg); | |
fb0b0862 | 5323 | x_clear_image (f, img); |
39e653ea CY |
5324 | image_error ("Invalid image size in image `%s'", |
5325 | img->spec, Qnil); | |
5326 | goto error; | |
5327 | } | |
5328 | ||
4ef0d4d0 KS |
5329 | for (y = 0; y < height; ++y) |
5330 | for (x = 0; x < width; ++x) | |
5331 | { | |
5332 | int r, g, b; | |
5333 | ||
b9c89e11 JR |
5334 | if (type == PBM_GRAY && raw_p) |
5335 | { | |
5336 | r = g = b = *p++; | |
5337 | if (max_color_idx > 255) | |
5338 | r = g = b = r * 256 + *p++; | |
5339 | } | |
5340 | else if (type == PBM_GRAY) | |
5341 | r = g = b = pbm_scan_number (&p, end); | |
4ef0d4d0 KS |
5342 | else if (raw_p) |
5343 | { | |
5344 | r = *p++; | |
b9c89e11 JR |
5345 | if (max_color_idx > 255) |
5346 | r = r * 256 + *p++; | |
4ef0d4d0 | 5347 | g = *p++; |
b9c89e11 JR |
5348 | if (max_color_idx > 255) |
5349 | g = g * 256 + *p++; | |
4ef0d4d0 | 5350 | b = *p++; |
b9c89e11 JR |
5351 | if (max_color_idx > 255) |
5352 | b = b * 256 + *p++; | |
4ef0d4d0 KS |
5353 | } |
5354 | else | |
5355 | { | |
5356 | r = pbm_scan_number (&p, end); | |
5357 | g = pbm_scan_number (&p, end); | |
5358 | b = pbm_scan_number (&p, end); | |
5359 | } | |
5360 | ||
5361 | if (r < 0 || g < 0 || b < 0) | |
5362 | { | |
5363 | x_destroy_x_image (ximg); | |
5364 | image_error ("Invalid pixel value in image `%s'", | |
5365 | img->spec, Qnil); | |
5366 | goto error; | |
5367 | } | |
5368 | ||
5369 | /* RGB values are now in the range 0..max_color_idx. | |
5370 | Scale this to the range 0..0xffff supported by X. */ | |
5371 | r = (double) r * 65535 / max_color_idx; | |
5372 | g = (double) g * 65535 / max_color_idx; | |
5373 | b = (double) b * 65535 / max_color_idx; | |
5374 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
5375 | } | |
5376 | } | |
5377 | ||
5378 | #ifdef COLOR_TABLE_SUPPORT | |
5379 | /* Store in IMG->colors the colors allocated for the image, and | |
5380 | free the color table. */ | |
5381 | img->colors = colors_in_color_table (&img->ncolors); | |
5382 | free_color_table (); | |
5383 | #endif /* COLOR_TABLE_SUPPORT */ | |
5384 | ||
5385 | img->width = width; | |
5386 | img->height = height; | |
5387 | ||
5388 | /* Maybe fill in the background field while we have ximg handy. */ | |
5389 | ||
5390 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
5391 | /* Casting avoids a GCC warning. */ |
5392 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 5393 | |
547c9269 YM |
5394 | /* Put ximg into the image. */ |
5395 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
5396 | |
5397 | /* X and W32 versions did it here, MAC version above. ++kfs | |
1f026899 | 5398 | img->width = width; |
4ef0d4d0 KS |
5399 | img->height = height; */ |
5400 | ||
4ef0d4d0 KS |
5401 | xfree (contents); |
5402 | return 1; | |
5403 | } | |
5404 | ||
5405 | \f | |
5406 | /*********************************************************************** | |
5407 | PNG | |
5408 | ***********************************************************************/ | |
5409 | ||
9e2a2647 | 5410 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
4ef0d4d0 KS |
5411 | |
5412 | /* Function prototypes. */ | |
5413 | ||
578098f3 PE |
5414 | static bool png_image_p (Lisp_Object object); |
5415 | static bool png_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
5416 | |
5417 | /* The symbol `png' identifying images of this type. */ | |
5418 | ||
955cbe7b | 5419 | static Lisp_Object Qpng; |
4ef0d4d0 KS |
5420 | |
5421 | /* Indices of image specification fields in png_format, below. */ | |
5422 | ||
5423 | enum png_keyword_index | |
5424 | { | |
5425 | PNG_TYPE, | |
5426 | PNG_DATA, | |
5427 | PNG_FILE, | |
5428 | PNG_ASCENT, | |
5429 | PNG_MARGIN, | |
5430 | PNG_RELIEF, | |
5431 | PNG_ALGORITHM, | |
5432 | PNG_HEURISTIC_MASK, | |
5433 | PNG_MASK, | |
5434 | PNG_BACKGROUND, | |
5435 | PNG_LAST | |
5436 | }; | |
5437 | ||
5438 | /* Vector of image_keyword structures describing the format | |
5439 | of valid user-defined image specifications. */ | |
5440 | ||
91433552 | 5441 | static const struct image_keyword png_format[PNG_LAST] = |
4ef0d4d0 KS |
5442 | { |
5443 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5444 | {":data", IMAGE_STRING_VALUE, 0}, | |
5445 | {":file", IMAGE_STRING_VALUE, 0}, | |
5446 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 5447 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
5448 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
5449 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5450 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5451 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5452 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5453 | }; | |
5454 | ||
a9e7a9d5 | 5455 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 5456 | static bool init_png_functions (void); |
bb4d86b4 CY |
5457 | #else |
5458 | #define init_png_functions NULL | |
5459 | #endif | |
5460 | ||
4ef0d4d0 KS |
5461 | /* Structure describing the image type `png'. */ |
5462 | ||
5463 | static struct image_type png_type = | |
5464 | { | |
5465 | &Qpng, | |
5466 | png_image_p, | |
5467 | png_load, | |
5468 | x_clear_image, | |
bb4d86b4 | 5469 | init_png_functions, |
4ef0d4d0 KS |
5470 | NULL |
5471 | }; | |
5472 | ||
578098f3 | 5473 | /* Return true if OBJECT is a valid PNG image specification. */ |
4ef0d4d0 | 5474 | |
578098f3 | 5475 | static bool |
d3da34e0 | 5476 | png_image_p (Lisp_Object object) |
4ef0d4d0 KS |
5477 | { |
5478 | struct image_keyword fmt[PNG_LAST]; | |
72af86bd | 5479 | memcpy (fmt, png_format, sizeof fmt); |
4ef0d4d0 KS |
5480 | |
5481 | if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)) | |
5482 | return 0; | |
5483 | ||
5484 | /* Must specify either the :data or :file keyword. */ | |
5485 | return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1; | |
5486 | } | |
5487 | ||
9e2a2647 | 5488 | #endif /* HAVE_PNG || HAVE_NS */ |
4ef0d4d0 KS |
5489 | |
5490 | ||
5491 | #ifdef HAVE_PNG | |
5492 | ||
0fda9b75 | 5493 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
5494 | /* PNG library details. */ |
5495 | ||
14beddf4 CY |
5496 | DEF_IMGLIB_FN (png_voidp, png_get_io_ptr, (png_structp)); |
5497 | DEF_IMGLIB_FN (int, png_sig_cmp, (png_bytep, png_size_t, png_size_t)); | |
5498 | DEF_IMGLIB_FN (png_structp, png_create_read_struct, (png_const_charp, png_voidp, | |
5499 | png_error_ptr, png_error_ptr)); | |
5500 | DEF_IMGLIB_FN (png_infop, png_create_info_struct, (png_structp)); | |
5501 | DEF_IMGLIB_FN (void, png_destroy_read_struct, (png_structpp, png_infopp, png_infopp)); | |
5502 | DEF_IMGLIB_FN (void, png_set_read_fn, (png_structp, png_voidp, png_rw_ptr)); | |
5503 | DEF_IMGLIB_FN (void, png_set_sig_bytes, (png_structp, int)); | |
5504 | DEF_IMGLIB_FN (void, png_read_info, (png_structp, png_infop)); | |
5505 | DEF_IMGLIB_FN (png_uint_32, png_get_IHDR, (png_structp, png_infop, | |
850690cc JB |
5506 | png_uint_32 *, png_uint_32 *, |
5507 | int *, int *, int *, int *, int *)); | |
14beddf4 CY |
5508 | DEF_IMGLIB_FN (png_uint_32, png_get_valid, (png_structp, png_infop, png_uint_32)); |
5509 | DEF_IMGLIB_FN (void, png_set_strip_16, (png_structp)); | |
5510 | DEF_IMGLIB_FN (void, png_set_expand, (png_structp)); | |
5511 | DEF_IMGLIB_FN (void, png_set_gray_to_rgb, (png_structp)); | |
5512 | DEF_IMGLIB_FN (void, png_set_background, (png_structp, png_color_16p, | |
850690cc | 5513 | int, int, double)); |
14beddf4 CY |
5514 | DEF_IMGLIB_FN (png_uint_32, png_get_bKGD, (png_structp, png_infop, png_color_16p *)); |
5515 | DEF_IMGLIB_FN (void, png_read_update_info, (png_structp, png_infop)); | |
5516 | DEF_IMGLIB_FN (png_byte, png_get_channels, (png_structp, png_infop)); | |
5517 | DEF_IMGLIB_FN (png_size_t, png_get_rowbytes, (png_structp, png_infop)); | |
5518 | DEF_IMGLIB_FN (void, png_read_image, (png_structp, png_bytepp)); | |
5519 | DEF_IMGLIB_FN (void, png_read_end, (png_structp, png_infop)); | |
5520 | DEF_IMGLIB_FN (void, png_error, (png_structp, png_const_charp)); | |
4ef0d4d0 | 5521 | |
7f9c5df9 | 5522 | #if (PNG_LIBPNG_VER >= 10500) |
14beddf4 CY |
5523 | DEF_IMGLIB_FN (void, png_longjmp, (png_structp, int)); |
5524 | DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn, (png_structp, png_longjmp_ptr, size_t)); | |
7f9c5df9 | 5525 | #endif /* libpng version >= 1.5 */ |
4ef0d4d0 | 5526 | |
578098f3 | 5527 | static bool |
d07ff9db | 5528 | init_png_functions (void) |
4ef0d4d0 KS |
5529 | { |
5530 | HMODULE library; | |
5531 | ||
d07ff9db | 5532 | if (!(library = w32_delayed_load (Qpng))) |
4ef0d4d0 KS |
5533 | return 0; |
5534 | ||
5535 | LOAD_IMGLIB_FN (library, png_get_io_ptr); | |
285d07e2 | 5536 | LOAD_IMGLIB_FN (library, png_sig_cmp); |
4ef0d4d0 KS |
5537 | LOAD_IMGLIB_FN (library, png_create_read_struct); |
5538 | LOAD_IMGLIB_FN (library, png_create_info_struct); | |
5539 | LOAD_IMGLIB_FN (library, png_destroy_read_struct); | |
5540 | LOAD_IMGLIB_FN (library, png_set_read_fn); | |
4ef0d4d0 KS |
5541 | LOAD_IMGLIB_FN (library, png_set_sig_bytes); |
5542 | LOAD_IMGLIB_FN (library, png_read_info); | |
5543 | LOAD_IMGLIB_FN (library, png_get_IHDR); | |
5544 | LOAD_IMGLIB_FN (library, png_get_valid); | |
5545 | LOAD_IMGLIB_FN (library, png_set_strip_16); | |
5546 | LOAD_IMGLIB_FN (library, png_set_expand); | |
5547 | LOAD_IMGLIB_FN (library, png_set_gray_to_rgb); | |
5548 | LOAD_IMGLIB_FN (library, png_set_background); | |
5549 | LOAD_IMGLIB_FN (library, png_get_bKGD); | |
5550 | LOAD_IMGLIB_FN (library, png_read_update_info); | |
5551 | LOAD_IMGLIB_FN (library, png_get_channels); | |
5552 | LOAD_IMGLIB_FN (library, png_get_rowbytes); | |
5553 | LOAD_IMGLIB_FN (library, png_read_image); | |
5554 | LOAD_IMGLIB_FN (library, png_read_end); | |
5555 | LOAD_IMGLIB_FN (library, png_error); | |
7f9c5df9 CY |
5556 | |
5557 | #if (PNG_LIBPNG_VER >= 10500) | |
5558 | LOAD_IMGLIB_FN (library, png_longjmp); | |
5559 | LOAD_IMGLIB_FN (library, png_set_longjmp_fn); | |
5560 | #endif /* libpng version >= 1.5 */ | |
5561 | ||
4ef0d4d0 KS |
5562 | return 1; |
5563 | } | |
5564 | #else | |
5565 | ||
5566 | #define fn_png_get_io_ptr png_get_io_ptr | |
285d07e2 | 5567 | #define fn_png_sig_cmp png_sig_cmp |
4ef0d4d0 KS |
5568 | #define fn_png_create_read_struct png_create_read_struct |
5569 | #define fn_png_create_info_struct png_create_info_struct | |
5570 | #define fn_png_destroy_read_struct png_destroy_read_struct | |
5571 | #define fn_png_set_read_fn png_set_read_fn | |
4ef0d4d0 KS |
5572 | #define fn_png_set_sig_bytes png_set_sig_bytes |
5573 | #define fn_png_read_info png_read_info | |
5574 | #define fn_png_get_IHDR png_get_IHDR | |
5575 | #define fn_png_get_valid png_get_valid | |
5576 | #define fn_png_set_strip_16 png_set_strip_16 | |
5577 | #define fn_png_set_expand png_set_expand | |
5578 | #define fn_png_set_gray_to_rgb png_set_gray_to_rgb | |
5579 | #define fn_png_set_background png_set_background | |
5580 | #define fn_png_get_bKGD png_get_bKGD | |
5581 | #define fn_png_read_update_info png_read_update_info | |
5582 | #define fn_png_get_channels png_get_channels | |
5583 | #define fn_png_get_rowbytes png_get_rowbytes | |
5584 | #define fn_png_read_image png_read_image | |
5585 | #define fn_png_read_end png_read_end | |
5586 | #define fn_png_error png_error | |
5587 | ||
7f9c5df9 CY |
5588 | #if (PNG_LIBPNG_VER >= 10500) |
5589 | #define fn_png_longjmp png_longjmp | |
5590 | #define fn_png_set_longjmp_fn png_set_longjmp_fn | |
5591 | #endif /* libpng version >= 1.5 */ | |
5592 | ||
0fda9b75 | 5593 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 5594 | |
0328b6de PE |
5595 | /* Possibly inefficient/inexact substitutes for _setjmp and _longjmp. |
5596 | Do not use sys_setjmp, as PNG supports only jmp_buf. The _longjmp | |
7105c8cb EZ |
5597 | substitute may munge the signal mask, but that should be OK here. |
5598 | MinGW (MS-Windows) uses _setjmp and defines setjmp to _setjmp in | |
5599 | the system header setjmp.h; don't mess up that. */ | |
0328b6de | 5600 | #ifndef HAVE__SETJMP |
39a57ad0 | 5601 | # define _setjmp(j) setjmp (j) |
0328b6de PE |
5602 | # define _longjmp longjmp |
5603 | #endif | |
7f9c5df9 CY |
5604 | |
5605 | #if (PNG_LIBPNG_VER < 10500) | |
af26b72c | 5606 | #define PNG_LONGJMP(ptr) (_longjmp ((ptr)->jmpbuf, 1)) |
7f9c5df9 CY |
5607 | #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf) |
5608 | #else | |
df61c790 | 5609 | /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */ |
c2e79cb4 | 5610 | #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1)) |
7f9c5df9 | 5611 | #define PNG_JMPBUF(ptr) \ |
af26b72c | 5612 | (*fn_png_set_longjmp_fn ((ptr), _longjmp, sizeof (jmp_buf))) |
7f9c5df9 CY |
5613 | #endif |
5614 | ||
4ef0d4d0 KS |
5615 | /* Error and warning handlers installed when the PNG library |
5616 | is initialized. */ | |
5617 | ||
845ca893 | 5618 | static _Noreturn void |
d3da34e0 | 5619 | my_png_error (png_struct *png_ptr, const char *msg) |
4ef0d4d0 | 5620 | { |
a54e2c05 | 5621 | eassert (png_ptr != NULL); |
5be1c984 EZ |
5622 | /* Avoid compiler warning about deprecated direct access to |
5623 | png_ptr's fields in libpng versions 1.4.x. */ | |
4ef0d4d0 | 5624 | image_error ("PNG error: %s", build_string (msg), Qnil); |
7f9c5df9 | 5625 | PNG_LONGJMP (png_ptr); |
4ef0d4d0 KS |
5626 | } |
5627 | ||
5628 | ||
5629 | static void | |
d3da34e0 | 5630 | my_png_warning (png_struct *png_ptr, const char *msg) |
4ef0d4d0 | 5631 | { |
a54e2c05 | 5632 | eassert (png_ptr != NULL); |
4ef0d4d0 KS |
5633 | image_error ("PNG warning: %s", build_string (msg), Qnil); |
5634 | } | |
5635 | ||
5636 | /* Memory source for PNG decoding. */ | |
5637 | ||
5638 | struct png_memory_storage | |
5639 | { | |
5640 | unsigned char *bytes; /* The data */ | |
3f791afe PE |
5641 | ptrdiff_t len; /* How big is it? */ |
5642 | ptrdiff_t index; /* Where are we? */ | |
4ef0d4d0 KS |
5643 | }; |
5644 | ||
5645 | ||
5646 | /* Function set as reader function when reading PNG image from memory. | |
5647 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5648 | bytes from the input to DATA. */ | |
5649 | ||
5650 | static void | |
d3da34e0 | 5651 | png_read_from_memory (png_structp png_ptr, png_bytep data, png_size_t length) |
4ef0d4d0 KS |
5652 | { |
5653 | struct png_memory_storage *tbr | |
5654 | = (struct png_memory_storage *) fn_png_get_io_ptr (png_ptr); | |
5655 | ||
5656 | if (length > tbr->len - tbr->index) | |
5657 | fn_png_error (png_ptr, "Read error"); | |
5658 | ||
72af86bd | 5659 | memcpy (data, tbr->bytes + tbr->index, length); |
4ef0d4d0 KS |
5660 | tbr->index = tbr->index + length; |
5661 | } | |
5662 | ||
b0da69a7 | 5663 | |
9a22a004 JB |
5664 | /* Function set as reader function when reading PNG image from a file. |
5665 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5666 | bytes from the input to DATA. */ | |
5667 | ||
5668 | static void | |
d3da34e0 | 5669 | png_read_from_file (png_structp png_ptr, png_bytep data, png_size_t length) |
9a22a004 JB |
5670 | { |
5671 | FILE *fp = (FILE *) fn_png_get_io_ptr (png_ptr); | |
5672 | ||
5673 | if (fread (data, 1, length, fp) < length) | |
5674 | fn_png_error (png_ptr, "Read error"); | |
5675 | } | |
5676 | ||
5677 | ||
578098f3 | 5678 | /* Load PNG image IMG for use on frame F. Value is true if |
4ef0d4d0 KS |
5679 | successful. */ |
5680 | ||
40bce90b PE |
5681 | struct png_load_context |
5682 | { | |
0328b6de | 5683 | /* These are members so that longjmp doesn't munge local variables. */ |
40bce90b PE |
5684 | png_struct *png_ptr; |
5685 | png_info *info_ptr; | |
5686 | png_info *end_info; | |
5687 | FILE *fp; | |
5688 | png_byte *pixels; | |
5689 | png_byte **rows; | |
5690 | }; | |
5691 | ||
578098f3 | 5692 | static bool |
40bce90b | 5693 | png_load_body (struct frame *f, struct image *img, struct png_load_context *c) |
4ef0d4d0 KS |
5694 | { |
5695 | Lisp_Object file, specified_file; | |
5696 | Lisp_Object specified_data; | |
5adf60bc PE |
5697 | int x, y; |
5698 | ptrdiff_t i; | |
4ef0d4d0 | 5699 | XImagePtr ximg, mask_img = NULL; |
40bce90b | 5700 | png_struct *png_ptr; |
4ef0d4d0 | 5701 | png_info *info_ptr = NULL, *end_info = NULL; |
40bce90b | 5702 | FILE *fp = NULL; |
4ef0d4d0 | 5703 | png_byte sig[8]; |
40bce90b PE |
5704 | png_byte *pixels = NULL; |
5705 | png_byte **rows = NULL; | |
4ef0d4d0 KS |
5706 | png_uint_32 width, height; |
5707 | int bit_depth, color_type, interlace_type; | |
5708 | png_byte channels; | |
5709 | png_uint_32 row_bytes; | |
578098f3 | 5710 | bool transparent_p; |
4ef0d4d0 KS |
5711 | struct png_memory_storage tbr; /* Data to be read */ |
5712 | ||
5713 | /* Find out what file to load. */ | |
5714 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
5715 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
4ef0d4d0 KS |
5716 | |
5717 | if (NILP (specified_data)) | |
5718 | { | |
5719 | file = x_find_image_file (specified_file); | |
5720 | if (!STRINGP (file)) | |
5721 | { | |
5722 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
5723 | return 0; |
5724 | } | |
5725 | ||
5726 | /* Open the image file. */ | |
406af475 | 5727 | fp = emacs_fopen (SSDATA (file), "rb"); |
4ef0d4d0 KS |
5728 | if (!fp) |
5729 | { | |
5730 | image_error ("Cannot open image file `%s'", file, Qnil); | |
4ef0d4d0 KS |
5731 | return 0; |
5732 | } | |
5733 | ||
5734 | /* Check PNG signature. */ | |
5735 | if (fread (sig, 1, sizeof sig, fp) != sizeof sig | |
285d07e2 | 5736 | || fn_png_sig_cmp (sig, 0, sizeof sig)) |
4ef0d4d0 KS |
5737 | { |
5738 | image_error ("Not a PNG file: `%s'", file, Qnil); | |
4ef0d4d0 KS |
5739 | fclose (fp); |
5740 | return 0; | |
5741 | } | |
5742 | } | |
5743 | else | |
5744 | { | |
7574650a AS |
5745 | if (!STRINGP (specified_data)) |
5746 | { | |
5747 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
5748 | return 0; | |
5749 | } | |
5750 | ||
4ef0d4d0 KS |
5751 | /* Read from memory. */ |
5752 | tbr.bytes = SDATA (specified_data); | |
5753 | tbr.len = SBYTES (specified_data); | |
5754 | tbr.index = 0; | |
5755 | ||
5756 | /* Check PNG signature. */ | |
5757 | if (tbr.len < sizeof sig | |
285d07e2 | 5758 | || fn_png_sig_cmp (tbr.bytes, 0, sizeof sig)) |
4ef0d4d0 KS |
5759 | { |
5760 | image_error ("Not a PNG image: `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
5761 | return 0; |
5762 | } | |
5763 | ||
5764 | /* Need to skip past the signature. */ | |
5765 | tbr.bytes += sizeof (sig); | |
5766 | } | |
5767 | ||
df61c790 AS |
5768 | /* Initialize read and info structs for PNG lib. */ |
5769 | png_ptr = fn_png_create_read_struct (PNG_LIBPNG_VER_STRING, | |
5770 | NULL, my_png_error, | |
5771 | my_png_warning); | |
40bce90b | 5772 | if (png_ptr) |
4ef0d4d0 | 5773 | { |
40bce90b PE |
5774 | info_ptr = fn_png_create_info_struct (png_ptr); |
5775 | end_info = fn_png_create_info_struct (png_ptr); | |
4ef0d4d0 KS |
5776 | } |
5777 | ||
40bce90b PE |
5778 | c->png_ptr = png_ptr; |
5779 | c->info_ptr = info_ptr; | |
5780 | c->end_info = end_info; | |
5781 | c->fp = fp; | |
5782 | c->pixels = pixels; | |
5783 | c->rows = rows; | |
5784 | ||
5785 | if (! (info_ptr && end_info)) | |
4ef0d4d0 | 5786 | { |
40bce90b PE |
5787 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); |
5788 | png_ptr = 0; | |
4ef0d4d0 | 5789 | } |
40bce90b | 5790 | if (! png_ptr) |
4ef0d4d0 | 5791 | { |
4ef0d4d0 | 5792 | if (fp) fclose (fp); |
4ef0d4d0 KS |
5793 | return 0; |
5794 | } | |
5795 | ||
5796 | /* Set error jump-back. We come back here when the PNG library | |
5797 | detects an error. */ | |
af26b72c | 5798 | if (_setjmp (PNG_JMPBUF (png_ptr))) |
4ef0d4d0 KS |
5799 | { |
5800 | error: | |
40bce90b PE |
5801 | if (c->png_ptr) |
5802 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); | |
5803 | xfree (c->pixels); | |
5804 | xfree (c->rows); | |
5805 | if (c->fp) | |
5806 | fclose (c->fp); | |
4ef0d4d0 KS |
5807 | return 0; |
5808 | } | |
5809 | ||
40bce90b PE |
5810 | /* Silence a bogus diagnostic; see GCC bug 54561. */ |
5811 | IF_LINT (fp = c->fp); | |
5812 | ||
4ef0d4d0 KS |
5813 | /* Read image info. */ |
5814 | if (!NILP (specified_data)) | |
5815 | fn_png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory); | |
5816 | else | |
9a22a004 | 5817 | fn_png_set_read_fn (png_ptr, (void *) fp, png_read_from_file); |
4ef0d4d0 KS |
5818 | |
5819 | fn_png_set_sig_bytes (png_ptr, sizeof sig); | |
5820 | fn_png_read_info (png_ptr, info_ptr); | |
5821 | fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, | |
5822 | &interlace_type, NULL, NULL); | |
5823 | ||
3f791afe PE |
5824 | if (! (width <= INT_MAX && height <= INT_MAX |
5825 | && check_image_size (f, width, height))) | |
10ea2b82 JR |
5826 | { |
5827 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
5828 | goto error; | |
5829 | } | |
ca4aa935 PE |
5830 | |
5831 | /* Create the X image and pixmap now, so that the work below can be | |
5832 | omitted if the image is too large for X. */ | |
547c9269 | 5833 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
ca4aa935 PE |
5834 | goto error; |
5835 | ||
4ef0d4d0 KS |
5836 | /* If image contains simply transparency data, we prefer to |
5837 | construct a clipping mask. */ | |
5838 | if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) | |
5839 | transparent_p = 1; | |
5840 | else | |
5841 | transparent_p = 0; | |
5842 | ||
5843 | /* This function is easier to write if we only have to handle | |
5844 | one data format: RGB or RGBA with 8 bits per channel. Let's | |
5845 | transform other formats into that format. */ | |
5846 | ||
5847 | /* Strip more than 8 bits per channel. */ | |
5848 | if (bit_depth == 16) | |
5849 | fn_png_set_strip_16 (png_ptr); | |
5850 | ||
5851 | /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel | |
5852 | if available. */ | |
5853 | fn_png_set_expand (png_ptr); | |
5854 | ||
5855 | /* Convert grayscale images to RGB. */ | |
5856 | if (color_type == PNG_COLOR_TYPE_GRAY | |
5857 | || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | |
5858 | fn_png_set_gray_to_rgb (png_ptr); | |
5859 | ||
4ef0d4d0 KS |
5860 | /* Handle alpha channel by combining the image with a background |
5861 | color. Do this only if a real alpha channel is supplied. For | |
5862 | simple transparency, we prefer a clipping mask. */ | |
5863 | if (!transparent_p) | |
5864 | { | |
917ac1a9 | 5865 | /* png_color_16 *image_bg; */ |
4ef0d4d0 KS |
5866 | Lisp_Object specified_bg |
5867 | = image_spec_value (img->spec, QCbackground, NULL); | |
c4f13f44 | 5868 | int shift = (bit_depth == 16) ? 0 : 8; |
4ef0d4d0 KS |
5869 | |
5870 | if (STRINGP (specified_bg)) | |
5871 | /* The user specified `:background', use that. */ | |
5872 | { | |
4ef0d4d0 | 5873 | XColor color; |
42a5b22f | 5874 | if (x_defined_color (f, SSDATA (specified_bg), &color, 0)) |
4ef0d4d0 KS |
5875 | { |
5876 | png_color_16 user_bg; | |
5877 | ||
72af86bd | 5878 | memset (&user_bg, 0, sizeof user_bg); |
c4f13f44 CY |
5879 | user_bg.red = color.red >> shift; |
5880 | user_bg.green = color.green >> shift; | |
5881 | user_bg.blue = color.blue >> shift; | |
4ef0d4d0 KS |
5882 | |
5883 | fn_png_set_background (png_ptr, &user_bg, | |
5884 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
5885 | } | |
5886 | } | |
4ef0d4d0 KS |
5887 | else |
5888 | { | |
c4f13f44 CY |
5889 | /* We use the current frame background, ignoring any default |
5890 | background color set by the image. */ | |
8b7d0a16 | 5891 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
4ef0d4d0 KS |
5892 | XColor color; |
5893 | png_color_16 frame_background; | |
5894 | ||
5895 | color.pixel = FRAME_BACKGROUND_PIXEL (f); | |
5896 | x_query_color (f, &color); | |
5897 | ||
72af86bd | 5898 | memset (&frame_background, 0, sizeof frame_background); |
c4f13f44 CY |
5899 | frame_background.red = color.red >> shift; |
5900 | frame_background.green = color.green >> shift; | |
5901 | frame_background.blue = color.blue >> shift; | |
4ef0d4d0 KS |
5902 | #endif /* HAVE_X_WINDOWS */ |
5903 | ||
4ef0d4d0 KS |
5904 | fn_png_set_background (png_ptr, &frame_background, |
5905 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
5906 | } | |
5907 | } | |
5908 | ||
5909 | /* Update info structure. */ | |
5910 | fn_png_read_update_info (png_ptr, info_ptr); | |
5911 | ||
5912 | /* Get number of channels. Valid values are 1 for grayscale images | |
5913 | and images with a palette, 2 for grayscale images with transparency | |
5914 | information (alpha channel), 3 for RGB images, and 4 for RGB | |
5915 | images with alpha channel, i.e. RGBA. If conversions above were | |
5916 | sufficient we should only have 3 or 4 channels here. */ | |
5917 | channels = fn_png_get_channels (png_ptr, info_ptr); | |
a54e2c05 | 5918 | eassert (channels == 3 || channels == 4); |
4ef0d4d0 KS |
5919 | |
5920 | /* Number of bytes needed for one row of the image. */ | |
5921 | row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr); | |
5922 | ||
5923 | /* Allocate memory for the image. */ | |
3f791afe PE |
5924 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *rows < height |
5925 | || min (PTRDIFF_MAX, SIZE_MAX) / sizeof *pixels / height < row_bytes) | |
5926 | memory_full (SIZE_MAX); | |
40bce90b PE |
5927 | c->pixels = pixels = xmalloc (sizeof *pixels * row_bytes * height); |
5928 | c->rows = rows = xmalloc (height * sizeof *rows); | |
4ef0d4d0 KS |
5929 | for (i = 0; i < height; ++i) |
5930 | rows[i] = pixels + i * row_bytes; | |
5931 | ||
5932 | /* Read the entire image. */ | |
5933 | fn_png_read_image (png_ptr, rows); | |
5934 | fn_png_read_end (png_ptr, info_ptr); | |
5935 | if (fp) | |
5936 | { | |
5937 | fclose (fp); | |
40bce90b | 5938 | c->fp = NULL; |
4ef0d4d0 KS |
5939 | } |
5940 | ||
4ef0d4d0 KS |
5941 | /* Create an image and pixmap serving as mask if the PNG image |
5942 | contains an alpha channel. */ | |
5943 | if (channels == 4 | |
5944 | && !transparent_p | |
547c9269 YM |
5945 | && !image_create_x_image_and_pixmap (f, img, width, height, 1, |
5946 | &mask_img, 1)) | |
4ef0d4d0 KS |
5947 | { |
5948 | x_destroy_x_image (ximg); | |
547c9269 | 5949 | x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP); |
4ef0d4d0 KS |
5950 | goto error; |
5951 | } | |
5952 | ||
5953 | /* Fill the X image and mask from PNG data. */ | |
5954 | init_color_table (); | |
5955 | ||
5956 | for (y = 0; y < height; ++y) | |
5957 | { | |
5958 | png_byte *p = rows[y]; | |
5959 | ||
5960 | for (x = 0; x < width; ++x) | |
5961 | { | |
13464394 | 5962 | int r, g, b; |
4ef0d4d0 KS |
5963 | |
5964 | r = *p++ << 8; | |
5965 | g = *p++ << 8; | |
5966 | b = *p++ << 8; | |
5967 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
5968 | /* An alpha channel, aka mask channel, associates variable | |
5969 | transparency with an image. Where other image formats | |
5970 | support binary transparency---fully transparent or fully | |
5971 | opaque---PNG allows up to 254 levels of partial transparency. | |
5972 | The PNG library implements partial transparency by combining | |
5973 | the image with a specified background color. | |
5974 | ||
5975 | I'm not sure how to handle this here nicely: because the | |
5976 | background on which the image is displayed may change, for | |
5977 | real alpha channel support, it would be necessary to create | |
5978 | a new image for each possible background. | |
5979 | ||
5980 | What I'm doing now is that a mask is created if we have | |
5981 | boolean transparency information. Otherwise I'm using | |
5982 | the frame's background color to combine the image with. */ | |
5983 | ||
5984 | if (channels == 4) | |
5985 | { | |
5986 | if (mask_img) | |
9ecf6403 | 5987 | XPutPixel (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN); |
4ef0d4d0 KS |
5988 | ++p; |
5989 | } | |
5990 | } | |
5991 | } | |
5992 | ||
5993 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
5994 | /* Set IMG's background color from the PNG image, unless the user | |
5995 | overrode it. */ | |
5996 | { | |
5997 | png_color_16 *bg; | |
5998 | if (fn_png_get_bKGD (png_ptr, info_ptr, &bg)) | |
5999 | { | |
6000 | img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue); | |
6001 | img->background_valid = 1; | |
6002 | } | |
6003 | } | |
6004 | ||
6005 | #ifdef COLOR_TABLE_SUPPORT | |
6006 | /* Remember colors allocated for this image. */ | |
6007 | img->colors = colors_in_color_table (&img->ncolors); | |
6008 | free_color_table (); | |
6009 | #endif /* COLOR_TABLE_SUPPORT */ | |
6010 | ||
6011 | /* Clean up. */ | |
40bce90b | 6012 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); |
4ef0d4d0 KS |
6013 | xfree (rows); |
6014 | xfree (pixels); | |
6015 | ||
6016 | img->width = width; | |
6017 | img->height = height; | |
6018 | ||
2e09fef1 EZ |
6019 | /* Maybe fill in the background field while we have ximg handy. |
6020 | Casting avoids a GCC warning. */ | |
6021 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 6022 | |
547c9269 YM |
6023 | /* Put ximg into the image. */ |
6024 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
6025 | |
6026 | /* Same for the mask. */ | |
6027 | if (mask_img) | |
6028 | { | |
2e09fef1 EZ |
6029 | /* Fill in the background_transparent field while we have the |
6030 | mask handy. Casting avoids a GCC warning. */ | |
6031 | image_background_transparent (img, f, (XImagePtr_or_DC)mask_img); | |
4ef0d4d0 | 6032 | |
547c9269 | 6033 | image_put_x_image (f, img, mask_img, 1); |
4ef0d4d0 KS |
6034 | } |
6035 | ||
4ef0d4d0 KS |
6036 | return 1; |
6037 | } | |
6038 | ||
578098f3 | 6039 | static bool |
40bce90b PE |
6040 | png_load (struct frame *f, struct image *img) |
6041 | { | |
6042 | struct png_load_context c; | |
6043 | return png_load_body (f, img, &c); | |
6044 | } | |
6045 | ||
4ef0d4d0 KS |
6046 | #else /* HAVE_PNG */ |
6047 | ||
edfda783 | 6048 | #ifdef HAVE_NS |
578098f3 | 6049 | static bool |
edfda783 AR |
6050 | png_load (struct frame *f, struct image *img) |
6051 | { | |
5e617bc2 JB |
6052 | return ns_load_image (f, img, |
6053 | image_spec_value (img->spec, QCfile, NULL), | |
6054 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
6055 | } |
6056 | #endif /* HAVE_NS */ | |
6057 | ||
6058 | ||
4ef0d4d0 KS |
6059 | #endif /* !HAVE_PNG */ |
6060 | ||
6061 | ||
6062 | \f | |
6063 | /*********************************************************************** | |
6064 | JPEG | |
6065 | ***********************************************************************/ | |
6066 | ||
9e2a2647 | 6067 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
4ef0d4d0 | 6068 | |
578098f3 PE |
6069 | static bool jpeg_image_p (Lisp_Object object); |
6070 | static bool jpeg_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
6071 | |
6072 | /* The symbol `jpeg' identifying images of this type. */ | |
6073 | ||
955cbe7b | 6074 | static Lisp_Object Qjpeg; |
4ef0d4d0 KS |
6075 | |
6076 | /* Indices of image specification fields in gs_format, below. */ | |
6077 | ||
6078 | enum jpeg_keyword_index | |
6079 | { | |
6080 | JPEG_TYPE, | |
6081 | JPEG_DATA, | |
6082 | JPEG_FILE, | |
6083 | JPEG_ASCENT, | |
6084 | JPEG_MARGIN, | |
6085 | JPEG_RELIEF, | |
6086 | JPEG_ALGORITHM, | |
6087 | JPEG_HEURISTIC_MASK, | |
6088 | JPEG_MASK, | |
6089 | JPEG_BACKGROUND, | |
6090 | JPEG_LAST | |
6091 | }; | |
6092 | ||
6093 | /* Vector of image_keyword structures describing the format | |
6094 | of valid user-defined image specifications. */ | |
6095 | ||
91433552 | 6096 | static const struct image_keyword jpeg_format[JPEG_LAST] = |
4ef0d4d0 KS |
6097 | { |
6098 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6099 | {":data", IMAGE_STRING_VALUE, 0}, | |
6100 | {":file", IMAGE_STRING_VALUE, 0}, | |
6101 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 6102 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
6103 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
6104 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6105 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6106 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6107 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
6108 | }; | |
6109 | ||
a9e7a9d5 | 6110 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 6111 | static bool init_jpeg_functions (void); |
bb4d86b4 CY |
6112 | #else |
6113 | #define init_jpeg_functions NULL | |
6114 | #endif | |
6115 | ||
4ef0d4d0 KS |
6116 | /* Structure describing the image type `jpeg'. */ |
6117 | ||
6118 | static struct image_type jpeg_type = | |
6119 | { | |
6120 | &Qjpeg, | |
6121 | jpeg_image_p, | |
6122 | jpeg_load, | |
6123 | x_clear_image, | |
bb4d86b4 | 6124 | init_jpeg_functions, |
4ef0d4d0 KS |
6125 | NULL |
6126 | }; | |
6127 | ||
578098f3 | 6128 | /* Return true if OBJECT is a valid JPEG image specification. */ |
4ef0d4d0 | 6129 | |
578098f3 | 6130 | static bool |
d3da34e0 | 6131 | jpeg_image_p (Lisp_Object object) |
4ef0d4d0 KS |
6132 | { |
6133 | struct image_keyword fmt[JPEG_LAST]; | |
6134 | ||
72af86bd | 6135 | memcpy (fmt, jpeg_format, sizeof fmt); |
4ef0d4d0 KS |
6136 | |
6137 | if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)) | |
6138 | return 0; | |
6139 | ||
6140 | /* Must specify either the :data or :file keyword. */ | |
6141 | return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1; | |
6142 | } | |
6143 | ||
9e2a2647 | 6144 | #endif /* HAVE_JPEG || HAVE_NS */ |
4ef0d4d0 KS |
6145 | |
6146 | #ifdef HAVE_JPEG | |
6147 | ||
6148 | /* Work around a warning about HAVE_STDLIB_H being redefined in | |
6149 | jconfig.h. */ | |
6150 | #ifdef HAVE_STDLIB_H | |
4ef0d4d0 KS |
6151 | #undef HAVE_STDLIB_H |
6152 | #endif /* HAVE_STLIB_H */ | |
6153 | ||
66354420 | 6154 | #if defined (HAVE_NTGUI) && !defined (__WIN32__) |
28893db2 JB |
6155 | /* In older releases of the jpeg library, jpeglib.h will define boolean |
6156 | differently depending on __WIN32__, so make sure it is defined. */ | |
66354420 JR |
6157 | #define __WIN32__ 1 |
6158 | #endif | |
6159 | ||
93aa5c81 DC |
6160 | /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types. |
6161 | Some versions of jpeglib try to detect whether rpcndr.h is loaded, | |
6162 | using the Windows boolean type instead of the jpeglib boolean type | |
6163 | if so. Cygwin jpeglib, however, doesn't try to detect whether its | |
6164 | headers are included along with windows.h, so under Cygwin, jpeglib | |
6165 | attempts to define a conflicting boolean type. Worse, forcing | |
6166 | Cygwin jpeglib headers to use the Windows boolean type doesn't work | |
6167 | because it created an ABI incompatibility between the | |
6168 | already-compiled jpeg library and the header interface definition. | |
6169 | ||
6170 | The best we can do is to define jpeglib's boolean type to a | |
6171 | different name. This name, jpeg_boolean, remains in effect through | |
6172 | the rest of image.c. | |
6173 | */ | |
a9e7a9d5 | 6174 | #if defined CYGWIN && defined HAVE_NTGUI |
0fda9b75 | 6175 | #define boolean jpeg_boolean |
2ae3a8a7 | 6176 | #endif |
4ef0d4d0 KS |
6177 | #include <jpeglib.h> |
6178 | #include <jerror.h> | |
4ef0d4d0 | 6179 | |
0fda9b75 | 6180 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
6181 | |
6182 | /* JPEG library details. */ | |
14beddf4 CY |
6183 | DEF_IMGLIB_FN (void, jpeg_CreateDecompress, (j_decompress_ptr, int, size_t)); |
6184 | DEF_IMGLIB_FN (boolean, jpeg_start_decompress, (j_decompress_ptr)); | |
6185 | DEF_IMGLIB_FN (boolean, jpeg_finish_decompress, (j_decompress_ptr)); | |
6186 | DEF_IMGLIB_FN (void, jpeg_destroy_decompress, (j_decompress_ptr)); | |
6187 | DEF_IMGLIB_FN (int, jpeg_read_header, (j_decompress_ptr, boolean)); | |
6188 | DEF_IMGLIB_FN (JDIMENSION, jpeg_read_scanlines, (j_decompress_ptr, JSAMPARRAY, JDIMENSION)); | |
6189 | DEF_IMGLIB_FN (struct jpeg_error_mgr *, jpeg_std_error, (struct jpeg_error_mgr *)); | |
6190 | DEF_IMGLIB_FN (boolean, jpeg_resync_to_restart, (j_decompress_ptr, int)); | |
4ef0d4d0 | 6191 | |
578098f3 | 6192 | static bool |
d07ff9db | 6193 | init_jpeg_functions (void) |
4ef0d4d0 KS |
6194 | { |
6195 | HMODULE library; | |
6196 | ||
d07ff9db | 6197 | if (!(library = w32_delayed_load (Qjpeg))) |
4ef0d4d0 KS |
6198 | return 0; |
6199 | ||
6200 | LOAD_IMGLIB_FN (library, jpeg_finish_decompress); | |
6201 | LOAD_IMGLIB_FN (library, jpeg_read_scanlines); | |
6202 | LOAD_IMGLIB_FN (library, jpeg_start_decompress); | |
6203 | LOAD_IMGLIB_FN (library, jpeg_read_header); | |
4ef0d4d0 KS |
6204 | LOAD_IMGLIB_FN (library, jpeg_CreateDecompress); |
6205 | LOAD_IMGLIB_FN (library, jpeg_destroy_decompress); | |
6206 | LOAD_IMGLIB_FN (library, jpeg_std_error); | |
6207 | LOAD_IMGLIB_FN (library, jpeg_resync_to_restart); | |
6208 | return 1; | |
6209 | } | |
6210 | ||
6211 | /* Wrapper since we can't directly assign the function pointer | |
6212 | to another function pointer that was declared more completely easily. */ | |
6213 | static boolean | |
7c3320d8 | 6214 | jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo, int desired) |
4ef0d4d0 KS |
6215 | { |
6216 | return fn_jpeg_resync_to_restart (cinfo, desired); | |
6217 | } | |
6218 | ||
6219 | #else | |
6220 | ||
5e617bc2 | 6221 | #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a) |
4ef0d4d0 KS |
6222 | #define fn_jpeg_start_decompress jpeg_start_decompress |
6223 | #define fn_jpeg_finish_decompress jpeg_finish_decompress | |
6224 | #define fn_jpeg_destroy_decompress jpeg_destroy_decompress | |
6225 | #define fn_jpeg_read_header jpeg_read_header | |
6226 | #define fn_jpeg_read_scanlines jpeg_read_scanlines | |
4ef0d4d0 KS |
6227 | #define fn_jpeg_std_error jpeg_std_error |
6228 | #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart | |
6229 | ||
0fda9b75 | 6230 | #endif /* WINDOWSNT */ |
4ef0d4d0 KS |
6231 | |
6232 | struct my_jpeg_error_mgr | |
6233 | { | |
6234 | struct jpeg_error_mgr pub; | |
0328b6de | 6235 | sys_jmp_buf setjmp_buffer; |
40bce90b | 6236 | |
0328b6de | 6237 | /* The remaining members are so that longjmp doesn't munge local |
40bce90b PE |
6238 | variables. */ |
6239 | struct jpeg_decompress_struct cinfo; | |
6240 | enum | |
6241 | { | |
6242 | MY_JPEG_ERROR_EXIT, | |
6243 | MY_JPEG_INVALID_IMAGE_SIZE, | |
6244 | MY_JPEG_CANNOT_CREATE_X | |
6245 | } failure_code; | |
5f0cb45a PE |
6246 | #ifdef lint |
6247 | FILE *fp; | |
6248 | #endif | |
4ef0d4d0 KS |
6249 | }; |
6250 | ||
6251 | ||
845ca893 | 6252 | static _Noreturn void |
d3da34e0 | 6253 | my_error_exit (j_common_ptr cinfo) |
4ef0d4d0 KS |
6254 | { |
6255 | struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err; | |
40bce90b | 6256 | mgr->failure_code = MY_JPEG_ERROR_EXIT; |
0328b6de | 6257 | sys_longjmp (mgr->setjmp_buffer, 1); |
4ef0d4d0 KS |
6258 | } |
6259 | ||
6260 | ||
6261 | /* Init source method for JPEG data source manager. Called by | |
6262 | jpeg_read_header() before any data is actually read. See | |
6263 | libjpeg.doc from the JPEG lib distribution. */ | |
6264 | ||
6265 | static void | |
d3da34e0 | 6266 | our_common_init_source (j_decompress_ptr cinfo) |
b25a72ab JB |
6267 | { |
6268 | } | |
6269 | ||
6270 | ||
6271 | /* Method to terminate data source. Called by | |
6272 | jpeg_finish_decompress() after all data has been processed. */ | |
6273 | ||
6274 | static void | |
d3da34e0 | 6275 | our_common_term_source (j_decompress_ptr cinfo) |
4ef0d4d0 KS |
6276 | { |
6277 | } | |
6278 | ||
6279 | ||
6280 | /* Fill input buffer method for JPEG data source manager. Called | |
6281 | whenever more data is needed. We read the whole image in one step, | |
6282 | so this only adds a fake end of input marker at the end. */ | |
6283 | ||
5a1539f3 | 6284 | static JOCTET our_memory_buffer[2]; |
aca946f3 | 6285 | |
4ef0d4d0 | 6286 | static boolean |
d3da34e0 | 6287 | our_memory_fill_input_buffer (j_decompress_ptr cinfo) |
4ef0d4d0 KS |
6288 | { |
6289 | /* Insert a fake EOI marker. */ | |
6290 | struct jpeg_source_mgr *src = cinfo->src; | |
4ef0d4d0 | 6291 | |
5a1539f3 MB |
6292 | our_memory_buffer[0] = (JOCTET) 0xFF; |
6293 | our_memory_buffer[1] = (JOCTET) JPEG_EOI; | |
4ef0d4d0 | 6294 | |
5a1539f3 | 6295 | src->next_input_byte = our_memory_buffer; |
4ef0d4d0 | 6296 | src->bytes_in_buffer = 2; |
e0e30823 | 6297 | return 1; |
4ef0d4d0 KS |
6298 | } |
6299 | ||
6300 | ||
6301 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6302 | is the JPEG data source manager. */ | |
6303 | ||
6304 | static void | |
d3da34e0 | 6305 | our_memory_skip_input_data (j_decompress_ptr cinfo, long int num_bytes) |
4ef0d4d0 KS |
6306 | { |
6307 | struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src; | |
6308 | ||
6309 | if (src) | |
6310 | { | |
6311 | if (num_bytes > src->bytes_in_buffer) | |
6312 | ERREXIT (cinfo, JERR_INPUT_EOF); | |
6313 | ||
6314 | src->bytes_in_buffer -= num_bytes; | |
6315 | src->next_input_byte += num_bytes; | |
6316 | } | |
6317 | } | |
6318 | ||
6319 | ||
4ef0d4d0 KS |
6320 | /* Set up the JPEG lib for reading an image from DATA which contains |
6321 | LEN bytes. CINFO is the decompression info structure created for | |
6322 | reading the image. */ | |
6323 | ||
6324 | static void | |
92547ff9 | 6325 | jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, ptrdiff_t len) |
4ef0d4d0 KS |
6326 | { |
6327 | struct jpeg_source_mgr *src; | |
6328 | ||
6329 | if (cinfo->src == NULL) | |
6330 | { | |
6331 | /* First time for this JPEG object? */ | |
6332 | cinfo->src = (struct jpeg_source_mgr *) | |
6333 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6334 | sizeof (struct jpeg_source_mgr)); | |
6335 | src = (struct jpeg_source_mgr *) cinfo->src; | |
6336 | src->next_input_byte = data; | |
6337 | } | |
6338 | ||
6339 | src = (struct jpeg_source_mgr *) cinfo->src; | |
b25a72ab JB |
6340 | src->init_source = our_common_init_source; |
6341 | src->fill_input_buffer = our_memory_fill_input_buffer; | |
6342 | src->skip_input_data = our_memory_skip_input_data; | |
4ef0d4d0 | 6343 | src->resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ |
b25a72ab | 6344 | src->term_source = our_common_term_source; |
4ef0d4d0 KS |
6345 | src->bytes_in_buffer = len; |
6346 | src->next_input_byte = data; | |
6347 | } | |
6348 | ||
6349 | ||
b25a72ab JB |
6350 | struct jpeg_stdio_mgr |
6351 | { | |
6352 | struct jpeg_source_mgr mgr; | |
6353 | boolean finished; | |
6354 | FILE *file; | |
6355 | JOCTET *buffer; | |
6356 | }; | |
6357 | ||
6358 | ||
6359 | /* Size of buffer to read JPEG from file. | |
6360 | Not too big, as we want to use alloc_small. */ | |
6361 | #define JPEG_STDIO_BUFFER_SIZE 8192 | |
6362 | ||
6363 | ||
6364 | /* Fill input buffer method for JPEG data source manager. Called | |
6365 | whenever more data is needed. The data is read from a FILE *. */ | |
6366 | ||
6367 | static boolean | |
d3da34e0 | 6368 | our_stdio_fill_input_buffer (j_decompress_ptr cinfo) |
b25a72ab JB |
6369 | { |
6370 | struct jpeg_stdio_mgr *src; | |
6371 | ||
6372 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6373 | if (!src->finished) | |
6374 | { | |
3f791afe | 6375 | ptrdiff_t bytes; |
b25a72ab JB |
6376 | |
6377 | bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file); | |
6378 | if (bytes > 0) | |
6379 | src->mgr.bytes_in_buffer = bytes; | |
6380 | else | |
6381 | { | |
6382 | WARNMS (cinfo, JWRN_JPEG_EOF); | |
6383 | src->finished = 1; | |
6384 | src->buffer[0] = (JOCTET) 0xFF; | |
6385 | src->buffer[1] = (JOCTET) JPEG_EOI; | |
6386 | src->mgr.bytes_in_buffer = 2; | |
6387 | } | |
6388 | src->mgr.next_input_byte = src->buffer; | |
6389 | } | |
6390 | ||
6391 | return 1; | |
6392 | } | |
6393 | ||
6394 | ||
6395 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6396 | is the JPEG data source manager. */ | |
6397 | ||
6398 | static void | |
d3da34e0 | 6399 | our_stdio_skip_input_data (j_decompress_ptr cinfo, long int num_bytes) |
b25a72ab JB |
6400 | { |
6401 | struct jpeg_stdio_mgr *src; | |
6402 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6403 | ||
6404 | while (num_bytes > 0 && !src->finished) | |
6405 | { | |
6406 | if (num_bytes <= src->mgr.bytes_in_buffer) | |
6407 | { | |
6408 | src->mgr.bytes_in_buffer -= num_bytes; | |
6409 | src->mgr.next_input_byte += num_bytes; | |
6410 | break; | |
6411 | } | |
6412 | else | |
6413 | { | |
6414 | num_bytes -= src->mgr.bytes_in_buffer; | |
6415 | src->mgr.bytes_in_buffer = 0; | |
6416 | src->mgr.next_input_byte = NULL; | |
6417 | ||
6418 | our_stdio_fill_input_buffer (cinfo); | |
6419 | } | |
6420 | } | |
6421 | } | |
6422 | ||
6423 | ||
6424 | /* Set up the JPEG lib for reading an image from a FILE *. | |
6425 | CINFO is the decompression info structure created for | |
6426 | reading the image. */ | |
6427 | ||
6428 | static void | |
d3da34e0 | 6429 | jpeg_file_src (j_decompress_ptr cinfo, FILE *fp) |
b25a72ab JB |
6430 | { |
6431 | struct jpeg_stdio_mgr *src; | |
6432 | ||
6433 | if (cinfo->src != NULL) | |
6434 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6435 | else | |
6436 | { | |
6437 | /* First time for this JPEG object? */ | |
6438 | cinfo->src = (struct jpeg_source_mgr *) | |
6439 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6440 | sizeof (struct jpeg_stdio_mgr)); | |
6441 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6442 | src->buffer = (JOCTET *) | |
6443 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
6444 | JPEG_STDIO_BUFFER_SIZE); | |
6445 | } | |
6446 | ||
6447 | src->file = fp; | |
6448 | src->finished = 0; | |
6449 | src->mgr.init_source = our_common_init_source; | |
6450 | src->mgr.fill_input_buffer = our_stdio_fill_input_buffer; | |
6451 | src->mgr.skip_input_data = our_stdio_skip_input_data; | |
6452 | src->mgr.resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ | |
6453 | src->mgr.term_source = our_common_term_source; | |
6454 | src->mgr.bytes_in_buffer = 0; | |
6455 | src->mgr.next_input_byte = NULL; | |
6456 | } | |
6457 | ||
6458 | ||
4ef0d4d0 KS |
6459 | /* Load image IMG for use on frame F. Patterned after example.c |
6460 | from the JPEG lib. */ | |
6461 | ||
578098f3 | 6462 | static bool |
40bce90b PE |
6463 | jpeg_load_body (struct frame *f, struct image *img, |
6464 | struct my_jpeg_error_mgr *mgr) | |
4ef0d4d0 | 6465 | { |
4ef0d4d0 KS |
6466 | Lisp_Object file, specified_file; |
6467 | Lisp_Object specified_data; | |
40bce90b | 6468 | FILE *fp = NULL; |
4ef0d4d0 KS |
6469 | JSAMPARRAY buffer; |
6470 | int row_stride, x, y; | |
6471 | XImagePtr ximg = NULL; | |
4ef0d4d0 KS |
6472 | unsigned long *colors; |
6473 | int width, height; | |
4ef0d4d0 KS |
6474 | |
6475 | /* Open the JPEG file. */ | |
6476 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6477 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
4ef0d4d0 KS |
6478 | |
6479 | if (NILP (specified_data)) | |
6480 | { | |
6481 | file = x_find_image_file (specified_file); | |
6482 | if (!STRINGP (file)) | |
6483 | { | |
6484 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
6485 | return 0; |
6486 | } | |
6487 | ||
406af475 | 6488 | fp = emacs_fopen (SSDATA (file), "rb"); |
4ef0d4d0 KS |
6489 | if (fp == NULL) |
6490 | { | |
6491 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
6492 | return 0; |
6493 | } | |
6494 | } | |
7574650a AS |
6495 | else if (!STRINGP (specified_data)) |
6496 | { | |
6497 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
6498 | return 0; | |
6499 | } | |
4ef0d4d0 | 6500 | |
5f0cb45a PE |
6501 | IF_LINT (mgr->fp = fp); |
6502 | ||
4ef0d4d0 | 6503 | /* Customize libjpeg's error handling to call my_error_exit when an |
df61c790 | 6504 | error is detected. This function will perform a longjmp. */ |
40bce90b PE |
6505 | mgr->cinfo.err = fn_jpeg_std_error (&mgr->pub); |
6506 | mgr->pub.error_exit = my_error_exit; | |
0328b6de | 6507 | if (sys_setjmp (mgr->setjmp_buffer)) |
4ef0d4d0 | 6508 | { |
40bce90b | 6509 | switch (mgr->failure_code) |
4ef0d4d0 | 6510 | { |
40bce90b PE |
6511 | case MY_JPEG_ERROR_EXIT: |
6512 | { | |
6513 | char buf[JMSG_LENGTH_MAX]; | |
6514 | mgr->cinfo.err->format_message ((j_common_ptr) &mgr->cinfo, buf); | |
6515 | image_error ("Error reading JPEG image `%s': %s", img->spec, | |
6516 | build_string (buf)); | |
6517 | break; | |
6518 | } | |
6519 | ||
6520 | case MY_JPEG_INVALID_IMAGE_SIZE: | |
6521 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
6522 | break; | |
6523 | ||
6524 | case MY_JPEG_CANNOT_CREATE_X: | |
6525 | break; | |
4ef0d4d0 KS |
6526 | } |
6527 | ||
6528 | /* Close the input file and destroy the JPEG object. */ | |
6529 | if (fp) | |
40bce90b PE |
6530 | fclose (fp); |
6531 | fn_jpeg_destroy_decompress (&mgr->cinfo); | |
4ef0d4d0 KS |
6532 | |
6533 | /* If we already have an XImage, free that. */ | |
6534 | x_destroy_x_image (ximg); | |
6535 | ||
6536 | /* Free pixmap and colors. */ | |
6537 | x_clear_image (f, img); | |
4ef0d4d0 KS |
6538 | return 0; |
6539 | } | |
6540 | ||
5f0cb45a PE |
6541 | /* Silence a bogus diagnostic; see GCC bug 54561. */ |
6542 | IF_LINT (fp = mgr->fp); | |
6543 | ||
4ef0d4d0 KS |
6544 | /* Create the JPEG decompression object. Let it read from fp. |
6545 | Read the JPEG image header. */ | |
40bce90b | 6546 | fn_jpeg_CreateDecompress (&mgr->cinfo, JPEG_LIB_VERSION, sizeof *&mgr->cinfo); |
4ef0d4d0 KS |
6547 | |
6548 | if (NILP (specified_data)) | |
40bce90b | 6549 | jpeg_file_src (&mgr->cinfo, fp); |
4ef0d4d0 | 6550 | else |
40bce90b | 6551 | jpeg_memory_src (&mgr->cinfo, SDATA (specified_data), |
4ef0d4d0 KS |
6552 | SBYTES (specified_data)); |
6553 | ||
40bce90b | 6554 | fn_jpeg_read_header (&mgr->cinfo, 1); |
4ef0d4d0 KS |
6555 | |
6556 | /* Customize decompression so that color quantization will be used. | |
6557 | Start decompression. */ | |
40bce90b PE |
6558 | mgr->cinfo.quantize_colors = 1; |
6559 | fn_jpeg_start_decompress (&mgr->cinfo); | |
6560 | width = img->width = mgr->cinfo.output_width; | |
6561 | height = img->height = mgr->cinfo.output_height; | |
4ef0d4d0 | 6562 | |
f1f25b99 CY |
6563 | if (!check_image_size (f, width, height)) |
6564 | { | |
40bce90b | 6565 | mgr->failure_code = MY_JPEG_INVALID_IMAGE_SIZE; |
0328b6de | 6566 | sys_longjmp (mgr->setjmp_buffer, 1); |
f1f25b99 CY |
6567 | } |
6568 | ||
4ef0d4d0 | 6569 | /* Create X image and pixmap. */ |
547c9269 | 6570 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
40bce90b PE |
6571 | { |
6572 | mgr->failure_code = MY_JPEG_CANNOT_CREATE_X; | |
0328b6de | 6573 | sys_longjmp (mgr->setjmp_buffer, 1); |
40bce90b | 6574 | } |
4ef0d4d0 KS |
6575 | |
6576 | /* Allocate colors. When color quantization is used, | |
40bce90b PE |
6577 | mgr->cinfo.actual_number_of_colors has been set with the number of |
6578 | colors generated, and mgr->cinfo.colormap is a two-dimensional array | |
6579 | of color indices in the range 0..mgr->cinfo.actual_number_of_colors. | |
4ef0d4d0 KS |
6580 | No more than 255 colors will be generated. */ |
6581 | { | |
6582 | int i, ir, ig, ib; | |
6583 | ||
40bce90b | 6584 | if (mgr->cinfo.out_color_components > 2) |
4ef0d4d0 | 6585 | ir = 0, ig = 1, ib = 2; |
40bce90b | 6586 | else if (mgr->cinfo.out_color_components > 1) |
4ef0d4d0 KS |
6587 | ir = 0, ig = 1, ib = 0; |
6588 | else | |
6589 | ir = 0, ig = 0, ib = 0; | |
6590 | ||
6591 | /* Use the color table mechanism because it handles colors that | |
6592 | cannot be allocated nicely. Such colors will be replaced with | |
6593 | a default color, and we don't have to care about which colors | |
6594 | can be freed safely, and which can't. */ | |
6595 | init_color_table (); | |
40bce90b | 6596 | colors = alloca (mgr->cinfo.actual_number_of_colors * sizeof *colors); |
4ef0d4d0 | 6597 | |
40bce90b | 6598 | for (i = 0; i < mgr->cinfo.actual_number_of_colors; ++i) |
4ef0d4d0 KS |
6599 | { |
6600 | /* Multiply RGB values with 255 because X expects RGB values | |
6601 | in the range 0..0xffff. */ | |
40bce90b PE |
6602 | int r = mgr->cinfo.colormap[ir][i] << 8; |
6603 | int g = mgr->cinfo.colormap[ig][i] << 8; | |
6604 | int b = mgr->cinfo.colormap[ib][i] << 8; | |
4ef0d4d0 KS |
6605 | colors[i] = lookup_rgb_color (f, r, g, b); |
6606 | } | |
6607 | ||
6608 | #ifdef COLOR_TABLE_SUPPORT | |
6609 | /* Remember those colors actually allocated. */ | |
6610 | img->colors = colors_in_color_table (&img->ncolors); | |
6611 | free_color_table (); | |
6612 | #endif /* COLOR_TABLE_SUPPORT */ | |
6613 | } | |
6614 | ||
6615 | /* Read pixels. */ | |
40bce90b PE |
6616 | row_stride = width * mgr->cinfo.output_components; |
6617 | buffer = mgr->cinfo.mem->alloc_sarray ((j_common_ptr) &mgr->cinfo, | |
6618 | JPOOL_IMAGE, row_stride, 1); | |
4ef0d4d0 KS |
6619 | for (y = 0; y < height; ++y) |
6620 | { | |
40bce90b PE |
6621 | fn_jpeg_read_scanlines (&mgr->cinfo, buffer, 1); |
6622 | for (x = 0; x < mgr->cinfo.output_width; ++x) | |
4ef0d4d0 KS |
6623 | XPutPixel (ximg, x, y, colors[buffer[0][x]]); |
6624 | } | |
6625 | ||
6626 | /* Clean up. */ | |
40bce90b PE |
6627 | fn_jpeg_finish_decompress (&mgr->cinfo); |
6628 | fn_jpeg_destroy_decompress (&mgr->cinfo); | |
4ef0d4d0 | 6629 | if (fp) |
40bce90b | 6630 | fclose (fp); |
4ef0d4d0 KS |
6631 | |
6632 | /* Maybe fill in the background field while we have ximg handy. */ | |
6633 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
6634 | /* Casting avoids a GCC warning. */ |
6635 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 6636 | |
547c9269 YM |
6637 | /* Put ximg into the image. */ |
6638 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
6639 | return 1; |
6640 | } | |
6641 | ||
578098f3 | 6642 | static bool |
40bce90b PE |
6643 | jpeg_load (struct frame *f, struct image *img) |
6644 | { | |
6645 | struct my_jpeg_error_mgr mgr; | |
6646 | return jpeg_load_body (f, img, &mgr); | |
6647 | } | |
6648 | ||
4ef0d4d0 KS |
6649 | #else /* HAVE_JPEG */ |
6650 | ||
edfda783 | 6651 | #ifdef HAVE_NS |
578098f3 | 6652 | static bool |
edfda783 AR |
6653 | jpeg_load (struct frame *f, struct image *img) |
6654 | { | |
ed3751c8 JB |
6655 | return ns_load_image (f, img, |
6656 | image_spec_value (img->spec, QCfile, NULL), | |
6657 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
6658 | } |
6659 | #endif /* HAVE_NS */ | |
6660 | ||
4ef0d4d0 KS |
6661 | #endif /* !HAVE_JPEG */ |
6662 | ||
6663 | ||
6664 | \f | |
6665 | /*********************************************************************** | |
6666 | TIFF | |
6667 | ***********************************************************************/ | |
6668 | ||
9e2a2647 | 6669 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
4ef0d4d0 | 6670 | |
578098f3 PE |
6671 | static bool tiff_image_p (Lisp_Object object); |
6672 | static bool tiff_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
6673 | |
6674 | /* The symbol `tiff' identifying images of this type. */ | |
6675 | ||
955cbe7b | 6676 | static Lisp_Object Qtiff; |
4ef0d4d0 KS |
6677 | |
6678 | /* Indices of image specification fields in tiff_format, below. */ | |
6679 | ||
6680 | enum tiff_keyword_index | |
6681 | { | |
6682 | TIFF_TYPE, | |
6683 | TIFF_DATA, | |
6684 | TIFF_FILE, | |
6685 | TIFF_ASCENT, | |
6686 | TIFF_MARGIN, | |
6687 | TIFF_RELIEF, | |
6688 | TIFF_ALGORITHM, | |
6689 | TIFF_HEURISTIC_MASK, | |
6690 | TIFF_MASK, | |
6691 | TIFF_BACKGROUND, | |
58279503 | 6692 | TIFF_INDEX, |
4ef0d4d0 KS |
6693 | TIFF_LAST |
6694 | }; | |
6695 | ||
6696 | /* Vector of image_keyword structures describing the format | |
6697 | of valid user-defined image specifications. */ | |
6698 | ||
91433552 | 6699 | static const struct image_keyword tiff_format[TIFF_LAST] = |
4ef0d4d0 KS |
6700 | { |
6701 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6702 | {":data", IMAGE_STRING_VALUE, 0}, | |
6703 | {":file", IMAGE_STRING_VALUE, 0}, | |
6704 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 6705 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
6706 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
6707 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6708 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6709 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 JR |
6710 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, |
6711 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0} | |
4ef0d4d0 KS |
6712 | }; |
6713 | ||
33d4113c | 6714 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 6715 | static bool init_tiff_functions (void); |
bb4d86b4 CY |
6716 | #else |
6717 | #define init_tiff_functions NULL | |
6718 | #endif | |
6719 | ||
4ef0d4d0 KS |
6720 | /* Structure describing the image type `tiff'. */ |
6721 | ||
6722 | static struct image_type tiff_type = | |
6723 | { | |
6724 | &Qtiff, | |
6725 | tiff_image_p, | |
6726 | tiff_load, | |
6727 | x_clear_image, | |
bb4d86b4 | 6728 | init_tiff_functions, |
4ef0d4d0 KS |
6729 | NULL |
6730 | }; | |
6731 | ||
578098f3 | 6732 | /* Return true if OBJECT is a valid TIFF image specification. */ |
4ef0d4d0 | 6733 | |
578098f3 | 6734 | static bool |
d3da34e0 | 6735 | tiff_image_p (Lisp_Object object) |
4ef0d4d0 KS |
6736 | { |
6737 | struct image_keyword fmt[TIFF_LAST]; | |
72af86bd | 6738 | memcpy (fmt, tiff_format, sizeof fmt); |
4ef0d4d0 KS |
6739 | |
6740 | if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff)) | |
6741 | return 0; | |
6742 | ||
6743 | /* Must specify either the :data or :file keyword. */ | |
6744 | return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1; | |
6745 | } | |
6746 | ||
9e2a2647 | 6747 | #endif /* HAVE_TIFF || HAVE_NS */ |
4ef0d4d0 KS |
6748 | |
6749 | #ifdef HAVE_TIFF | |
6750 | ||
6751 | #include <tiffio.h> | |
6752 | ||
0fda9b75 | 6753 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
6754 | |
6755 | /* TIFF library details. */ | |
14beddf4 CY |
6756 | DEF_IMGLIB_FN (TIFFErrorHandler, TIFFSetErrorHandler, (TIFFErrorHandler)); |
6757 | DEF_IMGLIB_FN (TIFFErrorHandler, TIFFSetWarningHandler, (TIFFErrorHandler)); | |
6758 | DEF_IMGLIB_FN (TIFF *, TIFFOpen, (const char *, const char *)); | |
6759 | DEF_IMGLIB_FN (TIFF *, TIFFClientOpen, (const char *, const char *, thandle_t, | |
850690cc JB |
6760 | TIFFReadWriteProc, TIFFReadWriteProc, |
6761 | TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, | |
6762 | TIFFMapFileProc, TIFFUnmapFileProc)); | |
14beddf4 CY |
6763 | DEF_IMGLIB_FN (int, TIFFGetField, (TIFF *, ttag_t, ...)); |
6764 | DEF_IMGLIB_FN (int, TIFFReadRGBAImage, (TIFF *, uint32, uint32, uint32 *, int)); | |
6765 | DEF_IMGLIB_FN (void, TIFFClose, (TIFF *)); | |
6766 | DEF_IMGLIB_FN (int, TIFFSetDirectory, (TIFF *, tdir_t)); | |
4ef0d4d0 | 6767 | |
578098f3 | 6768 | static bool |
d07ff9db | 6769 | init_tiff_functions (void) |
4ef0d4d0 KS |
6770 | { |
6771 | HMODULE library; | |
6772 | ||
d07ff9db | 6773 | if (!(library = w32_delayed_load (Qtiff))) |
4ef0d4d0 KS |
6774 | return 0; |
6775 | ||
6776 | LOAD_IMGLIB_FN (library, TIFFSetErrorHandler); | |
6777 | LOAD_IMGLIB_FN (library, TIFFSetWarningHandler); | |
6778 | LOAD_IMGLIB_FN (library, TIFFOpen); | |
6779 | LOAD_IMGLIB_FN (library, TIFFClientOpen); | |
6780 | LOAD_IMGLIB_FN (library, TIFFGetField); | |
6781 | LOAD_IMGLIB_FN (library, TIFFReadRGBAImage); | |
6782 | LOAD_IMGLIB_FN (library, TIFFClose); | |
58279503 | 6783 | LOAD_IMGLIB_FN (library, TIFFSetDirectory); |
4ef0d4d0 KS |
6784 | return 1; |
6785 | } | |
6786 | ||
6787 | #else | |
6788 | ||
6789 | #define fn_TIFFSetErrorHandler TIFFSetErrorHandler | |
6790 | #define fn_TIFFSetWarningHandler TIFFSetWarningHandler | |
6791 | #define fn_TIFFOpen TIFFOpen | |
6792 | #define fn_TIFFClientOpen TIFFClientOpen | |
6793 | #define fn_TIFFGetField TIFFGetField | |
6794 | #define fn_TIFFReadRGBAImage TIFFReadRGBAImage | |
6795 | #define fn_TIFFClose TIFFClose | |
58279503 | 6796 | #define fn_TIFFSetDirectory TIFFSetDirectory |
0fda9b75 | 6797 | #endif /* WINDOWSNT */ |
4ef0d4d0 KS |
6798 | |
6799 | ||
6800 | /* Reading from a memory buffer for TIFF images Based on the PNG | |
6801 | memory source, but we have to provide a lot of extra functions. | |
6802 | Blah. | |
6803 | ||
6804 | We really only need to implement read and seek, but I am not | |
6805 | convinced that the TIFF library is smart enough not to destroy | |
6806 | itself if we only hand it the function pointers we need to | |
6807 | override. */ | |
6808 | ||
6809 | typedef struct | |
6810 | { | |
6811 | unsigned char *bytes; | |
3f791afe PE |
6812 | ptrdiff_t len; |
6813 | ptrdiff_t index; | |
4ef0d4d0 KS |
6814 | } |
6815 | tiff_memory_source; | |
6816 | ||
3f791afe | 6817 | static tsize_t |
d3da34e0 | 6818 | tiff_read_from_memory (thandle_t data, tdata_t buf, tsize_t size) |
4ef0d4d0 KS |
6819 | { |
6820 | tiff_memory_source *src = (tiff_memory_source *) data; | |
6821 | ||
3f791afe | 6822 | size = min (size, src->len - src->index); |
72af86bd | 6823 | memcpy (buf, src->bytes + src->index, size); |
4ef0d4d0 KS |
6824 | src->index += size; |
6825 | return size; | |
6826 | } | |
6827 | ||
3f791afe | 6828 | static tsize_t |
d3da34e0 | 6829 | tiff_write_from_memory (thandle_t data, tdata_t buf, tsize_t size) |
4ef0d4d0 | 6830 | { |
3f791afe | 6831 | return -1; |
4ef0d4d0 KS |
6832 | } |
6833 | ||
6834 | static toff_t | |
d3da34e0 | 6835 | tiff_seek_in_memory (thandle_t data, toff_t off, int whence) |
4ef0d4d0 KS |
6836 | { |
6837 | tiff_memory_source *src = (tiff_memory_source *) data; | |
3f791afe | 6838 | ptrdiff_t idx; |
4ef0d4d0 KS |
6839 | |
6840 | switch (whence) | |
6841 | { | |
6842 | case SEEK_SET: /* Go from beginning of source. */ | |
6843 | idx = off; | |
6844 | break; | |
6845 | ||
6846 | case SEEK_END: /* Go from end of source. */ | |
6847 | idx = src->len + off; | |
6848 | break; | |
6849 | ||
6850 | case SEEK_CUR: /* Go from current position. */ | |
6851 | idx = src->index + off; | |
6852 | break; | |
6853 | ||
6854 | default: /* Invalid `whence'. */ | |
6855 | return -1; | |
6856 | } | |
6857 | ||
6858 | if (idx > src->len || idx < 0) | |
6859 | return -1; | |
6860 | ||
6861 | src->index = idx; | |
6862 | return src->index; | |
6863 | } | |
6864 | ||
6865 | static int | |
d3da34e0 | 6866 | tiff_close_memory (thandle_t data) |
4ef0d4d0 KS |
6867 | { |
6868 | /* NOOP */ | |
6869 | return 0; | |
6870 | } | |
6871 | ||
6872 | static int | |
d3da34e0 | 6873 | tiff_mmap_memory (thandle_t data, tdata_t *pbase, toff_t *psize) |
4ef0d4d0 KS |
6874 | { |
6875 | /* It is already _IN_ memory. */ | |
6876 | return 0; | |
6877 | } | |
6878 | ||
6879 | static void | |
d3da34e0 | 6880 | tiff_unmap_memory (thandle_t data, tdata_t base, toff_t size) |
4ef0d4d0 KS |
6881 | { |
6882 | /* We don't need to do this. */ | |
6883 | } | |
6884 | ||
6885 | static toff_t | |
d3da34e0 | 6886 | tiff_size_of_memory (thandle_t data) |
4ef0d4d0 KS |
6887 | { |
6888 | return ((tiff_memory_source *) data)->len; | |
6889 | } | |
6890 | ||
b41c3a35 EZ |
6891 | /* GCC 3.x on x86 Windows targets has a bug that triggers an internal |
6892 | compiler error compiling tiff_handler, see Bugzilla bug #17406 | |
6893 | (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring | |
6894 | this function as external works around that problem. */ | |
6895 | #if defined (__MINGW32__) && __GNUC__ == 3 | |
6896 | # define MINGW_STATIC | |
6897 | #else | |
6898 | # define MINGW_STATIC static | |
6899 | #endif | |
4ef0d4d0 | 6900 | |
b41c3a35 EZ |
6901 | MINGW_STATIC void |
6902 | tiff_handler (const char *, const char *, const char *, va_list) | |
13464394 | 6903 | ATTRIBUTE_FORMAT_PRINTF (3, 0); |
b41c3a35 | 6904 | MINGW_STATIC void |
13464394 PE |
6905 | tiff_handler (const char *log_format, const char *title, |
6906 | const char *format, va_list ap) | |
6907 | { | |
6908 | /* doprnt is not suitable here, as TIFF handlers are called from | |
6909 | libtiff and are passed arbitrary printf directives. Instead, use | |
6910 | vsnprintf, taking care to be portable to nonstandard environments | |
6911 | where vsnprintf returns -1 on buffer overflow. Since it's just a | |
6912 | log entry, it's OK to truncate it. */ | |
6913 | char buf[4000]; | |
6914 | int len = vsnprintf (buf, sizeof buf, format, ap); | |
6915 | add_to_log (log_format, build_string (title), | |
6916 | make_string (buf, max (0, min (len, sizeof buf - 1)))); | |
6917 | } | |
b41c3a35 | 6918 | #undef MINGW_STATIC |
13464394 | 6919 | |
5e2d4a30 PE |
6920 | static void tiff_error_handler (const char *, const char *, va_list) |
6921 | ATTRIBUTE_FORMAT_PRINTF (2, 0); | |
4ef0d4d0 | 6922 | static void |
d3da34e0 | 6923 | tiff_error_handler (const char *title, const char *format, va_list ap) |
4ef0d4d0 | 6924 | { |
13464394 | 6925 | tiff_handler ("TIFF error: %s %s", title, format, ap); |
4ef0d4d0 KS |
6926 | } |
6927 | ||
6928 | ||
5e2d4a30 PE |
6929 | static void tiff_warning_handler (const char *, const char *, va_list) |
6930 | ATTRIBUTE_FORMAT_PRINTF (2, 0); | |
4ef0d4d0 | 6931 | static void |
d3da34e0 | 6932 | tiff_warning_handler (const char *title, const char *format, va_list ap) |
4ef0d4d0 | 6933 | { |
13464394 | 6934 | tiff_handler ("TIFF warning: %s %s", title, format, ap); |
4ef0d4d0 KS |
6935 | } |
6936 | ||
6937 | ||
578098f3 | 6938 | /* Load TIFF image IMG for use on frame F. Value is true if |
4ef0d4d0 KS |
6939 | successful. */ |
6940 | ||
578098f3 | 6941 | static bool |
d3da34e0 | 6942 | tiff_load (struct frame *f, struct image *img) |
4ef0d4d0 KS |
6943 | { |
6944 | Lisp_Object file, specified_file; | |
6945 | Lisp_Object specified_data; | |
6946 | TIFF *tiff; | |
58279503 | 6947 | int width, height, x, y, count; |
4ef0d4d0 | 6948 | uint32 *buf; |
03d0a109 | 6949 | int rc; |
4ef0d4d0 | 6950 | XImagePtr ximg; |
4ef0d4d0 | 6951 | tiff_memory_source memsrc; |
58279503 | 6952 | Lisp_Object image; |
4ef0d4d0 KS |
6953 | |
6954 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6955 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
4ef0d4d0 | 6956 | |
5e2d4a30 PE |
6957 | fn_TIFFSetErrorHandler ((TIFFErrorHandler) tiff_error_handler); |
6958 | fn_TIFFSetWarningHandler ((TIFFErrorHandler) tiff_warning_handler); | |
4ef0d4d0 KS |
6959 | |
6960 | if (NILP (specified_data)) | |
6961 | { | |
6962 | /* Read from a file */ | |
6963 | file = x_find_image_file (specified_file); | |
6964 | if (!STRINGP (file)) | |
6965 | { | |
6966 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
6967 | return 0; |
6968 | } | |
6969 | ||
df61c790 | 6970 | /* Try to open the image file. */ |
b19beacc | 6971 | tiff = fn_TIFFOpen (SSDATA (file), "r"); |
4ef0d4d0 KS |
6972 | if (tiff == NULL) |
6973 | { | |
6974 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
6975 | return 0; |
6976 | } | |
6977 | } | |
6978 | else | |
6979 | { | |
7574650a AS |
6980 | if (!STRINGP (specified_data)) |
6981 | { | |
6982 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
6983 | return 0; | |
6984 | } | |
6985 | ||
4ef0d4d0 KS |
6986 | /* Memory source! */ |
6987 | memsrc.bytes = SDATA (specified_data); | |
6988 | memsrc.len = SBYTES (specified_data); | |
6989 | memsrc.index = 0; | |
6990 | ||
3082f617 | 6991 | tiff = fn_TIFFClientOpen ("memory_source", "r", (thandle_t)&memsrc, |
3f791afe PE |
6992 | tiff_read_from_memory, |
6993 | tiff_write_from_memory, | |
df61c790 AS |
6994 | tiff_seek_in_memory, |
6995 | tiff_close_memory, | |
6996 | tiff_size_of_memory, | |
6997 | tiff_mmap_memory, | |
6998 | tiff_unmap_memory); | |
4ef0d4d0 KS |
6999 | |
7000 | if (!tiff) | |
7001 | { | |
7002 | image_error ("Cannot open memory source for `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
7003 | return 0; |
7004 | } | |
7005 | } | |
7006 | ||
58279503 JR |
7007 | image = image_spec_value (img->spec, QCindex, NULL); |
7008 | if (INTEGERP (image)) | |
7009 | { | |
13464394 PE |
7010 | EMACS_INT ino = XFASTINT (image); |
7011 | if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t) | |
7012 | && fn_TIFFSetDirectory (tiff, ino))) | |
58279503 JR |
7013 | { |
7014 | image_error ("Invalid image number `%s' in image `%s'", | |
7015 | image, img->spec); | |
7016 | fn_TIFFClose (tiff); | |
58279503 JR |
7017 | return 0; |
7018 | } | |
7019 | } | |
7020 | ||
4ef0d4d0 KS |
7021 | /* Get width and height of the image, and allocate a raster buffer |
7022 | of width x height 32-bit values. */ | |
7023 | fn_TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width); | |
7024 | fn_TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height); | |
f1f25b99 CY |
7025 | |
7026 | if (!check_image_size (f, width, height)) | |
7027 | { | |
10ea2b82 | 7028 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
58279503 | 7029 | fn_TIFFClose (tiff); |
f1f25b99 CY |
7030 | return 0; |
7031 | } | |
7032 | ||
ca4aa935 PE |
7033 | /* Create the X image and pixmap. */ |
7034 | if (! (height <= min (PTRDIFF_MAX, SIZE_MAX) / sizeof *buf / width | |
547c9269 YM |
7035 | && image_create_x_image_and_pixmap (f, img, width, height, 0, |
7036 | &ximg, 0))) | |
ca4aa935 PE |
7037 | { |
7038 | fn_TIFFClose (tiff); | |
7039 | return 0; | |
7040 | } | |
7041 | ||
23f86fce | 7042 | buf = xmalloc (sizeof *buf * width * height); |
4ef0d4d0 KS |
7043 | |
7044 | rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0); | |
58279503 JR |
7045 | |
7046 | /* Count the number of images in the file. */ | |
03d0a109 PE |
7047 | for (count = 1; fn_TIFFSetDirectory (tiff, count); count++) |
7048 | continue; | |
58279503 JR |
7049 | |
7050 | if (count > 1) | |
b50691aa CY |
7051 | img->lisp_data = Fcons (Qcount, |
7052 | Fcons (make_number (count), | |
7053 | img->lisp_data)); | |
58279503 | 7054 | |
4ef0d4d0 KS |
7055 | fn_TIFFClose (tiff); |
7056 | if (!rc) | |
7057 | { | |
7058 | image_error ("Error reading TIFF image `%s'", img->spec, Qnil); | |
7059 | xfree (buf); | |
4ef0d4d0 KS |
7060 | return 0; |
7061 | } | |
7062 | ||
4ef0d4d0 KS |
7063 | /* Initialize the color table. */ |
7064 | init_color_table (); | |
7065 | ||
7066 | /* Process the pixel raster. Origin is in the lower-left corner. */ | |
7067 | for (y = 0; y < height; ++y) | |
7068 | { | |
7069 | uint32 *row = buf + y * width; | |
7070 | ||
7071 | for (x = 0; x < width; ++x) | |
7072 | { | |
7073 | uint32 abgr = row[x]; | |
7074 | int r = TIFFGetR (abgr) << 8; | |
7075 | int g = TIFFGetG (abgr) << 8; | |
7076 | int b = TIFFGetB (abgr) << 8; | |
7077 | XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b)); | |
7078 | } | |
7079 | } | |
7080 | ||
7081 | #ifdef COLOR_TABLE_SUPPORT | |
7082 | /* Remember the colors allocated for the image. Free the color table. */ | |
7083 | img->colors = colors_in_color_table (&img->ncolors); | |
7084 | free_color_table (); | |
7085 | #endif /* COLOR_TABLE_SUPPORT */ | |
7086 | ||
7087 | img->width = width; | |
7088 | img->height = height; | |
7089 | ||
7090 | /* Maybe fill in the background field while we have ximg handy. */ | |
7091 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7092 | /* Casting avoids a GCC warning on W32. */ |
7093 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 7094 | |
547c9269 YM |
7095 | /* Put ximg into the image. */ |
7096 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
7097 | xfree (buf); |
7098 | ||
4ef0d4d0 KS |
7099 | return 1; |
7100 | } | |
7101 | ||
7102 | #else /* HAVE_TIFF */ | |
7103 | ||
edfda783 | 7104 | #ifdef HAVE_NS |
578098f3 | 7105 | static bool |
edfda783 AR |
7106 | tiff_load (struct frame *f, struct image *img) |
7107 | { | |
ed3751c8 JB |
7108 | return ns_load_image (f, img, |
7109 | image_spec_value (img->spec, QCfile, NULL), | |
7110 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
7111 | } |
7112 | #endif /* HAVE_NS */ | |
7113 | ||
4ef0d4d0 KS |
7114 | #endif /* !HAVE_TIFF */ |
7115 | ||
7116 | ||
7117 | \f | |
7118 | /*********************************************************************** | |
7119 | GIF | |
7120 | ***********************************************************************/ | |
7121 | ||
9e2a2647 | 7122 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
4ef0d4d0 | 7123 | |
578098f3 PE |
7124 | static bool gif_image_p (Lisp_Object object); |
7125 | static bool gif_load (struct frame *f, struct image *img); | |
f57e2426 | 7126 | static void gif_clear_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
7127 | |
7128 | /* The symbol `gif' identifying images of this type. */ | |
7129 | ||
955cbe7b | 7130 | static Lisp_Object Qgif; |
4ef0d4d0 KS |
7131 | |
7132 | /* Indices of image specification fields in gif_format, below. */ | |
7133 | ||
7134 | enum gif_keyword_index | |
7135 | { | |
7136 | GIF_TYPE, | |
7137 | GIF_DATA, | |
7138 | GIF_FILE, | |
7139 | GIF_ASCENT, | |
7140 | GIF_MARGIN, | |
7141 | GIF_RELIEF, | |
7142 | GIF_ALGORITHM, | |
7143 | GIF_HEURISTIC_MASK, | |
7144 | GIF_MASK, | |
7145 | GIF_IMAGE, | |
7146 | GIF_BACKGROUND, | |
7147 | GIF_LAST | |
7148 | }; | |
7149 | ||
7150 | /* Vector of image_keyword structures describing the format | |
7151 | of valid user-defined image specifications. */ | |
7152 | ||
91433552 | 7153 | static const struct image_keyword gif_format[GIF_LAST] = |
4ef0d4d0 KS |
7154 | { |
7155 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7156 | {":data", IMAGE_STRING_VALUE, 0}, | |
7157 | {":file", IMAGE_STRING_VALUE, 0}, | |
7158 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 7159 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
7160 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
7161 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7162 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7163 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 | 7164 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}, |
4ef0d4d0 KS |
7165 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} |
7166 | }; | |
7167 | ||
a9e7a9d5 | 7168 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 7169 | static bool init_gif_functions (void); |
bb4d86b4 CY |
7170 | #else |
7171 | #define init_gif_functions NULL | |
7172 | #endif | |
7173 | ||
4ef0d4d0 KS |
7174 | /* Structure describing the image type `gif'. */ |
7175 | ||
7176 | static struct image_type gif_type = | |
7177 | { | |
7178 | &Qgif, | |
7179 | gif_image_p, | |
7180 | gif_load, | |
6ac3c7bb | 7181 | gif_clear_image, |
bb4d86b4 | 7182 | init_gif_functions, |
4ef0d4d0 KS |
7183 | NULL |
7184 | }; | |
7185 | ||
6ac3c7bb KS |
7186 | /* Free X resources of GIF image IMG which is used on frame F. */ |
7187 | ||
7188 | static void | |
d3da34e0 | 7189 | gif_clear_image (struct frame *f, struct image *img) |
6ac3c7bb | 7190 | { |
b50691aa | 7191 | img->lisp_data = Qnil; |
6ac3c7bb KS |
7192 | x_clear_image (f, img); |
7193 | } | |
7194 | ||
578098f3 | 7195 | /* Return true if OBJECT is a valid GIF image specification. */ |
4ef0d4d0 | 7196 | |
578098f3 | 7197 | static bool |
d3da34e0 | 7198 | gif_image_p (Lisp_Object object) |
4ef0d4d0 KS |
7199 | { |
7200 | struct image_keyword fmt[GIF_LAST]; | |
72af86bd | 7201 | memcpy (fmt, gif_format, sizeof fmt); |
4ef0d4d0 KS |
7202 | |
7203 | if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)) | |
7204 | return 0; | |
7205 | ||
7206 | /* Must specify either the :data or :file keyword. */ | |
7207 | return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1; | |
7208 | } | |
7209 | ||
9e2a2647 | 7210 | #endif /* HAVE_GIF */ |
4ef0d4d0 KS |
7211 | |
7212 | #ifdef HAVE_GIF | |
7213 | ||
9e2a2647 | 7214 | #if defined (HAVE_NTGUI) |
2e09fef1 EZ |
7215 | /* winuser.h might define DrawText to DrawTextA or DrawTextW. |
7216 | Undefine before redefining to avoid a preprocessor warning. */ | |
7217 | #ifdef DrawText | |
7218 | #undef DrawText | |
7219 | #endif | |
4ef0d4d0 KS |
7220 | /* avoid conflict with QuickdrawText.h */ |
7221 | #define DrawText gif_DrawText | |
7222 | #include <gif_lib.h> | |
7223 | #undef DrawText | |
7224 | ||
9e2a2647 | 7225 | #else /* HAVE_NTGUI */ |
4ef0d4d0 KS |
7226 | |
7227 | #include <gif_lib.h> | |
7228 | ||
9e2a2647 | 7229 | #endif /* HAVE_NTGUI */ |
4ef0d4d0 KS |
7230 | |
7231 | ||
0fda9b75 | 7232 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
7233 | |
7234 | /* GIF library details. */ | |
14beddf4 CY |
7235 | DEF_IMGLIB_FN (int, DGifCloseFile, (GifFileType *)); |
7236 | DEF_IMGLIB_FN (int, DGifSlurp, (GifFileType *)); | |
7237 | DEF_IMGLIB_FN (GifFileType *, DGifOpen, (void *, InputFunc)); | |
7238 | DEF_IMGLIB_FN (GifFileType *, DGifOpenFileName, (const char *)); | |
4ef0d4d0 | 7239 | |
578098f3 | 7240 | static bool |
d07ff9db | 7241 | init_gif_functions (void) |
4ef0d4d0 KS |
7242 | { |
7243 | HMODULE library; | |
7244 | ||
d07ff9db | 7245 | if (!(library = w32_delayed_load (Qgif))) |
4ef0d4d0 KS |
7246 | return 0; |
7247 | ||
7248 | LOAD_IMGLIB_FN (library, DGifCloseFile); | |
7249 | LOAD_IMGLIB_FN (library, DGifSlurp); | |
7250 | LOAD_IMGLIB_FN (library, DGifOpen); | |
7251 | LOAD_IMGLIB_FN (library, DGifOpenFileName); | |
7252 | return 1; | |
7253 | } | |
7254 | ||
7255 | #else | |
7256 | ||
7257 | #define fn_DGifCloseFile DGifCloseFile | |
7258 | #define fn_DGifSlurp DGifSlurp | |
7259 | #define fn_DGifOpen DGifOpen | |
7260 | #define fn_DGifOpenFileName DGifOpenFileName | |
7261 | ||
0fda9b75 | 7262 | #endif /* WINDOWSNT */ |
4ef0d4d0 KS |
7263 | |
7264 | /* Reading a GIF image from memory | |
7265 | Based on the PNG memory stuff to a certain extent. */ | |
7266 | ||
7267 | typedef struct | |
7268 | { | |
7269 | unsigned char *bytes; | |
3f791afe PE |
7270 | ptrdiff_t len; |
7271 | ptrdiff_t index; | |
4ef0d4d0 KS |
7272 | } |
7273 | gif_memory_source; | |
7274 | ||
7275 | /* Make the current memory source available to gif_read_from_memory. | |
7276 | It's done this way because not all versions of libungif support | |
7277 | a UserData field in the GifFileType structure. */ | |
7278 | static gif_memory_source *current_gif_memory_src; | |
7279 | ||
7280 | static int | |
d3da34e0 | 7281 | gif_read_from_memory (GifFileType *file, GifByteType *buf, int len) |
4ef0d4d0 KS |
7282 | { |
7283 | gif_memory_source *src = current_gif_memory_src; | |
7284 | ||
7285 | if (len > src->len - src->index) | |
7286 | return -1; | |
7287 | ||
72af86bd | 7288 | memcpy (buf, src->bytes + src->index, len); |
4ef0d4d0 KS |
7289 | src->index += len; |
7290 | return len; | |
7291 | } | |
7292 | ||
7293 | ||
578098f3 | 7294 | /* Load GIF image IMG for use on frame F. Value is true if |
4ef0d4d0 KS |
7295 | successful. */ |
7296 | ||
91433552 DN |
7297 | static const int interlace_start[] = {0, 4, 2, 1}; |
7298 | static const int interlace_increment[] = {8, 8, 4, 2}; | |
aca946f3 | 7299 | |
17c0c952 JD |
7300 | #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249 |
7301 | ||
578098f3 | 7302 | static bool |
d3da34e0 | 7303 | gif_load (struct frame *f, struct image *img) |
4ef0d4d0 | 7304 | { |
60002bf5 CY |
7305 | Lisp_Object file; |
7306 | int rc, width, height, x, y, i, j; | |
4ef0d4d0 KS |
7307 | XImagePtr ximg; |
7308 | ColorMapObject *gif_color_map; | |
7309 | unsigned long pixel_colors[256]; | |
7310 | GifFileType *gif; | |
4ef0d4d0 | 7311 | gif_memory_source memsrc; |
60002bf5 CY |
7312 | Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL); |
7313 | Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL); | |
7314 | Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL); | |
7315 | unsigned long bgcolor = 0; | |
13464394 | 7316 | EMACS_INT idx; |
4ef0d4d0 KS |
7317 | |
7318 | if (NILP (specified_data)) | |
7319 | { | |
7320 | file = x_find_image_file (specified_file); | |
7321 | if (!STRINGP (file)) | |
7322 | { | |
7323 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
7324 | return 0; |
7325 | } | |
7326 | ||
df61c790 | 7327 | /* Open the GIF file. */ |
2037898d | 7328 | gif = fn_DGifOpenFileName (SSDATA (file)); |
4ef0d4d0 KS |
7329 | if (gif == NULL) |
7330 | { | |
7331 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
7332 | return 0; |
7333 | } | |
7334 | } | |
7335 | else | |
7336 | { | |
7574650a AS |
7337 | if (!STRINGP (specified_data)) |
7338 | { | |
7339 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
7340 | return 0; | |
7341 | } | |
7342 | ||
4ef0d4d0 KS |
7343 | /* Read from memory! */ |
7344 | current_gif_memory_src = &memsrc; | |
7345 | memsrc.bytes = SDATA (specified_data); | |
7346 | memsrc.len = SBYTES (specified_data); | |
7347 | memsrc.index = 0; | |
7348 | ||
df61c790 | 7349 | gif = fn_DGifOpen (&memsrc, gif_read_from_memory); |
4ef0d4d0 KS |
7350 | if (!gif) |
7351 | { | |
7352 | image_error ("Cannot open memory source `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
7353 | return 0; |
7354 | } | |
7355 | } | |
7356 | ||
f1f25b99 CY |
7357 | /* Before reading entire contents, check the declared image size. */ |
7358 | if (!check_image_size (f, gif->SWidth, gif->SHeight)) | |
7359 | { | |
10ea2b82 | 7360 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 | 7361 | fn_DGifCloseFile (gif); |
f1f25b99 CY |
7362 | return 0; |
7363 | } | |
7364 | ||
4ef0d4d0 KS |
7365 | /* Read entire contents. */ |
7366 | rc = fn_DGifSlurp (gif); | |
60002bf5 | 7367 | if (rc == GIF_ERROR || gif->ImageCount <= 0) |
4ef0d4d0 KS |
7368 | { |
7369 | image_error ("Error reading `%s'", img->spec, Qnil); | |
7370 | fn_DGifCloseFile (gif); | |
4ef0d4d0 KS |
7371 | return 0; |
7372 | } | |
7373 | ||
60002bf5 CY |
7374 | /* Which sub-image are we to display? */ |
7375 | { | |
33290528 PE |
7376 | Lisp_Object image_number = image_spec_value (img->spec, QCindex, NULL); |
7377 | idx = INTEGERP (image_number) ? XFASTINT (image_number) : 0; | |
60002bf5 | 7378 | if (idx < 0 || idx >= gif->ImageCount) |
17c0c952 | 7379 | { |
60002bf5 | 7380 | image_error ("Invalid image number `%s' in image `%s'", |
33290528 | 7381 | image_number, img->spec); |
60002bf5 CY |
7382 | fn_DGifCloseFile (gif); |
7383 | return 0; | |
17c0c952 | 7384 | } |
60002bf5 | 7385 | } |
17c0c952 | 7386 | |
e013fb34 CY |
7387 | width = img->width = gif->SWidth; |
7388 | height = img->height = gif->SHeight; | |
0f4aca46 | 7389 | |
e013fb34 CY |
7390 | img->corners[TOP_CORNER] = gif->SavedImages[0].ImageDesc.Top; |
7391 | img->corners[LEFT_CORNER] = gif->SavedImages[0].ImageDesc.Left; | |
8d903f4e AS |
7392 | img->corners[BOT_CORNER] |
7393 | = img->corners[TOP_CORNER] + gif->SavedImages[0].ImageDesc.Height; | |
7394 | img->corners[RIGHT_CORNER] | |
7395 | = img->corners[LEFT_CORNER] + gif->SavedImages[0].ImageDesc.Width; | |
4ef0d4d0 | 7396 | |
f1f25b99 CY |
7397 | if (!check_image_size (f, width, height)) |
7398 | { | |
10ea2b82 | 7399 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 | 7400 | fn_DGifCloseFile (gif); |
f1f25b99 CY |
7401 | return 0; |
7402 | } | |
7403 | ||
44aa9ee6 PE |
7404 | /* Check that the selected subimages fit. It's not clear whether |
7405 | the GIF spec requires this, but Emacs can crash if they don't fit. */ | |
7406 | for (j = 0; j <= idx; ++j) | |
7407 | { | |
7408 | struct SavedImage *subimage = gif->SavedImages + j; | |
7409 | int subimg_width = subimage->ImageDesc.Width; | |
7410 | int subimg_height = subimage->ImageDesc.Height; | |
7411 | int subimg_top = subimage->ImageDesc.Top; | |
7412 | int subimg_left = subimage->ImageDesc.Left; | |
7413 | if (! (0 <= subimg_width && 0 <= subimg_height | |
7414 | && 0 <= subimg_top && subimg_top <= height - subimg_height | |
7415 | && 0 <= subimg_left && subimg_left <= width - subimg_width)) | |
7416 | { | |
7417 | image_error ("Subimage does not fit in image", Qnil, Qnil); | |
7418 | fn_DGifCloseFile (gif); | |
7419 | return 0; | |
7420 | } | |
7421 | } | |
7422 | ||
4ef0d4d0 | 7423 | /* Create the X image and pixmap. */ |
547c9269 | 7424 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
4ef0d4d0 KS |
7425 | { |
7426 | fn_DGifCloseFile (gif); | |
4ef0d4d0 KS |
7427 | return 0; |
7428 | } | |
7429 | ||
60002bf5 CY |
7430 | /* Clear the part of the screen image not covered by the image. |
7431 | Full animated GIF support requires more here (see the gif89 spec, | |
7432 | disposal methods). Let's simply assume that the part not covered | |
7433 | by a sub-image is in the frame's background color. */ | |
6ac3c7bb | 7434 | for (y = 0; y < img->corners[TOP_CORNER]; ++y) |
4ef0d4d0 KS |
7435 | for (x = 0; x < width; ++x) |
7436 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7437 | ||
6ac3c7bb | 7438 | for (y = img->corners[BOT_CORNER]; y < height; ++y) |
4ef0d4d0 KS |
7439 | for (x = 0; x < width; ++x) |
7440 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7441 | ||
6ac3c7bb | 7442 | for (y = img->corners[TOP_CORNER]; y < img->corners[BOT_CORNER]; ++y) |
4ef0d4d0 | 7443 | { |
6ac3c7bb | 7444 | for (x = 0; x < img->corners[LEFT_CORNER]; ++x) |
4ef0d4d0 | 7445 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
6ac3c7bb | 7446 | for (x = img->corners[RIGHT_CORNER]; x < width; ++x) |
4ef0d4d0 KS |
7447 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
7448 | } | |
7449 | ||
60002bf5 | 7450 | /* Read the GIF image into the X image. */ |
4ef0d4d0 | 7451 | |
60002bf5 CY |
7452 | /* FIXME: With the current implementation, loading an animated gif |
7453 | is quadratic in the number of animation frames, since each frame | |
7454 | is a separate struct image. We must provide a way for a single | |
7455 | gif_load call to construct and save all animation frames. */ | |
4ef0d4d0 | 7456 | |
60002bf5 CY |
7457 | init_color_table (); |
7458 | if (STRINGP (specified_bg)) | |
7459 | bgcolor = x_alloc_image_color (f, img, specified_bg, | |
7460 | FRAME_BACKGROUND_PIXEL (f)); | |
7461 | for (j = 0; j <= idx; ++j) | |
7462 | { | |
7463 | /* We use a local variable `raster' here because RasterBits is a | |
7464 | char *, which invites problems with bytes >= 0x80. */ | |
7465 | struct SavedImage *subimage = gif->SavedImages + j; | |
7466 | unsigned char *raster = (unsigned char *) subimage->RasterBits; | |
7467 | int transparency_color_index = -1; | |
7468 | int disposal = 0; | |
e013fb34 CY |
7469 | int subimg_width = subimage->ImageDesc.Width; |
7470 | int subimg_height = subimage->ImageDesc.Height; | |
7471 | int subimg_top = subimage->ImageDesc.Top; | |
7472 | int subimg_left = subimage->ImageDesc.Left; | |
60002bf5 CY |
7473 | |
7474 | /* Find the Graphic Control Extension block for this sub-image. | |
7475 | Extract the disposal method and transparency color. */ | |
7476 | for (i = 0; i < subimage->ExtensionBlockCount; i++) | |
4ef0d4d0 | 7477 | { |
60002bf5 | 7478 | ExtensionBlock *extblock = subimage->ExtensionBlocks + i; |
4ef0d4d0 | 7479 | |
60002bf5 CY |
7480 | if ((extblock->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION) |
7481 | && extblock->ByteCount == 4 | |
7482 | && extblock->Bytes[0] & 1) | |
4ef0d4d0 | 7483 | { |
60002bf5 CY |
7484 | /* From gif89a spec: 1 = "keep in place", 2 = "restore |
7485 | to background". Treat any other value like 2. */ | |
7486 | disposal = (extblock->Bytes[0] >> 2) & 7; | |
2547adb1 | 7487 | transparency_color_index = (unsigned char) extblock->Bytes[3]; |
60002bf5 | 7488 | break; |
4ef0d4d0 | 7489 | } |
4ef0d4d0 | 7490 | } |
60002bf5 CY |
7491 | |
7492 | /* We can't "keep in place" the first subimage. */ | |
7493 | if (j == 0) | |
7494 | disposal = 2; | |
7495 | ||
e013fb34 CY |
7496 | /* For disposal == 0, the spec says "No disposal specified. The |
7497 | decoder is not required to take any action." In practice, it | |
7498 | seems we need to treat this like "keep in place", see e.g. | |
7499 | http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */ | |
7500 | if (disposal == 0) | |
7501 | disposal = 1; | |
7502 | ||
60002bf5 CY |
7503 | /* Allocate subimage colors. */ |
7504 | memset (pixel_colors, 0, sizeof pixel_colors); | |
7505 | gif_color_map = subimage->ImageDesc.ColorMap; | |
7506 | if (!gif_color_map) | |
7507 | gif_color_map = gif->SColorMap; | |
7508 | ||
7509 | if (gif_color_map) | |
7510 | for (i = 0; i < gif_color_map->ColorCount; ++i) | |
4ef0d4d0 | 7511 | { |
60002bf5 CY |
7512 | if (transparency_color_index == i) |
7513 | pixel_colors[i] = STRINGP (specified_bg) | |
7514 | ? bgcolor : FRAME_BACKGROUND_PIXEL (f); | |
7515 | else | |
7516 | { | |
7517 | int r = gif_color_map->Colors[i].Red << 8; | |
7518 | int g = gif_color_map->Colors[i].Green << 8; | |
7519 | int b = gif_color_map->Colors[i].Blue << 8; | |
7520 | pixel_colors[i] = lookup_rgb_color (f, r, g, b); | |
7521 | } | |
4ef0d4d0 | 7522 | } |
60002bf5 CY |
7523 | |
7524 | /* Apply the pixel values. */ | |
7525 | if (gif->SavedImages[j].ImageDesc.Interlace) | |
7526 | { | |
7527 | int row, pass; | |
7528 | ||
7529 | for (y = 0, row = interlace_start[0], pass = 0; | |
e013fb34 | 7530 | y < subimg_height; |
60002bf5 CY |
7531 | y++, row += interlace_increment[pass]) |
7532 | { | |
a9ebfa0b | 7533 | while (subimg_height <= row) |
60002bf5 | 7534 | { |
a9ebfa0b | 7535 | lint_assume (pass < 3); |
60002bf5 | 7536 | row = interlace_start[++pass]; |
60002bf5 CY |
7537 | } |
7538 | ||
e013fb34 | 7539 | for (x = 0; x < subimg_width; x++) |
60002bf5 | 7540 | { |
e013fb34 | 7541 | int c = raster[y * subimg_width + x]; |
60002bf5 | 7542 | if (transparency_color_index != c || disposal != 1) |
e013fb34 CY |
7543 | XPutPixel (ximg, x + subimg_left, row + subimg_top, |
7544 | pixel_colors[c]); | |
60002bf5 CY |
7545 | } |
7546 | } | |
7547 | } | |
7548 | else | |
7549 | { | |
e013fb34 CY |
7550 | for (y = 0; y < subimg_height; ++y) |
7551 | for (x = 0; x < subimg_width; ++x) | |
60002bf5 | 7552 | { |
e013fb34 | 7553 | int c = raster[y * subimg_width + x]; |
60002bf5 | 7554 | if (transparency_color_index != c || disposal != 1) |
e013fb34 CY |
7555 | XPutPixel (ximg, x + subimg_left, y + subimg_top, |
7556 | pixel_colors[c]); | |
60002bf5 CY |
7557 | } |
7558 | } | |
4ef0d4d0 KS |
7559 | } |
7560 | ||
60002bf5 CY |
7561 | #ifdef COLOR_TABLE_SUPPORT |
7562 | img->colors = colors_in_color_table (&img->ncolors); | |
7563 | free_color_table (); | |
7564 | #endif /* COLOR_TABLE_SUPPORT */ | |
7565 | ||
1546c559 JL |
7566 | /* Save GIF image extension data for `image-metadata'. |
7567 | Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */ | |
b50691aa | 7568 | img->lisp_data = Qnil; |
60002bf5 | 7569 | if (gif->SavedImages[idx].ExtensionBlockCount > 0) |
6ac3c7bb | 7570 | { |
13464394 | 7571 | int delay = 0; |
60002bf5 CY |
7572 | ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks; |
7573 | for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++) | |
6ac3c7bb | 7574 | /* Append (... FUNCTION "BYTES") */ |
1100a63c CY |
7575 | { |
7576 | img->lisp_data | |
7577 | = Fcons (make_number (ext->Function), | |
7578 | Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount), | |
7579 | img->lisp_data)); | |
7580 | if (ext->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION | |
7581 | && ext->ByteCount == 4) | |
7582 | { | |
7583 | delay = ext->Bytes[2] << CHAR_BIT; | |
7584 | delay |= ext->Bytes[1]; | |
7585 | } | |
7586 | } | |
b50691aa | 7587 | img->lisp_data = Fcons (Qextension_data, |
1100a63c CY |
7588 | Fcons (img->lisp_data, Qnil)); |
7589 | if (delay) | |
7590 | img->lisp_data | |
7591 | = Fcons (Qdelay, | |
13464394 | 7592 | Fcons (make_float (delay / 100.0), |
1100a63c | 7593 | img->lisp_data)); |
6ac3c7bb | 7594 | } |
1100a63c | 7595 | |
6ac3c7bb | 7596 | if (gif->ImageCount > 1) |
b50691aa CY |
7597 | img->lisp_data = Fcons (Qcount, |
7598 | Fcons (make_number (gif->ImageCount), | |
7599 | img->lisp_data)); | |
6ac3c7bb | 7600 | |
4ef0d4d0 KS |
7601 | fn_DGifCloseFile (gif); |
7602 | ||
7603 | /* Maybe fill in the background field while we have ximg handy. */ | |
7604 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7605 | /* Casting avoids a GCC warning. */ |
7606 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 7607 | |
547c9269 YM |
7608 | /* Put ximg into the image. */ |
7609 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 | 7610 | |
4ef0d4d0 KS |
7611 | return 1; |
7612 | } | |
7613 | ||
2e09fef1 | 7614 | #else /* !HAVE_GIF */ |
4ef0d4d0 | 7615 | |
edfda783 | 7616 | #ifdef HAVE_NS |
578098f3 | 7617 | static bool |
edfda783 AR |
7618 | gif_load (struct frame *f, struct image *img) |
7619 | { | |
ed3751c8 JB |
7620 | return ns_load_image (f, img, |
7621 | image_spec_value (img->spec, QCfile, NULL), | |
7622 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
7623 | } |
7624 | #endif /* HAVE_NS */ | |
7625 | ||
4ef0d4d0 KS |
7626 | #endif /* HAVE_GIF */ |
7627 | ||
7628 | ||
b9ed53d5 PE |
7629 | #ifdef HAVE_IMAGEMAGICK |
7630 | ||
7631 | /*********************************************************************** | |
7632 | ImageMagick | |
7633 | ***********************************************************************/ | |
7634 | ||
7635 | /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER, | |
7636 | safely rounded and clipped to int range. */ | |
7637 | ||
7638 | static int | |
7639 | scale_image_size (int size, size_t divisor, size_t multiplier) | |
7640 | { | |
7641 | if (divisor != 0) | |
7642 | { | |
7643 | double s = size; | |
7644 | double scaled = s * multiplier / divisor + 0.5; | |
7645 | if (scaled < INT_MAX) | |
7646 | return scaled; | |
7647 | } | |
7648 | return INT_MAX; | |
7649 | } | |
7650 | ||
7651 | /* Compute the desired size of an image with native size WIDTH x HEIGHT. | |
7652 | Use SPEC to deduce the size. Store the desired size into | |
7653 | *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */ | |
f3f9606c LMI |
7654 | static void |
7655 | compute_image_size (size_t width, size_t height, | |
7656 | Lisp_Object spec, | |
7657 | int *d_width, int *d_height) | |
7658 | { | |
7659 | Lisp_Object value; | |
7660 | int desired_width, desired_height; | |
7661 | ||
7662 | /* If width and/or height is set in the display spec assume we want | |
7663 | to scale to those values. If either h or w is unspecified, the | |
7664 | unspecified should be calculated from the specified to preserve | |
7665 | aspect ratio. */ | |
7666 | value = image_spec_value (spec, QCwidth, NULL); | |
b9ed53d5 | 7667 | desired_width = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1; |
f3f9606c | 7668 | value = image_spec_value (spec, QCheight, NULL); |
b9ed53d5 | 7669 | desired_height = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1; |
f3f9606c LMI |
7670 | |
7671 | if (desired_width == -1) | |
7672 | { | |
7673 | value = image_spec_value (spec, QCmax_width, NULL); | |
b9ed53d5 | 7674 | if (NATNUMP (value)) |
f3f9606c | 7675 | { |
b9ed53d5 PE |
7676 | int max_width = min (XFASTINT (value), INT_MAX); |
7677 | if (max_width < width) | |
f3f9606c | 7678 | { |
b9ed53d5 PE |
7679 | /* The image is wider than :max-width. */ |
7680 | desired_width = max_width; | |
7681 | if (desired_height == -1) | |
f3f9606c | 7682 | { |
b9ed53d5 PE |
7683 | desired_height = scale_image_size (desired_width, |
7684 | width, height); | |
7685 | value = image_spec_value (spec, QCmax_height, NULL); | |
7686 | if (NATNUMP (value)) | |
f3f9606c | 7687 | { |
b9ed53d5 PE |
7688 | int max_height = min (XFASTINT (value), INT_MAX); |
7689 | if (max_height < desired_height) | |
7690 | { | |
7691 | desired_height = max_height; | |
7692 | desired_width = scale_image_size (desired_height, | |
7693 | height, width); | |
7694 | } | |
f3f9606c LMI |
7695 | } |
7696 | } | |
f3f9606c LMI |
7697 | } |
7698 | } | |
7699 | } | |
7700 | ||
7701 | if (desired_height == -1) | |
7702 | { | |
7703 | value = image_spec_value (spec, QCmax_height, NULL); | |
b9ed53d5 PE |
7704 | if (NATNUMP (value)) |
7705 | { | |
7706 | int max_height = min (XFASTINT (value), INT_MAX); | |
7707 | if (max_height < height) | |
7708 | desired_height = max_height; | |
7709 | } | |
f3f9606c LMI |
7710 | } |
7711 | ||
7712 | if (desired_width != -1 && desired_height == -1) | |
7713 | /* w known, calculate h. */ | |
b9ed53d5 | 7714 | desired_height = scale_image_size (desired_width, width, height); |
f3f9606c LMI |
7715 | |
7716 | if (desired_width == -1 && desired_height != -1) | |
7717 | /* h known, calculate w. */ | |
b9ed53d5 | 7718 | desired_width = scale_image_size (desired_height, height, width); |
f3f9606c LMI |
7719 | |
7720 | *d_width = desired_width; | |
7721 | *d_height = desired_height; | |
7722 | } | |
7723 | ||
4b66faf3 | 7724 | static Lisp_Object Qimagemagick; |
a32d4040 | 7725 | |
578098f3 PE |
7726 | static bool imagemagick_image_p (Lisp_Object); |
7727 | static bool imagemagick_load (struct frame *, struct image *); | |
d66c4c7c CY |
7728 | static void imagemagick_clear_image (struct frame *, struct image *); |
7729 | ||
a32d4040 | 7730 | /* Indices of image specification fields in imagemagick_format. */ |
3de25479 JV |
7731 | |
7732 | enum imagemagick_keyword_index | |
4917006b JV |
7733 | { |
7734 | IMAGEMAGICK_TYPE, | |
7735 | IMAGEMAGICK_DATA, | |
7736 | IMAGEMAGICK_FILE, | |
7737 | IMAGEMAGICK_ASCENT, | |
7738 | IMAGEMAGICK_MARGIN, | |
7739 | IMAGEMAGICK_RELIEF, | |
7740 | IMAGEMAGICK_ALGORITHM, | |
7741 | IMAGEMAGICK_HEURISTIC_MASK, | |
7742 | IMAGEMAGICK_MASK, | |
7743 | IMAGEMAGICK_BACKGROUND, | |
c7f514b5 JV |
7744 | IMAGEMAGICK_HEIGHT, |
7745 | IMAGEMAGICK_WIDTH, | |
f3f9606c LMI |
7746 | IMAGEMAGICK_MAX_HEIGHT, |
7747 | IMAGEMAGICK_MAX_WIDTH, | |
c7f514b5 JV |
7748 | IMAGEMAGICK_ROTATION, |
7749 | IMAGEMAGICK_CROP, | |
4917006b JV |
7750 | IMAGEMAGICK_LAST |
7751 | }; | |
3de25479 JV |
7752 | |
7753 | /* Vector of image_keyword structures describing the format | |
7754 | of valid user-defined image specifications. */ | |
7755 | ||
7756 | static struct image_keyword imagemagick_format[IMAGEMAGICK_LAST] = | |
4917006b JV |
7757 | { |
7758 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7759 | {":data", IMAGE_STRING_VALUE, 0}, | |
7760 | {":file", IMAGE_STRING_VALUE, 0}, | |
7761 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 7762 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4917006b JV |
7763 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
7764 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7765 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7766 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
c7f514b5 | 7767 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, |
7574650a AS |
7768 | {":height", IMAGE_INTEGER_VALUE, 0}, |
7769 | {":width", IMAGE_INTEGER_VALUE, 0}, | |
f3f9606c LMI |
7770 | {":max-height", IMAGE_INTEGER_VALUE, 0}, |
7771 | {":max-width", IMAGE_INTEGER_VALUE, 0}, | |
7574650a | 7772 | {":rotation", IMAGE_NUMBER_VALUE, 0}, |
c7f514b5 | 7773 | {":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0} |
4917006b | 7774 | }; |
a32d4040 | 7775 | |
33d4113c | 7776 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 7777 | static bool init_imagemagick_functions (void); |
bb4d86b4 CY |
7778 | #else |
7779 | #define init_imagemagick_functions NULL | |
7780 | #endif | |
7781 | ||
d66c4c7c CY |
7782 | /* Structure describing the image type for any image handled via |
7783 | ImageMagick. */ | |
7784 | ||
7785 | static struct image_type imagemagick_type = | |
7786 | { | |
7787 | &Qimagemagick, | |
7788 | imagemagick_image_p, | |
7789 | imagemagick_load, | |
7790 | imagemagick_clear_image, | |
bb4d86b4 | 7791 | init_imagemagick_functions, |
d66c4c7c CY |
7792 | NULL |
7793 | }; | |
7794 | ||
3de25479 JV |
7795 | /* Free X resources of imagemagick image IMG which is used on frame F. */ |
7796 | ||
7797 | static void | |
4917006b JV |
7798 | imagemagick_clear_image (struct frame *f, |
7799 | struct image *img) | |
3de25479 | 7800 | { |
3de25479 JV |
7801 | x_clear_image (f, img); |
7802 | } | |
7803 | ||
578098f3 | 7804 | /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do |
3de25479 JV |
7805 | this by calling parse_image_spec and supplying the keywords that |
7806 | identify the IMAGEMAGICK format. */ | |
7807 | ||
578098f3 | 7808 | static bool |
4917006b | 7809 | imagemagick_image_p (Lisp_Object object) |
3de25479 JV |
7810 | { |
7811 | struct image_keyword fmt[IMAGEMAGICK_LAST]; | |
dbfe4532 | 7812 | memcpy (fmt, imagemagick_format, sizeof fmt); |
3de25479 JV |
7813 | |
7814 | if (!parse_image_spec (object, fmt, IMAGEMAGICK_LAST, Qimagemagick)) | |
7815 | return 0; | |
7816 | ||
7817 | /* Must specify either the :data or :file keyword. */ | |
7818 | return fmt[IMAGEMAGICK_FILE].count + fmt[IMAGEMAGICK_DATA].count == 1; | |
7819 | } | |
7820 | ||
7821 | /* The GIF library also defines DrawRectangle, but its never used in Emacs. | |
c7015153 | 7822 | Therefore rename the function so it doesn't collide with ImageMagick. */ |
3de25479 JV |
7823 | #define DrawRectangle DrawRectangleGif |
7824 | #include <wand/MagickWand.h> | |
7825 | ||
60737f02 PE |
7826 | /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason. |
7827 | Emacs seems to work fine with the hidden version, so unhide it. */ | |
7828 | #include <magick/version.h> | |
7829 | #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665 | |
7830 | extern WandExport void PixelGetMagickColor (const PixelWand *, | |
7831 | MagickPixelPacket *); | |
7832 | #endif | |
7833 | ||
d1d7b339 JL |
7834 | /* Log ImageMagick error message. |
7835 | Useful when a ImageMagick function returns the status `MagickFalse'. */ | |
7836 | ||
7837 | static void | |
7838 | imagemagick_error (MagickWand *wand) | |
7839 | { | |
7840 | char *description; | |
7841 | ExceptionType severity; | |
7842 | ||
7843 | description = MagickGetException (wand, &severity); | |
7844 | image_error ("ImageMagick error: %s", | |
1130ecfc | 7845 | build_string (description), |
d1d7b339 JL |
7846 | Qnil); |
7847 | description = (char *) MagickRelinquishMemory (description); | |
7848 | } | |
7849 | ||
d66c4c7c CY |
7850 | /* Helper function for imagemagick_load, which does the actual loading |
7851 | given contents and size, apart from frame and image structures, | |
7852 | passed from imagemagick_load. Uses librimagemagick to do most of | |
7853 | the image processing. | |
3de25479 | 7854 | |
d66c4c7c CY |
7855 | F is a pointer to the Emacs frame; IMG to the image structure to |
7856 | prepare; CONTENTS is the string containing the IMAGEMAGICK data to | |
7857 | be parsed; SIZE is the number of bytes of data; and FILENAME is | |
7858 | either the file name or the image data. | |
3de25479 | 7859 | |
578098f3 | 7860 | Return true if successful. */ |
3de25479 | 7861 | |
578098f3 | 7862 | static bool |
d66c4c7c CY |
7863 | imagemagick_load_image (struct frame *f, struct image *img, |
7864 | unsigned char *contents, unsigned int size, | |
33290528 | 7865 | char *filename) |
3de25479 | 7866 | { |
1b9b4cf4 | 7867 | size_t width, height; |
d66c4c7c | 7868 | MagickBooleanType status; |
3de25479 | 7869 | XImagePtr ximg; |
1b9b4cf4 CY |
7870 | int x, y; |
7871 | MagickWand *image_wand; | |
7872 | MagickWand *ping_wand; | |
3de25479 | 7873 | PixelIterator *iterator; |
1b9b4cf4 | 7874 | PixelWand **pixels, *bg_wand = NULL; |
3de25479 | 7875 | MagickPixelPacket pixel; |
4917006b | 7876 | Lisp_Object image; |
7574650a | 7877 | Lisp_Object value; |
33290528 | 7878 | Lisp_Object crop; |
13464394 | 7879 | EMACS_INT ino; |
4917006b JV |
7880 | int desired_width, desired_height; |
7881 | double rotation; | |
7574650a | 7882 | int pixelwidth; |
7574650a | 7883 | |
a32d4040 CY |
7884 | /* Handle image index for image types who can contain more than one image. |
7885 | Interface :index is same as for GIF. First we "ping" the image to see how | |
7886 | many sub-images it contains. Pinging is faster than loading the image to | |
7887 | find out things about it. */ | |
f853f599 | 7888 | |
a32d4040 | 7889 | /* Initialize the imagemagick environment. */ |
f853f599 | 7890 | MagickWandGenesis (); |
3de25479 JV |
7891 | image = image_spec_value (img->spec, QCindex, NULL); |
7892 | ino = INTEGERP (image) ? XFASTINT (image) : 0; | |
7574650a | 7893 | ping_wand = NewMagickWand (); |
d1d7b339 | 7894 | /* MagickSetResolution (ping_wand, 2, 2); (Bug#10112) */ |
fa4fdb5c | 7895 | |
1b9b4cf4 CY |
7896 | status = filename |
7897 | ? MagickPingImage (ping_wand, filename) | |
7898 | : MagickPingImageBlob (ping_wand, contents, size); | |
7574650a | 7899 | |
d1d7b339 JL |
7900 | if (status == MagickFalse) |
7901 | { | |
7902 | imagemagick_error (ping_wand); | |
7903 | DestroyMagickWand (ping_wand); | |
7904 | return 0; | |
7905 | } | |
fa4fdb5c | 7906 | |
1b9b4cf4 | 7907 | if (ino < 0 || ino >= MagickGetNumberImages (ping_wand)) |
7574650a AS |
7908 | { |
7909 | image_error ("Invalid image number `%s' in image `%s'", | |
7910 | image, img->spec); | |
7911 | DestroyMagickWand (ping_wand); | |
7912 | return 0; | |
f3700365 | 7913 | } |
431dab04 | 7914 | |
5e617bc2 | 7915 | if (MagickGetNumberImages (ping_wand) > 1) |
b50691aa | 7916 | img->lisp_data = |
d2a2d277 | 7917 | Fcons (Qcount, |
7574650a | 7918 | Fcons (make_number (MagickGetNumberImages (ping_wand)), |
b50691aa | 7919 | img->lisp_data)); |
f3700365 JV |
7920 | |
7921 | DestroyMagickWand (ping_wand); | |
f853f599 | 7922 | |
d66c4c7c | 7923 | /* Now we know how many images are inside the file. If it's not a |
1b9b4cf4 | 7924 | bundle, the number is one. Load the image data. */ |
f3700365 | 7925 | |
1b9b4cf4 | 7926 | image_wand = NewMagickWand (); |
f3700365 | 7927 | |
1b9b4cf4 CY |
7928 | if ((filename |
7929 | ? MagickReadImage (image_wand, filename) | |
7930 | : MagickReadImageBlob (image_wand, contents, size)) | |
7931 | == MagickFalse) | |
f3700365 | 7932 | { |
1b9b4cf4 CY |
7933 | imagemagick_error (image_wand); |
7934 | goto imagemagick_error; | |
f3700365 | 7935 | } |
f853f599 | 7936 | |
1b9b4cf4 CY |
7937 | /* Retrieve the frame's background color, for use later. */ |
7938 | { | |
7939 | XColor bgcolor; | |
7940 | Lisp_Object specified_bg; | |
7941 | ||
7942 | specified_bg = image_spec_value (img->spec, QCbackground, NULL); | |
7943 | if (!STRINGP (specified_bg) | |
7944 | || !x_defined_color (f, SSDATA (specified_bg), &bgcolor, 0)) | |
7945 | { | |
7946 | #ifndef HAVE_NS | |
7947 | bgcolor.pixel = FRAME_BACKGROUND_PIXEL (f); | |
7948 | x_query_color (f, &bgcolor); | |
7949 | #else | |
7950 | ns_query_color (FRAME_BACKGROUND_COLOR (f), &bgcolor, 1); | |
7951 | #endif | |
7952 | } | |
7953 | ||
7954 | bg_wand = NewPixelWand (); | |
7955 | PixelSetRed (bg_wand, (double) bgcolor.red / 65535); | |
7956 | PixelSetGreen (bg_wand, (double) bgcolor.green / 65535); | |
7957 | PixelSetBlue (bg_wand, (double) bgcolor.blue / 65535); | |
7958 | } | |
7959 | ||
f3f9606c LMI |
7960 | compute_image_size (MagickGetImageWidth (image_wand), |
7961 | MagickGetImageHeight (image_wand), | |
7962 | img->spec, &desired_width, &desired_height); | |
bdf6a35d | 7963 | |
a32d4040 | 7964 | if (desired_width != -1 && desired_height != -1) |
4917006b | 7965 | { |
7574650a AS |
7966 | status = MagickScaleImage (image_wand, desired_width, desired_height); |
7967 | if (status == MagickFalse) | |
7968 | { | |
7969 | image_error ("Imagemagick scale failed", Qnil, Qnil); | |
d1d7b339 | 7970 | imagemagick_error (image_wand); |
7574650a AS |
7971 | goto imagemagick_error; |
7972 | } | |
3de25479 | 7973 | } |
3de25479 | 7974 | |
f3700365 | 7975 | /* crop behaves similar to image slicing in Emacs but is more memory |
7574650a AS |
7976 | efficient. */ |
7977 | crop = image_spec_value (img->spec, QCcrop, NULL); | |
7978 | ||
13464394 | 7979 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop))) |
7574650a | 7980 | { |
a32d4040 CY |
7981 | /* After some testing, it seems MagickCropImage is the fastest crop |
7982 | function in ImageMagick. This crop function seems to do less copying | |
7983 | than the alternatives, but it still reads the entire image into memory | |
13464394 | 7984 | before cropping, which is apparently difficult to avoid when using |
a32d4040 | 7985 | imagemagick. */ |
13464394 | 7986 | size_t crop_width = XINT (XCAR (crop)); |
7574650a | 7987 | crop = XCDR (crop); |
13464394 | 7988 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop))) |
7574650a | 7989 | { |
13464394 | 7990 | size_t crop_height = XINT (XCAR (crop)); |
7574650a | 7991 | crop = XCDR (crop); |
13464394 | 7992 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop))) |
7574650a | 7993 | { |
13464394 | 7994 | ssize_t crop_x = XINT (XCAR (crop)); |
7574650a | 7995 | crop = XCDR (crop); |
13464394 | 7996 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop))) |
7574650a | 7997 | { |
13464394 PE |
7998 | ssize_t crop_y = XINT (XCAR (crop)); |
7999 | MagickCropImage (image_wand, crop_width, crop_height, | |
8000 | crop_x, crop_y); | |
7574650a AS |
8001 | } |
8002 | } | |
8003 | } | |
8004 | } | |
8005 | ||
d2a2d277 JV |
8006 | /* Furthermore :rotation. we need background color and angle for |
8007 | rotation. */ | |
3de25479 | 8008 | /* |
d2a2d277 JV |
8009 | TODO background handling for rotation specified_bg = |
8010 | image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP | |
549a73b9 | 8011 | (specified_bg). */ |
4917006b JV |
8012 | value = image_spec_value (img->spec, QCrotation, NULL); |
8013 | if (FLOATP (value)) | |
8014 | { | |
3de25479 | 8015 | rotation = extract_float (value); |
1b9b4cf4 | 8016 | status = MagickRotateImage (image_wand, bg_wand, rotation); |
4917006b JV |
8017 | if (status == MagickFalse) |
8018 | { | |
8019 | image_error ("Imagemagick image rotate failed", Qnil, Qnil); | |
d1d7b339 | 8020 | imagemagick_error (image_wand); |
4917006b JV |
8021 | goto imagemagick_error; |
8022 | } | |
3de25479 | 8023 | } |
7574650a | 8024 | |
1b9b4cf4 CY |
8025 | /* Set the canvas background color to the frame or specified |
8026 | background, and flatten the image. Note: as of ImageMagick | |
8027 | 6.6.0, SVG image transparency is not handled properly | |
8028 | (e.g. etc/images/splash.svg shows a white background always). */ | |
8029 | { | |
8030 | MagickWand *new_wand; | |
8031 | MagickSetImageBackgroundColor (image_wand, bg_wand); | |
0e25d334 | 8032 | #ifdef HAVE_MAGICKMERGEIMAGELAYERS |
1b9b4cf4 | 8033 | new_wand = MagickMergeImageLayers (image_wand, MergeLayer); |
0e25d334 CY |
8034 | #else |
8035 | new_wand = MagickFlattenImages (image_wand); | |
8036 | #endif | |
1b9b4cf4 CY |
8037 | DestroyMagickWand (image_wand); |
8038 | image_wand = new_wand; | |
8039 | } | |
8040 | ||
84ac6f9d PE |
8041 | /* Finally we are done manipulating the image. Figure out the |
8042 | resulting width/height and transfer ownership to Emacs. */ | |
8043 | height = MagickGetImageHeight (image_wand); | |
8044 | width = MagickGetImageWidth (image_wand); | |
8045 | ||
3f791afe PE |
8046 | if (! (width <= INT_MAX && height <= INT_MAX |
8047 | && check_image_size (f, width, height))) | |
3de25479 JV |
8048 | { |
8049 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
8050 | goto imagemagick_error; | |
8051 | } | |
7574650a | 8052 | |
3de25479 JV |
8053 | /* We can now get a valid pixel buffer from the imagemagick file, if all |
8054 | went ok. */ | |
3de25479 JV |
8055 | |
8056 | init_color_table (); | |
af008560 | 8057 | |
cf7a0de6 | 8058 | #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS) |
0e25d334 CY |
8059 | if (imagemagick_render_type != 0) |
8060 | { | |
8061 | /* Magicexportimage is normally faster than pixelpushing. This | |
8062 | method is also well tested. Some aspects of this method are | |
8063 | ad-hoc and needs to be more researched. */ | |
8064 | int imagedepth = 24; /*MagickGetImageDepth(image_wand);*/ | |
8065 | const char *exportdepth = imagedepth <= 8 ? "I" : "BGRP"; /*"RGBP";*/ | |
8066 | /* Try to create a x pixmap to hold the imagemagick pixmap. */ | |
547c9269 YM |
8067 | if (!image_create_x_image_and_pixmap (f, img, width, height, imagedepth, |
8068 | &ximg, 0)) | |
0e25d334 CY |
8069 | { |
8070 | #ifdef COLOR_TABLE_SUPPORT | |
8071 | free_color_table (); | |
8072 | #endif | |
8073 | image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil); | |
8074 | goto imagemagick_error; | |
8075 | } | |
8076 | ||
8077 | /* Oddly, the below code doesn't seem to work:*/ | |
8078 | /* switch(ximg->bitmap_unit){ */ | |
8079 | /* case 8: */ | |
8080 | /* pixelwidth=CharPixel; */ | |
8081 | /* break; */ | |
8082 | /* case 16: */ | |
8083 | /* pixelwidth=ShortPixel; */ | |
8084 | /* break; */ | |
8085 | /* case 32: */ | |
8086 | /* pixelwidth=LongPixel; */ | |
8087 | /* break; */ | |
8088 | /* } */ | |
8089 | /* | |
8090 | Here im just guessing the format of the bitmap. | |
8091 | happens to work fine for: | |
8092 | - bw djvu images | |
8093 | on rgb display. | |
8094 | seems about 3 times as fast as pixel pushing(not carefully measured) | |
8095 | */ | |
8096 | pixelwidth = CharPixel; /*??? TODO figure out*/ | |
8097 | MagickExportImagePixels (image_wand, 0, 0, width, height, | |
8098 | exportdepth, pixelwidth, ximg->data); | |
8099 | } | |
8100 | else | |
8101 | #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */ | |
4917006b | 8102 | { |
13464394 PE |
8103 | size_t image_height; |
8104 | ||
4917006b | 8105 | /* Try to create a x pixmap to hold the imagemagick pixmap. */ |
547c9269 YM |
8106 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, |
8107 | &ximg, 0)) | |
4917006b | 8108 | { |
466cbae9 AS |
8109 | #ifdef COLOR_TABLE_SUPPORT |
8110 | free_color_table (); | |
8111 | #endif | |
5e617bc2 | 8112 | image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil); |
4917006b JV |
8113 | goto imagemagick_error; |
8114 | } | |
7574650a | 8115 | |
53964682 | 8116 | /* Copy imagemagick image to x with primitive yet robust pixel |
4917006b | 8117 | pusher loop. This has been tested a lot with many different |
549a73b9 | 8118 | images. */ |
7574650a | 8119 | |
4917006b JV |
8120 | /* Copy pixels from the imagemagick image structure to the x image map. */ |
8121 | iterator = NewPixelIterator (image_wand); | |
7574650a | 8122 | if (iterator == (PixelIterator *) NULL) |
4917006b | 8123 | { |
466cbae9 AS |
8124 | #ifdef COLOR_TABLE_SUPPORT |
8125 | free_color_table (); | |
8126 | #endif | |
8127 | x_destroy_x_image (ximg); | |
d2a2d277 JV |
8128 | image_error ("Imagemagick pixel iterator creation failed", |
8129 | Qnil, Qnil); | |
3de25479 | 8130 | goto imagemagick_error; |
4917006b JV |
8131 | } |
8132 | ||
13464394 PE |
8133 | image_height = MagickGetImageHeight (image_wand); |
8134 | for (y = 0; y < image_height; y++) | |
4917006b JV |
8135 | { |
8136 | pixels = PixelGetNextIteratorRow (iterator, &width); | |
7574650a | 8137 | if (pixels == (PixelWand **) NULL) |
4917006b JV |
8138 | break; |
8139 | for (x = 0; x < (long) width; x++) | |
8140 | { | |
8141 | PixelGetMagickColor (pixels[x], &pixel); | |
d2a2d277 JV |
8142 | XPutPixel (ximg, x, y, |
8143 | lookup_rgb_color (f, | |
8144 | pixel.red, | |
8145 | pixel.green, | |
8146 | pixel.blue)); | |
4917006b JV |
8147 | } |
8148 | } | |
8149 | DestroyPixelIterator (iterator); | |
3de25479 | 8150 | } |
3ed42149 | 8151 | |
3de25479 JV |
8152 | #ifdef COLOR_TABLE_SUPPORT |
8153 | /* Remember colors allocated for this image. */ | |
8154 | img->colors = colors_in_color_table (&img->ncolors); | |
8155 | free_color_table (); | |
8156 | #endif /* COLOR_TABLE_SUPPORT */ | |
8157 | ||
3de25479 JV |
8158 | img->width = width; |
8159 | img->height = height; | |
8160 | ||
547c9269 YM |
8161 | /* Put ximg into the image. */ |
8162 | image_put_x_image (f, img, ximg, 0); | |
3de25479 | 8163 | |
f3700365 | 8164 | /* Final cleanup. image_wand should be the only resource left. */ |
3de25479 | 8165 | DestroyMagickWand (image_wand); |
1b9b4cf4 CY |
8166 | if (bg_wand) DestroyPixelWand (bg_wand); |
8167 | ||
03d32f1b | 8168 | /* `MagickWandTerminus' terminates the imagemagick environment. */ |
f853f599 | 8169 | MagickWandTerminus (); |
3de25479 JV |
8170 | |
8171 | return 1; | |
8172 | ||
8173 | imagemagick_error: | |
466cbae9 | 8174 | DestroyMagickWand (image_wand); |
1b9b4cf4 CY |
8175 | if (bg_wand) DestroyPixelWand (bg_wand); |
8176 | ||
f853f599 | 8177 | MagickWandTerminus (); |
3de25479 JV |
8178 | /* TODO more cleanup. */ |
8179 | image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec, Qnil); | |
3de25479 JV |
8180 | return 0; |
8181 | } | |
8182 | ||
4917006b | 8183 | |
578098f3 | 8184 | /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if |
4917006b JV |
8185 | successful. this function will go into the imagemagick_type structure, and |
8186 | the prototype thus needs to be compatible with that structure. */ | |
8187 | ||
578098f3 | 8188 | static bool |
d66c4c7c | 8189 | imagemagick_load (struct frame *f, struct image *img) |
4917006b | 8190 | { |
578098f3 | 8191 | bool success_p = 0; |
4917006b JV |
8192 | Lisp_Object file_name; |
8193 | ||
8194 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
8195 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
8196 | if (STRINGP (file_name)) | |
8197 | { | |
8198 | Lisp_Object file; | |
4917006b JV |
8199 | |
8200 | file = x_find_image_file (file_name); | |
4917006b JV |
8201 | if (!STRINGP (file)) |
8202 | { | |
8203 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
4917006b JV |
8204 | return 0; |
8205 | } | |
33290528 | 8206 | success_p = imagemagick_load_image (f, img, 0, 0, SSDATA (file)); |
4917006b JV |
8207 | } |
8208 | /* Else its not a file, its a lisp object. Load the image from a | |
8209 | lisp object rather than a file. */ | |
8210 | else | |
8211 | { | |
8212 | Lisp_Object data; | |
8213 | ||
8214 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
8215 | if (!STRINGP (data)) |
8216 | { | |
8217 | image_error ("Invalid image data `%s'", data, Qnil); | |
8218 | return 0; | |
8219 | } | |
d2a2d277 JV |
8220 | success_p = imagemagick_load_image (f, img, SDATA (data), |
8221 | SBYTES (data), NULL); | |
4917006b JV |
8222 | } |
8223 | ||
8224 | return success_p; | |
8225 | } | |
8226 | ||
a7ca3326 | 8227 | DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0, |
d66c4c7c CY |
8228 | doc: /* Return a list of image types supported by ImageMagick. |
8229 | Each entry in this list is a symbol named after an ImageMagick format | |
8230 | tag. See the ImageMagick manual for a list of ImageMagick formats and | |
8231 | their descriptions (http://www.imagemagick.org/script/formats.php). | |
dd605cc4 | 8232 | You can also try the shell command: `identify -list format'. |
d66c4c7c CY |
8233 | |
8234 | Note that ImageMagick recognizes many file-types that Emacs does not | |
32d72c2f GM |
8235 | recognize as images, such as C. See `imagemagick-types-enable' |
8236 | and `imagemagick-types-inhibit'. */) | |
a8101f66 | 8237 | (void) |
3de25479 JV |
8238 | { |
8239 | Lisp_Object typelist = Qnil; | |
1ab0dee5 | 8240 | size_t numf = 0; |
3de25479 | 8241 | ExceptionInfo ex; |
2277de02 | 8242 | char **imtypes; |
3f791afe | 8243 | size_t i; |
3de25479 | 8244 | Lisp_Object Qimagemagicktype; |
2277de02 JD |
8245 | |
8246 | GetExceptionInfo(&ex); | |
8247 | imtypes = GetMagickList ("*", &numf, &ex); | |
8248 | DestroyExceptionInfo(&ex); | |
8249 | ||
4917006b JV |
8250 | for (i = 0; i < numf; i++) |
8251 | { | |
8252 | Qimagemagicktype = intern (imtypes[i]); | |
f3700365 JV |
8253 | typelist = Fcons (Qimagemagicktype, typelist); |
8254 | } | |
d1d7b339 | 8255 | return Fnreverse (typelist); |
3de25479 | 8256 | } |
7574650a | 8257 | |
3de25479 JV |
8258 | #endif /* defined (HAVE_IMAGEMAGICK) */ |
8259 | ||
8260 | ||
4ef0d4d0 | 8261 | \f |
4ab27a43 GM |
8262 | /*********************************************************************** |
8263 | SVG | |
8264 | ***********************************************************************/ | |
8265 | ||
8266 | #if defined (HAVE_RSVG) | |
8267 | ||
8268 | /* Function prototypes. */ | |
8269 | ||
578098f3 PE |
8270 | static bool svg_image_p (Lisp_Object object); |
8271 | static bool svg_load (struct frame *f, struct image *img); | |
4ab27a43 | 8272 | |
578098f3 PE |
8273 | static bool svg_load_image (struct frame *, struct image *, |
8274 | unsigned char *, ptrdiff_t); | |
4ab27a43 GM |
8275 | |
8276 | /* The symbol `svg' identifying images of this type. */ | |
8277 | ||
4b66faf3 | 8278 | static Lisp_Object Qsvg; |
4ab27a43 GM |
8279 | |
8280 | /* Indices of image specification fields in svg_format, below. */ | |
8281 | ||
8282 | enum svg_keyword_index | |
8283 | { | |
8284 | SVG_TYPE, | |
8285 | SVG_DATA, | |
8286 | SVG_FILE, | |
8287 | SVG_ASCENT, | |
8288 | SVG_MARGIN, | |
8289 | SVG_RELIEF, | |
8290 | SVG_ALGORITHM, | |
8291 | SVG_HEURISTIC_MASK, | |
8292 | SVG_MASK, | |
8293 | SVG_BACKGROUND, | |
8294 | SVG_LAST | |
8295 | }; | |
8296 | ||
8297 | /* Vector of image_keyword structures describing the format | |
8298 | of valid user-defined image specifications. */ | |
8299 | ||
91433552 | 8300 | static const struct image_keyword svg_format[SVG_LAST] = |
4ab27a43 GM |
8301 | { |
8302 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8303 | {":data", IMAGE_STRING_VALUE, 0}, | |
8304 | {":file", IMAGE_STRING_VALUE, 0}, | |
8305 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 8306 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ab27a43 GM |
8307 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
8308 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8309 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8310 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8311 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8312 | }; | |
8313 | ||
33d4113c | 8314 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 8315 | static bool init_svg_functions (void); |
bb4d86b4 CY |
8316 | #else |
8317 | #define init_svg_functions NULL | |
8318 | #endif | |
8319 | ||
4ab27a43 GM |
8320 | /* Structure describing the image type `svg'. Its the same type of |
8321 | structure defined for all image formats, handled by emacs image | |
8322 | functions. See struct image_type in dispextern.h. */ | |
8323 | ||
8324 | static struct image_type svg_type = | |
8325 | { | |
4ab27a43 | 8326 | &Qsvg, |
4ab27a43 | 8327 | svg_image_p, |
4ab27a43 | 8328 | svg_load, |
4ab27a43 | 8329 | x_clear_image, |
bb4d86b4 | 8330 | init_svg_functions, |
c21e6151 | 8331 | NULL |
4ab27a43 GM |
8332 | }; |
8333 | ||
8334 | ||
578098f3 | 8335 | /* Return true if OBJECT is a valid SVG image specification. Do |
4ab27a43 GM |
8336 | this by calling parse_image_spec and supplying the keywords that |
8337 | identify the SVG format. */ | |
8338 | ||
578098f3 | 8339 | static bool |
d3da34e0 | 8340 | svg_image_p (Lisp_Object object) |
4ab27a43 GM |
8341 | { |
8342 | struct image_keyword fmt[SVG_LAST]; | |
72af86bd | 8343 | memcpy (fmt, svg_format, sizeof fmt); |
4ab27a43 GM |
8344 | |
8345 | if (!parse_image_spec (object, fmt, SVG_LAST, Qsvg)) | |
8346 | return 0; | |
8347 | ||
8348 | /* Must specify either the :data or :file keyword. */ | |
8349 | return fmt[SVG_FILE].count + fmt[SVG_DATA].count == 1; | |
8350 | } | |
8351 | ||
8352 | #include <librsvg/rsvg.h> | |
8353 | ||
0fda9b75 | 8354 | #ifdef WINDOWSNT |
5fc9fdeb JR |
8355 | |
8356 | /* SVG library functions. */ | |
40693bba | 8357 | DEF_IMGLIB_FN (RsvgHandle *, rsvg_handle_new, (void)); |
8358 | DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions, (RsvgHandle *, RsvgDimensionData *)); | |
8359 | DEF_IMGLIB_FN (gboolean, rsvg_handle_write, (RsvgHandle *, const guchar *, gsize, GError **)); | |
8360 | DEF_IMGLIB_FN (gboolean, rsvg_handle_close, (RsvgHandle *, GError **)); | |
8361 | DEF_IMGLIB_FN (GdkPixbuf *, rsvg_handle_get_pixbuf, (RsvgHandle *)); | |
8362 | DEF_IMGLIB_FN (void *, rsvg_handle_set_size_callback, (RsvgHandle *, RsvgSizeFunc, gpointer, GDestroyNotify)); | |
8363 | ||
8364 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_width, (const GdkPixbuf *)); | |
8365 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_height, (const GdkPixbuf *)); | |
8366 | DEF_IMGLIB_FN (guchar *, gdk_pixbuf_get_pixels, (const GdkPixbuf *)); | |
8367 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride, (const GdkPixbuf *)); | |
8368 | DEF_IMGLIB_FN (GdkColorspace, gdk_pixbuf_get_colorspace, (const GdkPixbuf *)); | |
8369 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels, (const GdkPixbuf *)); | |
8370 | DEF_IMGLIB_FN (gboolean, gdk_pixbuf_get_has_alpha, (const GdkPixbuf *)); | |
8371 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample, (const GdkPixbuf *)); | |
8372 | ||
8373 | DEF_IMGLIB_FN (void, g_type_init, (void)); | |
8374 | DEF_IMGLIB_FN (void, g_object_unref, (gpointer)); | |
121ab1cd | 8375 | DEF_IMGLIB_FN (void, g_error_free, (GError *)); |
5fc9fdeb | 8376 | |
df23bf08 | 8377 | Lisp_Object Qgdk_pixbuf, Qglib, Qgobject; |
5fc9fdeb | 8378 | |
578098f3 | 8379 | static bool |
d07ff9db | 8380 | init_svg_functions (void) |
5fc9fdeb | 8381 | { |
df23bf08 | 8382 | HMODULE library, gdklib, glib, gobject; |
5fc9fdeb | 8383 | |
d07ff9db CY |
8384 | if (!(glib = w32_delayed_load (Qglib)) |
8385 | || !(gobject = w32_delayed_load (Qgobject)) | |
8386 | || !(gdklib = w32_delayed_load (Qgdk_pixbuf)) | |
8387 | || !(library = w32_delayed_load (Qsvg))) | |
5fc9fdeb JR |
8388 | return 0; |
8389 | ||
8390 | LOAD_IMGLIB_FN (library, rsvg_handle_new); | |
e025c9c2 | 8391 | LOAD_IMGLIB_FN (library, rsvg_handle_get_dimensions); |
5fc9fdeb JR |
8392 | LOAD_IMGLIB_FN (library, rsvg_handle_write); |
8393 | LOAD_IMGLIB_FN (library, rsvg_handle_close); | |
8394 | LOAD_IMGLIB_FN (library, rsvg_handle_get_pixbuf); | |
5fc9fdeb JR |
8395 | |
8396 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_width); | |
8397 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_height); | |
8398 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_pixels); | |
8399 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_rowstride); | |
8400 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_colorspace); | |
8401 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_n_channels); | |
8402 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_has_alpha); | |
8403 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_bits_per_sample); | |
8404 | ||
df23bf08 JR |
8405 | LOAD_IMGLIB_FN (gobject, g_type_init); |
8406 | LOAD_IMGLIB_FN (gobject, g_object_unref); | |
5fc9fdeb | 8407 | LOAD_IMGLIB_FN (glib, g_error_free); |
df23bf08 | 8408 | |
5fc9fdeb JR |
8409 | return 1; |
8410 | } | |
4ab27a43 | 8411 | |
5fc9fdeb JR |
8412 | #else |
8413 | /* The following aliases for library functions allow dynamic loading | |
8414 | to be used on some platforms. */ | |
4ab27a43 | 8415 | #define fn_rsvg_handle_new rsvg_handle_new |
e025c9c2 | 8416 | #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions |
4ab27a43 GM |
8417 | #define fn_rsvg_handle_write rsvg_handle_write |
8418 | #define fn_rsvg_handle_close rsvg_handle_close | |
8419 | #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf | |
4ab27a43 | 8420 | |
5fc9fdeb JR |
8421 | #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width |
8422 | #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height | |
8423 | #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels | |
8424 | #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride | |
8425 | #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace | |
8426 | #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels | |
8427 | #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha | |
4ab27a43 GM |
8428 | #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample |
8429 | ||
5fc9fdeb JR |
8430 | #define fn_g_type_init g_type_init |
8431 | #define fn_g_object_unref g_object_unref | |
8432 | #define fn_g_error_free g_error_free | |
0fda9b75 | 8433 | #endif /* !WINDOWSNT */ |
4ab27a43 | 8434 | |
578098f3 PE |
8435 | /* Load SVG image IMG for use on frame F. Value is true if |
8436 | successful. */ | |
4ab27a43 | 8437 | |
578098f3 | 8438 | static bool |
d3da34e0 | 8439 | svg_load (struct frame *f, struct image *img) |
4ab27a43 | 8440 | { |
578098f3 | 8441 | bool success_p = 0; |
4ab27a43 GM |
8442 | Lisp_Object file_name; |
8443 | ||
8444 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
8445 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
8446 | if (STRINGP (file_name)) | |
8447 | { | |
8448 | Lisp_Object file; | |
8449 | unsigned char *contents; | |
dd52fcea | 8450 | ptrdiff_t size; |
4ab27a43 GM |
8451 | |
8452 | file = x_find_image_file (file_name); | |
4ab27a43 | 8453 | if (!STRINGP (file)) |
c21e6151 YM |
8454 | { |
8455 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
c21e6151 YM |
8456 | return 0; |
8457 | } | |
8458 | ||
4ab27a43 | 8459 | /* Read the entire file into memory. */ |
33290528 | 8460 | contents = slurp_file (SSDATA (file), &size); |
4ab27a43 | 8461 | if (contents == NULL) |
c21e6151 YM |
8462 | { |
8463 | image_error ("Error loading SVG image `%s'", img->spec, Qnil); | |
c21e6151 YM |
8464 | return 0; |
8465 | } | |
4ab27a43 | 8466 | /* If the file was slurped into memory properly, parse it. */ |
c21e6151 | 8467 | success_p = svg_load_image (f, img, contents, size); |
4ab27a43 | 8468 | xfree (contents); |
4ab27a43 GM |
8469 | } |
8470 | /* Else its not a file, its a lisp object. Load the image from a | |
8471 | lisp object rather than a file. */ | |
8472 | else | |
8473 | { | |
8474 | Lisp_Object data; | |
8475 | ||
8476 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
8477 | if (!STRINGP (data)) |
8478 | { | |
8479 | image_error ("Invalid image data `%s'", data, Qnil); | |
8480 | return 0; | |
8481 | } | |
4ab27a43 GM |
8482 | success_p = svg_load_image (f, img, SDATA (data), SBYTES (data)); |
8483 | } | |
8484 | ||
8485 | return success_p; | |
8486 | } | |
8487 | ||
bbe6f2aa JB |
8488 | /* svg_load_image is a helper function for svg_load, which does the |
8489 | actual loading given contents and size, apart from frame and image | |
8490 | structures, passed from svg_load. | |
4ab27a43 | 8491 | |
bbe6f2aa | 8492 | Uses librsvg to do most of the image processing. |
c21e6151 | 8493 | |
578098f3 PE |
8494 | Returns true when successful. */ |
8495 | static bool | |
d3da34e0 JB |
8496 | svg_load_image (struct frame *f, /* Pointer to emacs frame structure. */ |
8497 | struct image *img, /* Pointer to emacs image structure. */ | |
8498 | unsigned char *contents, /* String containing the SVG XML data to be parsed. */ | |
dd52fcea | 8499 | ptrdiff_t size) /* Size of data in bytes. */ |
4ab27a43 GM |
8500 | { |
8501 | RsvgHandle *rsvg_handle; | |
e025c9c2 | 8502 | RsvgDimensionData dimension_data; |
33290528 | 8503 | GError *err = NULL; |
4ab27a43 GM |
8504 | GdkPixbuf *pixbuf; |
8505 | int width; | |
8506 | int height; | |
8507 | const guint8 *pixels; | |
8508 | int rowstride; | |
8509 | XImagePtr ximg; | |
c21e6151 | 8510 | Lisp_Object specified_bg; |
4ab27a43 GM |
8511 | XColor background; |
8512 | int x; | |
8513 | int y; | |
8514 | ||
8515 | /* g_type_init is a glib function that must be called prior to using | |
8516 | gnome type library functions. */ | |
5fc9fdeb | 8517 | fn_g_type_init (); |
4ab27a43 | 8518 | /* Make a handle to a new rsvg object. */ |
df61c790 | 8519 | rsvg_handle = fn_rsvg_handle_new (); |
4ab27a43 GM |
8520 | |
8521 | /* Parse the contents argument and fill in the rsvg_handle. */ | |
33290528 PE |
8522 | fn_rsvg_handle_write (rsvg_handle, contents, size, &err); |
8523 | if (err) goto rsvg_error; | |
4ab27a43 GM |
8524 | |
8525 | /* The parsing is complete, rsvg_handle is ready to used, close it | |
8526 | for further writes. */ | |
33290528 PE |
8527 | fn_rsvg_handle_close (rsvg_handle, &err); |
8528 | if (err) goto rsvg_error; | |
e025c9c2 CY |
8529 | |
8530 | fn_rsvg_handle_get_dimensions (rsvg_handle, &dimension_data); | |
8531 | if (! check_image_size (f, dimension_data.width, dimension_data.height)) | |
10ea2b82 JR |
8532 | { |
8533 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
8534 | goto rsvg_error; | |
8535 | } | |
b2996e57 | 8536 | |
4ab27a43 GM |
8537 | /* We can now get a valid pixel buffer from the svg file, if all |
8538 | went ok. */ | |
df61c790 | 8539 | pixbuf = fn_rsvg_handle_get_pixbuf (rsvg_handle); |
e025c9c2 CY |
8540 | if (!pixbuf) goto rsvg_error; |
8541 | fn_g_object_unref (rsvg_handle); | |
4ab27a43 GM |
8542 | |
8543 | /* Extract some meta data from the svg handle. */ | |
8544 | width = fn_gdk_pixbuf_get_width (pixbuf); | |
8545 | height = fn_gdk_pixbuf_get_height (pixbuf); | |
df61c790 | 8546 | pixels = fn_gdk_pixbuf_get_pixels (pixbuf); |
4ab27a43 GM |
8547 | rowstride = fn_gdk_pixbuf_get_rowstride (pixbuf); |
8548 | ||
8549 | /* Validate the svg meta data. */ | |
8550 | eassert (fn_gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB); | |
8551 | eassert (fn_gdk_pixbuf_get_n_channels (pixbuf) == 4); | |
8552 | eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf)); | |
8553 | eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf) == 8); | |
8554 | ||
8555 | /* Try to create a x pixmap to hold the svg pixmap. */ | |
547c9269 | 8556 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
c21e6151 | 8557 | { |
5fc9fdeb | 8558 | fn_g_object_unref (pixbuf); |
c21e6151 YM |
8559 | return 0; |
8560 | } | |
4ab27a43 GM |
8561 | |
8562 | init_color_table (); | |
8563 | ||
c21e6151 YM |
8564 | /* Handle alpha channel by combining the image with a background |
8565 | color. */ | |
8566 | specified_bg = image_spec_value (img->spec, QCbackground, NULL); | |
8d3c4e79 | 8567 | if (!STRINGP (specified_bg) |
33290528 | 8568 | || !x_defined_color (f, SSDATA (specified_bg), &background, 0)) |
c21e6151 | 8569 | { |
d7e008d0 | 8570 | #ifndef HAVE_NS |
c21e6151 YM |
8571 | background.pixel = FRAME_BACKGROUND_PIXEL (f); |
8572 | x_query_color (f, &background); | |
d7e008d0 | 8573 | #else |
ed3751c8 | 8574 | ns_query_color (FRAME_BACKGROUND_COLOR (f), &background, 1); |
d7e008d0 | 8575 | #endif |
c21e6151 | 8576 | } |
4ab27a43 | 8577 | |
8d3c4e79 CY |
8578 | /* SVG pixmaps specify transparency in the last byte, so right |
8579 | shift 8 bits to get rid of it, since emacs doesn't support | |
8580 | transparency. */ | |
8581 | background.red >>= 8; | |
8582 | background.green >>= 8; | |
8583 | background.blue >>= 8; | |
8584 | ||
4ab27a43 GM |
8585 | /* This loop handles opacity values, since Emacs assumes |
8586 | non-transparent images. Each pixel must be "flattened" by | |
bbe6f2aa JB |
8587 | calculating the resulting color, given the transparency of the |
8588 | pixel, and the image background color. */ | |
4ab27a43 GM |
8589 | for (y = 0; y < height; ++y) |
8590 | { | |
8591 | for (x = 0; x < width; ++x) | |
8592 | { | |
13464394 PE |
8593 | int red; |
8594 | int green; | |
8595 | int blue; | |
8596 | int opacity; | |
4ab27a43 GM |
8597 | |
8598 | red = *pixels++; | |
8599 | green = *pixels++; | |
8600 | blue = *pixels++; | |
8601 | opacity = *pixels++; | |
8602 | ||
8603 | red = ((red * opacity) | |
8604 | + (background.red * ((1 << 8) - opacity))); | |
8605 | green = ((green * opacity) | |
8606 | + (background.green * ((1 << 8) - opacity))); | |
8607 | blue = ((blue * opacity) | |
8608 | + (background.blue * ((1 << 8) - opacity))); | |
8609 | ||
8610 | XPutPixel (ximg, x, y, lookup_rgb_color (f, red, green, blue)); | |
8611 | } | |
8612 | ||
8613 | pixels += rowstride - 4 * width; | |
8614 | } | |
8615 | ||
8616 | #ifdef COLOR_TABLE_SUPPORT | |
8617 | /* Remember colors allocated for this image. */ | |
8618 | img->colors = colors_in_color_table (&img->ncolors); | |
8619 | free_color_table (); | |
8620 | #endif /* COLOR_TABLE_SUPPORT */ | |
8621 | ||
5fc9fdeb | 8622 | fn_g_object_unref (pixbuf); |
4ab27a43 | 8623 | |
c21e6151 YM |
8624 | img->width = width; |
8625 | img->height = height; | |
8626 | ||
8627 | /* Maybe fill in the background field while we have ximg handy. | |
8628 | Casting avoids a GCC warning. */ | |
8629 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
8630 | ||
547c9269 YM |
8631 | /* Put ximg into the image. */ |
8632 | image_put_x_image (f, img, ximg, 0); | |
4ab27a43 | 8633 | |
4ab27a43 GM |
8634 | return 1; |
8635 | ||
8636 | rsvg_error: | |
e025c9c2 | 8637 | fn_g_object_unref (rsvg_handle); |
4ab27a43 GM |
8638 | /* FIXME: Use error->message so the user knows what is the actual |
8639 | problem with the image. */ | |
8640 | image_error ("Error parsing SVG image `%s'", img->spec, Qnil); | |
33290528 | 8641 | fn_g_error_free (err); |
4ab27a43 GM |
8642 | return 0; |
8643 | } | |
8644 | ||
8645 | #endif /* defined (HAVE_RSVG) */ | |
8646 | ||
8647 | ||
8648 | ||
4ef0d4d0 KS |
8649 | \f |
8650 | /*********************************************************************** | |
8651 | Ghostscript | |
8652 | ***********************************************************************/ | |
8653 | ||
8654 | #ifdef HAVE_X_WINDOWS | |
8655 | #define HAVE_GHOSTSCRIPT 1 | |
8656 | #endif /* HAVE_X_WINDOWS */ | |
8657 | ||
4ef0d4d0 KS |
8658 | #ifdef HAVE_GHOSTSCRIPT |
8659 | ||
578098f3 PE |
8660 | static bool gs_image_p (Lisp_Object object); |
8661 | static bool gs_load (struct frame *f, struct image *img); | |
f57e2426 | 8662 | static void gs_clear_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
8663 | |
8664 | /* Keyword symbols. */ | |
8665 | ||
955cbe7b | 8666 | static Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height; |
4ef0d4d0 KS |
8667 | |
8668 | /* Indices of image specification fields in gs_format, below. */ | |
8669 | ||
8670 | enum gs_keyword_index | |
8671 | { | |
8672 | GS_TYPE, | |
8673 | GS_PT_WIDTH, | |
8674 | GS_PT_HEIGHT, | |
8675 | GS_FILE, | |
8676 | GS_LOADER, | |
8677 | GS_BOUNDING_BOX, | |
8678 | GS_ASCENT, | |
8679 | GS_MARGIN, | |
8680 | GS_RELIEF, | |
8681 | GS_ALGORITHM, | |
8682 | GS_HEURISTIC_MASK, | |
8683 | GS_MASK, | |
8684 | GS_BACKGROUND, | |
8685 | GS_LAST | |
8686 | }; | |
8687 | ||
8688 | /* Vector of image_keyword structures describing the format | |
8689 | of valid user-defined image specifications. */ | |
8690 | ||
91433552 | 8691 | static const struct image_keyword gs_format[GS_LAST] = |
4ef0d4d0 KS |
8692 | { |
8693 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8694 | {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8695 | {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8696 | {":file", IMAGE_STRING_VALUE, 1}, | |
8697 | {":loader", IMAGE_FUNCTION_VALUE, 0}, | |
8698 | {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1}, | |
8699 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 8700 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
8701 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
8702 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8703 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8704 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8705 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8706 | }; | |
8707 | ||
8708 | /* Structure describing the image type `ghostscript'. */ | |
8709 | ||
8710 | static struct image_type gs_type = | |
8711 | { | |
8712 | &Qpostscript, | |
8713 | gs_image_p, | |
8714 | gs_load, | |
8715 | gs_clear_image, | |
d07ff9db | 8716 | NULL, |
4ef0d4d0 KS |
8717 | NULL |
8718 | }; | |
8719 | ||
8720 | ||
8721 | /* Free X resources of Ghostscript image IMG which is used on frame F. */ | |
8722 | ||
8723 | static void | |
d3da34e0 | 8724 | gs_clear_image (struct frame *f, struct image *img) |
4ef0d4d0 | 8725 | { |
4ef0d4d0 KS |
8726 | x_clear_image (f, img); |
8727 | } | |
8728 | ||
8729 | ||
578098f3 | 8730 | /* Return true if OBJECT is a valid Ghostscript image |
4ef0d4d0 KS |
8731 | specification. */ |
8732 | ||
578098f3 | 8733 | static bool |
d3da34e0 | 8734 | gs_image_p (Lisp_Object object) |
4ef0d4d0 KS |
8735 | { |
8736 | struct image_keyword fmt[GS_LAST]; | |
8737 | Lisp_Object tem; | |
8738 | int i; | |
8739 | ||
72af86bd | 8740 | memcpy (fmt, gs_format, sizeof fmt); |
4ef0d4d0 KS |
8741 | |
8742 | if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)) | |
8743 | return 0; | |
8744 | ||
8745 | /* Bounding box must be a list or vector containing 4 integers. */ | |
8746 | tem = fmt[GS_BOUNDING_BOX].value; | |
8747 | if (CONSP (tem)) | |
8748 | { | |
8749 | for (i = 0; i < 4; ++i, tem = XCDR (tem)) | |
8750 | if (!CONSP (tem) || !INTEGERP (XCAR (tem))) | |
8751 | return 0; | |
8752 | if (!NILP (tem)) | |
8753 | return 0; | |
8754 | } | |
8755 | else if (VECTORP (tem)) | |
8756 | { | |
77b37c05 | 8757 | if (ASIZE (tem) != 4) |
4ef0d4d0 KS |
8758 | return 0; |
8759 | for (i = 0; i < 4; ++i) | |
28be1ada | 8760 | if (!INTEGERP (AREF (tem, i))) |
4ef0d4d0 KS |
8761 | return 0; |
8762 | } | |
8763 | else | |
8764 | return 0; | |
8765 | ||
8766 | return 1; | |
8767 | } | |
8768 | ||
8769 | ||
578098f3 | 8770 | /* Load Ghostscript image IMG for use on frame F. Value is true |
4ef0d4d0 KS |
8771 | if successful. */ |
8772 | ||
578098f3 | 8773 | static bool |
d3da34e0 | 8774 | gs_load (struct frame *f, struct image *img) |
4ef0d4d0 | 8775 | { |
13464394 PE |
8776 | uprintmax_t printnum1, printnum2; |
8777 | char buffer[sizeof " " + INT_STRLEN_BOUND (printmax_t)]; | |
4ef0d4d0 | 8778 | Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width; |
4ef0d4d0 KS |
8779 | Lisp_Object frame; |
8780 | double in_width, in_height; | |
8781 | Lisp_Object pixel_colors = Qnil; | |
8782 | ||
8783 | /* Compute pixel size of pixmap needed from the given size in the | |
8784 | image specification. Sizes in the specification are in pt. 1 pt | |
8785 | = 1/72 in, xdpi and ydpi are stored in the frame's X display | |
8786 | info. */ | |
8787 | pt_width = image_spec_value (img->spec, QCpt_width, NULL); | |
7574650a | 8788 | in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0; |
42143acd | 8789 | in_width *= FRAME_RES_X (f); |
4ef0d4d0 | 8790 | pt_height = image_spec_value (img->spec, QCpt_height, NULL); |
7574650a | 8791 | in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0; |
42143acd | 8792 | in_height *= FRAME_RES_Y (f); |
4ef0d4d0 | 8793 | |
13464394 PE |
8794 | if (! (in_width <= INT_MAX && in_height <= INT_MAX |
8795 | && check_image_size (f, in_width, in_height))) | |
f1f25b99 | 8796 | { |
10ea2b82 | 8797 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
8798 | return 0; |
8799 | } | |
13464394 PE |
8800 | img->width = in_width; |
8801 | img->height = in_height; | |
f1f25b99 | 8802 | |
4ef0d4d0 | 8803 | /* Create the pixmap. */ |
a54e2c05 | 8804 | eassert (img->pixmap == NO_PIXMAP); |
4ef0d4d0 | 8805 | |
ca4aa935 PE |
8806 | if (x_check_image_size (0, img->width, img->height)) |
8807 | { | |
8808 | /* Only W32 version did BLOCK_INPUT here. ++kfs */ | |
4d7e6e51 | 8809 | block_input (); |
ca4aa935 PE |
8810 | img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
8811 | img->width, img->height, | |
8812 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))); | |
4d7e6e51 | 8813 | unblock_input (); |
ca4aa935 | 8814 | } |
4ef0d4d0 KS |
8815 | |
8816 | if (!img->pixmap) | |
8817 | { | |
8818 | image_error ("Unable to create pixmap for `%s'", img->spec, Qnil); | |
8819 | return 0; | |
8820 | } | |
8821 | ||
8822 | /* Call the loader to fill the pixmap. It returns a process object | |
8823 | if successful. We do not record_unwind_protect here because | |
8824 | other places in redisplay like calling window scroll functions | |
8825 | don't either. Let the Lisp loader use `unwind-protect' instead. */ | |
13464394 PE |
8826 | printnum1 = FRAME_X_WINDOW (f); |
8827 | printnum2 = img->pixmap; | |
620f13b0 | 8828 | window_and_pixmap_id |
a8290ec3 | 8829 | = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2); |
4ef0d4d0 | 8830 | |
13464394 PE |
8831 | printnum1 = FRAME_FOREGROUND_PIXEL (f); |
8832 | printnum2 = FRAME_BACKGROUND_PIXEL (f); | |
a8290ec3 DA |
8833 | pixel_colors |
8834 | = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2); | |
4ef0d4d0 KS |
8835 | |
8836 | XSETFRAME (frame, f); | |
8837 | loader = image_spec_value (img->spec, QCloader, NULL); | |
8838 | if (NILP (loader)) | |
8839 | loader = intern ("gs-load-image"); | |
8840 | ||
b50691aa CY |
8841 | img->lisp_data = call6 (loader, frame, img->spec, |
8842 | make_number (img->width), | |
8843 | make_number (img->height), | |
8844 | window_and_pixmap_id, | |
8845 | pixel_colors); | |
8846 | return PROCESSP (img->lisp_data); | |
4ef0d4d0 KS |
8847 | } |
8848 | ||
8849 | ||
8850 | /* Kill the Ghostscript process that was started to fill PIXMAP on | |
8851 | frame F. Called from XTread_socket when receiving an event | |
8852 | telling Emacs that Ghostscript has finished drawing. */ | |
8853 | ||
8854 | void | |
d3da34e0 | 8855 | x_kill_gs_process (Pixmap pixmap, struct frame *f) |
4ef0d4d0 | 8856 | { |
354884c4 | 8857 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
13464394 PE |
8858 | int class; |
8859 | ptrdiff_t i; | |
4ef0d4d0 KS |
8860 | struct image *img; |
8861 | ||
8862 | /* Find the image containing PIXMAP. */ | |
8863 | for (i = 0; i < c->used; ++i) | |
8864 | if (c->images[i]->pixmap == pixmap) | |
8865 | break; | |
8866 | ||
8867 | /* Should someone in between have cleared the image cache, for | |
8868 | instance, give up. */ | |
8869 | if (i == c->used) | |
8870 | return; | |
8871 | ||
8872 | /* Kill the GS process. We should have found PIXMAP in the image | |
8873 | cache and its image should contain a process object. */ | |
8874 | img = c->images[i]; | |
a54e2c05 | 8875 | eassert (PROCESSP (img->lisp_data)); |
b50691aa CY |
8876 | Fkill_process (img->lisp_data, Qnil); |
8877 | img->lisp_data = Qnil; | |
4ef0d4d0 KS |
8878 | |
8879 | #if defined (HAVE_X_WINDOWS) | |
8880 | ||
8881 | /* On displays with a mutable colormap, figure out the colors | |
8882 | allocated for the image by looking at the pixels of an XImage for | |
8883 | img->pixmap. */ | |
8884 | class = FRAME_X_VISUAL (f)->class; | |
8885 | if (class != StaticColor && class != StaticGray && class != TrueColor) | |
8886 | { | |
8887 | XImagePtr ximg; | |
8888 | ||
4d7e6e51 | 8889 | block_input (); |
4ef0d4d0 KS |
8890 | |
8891 | /* Try to get an XImage for img->pixmep. */ | |
8892 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, | |
8893 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
8894 | if (ximg) | |
8895 | { | |
8896 | int x, y; | |
8897 | ||
8898 | /* Initialize the color table. */ | |
8899 | init_color_table (); | |
8900 | ||
8901 | /* For each pixel of the image, look its color up in the | |
8902 | color table. After having done so, the color table will | |
8903 | contain an entry for each color used by the image. */ | |
8904 | for (y = 0; y < img->height; ++y) | |
8905 | for (x = 0; x < img->width; ++x) | |
8906 | { | |
8907 | unsigned long pixel = XGetPixel (ximg, x, y); | |
8908 | lookup_pixel_color (f, pixel); | |
8909 | } | |
8910 | ||
8911 | /* Record colors in the image. Free color table and XImage. */ | |
8912 | #ifdef COLOR_TABLE_SUPPORT | |
8913 | img->colors = colors_in_color_table (&img->ncolors); | |
8914 | free_color_table (); | |
8915 | #endif | |
8916 | XDestroyImage (ximg); | |
8917 | ||
8918 | #if 0 /* This doesn't seem to be the case. If we free the colors | |
8919 | here, we get a BadAccess later in x_clear_image when | |
8920 | freeing the colors. */ | |
8921 | /* We have allocated colors once, but Ghostscript has also | |
8922 | allocated colors on behalf of us. So, to get the | |
8923 | reference counts right, free them once. */ | |
8924 | if (img->ncolors) | |
8925 | x_free_colors (f, img->colors, img->ncolors); | |
8926 | #endif | |
8927 | } | |
8928 | else | |
8929 | image_error ("Cannot get X image of `%s'; colors will not be freed", | |
8930 | img->spec, Qnil); | |
8931 | ||
4d7e6e51 | 8932 | unblock_input (); |
4ef0d4d0 KS |
8933 | } |
8934 | #endif /* HAVE_X_WINDOWS */ | |
8935 | ||
8936 | /* Now that we have the pixmap, compute mask and transform the | |
8937 | image if requested. */ | |
4d7e6e51 | 8938 | block_input (); |
4ef0d4d0 | 8939 | postprocess_image (f, img); |
4d7e6e51 | 8940 | unblock_input (); |
4ef0d4d0 KS |
8941 | } |
8942 | ||
8943 | #endif /* HAVE_GHOSTSCRIPT */ | |
8944 | ||
8945 | \f | |
8946 | /*********************************************************************** | |
8947 | Tests | |
8948 | ***********************************************************************/ | |
8949 | ||
e509cfa6 | 8950 | #ifdef GLYPH_DEBUG |
4ef0d4d0 | 8951 | |
a7ca3326 | 8952 | DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0, |
4ef0d4d0 | 8953 | doc: /* Value is non-nil if SPEC is a valid image specification. */) |
5842a27b | 8954 | (Lisp_Object spec) |
4ef0d4d0 KS |
8955 | { |
8956 | return valid_image_p (spec) ? Qt : Qnil; | |
8957 | } | |
8958 | ||
8959 | ||
a7ca3326 | 8960 | DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "") |
5842a27b | 8961 | (Lisp_Object spec) |
4ef0d4d0 | 8962 | { |
13464394 | 8963 | ptrdiff_t id = -1; |
4ef0d4d0 KS |
8964 | |
8965 | if (valid_image_p (spec)) | |
8966 | id = lookup_image (SELECTED_FRAME (), spec); | |
8967 | ||
8968 | debug_print (spec); | |
8969 | return make_number (id); | |
8970 | } | |
8971 | ||
e509cfa6 | 8972 | #endif /* GLYPH_DEBUG */ |
4ef0d4d0 KS |
8973 | |
8974 | ||
8975 | /*********************************************************************** | |
8976 | Initialization | |
8977 | ***********************************************************************/ | |
8978 | ||
d07ff9db | 8979 | DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 1, 1, 0, |
0855eb52 JB |
8980 | doc: /* Initialize image library implementing image type TYPE. |
8981 | Return non-nil if TYPE is a supported image type. | |
8982 | ||
d07ff9db CY |
8983 | If image libraries are loaded dynamically (currently only the case on |
8984 | MS-Windows), load the library for TYPE if it is not yet loaded, using | |
8985 | the library file(s) specified by `dynamic-library-alist'. */) | |
8986 | (Lisp_Object type) | |
0855eb52 | 8987 | { |
d07ff9db | 8988 | return lookup_image_type (type) ? Qt : Qnil; |
bb4d86b4 | 8989 | } |
0855eb52 | 8990 | |
bb4d86b4 | 8991 | /* Look up image type TYPE, and return a pointer to its image_type |
d07ff9db | 8992 | structure. Return 0 if TYPE is not a known image type. */ |
bb4d86b4 CY |
8993 | |
8994 | static struct image_type * | |
d07ff9db | 8995 | lookup_image_type (Lisp_Object type) |
bb4d86b4 | 8996 | { |
2f142cc5 | 8997 | /* Types pbm and xbm are built-in and always available. */ |
bb4d86b4 | 8998 | if (EQ (type, Qpbm)) |
d07ff9db | 8999 | return define_image_type (&pbm_type); |
bb4d86b4 CY |
9000 | |
9001 | if (EQ (type, Qxbm)) | |
d07ff9db | 9002 | return define_image_type (&xbm_type); |
2f142cc5 | 9003 | |
9e2a2647 | 9004 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
0855eb52 | 9005 | if (EQ (type, Qxpm)) |
d07ff9db | 9006 | return define_image_type (&xpm_type); |
0855eb52 JB |
9007 | #endif |
9008 | ||
9e2a2647 | 9009 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
0855eb52 | 9010 | if (EQ (type, Qjpeg)) |
d07ff9db | 9011 | return define_image_type (&jpeg_type); |
0855eb52 JB |
9012 | #endif |
9013 | ||
9e2a2647 | 9014 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
0855eb52 | 9015 | if (EQ (type, Qtiff)) |
d07ff9db | 9016 | return define_image_type (&tiff_type); |
0855eb52 JB |
9017 | #endif |
9018 | ||
9e2a2647 | 9019 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
0855eb52 | 9020 | if (EQ (type, Qgif)) |
d07ff9db | 9021 | return define_image_type (&gif_type); |
0855eb52 JB |
9022 | #endif |
9023 | ||
9e2a2647 | 9024 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
0855eb52 | 9025 | if (EQ (type, Qpng)) |
d07ff9db | 9026 | return define_image_type (&png_type); |
0855eb52 JB |
9027 | #endif |
9028 | ||
4ab27a43 GM |
9029 | #if defined (HAVE_RSVG) |
9030 | if (EQ (type, Qsvg)) | |
d07ff9db | 9031 | return define_image_type (&svg_type); |
4ab27a43 | 9032 | #endif |
c21e6151 | 9033 | |
3de25479 | 9034 | #if defined (HAVE_IMAGEMAGICK) |
7574650a | 9035 | if (EQ (type, Qimagemagick)) |
d07ff9db | 9036 | return define_image_type (&imagemagick_type); |
3de25479 JV |
9037 | #endif |
9038 | ||
0855eb52 JB |
9039 | #ifdef HAVE_GHOSTSCRIPT |
9040 | if (EQ (type, Qpostscript)) | |
d07ff9db | 9041 | return define_image_type (&gs_type); |
0855eb52 JB |
9042 | #endif |
9043 | ||
bb4d86b4 | 9044 | return NULL; |
0855eb52 JB |
9045 | } |
9046 | ||
aa15c6bb JB |
9047 | /* Reset image_types before dumping. |
9048 | Called from Fdump_emacs. */ | |
9049 | ||
9050 | void | |
9051 | reset_image_types (void) | |
9052 | { | |
9053 | while (image_types) | |
9054 | { | |
9055 | struct image_type *next = image_types->next; | |
9056 | xfree (image_types); | |
9057 | image_types = next; | |
9058 | } | |
0855eb52 JB |
9059 | } |
9060 | ||
4ef0d4d0 | 9061 | void |
d3da34e0 | 9062 | syms_of_image (void) |
4ef0d4d0 | 9063 | { |
aa15c6bb | 9064 | /* Initialize this only once; it will be reset before dumping. */ |
6e6fc3fd RS |
9065 | image_types = NULL; |
9066 | ||
91af3942 | 9067 | /* Must be defined now because we're going to update it below, while |
0855eb52 | 9068 | defining the supported image types. */ |
29208e82 | 9069 | DEFVAR_LISP ("image-types", Vimage_types, |
0855eb52 | 9070 | doc: /* List of potentially supported image types. |
bbe6f2aa | 9071 | Each element of the list is a symbol for an image type, like 'jpeg or 'png. |
0855eb52 JB |
9072 | To check whether it is really supported, use `image-type-available-p'. */); |
9073 | Vimage_types = Qnil; | |
9074 | ||
29208e82 | 9075 | DEFVAR_LISP ("max-image-size", Vmax_image_size, |
7df4765a KS |
9076 | doc: /* Maximum size of images. |
9077 | Emacs will not load an image into memory if its pixel width or | |
9078 | pixel height exceeds this limit. | |
9079 | ||
9080 | If the value is an integer, it directly specifies the maximum | |
9081 | image height and width, measured in pixels. If it is a floating | |
9082 | point number, it specifies the maximum image height and width | |
9083 | as a ratio to the frame height and width. If the value is | |
9084 | non-numeric, there is no explicit limit on the size of images. */); | |
f1f25b99 CY |
9085 | Vmax_image_size = make_float (MAX_IMAGE_SIZE); |
9086 | ||
1100a63c CY |
9087 | DEFSYM (Qcount, "count"); |
9088 | DEFSYM (Qextension_data, "extension-data"); | |
9089 | DEFSYM (Qdelay, "delay"); | |
9090 | ||
9091 | DEFSYM (QCascent, ":ascent"); | |
9092 | DEFSYM (QCmargin, ":margin"); | |
9093 | DEFSYM (QCrelief, ":relief"); | |
9094 | DEFSYM (QCconversion, ":conversion"); | |
9095 | DEFSYM (QCcolor_symbols, ":color-symbols"); | |
9096 | DEFSYM (QCheuristic_mask, ":heuristic-mask"); | |
9097 | DEFSYM (QCindex, ":index"); | |
9098 | DEFSYM (QCgeometry, ":geometry"); | |
9099 | DEFSYM (QCcrop, ":crop"); | |
9100 | DEFSYM (QCrotation, ":rotation"); | |
9101 | DEFSYM (QCmatrix, ":matrix"); | |
9102 | DEFSYM (QCcolor_adjustment, ":color-adjustment"); | |
9103 | DEFSYM (QCmask, ":mask"); | |
9104 | ||
9105 | DEFSYM (Qlaplace, "laplace"); | |
9106 | DEFSYM (Qemboss, "emboss"); | |
9107 | DEFSYM (Qedge_detection, "edge-detection"); | |
9108 | DEFSYM (Qheuristic, "heuristic"); | |
9109 | ||
9110 | DEFSYM (Qpostscript, "postscript"); | |
f3f9606c LMI |
9111 | DEFSYM (QCmax_width, ":max-width"); |
9112 | DEFSYM (QCmax_height, ":max-height"); | |
4ef0d4d0 | 9113 | #ifdef HAVE_GHOSTSCRIPT |
5fc9fdeb | 9114 | ADD_IMAGE_TYPE (Qpostscript); |
1100a63c CY |
9115 | DEFSYM (QCloader, ":loader"); |
9116 | DEFSYM (QCbounding_box, ":bounding-box"); | |
9117 | DEFSYM (QCpt_width, ":pt-width"); | |
9118 | DEFSYM (QCpt_height, ":pt-height"); | |
4ef0d4d0 KS |
9119 | #endif /* HAVE_GHOSTSCRIPT */ |
9120 | ||
5be1c984 | 9121 | #ifdef HAVE_NTGUI |
1100a63c | 9122 | DEFSYM (Qlibpng_version, "libpng-version"); |
0514b4be | 9123 | Fset (Qlibpng_version, |
5be1c984 | 9124 | #if HAVE_PNG |
0514b4be | 9125 | make_number (PNG_LIBPNG_VER) |
5be1c984 | 9126 | #else |
0514b4be | 9127 | make_number (-1) |
5be1c984 | 9128 | #endif |
0514b4be | 9129 | ); |
5be1c984 EZ |
9130 | #endif |
9131 | ||
bb4d86b4 CY |
9132 | DEFSYM (Qpbm, "pbm"); |
9133 | ADD_IMAGE_TYPE (Qpbm); | |
9134 | ||
9135 | DEFSYM (Qxbm, "xbm"); | |
9136 | ADD_IMAGE_TYPE (Qxbm); | |
9137 | ||
9e2a2647 | 9138 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
1100a63c | 9139 | DEFSYM (Qxpm, "xpm"); |
5fc9fdeb | 9140 | ADD_IMAGE_TYPE (Qxpm); |
4ef0d4d0 KS |
9141 | #endif |
9142 | ||
9e2a2647 | 9143 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
1100a63c | 9144 | DEFSYM (Qjpeg, "jpeg"); |
5fc9fdeb | 9145 | ADD_IMAGE_TYPE (Qjpeg); |
4ef0d4d0 KS |
9146 | #endif |
9147 | ||
9e2a2647 | 9148 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
1100a63c | 9149 | DEFSYM (Qtiff, "tiff"); |
5fc9fdeb | 9150 | ADD_IMAGE_TYPE (Qtiff); |
4ef0d4d0 KS |
9151 | #endif |
9152 | ||
9e2a2647 | 9153 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
1100a63c | 9154 | DEFSYM (Qgif, "gif"); |
5fc9fdeb | 9155 | ADD_IMAGE_TYPE (Qgif); |
4ef0d4d0 KS |
9156 | #endif |
9157 | ||
9e2a2647 | 9158 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
1100a63c | 9159 | DEFSYM (Qpng, "png"); |
5fc9fdeb | 9160 | ADD_IMAGE_TYPE (Qpng); |
4ef0d4d0 KS |
9161 | #endif |
9162 | ||
3de25479 | 9163 | #if defined (HAVE_IMAGEMAGICK) |
1100a63c | 9164 | DEFSYM (Qimagemagick, "imagemagick"); |
3de25479 JV |
9165 | ADD_IMAGE_TYPE (Qimagemagick); |
9166 | #endif | |
7574650a | 9167 | |
4ab27a43 | 9168 | #if defined (HAVE_RSVG) |
1100a63c | 9169 | DEFSYM (Qsvg, "svg"); |
5fc9fdeb JR |
9170 | ADD_IMAGE_TYPE (Qsvg); |
9171 | #ifdef HAVE_NTGUI | |
df23bf08 | 9172 | /* Other libraries used directly by svg code. */ |
1100a63c CY |
9173 | DEFSYM (Qgdk_pixbuf, "gdk-pixbuf"); |
9174 | DEFSYM (Qglib, "glib"); | |
9175 | DEFSYM (Qgobject, "gobject"); | |
5fc9fdeb JR |
9176 | #endif /* HAVE_NTGUI */ |
9177 | #endif /* HAVE_RSVG */ | |
4ab27a43 | 9178 | |
0855eb52 | 9179 | defsubr (&Sinit_image_library); |
7574650a | 9180 | #ifdef HAVE_IMAGEMAGICK |
3de25479 | 9181 | defsubr (&Simagemagick_types); |
7574650a | 9182 | #endif |
4ef0d4d0 | 9183 | defsubr (&Sclear_image_cache); |
110683ad | 9184 | defsubr (&Simage_flush); |
4ef0d4d0 KS |
9185 | defsubr (&Simage_size); |
9186 | defsubr (&Simage_mask_p); | |
1546c559 | 9187 | defsubr (&Simage_metadata); |
4ef0d4d0 | 9188 | |
e509cfa6 | 9189 | #ifdef GLYPH_DEBUG |
4ef0d4d0 KS |
9190 | defsubr (&Simagep); |
9191 | defsubr (&Slookup_image); | |
9192 | #endif | |
9193 | ||
29208e82 | 9194 | DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images, |
4ef0d4d0 | 9195 | doc: /* Non-nil means always draw a cross over disabled images. |
3046a84b | 9196 | Disabled images are those having a `:conversion disabled' property. |
4ef0d4d0 KS |
9197 | A cross is always drawn on black & white displays. */); |
9198 | cross_disabled_images = 0; | |
9199 | ||
29208e82 | 9200 | DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path, |
4ef0d4d0 KS |
9201 | doc: /* List of directories to search for window system bitmap files. */); |
9202 | Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS); | |
9203 | ||
29208e82 | 9204 | DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay, |
98fe5161 CY |
9205 | doc: /* Maximum time after which images are removed from the cache. |
9206 | When an image has not been displayed this many seconds, Emacs | |
9207 | automatically removes it from the image cache. If the cache contains | |
9208 | a large number of images, the actual eviction time may be shorter. | |
9209 | The value can also be nil, meaning the cache is never cleared. | |
9210 | ||
9211 | The function `clear-image-cache' disregards this variable. */); | |
9212 | Vimage_cache_eviction_delay = make_number (300); | |
7574650a | 9213 | #ifdef HAVE_IMAGEMAGICK |
af008560 GM |
9214 | DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type, |
9215 | doc: /* Integer indicating which ImageMagick rendering method to use. | |
9216 | The options are: | |
9217 | 0 -- the default method (pixel pushing) | |
9218 | 1 -- a newer method ("MagickExportImagePixels") that may perform | |
9219 | better (speed etc) in some cases, but has not been as thoroughly | |
9220 | tested with Emacs as the default method. This method requires | |
9221 | ImageMagick version 6.4.6 (approximately) or later. | |
9222 | */); | |
9223 | /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */ | |
9224 | imagemagick_render_type = 0; | |
7574650a | 9225 | #endif |
4ef0d4d0 | 9226 | |
4ef0d4d0 | 9227 | } |