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