Commit | Line | Data |
---|---|---|
4ef0d4d0 | 1 | /* Functions for image support on window system. |
32d72c2f | 2 | |
ab422c4d | 3 | Copyright (C) 1989, 1992-2013 Free Software Foundation, Inc. |
4ef0d4d0 KS |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
4ef0d4d0 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
4ef0d4d0 KS |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ef0d4d0 KS |
19 | |
20 | #include <config.h> | |
406af475 | 21 | #include "sysstdio.h" |
4ef0d4d0 | 22 | #include <unistd.h> |
4ef0d4d0 | 23 | |
d7306fe6 DN |
24 | #ifdef HAVE_PNG |
25 | #if defined HAVE_LIBPNG_PNG_H | |
26 | # include <libpng/png.h> | |
27 | #else | |
28 | # include <png.h> | |
29 | #endif | |
285d07e2 | 30 | #endif |
d7306fe6 | 31 | |
c3417a74 | 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; | |
267 | dpyinfo->bitmaps[id - 1].have_mask = 0; | |
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. */ | |
a773ed9a | 330 | if (openp (Vx_bitmap_file_path, file, Qnil, &found, make_number (R_OK)) < 0) |
4ef0d4d0 | 331 | return -1; |
4ef0d4d0 | 332 | |
51b59d79 | 333 | filename = SSDATA (found); |
4ef0d4d0 KS |
334 | |
335 | result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
336 | filename, &width, &height, &bitmap, &xhot, &yhot); | |
337 | if (result != BitmapSuccess) | |
338 | return -1; | |
339 | ||
340 | id = x_allocate_bitmap_record (f); | |
341 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
342 | dpyinfo->bitmaps[id - 1].have_mask = 0; | |
343 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
5b71542d | 344 | dpyinfo->bitmaps[id - 1].file = xlispstrdup (file); |
4ef0d4d0 KS |
345 | dpyinfo->bitmaps[id - 1].depth = 1; |
346 | dpyinfo->bitmaps[id - 1].height = height; | |
347 | dpyinfo->bitmaps[id - 1].width = width; | |
4ef0d4d0 KS |
348 | |
349 | return id; | |
350 | #endif /* HAVE_X_WINDOWS */ | |
351 | } | |
352 | ||
353 | /* Free bitmap B. */ | |
354 | ||
355 | static void | |
d3da34e0 | 356 | free_bitmap_record (Display_Info *dpyinfo, Bitmap_Record *bm) |
4ef0d4d0 KS |
357 | { |
358 | #ifdef HAVE_X_WINDOWS | |
359 | XFreePixmap (dpyinfo->display, bm->pixmap); | |
360 | if (bm->have_mask) | |
361 | XFreePixmap (dpyinfo->display, bm->mask); | |
362 | #endif /* HAVE_X_WINDOWS */ | |
363 | ||
364 | #ifdef HAVE_NTGUI | |
365 | DeleteObject (bm->pixmap); | |
366 | #endif /* HAVE_NTGUI */ | |
367 | ||
edfda783 | 368 | #ifdef HAVE_NS |
ed3751c8 | 369 | ns_release_object (bm->img); |
edfda783 AR |
370 | #endif |
371 | ||
4ef0d4d0 KS |
372 | if (bm->file) |
373 | { | |
374 | xfree (bm->file); | |
375 | bm->file = NULL; | |
376 | } | |
377 | } | |
378 | ||
379 | /* Remove reference to bitmap with id number ID. */ | |
380 | ||
381 | void | |
a10c8269 | 382 | x_destroy_bitmap (struct frame *f, ptrdiff_t id) |
4ef0d4d0 | 383 | { |
aad3612f | 384 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
4ef0d4d0 KS |
385 | |
386 | if (id > 0) | |
387 | { | |
388 | Bitmap_Record *bm = &dpyinfo->bitmaps[id - 1]; | |
389 | ||
390 | if (--bm->refcount == 0) | |
391 | { | |
4d7e6e51 | 392 | block_input (); |
65342ae3 | 393 | free_bitmap_record (dpyinfo, bm); |
4d7e6e51 | 394 | unblock_input (); |
4ef0d4d0 KS |
395 | } |
396 | } | |
397 | } | |
398 | ||
399 | /* Free all the bitmaps for the display specified by DPYINFO. */ | |
400 | ||
401 | void | |
d3da34e0 | 402 | x_destroy_all_bitmaps (Display_Info *dpyinfo) |
4ef0d4d0 | 403 | { |
0766b489 | 404 | ptrdiff_t i; |
4ef0d4d0 KS |
405 | Bitmap_Record *bm = dpyinfo->bitmaps; |
406 | ||
407 | for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++) | |
408 | if (bm->refcount > 0) | |
65342ae3 | 409 | free_bitmap_record (dpyinfo, bm); |
4ef0d4d0 KS |
410 | |
411 | dpyinfo->bitmaps_last = 0; | |
412 | } | |
413 | ||
578098f3 PE |
414 | static bool x_create_x_image_and_pixmap (struct frame *, int, int, int, |
415 | XImagePtr *, Pixmap *); | |
416 | static void x_destroy_x_image (XImagePtr ximg); | |
4ef0d4d0 | 417 | |
547c9269 YM |
418 | #ifdef HAVE_NTGUI |
419 | static XImagePtr_or_DC image_get_x_image_or_dc (struct frame *, struct image *, | |
420 | bool, HGDIOBJ *); | |
421 | static void image_unget_x_image_or_dc (struct image *, bool, XImagePtr_or_DC, | |
422 | HGDIOBJ); | |
423 | #else | |
424 | static XImagePtr image_get_x_image (struct frame *, struct image *, bool); | |
425 | static void image_unget_x_image (struct image *, bool, XImagePtr); | |
426 | #define image_get_x_image_or_dc(f, img, mask_p, dummy) \ | |
427 | image_get_x_image (f, img, mask_p) | |
428 | #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \ | |
429 | image_unget_x_image (img, mask_p, ximg) | |
430 | #endif | |
431 | ||
4ef0d4d0 KS |
432 | #ifdef HAVE_X_WINDOWS |
433 | ||
547c9269 YM |
434 | static void image_sync_to_pixmaps (struct frame *, struct image *); |
435 | ||
4ef0d4d0 KS |
436 | /* Useful functions defined in the section |
437 | `Image type independent image structures' below. */ | |
438 | ||
f57e2426 J |
439 | static unsigned long four_corners_best (XImagePtr ximg, |
440 | int *corners, | |
441 | unsigned long width, | |
442 | unsigned long height); | |
4ef0d4d0 | 443 | |
4ef0d4d0 KS |
444 | |
445 | /* Create a mask of a bitmap. Note is this not a perfect mask. | |
446 | It's nicer with some borders in this context */ | |
447 | ||
578098f3 | 448 | void |
0766b489 | 449 | x_create_bitmap_mask (struct frame *f, ptrdiff_t id) |
4ef0d4d0 KS |
450 | { |
451 | Pixmap pixmap, mask; | |
452 | XImagePtr ximg, mask_img; | |
453 | unsigned long width, height; | |
578098f3 | 454 | bool result; |
4ef0d4d0 KS |
455 | unsigned long bg; |
456 | unsigned long x, y, xp, xm, yp, ym; | |
457 | GC gc; | |
458 | ||
aad3612f | 459 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
4ef0d4d0 KS |
460 | |
461 | if (!(id > 0)) | |
578098f3 | 462 | return; |
4ef0d4d0 KS |
463 | |
464 | pixmap = x_bitmap_pixmap (f, id); | |
465 | width = x_bitmap_width (f, id); | |
466 | height = x_bitmap_height (f, id); | |
467 | ||
4d7e6e51 | 468 | block_input (); |
4ef0d4d0 KS |
469 | ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height, |
470 | ~0, ZPixmap); | |
471 | ||
472 | if (!ximg) | |
473 | { | |
4d7e6e51 | 474 | unblock_input (); |
578098f3 | 475 | return; |
4ef0d4d0 KS |
476 | } |
477 | ||
478 | result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask); | |
479 | ||
4d7e6e51 | 480 | unblock_input (); |
4ef0d4d0 KS |
481 | if (!result) |
482 | { | |
483 | XDestroyImage (ximg); | |
578098f3 | 484 | return; |
4ef0d4d0 KS |
485 | } |
486 | ||
6ac3c7bb | 487 | bg = four_corners_best (ximg, NULL, width, height); |
4ef0d4d0 KS |
488 | |
489 | for (y = 0; y < ximg->height; ++y) | |
490 | { | |
491 | for (x = 0; x < ximg->width; ++x) | |
492 | { | |
493 | xp = x != ximg->width - 1 ? x + 1 : 0; | |
494 | xm = x != 0 ? x - 1 : ximg->width - 1; | |
495 | yp = y != ximg->height - 1 ? y + 1 : 0; | |
496 | ym = y != 0 ? y - 1 : ximg->height - 1; | |
497 | if (XGetPixel (ximg, x, y) == bg | |
498 | && XGetPixel (ximg, x, yp) == bg | |
499 | && XGetPixel (ximg, x, ym) == bg | |
500 | && XGetPixel (ximg, xp, y) == bg | |
501 | && XGetPixel (ximg, xp, yp) == bg | |
502 | && XGetPixel (ximg, xp, ym) == bg | |
503 | && XGetPixel (ximg, xm, y) == bg | |
504 | && XGetPixel (ximg, xm, yp) == bg | |
505 | && XGetPixel (ximg, xm, ym) == bg) | |
506 | XPutPixel (mask_img, x, y, 0); | |
507 | else | |
508 | XPutPixel (mask_img, x, y, 1); | |
509 | } | |
510 | } | |
511 | ||
4d7e6e51 | 512 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
513 | gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL); |
514 | XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0, | |
515 | width, height); | |
516 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
517 | ||
518 | dpyinfo->bitmaps[id - 1].have_mask = 1; | |
519 | dpyinfo->bitmaps[id - 1].mask = mask; | |
520 | ||
521 | XDestroyImage (ximg); | |
522 | x_destroy_x_image (mask_img); | |
4ef0d4d0 KS |
523 | } |
524 | ||
525 | #endif /* HAVE_X_WINDOWS */ | |
526 | ||
527 | ||
528 | /*********************************************************************** | |
529 | Image types | |
530 | ***********************************************************************/ | |
531 | ||
4ef0d4d0 KS |
532 | /* List of supported image types. Use define_image_type to add new |
533 | types. Use lookup_image_type to find a type for a given symbol. */ | |
534 | ||
535 | static struct image_type *image_types; | |
536 | ||
537 | /* The symbol `xbm' which is used as the type symbol for XBM images. */ | |
538 | ||
955cbe7b | 539 | static Lisp_Object Qxbm; |
4ef0d4d0 KS |
540 | |
541 | /* Keywords. */ | |
542 | ||
955cbe7b | 543 | Lisp_Object QCascent, QCmargin, QCrelief; |
955cbe7b PE |
544 | Lisp_Object QCconversion; |
545 | static Lisp_Object QCheuristic_mask; | |
546 | static Lisp_Object QCcolor_symbols; | |
547 | static Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask, QCgeometry; | |
548 | static Lisp_Object QCcrop, QCrotation; | |
4ef0d4d0 KS |
549 | |
550 | /* Other symbols. */ | |
551 | ||
1100a63c | 552 | static Lisp_Object Qcount, Qextension_data, Qdelay; |
955cbe7b | 553 | static Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic; |
4ef0d4d0 | 554 | |
578098f3 | 555 | /* Forward function prototypes. */ |
4ef0d4d0 | 556 | |
641cfd14 | 557 | static struct image_type *lookup_image_type (Lisp_Object); |
f57e2426 J |
558 | static void x_laplace (struct frame *, struct image *); |
559 | static void x_emboss (struct frame *, struct image *); | |
578098f3 | 560 | static void x_build_heuristic_mask (struct frame *, struct image *, |
36a305a7 | 561 | Lisp_Object); |
0fda9b75 | 562 | #ifdef WINDOWSNT |
0855eb52 | 563 | #define CACHE_IMAGE_TYPE(type, status) \ |
0898ca10 | 564 | do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0) |
84d358f0 JB |
565 | #else |
566 | #define CACHE_IMAGE_TYPE(type, status) | |
a9e7a9d5 | 567 | #endif |
0855eb52 JB |
568 | |
569 | #define ADD_IMAGE_TYPE(type) \ | |
570 | do { Vimage_types = Fcons (type, Vimage_types); } while (0) | |
4ef0d4d0 KS |
571 | |
572 | /* Define a new image type from TYPE. This adds a copy of TYPE to | |
0855eb52 | 573 | image_types and caches the loading status of TYPE. */ |
4ef0d4d0 | 574 | |
bb4d86b4 | 575 | static struct image_type * |
d07ff9db | 576 | define_image_type (struct image_type *type) |
4ef0d4d0 | 577 | { |
bb4d86b4 CY |
578 | struct image_type *p = NULL; |
579 | Lisp_Object target_type = *type->type; | |
578098f3 | 580 | bool type_valid = 1; |
0855eb52 | 581 | |
4d7e6e51 | 582 | block_input (); |
bb4d86b4 CY |
583 | |
584 | for (p = image_types; p; p = p->next) | |
585 | if (EQ (*p->type, target_type)) | |
586 | goto done; | |
587 | ||
588 | if (type->init) | |
0855eb52 | 589 | { |
a9e7a9d5 | 590 | #if defined HAVE_NTGUI && defined WINDOWSNT |
bb4d86b4 CY |
591 | /* If we failed to load the library before, don't try again. */ |
592 | Lisp_Object tested = Fassq (target_type, Vlibrary_cache); | |
593 | if (CONSP (tested) && NILP (XCDR (tested))) | |
594 | type_valid = 0; | |
595 | else | |
596 | #endif | |
597 | { | |
d07ff9db | 598 | type_valid = type->init (); |
bb4d86b4 CY |
599 | CACHE_IMAGE_TYPE (target_type, type_valid ? Qt : Qnil); |
600 | } | |
601 | } | |
09c01941 | 602 | |
bb4d86b4 | 603 | if (type_valid) |
0855eb52 JB |
604 | { |
605 | /* Make a copy of TYPE to avoid a bus error in a dumped Emacs. | |
606 | The initialized data segment is read-only. */ | |
09c01941 | 607 | p = xmalloc (sizeof *p); |
ae1d87e2 | 608 | *p = *type; |
0855eb52 JB |
609 | p->next = image_types; |
610 | image_types = p; | |
0855eb52 JB |
611 | } |
612 | ||
bb4d86b4 | 613 | done: |
4d7e6e51 | 614 | unblock_input (); |
bb4d86b4 | 615 | return p; |
4ef0d4d0 KS |
616 | } |
617 | ||
618 | ||
578098f3 | 619 | /* Value is true if OBJECT is a valid Lisp image specification. A |
4ef0d4d0 KS |
620 | valid image specification is a list whose car is the symbol |
621 | `image', and whose rest is a property list. The property list must | |
622 | contain a value for key `:type'. That value must be the name of a | |
623 | supported image type. The rest of the property list depends on the | |
624 | image type. */ | |
625 | ||
578098f3 | 626 | bool |
d3da34e0 | 627 | valid_image_p (Lisp_Object object) |
4ef0d4d0 | 628 | { |
578098f3 | 629 | bool valid_p = 0; |
4ef0d4d0 KS |
630 | |
631 | if (IMAGEP (object)) | |
632 | { | |
633 | Lisp_Object tem; | |
634 | ||
635 | for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem)) | |
636 | if (EQ (XCAR (tem), QCtype)) | |
637 | { | |
638 | tem = XCDR (tem); | |
639 | if (CONSP (tem) && SYMBOLP (XCAR (tem))) | |
640 | { | |
641 | struct image_type *type; | |
642 | type = lookup_image_type (XCAR (tem)); | |
643 | if (type) | |
644 | valid_p = type->valid_p (object); | |
645 | } | |
646 | ||
647 | break; | |
648 | } | |
649 | } | |
650 | ||
651 | return valid_p; | |
652 | } | |
653 | ||
654 | ||
655 | /* Log error message with format string FORMAT and argument ARG. | |
656 | Signaling an error, e.g. when an image cannot be loaded, is not a | |
657 | good idea because this would interrupt redisplay, and the error | |
658 | message display would lead to another redisplay. This function | |
659 | therefore simply displays a message. */ | |
660 | ||
661 | static void | |
675e2c69 | 662 | image_error (const char *format, Lisp_Object arg1, Lisp_Object arg2) |
4ef0d4d0 KS |
663 | { |
664 | add_to_log (format, arg1, arg2); | |
665 | } | |
666 | ||
667 | ||
668 | \f | |
669 | /*********************************************************************** | |
670 | Image specifications | |
671 | ***********************************************************************/ | |
672 | ||
673 | enum image_value_type | |
674 | { | |
675 | IMAGE_DONT_CHECK_VALUE_TYPE, | |
676 | IMAGE_STRING_VALUE, | |
677 | IMAGE_STRING_OR_NIL_VALUE, | |
678 | IMAGE_SYMBOL_VALUE, | |
679 | IMAGE_POSITIVE_INTEGER_VALUE, | |
c4a07a4c | 680 | IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, |
4ef0d4d0 KS |
681 | IMAGE_NON_NEGATIVE_INTEGER_VALUE, |
682 | IMAGE_ASCENT_VALUE, | |
683 | IMAGE_INTEGER_VALUE, | |
684 | IMAGE_FUNCTION_VALUE, | |
685 | IMAGE_NUMBER_VALUE, | |
686 | IMAGE_BOOL_VALUE | |
687 | }; | |
688 | ||
689 | /* Structure used when parsing image specifications. */ | |
690 | ||
691 | struct image_keyword | |
692 | { | |
693 | /* Name of keyword. */ | |
675e2c69 | 694 | const char *name; |
4ef0d4d0 KS |
695 | |
696 | /* The type of value allowed. */ | |
697 | enum image_value_type type; | |
698 | ||
578098f3 PE |
699 | /* True means key must be present. */ |
700 | bool mandatory_p; | |
4ef0d4d0 KS |
701 | |
702 | /* Used to recognize duplicate keywords in a property list. */ | |
703 | int count; | |
704 | ||
705 | /* The value that was found. */ | |
706 | Lisp_Object value; | |
707 | }; | |
708 | ||
709 | ||
4ef0d4d0 KS |
710 | /* Parse image spec SPEC according to KEYWORDS. A valid image spec |
711 | has the format (image KEYWORD VALUE ...). One of the keyword/ | |
712 | value pairs must be `:type TYPE'. KEYWORDS is a vector of | |
713 | image_keywords structures of size NKEYWORDS describing other | |
578098f3 | 714 | allowed keyword/value pairs. Value is true if SPEC is valid. */ |
4ef0d4d0 | 715 | |
578098f3 | 716 | static bool |
d3da34e0 JB |
717 | parse_image_spec (Lisp_Object spec, struct image_keyword *keywords, |
718 | int nkeywords, Lisp_Object type) | |
4ef0d4d0 KS |
719 | { |
720 | int i; | |
721 | Lisp_Object plist; | |
722 | ||
723 | if (!IMAGEP (spec)) | |
724 | return 0; | |
725 | ||
726 | plist = XCDR (spec); | |
727 | while (CONSP (plist)) | |
728 | { | |
729 | Lisp_Object key, value; | |
730 | ||
731 | /* First element of a pair must be a symbol. */ | |
732 | key = XCAR (plist); | |
733 | plist = XCDR (plist); | |
734 | if (!SYMBOLP (key)) | |
735 | return 0; | |
736 | ||
737 | /* There must follow a value. */ | |
738 | if (!CONSP (plist)) | |
739 | return 0; | |
740 | value = XCAR (plist); | |
741 | plist = XCDR (plist); | |
742 | ||
743 | /* Find key in KEYWORDS. Error if not found. */ | |
744 | for (i = 0; i < nkeywords; ++i) | |
42a5b22f | 745 | if (strcmp (keywords[i].name, SSDATA (SYMBOL_NAME (key))) == 0) |
4ef0d4d0 KS |
746 | break; |
747 | ||
748 | if (i == nkeywords) | |
749 | continue; | |
750 | ||
751 | /* Record that we recognized the keyword. If a keywords | |
752 | was found more than once, it's an error. */ | |
753 | keywords[i].value = value; | |
4ef0d4d0 KS |
754 | if (keywords[i].count > 1) |
755 | return 0; | |
d311d28c | 756 | ++keywords[i].count; |
4ef0d4d0 KS |
757 | |
758 | /* Check type of value against allowed type. */ | |
759 | switch (keywords[i].type) | |
760 | { | |
761 | case IMAGE_STRING_VALUE: | |
762 | if (!STRINGP (value)) | |
763 | return 0; | |
764 | break; | |
765 | ||
766 | case IMAGE_STRING_OR_NIL_VALUE: | |
767 | if (!STRINGP (value) && !NILP (value)) | |
768 | return 0; | |
769 | break; | |
770 | ||
771 | case IMAGE_SYMBOL_VALUE: | |
772 | if (!SYMBOLP (value)) | |
773 | return 0; | |
774 | break; | |
775 | ||
776 | case IMAGE_POSITIVE_INTEGER_VALUE: | |
13464394 | 777 | if (! RANGED_INTEGERP (1, value, INT_MAX)) |
4ef0d4d0 KS |
778 | return 0; |
779 | break; | |
780 | ||
c4a07a4c PE |
781 | case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR: |
782 | if (RANGED_INTEGERP (0, value, INT_MAX)) | |
4ef0d4d0 KS |
783 | break; |
784 | if (CONSP (value) | |
c4a07a4c PE |
785 | && RANGED_INTEGERP (0, XCAR (value), INT_MAX) |
786 | && RANGED_INTEGERP (0, XCDR (value), INT_MAX)) | |
4ef0d4d0 KS |
787 | break; |
788 | return 0; | |
789 | ||
790 | case IMAGE_ASCENT_VALUE: | |
791 | if (SYMBOLP (value) && EQ (value, Qcenter)) | |
792 | break; | |
13464394 | 793 | else if (RANGED_INTEGERP (0, value, 100)) |
4ef0d4d0 KS |
794 | break; |
795 | return 0; | |
796 | ||
797 | case IMAGE_NON_NEGATIVE_INTEGER_VALUE: | |
13464394 PE |
798 | /* Unlike the other integer-related cases, this one does not |
799 | verify that VALUE fits in 'int'. This is because callers | |
800 | want EMACS_INT. */ | |
4ef0d4d0 KS |
801 | if (!INTEGERP (value) || XINT (value) < 0) |
802 | return 0; | |
803 | break; | |
804 | ||
805 | case IMAGE_DONT_CHECK_VALUE_TYPE: | |
806 | break; | |
807 | ||
808 | case IMAGE_FUNCTION_VALUE: | |
809 | value = indirect_function (value); | |
ca105506 | 810 | if (!NILP (Ffunctionp (value))) |
4ef0d4d0 KS |
811 | break; |
812 | return 0; | |
813 | ||
814 | case IMAGE_NUMBER_VALUE: | |
815 | if (!INTEGERP (value) && !FLOATP (value)) | |
816 | return 0; | |
817 | break; | |
818 | ||
819 | case IMAGE_INTEGER_VALUE: | |
13464394 | 820 | if (! TYPE_RANGED_INTEGERP (int, value)) |
4ef0d4d0 KS |
821 | return 0; |
822 | break; | |
823 | ||
824 | case IMAGE_BOOL_VALUE: | |
825 | if (!NILP (value) && !EQ (value, Qt)) | |
826 | return 0; | |
827 | break; | |
828 | ||
829 | default: | |
1088b922 | 830 | emacs_abort (); |
4ef0d4d0 KS |
831 | break; |
832 | } | |
833 | ||
834 | if (EQ (key, QCtype) && !EQ (type, value)) | |
835 | return 0; | |
836 | } | |
837 | ||
838 | /* Check that all mandatory fields are present. */ | |
839 | for (i = 0; i < nkeywords; ++i) | |
840 | if (keywords[i].mandatory_p && keywords[i].count == 0) | |
841 | return 0; | |
842 | ||
843 | return NILP (plist); | |
844 | } | |
845 | ||
846 | ||
847 | /* Return the value of KEY in image specification SPEC. Value is nil | |
578098f3 PE |
848 | if KEY is not present in SPEC. Set *FOUND depending on whether KEY |
849 | was found in SPEC. */ | |
4ef0d4d0 KS |
850 | |
851 | static Lisp_Object | |
578098f3 | 852 | image_spec_value (Lisp_Object spec, Lisp_Object key, bool *found) |
4ef0d4d0 KS |
853 | { |
854 | Lisp_Object tail; | |
855 | ||
a54e2c05 | 856 | eassert (valid_image_p (spec)); |
4ef0d4d0 KS |
857 | |
858 | for (tail = XCDR (spec); | |
859 | CONSP (tail) && CONSP (XCDR (tail)); | |
860 | tail = XCDR (XCDR (tail))) | |
861 | { | |
862 | if (EQ (XCAR (tail), key)) | |
863 | { | |
864 | if (found) | |
865 | *found = 1; | |
866 | return XCAR (XCDR (tail)); | |
867 | } | |
868 | } | |
869 | ||
870 | if (found) | |
871 | *found = 0; | |
872 | return Qnil; | |
873 | } | |
874 | ||
875 | ||
876 | DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0, | |
877 | doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT). | |
878 | PIXELS non-nil means return the size in pixels, otherwise return the | |
879 | size in canonical character units. | |
880 | FRAME is the frame on which the image will be displayed. FRAME nil | |
881 | or omitted means use the selected frame. */) | |
5842a27b | 882 | (Lisp_Object spec, Lisp_Object pixels, Lisp_Object frame) |
4ef0d4d0 KS |
883 | { |
884 | Lisp_Object size; | |
885 | ||
886 | size = Qnil; | |
887 | if (valid_image_p (spec)) | |
888 | { | |
7452b7bd | 889 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 890 | ptrdiff_t id = lookup_image (f, spec); |
4ef0d4d0 KS |
891 | struct image *img = IMAGE_FROM_ID (f, id); |
892 | int width = img->width + 2 * img->hmargin; | |
893 | int height = img->height + 2 * img->vmargin; | |
894 | ||
895 | if (NILP (pixels)) | |
896 | size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)), | |
897 | make_float ((double) height / FRAME_LINE_HEIGHT (f))); | |
898 | else | |
899 | size = Fcons (make_number (width), make_number (height)); | |
900 | } | |
901 | else | |
902 | error ("Invalid image specification"); | |
903 | ||
904 | return size; | |
905 | } | |
906 | ||
907 | ||
908 | DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0, | |
909 | doc: /* Return t if image SPEC has a mask bitmap. | |
910 | FRAME is the frame on which the image will be displayed. FRAME nil | |
911 | or omitted means use the selected frame. */) | |
5842a27b | 912 | (Lisp_Object spec, Lisp_Object frame) |
4ef0d4d0 KS |
913 | { |
914 | Lisp_Object mask; | |
915 | ||
916 | mask = Qnil; | |
917 | if (valid_image_p (spec)) | |
918 | { | |
7452b7bd | 919 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 920 | ptrdiff_t id = lookup_image (f, spec); |
4ef0d4d0 KS |
921 | struct image *img = IMAGE_FROM_ID (f, id); |
922 | if (img->mask) | |
923 | mask = Qt; | |
924 | } | |
925 | else | |
926 | error ("Invalid image specification"); | |
927 | ||
928 | return mask; | |
929 | } | |
930 | ||
1546c559 JL |
931 | DEFUN ("image-metadata", Fimage_metadata, Simage_metadata, 1, 2, 0, |
932 | doc: /* Return metadata for image SPEC. | |
6ac3c7bb KS |
933 | FRAME is the frame on which the image will be displayed. FRAME nil |
934 | or omitted means use the selected frame. */) | |
5842a27b | 935 | (Lisp_Object spec, Lisp_Object frame) |
6ac3c7bb KS |
936 | { |
937 | Lisp_Object ext; | |
938 | ||
939 | ext = Qnil; | |
940 | if (valid_image_p (spec)) | |
941 | { | |
7452b7bd | 942 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 943 | ptrdiff_t id = lookup_image (f, spec); |
6ac3c7bb | 944 | struct image *img = IMAGE_FROM_ID (f, id); |
b50691aa | 945 | ext = img->lisp_data; |
6ac3c7bb KS |
946 | } |
947 | ||
948 | return ext; | |
949 | } | |
950 | ||
4ef0d4d0 KS |
951 | \f |
952 | /*********************************************************************** | |
953 | Image type independent image structures | |
954 | ***********************************************************************/ | |
955 | ||
11273115 | 956 | #define MAX_IMAGE_SIZE 10.0 |
4ef0d4d0 KS |
957 | /* Allocate and return a new image structure for image specification |
958 | SPEC. SPEC has a hash value of HASH. */ | |
959 | ||
960 | static struct image * | |
0de4bb68 | 961 | make_image (Lisp_Object spec, EMACS_UINT hash) |
4ef0d4d0 | 962 | { |
23f86fce | 963 | struct image *img = xzalloc (sizeof *img); |
a2bc5bdd | 964 | Lisp_Object file = image_spec_value (spec, QCfile, NULL); |
4ef0d4d0 | 965 | |
a54e2c05 | 966 | eassert (valid_image_p (spec)); |
a2bc5bdd | 967 | img->dependencies = NILP (file) ? Qnil : list1 (file); |
4ef0d4d0 | 968 | img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL)); |
a54e2c05 | 969 | eassert (img->type != NULL); |
4ef0d4d0 | 970 | img->spec = spec; |
b50691aa | 971 | img->lisp_data = Qnil; |
4ef0d4d0 KS |
972 | img->ascent = DEFAULT_IMAGE_ASCENT; |
973 | img->hash = hash; | |
6ac3c7bb | 974 | img->corners[BOT_CORNER] = -1; /* Full image */ |
4ef0d4d0 KS |
975 | return img; |
976 | } | |
977 | ||
978 | ||
979 | /* Free image IMG which was used on frame F, including its resources. */ | |
980 | ||
981 | static void | |
d3da34e0 | 982 | free_image (struct frame *f, struct image *img) |
4ef0d4d0 KS |
983 | { |
984 | if (img) | |
985 | { | |
354884c4 | 986 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
987 | |
988 | /* Remove IMG from the hash table of its cache. */ | |
989 | if (img->prev) | |
990 | img->prev->next = img->next; | |
991 | else | |
992 | c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next; | |
993 | ||
994 | if (img->next) | |
995 | img->next->prev = img->prev; | |
996 | ||
997 | c->images[img->id] = NULL; | |
998 | ||
999 | /* Free resources, then free IMG. */ | |
1000 | img->type->free (f, img); | |
1001 | xfree (img); | |
1002 | } | |
1003 | } | |
1004 | ||
578098f3 | 1005 | /* Return true if the given widths and heights are valid for display. */ |
f1f25b99 | 1006 | |
578098f3 | 1007 | static bool |
d3da34e0 | 1008 | check_image_size (struct frame *f, int width, int height) |
f1f25b99 | 1009 | { |
7df4765a | 1010 | int w, h; |
f1f25b99 | 1011 | |
7df4765a | 1012 | if (width <= 0 || height <= 0) |
f1f25b99 CY |
1013 | return 0; |
1014 | ||
7df4765a | 1015 | if (INTEGERP (Vmax_image_size)) |
3f791afe PE |
1016 | return (width <= XINT (Vmax_image_size) |
1017 | && height <= XINT (Vmax_image_size)); | |
7df4765a KS |
1018 | else if (FLOATP (Vmax_image_size)) |
1019 | { | |
e5d76069 PE |
1020 | if (f != NULL) |
1021 | { | |
1022 | w = FRAME_PIXEL_WIDTH (f); | |
1023 | h = FRAME_PIXEL_HEIGHT (f); | |
1024 | } | |
1025 | else | |
1026 | w = h = 1024; /* Arbitrary size for unknown frame. */ | |
3f791afe PE |
1027 | return (width <= XFLOAT_DATA (Vmax_image_size) * w |
1028 | && height <= XFLOAT_DATA (Vmax_image_size) * h); | |
7df4765a KS |
1029 | } |
1030 | else | |
1031 | return 1; | |
f1f25b99 | 1032 | } |
4ef0d4d0 KS |
1033 | |
1034 | /* Prepare image IMG for display on frame F. Must be called before | |
1035 | drawing an image. */ | |
1036 | ||
1037 | void | |
d3da34e0 | 1038 | prepare_image_for_display (struct frame *f, struct image *img) |
4ef0d4d0 | 1039 | { |
4ef0d4d0 | 1040 | /* We're about to display IMG, so set its timestamp to `now'. */ |
43aac990 | 1041 | img->timestamp = current_timespec (); |
4ef0d4d0 KS |
1042 | |
1043 | /* If IMG doesn't have a pixmap yet, load it now, using the image | |
1044 | type dependent loader function. */ | |
1045 | if (img->pixmap == NO_PIXMAP && !img->load_failed_p) | |
578098f3 | 1046 | img->load_failed_p = ! img->type->load (f, img); |
c7fea325 | 1047 | |
547c9269 YM |
1048 | #ifdef HAVE_X_WINDOWS |
1049 | if (!img->load_failed_p) | |
1050 | { | |
1051 | block_input (); | |
1052 | image_sync_to_pixmaps (f, img); | |
1053 | unblock_input (); | |
1054 | } | |
1055 | #endif | |
4ef0d4d0 KS |
1056 | } |
1057 | ||
1058 | ||
1059 | /* Value is the number of pixels for the ascent of image IMG when | |
1060 | drawn in face FACE. */ | |
1061 | ||
1062 | int | |
d3da34e0 | 1063 | image_ascent (struct image *img, struct face *face, struct glyph_slice *slice) |
4ef0d4d0 | 1064 | { |
1938bc36 | 1065 | int height; |
4ef0d4d0 KS |
1066 | int ascent; |
1067 | ||
1938bc36 KS |
1068 | if (slice->height == img->height) |
1069 | height = img->height + img->vmargin; | |
1070 | else if (slice->y == 0) | |
1071 | height = slice->height + img->vmargin; | |
1072 | else | |
1073 | height = slice->height; | |
1074 | ||
4ef0d4d0 KS |
1075 | if (img->ascent == CENTERED_IMAGE_ASCENT) |
1076 | { | |
1077 | if (face->font) | |
1078 | { | |
1079 | #ifdef HAVE_NTGUI | |
1080 | /* W32 specific version. Why?. ++kfs */ | |
5fc9fdeb JR |
1081 | ascent = height / 2 - (FONT_DESCENT (face->font) |
1082 | - FONT_BASE (face->font)) / 2; | |
4ef0d4d0 KS |
1083 | #else |
1084 | /* This expression is arranged so that if the image can't be | |
1085 | exactly centered, it will be moved slightly up. This is | |
1086 | because a typical font is `top-heavy' (due to the presence | |
1087 | uppercase letters), so the image placement should err towards | |
1088 | being top-heavy too. It also just generally looks better. */ | |
5e617bc2 JB |
1089 | ascent = (height + FONT_BASE (face->font) |
1090 | - FONT_DESCENT (face->font) + 1) / 2; | |
4ef0d4d0 KS |
1091 | #endif /* HAVE_NTGUI */ |
1092 | } | |
1093 | else | |
1094 | ascent = height / 2; | |
1095 | } | |
1096 | else | |
13464394 | 1097 | ascent = height * (img->ascent / 100.0); |
4ef0d4d0 KS |
1098 | |
1099 | return ascent; | |
1100 | } | |
1101 | ||
1102 | \f | |
1103 | /* Image background colors. */ | |
1104 | ||
1105 | /* Find the "best" corner color of a bitmap. | |
1106 | On W32, XIMG is assumed to a device context with the bitmap selected. */ | |
1107 | ||
1108 | static RGB_PIXEL_COLOR | |
d3da34e0 JB |
1109 | four_corners_best (XImagePtr_or_DC ximg, int *corners, |
1110 | unsigned long width, unsigned long height) | |
4ef0d4d0 | 1111 | { |
f0c77cd1 | 1112 | RGB_PIXEL_COLOR corner_pixels[4], best IF_LINT (= 0); |
4ef0d4d0 KS |
1113 | int i, best_count; |
1114 | ||
6ac3c7bb KS |
1115 | if (corners && corners[BOT_CORNER] >= 0) |
1116 | { | |
1117 | /* Get the colors at the corner_pixels of ximg. */ | |
1118 | corner_pixels[0] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[TOP_CORNER]); | |
1119 | corner_pixels[1] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[TOP_CORNER]); | |
1120 | corner_pixels[2] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[BOT_CORNER] - 1); | |
1121 | corner_pixels[3] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[BOT_CORNER] - 1); | |
1122 | } | |
1123 | else | |
1124 | { | |
1125 | /* Get the colors at the corner_pixels of ximg. */ | |
1126 | corner_pixels[0] = GET_PIXEL (ximg, 0, 0); | |
1127 | corner_pixels[1] = GET_PIXEL (ximg, width - 1, 0); | |
1128 | corner_pixels[2] = GET_PIXEL (ximg, width - 1, height - 1); | |
1129 | corner_pixels[3] = GET_PIXEL (ximg, 0, height - 1); | |
1130 | } | |
4ef0d4d0 KS |
1131 | /* Choose the most frequently found color as background. */ |
1132 | for (i = best_count = 0; i < 4; ++i) | |
1133 | { | |
1134 | int j, n; | |
1135 | ||
1136 | for (j = n = 0; j < 4; ++j) | |
6ac3c7bb | 1137 | if (corner_pixels[i] == corner_pixels[j]) |
4ef0d4d0 KS |
1138 | ++n; |
1139 | ||
1140 | if (n > best_count) | |
6ac3c7bb | 1141 | best = corner_pixels[i], best_count = n; |
4ef0d4d0 KS |
1142 | } |
1143 | ||
1144 | return best; | |
1145 | } | |
1146 | ||
1147 | /* Portability macros */ | |
1148 | ||
1149 | #ifdef HAVE_NTGUI | |
1150 | ||
4ef0d4d0 KS |
1151 | #define Free_Pixmap(display, pixmap) \ |
1152 | DeleteObject (pixmap) | |
1153 | ||
edfda783 AR |
1154 | #elif defined (HAVE_NS) |
1155 | ||
edfda783 | 1156 | #define Free_Pixmap(display, pixmap) \ |
ed3751c8 | 1157 | ns_release_object (pixmap) |
edfda783 | 1158 | |
4ef0d4d0 KS |
1159 | #else |
1160 | ||
4ef0d4d0 KS |
1161 | #define Free_Pixmap(display, pixmap) \ |
1162 | XFreePixmap (display, pixmap) | |
1163 | ||
edfda783 | 1164 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
1165 | |
1166 | ||
1167 | /* Return the `background' field of IMG. If IMG doesn't have one yet, | |
1168 | it is guessed heuristically. If non-zero, XIMG is an existing | |
1169 | XImage object (or device context with the image selected on W32) to | |
1f026899 | 1170 | use for the heuristic. */ |
4ef0d4d0 KS |
1171 | |
1172 | RGB_PIXEL_COLOR | |
d3da34e0 | 1173 | image_background (struct image *img, struct frame *f, XImagePtr_or_DC ximg) |
4ef0d4d0 KS |
1174 | { |
1175 | if (! img->background_valid) | |
1176 | /* IMG doesn't have a background yet, try to guess a reasonable value. */ | |
1177 | { | |
578098f3 | 1178 | bool free_ximg = !ximg; |
4ef0d4d0 KS |
1179 | #ifdef HAVE_NTGUI |
1180 | HGDIOBJ prev; | |
1181 | #endif /* HAVE_NTGUI */ | |
1182 | ||
1183 | if (free_ximg) | |
547c9269 | 1184 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); |
4ef0d4d0 | 1185 | |
6ac3c7bb | 1186 | img->background = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
1187 | |
1188 | if (free_ximg) | |
547c9269 | 1189 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
1f026899 | 1190 | |
4ef0d4d0 KS |
1191 | img->background_valid = 1; |
1192 | } | |
1193 | ||
1194 | return img->background; | |
1195 | } | |
1196 | ||
1197 | /* Return the `background_transparent' field of IMG. If IMG doesn't | |
1198 | have one yet, it is guessed heuristically. If non-zero, MASK is an | |
1199 | existing XImage object to use for the heuristic. */ | |
1200 | ||
1201 | int | |
d3da34e0 | 1202 | image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_DC mask) |
4ef0d4d0 KS |
1203 | { |
1204 | if (! img->background_transparent_valid) | |
1205 | /* IMG doesn't have a background yet, try to guess a reasonable value. */ | |
1206 | { | |
1207 | if (img->mask) | |
1208 | { | |
578098f3 | 1209 | bool free_mask = !mask; |
4ef0d4d0 KS |
1210 | #ifdef HAVE_NTGUI |
1211 | HGDIOBJ prev; | |
1212 | #endif /* HAVE_NTGUI */ | |
1213 | ||
1214 | if (free_mask) | |
547c9269 | 1215 | mask = image_get_x_image_or_dc (f, img, 1, &prev); |
4ef0d4d0 KS |
1216 | |
1217 | img->background_transparent | |
6ac3c7bb | 1218 | = (four_corners_best (mask, img->corners, img->width, img->height) == PIX_MASK_RETAIN); |
4ef0d4d0 KS |
1219 | |
1220 | if (free_mask) | |
547c9269 | 1221 | image_unget_x_image_or_dc (img, 1, mask, prev); |
4ef0d4d0 KS |
1222 | } |
1223 | else | |
1224 | img->background_transparent = 0; | |
1225 | ||
1226 | img->background_transparent_valid = 1; | |
1227 | } | |
1228 | ||
1229 | return img->background_transparent; | |
1230 | } | |
1231 | ||
1232 | \f | |
1233 | /*********************************************************************** | |
1234 | Helper functions for X image types | |
1235 | ***********************************************************************/ | |
1236 | ||
547c9269 YM |
1237 | /* Clear X resources of image IMG on frame F according to FLAGS. |
1238 | FLAGS is bitwise-or of the following masks: | |
1239 | CLEAR_IMAGE_PIXMAP free the pixmap if any. | |
1240 | CLEAR_IMAGE_MASK means clear the mask pixmap if any. | |
1241 | CLEAR_IMAGE_COLORS means free colors allocated for the image, if | |
1242 | any. */ | |
1243 | ||
1244 | #define CLEAR_IMAGE_PIXMAP (1 << 0) | |
1245 | #define CLEAR_IMAGE_MASK (1 << 1) | |
1246 | #define CLEAR_IMAGE_COLORS (1 << 2) | |
4ef0d4d0 KS |
1247 | |
1248 | static void | |
547c9269 | 1249 | x_clear_image_1 (struct frame *f, struct image *img, int flags) |
4ef0d4d0 | 1250 | { |
547c9269 | 1251 | if (flags & CLEAR_IMAGE_PIXMAP) |
4ef0d4d0 | 1252 | { |
547c9269 YM |
1253 | if (img->pixmap) |
1254 | { | |
1255 | Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap); | |
1256 | img->pixmap = NO_PIXMAP; | |
1257 | /* NOTE (HAVE_NS): background color is NOT an indexed color! */ | |
1258 | img->background_valid = 0; | |
1259 | } | |
1260 | #ifdef HAVE_X_WINDOWS | |
1261 | if (img->ximg) | |
1262 | { | |
1263 | x_destroy_x_image (img->ximg); | |
1264 | img->ximg = NULL; | |
1265 | img->background_valid = 0; | |
1266 | } | |
1267 | #endif | |
4ef0d4d0 KS |
1268 | } |
1269 | ||
547c9269 | 1270 | if (flags & CLEAR_IMAGE_MASK) |
4ef0d4d0 | 1271 | { |
547c9269 YM |
1272 | if (img->mask) |
1273 | { | |
1274 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
1275 | img->mask = NO_PIXMAP; | |
1276 | img->background_transparent_valid = 0; | |
1277 | } | |
1278 | #ifdef HAVE_X_WINDOWS | |
1279 | if (img->mask_img) | |
1280 | { | |
1281 | x_destroy_x_image (img->mask_img); | |
1282 | img->mask_img = NULL; | |
1283 | img->background_transparent_valid = 0; | |
1284 | } | |
1285 | #endif | |
4ef0d4d0 KS |
1286 | } |
1287 | ||
547c9269 | 1288 | if ((flags & CLEAR_IMAGE_COLORS) && img->ncolors) |
4ef0d4d0 | 1289 | { |
4ef0d4d0 KS |
1290 | /* W32_TODO: color table support. */ |
1291 | #ifdef HAVE_X_WINDOWS | |
1292 | x_free_colors (f, img->colors, img->ncolors); | |
1293 | #endif /* HAVE_X_WINDOWS */ | |
1294 | xfree (img->colors); | |
1295 | img->colors = NULL; | |
1296 | img->ncolors = 0; | |
1297 | } | |
c7fea325 | 1298 | |
4ef0d4d0 KS |
1299 | } |
1300 | ||
1301 | /* Free X resources of image IMG which is used on frame F. */ | |
1302 | ||
1303 | static void | |
d3da34e0 | 1304 | x_clear_image (struct frame *f, struct image *img) |
4ef0d4d0 | 1305 | { |
4d7e6e51 | 1306 | block_input (); |
547c9269 YM |
1307 | x_clear_image_1 (f, img, |
1308 | CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_MASK | CLEAR_IMAGE_COLORS); | |
4d7e6e51 | 1309 | unblock_input (); |
4ef0d4d0 KS |
1310 | } |
1311 | ||
1312 | ||
1313 | /* Allocate color COLOR_NAME for image IMG on frame F. If color | |
1314 | cannot be allocated, use DFLT. Add a newly allocated color to | |
1315 | IMG->colors, so that it can be freed again. Value is the pixel | |
1316 | color. */ | |
1317 | ||
1318 | static unsigned long | |
d3da34e0 JB |
1319 | x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name, |
1320 | unsigned long dflt) | |
4ef0d4d0 KS |
1321 | { |
1322 | XColor color; | |
1323 | unsigned long result; | |
1324 | ||
a54e2c05 | 1325 | eassert (STRINGP (color_name)); |
4ef0d4d0 | 1326 | |
3f791afe PE |
1327 | if (x_defined_color (f, SSDATA (color_name), &color, 1) |
1328 | && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors, | |
1329 | INT_MAX)) | |
4ef0d4d0 KS |
1330 | { |
1331 | /* This isn't called frequently so we get away with simply | |
1332 | reallocating the color vector to the needed size, here. */ | |
ddff3151 | 1333 | ptrdiff_t ncolors = img->ncolors + 1; |
38182d90 | 1334 | img->colors = xrealloc (img->colors, ncolors * sizeof *img->colors); |
ddff3151 PE |
1335 | img->colors[ncolors - 1] = color.pixel; |
1336 | img->ncolors = ncolors; | |
4ef0d4d0 KS |
1337 | result = color.pixel; |
1338 | } | |
1339 | else | |
1340 | result = dflt; | |
1341 | ||
1342 | return result; | |
1343 | } | |
1344 | ||
1345 | ||
1346 | \f | |
1347 | /*********************************************************************** | |
1348 | Image Cache | |
1349 | ***********************************************************************/ | |
1350 | ||
f57e2426 | 1351 | static void cache_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
1352 | |
1353 | /* Return a new, initialized image cache that is allocated from the | |
1354 | heap. Call free_image_cache to free an image cache. */ | |
1355 | ||
1356 | struct image_cache * | |
d3da34e0 | 1357 | make_image_cache (void) |
4ef0d4d0 | 1358 | { |
9c253307 DA |
1359 | struct image_cache *c = xmalloc (sizeof *c); |
1360 | ||
1361 | c->size = 50; | |
1362 | c->used = c->refcount = 0; | |
1363 | c->images = xmalloc (c->size * sizeof *c->images); | |
1364 | c->buckets = xzalloc (IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets); | |
4ef0d4d0 KS |
1365 | return c; |
1366 | } | |
1367 | ||
1368 | ||
d3c35d17 CY |
1369 | /* Find an image matching SPEC in the cache, and return it. If no |
1370 | image is found, return NULL. */ | |
1371 | static struct image * | |
0de4bb68 | 1372 | search_image_cache (struct frame *f, Lisp_Object spec, EMACS_UINT hash) |
483e7ae0 | 1373 | { |
91be6b66 | 1374 | struct image *img; |
354884c4 | 1375 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
483e7ae0 CY |
1376 | int i = hash % IMAGE_CACHE_BUCKETS_SIZE; |
1377 | ||
8198352b CY |
1378 | if (!c) return NULL; |
1379 | ||
d3c35d17 CY |
1380 | /* If the image spec does not specify a background color, the cached |
1381 | image must have the same background color as the current frame. | |
cf26ebe8 CY |
1382 | The foreground color must also match, for the sake of monochrome |
1383 | images. | |
1384 | ||
1385 | In fact, we could ignore the foreground color matching condition | |
1386 | for color images, or if the image spec specifies :foreground; | |
1387 | similarly we could ignore the background color matching condition | |
1388 | for formats that don't use transparency (such as jpeg), or if the | |
1389 | image spec specifies :background. However, the extra memory | |
1390 | usage is probably negligible in practice, so we don't bother. */ | |
d3c35d17 | 1391 | |
483e7ae0 | 1392 | for (img = c->buckets[i]; img; img = img->next) |
d3c35d17 CY |
1393 | if (img->hash == hash |
1394 | && !NILP (Fequal (img->spec, spec)) | |
c5b8e0ea YM |
1395 | && img->frame_foreground == FRAME_FOREGROUND_PIXEL (f) |
1396 | && img->frame_background == FRAME_BACKGROUND_PIXEL (f)) | |
d3c35d17 CY |
1397 | break; |
1398 | return img; | |
1399 | } | |
1400 | ||
1401 | ||
1402 | /* Search frame F for an image with spec SPEC, and free it. */ | |
1403 | ||
1404 | static void | |
d3da34e0 | 1405 | uncache_image (struct frame *f, Lisp_Object spec) |
d3c35d17 CY |
1406 | { |
1407 | struct image *img = search_image_cache (f, spec, sxhash (spec, 0)); | |
1408 | if (img) | |
be3faa80 CY |
1409 | { |
1410 | free_image (f, img); | |
1411 | /* As display glyphs may still be referring to the image ID, we | |
1412 | must garbage the frame (Bug#6426). */ | |
1413 | SET_FRAME_GARBAGED (f); | |
1414 | } | |
483e7ae0 CY |
1415 | } |
1416 | ||
1417 | ||
4ef0d4d0 KS |
1418 | /* Free image cache of frame F. Be aware that X frames share images |
1419 | caches. */ | |
1420 | ||
1421 | void | |
d3da34e0 | 1422 | free_image_cache (struct frame *f) |
4ef0d4d0 | 1423 | { |
354884c4 | 1424 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
1425 | if (c) |
1426 | { | |
13464394 | 1427 | ptrdiff_t i; |
4ef0d4d0 KS |
1428 | |
1429 | /* Cache should not be referenced by any frame when freed. */ | |
a54e2c05 | 1430 | eassert (c->refcount == 0); |
4ef0d4d0 KS |
1431 | |
1432 | for (i = 0; i < c->used; ++i) | |
1433 | free_image (f, c->images[i]); | |
1434 | xfree (c->images); | |
1435 | xfree (c->buckets); | |
1436 | xfree (c); | |
354884c4 | 1437 | FRAME_IMAGE_CACHE (f) = NULL; |
4ef0d4d0 KS |
1438 | } |
1439 | } | |
1440 | ||
1441 | ||
a2bc5bdd SM |
1442 | /* Clear image cache of frame F. FILTER=t means free all images. |
1443 | FILTER=nil means clear only images that haven't been | |
1444 | displayed for some time. | |
1445 | Else, only free the images which have FILTER in their `dependencies'. | |
1446 | Should be called from time to time to reduce the number of loaded images. | |
1447 | If image-cache-eviction-delay is non-nil, this frees images in the cache | |
1448 | which weren't displayed for at least that many seconds. */ | |
4ef0d4d0 | 1449 | |
ce0ad53d | 1450 | static void |
a2bc5bdd | 1451 | clear_image_cache (struct frame *f, Lisp_Object filter) |
4ef0d4d0 | 1452 | { |
354884c4 | 1453 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 | 1454 | |
98fe5161 | 1455 | if (c) |
4ef0d4d0 | 1456 | { |
13464394 | 1457 | ptrdiff_t i, nfreed = 0; |
4ef0d4d0 KS |
1458 | |
1459 | /* Block input so that we won't be interrupted by a SIGIO | |
1460 | while being in an inconsistent state. */ | |
4d7e6e51 | 1461 | block_input (); |
4ef0d4d0 | 1462 | |
98fe5161 CY |
1463 | if (!NILP (filter)) |
1464 | { | |
1465 | /* Filter image cache. */ | |
1466 | for (i = 0; i < c->used; ++i) | |
1467 | { | |
1468 | struct image *img = c->images[i]; | |
1469 | if (img && (EQ (Qt, filter) | |
1470 | || !NILP (Fmember (filter, img->dependencies)))) | |
1471 | { | |
1472 | free_image (f, img); | |
1473 | ++nfreed; | |
1474 | } | |
1475 | } | |
1476 | } | |
1477 | else if (INTEGERP (Vimage_cache_eviction_delay)) | |
4ef0d4d0 | 1478 | { |
98fe5161 | 1479 | /* Free cache based on timestamp. */ |
43aac990 | 1480 | struct timespec old, t; |
d35af63c | 1481 | double delay; |
13464394 | 1482 | ptrdiff_t nimages = 0; |
98fe5161 CY |
1483 | |
1484 | for (i = 0; i < c->used; ++i) | |
1485 | if (c->images[i]) | |
1486 | nimages++; | |
1487 | ||
1488 | /* If the number of cached images has grown unusually large, | |
1489 | decrease the cache eviction delay (Bug#6230). */ | |
13464394 | 1490 | delay = XINT (Vimage_cache_eviction_delay); |
98fe5161 | 1491 | if (nimages > 40) |
13464394 PE |
1492 | delay = 1600 * delay / nimages / nimages; |
1493 | delay = max (delay, 1); | |
98fe5161 | 1494 | |
43aac990 PE |
1495 | t = current_timespec (); |
1496 | old = timespec_sub (t, dtotimespec (delay)); | |
98fe5161 CY |
1497 | |
1498 | for (i = 0; i < c->used; ++i) | |
4ef0d4d0 | 1499 | { |
98fe5161 | 1500 | struct image *img = c->images[i]; |
43aac990 | 1501 | if (img && timespec_cmp (img->timestamp, old) < 0) |
98fe5161 CY |
1502 | { |
1503 | free_image (f, img); | |
1504 | ++nfreed; | |
1505 | } | |
4ef0d4d0 KS |
1506 | } |
1507 | } | |
1508 | ||
1509 | /* We may be clearing the image cache because, for example, | |
1510 | Emacs was iconified for a longer period of time. In that | |
1511 | case, current matrices may still contain references to | |
1512 | images freed above. So, clear these matrices. */ | |
1513 | if (nfreed) | |
1514 | { | |
1515 | Lisp_Object tail, frame; | |
1516 | ||
1517 | FOR_EACH_FRAME (tail, frame) | |
1518 | { | |
6ae141d6 PE |
1519 | struct frame *fr = XFRAME (frame); |
1520 | if (FRAME_IMAGE_CACHE (fr) == c) | |
1521 | clear_current_matrices (fr); | |
4ef0d4d0 KS |
1522 | } |
1523 | ||
94ae2ad4 | 1524 | windows_or_buffers_changed = 19; |
4ef0d4d0 KS |
1525 | } |
1526 | ||
4d7e6e51 | 1527 | unblock_input (); |
4ef0d4d0 KS |
1528 | } |
1529 | } | |
1530 | ||
354884c4 | 1531 | void |
a2bc5bdd | 1532 | clear_image_caches (Lisp_Object filter) |
354884c4 SM |
1533 | { |
1534 | /* FIXME: We want to do | |
1535 | * struct terminal *t; | |
1536 | * for (t = terminal_list; t; t = t->next_terminal) | |
1537 | * clear_image_cache (t, filter); */ | |
1538 | Lisp_Object tail, frame; | |
1539 | FOR_EACH_FRAME (tail, frame) | |
1540 | if (FRAME_WINDOW_P (XFRAME (frame))) | |
a2bc5bdd | 1541 | clear_image_cache (XFRAME (frame), filter); |
354884c4 | 1542 | } |
4ef0d4d0 KS |
1543 | |
1544 | DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache, | |
1545 | 0, 1, 0, | |
a2bc5bdd SM |
1546 | doc: /* Clear the image cache. |
1547 | FILTER nil or a frame means clear all images in the selected frame. | |
1548 | FILTER t means clear the image caches of all frames. | |
1549 | Anything else, means only clear those images which refer to FILTER, | |
1550 | which is then usually a filename. */) | |
5842a27b | 1551 | (Lisp_Object filter) |
4ef0d4d0 | 1552 | { |
a2bc5bdd SM |
1553 | if (!(EQ (filter, Qnil) || FRAMEP (filter))) |
1554 | clear_image_caches (filter); | |
4ef0d4d0 | 1555 | else |
7452b7bd | 1556 | clear_image_cache (decode_window_system_frame (filter), Qt); |
4ef0d4d0 KS |
1557 | |
1558 | return Qnil; | |
1559 | } | |
1560 | ||
1561 | ||
110683ad | 1562 | DEFUN ("image-flush", Fimage_flush, Simage_flush, |
483e7ae0 | 1563 | 1, 2, 0, |
8bfcc21a | 1564 | doc: /* Flush the image with specification SPEC on frame FRAME. |
110683ad CY |
1565 | This removes the image from the Emacs image cache. If SPEC specifies |
1566 | an image file, the next redisplay of this image will read from the | |
1567 | current contents of that file. | |
1568 | ||
483e7ae0 CY |
1569 | FRAME nil or omitted means use the selected frame. |
1570 | FRAME t means refresh the image on all frames. */) | |
5842a27b | 1571 | (Lisp_Object spec, Lisp_Object frame) |
483e7ae0 CY |
1572 | { |
1573 | if (!valid_image_p (spec)) | |
1574 | error ("Invalid image specification"); | |
1575 | ||
1576 | if (EQ (frame, Qt)) | |
1577 | { | |
1578 | Lisp_Object tail; | |
1579 | FOR_EACH_FRAME (tail, frame) | |
1580 | { | |
1581 | struct frame *f = XFRAME (frame); | |
1582 | if (FRAME_WINDOW_P (f)) | |
1583 | uncache_image (f, spec); | |
1584 | } | |
1585 | } | |
1586 | else | |
7452b7bd | 1587 | uncache_image (decode_window_system_frame (frame), spec); |
483e7ae0 CY |
1588 | |
1589 | return Qnil; | |
1590 | } | |
1591 | ||
1592 | ||
4ef0d4d0 KS |
1593 | /* Compute masks and transform image IMG on frame F, as specified |
1594 | by the image's specification, */ | |
1595 | ||
1596 | static void | |
d3da34e0 | 1597 | postprocess_image (struct frame *f, struct image *img) |
4ef0d4d0 KS |
1598 | { |
1599 | /* Manipulation of the image's mask. */ | |
1600 | if (img->pixmap) | |
1601 | { | |
1602 | Lisp_Object conversion, spec; | |
1603 | Lisp_Object mask; | |
1604 | ||
1605 | spec = img->spec; | |
1606 | ||
1607 | /* `:heuristic-mask t' | |
1608 | `:mask heuristic' | |
1609 | means build a mask heuristically. | |
1610 | `:heuristic-mask (R G B)' | |
1611 | `:mask (heuristic (R G B))' | |
1612 | means build a mask from color (R G B) in the | |
1613 | image. | |
1614 | `:mask nil' | |
1615 | means remove a mask, if any. */ | |
1616 | ||
1617 | mask = image_spec_value (spec, QCheuristic_mask, NULL); | |
1618 | if (!NILP (mask)) | |
1619 | x_build_heuristic_mask (f, img, mask); | |
1620 | else | |
1621 | { | |
578098f3 | 1622 | bool found_p; |
4ef0d4d0 KS |
1623 | |
1624 | mask = image_spec_value (spec, QCmask, &found_p); | |
1625 | ||
1626 | if (EQ (mask, Qheuristic)) | |
1627 | x_build_heuristic_mask (f, img, Qt); | |
1628 | else if (CONSP (mask) | |
1629 | && EQ (XCAR (mask), Qheuristic)) | |
1630 | { | |
1631 | if (CONSP (XCDR (mask))) | |
1632 | x_build_heuristic_mask (f, img, XCAR (XCDR (mask))); | |
1633 | else | |
1634 | x_build_heuristic_mask (f, img, XCDR (mask)); | |
1635 | } | |
1636 | else if (NILP (mask) && found_p && img->mask) | |
547c9269 | 1637 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
4ef0d4d0 KS |
1638 | } |
1639 | ||
1640 | ||
1641 | /* Should we apply an image transformation algorithm? */ | |
1642 | conversion = image_spec_value (spec, QCconversion, NULL); | |
1643 | if (EQ (conversion, Qdisabled)) | |
1644 | x_disable_image (f, img); | |
1645 | else if (EQ (conversion, Qlaplace)) | |
1646 | x_laplace (f, img); | |
1647 | else if (EQ (conversion, Qemboss)) | |
1648 | x_emboss (f, img); | |
1649 | else if (CONSP (conversion) | |
1650 | && EQ (XCAR (conversion), Qedge_detection)) | |
1651 | { | |
1652 | Lisp_Object tem; | |
1653 | tem = XCDR (conversion); | |
1654 | if (CONSP (tem)) | |
1655 | x_edge_detection (f, img, | |
1656 | Fplist_get (tem, QCmatrix), | |
1657 | Fplist_get (tem, QCcolor_adjustment)); | |
1658 | } | |
1659 | } | |
1660 | } | |
1661 | ||
1662 | ||
1663 | /* Return the id of image with Lisp specification SPEC on frame F. | |
1664 | SPEC must be a valid Lisp image specification (see valid_image_p). */ | |
1665 | ||
13464394 | 1666 | ptrdiff_t |
d3da34e0 | 1667 | lookup_image (struct frame *f, Lisp_Object spec) |
4ef0d4d0 | 1668 | { |
4ef0d4d0 | 1669 | struct image *img; |
0de4bb68 | 1670 | EMACS_UINT hash; |
4ef0d4d0 KS |
1671 | |
1672 | /* F must be a window-system frame, and SPEC must be a valid image | |
1673 | specification. */ | |
a54e2c05 DA |
1674 | eassert (FRAME_WINDOW_P (f)); |
1675 | eassert (valid_image_p (spec)); | |
4ef0d4d0 | 1676 | |
4ef0d4d0 KS |
1677 | /* Look up SPEC in the hash table of the image cache. */ |
1678 | hash = sxhash (spec, 0); | |
d3c35d17 | 1679 | img = search_image_cache (f, spec, hash); |
f1f25b99 CY |
1680 | if (img && img->load_failed_p) |
1681 | { | |
1682 | free_image (f, img); | |
1683 | img = NULL; | |
1684 | } | |
1685 | ||
4ef0d4d0 KS |
1686 | /* If not found, create a new image and cache it. */ |
1687 | if (img == NULL) | |
1688 | { | |
4d7e6e51 | 1689 | block_input (); |
4ef0d4d0 KS |
1690 | img = make_image (spec, hash); |
1691 | cache_image (f, img); | |
578098f3 | 1692 | img->load_failed_p = ! img->type->load (f, img); |
c5b8e0ea YM |
1693 | img->frame_foreground = FRAME_FOREGROUND_PIXEL (f); |
1694 | img->frame_background = FRAME_BACKGROUND_PIXEL (f); | |
4ef0d4d0 KS |
1695 | |
1696 | /* If we can't load the image, and we don't have a width and | |
1697 | height, use some arbitrary width and height so that we can | |
1698 | draw a rectangle for it. */ | |
1699 | if (img->load_failed_p) | |
1700 | { | |
1701 | Lisp_Object value; | |
1702 | ||
1703 | value = image_spec_value (spec, QCwidth, NULL); | |
1704 | img->width = (INTEGERP (value) | |
1705 | ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH); | |
1706 | value = image_spec_value (spec, QCheight, NULL); | |
1707 | img->height = (INTEGERP (value) | |
1708 | ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT); | |
1709 | } | |
1710 | else | |
1711 | { | |
1712 | /* Handle image type independent image attributes | |
1713 | `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF', | |
1714 | `:background COLOR'. */ | |
1715 | Lisp_Object ascent, margin, relief, bg; | |
d311d28c | 1716 | int relief_bound; |
4ef0d4d0 KS |
1717 | |
1718 | ascent = image_spec_value (spec, QCascent, NULL); | |
1719 | if (INTEGERP (ascent)) | |
1720 | img->ascent = XFASTINT (ascent); | |
1721 | else if (EQ (ascent, Qcenter)) | |
1722 | img->ascent = CENTERED_IMAGE_ASCENT; | |
1723 | ||
1724 | margin = image_spec_value (spec, QCmargin, NULL); | |
13464394 | 1725 | if (INTEGERP (margin)) |
4ef0d4d0 | 1726 | img->vmargin = img->hmargin = XFASTINT (margin); |
13464394 | 1727 | else if (CONSP (margin)) |
4ef0d4d0 | 1728 | { |
13464394 PE |
1729 | img->hmargin = XFASTINT (XCAR (margin)); |
1730 | img->vmargin = XFASTINT (XCDR (margin)); | |
4ef0d4d0 KS |
1731 | } |
1732 | ||
1733 | relief = image_spec_value (spec, QCrelief, NULL); | |
d311d28c PE |
1734 | relief_bound = INT_MAX - max (img->hmargin, img->vmargin); |
1735 | if (RANGED_INTEGERP (- relief_bound, relief, relief_bound)) | |
4ef0d4d0 KS |
1736 | { |
1737 | img->relief = XINT (relief); | |
1ea40aa2 EZ |
1738 | img->hmargin += eabs (img->relief); |
1739 | img->vmargin += eabs (img->relief); | |
4ef0d4d0 KS |
1740 | } |
1741 | ||
1742 | if (! img->background_valid) | |
1743 | { | |
1744 | bg = image_spec_value (img->spec, QCbackground, NULL); | |
1745 | if (!NILP (bg)) | |
1746 | { | |
1747 | img->background | |
1748 | = x_alloc_image_color (f, img, bg, | |
1749 | FRAME_BACKGROUND_PIXEL (f)); | |
1750 | img->background_valid = 1; | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | /* Do image transformations and compute masks, unless we | |
1755 | don't have the image yet. */ | |
1756 | if (!EQ (*img->type->type, Qpostscript)) | |
1757 | postprocess_image (f, img); | |
1758 | } | |
1759 | ||
4d7e6e51 | 1760 | unblock_input (); |
4ef0d4d0 KS |
1761 | } |
1762 | ||
1763 | /* We're using IMG, so set its timestamp to `now'. */ | |
43aac990 | 1764 | img->timestamp = current_timespec (); |
4ef0d4d0 | 1765 | |
4ef0d4d0 KS |
1766 | /* Value is the image id. */ |
1767 | return img->id; | |
1768 | } | |
1769 | ||
1770 | ||
1771 | /* Cache image IMG in the image cache of frame F. */ | |
1772 | ||
1773 | static void | |
d3da34e0 | 1774 | cache_image (struct frame *f, struct image *img) |
4ef0d4d0 | 1775 | { |
354884c4 | 1776 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
13464394 | 1777 | ptrdiff_t i; |
4ef0d4d0 KS |
1778 | |
1779 | /* Find a free slot in c->images. */ | |
1780 | for (i = 0; i < c->used; ++i) | |
1781 | if (c->images[i] == NULL) | |
1782 | break; | |
1783 | ||
1784 | /* If no free slot found, maybe enlarge c->images. */ | |
1785 | if (i == c->used && c->used == c->size) | |
a69fbedb | 1786 | c->images = xpalloc (c->images, &c->size, 1, -1, sizeof *c->images); |
4ef0d4d0 KS |
1787 | |
1788 | /* Add IMG to c->images, and assign IMG an id. */ | |
1789 | c->images[i] = img; | |
1790 | img->id = i; | |
1791 | if (i == c->used) | |
1792 | ++c->used; | |
1793 | ||
1794 | /* Add IMG to the cache's hash table. */ | |
1795 | i = img->hash % IMAGE_CACHE_BUCKETS_SIZE; | |
1796 | img->next = c->buckets[i]; | |
1797 | if (img->next) | |
1798 | img->next->prev = img; | |
1799 | img->prev = NULL; | |
1800 | c->buckets[i] = img; | |
1801 | } | |
1802 | ||
1803 | ||
1804 | /* Call FN on every image in the image cache of frame F. Used to mark | |
1805 | Lisp Objects in the image cache. */ | |
1806 | ||
354884c4 SM |
1807 | /* Mark Lisp objects in image IMG. */ |
1808 | ||
1809 | static void | |
d3da34e0 | 1810 | mark_image (struct image *img) |
354884c4 SM |
1811 | { |
1812 | mark_object (img->spec); | |
a2bc5bdd | 1813 | mark_object (img->dependencies); |
354884c4 | 1814 | |
b50691aa CY |
1815 | if (!NILP (img->lisp_data)) |
1816 | mark_object (img->lisp_data); | |
354884c4 SM |
1817 | } |
1818 | ||
1819 | ||
4ef0d4d0 | 1820 | void |
354884c4 | 1821 | mark_image_cache (struct image_cache *c) |
4ef0d4d0 | 1822 | { |
354884c4 | 1823 | if (c) |
4ef0d4d0 | 1824 | { |
13464394 | 1825 | ptrdiff_t i; |
354884c4 SM |
1826 | for (i = 0; i < c->used; ++i) |
1827 | if (c->images[i]) | |
1828 | mark_image (c->images[i]); | |
4ef0d4d0 KS |
1829 | } |
1830 | } | |
1831 | ||
1832 | ||
1833 | \f | |
1834 | /*********************************************************************** | |
9e2a2647 | 1835 | X / NS / W32 support code |
4ef0d4d0 KS |
1836 | ***********************************************************************/ |
1837 | ||
0fda9b75 | 1838 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
1839 | |
1840 | /* Macro for defining functions that will be loaded from image DLLs. */ | |
dbdb9a7c | 1841 | #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args |
4ef0d4d0 KS |
1842 | |
1843 | /* Macro for loading those image functions from the library. */ | |
1844 | #define LOAD_IMGLIB_FN(lib,func) { \ | |
1845 | fn_##func = (void *) GetProcAddress (lib, #func); \ | |
1846 | if (!fn_##func) return 0; \ | |
1847 | } | |
1848 | ||
0fda9b75 | 1849 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 1850 | |
578098f3 | 1851 | /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the |
476371c4 | 1852 | windowing system. |
ca4aa935 PE |
1853 | WIDTH and HEIGHT must both be positive. |
1854 | If XIMG is null, assume it is a bitmap. */ | |
578098f3 | 1855 | static bool |
ca4aa935 PE |
1856 | x_check_image_size (XImagePtr ximg, int width, int height) |
1857 | { | |
476371c4 | 1858 | #ifdef HAVE_X_WINDOWS |
ca4aa935 PE |
1859 | /* Respect Xlib's limits: it cannot deal with images that have more |
1860 | than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits | |
476371c4 | 1861 | of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */ |
ca4aa935 PE |
1862 | enum |
1863 | { | |
1864 | XLIB_BYTES_MAX = min (INT_MAX, UINT_MAX), | |
1865 | X_IMAGE_BYTES_MAX = min (XLIB_BYTES_MAX, min (PTRDIFF_MAX, SIZE_MAX)) | |
1866 | }; | |
1867 | ||
1868 | int bitmap_pad, depth, bytes_per_line; | |
1869 | if (ximg) | |
1870 | { | |
1871 | bitmap_pad = ximg->bitmap_pad; | |
1872 | depth = ximg->depth; | |
1873 | bytes_per_line = ximg->bytes_per_line; | |
1874 | } | |
1875 | else | |
1876 | { | |
1877 | bitmap_pad = 8; | |
1878 | depth = 1; | |
1879 | bytes_per_line = (width >> 3) + ((width & 7) != 0); | |
1880 | } | |
1881 | return (width <= (INT_MAX - (bitmap_pad - 1)) / depth | |
1882 | && height <= X_IMAGE_BYTES_MAX / bytes_per_line); | |
476371c4 PE |
1883 | #else |
1884 | /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases. | |
1885 | For now, assume that every image size is allowed on these systems. */ | |
1886 | return 1; | |
1887 | #endif | |
ca4aa935 | 1888 | } |
4ef0d4d0 KS |
1889 | |
1890 | /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on | |
1891 | frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created. | |
1892 | Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated | |
1893 | via xmalloc. Print error messages via image_error if an error | |
578098f3 | 1894 | occurs. Value is true if successful. |
4ef0d4d0 KS |
1895 | |
1896 | On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH | |
1897 | should indicate the bit depth of the image. */ | |
1898 | ||
578098f3 | 1899 | static bool |
d3da34e0 JB |
1900 | x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth, |
1901 | XImagePtr *ximg, Pixmap *pixmap) | |
4ef0d4d0 KS |
1902 | { |
1903 | #ifdef HAVE_X_WINDOWS | |
1904 | Display *display = FRAME_X_DISPLAY (f); | |
1905 | Window window = FRAME_X_WINDOW (f); | |
1906 | Screen *screen = FRAME_X_SCREEN (f); | |
1907 | ||
4d7e6e51 | 1908 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
1909 | |
1910 | if (depth <= 0) | |
1911 | depth = DefaultDepthOfScreen (screen); | |
1912 | *ximg = XCreateImage (display, DefaultVisualOfScreen (screen), | |
1913 | depth, ZPixmap, 0, NULL, width, height, | |
1914 | depth > 16 ? 32 : depth > 8 ? 16 : 8, 0); | |
1915 | if (*ximg == NULL) | |
1916 | { | |
1917 | image_error ("Unable to allocate X image", Qnil, Qnil); | |
1918 | return 0; | |
1919 | } | |
1920 | ||
ca4aa935 PE |
1921 | if (! x_check_image_size (*ximg, width, height)) |
1922 | { | |
1923 | x_destroy_x_image (*ximg); | |
1924 | *ximg = NULL; | |
1925 | image_error ("Image too large (%dx%d)", | |
1926 | make_number (width), make_number (height)); | |
1927 | return 0; | |
1928 | } | |
1929 | ||
4ef0d4d0 | 1930 | /* Allocate image raster. */ |
23f86fce | 1931 | (*ximg)->data = xmalloc ((*ximg)->bytes_per_line * height); |
4ef0d4d0 KS |
1932 | |
1933 | /* Allocate a pixmap of the same size. */ | |
1934 | *pixmap = XCreatePixmap (display, window, width, height, depth); | |
1935 | if (*pixmap == NO_PIXMAP) | |
1936 | { | |
1937 | x_destroy_x_image (*ximg); | |
1938 | *ximg = NULL; | |
1939 | image_error ("Unable to create X pixmap", Qnil, Qnil); | |
1940 | return 0; | |
1941 | } | |
1942 | ||
1943 | return 1; | |
1944 | #endif /* HAVE_X_WINDOWS */ | |
1945 | ||
1946 | #ifdef HAVE_NTGUI | |
1947 | ||
1948 | BITMAPINFOHEADER *header; | |
1949 | HDC hdc; | |
1950 | int scanline_width_bits; | |
1951 | int remainder; | |
1952 | int palette_colors = 0; | |
1953 | ||
1954 | if (depth == 0) | |
1955 | depth = 24; | |
1956 | ||
1957 | if (depth != 1 && depth != 4 && depth != 8 | |
1958 | && depth != 16 && depth != 24 && depth != 32) | |
1959 | { | |
1960 | image_error ("Invalid image bit depth specified", Qnil, Qnil); | |
1961 | return 0; | |
1962 | } | |
1963 | ||
1964 | scanline_width_bits = width * depth; | |
1965 | remainder = scanline_width_bits % 32; | |
1966 | ||
1967 | if (remainder) | |
1968 | scanline_width_bits += 32 - remainder; | |
1969 | ||
1970 | /* Bitmaps with a depth less than 16 need a palette. */ | |
1971 | /* BITMAPINFO structure already contains the first RGBQUAD. */ | |
1972 | if (depth < 16) | |
657d08d3 | 1973 | palette_colors = 1 << (depth - 1); |
4ef0d4d0 KS |
1974 | |
1975 | *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD)); | |
4ef0d4d0 | 1976 | |
72af86bd AS |
1977 | header = &(*ximg)->info.bmiHeader; |
1978 | memset (&(*ximg)->info, 0, sizeof (BITMAPINFO)); | |
4ef0d4d0 KS |
1979 | header->biSize = sizeof (*header); |
1980 | header->biWidth = width; | |
1981 | header->biHeight = -height; /* negative indicates a top-down bitmap. */ | |
1982 | header->biPlanes = 1; | |
1983 | header->biBitCount = depth; | |
1984 | header->biCompression = BI_RGB; | |
1985 | header->biClrUsed = palette_colors; | |
1986 | ||
1987 | /* TODO: fill in palette. */ | |
1988 | if (depth == 1) | |
1989 | { | |
1990 | (*ximg)->info.bmiColors[0].rgbBlue = 0; | |
1991 | (*ximg)->info.bmiColors[0].rgbGreen = 0; | |
1992 | (*ximg)->info.bmiColors[0].rgbRed = 0; | |
1993 | (*ximg)->info.bmiColors[0].rgbReserved = 0; | |
1994 | (*ximg)->info.bmiColors[1].rgbBlue = 255; | |
1995 | (*ximg)->info.bmiColors[1].rgbGreen = 255; | |
1996 | (*ximg)->info.bmiColors[1].rgbRed = 255; | |
1997 | (*ximg)->info.bmiColors[1].rgbReserved = 0; | |
1998 | } | |
1999 | ||
2000 | hdc = get_frame_dc (f); | |
2001 | ||
2002 | /* Create a DIBSection and raster array for the bitmap, | |
2003 | and store its handle in *pixmap. */ | |
2004 | *pixmap = CreateDIBSection (hdc, &((*ximg)->info), | |
2005 | (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS, | |
2e09fef1 EZ |
2006 | /* casting avoids a GCC warning */ |
2007 | (void **)&((*ximg)->data), NULL, 0); | |
4ef0d4d0 KS |
2008 | |
2009 | /* Realize display palette and garbage all frames. */ | |
2010 | release_frame_dc (f, hdc); | |
2011 | ||
2012 | if (*pixmap == NULL) | |
2013 | { | |
5fc9fdeb | 2014 | DWORD err = GetLastError (); |
4ef0d4d0 KS |
2015 | Lisp_Object errcode; |
2016 | /* All system errors are < 10000, so the following is safe. */ | |
13464394 | 2017 | XSETINT (errcode, err); |
4ef0d4d0 KS |
2018 | image_error ("Unable to create bitmap, error code %d", errcode, Qnil); |
2019 | x_destroy_x_image (*ximg); | |
2020 | return 0; | |
2021 | } | |
2022 | ||
2023 | return 1; | |
2024 | ||
2025 | #endif /* HAVE_NTGUI */ | |
2026 | ||
edfda783 | 2027 | #ifdef HAVE_NS |
ed3751c8 | 2028 | *pixmap = ns_image_for_XPM (width, height, depth); |
edfda783 AR |
2029 | if (*pixmap == 0) |
2030 | { | |
2031 | *ximg = NULL; | |
2032 | image_error ("Unable to allocate NSImage for XPM pixmap", Qnil, Qnil); | |
2033 | return 0; | |
2034 | } | |
2035 | *ximg = *pixmap; | |
2036 | return 1; | |
2037 | #endif | |
4ef0d4d0 KS |
2038 | } |
2039 | ||
2040 | ||
2041 | /* Destroy XImage XIMG. Free XIMG->data. */ | |
2042 | ||
2043 | static void | |
d3da34e0 | 2044 | x_destroy_x_image (XImagePtr ximg) |
4ef0d4d0 | 2045 | { |
4d7e6e51 | 2046 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
2047 | if (ximg) |
2048 | { | |
2049 | #ifdef HAVE_X_WINDOWS | |
2050 | xfree (ximg->data); | |
2051 | ximg->data = NULL; | |
2052 | XDestroyImage (ximg); | |
2053 | #endif /* HAVE_X_WINDOWS */ | |
2054 | #ifdef HAVE_NTGUI | |
2055 | /* Data will be freed by DestroyObject. */ | |
2056 | ximg->data = NULL; | |
2057 | xfree (ximg); | |
2058 | #endif /* HAVE_NTGUI */ | |
edfda783 | 2059 | #ifdef HAVE_NS |
ed3751c8 | 2060 | ns_release_object (ximg); |
edfda783 | 2061 | #endif /* HAVE_NS */ |
4ef0d4d0 KS |
2062 | } |
2063 | } | |
2064 | ||
2065 | ||
2066 | /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT | |
2067 | are width and height of both the image and pixmap. */ | |
2068 | ||
2069 | static void | |
d3da34e0 | 2070 | x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int height) |
4ef0d4d0 KS |
2071 | { |
2072 | #ifdef HAVE_X_WINDOWS | |
2073 | GC gc; | |
2074 | ||
4d7e6e51 | 2075 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
2076 | gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL); |
2077 | XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height); | |
2078 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
2079 | #endif /* HAVE_X_WINDOWS */ | |
2080 | ||
2081 | #ifdef HAVE_NTGUI | |
2082 | #if 0 /* I don't think this is necessary looking at where it is used. */ | |
2083 | HDC hdc = get_frame_dc (f); | |
2084 | SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS); | |
2085 | release_frame_dc (f, hdc); | |
2086 | #endif | |
2087 | #endif /* HAVE_NTGUI */ | |
2088 | ||
edfda783 | 2089 | #ifdef HAVE_NS |
a54e2c05 | 2090 | eassert (ximg == pixmap); |
ed3751c8 | 2091 | ns_retain_object (ximg); |
edfda783 | 2092 | #endif |
4ef0d4d0 KS |
2093 | } |
2094 | ||
547c9269 YM |
2095 | /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches |
2096 | with image_put_x_image. */ | |
2097 | ||
2098 | static bool | |
2099 | image_create_x_image_and_pixmap (struct frame *f, struct image *img, | |
2100 | int width, int height, int depth, | |
2101 | XImagePtr *ximg, bool mask_p) | |
2102 | { | |
2103 | eassert ((!mask_p ? img->pixmap : img->mask) == NO_PIXMAP); | |
2104 | ||
2105 | return x_create_x_image_and_pixmap (f, width, height, depth, ximg, | |
2106 | !mask_p ? &img->pixmap : &img->mask); | |
2107 | } | |
2108 | ||
2109 | /* Put X image XIMG into image IMG on frame F, as a mask if and only | |
2110 | if MASK_P. On X, this simply records XIMG on a member of IMG, so | |
2111 | it can be put into the pixmap afterwards via image_sync_to_pixmaps. | |
2112 | On the other platforms, it puts XIMG into the pixmap, then frees | |
2113 | the X image and its buffer. */ | |
2114 | ||
2115 | static void | |
2116 | image_put_x_image (struct frame *f, struct image *img, XImagePtr ximg, | |
2117 | bool mask_p) | |
2118 | { | |
2119 | #ifdef HAVE_X_WINDOWS | |
2120 | if (!mask_p) | |
2121 | { | |
2122 | eassert (img->ximg == NULL); | |
2123 | img->ximg = ximg; | |
2124 | } | |
2125 | else | |
2126 | { | |
2127 | eassert (img->mask_img == NULL); | |
2128 | img->mask_img = ximg; | |
2129 | } | |
2130 | #else | |
2131 | x_put_x_image (f, ximg, !mask_p ? img->pixmap : img->mask, | |
2132 | img->width, img->height); | |
2133 | x_destroy_x_image (ximg); | |
2134 | #endif | |
2135 | } | |
2136 | ||
2137 | #ifdef HAVE_X_WINDOWS | |
2138 | /* Put the X images recorded in IMG on frame F into pixmaps, then free | |
2139 | the X images and their buffers. */ | |
2140 | ||
2141 | static void | |
2142 | image_sync_to_pixmaps (struct frame *f, struct image *img) | |
2143 | { | |
2144 | if (img->ximg) | |
2145 | { | |
2146 | x_put_x_image (f, img->ximg, img->pixmap, img->width, img->height); | |
2147 | x_destroy_x_image (img->ximg); | |
2148 | img->ximg = NULL; | |
2149 | } | |
2150 | if (img->mask_img) | |
2151 | { | |
2152 | x_put_x_image (f, img->mask_img, img->mask, img->width, img->height); | |
2153 | x_destroy_x_image (img->mask_img); | |
2154 | img->mask_img = NULL; | |
2155 | } | |
2156 | } | |
2157 | #endif | |
2158 | ||
2159 | #ifdef HAVE_NTGUI | |
2160 | /* Create a memory device context for IMG on frame F. It stores the | |
2161 | currently selected GDI object into *PREV for future restoration by | |
2162 | image_unget_x_image_or_dc. */ | |
2163 | ||
2164 | static XImagePtr_or_DC | |
2165 | image_get_x_image_or_dc (struct frame *f, struct image *img, bool mask_p, | |
2166 | HGDIOBJ *prev) | |
2167 | { | |
2168 | HDC frame_dc = get_frame_dc (f); | |
2169 | XImagePtr_or_DC ximg = CreateCompatibleDC (frame_dc); | |
2170 | ||
2171 | release_frame_dc (f, frame_dc); | |
2172 | *prev = SelectObject (ximg, !mask_p ? img->pixmap : img->mask); | |
b8075603 YM |
2173 | |
2174 | return ximg; | |
547c9269 YM |
2175 | } |
2176 | ||
b8075603 YM |
2177 | static void |
2178 | image_unget_x_image_or_dc (struct image *img, bool mask_p, | |
2179 | XImagePtr_or_DC ximg, HGDIOBJ prev) | |
547c9269 YM |
2180 | { |
2181 | SelectObject (ximg, prev); | |
2182 | DeleteDC (ximg); | |
2183 | } | |
2184 | #else /* !HAVE_NTGUI */ | |
2185 | /* Get the X image for IMG on frame F. The resulting X image data | |
2186 | should be treated as read-only at least on X. */ | |
2187 | ||
2188 | static XImagePtr | |
2189 | image_get_x_image (struct frame *f, struct image *img, bool mask_p) | |
2190 | { | |
2191 | #ifdef HAVE_X_WINDOWS | |
2192 | XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img; | |
2193 | ||
2194 | if (ximg_in_img) | |
2195 | return ximg_in_img; | |
2196 | else | |
2197 | return XGetImage (FRAME_X_DISPLAY (f), !mask_p ? img->pixmap : img->mask, | |
2198 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
2199 | #elif defined (HAVE_NS) | |
2200 | XImagePtr pixmap = !mask_p ? img->pixmap : img->mask; | |
2201 | ||
2202 | ns_retain_object (pixmap); | |
2203 | return pixmap; | |
2204 | #endif | |
2205 | } | |
2206 | ||
df03dc8a YM |
2207 | static void |
2208 | image_unget_x_image (struct image *img, bool mask_p, XImagePtr ximg) | |
547c9269 YM |
2209 | { |
2210 | #ifdef HAVE_X_WINDOWS | |
2211 | XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img; | |
2212 | ||
2213 | if (ximg_in_img) | |
2214 | eassert (ximg == ximg_in_img); | |
2215 | else | |
2216 | XDestroyImage (ximg); | |
2217 | #elif defined (HAVE_NS) | |
2218 | ns_release_object (ximg); | |
2219 | #endif | |
2220 | } | |
2221 | #endif /* !HAVE_NTGUI */ | |
2222 | ||
4ef0d4d0 KS |
2223 | \f |
2224 | /*********************************************************************** | |
2225 | File Handling | |
2226 | ***********************************************************************/ | |
2227 | ||
6678dc11 | 2228 | /* Find image file FILE. Look in data-directory/images, then |
022af124 KH |
2229 | x-bitmap-file-path. Value is the encoded full name of the file |
2230 | found, or nil if not found. */ | |
4ef0d4d0 | 2231 | |
c640aa9b | 2232 | Lisp_Object |
d3da34e0 | 2233 | x_find_image_file (Lisp_Object file) |
4ef0d4d0 KS |
2234 | { |
2235 | Lisp_Object file_found, search_path; | |
4ef0d4d0 KS |
2236 | int fd; |
2237 | ||
6678dc11 GM |
2238 | /* TODO I think this should use something like image-load-path |
2239 | instead. Unfortunately, that can contain non-string elements. */ | |
2240 | search_path = Fcons (Fexpand_file_name (build_string ("images"), | |
2241 | Vdata_directory), | |
2242 | Vx_bitmap_file_path); | |
4ef0d4d0 | 2243 | |
050b82f6 | 2244 | /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */ |
4ef0d4d0 KS |
2245 | fd = openp (search_path, file, Qnil, &file_found, Qnil); |
2246 | ||
2247 | if (fd == -1) | |
2248 | file_found = Qnil; | |
2249 | else | |
022af124 KH |
2250 | { |
2251 | file_found = ENCODE_FILE (file_found); | |
f6774c1a AS |
2252 | if (fd != -2) |
2253 | emacs_close (fd); | |
022af124 | 2254 | } |
4ef0d4d0 | 2255 | |
4ef0d4d0 KS |
2256 | return file_found; |
2257 | } | |
2258 | ||
2259 | ||
2260 | /* Read FILE into memory. Value is a pointer to a buffer allocated | |
2261 | with xmalloc holding FILE's contents. Value is null if an error | |
2262 | occurred. *SIZE is set to the size of the file. */ | |
2263 | ||
2264 | static unsigned char * | |
dd52fcea | 2265 | slurp_file (char *file, ptrdiff_t *size) |
4ef0d4d0 | 2266 | { |
406af475 | 2267 | FILE *fp = emacs_fopen (file, "rb"); |
4ef0d4d0 KS |
2268 | unsigned char *buf = NULL; |
2269 | struct stat st; | |
2270 | ||
3f5bef16 | 2271 | if (fp) |
4ef0d4d0 | 2272 | { |
3f5bef16 PE |
2273 | ptrdiff_t count = SPECPDL_INDEX (); |
2274 | record_unwind_protect_ptr (fclose_unwind, fp); | |
2275 | ||
2276 | if (fstat (fileno (fp), &st) == 0 | |
2277 | && 0 <= st.st_size && st.st_size < min (PTRDIFF_MAX, SIZE_MAX)) | |
4ef0d4d0 | 2278 | { |
3f5bef16 PE |
2279 | /* Report an error if we read past the purported EOF. |
2280 | This can happen if the file grows as we read it. */ | |
2281 | ptrdiff_t buflen = st.st_size; | |
2282 | buf = xmalloc (buflen + 1); | |
2283 | if (fread (buf, 1, buflen + 1, fp) == buflen) | |
2284 | *size = buflen; | |
2285 | else | |
2286 | { | |
2287 | xfree (buf); | |
2288 | buf = NULL; | |
2289 | } | |
4ef0d4d0 | 2290 | } |
3f5bef16 PE |
2291 | |
2292 | unbind_to (count, Qnil); | |
4ef0d4d0 KS |
2293 | } |
2294 | ||
2295 | return buf; | |
2296 | } | |
2297 | ||
2298 | ||
2299 | \f | |
4ef0d4d0 KS |
2300 | /*********************************************************************** |
2301 | XBM images | |
2302 | ***********************************************************************/ | |
2303 | ||
578098f3 PE |
2304 | static bool xbm_load (struct frame *f, struct image *img); |
2305 | static bool xbm_image_p (Lisp_Object object); | |
2306 | static bool xbm_file_p (Lisp_Object); | |
4ef0d4d0 KS |
2307 | |
2308 | ||
2309 | /* Indices of image specification fields in xbm_format, below. */ | |
2310 | ||
2311 | enum xbm_keyword_index | |
2312 | { | |
2313 | XBM_TYPE, | |
2314 | XBM_FILE, | |
2315 | XBM_WIDTH, | |
2316 | XBM_HEIGHT, | |
2317 | XBM_DATA, | |
2318 | XBM_FOREGROUND, | |
2319 | XBM_BACKGROUND, | |
2320 | XBM_ASCENT, | |
2321 | XBM_MARGIN, | |
2322 | XBM_RELIEF, | |
2323 | XBM_ALGORITHM, | |
2324 | XBM_HEURISTIC_MASK, | |
2325 | XBM_MASK, | |
2326 | XBM_LAST | |
2327 | }; | |
2328 | ||
2329 | /* Vector of image_keyword structures describing the format | |
2330 | of valid XBM image specifications. */ | |
2331 | ||
91433552 | 2332 | static const struct image_keyword xbm_format[XBM_LAST] = |
4ef0d4d0 KS |
2333 | { |
2334 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
2335 | {":file", IMAGE_STRING_VALUE, 0}, | |
2336 | {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2337 | {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2338 | {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2339 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2340 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2341 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 2342 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
2343 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
2344 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2345 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2346 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0} | |
2347 | }; | |
2348 | ||
2349 | /* Structure describing the image type XBM. */ | |
2350 | ||
2351 | static struct image_type xbm_type = | |
2352 | { | |
2353 | &Qxbm, | |
2354 | xbm_image_p, | |
2355 | xbm_load, | |
2356 | x_clear_image, | |
bb4d86b4 | 2357 | NULL, |
4ef0d4d0 KS |
2358 | NULL |
2359 | }; | |
2360 | ||
2361 | /* Tokens returned from xbm_scan. */ | |
2362 | ||
2363 | enum xbm_token | |
2364 | { | |
2365 | XBM_TK_IDENT = 256, | |
2366 | XBM_TK_NUMBER | |
2367 | }; | |
2368 | ||
2369 | ||
578098f3 | 2370 | /* Return true if OBJECT is a valid XBM-type image specification. |
4ef0d4d0 KS |
2371 | A valid specification is a list starting with the symbol `image' |
2372 | The rest of the list is a property list which must contain an | |
578098f3 | 2373 | entry `:type xbm'. |
4ef0d4d0 KS |
2374 | |
2375 | If the specification specifies a file to load, it must contain | |
2376 | an entry `:file FILENAME' where FILENAME is a string. | |
2377 | ||
2378 | If the specification is for a bitmap loaded from memory it must | |
2379 | contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where | |
2380 | WIDTH and HEIGHT are integers > 0. DATA may be: | |
2381 | ||
2382 | 1. a string large enough to hold the bitmap data, i.e. it must | |
2383 | have a size >= (WIDTH + 7) / 8 * HEIGHT | |
2384 | ||
2385 | 2. a bool-vector of size >= WIDTH * HEIGHT | |
2386 | ||
2387 | 3. a vector of strings or bool-vectors, one for each line of the | |
2388 | bitmap. | |
2389 | ||
bbe6f2aa | 2390 | 4. a string containing an in-memory XBM file. WIDTH and HEIGHT |
4ef0d4d0 KS |
2391 | may not be specified in this case because they are defined in the |
2392 | XBM file. | |
2393 | ||
2394 | Both the file and data forms may contain the additional entries | |
2395 | `:background COLOR' and `:foreground COLOR'. If not present, | |
2396 | foreground and background of the frame on which the image is | |
2397 | displayed is used. */ | |
2398 | ||
578098f3 | 2399 | static bool |
d3da34e0 | 2400 | xbm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
2401 | { |
2402 | struct image_keyword kw[XBM_LAST]; | |
2403 | ||
72af86bd | 2404 | memcpy (kw, xbm_format, sizeof kw); |
4ef0d4d0 KS |
2405 | if (!parse_image_spec (object, kw, XBM_LAST, Qxbm)) |
2406 | return 0; | |
2407 | ||
a54e2c05 | 2408 | eassert (EQ (kw[XBM_TYPE].value, Qxbm)); |
4ef0d4d0 KS |
2409 | |
2410 | if (kw[XBM_FILE].count) | |
2411 | { | |
2412 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count) | |
2413 | return 0; | |
2414 | } | |
2415 | else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value)) | |
2416 | { | |
2417 | /* In-memory XBM file. */ | |
2418 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count) | |
2419 | return 0; | |
2420 | } | |
2421 | else | |
2422 | { | |
2423 | Lisp_Object data; | |
13464394 | 2424 | int width, height; |
4ef0d4d0 KS |
2425 | |
2426 | /* Entries for `:width', `:height' and `:data' must be present. */ | |
2427 | if (!kw[XBM_WIDTH].count | |
2428 | || !kw[XBM_HEIGHT].count | |
2429 | || !kw[XBM_DATA].count) | |
2430 | return 0; | |
2431 | ||
2432 | data = kw[XBM_DATA].value; | |
2433 | width = XFASTINT (kw[XBM_WIDTH].value); | |
2434 | height = XFASTINT (kw[XBM_HEIGHT].value); | |
2435 | ||
2436 | /* Check type of data, and width and height against contents of | |
2437 | data. */ | |
2438 | if (VECTORP (data)) | |
2439 | { | |
45aebb64 | 2440 | EMACS_INT i; |
4ef0d4d0 KS |
2441 | |
2442 | /* Number of elements of the vector must be >= height. */ | |
77b37c05 | 2443 | if (ASIZE (data) < height) |
4ef0d4d0 KS |
2444 | return 0; |
2445 | ||
2446 | /* Each string or bool-vector in data must be large enough | |
2447 | for one line of the image. */ | |
2448 | for (i = 0; i < height; ++i) | |
2449 | { | |
28be1ada | 2450 | Lisp_Object elt = AREF (data, i); |
4ef0d4d0 KS |
2451 | |
2452 | if (STRINGP (elt)) | |
2453 | { | |
2454 | if (SCHARS (elt) | |
2455 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR) | |
2456 | return 0; | |
2457 | } | |
2458 | else if (BOOL_VECTOR_P (elt)) | |
2459 | { | |
1c0a7493 | 2460 | if (bool_vector_size (elt) < width) |
4ef0d4d0 KS |
2461 | return 0; |
2462 | } | |
2463 | else | |
2464 | return 0; | |
2465 | } | |
2466 | } | |
2467 | else if (STRINGP (data)) | |
2468 | { | |
2469 | if (SCHARS (data) | |
2470 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height) | |
2471 | return 0; | |
2472 | } | |
2473 | else if (BOOL_VECTOR_P (data)) | |
2474 | { | |
1c0a7493 | 2475 | if (bool_vector_size (data) / height < width) |
4ef0d4d0 KS |
2476 | return 0; |
2477 | } | |
2478 | else | |
2479 | return 0; | |
2480 | } | |
2481 | ||
2482 | return 1; | |
2483 | } | |
2484 | ||
2485 | ||
2486 | /* Scan a bitmap file. FP is the stream to read from. Value is | |
2487 | either an enumerator from enum xbm_token, or a character for a | |
2488 | single-character token, or 0 at end of file. If scanning an | |
2489 | identifier, store the lexeme of the identifier in SVAL. If | |
2490 | scanning a number, store its value in *IVAL. */ | |
2491 | ||
2492 | static int | |
d3da34e0 | 2493 | xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival) |
4ef0d4d0 KS |
2494 | { |
2495 | unsigned int c; | |
2496 | ||
2497 | loop: | |
2498 | ||
2499 | /* Skip white space. */ | |
620f13b0 | 2500 | while (*s < end && (c = *(*s)++, c_isspace (c))) |
4ef0d4d0 KS |
2501 | ; |
2502 | ||
2503 | if (*s >= end) | |
2504 | c = 0; | |
620f13b0 | 2505 | else if (c_isdigit (c)) |
4ef0d4d0 KS |
2506 | { |
2507 | int value = 0, digit; | |
2508 | ||
2509 | if (c == '0' && *s < end) | |
2510 | { | |
2511 | c = *(*s)++; | |
2512 | if (c == 'x' || c == 'X') | |
2513 | { | |
2514 | while (*s < end) | |
2515 | { | |
2516 | c = *(*s)++; | |
620f13b0 | 2517 | if (c_isdigit (c)) |
4ef0d4d0 KS |
2518 | digit = c - '0'; |
2519 | else if (c >= 'a' && c <= 'f') | |
2520 | digit = c - 'a' + 10; | |
2521 | else if (c >= 'A' && c <= 'F') | |
2522 | digit = c - 'A' + 10; | |
2523 | else | |
2524 | break; | |
2525 | value = 16 * value + digit; | |
2526 | } | |
2527 | } | |
620f13b0 | 2528 | else if (c_isdigit (c)) |
4ef0d4d0 KS |
2529 | { |
2530 | value = c - '0'; | |
2531 | while (*s < end | |
620f13b0 | 2532 | && (c = *(*s)++, c_isdigit (c))) |
4ef0d4d0 KS |
2533 | value = 8 * value + c - '0'; |
2534 | } | |
2535 | } | |
2536 | else | |
2537 | { | |
2538 | value = c - '0'; | |
2539 | while (*s < end | |
620f13b0 | 2540 | && (c = *(*s)++, c_isdigit (c))) |
4ef0d4d0 KS |
2541 | value = 10 * value + c - '0'; |
2542 | } | |
2543 | ||
2544 | if (*s < end) | |
2545 | *s = *s - 1; | |
2546 | *ival = value; | |
2547 | c = XBM_TK_NUMBER; | |
2548 | } | |
620f13b0 | 2549 | else if (c_isalpha (c) || c == '_') |
4ef0d4d0 KS |
2550 | { |
2551 | *sval++ = c; | |
2552 | while (*s < end | |
620f13b0 | 2553 | && (c = *(*s)++, (c_isalnum (c) || c == '_'))) |
4ef0d4d0 KS |
2554 | *sval++ = c; |
2555 | *sval = 0; | |
2556 | if (*s < end) | |
2557 | *s = *s - 1; | |
2558 | c = XBM_TK_IDENT; | |
2559 | } | |
2560 | else if (c == '/' && **s == '*') | |
2561 | { | |
2562 | /* C-style comment. */ | |
2563 | ++*s; | |
2564 | while (**s && (**s != '*' || *(*s + 1) != '/')) | |
2565 | ++*s; | |
2566 | if (**s) | |
2567 | { | |
2568 | *s += 2; | |
2569 | goto loop; | |
2570 | } | |
2571 | } | |
2572 | ||
2573 | return c; | |
2574 | } | |
2575 | ||
2576 | #ifdef HAVE_NTGUI | |
2577 | ||
2578 | /* Create a Windows bitmap from X bitmap data. */ | |
2579 | static HBITMAP | |
2580 | w32_create_pixmap_from_bitmap_data (int width, int height, char *data) | |
2581 | { | |
2582 | static unsigned char swap_nibble[16] | |
2583 | = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */ | |
2584 | 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */ | |
2585 | 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */ | |
2586 | 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */ | |
2587 | int i, j, w1, w2; | |
2588 | unsigned char *bits, *p; | |
2589 | HBITMAP bmp; | |
2590 | ||
2591 | w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */ | |
2592 | w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */ | |
38182d90 | 2593 | bits = alloca (height * w2); |
72af86bd | 2594 | memset (bits, 0, height * w2); |
4ef0d4d0 KS |
2595 | for (i = 0; i < height; i++) |
2596 | { | |
2597 | p = bits + i*w2; | |
2598 | for (j = 0; j < w1; j++) | |
2599 | { | |
2600 | /* Bitswap XBM bytes to match how Windows does things. */ | |
2601 | unsigned char c = *data++; | |
2602 | *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4) | |
2603 | | (swap_nibble[(c>>4) & 0xf])); | |
2604 | } | |
2605 | } | |
2606 | bmp = CreateBitmap (width, height, 1, 1, (char *) bits); | |
2607 | ||
2608 | return bmp; | |
2609 | } | |
2610 | ||
3046a84b | 2611 | static void |
7c3320d8 JB |
2612 | convert_mono_to_color_image (struct frame *f, struct image *img, |
2613 | COLORREF foreground, COLORREF background) | |
4ef0d4d0 KS |
2614 | { |
2615 | HDC hdc, old_img_dc, new_img_dc; | |
2616 | HGDIOBJ old_prev, new_prev; | |
2617 | HBITMAP new_pixmap; | |
2618 | ||
2619 | hdc = get_frame_dc (f); | |
2620 | old_img_dc = CreateCompatibleDC (hdc); | |
2621 | new_img_dc = CreateCompatibleDC (hdc); | |
2622 | new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height); | |
2623 | release_frame_dc (f, hdc); | |
2624 | old_prev = SelectObject (old_img_dc, img->pixmap); | |
2625 | new_prev = SelectObject (new_img_dc, new_pixmap); | |
44b1dc2e JR |
2626 | /* Windows convention for mono bitmaps is black = background, |
2627 | white = foreground. */ | |
2386b1f1 JR |
2628 | SetTextColor (new_img_dc, background); |
2629 | SetBkColor (new_img_dc, foreground); | |
4ef0d4d0 KS |
2630 | |
2631 | BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc, | |
2632 | 0, 0, SRCCOPY); | |
2633 | ||
2634 | SelectObject (old_img_dc, old_prev); | |
2635 | SelectObject (new_img_dc, new_prev); | |
2636 | DeleteDC (old_img_dc); | |
2637 | DeleteDC (new_img_dc); | |
2638 | DeleteObject (img->pixmap); | |
2639 | if (new_pixmap == 0) | |
2640 | fprintf (stderr, "Failed to convert image to color.\n"); | |
2641 | else | |
2642 | img->pixmap = new_pixmap; | |
2643 | } | |
2644 | ||
2645 | #define XBM_BIT_SHUFFLE(b) (~(b)) | |
2646 | ||
2647 | #else | |
2648 | ||
2649 | #define XBM_BIT_SHUFFLE(b) (b) | |
2650 | ||
2651 | #endif /* HAVE_NTGUI */ | |
2652 | ||
2653 | ||
2654 | static void | |
d3da34e0 JB |
2655 | Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data, |
2656 | RGB_PIXEL_COLOR fg, RGB_PIXEL_COLOR bg, | |
578098f3 | 2657 | bool non_default_colors) |
4ef0d4d0 KS |
2658 | { |
2659 | #ifdef HAVE_NTGUI | |
2660 | img->pixmap | |
2661 | = w32_create_pixmap_from_bitmap_data (img->width, img->height, data); | |
2662 | ||
2663 | /* If colors were specified, transfer the bitmap to a color one. */ | |
2664 | if (non_default_colors) | |
2665 | convert_mono_to_color_image (f, img, fg, bg); | |
edfda783 AR |
2666 | |
2667 | #elif defined (HAVE_NS) | |
ed3751c8 | 2668 | img->pixmap = ns_image_from_XBM (data, img->width, img->height); |
edfda783 | 2669 | |
4ef0d4d0 | 2670 | #else |
ca4aa935 PE |
2671 | img->pixmap = |
2672 | (x_check_image_size (0, img->width, img->height) | |
2673 | ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f), | |
4ef0d4d0 KS |
2674 | FRAME_X_WINDOW (f), |
2675 | data, | |
2676 | img->width, img->height, | |
2677 | fg, bg, | |
ca4aa935 PE |
2678 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))) |
2679 | : NO_PIXMAP); | |
edfda783 | 2680 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
2681 | } |
2682 | ||
2683 | ||
2684 | ||
2685 | /* Replacement for XReadBitmapFileData which isn't available under old | |
2686 | X versions. CONTENTS is a pointer to a buffer to parse; END is the | |
2687 | buffer's end. Set *WIDTH and *HEIGHT to the width and height of | |
2688 | the image. Return in *DATA the bitmap data allocated with xmalloc. | |
578098f3 PE |
2689 | Value is true if successful. DATA null means just test if |
2690 | CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR, | |
2691 | inhibit the call to image_error when the image size is invalid (the | |
2692 | bitmap remains unread). */ | |
4ef0d4d0 | 2693 | |
578098f3 | 2694 | static bool |
d3da34e0 | 2695 | xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *end, |
b8dc29e9 | 2696 | int *width, int *height, char **data, |
578098f3 | 2697 | bool inhibit_image_error) |
4ef0d4d0 KS |
2698 | { |
2699 | unsigned char *s = contents; | |
2700 | char buffer[BUFSIZ]; | |
578098f3 PE |
2701 | bool padding_p = 0; |
2702 | bool v10 = 0; | |
ca4aa935 | 2703 | int bytes_per_line, i, nbytes; |
b8dc29e9 | 2704 | char *p; |
4ef0d4d0 KS |
2705 | int value; |
2706 | int LA1; | |
2707 | ||
2708 | #define match() \ | |
2709 | LA1 = xbm_scan (&s, end, buffer, &value) | |
2710 | ||
2711 | #define expect(TOKEN) \ | |
f462f075 PE |
2712 | do \ |
2713 | { \ | |
2714 | if (LA1 != (TOKEN)) \ | |
2715 | goto failure; \ | |
2716 | match (); \ | |
2717 | } \ | |
2718 | while (0) | |
4ef0d4d0 KS |
2719 | |
2720 | #define expect_ident(IDENT) \ | |
2721 | if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \ | |
2722 | match (); \ | |
2723 | else \ | |
2724 | goto failure | |
2725 | ||
2726 | *width = *height = -1; | |
2727 | if (data) | |
2728 | *data = NULL; | |
2729 | LA1 = xbm_scan (&s, end, buffer, &value); | |
2730 | ||
2731 | /* Parse defines for width, height and hot-spots. */ | |
2732 | while (LA1 == '#') | |
2733 | { | |
2734 | match (); | |
2735 | expect_ident ("define"); | |
2736 | expect (XBM_TK_IDENT); | |
2737 | ||
431feaf6 | 2738 | if (LA1 == XBM_TK_NUMBER) |
4ef0d4d0 | 2739 | { |
6ae141d6 PE |
2740 | char *q = strrchr (buffer, '_'); |
2741 | q = q ? q + 1 : buffer; | |
2742 | if (strcmp (q, "width") == 0) | |
4ef0d4d0 | 2743 | *width = value; |
6ae141d6 | 2744 | else if (strcmp (q, "height") == 0) |
4ef0d4d0 KS |
2745 | *height = value; |
2746 | } | |
2747 | expect (XBM_TK_NUMBER); | |
2748 | } | |
2749 | ||
f1f25b99 | 2750 | if (!check_image_size (f, *width, *height)) |
10ea2b82 | 2751 | { |
ce959360 CY |
2752 | if (!inhibit_image_error) |
2753 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
10ea2b82 JR |
2754 | goto failure; |
2755 | } | |
4ef0d4d0 KS |
2756 | else if (data == NULL) |
2757 | goto success; | |
2758 | ||
2759 | /* Parse bits. Must start with `static'. */ | |
2760 | expect_ident ("static"); | |
2761 | if (LA1 == XBM_TK_IDENT) | |
2762 | { | |
2763 | if (strcmp (buffer, "unsigned") == 0) | |
2764 | { | |
2765 | match (); | |
2766 | expect_ident ("char"); | |
2767 | } | |
2768 | else if (strcmp (buffer, "short") == 0) | |
2769 | { | |
2770 | match (); | |
2771 | v10 = 1; | |
2772 | if (*width % 16 && *width % 16 < 9) | |
2773 | padding_p = 1; | |
2774 | } | |
2775 | else if (strcmp (buffer, "char") == 0) | |
2776 | match (); | |
2777 | else | |
2778 | goto failure; | |
2779 | } | |
2780 | else | |
2781 | goto failure; | |
2782 | ||
2783 | expect (XBM_TK_IDENT); | |
2784 | expect ('['); | |
2785 | expect (']'); | |
2786 | expect ('='); | |
2787 | expect ('{'); | |
2788 | ||
ca4aa935 PE |
2789 | if (! x_check_image_size (0, *width, *height)) |
2790 | { | |
2791 | if (!inhibit_image_error) | |
2792 | image_error ("Image too large (%dx%d)", | |
2793 | make_number (*width), make_number (*height)); | |
2794 | goto failure; | |
2795 | } | |
4ef0d4d0 KS |
2796 | bytes_per_line = (*width + 7) / 8 + padding_p; |
2797 | nbytes = bytes_per_line * *height; | |
23f86fce | 2798 | p = *data = xmalloc (nbytes); |
4ef0d4d0 KS |
2799 | |
2800 | if (v10) | |
2801 | { | |
2802 | for (i = 0; i < nbytes; i += 2) | |
2803 | { | |
2804 | int val = value; | |
2805 | expect (XBM_TK_NUMBER); | |
2806 | ||
2807 | *p++ = XBM_BIT_SHUFFLE (val); | |
2808 | if (!padding_p || ((i + 2) % bytes_per_line)) | |
2809 | *p++ = XBM_BIT_SHUFFLE (value >> 8); | |
2810 | ||
2811 | if (LA1 == ',' || LA1 == '}') | |
2812 | match (); | |
2813 | else | |
2814 | goto failure; | |
2815 | } | |
2816 | } | |
2817 | else | |
2818 | { | |
2819 | for (i = 0; i < nbytes; ++i) | |
2820 | { | |
2821 | int val = value; | |
2822 | expect (XBM_TK_NUMBER); | |
2823 | ||
2824 | *p++ = XBM_BIT_SHUFFLE (val); | |
2825 | ||
2826 | if (LA1 == ',' || LA1 == '}') | |
2827 | match (); | |
2828 | else | |
2829 | goto failure; | |
2830 | } | |
2831 | } | |
2832 | ||
2833 | success: | |
2834 | return 1; | |
2835 | ||
2836 | failure: | |
2837 | ||
2838 | if (data && *data) | |
2839 | { | |
2840 | xfree (*data); | |
2841 | *data = NULL; | |
2842 | } | |
2843 | return 0; | |
2844 | ||
2845 | #undef match | |
2846 | #undef expect | |
2847 | #undef expect_ident | |
2848 | } | |
2849 | ||
2850 | ||
2851 | /* Load XBM image IMG which will be displayed on frame F from buffer | |
578098f3 | 2852 | CONTENTS. END is the end of the buffer. Value is true if |
4ef0d4d0 KS |
2853 | successful. */ |
2854 | ||
578098f3 | 2855 | static bool |
d3da34e0 JB |
2856 | xbm_load_image (struct frame *f, struct image *img, unsigned char *contents, |
2857 | unsigned char *end) | |
4ef0d4d0 | 2858 | { |
578098f3 | 2859 | bool rc; |
b8dc29e9 | 2860 | char *data; |
578098f3 | 2861 | bool success_p = 0; |
4ef0d4d0 | 2862 | |
ce959360 CY |
2863 | rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height, |
2864 | &data, 0); | |
4ef0d4d0 KS |
2865 | if (rc) |
2866 | { | |
2867 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2868 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
578098f3 | 2869 | bool non_default_colors = 0; |
4ef0d4d0 KS |
2870 | Lisp_Object value; |
2871 | ||
a54e2c05 | 2872 | eassert (img->width > 0 && img->height > 0); |
4ef0d4d0 KS |
2873 | |
2874 | /* Get foreground and background colors, maybe allocate colors. */ | |
2875 | value = image_spec_value (img->spec, QCforeground, NULL); | |
2876 | if (!NILP (value)) | |
2877 | { | |
2878 | foreground = x_alloc_image_color (f, img, value, foreground); | |
2879 | non_default_colors = 1; | |
2880 | } | |
2881 | value = image_spec_value (img->spec, QCbackground, NULL); | |
2882 | if (!NILP (value)) | |
2883 | { | |
2884 | background = x_alloc_image_color (f, img, value, background); | |
2885 | img->background = background; | |
2886 | img->background_valid = 1; | |
2887 | non_default_colors = 1; | |
2888 | } | |
2889 | ||
1f026899 | 2890 | Create_Pixmap_From_Bitmap_Data (f, img, data, |
4ef0d4d0 KS |
2891 | foreground, background, |
2892 | non_default_colors); | |
2893 | xfree (data); | |
2894 | ||
2895 | if (img->pixmap == NO_PIXMAP) | |
2896 | { | |
2897 | x_clear_image (f, img); | |
2898 | image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil); | |
2899 | } | |
2900 | else | |
2901 | success_p = 1; | |
2902 | } | |
2903 | else | |
2904 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
2905 | ||
2906 | return success_p; | |
2907 | } | |
2908 | ||
2909 | ||
578098f3 | 2910 | /* Value is true if DATA looks like an in-memory XBM file. */ |
4ef0d4d0 | 2911 | |
578098f3 | 2912 | static bool |
d3da34e0 | 2913 | xbm_file_p (Lisp_Object data) |
4ef0d4d0 KS |
2914 | { |
2915 | int w, h; | |
2916 | return (STRINGP (data) | |
f1f25b99 | 2917 | && xbm_read_bitmap_data (NULL, SDATA (data), |
ce959360 CY |
2918 | (SDATA (data) + SBYTES (data)), |
2919 | &w, &h, NULL, 1)); | |
4ef0d4d0 KS |
2920 | } |
2921 | ||
2922 | ||
2923 | /* Fill image IMG which is used on frame F with pixmap data. Value is | |
578098f3 | 2924 | true if successful. */ |
4ef0d4d0 | 2925 | |
578098f3 | 2926 | static bool |
d3da34e0 | 2927 | xbm_load (struct frame *f, struct image *img) |
4ef0d4d0 | 2928 | { |
578098f3 | 2929 | bool success_p = 0; |
4ef0d4d0 KS |
2930 | Lisp_Object file_name; |
2931 | ||
a54e2c05 | 2932 | eassert (xbm_image_p (img->spec)); |
4ef0d4d0 KS |
2933 | |
2934 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
2935 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
2936 | if (STRINGP (file_name)) | |
2937 | { | |
2938 | Lisp_Object file; | |
2939 | unsigned char *contents; | |
dd52fcea | 2940 | ptrdiff_t size; |
4ef0d4d0 KS |
2941 | |
2942 | file = x_find_image_file (file_name); | |
4ef0d4d0 KS |
2943 | if (!STRINGP (file)) |
2944 | { | |
2945 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
4ef0d4d0 KS |
2946 | return 0; |
2947 | } | |
2948 | ||
42a5b22f | 2949 | contents = slurp_file (SSDATA (file), &size); |
4ef0d4d0 KS |
2950 | if (contents == NULL) |
2951 | { | |
2952 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
2953 | return 0; |
2954 | } | |
2955 | ||
2956 | success_p = xbm_load_image (f, img, contents, contents + size); | |
d5a19415 | 2957 | xfree (contents); |
4ef0d4d0 KS |
2958 | } |
2959 | else | |
2960 | { | |
2961 | struct image_keyword fmt[XBM_LAST]; | |
2962 | Lisp_Object data; | |
2963 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2964 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
578098f3 | 2965 | bool non_default_colors = 0; |
4ef0d4d0 | 2966 | char *bits; |
578098f3 PE |
2967 | bool parsed_p; |
2968 | bool in_memory_file_p = 0; | |
4ef0d4d0 KS |
2969 | |
2970 | /* See if data looks like an in-memory XBM file. */ | |
2971 | data = image_spec_value (img->spec, QCdata, NULL); | |
2972 | in_memory_file_p = xbm_file_p (data); | |
2973 | ||
2974 | /* Parse the image specification. */ | |
72af86bd | 2975 | memcpy (fmt, xbm_format, sizeof fmt); |
4ef0d4d0 | 2976 | parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm); |
a54e2c05 | 2977 | eassert (parsed_p); |
4ef0d4d0 KS |
2978 | |
2979 | /* Get specified width, and height. */ | |
2980 | if (!in_memory_file_p) | |
2981 | { | |
2982 | img->width = XFASTINT (fmt[XBM_WIDTH].value); | |
2983 | img->height = XFASTINT (fmt[XBM_HEIGHT].value); | |
a54e2c05 | 2984 | eassert (img->width > 0 && img->height > 0); |
ca4aa935 PE |
2985 | if (!check_image_size (f, img->width, img->height)) |
2986 | { | |
2987 | image_error ("Invalid image size (see `max-image-size')", | |
2988 | Qnil, Qnil); | |
2989 | return 0; | |
2990 | } | |
4ef0d4d0 KS |
2991 | } |
2992 | ||
2993 | /* Get foreground and background colors, maybe allocate colors. */ | |
2994 | if (fmt[XBM_FOREGROUND].count | |
2995 | && STRINGP (fmt[XBM_FOREGROUND].value)) | |
2996 | { | |
2997 | foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value, | |
2998 | foreground); | |
2999 | non_default_colors = 1; | |
3000 | } | |
3001 | ||
3002 | if (fmt[XBM_BACKGROUND].count | |
3003 | && STRINGP (fmt[XBM_BACKGROUND].value)) | |
3004 | { | |
3005 | background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value, | |
3006 | background); | |
3007 | non_default_colors = 1; | |
3008 | } | |
3009 | ||
3010 | if (in_memory_file_p) | |
3011 | success_p = xbm_load_image (f, img, SDATA (data), | |
3012 | (SDATA (data) | |
3013 | + SBYTES (data))); | |
3014 | else | |
3015 | { | |
3016 | if (VECTORP (data)) | |
3017 | { | |
3018 | int i; | |
3019 | char *p; | |
3020 | int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR; | |
3021 | ||
38182d90 | 3022 | p = bits = alloca (nbytes * img->height); |
4ef0d4d0 KS |
3023 | for (i = 0; i < img->height; ++i, p += nbytes) |
3024 | { | |
28be1ada | 3025 | Lisp_Object line = AREF (data, i); |
4ef0d4d0 | 3026 | if (STRINGP (line)) |
72af86bd | 3027 | memcpy (p, SDATA (line), nbytes); |
4ef0d4d0 | 3028 | else |
df5b4930 | 3029 | memcpy (p, bool_vector_data (line), nbytes); |
4ef0d4d0 KS |
3030 | } |
3031 | } | |
3032 | else if (STRINGP (data)) | |
42a5b22f | 3033 | bits = SSDATA (data); |
4ef0d4d0 | 3034 | else |
df5b4930 | 3035 | bits = (char *) bool_vector_data (data); |
4ef0d4d0 | 3036 | |
0fda9b75 | 3037 | #ifdef HAVE_NTGUI |
44b1dc2e JR |
3038 | { |
3039 | char *invertedBits; | |
3040 | int nbytes, i; | |
3041 | /* Windows mono bitmaps are reversed compared with X. */ | |
3042 | invertedBits = bits; | |
285d07e2 | 3043 | nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR |
44b1dc2e | 3044 | * img->height; |
38182d90 | 3045 | bits = alloca (nbytes); |
44b1dc2e JR |
3046 | for (i = 0; i < nbytes; i++) |
3047 | bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]); | |
3048 | } | |
3049 | #endif | |
4ef0d4d0 KS |
3050 | /* Create the pixmap. */ |
3051 | ||
ca4aa935 PE |
3052 | if (x_check_image_size (0, img->width, img->height)) |
3053 | Create_Pixmap_From_Bitmap_Data (f, img, bits, | |
3054 | foreground, background, | |
3055 | non_default_colors); | |
3056 | else | |
3057 | img->pixmap = NO_PIXMAP; | |
3058 | ||
4ef0d4d0 KS |
3059 | if (img->pixmap) |
3060 | success_p = 1; | |
3061 | else | |
3062 | { | |
3063 | image_error ("Unable to create pixmap for XBM image `%s'", | |
3064 | img->spec, Qnil); | |
3065 | x_clear_image (f, img); | |
3066 | } | |
3067 | } | |
3068 | } | |
3069 | ||
3070 | return success_p; | |
3071 | } | |
3072 | ||
3073 | ||
3074 | \f | |
3075 | /*********************************************************************** | |
3076 | XPM images | |
3077 | ***********************************************************************/ | |
3078 | ||
9e2a2647 | 3079 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 | 3080 | |
578098f3 PE |
3081 | static bool xpm_image_p (Lisp_Object object); |
3082 | static bool xpm_load (struct frame *f, struct image *img); | |
4ef0d4d0 | 3083 | |
9e2a2647 | 3084 | #endif /* HAVE_XPM || HAVE_NS */ |
ea1aaa6f ST |
3085 | |
3086 | #ifdef HAVE_XPM | |
4ef0d4d0 KS |
3087 | #ifdef HAVE_NTGUI |
3088 | /* Indicate to xpm.h that we don't have Xlib. */ | |
3089 | #define FOR_MSW | |
3090 | /* simx.h in xpm defines XColor and XImage differently than Emacs. */ | |
279d3293 | 3091 | /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */ |
4ef0d4d0 KS |
3092 | #define XColor xpm_XColor |
3093 | #define XImage xpm_XImage | |
279d3293 | 3094 | #define Display xpm_Display |
4ef0d4d0 KS |
3095 | #define PIXEL_ALREADY_TYPEDEFED |
3096 | #include "X11/xpm.h" | |
3097 | #undef FOR_MSW | |
3098 | #undef XColor | |
3099 | #undef XImage | |
279d3293 | 3100 | #undef Display |
4ef0d4d0 KS |
3101 | #undef PIXEL_ALREADY_TYPEDEFED |
3102 | #else | |
3103 | #include "X11/xpm.h" | |
3104 | #endif /* HAVE_NTGUI */ | |
ea1aaa6f | 3105 | #endif /* HAVE_XPM */ |
4ef0d4d0 | 3106 | |
9e2a2647 | 3107 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 KS |
3108 | /* The symbol `xpm' identifying XPM-format images. */ |
3109 | ||
955cbe7b | 3110 | static Lisp_Object Qxpm; |
4ef0d4d0 KS |
3111 | |
3112 | /* Indices of image specification fields in xpm_format, below. */ | |
3113 | ||
3114 | enum xpm_keyword_index | |
3115 | { | |
3116 | XPM_TYPE, | |
3117 | XPM_FILE, | |
3118 | XPM_DATA, | |
3119 | XPM_ASCENT, | |
3120 | XPM_MARGIN, | |
3121 | XPM_RELIEF, | |
3122 | XPM_ALGORITHM, | |
3123 | XPM_HEURISTIC_MASK, | |
3124 | XPM_MASK, | |
3125 | XPM_COLOR_SYMBOLS, | |
3126 | XPM_BACKGROUND, | |
3127 | XPM_LAST | |
3128 | }; | |
3129 | ||
3130 | /* Vector of image_keyword structures describing the format | |
3131 | of valid XPM image specifications. */ | |
3132 | ||
91433552 | 3133 | static const struct image_keyword xpm_format[XPM_LAST] = |
4ef0d4d0 KS |
3134 | { |
3135 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
3136 | {":file", IMAGE_STRING_VALUE, 0}, | |
3137 | {":data", IMAGE_STRING_VALUE, 0}, | |
3138 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 3139 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
3140 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
3141 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3142 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3143 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3144 | {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3145 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
3146 | }; | |
3147 | ||
a9e7a9d5 | 3148 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 3149 | static bool init_xpm_functions (void); |
bb4d86b4 CY |
3150 | #else |
3151 | #define init_xpm_functions NULL | |
3152 | #endif | |
3153 | ||
4ef0d4d0 KS |
3154 | /* Structure describing the image type XPM. */ |
3155 | ||
3156 | static struct image_type xpm_type = | |
3157 | { | |
3158 | &Qxpm, | |
3159 | xpm_image_p, | |
3160 | xpm_load, | |
3161 | x_clear_image, | |
bb4d86b4 | 3162 | init_xpm_functions, |
4ef0d4d0 KS |
3163 | NULL |
3164 | }; | |
3165 | ||
3166 | #ifdef HAVE_X_WINDOWS | |
3167 | ||
3168 | /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation | |
3169 | functions for allocating image colors. Our own functions handle | |
3170 | color allocation failures more gracefully than the ones on the XPM | |
3171 | lib. */ | |
3172 | ||
3173 | #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure | |
3174 | #define ALLOC_XPM_COLORS | |
3175 | #endif | |
3176 | #endif /* HAVE_X_WINDOWS */ | |
3177 | ||
3178 | #ifdef ALLOC_XPM_COLORS | |
3179 | ||
f57e2426 J |
3180 | static struct xpm_cached_color *xpm_cache_color (struct frame *, char *, |
3181 | XColor *, int); | |
4ef0d4d0 KS |
3182 | |
3183 | /* An entry in a hash table used to cache color definitions of named | |
3184 | colors. This cache is necessary to speed up XPM image loading in | |
3185 | case we do color allocations ourselves. Without it, we would need | |
3186 | a call to XParseColor per pixel in the image. */ | |
3187 | ||
3188 | struct xpm_cached_color | |
3189 | { | |
3190 | /* Next in collision chain. */ | |
3191 | struct xpm_cached_color *next; | |
3192 | ||
3193 | /* Color definition (RGB and pixel color). */ | |
3194 | XColor color; | |
3195 | ||
3196 | /* Color name. */ | |
fbe9e0b9 | 3197 | char name[FLEXIBLE_ARRAY_MEMBER]; |
4ef0d4d0 KS |
3198 | }; |
3199 | ||
3200 | /* The hash table used for the color cache, and its bucket vector | |
3201 | size. */ | |
3202 | ||
3203 | #define XPM_COLOR_CACHE_BUCKETS 1001 | |
cd44d2eb | 3204 | static struct xpm_cached_color **xpm_color_cache; |
4ef0d4d0 KS |
3205 | |
3206 | /* Initialize the color cache. */ | |
3207 | ||
3208 | static void | |
d3da34e0 | 3209 | xpm_init_color_cache (struct frame *f, XpmAttributes *attrs) |
4ef0d4d0 KS |
3210 | { |
3211 | size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache; | |
23f86fce | 3212 | xpm_color_cache = xzalloc (nbytes); |
4ef0d4d0 KS |
3213 | init_color_table (); |
3214 | ||
3215 | if (attrs->valuemask & XpmColorSymbols) | |
3216 | { | |
3217 | int i; | |
3218 | XColor color; | |
3219 | ||
3220 | for (i = 0; i < attrs->numsymbols; ++i) | |
3221 | if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3222 | attrs->colorsymbols[i].value, &color)) | |
3223 | { | |
3224 | color.pixel = lookup_rgb_color (f, color.red, color.green, | |
3225 | color.blue); | |
3226 | xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1); | |
3227 | } | |
3228 | } | |
3229 | } | |
3230 | ||
3231 | /* Free the color cache. */ | |
3232 | ||
3233 | static void | |
d3da34e0 | 3234 | xpm_free_color_cache (void) |
4ef0d4d0 KS |
3235 | { |
3236 | struct xpm_cached_color *p, *next; | |
3237 | int i; | |
3238 | ||
3239 | for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i) | |
3240 | for (p = xpm_color_cache[i]; p; p = next) | |
3241 | { | |
3242 | next = p->next; | |
3243 | xfree (p); | |
3244 | } | |
3245 | ||
3246 | xfree (xpm_color_cache); | |
3247 | xpm_color_cache = NULL; | |
3248 | free_color_table (); | |
3249 | } | |
3250 | ||
3251 | /* Return the bucket index for color named COLOR_NAME in the color | |
3252 | cache. */ | |
3253 | ||
3254 | static int | |
d3da34e0 | 3255 | xpm_color_bucket (char *color_name) |
4ef0d4d0 | 3256 | { |
3f791afe PE |
3257 | EMACS_UINT hash = hash_string (color_name, strlen (color_name)); |
3258 | return hash % XPM_COLOR_CACHE_BUCKETS; | |
4ef0d4d0 KS |
3259 | } |
3260 | ||
3261 | ||
3262 | /* On frame F, cache values COLOR for color with name COLOR_NAME. | |
3263 | BUCKET, if >= 0, is a precomputed bucket index. Value is the cache | |
3264 | entry added. */ | |
3265 | ||
3266 | static struct xpm_cached_color * | |
d3da34e0 | 3267 | xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket) |
4ef0d4d0 KS |
3268 | { |
3269 | size_t nbytes; | |
3270 | struct xpm_cached_color *p; | |
3271 | ||
3272 | if (bucket < 0) | |
3273 | bucket = xpm_color_bucket (color_name); | |
3274 | ||
3f791afe | 3275 | nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1; |
23f86fce | 3276 | p = xmalloc (nbytes); |
4ef0d4d0 KS |
3277 | strcpy (p->name, color_name); |
3278 | p->color = *color; | |
3279 | p->next = xpm_color_cache[bucket]; | |
3280 | xpm_color_cache[bucket] = p; | |
3281 | return p; | |
3282 | } | |
3283 | ||
3284 | /* Look up color COLOR_NAME for frame F in the color cache. If found, | |
3285 | return the cached definition in *COLOR. Otherwise, make a new | |
578098f3 | 3286 | entry in the cache and allocate the color. Value is false if color |
4ef0d4d0 KS |
3287 | allocation failed. */ |
3288 | ||
578098f3 | 3289 | static bool |
d3da34e0 | 3290 | xpm_lookup_color (struct frame *f, char *color_name, XColor *color) |
4ef0d4d0 KS |
3291 | { |
3292 | struct xpm_cached_color *p; | |
3293 | int h = xpm_color_bucket (color_name); | |
3294 | ||
3295 | for (p = xpm_color_cache[h]; p; p = p->next) | |
3296 | if (strcmp (p->name, color_name) == 0) | |
3297 | break; | |
3298 | ||
3299 | if (p != NULL) | |
3300 | *color = p->color; | |
3301 | else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3302 | color_name, color)) | |
3303 | { | |
3304 | color->pixel = lookup_rgb_color (f, color->red, color->green, | |
3305 | color->blue); | |
3306 | p = xpm_cache_color (f, color_name, color, h); | |
3307 | } | |
3308 | /* You get `opaque' at least from ImageMagick converting pbm to xpm | |
3309 | with transparency, and it's useful. */ | |
3310 | else if (strcmp ("opaque", color_name) == 0) | |
3311 | { | |
72af86bd | 3312 | memset (color, 0, sizeof (XColor)); /* Is this necessary/correct? */ |
4ef0d4d0 KS |
3313 | color->pixel = FRAME_FOREGROUND_PIXEL (f); |
3314 | p = xpm_cache_color (f, color_name, color, h); | |
3315 | } | |
3316 | ||
3317 | return p != NULL; | |
3318 | } | |
3319 | ||
3320 | ||
3321 | /* Callback for allocating color COLOR_NAME. Called from the XPM lib. | |
3322 | CLOSURE is a pointer to the frame on which we allocate the | |
3323 | color. Return in *COLOR the allocated color. Value is non-zero | |
3324 | if successful. */ | |
3325 | ||
3326 | static int | |
d3da34e0 JB |
3327 | xpm_alloc_color (Display *dpy, Colormap cmap, char *color_name, XColor *color, |
3328 | void *closure) | |
4ef0d4d0 | 3329 | { |
7d652d97 | 3330 | return xpm_lookup_color (closure, color_name, color); |
4ef0d4d0 KS |
3331 | } |
3332 | ||
3333 | ||
3334 | /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE | |
3335 | is a pointer to the frame on which we allocate the color. Value is | |
3336 | non-zero if successful. */ | |
3337 | ||
3338 | static int | |
d3da34e0 | 3339 | xpm_free_colors (Display *dpy, Colormap cmap, Pixel *pixels, int npixels, void *closure) |
4ef0d4d0 KS |
3340 | { |
3341 | return 1; | |
3342 | } | |
3343 | ||
3344 | #endif /* ALLOC_XPM_COLORS */ | |
3345 | ||
3346 | ||
0fda9b75 | 3347 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
3348 | |
3349 | /* XPM library details. */ | |
3350 | ||
14beddf4 CY |
3351 | DEF_IMGLIB_FN (void, XpmFreeAttributes, (XpmAttributes *)); |
3352 | DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer, (Display *, char *, xpm_XImage **, | |
850690cc | 3353 | xpm_XImage **, XpmAttributes *)); |
14beddf4 | 3354 | DEF_IMGLIB_FN (int, XpmReadFileToImage, (Display *, char *, xpm_XImage **, |
850690cc | 3355 | xpm_XImage **, XpmAttributes *)); |
14beddf4 | 3356 | DEF_IMGLIB_FN (void, XImageFree, (xpm_XImage *)); |
4ef0d4d0 | 3357 | |
578098f3 | 3358 | static bool |
d07ff9db | 3359 | init_xpm_functions (void) |
4ef0d4d0 KS |
3360 | { |
3361 | HMODULE library; | |
3362 | ||
d07ff9db | 3363 | if (!(library = w32_delayed_load (Qxpm))) |
4ef0d4d0 KS |
3364 | return 0; |
3365 | ||
3366 | LOAD_IMGLIB_FN (library, XpmFreeAttributes); | |
3367 | LOAD_IMGLIB_FN (library, XpmCreateImageFromBuffer); | |
3368 | LOAD_IMGLIB_FN (library, XpmReadFileToImage); | |
3369 | LOAD_IMGLIB_FN (library, XImageFree); | |
3370 | return 1; | |
3371 | } | |
3372 | ||
0fda9b75 | 3373 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 3374 | |
a9e7a9d5 | 3375 | #if defined HAVE_NTGUI && !defined WINDOWSNT |
0fda9b75 DC |
3376 | /* Glue for code below */ |
3377 | #define fn_XpmReadFileToImage XpmReadFileToImage | |
3378 | #define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer | |
3379 | #define fn_XImageFree XImageFree | |
3380 | #define fn_XpmFreeAttributes XpmFreeAttributes | |
2ae3a8a7 | 3381 | #endif /* HAVE_NTGUI && !WINDOWSNT */ |
4ef0d4d0 | 3382 | |
578098f3 | 3383 | /* Value is true if COLOR_SYMBOLS is a valid color symbols list |
4ef0d4d0 KS |
3384 | for XPM images. Such a list must consist of conses whose car and |
3385 | cdr are strings. */ | |
3386 | ||
578098f3 | 3387 | static bool |
d3da34e0 | 3388 | xpm_valid_color_symbols_p (Lisp_Object color_symbols) |
4ef0d4d0 KS |
3389 | { |
3390 | while (CONSP (color_symbols)) | |
3391 | { | |
3392 | Lisp_Object sym = XCAR (color_symbols); | |
3393 | if (!CONSP (sym) | |
3394 | || !STRINGP (XCAR (sym)) | |
3395 | || !STRINGP (XCDR (sym))) | |
3396 | break; | |
3397 | color_symbols = XCDR (color_symbols); | |
3398 | } | |
3399 | ||
3400 | return NILP (color_symbols); | |
3401 | } | |
3402 | ||
3403 | ||
578098f3 | 3404 | /* Value is true if OBJECT is a valid XPM image specification. */ |
4ef0d4d0 | 3405 | |
578098f3 | 3406 | static bool |
d3da34e0 | 3407 | xpm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
3408 | { |
3409 | struct image_keyword fmt[XPM_LAST]; | |
72af86bd | 3410 | memcpy (fmt, xpm_format, sizeof fmt); |
4ef0d4d0 KS |
3411 | return (parse_image_spec (object, fmt, XPM_LAST, Qxpm) |
3412 | /* Either `:file' or `:data' must be present. */ | |
3413 | && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1 | |
3414 | /* Either no `:color-symbols' or it's a list of conses | |
3415 | whose car and cdr are strings. */ | |
3416 | && (fmt[XPM_COLOR_SYMBOLS].count == 0 | |
3417 | || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))); | |
3418 | } | |
3419 | ||
9e2a2647 | 3420 | #endif /* HAVE_XPM || HAVE_NS */ |
4ef0d4d0 | 3421 | |
cd44d2eb | 3422 | #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK |
0766b489 | 3423 | ptrdiff_t |
ef1b0ba7 | 3424 | x_create_bitmap_from_xpm_data (struct frame *f, const char **bits) |
786a43d6 | 3425 | { |
aad3612f | 3426 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
0766b489 PE |
3427 | ptrdiff_t id; |
3428 | int rc; | |
786a43d6 CY |
3429 | XpmAttributes attrs; |
3430 | Pixmap bitmap, mask; | |
3431 | ||
72af86bd | 3432 | memset (&attrs, 0, sizeof attrs); |
786a43d6 | 3433 | |
54188d8f CY |
3434 | attrs.visual = FRAME_X_VISUAL (f); |
3435 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3436 | attrs.valuemask |= XpmVisual; | |
3437 | attrs.valuemask |= XpmColormap; | |
3438 | ||
786a43d6 | 3439 | rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
f77fabaf | 3440 | (char **) bits, &bitmap, &mask, &attrs); |
786a43d6 | 3441 | if (rc != XpmSuccess) |
fe45ad15 CY |
3442 | { |
3443 | XpmFreeAttributes (&attrs); | |
3444 | return -1; | |
3445 | } | |
786a43d6 CY |
3446 | |
3447 | id = x_allocate_bitmap_record (f); | |
786a43d6 CY |
3448 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; |
3449 | dpyinfo->bitmaps[id - 1].have_mask = 1; | |
3450 | dpyinfo->bitmaps[id - 1].mask = mask; | |
3451 | dpyinfo->bitmaps[id - 1].file = NULL; | |
3452 | dpyinfo->bitmaps[id - 1].height = attrs.height; | |
3453 | dpyinfo->bitmaps[id - 1].width = attrs.width; | |
3454 | dpyinfo->bitmaps[id - 1].depth = attrs.depth; | |
3455 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
3456 | ||
3457 | XpmFreeAttributes (&attrs); | |
3458 | return id; | |
3459 | } | |
0d876a14 | 3460 | #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */ |
786a43d6 | 3461 | |
4ef0d4d0 | 3462 | /* Load image IMG which will be displayed on frame F. Value is |
578098f3 | 3463 | true if successful. */ |
4ef0d4d0 | 3464 | |
ea1aaa6f ST |
3465 | #ifdef HAVE_XPM |
3466 | ||
578098f3 | 3467 | static bool |
d3da34e0 | 3468 | xpm_load (struct frame *f, struct image *img) |
4ef0d4d0 KS |
3469 | { |
3470 | int rc; | |
3471 | XpmAttributes attrs; | |
3472 | Lisp_Object specified_file, color_symbols; | |
3473 | #ifdef HAVE_NTGUI | |
3474 | HDC hdc; | |
3475 | xpm_XImage * xpm_image = NULL, * xpm_mask = NULL; | |
3476 | #endif /* HAVE_NTGUI */ | |
3477 | ||
3478 | /* Configure the XPM lib. Use the visual of frame F. Allocate | |
3479 | close colors. Return colors allocated. */ | |
72af86bd | 3480 | memset (&attrs, 0, sizeof attrs); |
4ef0d4d0 KS |
3481 | |
3482 | #ifndef HAVE_NTGUI | |
3483 | attrs.visual = FRAME_X_VISUAL (f); | |
3484 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3485 | attrs.valuemask |= XpmVisual; | |
3486 | attrs.valuemask |= XpmColormap; | |
3487 | #endif /* HAVE_NTGUI */ | |
3488 | ||
3489 | #ifdef ALLOC_XPM_COLORS | |
3490 | /* Allocate colors with our own functions which handle | |
3491 | failing color allocation more gracefully. */ | |
3492 | attrs.color_closure = f; | |
3493 | attrs.alloc_color = xpm_alloc_color; | |
3494 | attrs.free_colors = xpm_free_colors; | |
3495 | attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure; | |
3496 | #else /* not ALLOC_XPM_COLORS */ | |
3497 | /* Let the XPM lib allocate colors. */ | |
3498 | attrs.valuemask |= XpmReturnAllocPixels; | |
3499 | #ifdef XpmAllocCloseColors | |
3500 | attrs.alloc_close_colors = 1; | |
3501 | attrs.valuemask |= XpmAllocCloseColors; | |
3502 | #else /* not XpmAllocCloseColors */ | |
3503 | attrs.closeness = 600; | |
3504 | attrs.valuemask |= XpmCloseness; | |
3505 | #endif /* not XpmAllocCloseColors */ | |
3506 | #endif /* ALLOC_XPM_COLORS */ | |
3507 | ||
3508 | /* If image specification contains symbolic color definitions, add | |
3509 | these to `attrs'. */ | |
3510 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
3511 | if (CONSP (color_symbols)) | |
3512 | { | |
3513 | Lisp_Object tail; | |
3514 | XpmColorSymbol *xpm_syms; | |
3515 | int i, size; | |
3516 | ||
3517 | attrs.valuemask |= XpmColorSymbols; | |
3518 | ||
3519 | /* Count number of symbols. */ | |
3520 | attrs.numsymbols = 0; | |
3521 | for (tail = color_symbols; CONSP (tail); tail = XCDR (tail)) | |
3522 | ++attrs.numsymbols; | |
3523 | ||
3524 | /* Allocate an XpmColorSymbol array. */ | |
3525 | size = attrs.numsymbols * sizeof *xpm_syms; | |
38182d90 | 3526 | xpm_syms = alloca (size); |
72af86bd | 3527 | memset (xpm_syms, 0, size); |
4ef0d4d0 KS |
3528 | attrs.colorsymbols = xpm_syms; |
3529 | ||
3530 | /* Fill the color symbol array. */ | |
3531 | for (tail = color_symbols, i = 0; | |
3532 | CONSP (tail); | |
3533 | ++i, tail = XCDR (tail)) | |
3534 | { | |
7574650a AS |
3535 | Lisp_Object name; |
3536 | Lisp_Object color; | |
e22cffbc | 3537 | char *empty_string = (char *) ""; |
7574650a AS |
3538 | |
3539 | if (!CONSP (XCAR (tail))) | |
3540 | { | |
e22cffbc PE |
3541 | xpm_syms[i].name = empty_string; |
3542 | xpm_syms[i].value = empty_string; | |
7574650a AS |
3543 | continue; |
3544 | } | |
3545 | name = XCAR (XCAR (tail)); | |
3546 | color = XCDR (XCAR (tail)); | |
3547 | if (STRINGP (name)) | |
3548 | { | |
38182d90 | 3549 | xpm_syms[i].name = alloca (SCHARS (name) + 1); |
42a5b22f | 3550 | strcpy (xpm_syms[i].name, SSDATA (name)); |
7574650a AS |
3551 | } |
3552 | else | |
e22cffbc | 3553 | xpm_syms[i].name = empty_string; |
7574650a AS |
3554 | if (STRINGP (color)) |
3555 | { | |
38182d90 | 3556 | xpm_syms[i].value = alloca (SCHARS (color) + 1); |
42a5b22f | 3557 | strcpy (xpm_syms[i].value, SSDATA (color)); |
7574650a AS |
3558 | } |
3559 | else | |
e22cffbc | 3560 | xpm_syms[i].value = empty_string; |
4ef0d4d0 KS |
3561 | } |
3562 | } | |
3563 | ||
3564 | /* Create a pixmap for the image, either from a file, or from a | |
3565 | string buffer containing data in the same format as an XPM file. */ | |
3566 | #ifdef ALLOC_XPM_COLORS | |
3567 | xpm_init_color_cache (f, &attrs); | |
3568 | #endif | |
3569 | ||
3570 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
3571 | ||
3572 | #ifdef HAVE_NTGUI | |
3573 | { | |
3574 | HDC frame_dc = get_frame_dc (f); | |
3575 | hdc = CreateCompatibleDC (frame_dc); | |
3576 | release_frame_dc (f, frame_dc); | |
3577 | } | |
3578 | #endif /* HAVE_NTGUI */ | |
3579 | ||
3580 | if (STRINGP (specified_file)) | |
3581 | { | |
3582 | Lisp_Object file = x_find_image_file (specified_file); | |
3583 | if (!STRINGP (file)) | |
3584 | { | |
3585 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
7574650a AS |
3586 | #ifdef ALLOC_XPM_COLORS |
3587 | xpm_free_color_cache (); | |
3588 | #endif | |
4ef0d4d0 KS |
3589 | return 0; |
3590 | } | |
3591 | ||
3592 | #ifdef HAVE_NTGUI | |
3593 | /* XpmReadFileToPixmap is not available in the Windows port of | |
3594 | libxpm. But XpmReadFileToImage almost does what we want. */ | |
3595 | rc = fn_XpmReadFileToImage (&hdc, SDATA (file), | |
3596 | &xpm_image, &xpm_mask, | |
3597 | &attrs); | |
3598 | #else | |
547c9269 YM |
3599 | rc = XpmReadFileToImage (FRAME_X_DISPLAY (f), SSDATA (file), |
3600 | &img->ximg, &img->mask_img, | |
3601 | &attrs); | |
4ef0d4d0 KS |
3602 | #endif /* HAVE_NTGUI */ |
3603 | } | |
3604 | else | |
3605 | { | |
3606 | Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
3607 | if (!STRINGP (buffer)) |
3608 | { | |
3609 | image_error ("Invalid image data `%s'", buffer, Qnil); | |
3610 | #ifdef ALLOC_XPM_COLORS | |
3611 | xpm_free_color_cache (); | |
3612 | #endif | |
3613 | return 0; | |
3614 | } | |
4ef0d4d0 KS |
3615 | #ifdef HAVE_NTGUI |
3616 | /* XpmCreatePixmapFromBuffer is not available in the Windows port | |
3617 | of libxpm. But XpmCreateImageFromBuffer almost does what we want. */ | |
3618 | rc = fn_XpmCreateImageFromBuffer (&hdc, SDATA (buffer), | |
3619 | &xpm_image, &xpm_mask, | |
3620 | &attrs); | |
3621 | #else | |
547c9269 YM |
3622 | rc = XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f), SSDATA (buffer), |
3623 | &img->ximg, &img->mask_img, | |
3624 | &attrs); | |
4ef0d4d0 KS |
3625 | #endif /* HAVE_NTGUI */ |
3626 | } | |
3627 | ||
547c9269 YM |
3628 | #ifdef HAVE_X_WINDOWS |
3629 | if (rc == XpmSuccess) | |
3630 | { | |
3631 | img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3632 | img->ximg->width, img->ximg->height, | |
3633 | img->ximg->depth); | |
3634 | if (img->pixmap == NO_PIXMAP) | |
3635 | { | |
3636 | x_clear_image (f, img); | |
3637 | rc = XpmNoMemory; | |
3638 | } | |
3639 | else if (img->mask_img) | |
3640 | { | |
3641 | img->mask = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3642 | img->mask_img->width, | |
3643 | img->mask_img->height, | |
3644 | img->mask_img->depth); | |
3645 | if (img->mask == NO_PIXMAP) | |
3646 | { | |
3647 | x_clear_image (f, img); | |
3648 | rc = XpmNoMemory; | |
3649 | } | |
3650 | } | |
3651 | } | |
3652 | #endif | |
3653 | ||
4ef0d4d0 KS |
3654 | if (rc == XpmSuccess) |
3655 | { | |
3656 | #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS) | |
3657 | img->colors = colors_in_color_table (&img->ncolors); | |
3658 | #else /* not ALLOC_XPM_COLORS */ | |
3659 | int i; | |
3660 | ||
3661 | #ifdef HAVE_NTGUI | |
3662 | /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap, | |
3663 | plus some duplicate attributes. */ | |
3664 | if (xpm_image && xpm_image->bitmap) | |
3665 | { | |
3666 | img->pixmap = xpm_image->bitmap; | |
3667 | /* XImageFree in libXpm frees XImage struct without destroying | |
3668 | the bitmap, which is what we want. */ | |
3669 | fn_XImageFree (xpm_image); | |
3670 | } | |
3671 | if (xpm_mask && xpm_mask->bitmap) | |
3672 | { | |
3673 | /* The mask appears to be inverted compared with what we expect. | |
3674 | TODO: invert our expectations. See other places where we | |
3675 | have to invert bits because our idea of masks is backwards. */ | |
3676 | HGDIOBJ old_obj; | |
3677 | old_obj = SelectObject (hdc, xpm_mask->bitmap); | |
3678 | ||
3679 | PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT); | |
3680 | SelectObject (hdc, old_obj); | |
3681 | ||
3682 | img->mask = xpm_mask->bitmap; | |
3683 | fn_XImageFree (xpm_mask); | |
3684 | DeleteDC (hdc); | |
3685 | } | |
3686 | ||
3687 | DeleteDC (hdc); | |
3688 | #endif /* HAVE_NTGUI */ | |
3689 | ||
3690 | /* Remember allocated colors. */ | |
0065d054 | 3691 | img->colors = xnmalloc (attrs.nalloc_pixels, sizeof *img->colors); |
ddff3151 | 3692 | img->ncolors = attrs.nalloc_pixels; |
4ef0d4d0 KS |
3693 | for (i = 0; i < attrs.nalloc_pixels; ++i) |
3694 | { | |
3695 | img->colors[i] = attrs.alloc_pixels[i]; | |
3696 | #ifdef DEBUG_X_COLORS | |
3697 | register_color (img->colors[i]); | |
3698 | #endif | |
3699 | } | |
3700 | #endif /* not ALLOC_XPM_COLORS */ | |
3701 | ||
3702 | img->width = attrs.width; | |
3703 | img->height = attrs.height; | |
a54e2c05 | 3704 | eassert (img->width > 0 && img->height > 0); |
4ef0d4d0 KS |
3705 | |
3706 | /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */ | |
3707 | #ifdef HAVE_NTGUI | |
3708 | fn_XpmFreeAttributes (&attrs); | |
3709 | #else | |
3710 | XpmFreeAttributes (&attrs); | |
3711 | #endif /* HAVE_NTGUI */ | |
547c9269 YM |
3712 | |
3713 | #ifdef HAVE_X_WINDOWS | |
3714 | /* Maybe fill in the background field while we have ximg handy. */ | |
3715 | IMAGE_BACKGROUND (img, f, img->ximg); | |
3716 | if (img->mask_img) | |
3717 | /* Fill in the background_transparent field while we have the | |
3718 | mask handy. */ | |
3719 | image_background_transparent (img, f, img->mask_img); | |
3720 | #endif | |
4ef0d4d0 KS |
3721 | } |
3722 | else | |
3723 | { | |
3724 | #ifdef HAVE_NTGUI | |
3725 | DeleteDC (hdc); | |
3726 | #endif /* HAVE_NTGUI */ | |
3727 | ||
3728 | switch (rc) | |
3729 | { | |
3730 | case XpmOpenFailed: | |
3731 | image_error ("Error opening XPM file (%s)", img->spec, Qnil); | |
3732 | break; | |
3733 | ||
3734 | case XpmFileInvalid: | |
3735 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
3736 | break; | |
3737 | ||
3738 | case XpmNoMemory: | |
3739 | image_error ("Out of memory (%s)", img->spec, Qnil); | |
3740 | break; | |
3741 | ||
3742 | case XpmColorFailed: | |
3743 | image_error ("Color allocation error (%s)", img->spec, Qnil); | |
3744 | break; | |
3745 | ||
3746 | default: | |
3747 | image_error ("Unknown error (%s)", img->spec, Qnil); | |
3748 | break; | |
3749 | } | |
3750 | } | |
3751 | ||
3752 | #ifdef ALLOC_XPM_COLORS | |
3753 | xpm_free_color_cache (); | |
3754 | #endif | |
3755 | return rc == XpmSuccess; | |
3756 | } | |
3757 | ||
3758 | #endif /* HAVE_XPM */ | |
3759 | ||
9e2a2647 | 3760 | #if defined (HAVE_NS) && !defined (HAVE_XPM) |
ea1aaa6f | 3761 | |
9e2a2647 | 3762 | /* XPM support functions for NS where libxpm is not available. |
ea1aaa6f ST |
3763 | Only XPM version 3 (without any extensions) is supported. */ |
3764 | ||
f57e2426 J |
3765 | static void xpm_put_color_table_v (Lisp_Object, const unsigned char *, |
3766 | int, Lisp_Object); | |
3767 | static Lisp_Object xpm_get_color_table_v (Lisp_Object, | |
3768 | const unsigned char *, int); | |
f57e2426 J |
3769 | static void xpm_put_color_table_h (Lisp_Object, const unsigned char *, |
3770 | int, Lisp_Object); | |
3771 | static Lisp_Object xpm_get_color_table_h (Lisp_Object, | |
3772 | const unsigned char *, int); | |
ea1aaa6f ST |
3773 | |
3774 | /* Tokens returned from xpm_scan. */ | |
3775 | ||
3776 | enum xpm_token | |
3777 | { | |
3778 | XPM_TK_IDENT = 256, | |
3779 | XPM_TK_STRING, | |
3780 | XPM_TK_EOF | |
3781 | }; | |
3782 | ||
3783 | /* Scan an XPM data and return a character (< 256) or a token defined | |
3784 | by enum xpm_token above. *S and END are the start (inclusive) and | |
3785 | the end (exclusive) addresses of the data, respectively. Advance | |
3786 | *S while scanning. If token is either XPM_TK_IDENT or | |
3787 | XPM_TK_STRING, *BEG and *LEN are set to the start address and the | |
3788 | length of the corresponding token, respectively. */ | |
3789 | ||
3790 | static int | |
3d608a86 J |
3791 | xpm_scan (const unsigned char **s, |
3792 | const unsigned char *end, | |
3793 | const unsigned char **beg, | |
74ca2eb3 | 3794 | ptrdiff_t *len) |
ea1aaa6f ST |
3795 | { |
3796 | int c; | |
3797 | ||
3798 | while (*s < end) | |
3799 | { | |
3800 | /* Skip white-space. */ | |
620f13b0 | 3801 | while (*s < end && (c = *(*s)++, c_isspace (c))) |
9ecf6403 | 3802 | ; |
ea1aaa6f ST |
3803 | |
3804 | /* gnus-pointer.xpm uses '-' in its identifier. | |
9ecf6403 | 3805 | sb-dir-plus.xpm uses '+' in its identifier. */ |
620f13b0 | 3806 | if (c_isalpha (c) || c == '_' || c == '-' || c == '+') |
9ecf6403 YM |
3807 | { |
3808 | *beg = *s - 1; | |
2ff0845e | 3809 | while (*s < end |
620f13b0 PE |
3810 | && (c = **s, c_isalnum (c) |
3811 | || c == '_' || c == '-' || c == '+')) | |
9ecf6403 YM |
3812 | ++*s; |
3813 | *len = *s - *beg; | |
3814 | return XPM_TK_IDENT; | |
3815 | } | |
ea1aaa6f | 3816 | else if (c == '"') |
9ecf6403 YM |
3817 | { |
3818 | *beg = *s; | |
3819 | while (*s < end && **s != '"') | |
3820 | ++*s; | |
3821 | *len = *s - *beg; | |
3822 | if (*s < end) | |
3823 | ++*s; | |
3824 | return XPM_TK_STRING; | |
3825 | } | |
ea1aaa6f | 3826 | else if (c == '/') |
9ecf6403 YM |
3827 | { |
3828 | if (*s < end && **s == '*') | |
3829 | { | |
3830 | /* C-style comment. */ | |
3831 | ++*s; | |
3832 | do | |
3833 | { | |
3834 | while (*s < end && *(*s)++ != '*') | |
3835 | ; | |
3836 | } | |
3837 | while (*s < end && **s != '/'); | |
3838 | if (*s < end) | |
3839 | ++*s; | |
3840 | } | |
3841 | else | |
3842 | return c; | |
3843 | } | |
ea1aaa6f | 3844 | else |
9ecf6403 | 3845 | return c; |
ea1aaa6f ST |
3846 | } |
3847 | ||
3848 | return XPM_TK_EOF; | |
3849 | } | |
3850 | ||
bbe6f2aa | 3851 | /* Functions for color table lookup in XPM data. A key is a string |
ea1aaa6f ST |
3852 | specifying the color of each pixel in XPM data. A value is either |
3853 | an integer that specifies a pixel color, Qt that specifies | |
3854 | transparency, or Qnil for the unspecified color. If the length of | |
3855 | the key string is one, a vector is used as a table. Otherwise, a | |
3856 | hash table is used. */ | |
3857 | ||
3858 | static Lisp_Object | |
3d608a86 J |
3859 | xpm_make_color_table_v (void (**put_func) (Lisp_Object, |
3860 | const unsigned char *, | |
3861 | int, | |
3862 | Lisp_Object), | |
3863 | Lisp_Object (**get_func) (Lisp_Object, | |
3864 | const unsigned char *, | |
3865 | int)) | |
ea1aaa6f ST |
3866 | { |
3867 | *put_func = xpm_put_color_table_v; | |
3868 | *get_func = xpm_get_color_table_v; | |
3869 | return Fmake_vector (make_number (256), Qnil); | |
3870 | } | |
3871 | ||
3872 | static void | |
3d608a86 J |
3873 | xpm_put_color_table_v (Lisp_Object color_table, |
3874 | const unsigned char *chars_start, | |
3875 | int chars_len, | |
3876 | Lisp_Object color) | |
ea1aaa6f | 3877 | { |
28be1ada | 3878 | ASET (color_table, *chars_start, color); |
ea1aaa6f ST |
3879 | } |
3880 | ||
3881 | static Lisp_Object | |
3d608a86 J |
3882 | xpm_get_color_table_v (Lisp_Object color_table, |
3883 | const unsigned char *chars_start, | |
3884 | int chars_len) | |
ea1aaa6f | 3885 | { |
28be1ada | 3886 | return AREF (color_table, *chars_start); |
ea1aaa6f ST |
3887 | } |
3888 | ||
3889 | static Lisp_Object | |
3d608a86 J |
3890 | xpm_make_color_table_h (void (**put_func) (Lisp_Object, |
3891 | const unsigned char *, | |
3892 | int, | |
3893 | Lisp_Object), | |
3894 | Lisp_Object (**get_func) (Lisp_Object, | |
3895 | const unsigned char *, | |
3896 | int)) | |
ea1aaa6f ST |
3897 | { |
3898 | *put_func = xpm_put_color_table_h; | |
3899 | *get_func = xpm_get_color_table_h; | |
4ab15c3e | 3900 | return make_hash_table (hashtest_equal, make_number (DEFAULT_HASH_SIZE), |
9ecf6403 YM |
3901 | make_float (DEFAULT_REHASH_SIZE), |
3902 | make_float (DEFAULT_REHASH_THRESHOLD), | |
a23c4171 | 3903 | Qnil); |
ea1aaa6f ST |
3904 | } |
3905 | ||
3906 | static void | |
3d608a86 J |
3907 | xpm_put_color_table_h (Lisp_Object color_table, |
3908 | const unsigned char *chars_start, | |
3909 | int chars_len, | |
3910 | Lisp_Object color) | |
ea1aaa6f ST |
3911 | { |
3912 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
0de4bb68 | 3913 | EMACS_UINT hash_code; |
ea1aaa6f ST |
3914 | Lisp_Object chars = make_unibyte_string (chars_start, chars_len); |
3915 | ||
3916 | hash_lookup (table, chars, &hash_code); | |
3917 | hash_put (table, chars, color, hash_code); | |
3918 | } | |
3919 | ||
3920 | static Lisp_Object | |
3d608a86 J |
3921 | xpm_get_color_table_h (Lisp_Object color_table, |
3922 | const unsigned char *chars_start, | |
3923 | int chars_len) | |
ea1aaa6f ST |
3924 | { |
3925 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
d3411f89 PE |
3926 | ptrdiff_t i = |
3927 | hash_lookup (table, make_unibyte_string (chars_start, chars_len), NULL); | |
ea1aaa6f ST |
3928 | |
3929 | return i >= 0 ? HASH_VALUE (table, i) : Qnil; | |
3930 | } | |
3931 | ||
3932 | enum xpm_color_key { | |
3933 | XPM_COLOR_KEY_S, | |
3934 | XPM_COLOR_KEY_M, | |
3935 | XPM_COLOR_KEY_G4, | |
3936 | XPM_COLOR_KEY_G, | |
3937 | XPM_COLOR_KEY_C | |
3938 | }; | |
3939 | ||
46accb8f | 3940 | static const char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"}; |
ea1aaa6f ST |
3941 | |
3942 | static int | |
3d608a86 | 3943 | xpm_str_to_color_key (const char *s) |
ea1aaa6f ST |
3944 | { |
3945 | int i; | |
3946 | ||
3947 | for (i = 0; | |
3948 | i < sizeof xpm_color_key_strings / sizeof xpm_color_key_strings[0]; | |
3949 | i++) | |
3950 | if (strcmp (xpm_color_key_strings[i], s) == 0) | |
3951 | return i; | |
3952 | return -1; | |
3953 | } | |
3954 | ||
578098f3 | 3955 | static bool |
3d608a86 J |
3956 | xpm_load_image (struct frame *f, |
3957 | struct image *img, | |
3958 | const unsigned char *contents, | |
3959 | const unsigned char *end) | |
ea1aaa6f | 3960 | { |
46accb8f | 3961 | const unsigned char *s = contents, *beg, *str; |
ea1aaa6f ST |
3962 | unsigned char buffer[BUFSIZ]; |
3963 | int width, height, x, y; | |
3964 | int num_colors, chars_per_pixel; | |
74ca2eb3 PE |
3965 | ptrdiff_t len; |
3966 | int LA1; | |
46accb8f YM |
3967 | void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object); |
3968 | Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int); | |
ea1aaa6f | 3969 | Lisp_Object frame, color_symbols, color_table; |
578098f3 PE |
3970 | int best_key; |
3971 | bool have_mask = 0; | |
ea1aaa6f ST |
3972 | XImagePtr ximg = NULL, mask_img = NULL; |
3973 | ||
3974 | #define match() \ | |
3975 | LA1 = xpm_scan (&s, end, &beg, &len) | |
3976 | ||
9ecf6403 | 3977 | #define expect(TOKEN) \ |
f462f075 PE |
3978 | do \ |
3979 | { \ | |
3980 | if (LA1 != (TOKEN)) \ | |
3981 | goto failure; \ | |
3982 | match (); \ | |
3983 | } \ | |
3984 | while (0) | |
ea1aaa6f | 3985 | |
9ecf6403 | 3986 | #define expect_ident(IDENT) \ |
ea1aaa6f | 3987 | if (LA1 == XPM_TK_IDENT \ |
9ecf6403 YM |
3988 | && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \ |
3989 | match (); \ | |
3990 | else \ | |
ea1aaa6f ST |
3991 | goto failure |
3992 | ||
3993 | if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0)) | |
3994 | goto failure; | |
3995 | s += 9; | |
ed3751c8 | 3996 | match (); |
ea1aaa6f ST |
3997 | expect_ident ("static"); |
3998 | expect_ident ("char"); | |
3999 | expect ('*'); | |
4000 | expect (XPM_TK_IDENT); | |
4001 | expect ('['); | |
4002 | expect (']'); | |
4003 | expect ('='); | |
4004 | expect ('{'); | |
4005 | expect (XPM_TK_STRING); | |
4006 | if (len >= BUFSIZ) | |
4007 | goto failure; | |
4008 | memcpy (buffer, beg, len); | |
4009 | buffer[len] = '\0'; | |
4010 | if (sscanf (buffer, "%d %d %d %d", &width, &height, | |
9ecf6403 | 4011 | &num_colors, &chars_per_pixel) != 4 |
ea1aaa6f ST |
4012 | || width <= 0 || height <= 0 |
4013 | || num_colors <= 0 || chars_per_pixel <= 0) | |
4014 | goto failure; | |
de297941 YM |
4015 | |
4016 | if (!check_image_size (f, width, height)) | |
4017 | { | |
10ea2b82 | 4018 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
de297941 YM |
4019 | goto failure; |
4020 | } | |
4021 | ||
547c9269 | 4022 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0) |
ca4aa935 | 4023 | #ifndef HAVE_NS |
547c9269 YM |
4024 | || !image_create_x_image_and_pixmap (f, img, width, height, 1, |
4025 | &mask_img, 1) | |
ca4aa935 PE |
4026 | #endif |
4027 | ) | |
4028 | { | |
4029 | image_error ("Image too large", Qnil, Qnil); | |
4030 | goto failure; | |
4031 | } | |
4032 | ||
ea1aaa6f ST |
4033 | expect (','); |
4034 | ||
4035 | XSETFRAME (frame, f); | |
4036 | if (!NILP (Fxw_display_color_p (frame))) | |
4037 | best_key = XPM_COLOR_KEY_C; | |
4038 | else if (!NILP (Fx_display_grayscale_p (frame))) | |
4039 | best_key = (XFASTINT (Fx_display_planes (frame)) > 2 | |
9ecf6403 | 4040 | ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4); |
ea1aaa6f ST |
4041 | else |
4042 | best_key = XPM_COLOR_KEY_M; | |
4043 | ||
4044 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
4045 | if (chars_per_pixel == 1) | |
4046 | color_table = xpm_make_color_table_v (&put_color_table, | |
9ecf6403 | 4047 | &get_color_table); |
ea1aaa6f ST |
4048 | else |
4049 | color_table = xpm_make_color_table_h (&put_color_table, | |
9ecf6403 | 4050 | &get_color_table); |
ea1aaa6f ST |
4051 | |
4052 | while (num_colors-- > 0) | |
4053 | { | |
25a48bd0 | 4054 | char *color, *max_color; |
ea1aaa6f ST |
4055 | int key, next_key, max_key = 0; |
4056 | Lisp_Object symbol_color = Qnil, color_val; | |
4057 | XColor cdef; | |
4058 | ||
4059 | expect (XPM_TK_STRING); | |
4060 | if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel) | |
9ecf6403 | 4061 | goto failure; |
ea1aaa6f ST |
4062 | memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel); |
4063 | buffer[len - chars_per_pixel] = '\0'; | |
4064 | ||
4065 | str = strtok (buffer, " \t"); | |
4066 | if (str == NULL) | |
9ecf6403 | 4067 | goto failure; |
ea1aaa6f ST |
4068 | key = xpm_str_to_color_key (str); |
4069 | if (key < 0) | |
9ecf6403 | 4070 | goto failure; |
ea1aaa6f | 4071 | do |
9ecf6403 YM |
4072 | { |
4073 | color = strtok (NULL, " \t"); | |
4074 | if (color == NULL) | |
4075 | goto failure; | |
ea1aaa6f | 4076 | |
9db94f82 | 4077 | while ((str = strtok (NULL, " \t")) != NULL) |
9ecf6403 YM |
4078 | { |
4079 | next_key = xpm_str_to_color_key (str); | |
4080 | if (next_key >= 0) | |
4081 | break; | |
4082 | color[strlen (color)] = ' '; | |
4083 | } | |
ea1aaa6f | 4084 | |
9ecf6403 YM |
4085 | if (key == XPM_COLOR_KEY_S) |
4086 | { | |
4087 | if (NILP (symbol_color)) | |
4088 | symbol_color = build_string (color); | |
4089 | } | |
4090 | else if (max_key < key && key <= best_key) | |
4091 | { | |
4092 | max_key = key; | |
4093 | max_color = color; | |
4094 | } | |
4095 | key = next_key; | |
4096 | } | |
ea1aaa6f ST |
4097 | while (str); |
4098 | ||
4099 | color_val = Qnil; | |
4100 | if (!NILP (color_symbols) && !NILP (symbol_color)) | |
9ecf6403 YM |
4101 | { |
4102 | Lisp_Object specified_color = Fassoc (symbol_color, color_symbols); | |
4103 | ||
4104 | if (CONSP (specified_color) && STRINGP (XCDR (specified_color))) | |
9db94f82 | 4105 | { |
25a48bd0 | 4106 | if (xstrcasecmp (SSDATA (XCDR (specified_color)), "None") == 0) |
9db94f82 | 4107 | color_val = Qt; |
6045c4fd | 4108 | else if (x_defined_color (f, SSDATA (XCDR (specified_color)), |
9db94f82 YM |
4109 | &cdef, 0)) |
4110 | color_val = make_number (cdef.pixel); | |
4111 | } | |
9ecf6403 | 4112 | } |
ea1aaa6f | 4113 | if (NILP (color_val) && max_key > 0) |
9db94f82 | 4114 | { |
05131107 | 4115 | if (xstrcasecmp (max_color, "None") == 0) |
9db94f82 YM |
4116 | color_val = Qt; |
4117 | else if (x_defined_color (f, max_color, &cdef, 0)) | |
4118 | color_val = make_number (cdef.pixel); | |
4119 | } | |
ea1aaa6f | 4120 | if (!NILP (color_val)) |
9ecf6403 | 4121 | (*put_color_table) (color_table, beg, chars_per_pixel, color_val); |
ea1aaa6f ST |
4122 | |
4123 | expect (','); | |
4124 | } | |
4125 | ||
ea1aaa6f ST |
4126 | for (y = 0; y < height; y++) |
4127 | { | |
4128 | expect (XPM_TK_STRING); | |
4129 | str = beg; | |
4130 | if (len < width * chars_per_pixel) | |
9ecf6403 | 4131 | goto failure; |
ea1aaa6f | 4132 | for (x = 0; x < width; x++, str += chars_per_pixel) |
9ecf6403 YM |
4133 | { |
4134 | Lisp_Object color_val = | |
4135 | (*get_color_table) (color_table, str, chars_per_pixel); | |
4136 | ||
4137 | XPutPixel (ximg, x, y, | |
4138 | (INTEGERP (color_val) ? XINT (color_val) | |
4139 | : FRAME_FOREGROUND_PIXEL (f))); | |
edfda783 | 4140 | #ifndef HAVE_NS |
9ecf6403 YM |
4141 | XPutPixel (mask_img, x, y, |
4142 | (!EQ (color_val, Qt) ? PIX_MASK_DRAW | |
4143 | : (have_mask = 1, PIX_MASK_RETAIN))); | |
edfda783 | 4144 | #else |
ed3751c8 JB |
4145 | if (EQ (color_val, Qt)) |
4146 | ns_set_alpha (ximg, x, y, 0); | |
edfda783 | 4147 | #endif |
9ecf6403 | 4148 | } |
ea1aaa6f | 4149 | if (y + 1 < height) |
9ecf6403 | 4150 | expect (','); |
ea1aaa6f ST |
4151 | } |
4152 | ||
4153 | img->width = width; | |
4154 | img->height = height; | |
4155 | ||
0b8ac842 YM |
4156 | /* Maybe fill in the background field while we have ximg handy. */ |
4157 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
4158 | IMAGE_BACKGROUND (img, f, ximg); | |
4159 | ||
547c9269 | 4160 | image_put_x_image (f, img, ximg, 0); |
edfda783 | 4161 | #ifndef HAVE_NS |
ea1aaa6f ST |
4162 | if (have_mask) |
4163 | { | |
9ecf6403 YM |
4164 | /* Fill in the background_transparent field while we have the |
4165 | mask handy. */ | |
4166 | image_background_transparent (img, f, mask_img); | |
4167 | ||
547c9269 | 4168 | image_put_x_image (f, img, mask_img, 1); |
ea1aaa6f ST |
4169 | } |
4170 | else | |
4171 | { | |
4172 | x_destroy_x_image (mask_img); | |
547c9269 | 4173 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
ea1aaa6f | 4174 | } |
edfda783 | 4175 | #endif |
ea1aaa6f ST |
4176 | return 1; |
4177 | ||
4178 | failure: | |
4179 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
ea1aaa6f ST |
4180 | x_destroy_x_image (ximg); |
4181 | x_destroy_x_image (mask_img); | |
4182 | x_clear_image (f, img); | |
4183 | return 0; | |
4184 | ||
4185 | #undef match | |
4186 | #undef expect | |
4187 | #undef expect_ident | |
4188 | } | |
4189 | ||
578098f3 | 4190 | static bool |
3d608a86 J |
4191 | xpm_load (struct frame *f, |
4192 | struct image *img) | |
ea1aaa6f | 4193 | { |
578098f3 | 4194 | bool success_p = 0; |
ea1aaa6f ST |
4195 | Lisp_Object file_name; |
4196 | ||
4197 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
4198 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
4199 | if (STRINGP (file_name)) | |
4200 | { | |
4201 | Lisp_Object file; | |
4202 | unsigned char *contents; | |
dd52fcea | 4203 | ptrdiff_t size; |
ea1aaa6f ST |
4204 | |
4205 | file = x_find_image_file (file_name); | |
ea1aaa6f | 4206 | if (!STRINGP (file)) |
9ecf6403 YM |
4207 | { |
4208 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
9ecf6403 YM |
4209 | return 0; |
4210 | } | |
ea1aaa6f | 4211 | |
6045c4fd | 4212 | contents = slurp_file (SSDATA (file), &size); |
ea1aaa6f | 4213 | if (contents == NULL) |
9ecf6403 YM |
4214 | { |
4215 | image_error ("Error loading XPM image `%s'", img->spec, Qnil); | |
9ecf6403 YM |
4216 | return 0; |
4217 | } | |
ea1aaa6f ST |
4218 | |
4219 | success_p = xpm_load_image (f, img, contents, contents + size); | |
4220 | xfree (contents); | |
ea1aaa6f ST |
4221 | } |
4222 | else | |
4223 | { | |
4224 | Lisp_Object data; | |
4225 | ||
4226 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
4227 | if (!STRINGP (data)) |
4228 | { | |
4229 | image_error ("Invalid image data `%s'", data, Qnil); | |
4230 | return 0; | |
4231 | } | |
ea1aaa6f | 4232 | success_p = xpm_load_image (f, img, SDATA (data), |
9ecf6403 | 4233 | SDATA (data) + SBYTES (data)); |
ea1aaa6f ST |
4234 | } |
4235 | ||
4236 | return success_p; | |
4237 | } | |
4238 | ||
9e2a2647 | 4239 | #endif /* HAVE_NS && !HAVE_XPM */ |
b0da69a7 | 4240 | |
ea1aaa6f | 4241 | |
4ef0d4d0 KS |
4242 | \f |
4243 | /*********************************************************************** | |
4244 | Color table | |
4245 | ***********************************************************************/ | |
4246 | ||
4247 | #ifdef COLOR_TABLE_SUPPORT | |
4248 | ||
4249 | /* An entry in the color table mapping an RGB color to a pixel color. */ | |
4250 | ||
4251 | struct ct_color | |
4252 | { | |
4253 | int r, g, b; | |
4254 | unsigned long pixel; | |
4255 | ||
4256 | /* Next in color table collision list. */ | |
4257 | struct ct_color *next; | |
4258 | }; | |
4259 | ||
4260 | /* The bucket vector size to use. Must be prime. */ | |
4261 | ||
4262 | #define CT_SIZE 101 | |
4263 | ||
4264 | /* Value is a hash of the RGB color given by R, G, and B. */ | |
4265 | ||
4266 | #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B)) | |
4267 | ||
4268 | /* The color hash table. */ | |
4269 | ||
cd44d2eb | 4270 | static struct ct_color **ct_table; |
4ef0d4d0 KS |
4271 | |
4272 | /* Number of entries in the color table. */ | |
4273 | ||
cd44d2eb | 4274 | static int ct_colors_allocated; |
ddff3151 PE |
4275 | enum |
4276 | { | |
4277 | ct_colors_allocated_max = | |
4278 | min (INT_MAX, | |
4279 | min (PTRDIFF_MAX, SIZE_MAX) / sizeof (unsigned long)) | |
4280 | }; | |
4ef0d4d0 KS |
4281 | |
4282 | /* Initialize the color table. */ | |
4283 | ||
4284 | static void | |
d3da34e0 | 4285 | init_color_table (void) |
4ef0d4d0 KS |
4286 | { |
4287 | int size = CT_SIZE * sizeof (*ct_table); | |
23f86fce | 4288 | ct_table = xzalloc (size); |
4ef0d4d0 KS |
4289 | ct_colors_allocated = 0; |
4290 | } | |
4291 | ||
4292 | ||
4293 | /* Free memory associated with the color table. */ | |
4294 | ||
4295 | static void | |
d3da34e0 | 4296 | free_color_table (void) |
4ef0d4d0 KS |
4297 | { |
4298 | int i; | |
4299 | struct ct_color *p, *next; | |
4300 | ||
4301 | for (i = 0; i < CT_SIZE; ++i) | |
4302 | for (p = ct_table[i]; p; p = next) | |
4303 | { | |
4304 | next = p->next; | |
4305 | xfree (p); | |
4306 | } | |
4307 | ||
4308 | xfree (ct_table); | |
4309 | ct_table = NULL; | |
4310 | } | |
4311 | ||
4312 | ||
4313 | /* Value is a pixel color for RGB color R, G, B on frame F. If an | |
4314 | entry for that color already is in the color table, return the | |
4315 | pixel color of that entry. Otherwise, allocate a new color for R, | |
4316 | G, B, and make an entry in the color table. */ | |
4317 | ||
4318 | static unsigned long | |
d3da34e0 | 4319 | lookup_rgb_color (struct frame *f, int r, int g, int b) |
4ef0d4d0 KS |
4320 | { |
4321 | unsigned hash = CT_HASH_RGB (r, g, b); | |
4322 | int i = hash % CT_SIZE; | |
4323 | struct ct_color *p; | |
4324 | Display_Info *dpyinfo; | |
4325 | ||
4326 | /* Handle TrueColor visuals specially, which improves performance by | |
4327 | two orders of magnitude. Freeing colors on TrueColor visuals is | |
4328 | a nop, and pixel colors specify RGB values directly. See also | |
4329 | the Xlib spec, chapter 3.1. */ | |
aad3612f | 4330 | dpyinfo = FRAME_DISPLAY_INFO (f); |
4ef0d4d0 KS |
4331 | if (dpyinfo->red_bits > 0) |
4332 | { | |
4333 | unsigned long pr, pg, pb; | |
4334 | ||
4335 | /* Apply gamma-correction like normal color allocation does. */ | |
4336 | if (f->gamma) | |
4337 | { | |
4338 | XColor color; | |
4339 | color.red = r, color.green = g, color.blue = b; | |
4340 | gamma_correct (f, &color); | |
4341 | r = color.red, g = color.green, b = color.blue; | |
4342 | } | |
4343 | ||
4344 | /* Scale down RGB values to the visual's bits per RGB, and shift | |
4345 | them to the right position in the pixel color. Note that the | |
4346 | original RGB values are 16-bit values, as usual in X. */ | |
4347 | pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset; | |
4348 | pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset; | |
4349 | pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset; | |
4350 | ||
4351 | /* Assemble the pixel color. */ | |
4352 | return pr | pg | pb; | |
4353 | } | |
1f026899 | 4354 | |
4ef0d4d0 KS |
4355 | for (p = ct_table[i]; p; p = p->next) |
4356 | if (p->r == r && p->g == g && p->b == b) | |
4357 | break; | |
4358 | ||
4359 | if (p == NULL) | |
4360 | { | |
4361 | ||
4362 | #ifdef HAVE_X_WINDOWS | |
4363 | XColor color; | |
4364 | Colormap cmap; | |
578098f3 | 4365 | bool rc; |
ddff3151 PE |
4366 | #else |
4367 | COLORREF color; | |
4368 | #endif | |
4ef0d4d0 | 4369 | |
ddff3151 PE |
4370 | if (ct_colors_allocated_max <= ct_colors_allocated) |
4371 | return FRAME_FOREGROUND_PIXEL (f); | |
4372 | ||
4373 | #ifdef HAVE_X_WINDOWS | |
4ef0d4d0 KS |
4374 | color.red = r; |
4375 | color.green = g; | |
4376 | color.blue = b; | |
4377 | ||
4378 | cmap = FRAME_X_COLORMAP (f); | |
4379 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4380 | if (rc) | |
4381 | { | |
4382 | ++ct_colors_allocated; | |
23f86fce | 4383 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4384 | p->r = r; |
4385 | p->g = g; | |
4386 | p->b = b; | |
4387 | p->pixel = color.pixel; | |
4388 | p->next = ct_table[i]; | |
4389 | ct_table[i] = p; | |
4390 | } | |
4391 | else | |
4392 | return FRAME_FOREGROUND_PIXEL (f); | |
4393 | ||
4394 | #else | |
4ef0d4d0 KS |
4395 | #ifdef HAVE_NTGUI |
4396 | color = PALETTERGB (r, g, b); | |
4397 | #else | |
4398 | color = RGB_TO_ULONG (r, g, b); | |
4399 | #endif /* HAVE_NTGUI */ | |
4400 | ++ct_colors_allocated; | |
23f86fce | 4401 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4402 | p->r = r; |
4403 | p->g = g; | |
4404 | p->b = b; | |
4405 | p->pixel = color; | |
4406 | p->next = ct_table[i]; | |
4407 | ct_table[i] = p; | |
4408 | #endif /* HAVE_X_WINDOWS */ | |
4409 | ||
4410 | } | |
4411 | ||
4412 | return p->pixel; | |
4413 | } | |
4414 | ||
4415 | ||
4416 | /* Look up pixel color PIXEL which is used on frame F in the color | |
4417 | table. If not already present, allocate it. Value is PIXEL. */ | |
4418 | ||
4419 | static unsigned long | |
d3da34e0 | 4420 | lookup_pixel_color (struct frame *f, unsigned long pixel) |
4ef0d4d0 KS |
4421 | { |
4422 | int i = pixel % CT_SIZE; | |
4423 | struct ct_color *p; | |
4424 | ||
4425 | for (p = ct_table[i]; p; p = p->next) | |
4426 | if (p->pixel == pixel) | |
4427 | break; | |
4428 | ||
4429 | if (p == NULL) | |
4430 | { | |
4431 | XColor color; | |
4432 | Colormap cmap; | |
578098f3 | 4433 | bool rc; |
4ef0d4d0 | 4434 | |
ddff3151 PE |
4435 | if (ct_colors_allocated_max <= ct_colors_allocated) |
4436 | return FRAME_FOREGROUND_PIXEL (f); | |
4437 | ||
4ef0d4d0 KS |
4438 | #ifdef HAVE_X_WINDOWS |
4439 | cmap = FRAME_X_COLORMAP (f); | |
4440 | color.pixel = pixel; | |
4441 | x_query_color (f, &color); | |
4442 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4443 | #else | |
4d7e6e51 | 4444 | block_input (); |
4ef0d4d0 KS |
4445 | cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f)); |
4446 | color.pixel = pixel; | |
4447 | XQueryColor (NULL, cmap, &color); | |
4448 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4d7e6e51 | 4449 | unblock_input (); |
4ef0d4d0 KS |
4450 | #endif /* HAVE_X_WINDOWS */ |
4451 | ||
4452 | if (rc) | |
4453 | { | |
4454 | ++ct_colors_allocated; | |
4455 | ||
23f86fce | 4456 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4457 | p->r = color.red; |
4458 | p->g = color.green; | |
4459 | p->b = color.blue; | |
4460 | p->pixel = pixel; | |
4461 | p->next = ct_table[i]; | |
4462 | ct_table[i] = p; | |
4463 | } | |
4464 | else | |
4465 | return FRAME_FOREGROUND_PIXEL (f); | |
4466 | } | |
4467 | return p->pixel; | |
4468 | } | |
4469 | ||
4470 | ||
4471 | /* Value is a vector of all pixel colors contained in the color table, | |
4472 | allocated via xmalloc. Set *N to the number of colors. */ | |
4473 | ||
4474 | static unsigned long * | |
d3da34e0 | 4475 | colors_in_color_table (int *n) |
4ef0d4d0 KS |
4476 | { |
4477 | int i, j; | |
4478 | struct ct_color *p; | |
4479 | unsigned long *colors; | |
4480 | ||
4481 | if (ct_colors_allocated == 0) | |
4482 | { | |
4483 | *n = 0; | |
4484 | colors = NULL; | |
4485 | } | |
4486 | else | |
4487 | { | |
23f86fce | 4488 | colors = xmalloc (ct_colors_allocated * sizeof *colors); |
4ef0d4d0 KS |
4489 | *n = ct_colors_allocated; |
4490 | ||
4491 | for (i = j = 0; i < CT_SIZE; ++i) | |
4492 | for (p = ct_table[i]; p; p = p->next) | |
4493 | colors[j++] = p->pixel; | |
4494 | } | |
4495 | ||
4496 | return colors; | |
4497 | } | |
4498 | ||
4499 | #else /* COLOR_TABLE_SUPPORT */ | |
4500 | ||
4501 | static unsigned long | |
7c3320d8 | 4502 | lookup_rgb_color (struct frame *f, int r, int g, int b) |
4ef0d4d0 KS |
4503 | { |
4504 | unsigned long pixel; | |
4505 | ||
4ef0d4d0 KS |
4506 | #ifdef HAVE_NTGUI |
4507 | pixel = PALETTERGB (r >> 8, g >> 8, b >> 8); | |
4508 | #endif /* HAVE_NTGUI */ | |
4509 | ||
edfda783 AR |
4510 | #ifdef HAVE_NS |
4511 | pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8); | |
4512 | #endif /* HAVE_NS */ | |
4ef0d4d0 KS |
4513 | return pixel; |
4514 | } | |
4515 | ||
4516 | static void | |
7c3320d8 | 4517 | init_color_table (void) |
4ef0d4d0 KS |
4518 | { |
4519 | } | |
4520 | #endif /* COLOR_TABLE_SUPPORT */ | |
4521 | ||
4522 | \f | |
4523 | /*********************************************************************** | |
4524 | Algorithms | |
4525 | ***********************************************************************/ | |
4526 | ||
4ef0d4d0 KS |
4527 | /* Edge detection matrices for different edge-detection |
4528 | strategies. */ | |
4529 | ||
4530 | static int emboss_matrix[9] = { | |
4531 | /* x - 1 x x + 1 */ | |
4532 | 2, -1, 0, /* y - 1 */ | |
4533 | -1, 0, 1, /* y */ | |
4534 | 0, 1, -2 /* y + 1 */ | |
4535 | }; | |
4536 | ||
4537 | static int laplace_matrix[9] = { | |
4538 | /* x - 1 x x + 1 */ | |
4539 | 1, 0, 0, /* y - 1 */ | |
4540 | 0, 0, 0, /* y */ | |
4541 | 0, 0, -1 /* y + 1 */ | |
4542 | }; | |
4543 | ||
4544 | /* Value is the intensity of the color whose red/green/blue values | |
4545 | are R, G, and B. */ | |
4546 | ||
4547 | #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6) | |
4548 | ||
4549 | ||
4550 | /* On frame F, return an array of XColor structures describing image | |
4551 | IMG->pixmap. Each XColor structure has its pixel color set. RGB_P | |
578098f3 | 4552 | means also fill the red/green/blue members of the XColor |
4ef0d4d0 KS |
4553 | structures. Value is a pointer to the array of XColors structures, |
4554 | allocated with xmalloc; it must be freed by the caller. */ | |
4555 | ||
4556 | static XColor * | |
578098f3 | 4557 | x_to_xcolors (struct frame *f, struct image *img, bool rgb_p) |
4ef0d4d0 KS |
4558 | { |
4559 | int x, y; | |
4560 | XColor *colors, *p; | |
4561 | XImagePtr_or_DC ximg; | |
4562 | #ifdef HAVE_NTGUI | |
4ef0d4d0 KS |
4563 | HGDIOBJ prev; |
4564 | #endif /* HAVE_NTGUI */ | |
4565 | ||
ddff3151 PE |
4566 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width < img->height) |
4567 | memory_full (SIZE_MAX); | |
23f86fce | 4568 | colors = xmalloc (sizeof *colors * img->width * img->height); |
4ef0d4d0 | 4569 | |
547c9269 YM |
4570 | /* Get the X image or create a memory device context for IMG. */ |
4571 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); | |
4ef0d4d0 KS |
4572 | |
4573 | /* Fill the `pixel' members of the XColor array. I wished there | |
4574 | were an easy and portable way to circumvent XGetPixel. */ | |
4575 | p = colors; | |
4576 | for (y = 0; y < img->height; ++y) | |
4577 | { | |
8b7d0a16 | 4578 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
6045c4fd | 4579 | XColor *row = p; |
4ef0d4d0 | 4580 | for (x = 0; x < img->width; ++x, ++p) |
8b7d0a16 | 4581 | p->pixel = GET_PIXEL (ximg, x, y); |
4ef0d4d0 KS |
4582 | if (rgb_p) |
4583 | x_query_colors (f, row, img->width); | |
4584 | ||
4585 | #else | |
4586 | ||
4587 | for (x = 0; x < img->width; ++x, ++p) | |
4588 | { | |
4589 | /* W32_TODO: palette support needed here? */ | |
4590 | p->pixel = GET_PIXEL (ximg, x, y); | |
4591 | if (rgb_p) | |
4592 | { | |
4ef0d4d0 KS |
4593 | p->red = RED16_FROM_ULONG (p->pixel); |
4594 | p->green = GREEN16_FROM_ULONG (p->pixel); | |
4595 | p->blue = BLUE16_FROM_ULONG (p->pixel); | |
4ef0d4d0 KS |
4596 | } |
4597 | } | |
4598 | #endif /* HAVE_X_WINDOWS */ | |
4599 | } | |
4600 | ||
547c9269 | 4601 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
4ef0d4d0 KS |
4602 | |
4603 | return colors; | |
4604 | } | |
4605 | ||
4606 | #ifdef HAVE_NTGUI | |
4607 | ||
4608 | /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been | |
4609 | created with CreateDIBSection, with the pointer to the bit values | |
4610 | stored in ximg->data. */ | |
4611 | ||
3046a84b | 4612 | static void |
7c3320d8 | 4613 | XPutPixel (XImagePtr ximg, int x, int y, COLORREF color) |
4ef0d4d0 KS |
4614 | { |
4615 | int width = ximg->info.bmiHeader.biWidth; | |
4ef0d4d0 KS |
4616 | unsigned char * pixel; |
4617 | ||
4618 | /* True color images. */ | |
4619 | if (ximg->info.bmiHeader.biBitCount == 24) | |
4620 | { | |
4621 | int rowbytes = width * 3; | |
4622 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4623 | if (rowbytes % 4) | |
4624 | rowbytes += 4 - (rowbytes % 4); | |
4625 | ||
4626 | pixel = ximg->data + y * rowbytes + x * 3; | |
4627 | /* Windows bitmaps are in BGR order. */ | |
4628 | *pixel = GetBValue (color); | |
4629 | *(pixel + 1) = GetGValue (color); | |
4630 | *(pixel + 2) = GetRValue (color); | |
4631 | } | |
4632 | /* Monochrome images. */ | |
4633 | else if (ximg->info.bmiHeader.biBitCount == 1) | |
4634 | { | |
4635 | int rowbytes = width / 8; | |
4636 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4637 | if (rowbytes % 4) | |
4638 | rowbytes += 4 - (rowbytes % 4); | |
4639 | pixel = ximg->data + y * rowbytes + x / 8; | |
4640 | /* Filter out palette info. */ | |
4641 | if (color & 0x00ffffff) | |
4642 | *pixel = *pixel | (1 << x % 8); | |
4643 | else | |
4644 | *pixel = *pixel & ~(1 << x % 8); | |
4645 | } | |
4646 | else | |
4647 | image_error ("XPutPixel: palette image not supported", Qnil, Qnil); | |
4648 | } | |
4649 | ||
4650 | #endif /* HAVE_NTGUI */ | |
4651 | ||
4652 | /* Create IMG->pixmap from an array COLORS of XColor structures, whose | |
4653 | RGB members are set. F is the frame on which this all happens. | |
4654 | COLORS will be freed; an existing IMG->pixmap will be freed, too. */ | |
4655 | ||
4656 | static void | |
d3da34e0 | 4657 | x_from_xcolors (struct frame *f, struct image *img, XColor *colors) |
4ef0d4d0 KS |
4658 | { |
4659 | int x, y; | |
edfda783 | 4660 | XImagePtr oimg = NULL; |
4ef0d4d0 KS |
4661 | XColor *p; |
4662 | ||
4663 | init_color_table (); | |
4664 | ||
547c9269 YM |
4665 | x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_COLORS); |
4666 | image_create_x_image_and_pixmap (f, img, img->width, img->height, 0, | |
4667 | &oimg, 0); | |
4ef0d4d0 KS |
4668 | p = colors; |
4669 | for (y = 0; y < img->height; ++y) | |
4670 | for (x = 0; x < img->width; ++x, ++p) | |
4671 | { | |
4672 | unsigned long pixel; | |
4673 | pixel = lookup_rgb_color (f, p->red, p->green, p->blue); | |
4674 | XPutPixel (oimg, x, y, pixel); | |
4675 | } | |
4676 | ||
4677 | xfree (colors); | |
4ef0d4d0 | 4678 | |
547c9269 | 4679 | image_put_x_image (f, img, oimg, 0); |
4ef0d4d0 KS |
4680 | #ifdef COLOR_TABLE_SUPPORT |
4681 | img->colors = colors_in_color_table (&img->ncolors); | |
4682 | free_color_table (); | |
4683 | #endif /* COLOR_TABLE_SUPPORT */ | |
4684 | } | |
4685 | ||
4686 | ||
4687 | /* On frame F, perform edge-detection on image IMG. | |
4688 | ||
4689 | MATRIX is a nine-element array specifying the transformation | |
4690 | matrix. See emboss_matrix for an example. | |
4691 | ||
4692 | COLOR_ADJUST is a color adjustment added to each pixel of the | |
4693 | outgoing image. */ | |
4694 | ||
4695 | static void | |
d3da34e0 | 4696 | x_detect_edges (struct frame *f, struct image *img, int *matrix, int color_adjust) |
4ef0d4d0 KS |
4697 | { |
4698 | XColor *colors = x_to_xcolors (f, img, 1); | |
4699 | XColor *new, *p; | |
4700 | int x, y, i, sum; | |
4701 | ||
4702 | for (i = sum = 0; i < 9; ++i) | |
1ea40aa2 | 4703 | sum += eabs (matrix[i]); |
4ef0d4d0 KS |
4704 | |
4705 | #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X)) | |
4706 | ||
ddff3151 PE |
4707 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width < img->height) |
4708 | memory_full (SIZE_MAX); | |
23f86fce | 4709 | new = xmalloc (sizeof *new * img->width * img->height); |
4ef0d4d0 KS |
4710 | |
4711 | for (y = 0; y < img->height; ++y) | |
4712 | { | |
4713 | p = COLOR (new, 0, y); | |
4714 | p->red = p->green = p->blue = 0xffff/2; | |
4715 | p = COLOR (new, img->width - 1, y); | |
4716 | p->red = p->green = p->blue = 0xffff/2; | |
4717 | } | |
4718 | ||
4719 | for (x = 1; x < img->width - 1; ++x) | |
4720 | { | |
4721 | p = COLOR (new, x, 0); | |
4722 | p->red = p->green = p->blue = 0xffff/2; | |
4723 | p = COLOR (new, x, img->height - 1); | |
4724 | p->red = p->green = p->blue = 0xffff/2; | |
4725 | } | |
4726 | ||
4727 | for (y = 1; y < img->height - 1; ++y) | |
4728 | { | |
4729 | p = COLOR (new, 1, y); | |
4730 | ||
4731 | for (x = 1; x < img->width - 1; ++x, ++p) | |
4732 | { | |
6ae141d6 | 4733 | int r, g, b, yy, xx; |
4ef0d4d0 KS |
4734 | |
4735 | r = g = b = i = 0; | |
6ae141d6 PE |
4736 | for (yy = y - 1; yy < y + 2; ++yy) |
4737 | for (xx = x - 1; xx < x + 2; ++xx, ++i) | |
4ef0d4d0 KS |
4738 | if (matrix[i]) |
4739 | { | |
6ae141d6 | 4740 | XColor *t = COLOR (colors, xx, yy); |
4ef0d4d0 KS |
4741 | r += matrix[i] * t->red; |
4742 | g += matrix[i] * t->green; | |
4743 | b += matrix[i] * t->blue; | |
4744 | } | |
4745 | ||
4746 | r = (r / sum + color_adjust) & 0xffff; | |
4747 | g = (g / sum + color_adjust) & 0xffff; | |
4748 | b = (b / sum + color_adjust) & 0xffff; | |
4749 | p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b); | |
4750 | } | |
4751 | } | |
4752 | ||
4753 | xfree (colors); | |
4754 | x_from_xcolors (f, img, new); | |
4755 | ||
4756 | #undef COLOR | |
4757 | } | |
4758 | ||
4759 | ||
4760 | /* Perform the pre-defined `emboss' edge-detection on image IMG | |
4761 | on frame F. */ | |
4762 | ||
4763 | static void | |
d3da34e0 | 4764 | x_emboss (struct frame *f, struct image *img) |
4ef0d4d0 KS |
4765 | { |
4766 | x_detect_edges (f, img, emboss_matrix, 0xffff / 2); | |
4767 | } | |
4768 | ||
4769 | ||
4770 | /* Transform image IMG which is used on frame F with a Laplace | |
4771 | edge-detection algorithm. The result is an image that can be used | |
4772 | to draw disabled buttons, for example. */ | |
4773 | ||
4774 | static void | |
d3da34e0 | 4775 | x_laplace (struct frame *f, struct image *img) |
4ef0d4d0 KS |
4776 | { |
4777 | x_detect_edges (f, img, laplace_matrix, 45000); | |
4778 | } | |
4779 | ||
4780 | ||
4781 | /* Perform edge-detection on image IMG on frame F, with specified | |
4782 | transformation matrix MATRIX and color-adjustment COLOR_ADJUST. | |
4783 | ||
4784 | MATRIX must be either | |
4785 | ||
4786 | - a list of at least 9 numbers in row-major form | |
4787 | - a vector of at least 9 numbers | |
4788 | ||
4789 | COLOR_ADJUST nil means use a default; otherwise it must be a | |
4790 | number. */ | |
4791 | ||
4792 | static void | |
d3da34e0 JB |
4793 | x_edge_detection (struct frame *f, struct image *img, Lisp_Object matrix, |
4794 | Lisp_Object color_adjust) | |
4ef0d4d0 KS |
4795 | { |
4796 | int i = 0; | |
4797 | int trans[9]; | |
4798 | ||
4799 | if (CONSP (matrix)) | |
4800 | { | |
4801 | for (i = 0; | |
4802 | i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix)); | |
4803 | ++i, matrix = XCDR (matrix)) | |
4804 | trans[i] = XFLOATINT (XCAR (matrix)); | |
4805 | } | |
4806 | else if (VECTORP (matrix) && ASIZE (matrix) >= 9) | |
4807 | { | |
4808 | for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i) | |
4809 | trans[i] = XFLOATINT (AREF (matrix, i)); | |
4810 | } | |
4811 | ||
4812 | if (NILP (color_adjust)) | |
4813 | color_adjust = make_number (0xffff / 2); | |
4814 | ||
4815 | if (i == 9 && NUMBERP (color_adjust)) | |
77a765fd | 4816 | x_detect_edges (f, img, trans, XFLOATINT (color_adjust)); |
4ef0d4d0 KS |
4817 | } |
4818 | ||
4819 | ||
4820 | /* Transform image IMG on frame F so that it looks disabled. */ | |
4821 | ||
4822 | static void | |
d3da34e0 | 4823 | x_disable_image (struct frame *f, struct image *img) |
4ef0d4d0 | 4824 | { |
aad3612f | 4825 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
4ef0d4d0 KS |
4826 | #ifdef HAVE_NTGUI |
4827 | int n_planes = dpyinfo->n_planes * dpyinfo->n_cbits; | |
4828 | #else | |
4829 | int n_planes = dpyinfo->n_planes; | |
4830 | #endif /* HAVE_NTGUI */ | |
4831 | ||
4832 | if (n_planes >= 2) | |
4833 | { | |
4834 | /* Color (or grayscale). Convert to gray, and equalize. Just | |
4835 | drawing such images with a stipple can look very odd, so | |
4836 | we're using this method instead. */ | |
4837 | XColor *colors = x_to_xcolors (f, img, 1); | |
4838 | XColor *p, *end; | |
4839 | const int h = 15000; | |
4840 | const int l = 30000; | |
4841 | ||
4842 | for (p = colors, end = colors + img->width * img->height; | |
4843 | p < end; | |
4844 | ++p) | |
4845 | { | |
4846 | int i = COLOR_INTENSITY (p->red, p->green, p->blue); | |
4847 | int i2 = (0xffff - h - l) * i / 0xffff + l; | |
4848 | p->red = p->green = p->blue = i2; | |
4849 | } | |
4850 | ||
4851 | x_from_xcolors (f, img, colors); | |
4852 | } | |
4853 | ||
4854 | /* Draw a cross over the disabled image, if we must or if we | |
4855 | should. */ | |
4856 | if (n_planes < 2 || cross_disabled_images) | |
4857 | { | |
4858 | #ifndef HAVE_NTGUI | |
5afa3a81 | 4859 | #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */ |
edfda783 | 4860 | |
4ef0d4d0 | 4861 | #define MaskForeground(f) WHITE_PIX_DEFAULT (f) |
4ef0d4d0 | 4862 | |
6045c4fd | 4863 | Display *dpy = FRAME_X_DISPLAY (f); |
547c9269 YM |
4864 | GC gc; |
4865 | ||
4866 | image_sync_to_pixmaps (f, img); | |
4867 | gc = XCreateGC (dpy, img->pixmap, 0, NULL); | |
4ef0d4d0 KS |
4868 | XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f)); |
4869 | XDrawLine (dpy, img->pixmap, gc, 0, 0, | |
4870 | img->width - 1, img->height - 1); | |
4871 | XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1, | |
4872 | img->width - 1, 0); | |
4873 | XFreeGC (dpy, gc); | |
4874 | ||
4875 | if (img->mask) | |
4876 | { | |
ae8279db | 4877 | gc = XCreateGC (dpy, img->mask, 0, NULL); |
4ef0d4d0 KS |
4878 | XSetForeground (dpy, gc, MaskForeground (f)); |
4879 | XDrawLine (dpy, img->mask, gc, 0, 0, | |
4880 | img->width - 1, img->height - 1); | |
4881 | XDrawLine (dpy, img->mask, gc, 0, img->height - 1, | |
4882 | img->width - 1, 0); | |
4883 | XFreeGC (dpy, gc); | |
4884 | } | |
edfda783 | 4885 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4886 | #else |
4887 | HDC hdc, bmpdc; | |
4888 | HGDIOBJ prev; | |
4889 | ||
4890 | hdc = get_frame_dc (f); | |
4891 | bmpdc = CreateCompatibleDC (hdc); | |
4892 | release_frame_dc (f, hdc); | |
4893 | ||
4894 | prev = SelectObject (bmpdc, img->pixmap); | |
4895 | ||
4896 | SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f)); | |
4897 | MoveToEx (bmpdc, 0, 0, NULL); | |
4898 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4899 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4900 | LineTo (bmpdc, img->width - 1, 0); | |
4901 | ||
4902 | if (img->mask) | |
4903 | { | |
4904 | SelectObject (bmpdc, img->mask); | |
4905 | SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f)); | |
4906 | MoveToEx (bmpdc, 0, 0, NULL); | |
4907 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4908 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4909 | LineTo (bmpdc, img->width - 1, 0); | |
4910 | } | |
4911 | SelectObject (bmpdc, prev); | |
4912 | DeleteDC (bmpdc); | |
4913 | #endif /* HAVE_NTGUI */ | |
4914 | } | |
4915 | } | |
4916 | ||
4917 | ||
4918 | /* Build a mask for image IMG which is used on frame F. FILE is the | |
4919 | name of an image file, for error messages. HOW determines how to | |
4920 | determine the background color of IMG. If it is a list '(R G B)', | |
4921 | with R, G, and B being integers >= 0, take that as the color of the | |
4922 | background. Otherwise, determine the background color of IMG | |
578098f3 | 4923 | heuristically. */ |
4ef0d4d0 | 4924 | |
578098f3 | 4925 | static void |
d3da34e0 | 4926 | x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) |
4ef0d4d0 KS |
4927 | { |
4928 | XImagePtr_or_DC ximg; | |
4929 | #ifndef HAVE_NTGUI | |
4930 | XImagePtr mask_img; | |
4931 | #else | |
4ef0d4d0 KS |
4932 | HGDIOBJ prev; |
4933 | char *mask_img; | |
4934 | int row_width; | |
4935 | #endif /* HAVE_NTGUI */ | |
578098f3 | 4936 | int x, y; |
f462f075 | 4937 | bool use_img_background; |
4ef0d4d0 KS |
4938 | unsigned long bg = 0; |
4939 | ||
4940 | if (img->mask) | |
547c9269 | 4941 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
4ef0d4d0 KS |
4942 | |
4943 | #ifndef HAVE_NTGUI | |
edfda783 | 4944 | #ifndef HAVE_NS |
4ef0d4d0 | 4945 | /* Create an image and pixmap serving as mask. */ |
f462f075 PE |
4946 | if (! image_create_x_image_and_pixmap (f, img, img->width, img->height, 1, |
4947 | &mask_img, 1)) | |
578098f3 | 4948 | return; |
edfda783 | 4949 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4950 | #else |
4951 | /* Create the bit array serving as mask. */ | |
4952 | row_width = (img->width + 7) / 8; | |
23f86fce | 4953 | mask_img = xzalloc (row_width * img->height); |
4ef0d4d0 KS |
4954 | #endif /* HAVE_NTGUI */ |
4955 | ||
547c9269 YM |
4956 | /* Get the X image or create a memory device context for IMG. */ |
4957 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); | |
4958 | ||
4ef0d4d0 KS |
4959 | /* Determine the background color of ximg. If HOW is `(R G B)' |
4960 | take that as color. Otherwise, use the image's background color. */ | |
4961 | use_img_background = 1; | |
4962 | ||
4963 | if (CONSP (how)) | |
4964 | { | |
4965 | int rgb[3], i; | |
4966 | ||
4967 | for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i) | |
4968 | { | |
4969 | rgb[i] = XFASTINT (XCAR (how)) & 0xffff; | |
4970 | how = XCDR (how); | |
4971 | } | |
4972 | ||
4973 | if (i == 3 && NILP (how)) | |
4974 | { | |
4975 | char color_name[30]; | |
4976 | sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]); | |
4977 | bg = ( | |
4978 | #ifdef HAVE_NTGUI | |
4979 | 0x00ffffff & /* Filter out palette info. */ | |
4980 | #endif /* HAVE_NTGUI */ | |
4981 | x_alloc_image_color (f, img, build_string (color_name), 0)); | |
4982 | use_img_background = 0; | |
4983 | } | |
4984 | } | |
4985 | ||
4986 | if (use_img_background) | |
6ac3c7bb | 4987 | bg = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
4988 | |
4989 | /* Set all bits in mask_img to 1 whose color in ximg is different | |
4990 | from the background color bg. */ | |
4991 | #ifndef HAVE_NTGUI | |
4992 | for (y = 0; y < img->height; ++y) | |
4993 | for (x = 0; x < img->width; ++x) | |
edfda783 | 4994 | #ifndef HAVE_NS |
4ef0d4d0 | 4995 | XPutPixel (mask_img, x, y, (XGetPixel (ximg, x, y) != bg |
9ecf6403 | 4996 | ? PIX_MASK_DRAW : PIX_MASK_RETAIN)); |
edfda783 AR |
4997 | #else |
4998 | if (XGetPixel (ximg, x, y) == bg) | |
ed3751c8 | 4999 | ns_set_alpha (ximg, x, y, 0); |
edfda783 AR |
5000 | #endif /* HAVE_NS */ |
5001 | #ifndef HAVE_NS | |
4ef0d4d0 KS |
5002 | /* Fill in the background_transparent field while we have the mask handy. */ |
5003 | image_background_transparent (img, f, mask_img); | |
5004 | ||
547c9269 YM |
5005 | /* Put mask_img into the image. */ |
5006 | image_put_x_image (f, img, mask_img, 1); | |
edfda783 | 5007 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
5008 | #else |
5009 | for (y = 0; y < img->height; ++y) | |
5010 | for (x = 0; x < img->width; ++x) | |
5011 | { | |
5012 | COLORREF p = GetPixel (ximg, x, y); | |
5013 | if (p != bg) | |
5014 | mask_img[y * row_width + x / 8] |= 1 << (x % 8); | |
5015 | } | |
5016 | ||
5017 | /* Create the mask image. */ | |
5018 | img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height, | |
5019 | mask_img); | |
5020 | /* Fill in the background_transparent field while we have the mask handy. */ | |
5021 | SelectObject (ximg, img->mask); | |
5022 | image_background_transparent (img, f, ximg); | |
5023 | ||
5024 | /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */ | |
5025 | xfree (mask_img); | |
5026 | #endif /* HAVE_NTGUI */ | |
5027 | ||
547c9269 | 5028 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
4ef0d4d0 KS |
5029 | } |
5030 | ||
5031 | \f | |
5032 | /*********************************************************************** | |
5033 | PBM (mono, gray, color) | |
5034 | ***********************************************************************/ | |
5035 | ||
578098f3 PE |
5036 | static bool pbm_image_p (Lisp_Object object); |
5037 | static bool pbm_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
5038 | |
5039 | /* The symbol `pbm' identifying images of this type. */ | |
5040 | ||
955cbe7b | 5041 | static Lisp_Object Qpbm; |
4ef0d4d0 KS |
5042 | |
5043 | /* Indices of image specification fields in gs_format, below. */ | |
5044 | ||
5045 | enum pbm_keyword_index | |
5046 | { | |
5047 | PBM_TYPE, | |
5048 | PBM_FILE, | |
5049 | PBM_DATA, | |
5050 | PBM_ASCENT, | |
5051 | PBM_MARGIN, | |
5052 | PBM_RELIEF, | |
5053 | PBM_ALGORITHM, | |
5054 | PBM_HEURISTIC_MASK, | |
5055 | PBM_MASK, | |
5056 | PBM_FOREGROUND, | |
5057 | PBM_BACKGROUND, | |
5058 | PBM_LAST | |
5059 | }; | |
5060 | ||
5061 | /* Vector of image_keyword structures describing the format | |
5062 | of valid user-defined image specifications. */ | |
5063 | ||
91433552 | 5064 | static const struct image_keyword pbm_format[PBM_LAST] = |
4ef0d4d0 KS |
5065 | { |
5066 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5067 | {":file", IMAGE_STRING_VALUE, 0}, | |
5068 | {":data", IMAGE_STRING_VALUE, 0}, | |
5069 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 5070 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
5071 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
5072 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5073 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5074 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5075 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
5076 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5077 | }; | |
5078 | ||
5079 | /* Structure describing the image type `pbm'. */ | |
5080 | ||
5081 | static struct image_type pbm_type = | |
5082 | { | |
5083 | &Qpbm, | |
5084 | pbm_image_p, | |
5085 | pbm_load, | |
5086 | x_clear_image, | |
bb4d86b4 | 5087 | NULL, |
4ef0d4d0 KS |
5088 | NULL |
5089 | }; | |
5090 | ||
5091 | ||
578098f3 | 5092 | /* Return true if OBJECT is a valid PBM image specification. */ |
4ef0d4d0 | 5093 | |
578098f3 | 5094 | static bool |
d3da34e0 | 5095 | pbm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
5096 | { |
5097 | struct image_keyword fmt[PBM_LAST]; | |
5098 | ||
72af86bd | 5099 | memcpy (fmt, pbm_format, sizeof fmt); |
4ef0d4d0 KS |
5100 | |
5101 | if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)) | |
5102 | return 0; | |
5103 | ||
5104 | /* Must specify either :data or :file. */ | |
5105 | return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1; | |
5106 | } | |
5107 | ||
5108 | ||
0e7690de CB |
5109 | /* Get next char skipping comments in Netpbm header. Returns -1 at |
5110 | end of input. */ | |
5111 | ||
5112 | static int | |
5113 | pbm_next_char (unsigned char **s, unsigned char *end) | |
5114 | { | |
5115 | int c = -1; | |
5116 | ||
5117 | while (*s < end && (c = *(*s)++, c == '#')) | |
5118 | { | |
5119 | /* Skip to the next line break. */ | |
5120 | while (*s < end && (c = *(*s)++, c != '\n' && c != '\r')) | |
5121 | ; | |
5122 | ||
5123 | c = -1; | |
5124 | } | |
5125 | ||
5126 | return c; | |
5127 | } | |
5128 | ||
5129 | ||
4ef0d4d0 KS |
5130 | /* Scan a decimal number from *S and return it. Advance *S while |
5131 | reading the number. END is the end of the string. Value is -1 at | |
5132 | end of input. */ | |
5133 | ||
5134 | static int | |
d3da34e0 | 5135 | pbm_scan_number (unsigned char **s, unsigned char *end) |
4ef0d4d0 KS |
5136 | { |
5137 | int c = 0, val = -1; | |
5138 | ||
0e7690de CB |
5139 | /* Skip white-space. */ |
5140 | while ((c = pbm_next_char (s, end)) != -1 && c_isspace (c)) | |
5141 | ; | |
4ef0d4d0 | 5142 | |
0e7690de CB |
5143 | if (c_isdigit (c)) |
5144 | { | |
5145 | /* Read decimal number. */ | |
5146 | val = c - '0'; | |
5147 | while ((c = pbm_next_char (s, end)) != -1 && c_isdigit (c)) | |
5148 | val = 10 * val + c - '0'; | |
4ef0d4d0 KS |
5149 | } |
5150 | ||
5151 | return val; | |
5152 | } | |
5153 | ||
5154 | ||
4ef0d4d0 KS |
5155 | /* Load PBM image IMG for use on frame F. */ |
5156 | ||
578098f3 | 5157 | static bool |
d3da34e0 | 5158 | pbm_load (struct frame *f, struct image *img) |
4ef0d4d0 | 5159 | { |
578098f3 PE |
5160 | bool raw_p; |
5161 | int x, y; | |
4ef0d4d0 KS |
5162 | int width, height, max_color_idx = 0; |
5163 | XImagePtr ximg; | |
5164 | Lisp_Object file, specified_file; | |
5165 | enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type; | |
4ef0d4d0 KS |
5166 | unsigned char *contents = NULL; |
5167 | unsigned char *end, *p; | |
dd52fcea | 5168 | ptrdiff_t size; |
4ef0d4d0 KS |
5169 | |
5170 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
4ef0d4d0 KS |
5171 | |
5172 | if (STRINGP (specified_file)) | |
5173 | { | |
5174 | file = x_find_image_file (specified_file); | |
5175 | if (!STRINGP (file)) | |
5176 | { | |
5177 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
5178 | return 0; |
5179 | } | |
5180 | ||
42a5b22f | 5181 | contents = slurp_file (SSDATA (file), &size); |
4ef0d4d0 KS |
5182 | if (contents == NULL) |
5183 | { | |
5184 | image_error ("Error reading `%s'", file, Qnil); | |
4ef0d4d0 KS |
5185 | return 0; |
5186 | } | |
5187 | ||
5188 | p = contents; | |
5189 | end = contents + size; | |
5190 | } | |
5191 | else | |
5192 | { | |
5193 | Lisp_Object data; | |
5194 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
5195 | if (!STRINGP (data)) |
5196 | { | |
5197 | image_error ("Invalid image data `%s'", data, Qnil); | |
5198 | return 0; | |
5199 | } | |
4ef0d4d0 KS |
5200 | p = SDATA (data); |
5201 | end = p + SBYTES (data); | |
5202 | } | |
5203 | ||
5204 | /* Check magic number. */ | |
5205 | if (end - p < 2 || *p++ != 'P') | |
5206 | { | |
5207 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5208 | error: | |
5209 | xfree (contents); | |
4ef0d4d0 KS |
5210 | return 0; |
5211 | } | |
5212 | ||
5213 | switch (*p++) | |
5214 | { | |
5215 | case '1': | |
5216 | raw_p = 0, type = PBM_MONO; | |
5217 | break; | |
5218 | ||
5219 | case '2': | |
5220 | raw_p = 0, type = PBM_GRAY; | |
5221 | break; | |
5222 | ||
5223 | case '3': | |
5224 | raw_p = 0, type = PBM_COLOR; | |
5225 | break; | |
5226 | ||
5227 | case '4': | |
5228 | raw_p = 1, type = PBM_MONO; | |
5229 | break; | |
5230 | ||
5231 | case '5': | |
5232 | raw_p = 1, type = PBM_GRAY; | |
5233 | break; | |
5234 | ||
5235 | case '6': | |
5236 | raw_p = 1, type = PBM_COLOR; | |
5237 | break; | |
5238 | ||
5239 | default: | |
5240 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5241 | goto error; | |
5242 | } | |
5243 | ||
5244 | /* Read width, height, maximum color-component. Characters | |
5245 | starting with `#' up to the end of a line are ignored. */ | |
5246 | width = pbm_scan_number (&p, end); | |
5247 | height = pbm_scan_number (&p, end); | |
5248 | ||
5249 | if (type != PBM_MONO) | |
5250 | { | |
5251 | max_color_idx = pbm_scan_number (&p, end); | |
b9c89e11 JR |
5252 | if (max_color_idx > 65535 || max_color_idx < 0) |
5253 | { | |
5254 | image_error ("Unsupported maximum PBM color value", Qnil, Qnil); | |
5255 | goto error; | |
5256 | } | |
4ef0d4d0 KS |
5257 | } |
5258 | ||
b9c89e11 JR |
5259 | if (!check_image_size (f, width, height)) |
5260 | { | |
10ea2b82 | 5261 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
b9c89e11 JR |
5262 | goto error; |
5263 | } | |
4ef0d4d0 | 5264 | |
547c9269 | 5265 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
4ef0d4d0 KS |
5266 | goto error; |
5267 | ||
5268 | /* Initialize the color hash table. */ | |
5269 | init_color_table (); | |
5270 | ||
5271 | if (type == PBM_MONO) | |
5272 | { | |
5273 | int c = 0, g; | |
5274 | struct image_keyword fmt[PBM_LAST]; | |
5275 | unsigned long fg = FRAME_FOREGROUND_PIXEL (f); | |
5276 | unsigned long bg = FRAME_BACKGROUND_PIXEL (f); | |
5277 | ||
5278 | /* Parse the image specification. */ | |
72af86bd | 5279 | memcpy (fmt, pbm_format, sizeof fmt); |
4ef0d4d0 KS |
5280 | parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm); |
5281 | ||
5282 | /* Get foreground and background colors, maybe allocate colors. */ | |
5283 | if (fmt[PBM_FOREGROUND].count | |
5284 | && STRINGP (fmt[PBM_FOREGROUND].value)) | |
5285 | fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg); | |
5286 | if (fmt[PBM_BACKGROUND].count | |
5287 | && STRINGP (fmt[PBM_BACKGROUND].value)) | |
5288 | { | |
5289 | bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg); | |
5290 | img->background = bg; | |
5291 | img->background_valid = 1; | |
5292 | } | |
5293 | ||
5294 | for (y = 0; y < height; ++y) | |
5295 | for (x = 0; x < width; ++x) | |
5296 | { | |
5297 | if (raw_p) | |
5298 | { | |
5299 | if ((x & 7) == 0) | |
c295e710 CY |
5300 | { |
5301 | if (p >= end) | |
5302 | { | |
5303 | x_destroy_x_image (ximg); | |
5304 | x_clear_image (f, img); | |
5305 | image_error ("Invalid image size in image `%s'", | |
5306 | img->spec, Qnil); | |
5307 | goto error; | |
5308 | } | |
5309 | c = *p++; | |
5310 | } | |
4ef0d4d0 KS |
5311 | g = c & 0x80; |
5312 | c <<= 1; | |
5313 | } | |
5314 | else | |
5315 | g = pbm_scan_number (&p, end); | |
5316 | ||
5317 | XPutPixel (ximg, x, y, g ? fg : bg); | |
5318 | } | |
5319 | } | |
5320 | else | |
5321 | { | |
b9c89e11 JR |
5322 | int expected_size = height * width; |
5323 | if (max_color_idx > 255) | |
5324 | expected_size *= 2; | |
5325 | if (type == PBM_COLOR) | |
5326 | expected_size *= 3; | |
5327 | ||
5328 | if (raw_p && p + expected_size > end) | |
39e653ea CY |
5329 | { |
5330 | x_destroy_x_image (ximg); | |
fb0b0862 | 5331 | x_clear_image (f, img); |
39e653ea CY |
5332 | image_error ("Invalid image size in image `%s'", |
5333 | img->spec, Qnil); | |
5334 | goto error; | |
5335 | } | |
5336 | ||
4ef0d4d0 KS |
5337 | for (y = 0; y < height; ++y) |
5338 | for (x = 0; x < width; ++x) | |
5339 | { | |
5340 | int r, g, b; | |
5341 | ||
b9c89e11 JR |
5342 | if (type == PBM_GRAY && raw_p) |
5343 | { | |
5344 | r = g = b = *p++; | |
5345 | if (max_color_idx > 255) | |
5346 | r = g = b = r * 256 + *p++; | |
5347 | } | |
5348 | else if (type == PBM_GRAY) | |
5349 | r = g = b = pbm_scan_number (&p, end); | |
4ef0d4d0 KS |
5350 | else if (raw_p) |
5351 | { | |
5352 | r = *p++; | |
b9c89e11 JR |
5353 | if (max_color_idx > 255) |
5354 | r = r * 256 + *p++; | |
4ef0d4d0 | 5355 | g = *p++; |
b9c89e11 JR |
5356 | if (max_color_idx > 255) |
5357 | g = g * 256 + *p++; | |
4ef0d4d0 | 5358 | b = *p++; |
b9c89e11 JR |
5359 | if (max_color_idx > 255) |
5360 | b = b * 256 + *p++; | |
4ef0d4d0 KS |
5361 | } |
5362 | else | |
5363 | { | |
5364 | r = pbm_scan_number (&p, end); | |
5365 | g = pbm_scan_number (&p, end); | |
5366 | b = pbm_scan_number (&p, end); | |
5367 | } | |
5368 | ||
5369 | if (r < 0 || g < 0 || b < 0) | |
5370 | { | |
5371 | x_destroy_x_image (ximg); | |
5372 | image_error ("Invalid pixel value in image `%s'", | |
5373 | img->spec, Qnil); | |
5374 | goto error; | |
5375 | } | |
5376 | ||
5377 | /* RGB values are now in the range 0..max_color_idx. | |
5378 | Scale this to the range 0..0xffff supported by X. */ | |
5379 | r = (double) r * 65535 / max_color_idx; | |
5380 | g = (double) g * 65535 / max_color_idx; | |
5381 | b = (double) b * 65535 / max_color_idx; | |
5382 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
5383 | } | |
5384 | } | |
5385 | ||
5386 | #ifdef COLOR_TABLE_SUPPORT | |
5387 | /* Store in IMG->colors the colors allocated for the image, and | |
5388 | free the color table. */ | |
5389 | img->colors = colors_in_color_table (&img->ncolors); | |
5390 | free_color_table (); | |
5391 | #endif /* COLOR_TABLE_SUPPORT */ | |
5392 | ||
5393 | img->width = width; | |
5394 | img->height = height; | |
5395 | ||
5396 | /* Maybe fill in the background field while we have ximg handy. */ | |
5397 | ||
5398 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
5399 | /* Casting avoids a GCC warning. */ |
5400 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 5401 | |
547c9269 YM |
5402 | /* Put ximg into the image. */ |
5403 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
5404 | |
5405 | /* X and W32 versions did it here, MAC version above. ++kfs | |
1f026899 | 5406 | img->width = width; |
4ef0d4d0 KS |
5407 | img->height = height; */ |
5408 | ||
4ef0d4d0 KS |
5409 | xfree (contents); |
5410 | return 1; | |
5411 | } | |
5412 | ||
5413 | \f | |
5414 | /*********************************************************************** | |
5415 | PNG | |
5416 | ***********************************************************************/ | |
5417 | ||
9e2a2647 | 5418 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
4ef0d4d0 KS |
5419 | |
5420 | /* Function prototypes. */ | |
5421 | ||
578098f3 PE |
5422 | static bool png_image_p (Lisp_Object object); |
5423 | static bool png_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
5424 | |
5425 | /* The symbol `png' identifying images of this type. */ | |
5426 | ||
955cbe7b | 5427 | static Lisp_Object Qpng; |
4ef0d4d0 KS |
5428 | |
5429 | /* Indices of image specification fields in png_format, below. */ | |
5430 | ||
5431 | enum png_keyword_index | |
5432 | { | |
5433 | PNG_TYPE, | |
5434 | PNG_DATA, | |
5435 | PNG_FILE, | |
5436 | PNG_ASCENT, | |
5437 | PNG_MARGIN, | |
5438 | PNG_RELIEF, | |
5439 | PNG_ALGORITHM, | |
5440 | PNG_HEURISTIC_MASK, | |
5441 | PNG_MASK, | |
5442 | PNG_BACKGROUND, | |
5443 | PNG_LAST | |
5444 | }; | |
5445 | ||
5446 | /* Vector of image_keyword structures describing the format | |
5447 | of valid user-defined image specifications. */ | |
5448 | ||
91433552 | 5449 | static const struct image_keyword png_format[PNG_LAST] = |
4ef0d4d0 KS |
5450 | { |
5451 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5452 | {":data", IMAGE_STRING_VALUE, 0}, | |
5453 | {":file", IMAGE_STRING_VALUE, 0}, | |
5454 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 5455 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
5456 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
5457 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5458 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5459 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5460 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5461 | }; | |
5462 | ||
a9e7a9d5 | 5463 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 5464 | static bool init_png_functions (void); |
bb4d86b4 CY |
5465 | #else |
5466 | #define init_png_functions NULL | |
5467 | #endif | |
5468 | ||
4ef0d4d0 KS |
5469 | /* Structure describing the image type `png'. */ |
5470 | ||
5471 | static struct image_type png_type = | |
5472 | { | |
5473 | &Qpng, | |
5474 | png_image_p, | |
5475 | png_load, | |
5476 | x_clear_image, | |
bb4d86b4 | 5477 | init_png_functions, |
4ef0d4d0 KS |
5478 | NULL |
5479 | }; | |
5480 | ||
578098f3 | 5481 | /* Return true if OBJECT is a valid PNG image specification. */ |
4ef0d4d0 | 5482 | |
578098f3 | 5483 | static bool |
d3da34e0 | 5484 | png_image_p (Lisp_Object object) |
4ef0d4d0 KS |
5485 | { |
5486 | struct image_keyword fmt[PNG_LAST]; | |
72af86bd | 5487 | memcpy (fmt, png_format, sizeof fmt); |
4ef0d4d0 KS |
5488 | |
5489 | if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)) | |
5490 | return 0; | |
5491 | ||
5492 | /* Must specify either the :data or :file keyword. */ | |
5493 | return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1; | |
5494 | } | |
5495 | ||
9e2a2647 | 5496 | #endif /* HAVE_PNG || HAVE_NS */ |
4ef0d4d0 KS |
5497 | |
5498 | ||
5499 | #ifdef HAVE_PNG | |
5500 | ||
0fda9b75 | 5501 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
5502 | /* PNG library details. */ |
5503 | ||
14beddf4 CY |
5504 | DEF_IMGLIB_FN (png_voidp, png_get_io_ptr, (png_structp)); |
5505 | DEF_IMGLIB_FN (int, png_sig_cmp, (png_bytep, png_size_t, png_size_t)); | |
5506 | DEF_IMGLIB_FN (png_structp, png_create_read_struct, (png_const_charp, png_voidp, | |
5507 | png_error_ptr, png_error_ptr)); | |
5508 | DEF_IMGLIB_FN (png_infop, png_create_info_struct, (png_structp)); | |
5509 | DEF_IMGLIB_FN (void, png_destroy_read_struct, (png_structpp, png_infopp, png_infopp)); | |
5510 | DEF_IMGLIB_FN (void, png_set_read_fn, (png_structp, png_voidp, png_rw_ptr)); | |
5511 | DEF_IMGLIB_FN (void, png_set_sig_bytes, (png_structp, int)); | |
5512 | DEF_IMGLIB_FN (void, png_read_info, (png_structp, png_infop)); | |
5513 | DEF_IMGLIB_FN (png_uint_32, png_get_IHDR, (png_structp, png_infop, | |
850690cc JB |
5514 | png_uint_32 *, png_uint_32 *, |
5515 | int *, int *, int *, int *, int *)); | |
14beddf4 CY |
5516 | DEF_IMGLIB_FN (png_uint_32, png_get_valid, (png_structp, png_infop, png_uint_32)); |
5517 | DEF_IMGLIB_FN (void, png_set_strip_16, (png_structp)); | |
5518 | DEF_IMGLIB_FN (void, png_set_expand, (png_structp)); | |
5519 | DEF_IMGLIB_FN (void, png_set_gray_to_rgb, (png_structp)); | |
5520 | DEF_IMGLIB_FN (void, png_set_background, (png_structp, png_color_16p, | |
850690cc | 5521 | int, int, double)); |
14beddf4 CY |
5522 | DEF_IMGLIB_FN (png_uint_32, png_get_bKGD, (png_structp, png_infop, png_color_16p *)); |
5523 | DEF_IMGLIB_FN (void, png_read_update_info, (png_structp, png_infop)); | |
5524 | DEF_IMGLIB_FN (png_byte, png_get_channels, (png_structp, png_infop)); | |
5525 | DEF_IMGLIB_FN (png_size_t, png_get_rowbytes, (png_structp, png_infop)); | |
5526 | DEF_IMGLIB_FN (void, png_read_image, (png_structp, png_bytepp)); | |
5527 | DEF_IMGLIB_FN (void, png_read_end, (png_structp, png_infop)); | |
5528 | DEF_IMGLIB_FN (void, png_error, (png_structp, png_const_charp)); | |
4ef0d4d0 | 5529 | |
7f9c5df9 | 5530 | #if (PNG_LIBPNG_VER >= 10500) |
14beddf4 CY |
5531 | DEF_IMGLIB_FN (void, png_longjmp, (png_structp, int)); |
5532 | DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn, (png_structp, png_longjmp_ptr, size_t)); | |
7f9c5df9 | 5533 | #endif /* libpng version >= 1.5 */ |
4ef0d4d0 | 5534 | |
578098f3 | 5535 | static bool |
d07ff9db | 5536 | init_png_functions (void) |
4ef0d4d0 KS |
5537 | { |
5538 | HMODULE library; | |
5539 | ||
d07ff9db | 5540 | if (!(library = w32_delayed_load (Qpng))) |
4ef0d4d0 KS |
5541 | return 0; |
5542 | ||
5543 | LOAD_IMGLIB_FN (library, png_get_io_ptr); | |
285d07e2 | 5544 | LOAD_IMGLIB_FN (library, png_sig_cmp); |
4ef0d4d0 KS |
5545 | LOAD_IMGLIB_FN (library, png_create_read_struct); |
5546 | LOAD_IMGLIB_FN (library, png_create_info_struct); | |
5547 | LOAD_IMGLIB_FN (library, png_destroy_read_struct); | |
5548 | LOAD_IMGLIB_FN (library, png_set_read_fn); | |
4ef0d4d0 KS |
5549 | LOAD_IMGLIB_FN (library, png_set_sig_bytes); |
5550 | LOAD_IMGLIB_FN (library, png_read_info); | |
5551 | LOAD_IMGLIB_FN (library, png_get_IHDR); | |
5552 | LOAD_IMGLIB_FN (library, png_get_valid); | |
5553 | LOAD_IMGLIB_FN (library, png_set_strip_16); | |
5554 | LOAD_IMGLIB_FN (library, png_set_expand); | |
5555 | LOAD_IMGLIB_FN (library, png_set_gray_to_rgb); | |
5556 | LOAD_IMGLIB_FN (library, png_set_background); | |
5557 | LOAD_IMGLIB_FN (library, png_get_bKGD); | |
5558 | LOAD_IMGLIB_FN (library, png_read_update_info); | |
5559 | LOAD_IMGLIB_FN (library, png_get_channels); | |
5560 | LOAD_IMGLIB_FN (library, png_get_rowbytes); | |
5561 | LOAD_IMGLIB_FN (library, png_read_image); | |
5562 | LOAD_IMGLIB_FN (library, png_read_end); | |
5563 | LOAD_IMGLIB_FN (library, png_error); | |
7f9c5df9 CY |
5564 | |
5565 | #if (PNG_LIBPNG_VER >= 10500) | |
5566 | LOAD_IMGLIB_FN (library, png_longjmp); | |
5567 | LOAD_IMGLIB_FN (library, png_set_longjmp_fn); | |
5568 | #endif /* libpng version >= 1.5 */ | |
5569 | ||
4ef0d4d0 KS |
5570 | return 1; |
5571 | } | |
5572 | #else | |
5573 | ||
5574 | #define fn_png_get_io_ptr png_get_io_ptr | |
285d07e2 | 5575 | #define fn_png_sig_cmp png_sig_cmp |
4ef0d4d0 KS |
5576 | #define fn_png_create_read_struct png_create_read_struct |
5577 | #define fn_png_create_info_struct png_create_info_struct | |
5578 | #define fn_png_destroy_read_struct png_destroy_read_struct | |
5579 | #define fn_png_set_read_fn png_set_read_fn | |
4ef0d4d0 KS |
5580 | #define fn_png_set_sig_bytes png_set_sig_bytes |
5581 | #define fn_png_read_info png_read_info | |
5582 | #define fn_png_get_IHDR png_get_IHDR | |
5583 | #define fn_png_get_valid png_get_valid | |
5584 | #define fn_png_set_strip_16 png_set_strip_16 | |
5585 | #define fn_png_set_expand png_set_expand | |
5586 | #define fn_png_set_gray_to_rgb png_set_gray_to_rgb | |
5587 | #define fn_png_set_background png_set_background | |
5588 | #define fn_png_get_bKGD png_get_bKGD | |
5589 | #define fn_png_read_update_info png_read_update_info | |
5590 | #define fn_png_get_channels png_get_channels | |
5591 | #define fn_png_get_rowbytes png_get_rowbytes | |
5592 | #define fn_png_read_image png_read_image | |
5593 | #define fn_png_read_end png_read_end | |
5594 | #define fn_png_error png_error | |
5595 | ||
7f9c5df9 CY |
5596 | #if (PNG_LIBPNG_VER >= 10500) |
5597 | #define fn_png_longjmp png_longjmp | |
5598 | #define fn_png_set_longjmp_fn png_set_longjmp_fn | |
5599 | #endif /* libpng version >= 1.5 */ | |
5600 | ||
0fda9b75 | 5601 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 5602 | |
0328b6de PE |
5603 | /* Possibly inefficient/inexact substitutes for _setjmp and _longjmp. |
5604 | Do not use sys_setjmp, as PNG supports only jmp_buf. The _longjmp | |
7105c8cb EZ |
5605 | substitute may munge the signal mask, but that should be OK here. |
5606 | MinGW (MS-Windows) uses _setjmp and defines setjmp to _setjmp in | |
5607 | the system header setjmp.h; don't mess up that. */ | |
0328b6de | 5608 | #ifndef HAVE__SETJMP |
39a57ad0 | 5609 | # define _setjmp(j) setjmp (j) |
0328b6de PE |
5610 | # define _longjmp longjmp |
5611 | #endif | |
7f9c5df9 CY |
5612 | |
5613 | #if (PNG_LIBPNG_VER < 10500) | |
af26b72c | 5614 | #define PNG_LONGJMP(ptr) (_longjmp ((ptr)->jmpbuf, 1)) |
7f9c5df9 CY |
5615 | #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf) |
5616 | #else | |
df61c790 | 5617 | /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */ |
c2e79cb4 | 5618 | #define PNG_LONGJMP(ptr) (fn_png_longjmp ((ptr), 1)) |
7f9c5df9 | 5619 | #define PNG_JMPBUF(ptr) \ |
af26b72c | 5620 | (*fn_png_set_longjmp_fn ((ptr), _longjmp, sizeof (jmp_buf))) |
7f9c5df9 CY |
5621 | #endif |
5622 | ||
4ef0d4d0 KS |
5623 | /* Error and warning handlers installed when the PNG library |
5624 | is initialized. */ | |
5625 | ||
845ca893 | 5626 | static _Noreturn void |
d3da34e0 | 5627 | my_png_error (png_struct *png_ptr, const char *msg) |
4ef0d4d0 | 5628 | { |
a54e2c05 | 5629 | eassert (png_ptr != NULL); |
5be1c984 EZ |
5630 | /* Avoid compiler warning about deprecated direct access to |
5631 | png_ptr's fields in libpng versions 1.4.x. */ | |
4ef0d4d0 | 5632 | image_error ("PNG error: %s", build_string (msg), Qnil); |
7f9c5df9 | 5633 | PNG_LONGJMP (png_ptr); |
4ef0d4d0 KS |
5634 | } |
5635 | ||
5636 | ||
5637 | static void | |
d3da34e0 | 5638 | my_png_warning (png_struct *png_ptr, const char *msg) |
4ef0d4d0 | 5639 | { |
a54e2c05 | 5640 | eassert (png_ptr != NULL); |
4ef0d4d0 KS |
5641 | image_error ("PNG warning: %s", build_string (msg), Qnil); |
5642 | } | |
5643 | ||
5644 | /* Memory source for PNG decoding. */ | |
5645 | ||
5646 | struct png_memory_storage | |
5647 | { | |
5648 | unsigned char *bytes; /* The data */ | |
3f791afe PE |
5649 | ptrdiff_t len; /* How big is it? */ |
5650 | ptrdiff_t index; /* Where are we? */ | |
4ef0d4d0 KS |
5651 | }; |
5652 | ||
5653 | ||
5654 | /* Function set as reader function when reading PNG image from memory. | |
5655 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5656 | bytes from the input to DATA. */ | |
5657 | ||
5658 | static void | |
d3da34e0 | 5659 | png_read_from_memory (png_structp png_ptr, png_bytep data, png_size_t length) |
4ef0d4d0 | 5660 | { |
7d652d97 | 5661 | struct png_memory_storage *tbr = fn_png_get_io_ptr (png_ptr); |
4ef0d4d0 KS |
5662 | |
5663 | if (length > tbr->len - tbr->index) | |
5664 | fn_png_error (png_ptr, "Read error"); | |
5665 | ||
72af86bd | 5666 | memcpy (data, tbr->bytes + tbr->index, length); |
4ef0d4d0 KS |
5667 | tbr->index = tbr->index + length; |
5668 | } | |
5669 | ||
b0da69a7 | 5670 | |
9a22a004 JB |
5671 | /* Function set as reader function when reading PNG image from a file. |
5672 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5673 | bytes from the input to DATA. */ | |
5674 | ||
5675 | static void | |
d3da34e0 | 5676 | png_read_from_file (png_structp png_ptr, png_bytep data, png_size_t length) |
9a22a004 | 5677 | { |
7d652d97 | 5678 | FILE *fp = fn_png_get_io_ptr (png_ptr); |
9a22a004 JB |
5679 | |
5680 | if (fread (data, 1, length, fp) < length) | |
5681 | fn_png_error (png_ptr, "Read error"); | |
5682 | } | |
5683 | ||
5684 | ||
578098f3 | 5685 | /* Load PNG image IMG for use on frame F. Value is true if |
4ef0d4d0 KS |
5686 | successful. */ |
5687 | ||
40bce90b PE |
5688 | struct png_load_context |
5689 | { | |
0328b6de | 5690 | /* These are members so that longjmp doesn't munge local variables. */ |
40bce90b PE |
5691 | png_struct *png_ptr; |
5692 | png_info *info_ptr; | |
5693 | png_info *end_info; | |
5694 | FILE *fp; | |
5695 | png_byte *pixels; | |
5696 | png_byte **rows; | |
5697 | }; | |
5698 | ||
578098f3 | 5699 | static bool |
40bce90b | 5700 | png_load_body (struct frame *f, struct image *img, struct png_load_context *c) |
4ef0d4d0 KS |
5701 | { |
5702 | Lisp_Object file, specified_file; | |
5703 | Lisp_Object specified_data; | |
5adf60bc PE |
5704 | int x, y; |
5705 | ptrdiff_t i; | |
4ef0d4d0 | 5706 | XImagePtr ximg, mask_img = NULL; |
40bce90b | 5707 | png_struct *png_ptr; |
4ef0d4d0 | 5708 | png_info *info_ptr = NULL, *end_info = NULL; |
40bce90b | 5709 | FILE *fp = NULL; |
4ef0d4d0 | 5710 | png_byte sig[8]; |
40bce90b PE |
5711 | png_byte *pixels = NULL; |
5712 | png_byte **rows = NULL; | |
4ef0d4d0 KS |
5713 | png_uint_32 width, height; |
5714 | int bit_depth, color_type, interlace_type; | |
5715 | png_byte channels; | |
5716 | png_uint_32 row_bytes; | |
578098f3 | 5717 | bool transparent_p; |
4ef0d4d0 KS |
5718 | struct png_memory_storage tbr; /* Data to be read */ |
5719 | ||
5720 | /* Find out what file to load. */ | |
5721 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
5722 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
4ef0d4d0 KS |
5723 | |
5724 | if (NILP (specified_data)) | |
5725 | { | |
5726 | file = x_find_image_file (specified_file); | |
5727 | if (!STRINGP (file)) | |
5728 | { | |
5729 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
5730 | return 0; |
5731 | } | |
5732 | ||
5733 | /* Open the image file. */ | |
406af475 | 5734 | fp = emacs_fopen (SSDATA (file), "rb"); |
4ef0d4d0 KS |
5735 | if (!fp) |
5736 | { | |
5737 | image_error ("Cannot open image file `%s'", file, Qnil); | |
4ef0d4d0 KS |
5738 | return 0; |
5739 | } | |
5740 | ||
5741 | /* Check PNG signature. */ | |
5742 | if (fread (sig, 1, sizeof sig, fp) != sizeof sig | |
285d07e2 | 5743 | || fn_png_sig_cmp (sig, 0, sizeof sig)) |
4ef0d4d0 | 5744 | { |
4ef0d4d0 | 5745 | fclose (fp); |
3f5bef16 | 5746 | image_error ("Not a PNG file: `%s'", file, Qnil); |
4ef0d4d0 KS |
5747 | return 0; |
5748 | } | |
5749 | } | |
5750 | else | |
5751 | { | |
7574650a AS |
5752 | if (!STRINGP (specified_data)) |
5753 | { | |
5754 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
5755 | return 0; | |
5756 | } | |
5757 | ||
4ef0d4d0 KS |
5758 | /* Read from memory. */ |
5759 | tbr.bytes = SDATA (specified_data); | |
5760 | tbr.len = SBYTES (specified_data); | |
5761 | tbr.index = 0; | |
5762 | ||
5763 | /* Check PNG signature. */ | |
5764 | if (tbr.len < sizeof sig | |
285d07e2 | 5765 | || fn_png_sig_cmp (tbr.bytes, 0, sizeof sig)) |
4ef0d4d0 KS |
5766 | { |
5767 | image_error ("Not a PNG image: `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
5768 | return 0; |
5769 | } | |
5770 | ||
5771 | /* Need to skip past the signature. */ | |
5772 | tbr.bytes += sizeof (sig); | |
5773 | } | |
5774 | ||
df61c790 AS |
5775 | /* Initialize read and info structs for PNG lib. */ |
5776 | png_ptr = fn_png_create_read_struct (PNG_LIBPNG_VER_STRING, | |
5777 | NULL, my_png_error, | |
5778 | my_png_warning); | |
40bce90b | 5779 | if (png_ptr) |
4ef0d4d0 | 5780 | { |
40bce90b PE |
5781 | info_ptr = fn_png_create_info_struct (png_ptr); |
5782 | end_info = fn_png_create_info_struct (png_ptr); | |
4ef0d4d0 KS |
5783 | } |
5784 | ||
40bce90b PE |
5785 | c->png_ptr = png_ptr; |
5786 | c->info_ptr = info_ptr; | |
5787 | c->end_info = end_info; | |
5788 | c->fp = fp; | |
5789 | c->pixels = pixels; | |
5790 | c->rows = rows; | |
5791 | ||
5792 | if (! (info_ptr && end_info)) | |
4ef0d4d0 | 5793 | { |
40bce90b PE |
5794 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); |
5795 | png_ptr = 0; | |
4ef0d4d0 | 5796 | } |
40bce90b | 5797 | if (! png_ptr) |
4ef0d4d0 | 5798 | { |
4ef0d4d0 | 5799 | if (fp) fclose (fp); |
4ef0d4d0 KS |
5800 | return 0; |
5801 | } | |
5802 | ||
5803 | /* Set error jump-back. We come back here when the PNG library | |
5804 | detects an error. */ | |
af26b72c | 5805 | if (_setjmp (PNG_JMPBUF (png_ptr))) |
4ef0d4d0 KS |
5806 | { |
5807 | error: | |
40bce90b PE |
5808 | if (c->png_ptr) |
5809 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); | |
5810 | xfree (c->pixels); | |
5811 | xfree (c->rows); | |
5812 | if (c->fp) | |
5813 | fclose (c->fp); | |
4ef0d4d0 KS |
5814 | return 0; |
5815 | } | |
5816 | ||
40bce90b PE |
5817 | /* Silence a bogus diagnostic; see GCC bug 54561. */ |
5818 | IF_LINT (fp = c->fp); | |
5819 | ||
4ef0d4d0 KS |
5820 | /* Read image info. */ |
5821 | if (!NILP (specified_data)) | |
7d652d97 | 5822 | fn_png_set_read_fn (png_ptr, &tbr, png_read_from_memory); |
4ef0d4d0 | 5823 | else |
7d652d97 | 5824 | fn_png_set_read_fn (png_ptr, fp, png_read_from_file); |
4ef0d4d0 KS |
5825 | |
5826 | fn_png_set_sig_bytes (png_ptr, sizeof sig); | |
5827 | fn_png_read_info (png_ptr, info_ptr); | |
5828 | fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, | |
5829 | &interlace_type, NULL, NULL); | |
5830 | ||
3f791afe PE |
5831 | if (! (width <= INT_MAX && height <= INT_MAX |
5832 | && check_image_size (f, width, height))) | |
10ea2b82 JR |
5833 | { |
5834 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
5835 | goto error; | |
5836 | } | |
ca4aa935 PE |
5837 | |
5838 | /* Create the X image and pixmap now, so that the work below can be | |
5839 | omitted if the image is too large for X. */ | |
547c9269 | 5840 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
ca4aa935 PE |
5841 | goto error; |
5842 | ||
4ef0d4d0 KS |
5843 | /* If image contains simply transparency data, we prefer to |
5844 | construct a clipping mask. */ | |
5845 | if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) | |
5846 | transparent_p = 1; | |
5847 | else | |
5848 | transparent_p = 0; | |
5849 | ||
5850 | /* This function is easier to write if we only have to handle | |
5851 | one data format: RGB or RGBA with 8 bits per channel. Let's | |
5852 | transform other formats into that format. */ | |
5853 | ||
5854 | /* Strip more than 8 bits per channel. */ | |
5855 | if (bit_depth == 16) | |
5856 | fn_png_set_strip_16 (png_ptr); | |
5857 | ||
5858 | /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel | |
5859 | if available. */ | |
5860 | fn_png_set_expand (png_ptr); | |
5861 | ||
5862 | /* Convert grayscale images to RGB. */ | |
5863 | if (color_type == PNG_COLOR_TYPE_GRAY | |
5864 | || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | |
5865 | fn_png_set_gray_to_rgb (png_ptr); | |
5866 | ||
4ef0d4d0 KS |
5867 | /* Handle alpha channel by combining the image with a background |
5868 | color. Do this only if a real alpha channel is supplied. For | |
5869 | simple transparency, we prefer a clipping mask. */ | |
5870 | if (!transparent_p) | |
5871 | { | |
917ac1a9 | 5872 | /* png_color_16 *image_bg; */ |
4ef0d4d0 KS |
5873 | Lisp_Object specified_bg |
5874 | = image_spec_value (img->spec, QCbackground, NULL); | |
c4f13f44 | 5875 | int shift = (bit_depth == 16) ? 0 : 8; |
4ef0d4d0 KS |
5876 | |
5877 | if (STRINGP (specified_bg)) | |
5878 | /* The user specified `:background', use that. */ | |
5879 | { | |
4ef0d4d0 | 5880 | XColor color; |
42a5b22f | 5881 | if (x_defined_color (f, SSDATA (specified_bg), &color, 0)) |
4ef0d4d0 KS |
5882 | { |
5883 | png_color_16 user_bg; | |
5884 | ||
72af86bd | 5885 | memset (&user_bg, 0, sizeof user_bg); |
c4f13f44 CY |
5886 | user_bg.red = color.red >> shift; |
5887 | user_bg.green = color.green >> shift; | |
5888 | user_bg.blue = color.blue >> shift; | |
4ef0d4d0 KS |
5889 | |
5890 | fn_png_set_background (png_ptr, &user_bg, | |
5891 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
5892 | } | |
5893 | } | |
4ef0d4d0 KS |
5894 | else |
5895 | { | |
c4f13f44 CY |
5896 | /* We use the current frame background, ignoring any default |
5897 | background color set by the image. */ | |
8b7d0a16 | 5898 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
4ef0d4d0 KS |
5899 | XColor color; |
5900 | png_color_16 frame_background; | |
5901 | ||
5902 | color.pixel = FRAME_BACKGROUND_PIXEL (f); | |
5903 | x_query_color (f, &color); | |
5904 | ||
72af86bd | 5905 | memset (&frame_background, 0, sizeof frame_background); |
c4f13f44 CY |
5906 | frame_background.red = color.red >> shift; |
5907 | frame_background.green = color.green >> shift; | |
5908 | frame_background.blue = color.blue >> shift; | |
4ef0d4d0 KS |
5909 | #endif /* HAVE_X_WINDOWS */ |
5910 | ||
4ef0d4d0 KS |
5911 | fn_png_set_background (png_ptr, &frame_background, |
5912 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
5913 | } | |
5914 | } | |
5915 | ||
5916 | /* Update info structure. */ | |
5917 | fn_png_read_update_info (png_ptr, info_ptr); | |
5918 | ||
5919 | /* Get number of channels. Valid values are 1 for grayscale images | |
5920 | and images with a palette, 2 for grayscale images with transparency | |
5921 | information (alpha channel), 3 for RGB images, and 4 for RGB | |
5922 | images with alpha channel, i.e. RGBA. If conversions above were | |
5923 | sufficient we should only have 3 or 4 channels here. */ | |
5924 | channels = fn_png_get_channels (png_ptr, info_ptr); | |
a54e2c05 | 5925 | eassert (channels == 3 || channels == 4); |
4ef0d4d0 KS |
5926 | |
5927 | /* Number of bytes needed for one row of the image. */ | |
5928 | row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr); | |
5929 | ||
5930 | /* Allocate memory for the image. */ | |
3f791afe PE |
5931 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *rows < height |
5932 | || min (PTRDIFF_MAX, SIZE_MAX) / sizeof *pixels / height < row_bytes) | |
5933 | memory_full (SIZE_MAX); | |
40bce90b PE |
5934 | c->pixels = pixels = xmalloc (sizeof *pixels * row_bytes * height); |
5935 | c->rows = rows = xmalloc (height * sizeof *rows); | |
4ef0d4d0 KS |
5936 | for (i = 0; i < height; ++i) |
5937 | rows[i] = pixels + i * row_bytes; | |
5938 | ||
5939 | /* Read the entire image. */ | |
5940 | fn_png_read_image (png_ptr, rows); | |
5941 | fn_png_read_end (png_ptr, info_ptr); | |
5942 | if (fp) | |
5943 | { | |
5944 | fclose (fp); | |
40bce90b | 5945 | c->fp = NULL; |
4ef0d4d0 KS |
5946 | } |
5947 | ||
4ef0d4d0 KS |
5948 | /* Create an image and pixmap serving as mask if the PNG image |
5949 | contains an alpha channel. */ | |
5950 | if (channels == 4 | |
5951 | && !transparent_p | |
547c9269 YM |
5952 | && !image_create_x_image_and_pixmap (f, img, width, height, 1, |
5953 | &mask_img, 1)) | |
4ef0d4d0 KS |
5954 | { |
5955 | x_destroy_x_image (ximg); | |
547c9269 | 5956 | x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP); |
4ef0d4d0 KS |
5957 | goto error; |
5958 | } | |
5959 | ||
5960 | /* Fill the X image and mask from PNG data. */ | |
5961 | init_color_table (); | |
5962 | ||
5963 | for (y = 0; y < height; ++y) | |
5964 | { | |
5965 | png_byte *p = rows[y]; | |
5966 | ||
5967 | for (x = 0; x < width; ++x) | |
5968 | { | |
13464394 | 5969 | int r, g, b; |
4ef0d4d0 KS |
5970 | |
5971 | r = *p++ << 8; | |
5972 | g = *p++ << 8; | |
5973 | b = *p++ << 8; | |
5974 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
5975 | /* An alpha channel, aka mask channel, associates variable | |
5976 | transparency with an image. Where other image formats | |
5977 | support binary transparency---fully transparent or fully | |
5978 | opaque---PNG allows up to 254 levels of partial transparency. | |
5979 | The PNG library implements partial transparency by combining | |
5980 | the image with a specified background color. | |
5981 | ||
5982 | I'm not sure how to handle this here nicely: because the | |
5983 | background on which the image is displayed may change, for | |
5984 | real alpha channel support, it would be necessary to create | |
5985 | a new image for each possible background. | |
5986 | ||
5987 | What I'm doing now is that a mask is created if we have | |
5988 | boolean transparency information. Otherwise I'm using | |
5989 | the frame's background color to combine the image with. */ | |
5990 | ||
5991 | if (channels == 4) | |
5992 | { | |
5993 | if (mask_img) | |
9ecf6403 | 5994 | XPutPixel (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN); |
4ef0d4d0 KS |
5995 | ++p; |
5996 | } | |
5997 | } | |
5998 | } | |
5999 | ||
6000 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
6001 | /* Set IMG's background color from the PNG image, unless the user | |
6002 | overrode it. */ | |
6003 | { | |
6004 | png_color_16 *bg; | |
6005 | if (fn_png_get_bKGD (png_ptr, info_ptr, &bg)) | |
6006 | { | |
6007 | img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue); | |
6008 | img->background_valid = 1; | |
6009 | } | |
6010 | } | |
6011 | ||
6012 | #ifdef COLOR_TABLE_SUPPORT | |
6013 | /* Remember colors allocated for this image. */ | |
6014 | img->colors = colors_in_color_table (&img->ncolors); | |
6015 | free_color_table (); | |
6016 | #endif /* COLOR_TABLE_SUPPORT */ | |
6017 | ||
6018 | /* Clean up. */ | |
40bce90b | 6019 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); |
4ef0d4d0 KS |
6020 | xfree (rows); |
6021 | xfree (pixels); | |
6022 | ||
6023 | img->width = width; | |
6024 | img->height = height; | |
6025 | ||
2e09fef1 EZ |
6026 | /* Maybe fill in the background field while we have ximg handy. |
6027 | Casting avoids a GCC warning. */ | |
6028 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 6029 | |
547c9269 YM |
6030 | /* Put ximg into the image. */ |
6031 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
6032 | |
6033 | /* Same for the mask. */ | |
6034 | if (mask_img) | |
6035 | { | |
2e09fef1 EZ |
6036 | /* Fill in the background_transparent field while we have the |
6037 | mask handy. Casting avoids a GCC warning. */ | |
6038 | image_background_transparent (img, f, (XImagePtr_or_DC)mask_img); | |
4ef0d4d0 | 6039 | |
547c9269 | 6040 | image_put_x_image (f, img, mask_img, 1); |
4ef0d4d0 KS |
6041 | } |
6042 | ||
4ef0d4d0 KS |
6043 | return 1; |
6044 | } | |
6045 | ||
578098f3 | 6046 | static bool |
40bce90b PE |
6047 | png_load (struct frame *f, struct image *img) |
6048 | { | |
6049 | struct png_load_context c; | |
6050 | return png_load_body (f, img, &c); | |
6051 | } | |
6052 | ||
4ef0d4d0 KS |
6053 | #else /* HAVE_PNG */ |
6054 | ||
edfda783 | 6055 | #ifdef HAVE_NS |
578098f3 | 6056 | static bool |
edfda783 AR |
6057 | png_load (struct frame *f, struct image *img) |
6058 | { | |
5e617bc2 JB |
6059 | return ns_load_image (f, img, |
6060 | image_spec_value (img->spec, QCfile, NULL), | |
6061 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
6062 | } |
6063 | #endif /* HAVE_NS */ | |
6064 | ||
6065 | ||
4ef0d4d0 KS |
6066 | #endif /* !HAVE_PNG */ |
6067 | ||
6068 | ||
6069 | \f | |
6070 | /*********************************************************************** | |
6071 | JPEG | |
6072 | ***********************************************************************/ | |
6073 | ||
9e2a2647 | 6074 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
4ef0d4d0 | 6075 | |
578098f3 PE |
6076 | static bool jpeg_image_p (Lisp_Object object); |
6077 | static bool jpeg_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
6078 | |
6079 | /* The symbol `jpeg' identifying images of this type. */ | |
6080 | ||
955cbe7b | 6081 | static Lisp_Object Qjpeg; |
4ef0d4d0 KS |
6082 | |
6083 | /* Indices of image specification fields in gs_format, below. */ | |
6084 | ||
6085 | enum jpeg_keyword_index | |
6086 | { | |
6087 | JPEG_TYPE, | |
6088 | JPEG_DATA, | |
6089 | JPEG_FILE, | |
6090 | JPEG_ASCENT, | |
6091 | JPEG_MARGIN, | |
6092 | JPEG_RELIEF, | |
6093 | JPEG_ALGORITHM, | |
6094 | JPEG_HEURISTIC_MASK, | |
6095 | JPEG_MASK, | |
6096 | JPEG_BACKGROUND, | |
6097 | JPEG_LAST | |
6098 | }; | |
6099 | ||
6100 | /* Vector of image_keyword structures describing the format | |
6101 | of valid user-defined image specifications. */ | |
6102 | ||
91433552 | 6103 | static const struct image_keyword jpeg_format[JPEG_LAST] = |
4ef0d4d0 KS |
6104 | { |
6105 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6106 | {":data", IMAGE_STRING_VALUE, 0}, | |
6107 | {":file", IMAGE_STRING_VALUE, 0}, | |
6108 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 6109 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
6110 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
6111 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6112 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6113 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6114 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
6115 | }; | |
6116 | ||
a9e7a9d5 | 6117 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 6118 | static bool init_jpeg_functions (void); |
bb4d86b4 CY |
6119 | #else |
6120 | #define init_jpeg_functions NULL | |
6121 | #endif | |
6122 | ||
4ef0d4d0 KS |
6123 | /* Structure describing the image type `jpeg'. */ |
6124 | ||
6125 | static struct image_type jpeg_type = | |
6126 | { | |
6127 | &Qjpeg, | |
6128 | jpeg_image_p, | |
6129 | jpeg_load, | |
6130 | x_clear_image, | |
bb4d86b4 | 6131 | init_jpeg_functions, |
4ef0d4d0 KS |
6132 | NULL |
6133 | }; | |
6134 | ||
578098f3 | 6135 | /* Return true if OBJECT is a valid JPEG image specification. */ |
4ef0d4d0 | 6136 | |
578098f3 | 6137 | static bool |
d3da34e0 | 6138 | jpeg_image_p (Lisp_Object object) |
4ef0d4d0 KS |
6139 | { |
6140 | struct image_keyword fmt[JPEG_LAST]; | |
6141 | ||
72af86bd | 6142 | memcpy (fmt, jpeg_format, sizeof fmt); |
4ef0d4d0 KS |
6143 | |
6144 | if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)) | |
6145 | return 0; | |
6146 | ||
6147 | /* Must specify either the :data or :file keyword. */ | |
6148 | return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1; | |
6149 | } | |
6150 | ||
9e2a2647 | 6151 | #endif /* HAVE_JPEG || HAVE_NS */ |
4ef0d4d0 KS |
6152 | |
6153 | #ifdef HAVE_JPEG | |
6154 | ||
6155 | /* Work around a warning about HAVE_STDLIB_H being redefined in | |
6156 | jconfig.h. */ | |
6157 | #ifdef HAVE_STDLIB_H | |
4ef0d4d0 KS |
6158 | #undef HAVE_STDLIB_H |
6159 | #endif /* HAVE_STLIB_H */ | |
6160 | ||
66354420 | 6161 | #if defined (HAVE_NTGUI) && !defined (__WIN32__) |
28893db2 JB |
6162 | /* In older releases of the jpeg library, jpeglib.h will define boolean |
6163 | differently depending on __WIN32__, so make sure it is defined. */ | |
66354420 JR |
6164 | #define __WIN32__ 1 |
6165 | #endif | |
6166 | ||
93aa5c81 DC |
6167 | /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types. |
6168 | Some versions of jpeglib try to detect whether rpcndr.h is loaded, | |
6169 | using the Windows boolean type instead of the jpeglib boolean type | |
6170 | if so. Cygwin jpeglib, however, doesn't try to detect whether its | |
6171 | headers are included along with windows.h, so under Cygwin, jpeglib | |
6172 | attempts to define a conflicting boolean type. Worse, forcing | |
6173 | Cygwin jpeglib headers to use the Windows boolean type doesn't work | |
6174 | because it created an ABI incompatibility between the | |
6175 | already-compiled jpeg library and the header interface definition. | |
6176 | ||
6177 | The best we can do is to define jpeglib's boolean type to a | |
6178 | different name. This name, jpeg_boolean, remains in effect through | |
6179 | the rest of image.c. | |
6180 | */ | |
a9e7a9d5 | 6181 | #if defined CYGWIN && defined HAVE_NTGUI |
0fda9b75 | 6182 | #define boolean jpeg_boolean |
2ae3a8a7 | 6183 | #endif |
4ef0d4d0 KS |
6184 | #include <jpeglib.h> |
6185 | #include <jerror.h> | |
4ef0d4d0 | 6186 | |
0fda9b75 | 6187 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
6188 | |
6189 | /* JPEG library details. */ | |
14beddf4 CY |
6190 | DEF_IMGLIB_FN (void, jpeg_CreateDecompress, (j_decompress_ptr, int, size_t)); |
6191 | DEF_IMGLIB_FN (boolean, jpeg_start_decompress, (j_decompress_ptr)); | |
6192 | DEF_IMGLIB_FN (boolean, jpeg_finish_decompress, (j_decompress_ptr)); | |
6193 | DEF_IMGLIB_FN (void, jpeg_destroy_decompress, (j_decompress_ptr)); | |
6194 | DEF_IMGLIB_FN (int, jpeg_read_header, (j_decompress_ptr, boolean)); | |
6195 | DEF_IMGLIB_FN (JDIMENSION, jpeg_read_scanlines, (j_decompress_ptr, JSAMPARRAY, JDIMENSION)); | |
6196 | DEF_IMGLIB_FN (struct jpeg_error_mgr *, jpeg_std_error, (struct jpeg_error_mgr *)); | |
6197 | DEF_IMGLIB_FN (boolean, jpeg_resync_to_restart, (j_decompress_ptr, int)); | |
4ef0d4d0 | 6198 | |
578098f3 | 6199 | static bool |
d07ff9db | 6200 | init_jpeg_functions (void) |
4ef0d4d0 KS |
6201 | { |
6202 | HMODULE library; | |
6203 | ||
d07ff9db | 6204 | if (!(library = w32_delayed_load (Qjpeg))) |
4ef0d4d0 KS |
6205 | return 0; |
6206 | ||
6207 | LOAD_IMGLIB_FN (library, jpeg_finish_decompress); | |
6208 | LOAD_IMGLIB_FN (library, jpeg_read_scanlines); | |
6209 | LOAD_IMGLIB_FN (library, jpeg_start_decompress); | |
6210 | LOAD_IMGLIB_FN (library, jpeg_read_header); | |
4ef0d4d0 KS |
6211 | LOAD_IMGLIB_FN (library, jpeg_CreateDecompress); |
6212 | LOAD_IMGLIB_FN (library, jpeg_destroy_decompress); | |
6213 | LOAD_IMGLIB_FN (library, jpeg_std_error); | |
6214 | LOAD_IMGLIB_FN (library, jpeg_resync_to_restart); | |
6215 | return 1; | |
6216 | } | |
6217 | ||
6218 | /* Wrapper since we can't directly assign the function pointer | |
6219 | to another function pointer that was declared more completely easily. */ | |
6220 | static boolean | |
7c3320d8 | 6221 | jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo, int desired) |
4ef0d4d0 KS |
6222 | { |
6223 | return fn_jpeg_resync_to_restart (cinfo, desired); | |
6224 | } | |
6225 | ||
6226 | #else | |
6227 | ||
5e617bc2 | 6228 | #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a) |
4ef0d4d0 KS |
6229 | #define fn_jpeg_start_decompress jpeg_start_decompress |
6230 | #define fn_jpeg_finish_decompress jpeg_finish_decompress | |
6231 | #define fn_jpeg_destroy_decompress jpeg_destroy_decompress | |
6232 | #define fn_jpeg_read_header jpeg_read_header | |
6233 | #define fn_jpeg_read_scanlines jpeg_read_scanlines | |
4ef0d4d0 KS |
6234 | #define fn_jpeg_std_error jpeg_std_error |
6235 | #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart | |
6236 | ||
0fda9b75 | 6237 | #endif /* WINDOWSNT */ |
4ef0d4d0 KS |
6238 | |
6239 | struct my_jpeg_error_mgr | |
6240 | { | |
6241 | struct jpeg_error_mgr pub; | |
0328b6de | 6242 | sys_jmp_buf setjmp_buffer; |
40bce90b | 6243 | |
0328b6de | 6244 | /* The remaining members are so that longjmp doesn't munge local |
40bce90b PE |
6245 | variables. */ |
6246 | struct jpeg_decompress_struct cinfo; | |
6247 | enum | |
6248 | { | |
6249 | MY_JPEG_ERROR_EXIT, | |
6250 | MY_JPEG_INVALID_IMAGE_SIZE, | |
6251 | MY_JPEG_CANNOT_CREATE_X | |
6252 | } failure_code; | |
5f0cb45a PE |
6253 | #ifdef lint |
6254 | FILE *fp; | |
6255 | #endif | |
4ef0d4d0 KS |
6256 | }; |
6257 | ||
6258 | ||
845ca893 | 6259 | static _Noreturn void |
d3da34e0 | 6260 | my_error_exit (j_common_ptr cinfo) |
4ef0d4d0 KS |
6261 | { |
6262 | struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err; | |
40bce90b | 6263 | mgr->failure_code = MY_JPEG_ERROR_EXIT; |
0328b6de | 6264 | sys_longjmp (mgr->setjmp_buffer, 1); |
4ef0d4d0 KS |
6265 | } |
6266 | ||
6267 | ||
6268 | /* Init source method for JPEG data source manager. Called by | |
6269 | jpeg_read_header() before any data is actually read. See | |
6270 | libjpeg.doc from the JPEG lib distribution. */ | |
6271 | ||
6272 | static void | |
d3da34e0 | 6273 | our_common_init_source (j_decompress_ptr cinfo) |
b25a72ab JB |
6274 | { |
6275 | } | |
6276 | ||
6277 | ||
6278 | /* Method to terminate data source. Called by | |
6279 | jpeg_finish_decompress() after all data has been processed. */ | |
6280 | ||
6281 | static void | |
d3da34e0 | 6282 | our_common_term_source (j_decompress_ptr cinfo) |
4ef0d4d0 KS |
6283 | { |
6284 | } | |
6285 | ||
6286 | ||
6287 | /* Fill input buffer method for JPEG data source manager. Called | |
6288 | whenever more data is needed. We read the whole image in one step, | |
6289 | so this only adds a fake end of input marker at the end. */ | |
6290 | ||
5a1539f3 | 6291 | static JOCTET our_memory_buffer[2]; |
aca946f3 | 6292 | |
4ef0d4d0 | 6293 | static boolean |
d3da34e0 | 6294 | our_memory_fill_input_buffer (j_decompress_ptr cinfo) |
4ef0d4d0 KS |
6295 | { |
6296 | /* Insert a fake EOI marker. */ | |
6297 | struct jpeg_source_mgr *src = cinfo->src; | |
4ef0d4d0 | 6298 | |
5a1539f3 MB |
6299 | our_memory_buffer[0] = (JOCTET) 0xFF; |
6300 | our_memory_buffer[1] = (JOCTET) JPEG_EOI; | |
4ef0d4d0 | 6301 | |
5a1539f3 | 6302 | src->next_input_byte = our_memory_buffer; |
4ef0d4d0 | 6303 | src->bytes_in_buffer = 2; |
e0e30823 | 6304 | return 1; |
4ef0d4d0 KS |
6305 | } |
6306 | ||
6307 | ||
6308 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6309 | is the JPEG data source manager. */ | |
6310 | ||
6311 | static void | |
d3da34e0 | 6312 | our_memory_skip_input_data (j_decompress_ptr cinfo, long int num_bytes) |
4ef0d4d0 | 6313 | { |
7d652d97 | 6314 | struct jpeg_source_mgr *src = cinfo->src; |
4ef0d4d0 KS |
6315 | |
6316 | if (src) | |
6317 | { | |
6318 | if (num_bytes > src->bytes_in_buffer) | |
6319 | ERREXIT (cinfo, JERR_INPUT_EOF); | |
6320 | ||
6321 | src->bytes_in_buffer -= num_bytes; | |
6322 | src->next_input_byte += num_bytes; | |
6323 | } | |
6324 | } | |
6325 | ||
6326 | ||
4ef0d4d0 KS |
6327 | /* Set up the JPEG lib for reading an image from DATA which contains |
6328 | LEN bytes. CINFO is the decompression info structure created for | |
6329 | reading the image. */ | |
6330 | ||
6331 | static void | |
92547ff9 | 6332 | jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, ptrdiff_t len) |
4ef0d4d0 | 6333 | { |
7d652d97 | 6334 | struct jpeg_source_mgr *src = cinfo->src; |
4ef0d4d0 | 6335 | |
7d652d97 | 6336 | if (! src) |
4ef0d4d0 KS |
6337 | { |
6338 | /* First time for this JPEG object? */ | |
7d652d97 PE |
6339 | src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, |
6340 | JPOOL_PERMANENT, sizeof *src); | |
6341 | cinfo->src = src; | |
4ef0d4d0 KS |
6342 | src->next_input_byte = data; |
6343 | } | |
6344 | ||
b25a72ab JB |
6345 | src->init_source = our_common_init_source; |
6346 | src->fill_input_buffer = our_memory_fill_input_buffer; | |
6347 | src->skip_input_data = our_memory_skip_input_data; | |
4ef0d4d0 | 6348 | src->resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ |
b25a72ab | 6349 | src->term_source = our_common_term_source; |
4ef0d4d0 KS |
6350 | src->bytes_in_buffer = len; |
6351 | src->next_input_byte = data; | |
6352 | } | |
6353 | ||
6354 | ||
b25a72ab JB |
6355 | struct jpeg_stdio_mgr |
6356 | { | |
6357 | struct jpeg_source_mgr mgr; | |
6358 | boolean finished; | |
6359 | FILE *file; | |
6360 | JOCTET *buffer; | |
6361 | }; | |
6362 | ||
6363 | ||
6364 | /* Size of buffer to read JPEG from file. | |
6365 | Not too big, as we want to use alloc_small. */ | |
6366 | #define JPEG_STDIO_BUFFER_SIZE 8192 | |
6367 | ||
6368 | ||
6369 | /* Fill input buffer method for JPEG data source manager. Called | |
6370 | whenever more data is needed. The data is read from a FILE *. */ | |
6371 | ||
6372 | static boolean | |
d3da34e0 | 6373 | our_stdio_fill_input_buffer (j_decompress_ptr cinfo) |
b25a72ab JB |
6374 | { |
6375 | struct jpeg_stdio_mgr *src; | |
6376 | ||
6377 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6378 | if (!src->finished) | |
6379 | { | |
3f791afe | 6380 | ptrdiff_t bytes; |
b25a72ab JB |
6381 | |
6382 | bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file); | |
6383 | if (bytes > 0) | |
6384 | src->mgr.bytes_in_buffer = bytes; | |
6385 | else | |
6386 | { | |
6387 | WARNMS (cinfo, JWRN_JPEG_EOF); | |
6388 | src->finished = 1; | |
6389 | src->buffer[0] = (JOCTET) 0xFF; | |
6390 | src->buffer[1] = (JOCTET) JPEG_EOI; | |
6391 | src->mgr.bytes_in_buffer = 2; | |
6392 | } | |
6393 | src->mgr.next_input_byte = src->buffer; | |
6394 | } | |
6395 | ||
6396 | return 1; | |
6397 | } | |
6398 | ||
6399 | ||
6400 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6401 | is the JPEG data source manager. */ | |
6402 | ||
6403 | static void | |
d3da34e0 | 6404 | our_stdio_skip_input_data (j_decompress_ptr cinfo, long int num_bytes) |
b25a72ab JB |
6405 | { |
6406 | struct jpeg_stdio_mgr *src; | |
6407 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6408 | ||
6409 | while (num_bytes > 0 && !src->finished) | |
6410 | { | |
6411 | if (num_bytes <= src->mgr.bytes_in_buffer) | |
6412 | { | |
6413 | src->mgr.bytes_in_buffer -= num_bytes; | |
6414 | src->mgr.next_input_byte += num_bytes; | |
6415 | break; | |
6416 | } | |
6417 | else | |
6418 | { | |
6419 | num_bytes -= src->mgr.bytes_in_buffer; | |
6420 | src->mgr.bytes_in_buffer = 0; | |
6421 | src->mgr.next_input_byte = NULL; | |
6422 | ||
6423 | our_stdio_fill_input_buffer (cinfo); | |
6424 | } | |
6425 | } | |
6426 | } | |
6427 | ||
6428 | ||
6429 | /* Set up the JPEG lib for reading an image from a FILE *. | |
6430 | CINFO is the decompression info structure created for | |
6431 | reading the image. */ | |
6432 | ||
6433 | static void | |
d3da34e0 | 6434 | jpeg_file_src (j_decompress_ptr cinfo, FILE *fp) |
b25a72ab | 6435 | { |
7d652d97 | 6436 | struct jpeg_stdio_mgr *src = (struct jpeg_stdio_mgr *) cinfo->src; |
b25a72ab | 6437 | |
7d652d97 | 6438 | if (! src) |
b25a72ab JB |
6439 | { |
6440 | /* First time for this JPEG object? */ | |
7d652d97 PE |
6441 | src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, |
6442 | JPOOL_PERMANENT, sizeof *src); | |
6443 | cinfo->src = (struct jpeg_source_mgr *) src; | |
6444 | src->buffer = cinfo->mem->alloc_small ((j_common_ptr) cinfo, | |
6445 | JPOOL_PERMANENT, | |
6446 | JPEG_STDIO_BUFFER_SIZE); | |
b25a72ab JB |
6447 | } |
6448 | ||
6449 | src->file = fp; | |
6450 | src->finished = 0; | |
6451 | src->mgr.init_source = our_common_init_source; | |
6452 | src->mgr.fill_input_buffer = our_stdio_fill_input_buffer; | |
6453 | src->mgr.skip_input_data = our_stdio_skip_input_data; | |
6454 | src->mgr.resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ | |
6455 | src->mgr.term_source = our_common_term_source; | |
6456 | src->mgr.bytes_in_buffer = 0; | |
6457 | src->mgr.next_input_byte = NULL; | |
6458 | } | |
6459 | ||
6460 | ||
4ef0d4d0 KS |
6461 | /* Load image IMG for use on frame F. Patterned after example.c |
6462 | from the JPEG lib. */ | |
6463 | ||
578098f3 | 6464 | static bool |
40bce90b PE |
6465 | jpeg_load_body (struct frame *f, struct image *img, |
6466 | struct my_jpeg_error_mgr *mgr) | |
4ef0d4d0 | 6467 | { |
4ef0d4d0 KS |
6468 | Lisp_Object file, specified_file; |
6469 | Lisp_Object specified_data; | |
40bce90b | 6470 | FILE *fp = NULL; |
4ef0d4d0 KS |
6471 | JSAMPARRAY buffer; |
6472 | int row_stride, x, y; | |
6473 | XImagePtr ximg = NULL; | |
4ef0d4d0 KS |
6474 | unsigned long *colors; |
6475 | int width, height; | |
4ef0d4d0 KS |
6476 | |
6477 | /* Open the JPEG file. */ | |
6478 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6479 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
4ef0d4d0 KS |
6480 | |
6481 | if (NILP (specified_data)) | |
6482 | { | |
6483 | file = x_find_image_file (specified_file); | |
6484 | if (!STRINGP (file)) | |
6485 | { | |
6486 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
6487 | return 0; |
6488 | } | |
6489 | ||
406af475 | 6490 | fp = emacs_fopen (SSDATA (file), "rb"); |
4ef0d4d0 KS |
6491 | if (fp == NULL) |
6492 | { | |
6493 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
6494 | return 0; |
6495 | } | |
6496 | } | |
7574650a AS |
6497 | else if (!STRINGP (specified_data)) |
6498 | { | |
6499 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
6500 | return 0; | |
6501 | } | |
4ef0d4d0 | 6502 | |
5f0cb45a PE |
6503 | IF_LINT (mgr->fp = fp); |
6504 | ||
4ef0d4d0 | 6505 | /* Customize libjpeg's error handling to call my_error_exit when an |
df61c790 | 6506 | error is detected. This function will perform a longjmp. */ |
40bce90b PE |
6507 | mgr->cinfo.err = fn_jpeg_std_error (&mgr->pub); |
6508 | mgr->pub.error_exit = my_error_exit; | |
0328b6de | 6509 | if (sys_setjmp (mgr->setjmp_buffer)) |
4ef0d4d0 | 6510 | { |
40bce90b | 6511 | switch (mgr->failure_code) |
4ef0d4d0 | 6512 | { |
40bce90b PE |
6513 | case MY_JPEG_ERROR_EXIT: |
6514 | { | |
6515 | char buf[JMSG_LENGTH_MAX]; | |
6516 | mgr->cinfo.err->format_message ((j_common_ptr) &mgr->cinfo, buf); | |
6517 | image_error ("Error reading JPEG image `%s': %s", img->spec, | |
6518 | build_string (buf)); | |
6519 | break; | |
6520 | } | |
6521 | ||
6522 | case MY_JPEG_INVALID_IMAGE_SIZE: | |
6523 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
6524 | break; | |
6525 | ||
6526 | case MY_JPEG_CANNOT_CREATE_X: | |
6527 | break; | |
4ef0d4d0 KS |
6528 | } |
6529 | ||
6530 | /* Close the input file and destroy the JPEG object. */ | |
6531 | if (fp) | |
40bce90b PE |
6532 | fclose (fp); |
6533 | fn_jpeg_destroy_decompress (&mgr->cinfo); | |
4ef0d4d0 KS |
6534 | |
6535 | /* If we already have an XImage, free that. */ | |
6536 | x_destroy_x_image (ximg); | |
6537 | ||
6538 | /* Free pixmap and colors. */ | |
6539 | x_clear_image (f, img); | |
4ef0d4d0 KS |
6540 | return 0; |
6541 | } | |
6542 | ||
5f0cb45a PE |
6543 | /* Silence a bogus diagnostic; see GCC bug 54561. */ |
6544 | IF_LINT (fp = mgr->fp); | |
6545 | ||
4ef0d4d0 KS |
6546 | /* Create the JPEG decompression object. Let it read from fp. |
6547 | Read the JPEG image header. */ | |
40bce90b | 6548 | fn_jpeg_CreateDecompress (&mgr->cinfo, JPEG_LIB_VERSION, sizeof *&mgr->cinfo); |
4ef0d4d0 KS |
6549 | |
6550 | if (NILP (specified_data)) | |
40bce90b | 6551 | jpeg_file_src (&mgr->cinfo, fp); |
4ef0d4d0 | 6552 | else |
40bce90b | 6553 | jpeg_memory_src (&mgr->cinfo, SDATA (specified_data), |
4ef0d4d0 KS |
6554 | SBYTES (specified_data)); |
6555 | ||
40bce90b | 6556 | fn_jpeg_read_header (&mgr->cinfo, 1); |
4ef0d4d0 KS |
6557 | |
6558 | /* Customize decompression so that color quantization will be used. | |
6559 | Start decompression. */ | |
40bce90b PE |
6560 | mgr->cinfo.quantize_colors = 1; |
6561 | fn_jpeg_start_decompress (&mgr->cinfo); | |
6562 | width = img->width = mgr->cinfo.output_width; | |
6563 | height = img->height = mgr->cinfo.output_height; | |
4ef0d4d0 | 6564 | |
f1f25b99 CY |
6565 | if (!check_image_size (f, width, height)) |
6566 | { | |
40bce90b | 6567 | mgr->failure_code = MY_JPEG_INVALID_IMAGE_SIZE; |
0328b6de | 6568 | sys_longjmp (mgr->setjmp_buffer, 1); |
f1f25b99 CY |
6569 | } |
6570 | ||
4ef0d4d0 | 6571 | /* Create X image and pixmap. */ |
547c9269 | 6572 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
40bce90b PE |
6573 | { |
6574 | mgr->failure_code = MY_JPEG_CANNOT_CREATE_X; | |
0328b6de | 6575 | sys_longjmp (mgr->setjmp_buffer, 1); |
40bce90b | 6576 | } |
4ef0d4d0 KS |
6577 | |
6578 | /* Allocate colors. When color quantization is used, | |
40bce90b PE |
6579 | mgr->cinfo.actual_number_of_colors has been set with the number of |
6580 | colors generated, and mgr->cinfo.colormap is a two-dimensional array | |
6581 | of color indices in the range 0..mgr->cinfo.actual_number_of_colors. | |
4ef0d4d0 KS |
6582 | No more than 255 colors will be generated. */ |
6583 | { | |
6584 | int i, ir, ig, ib; | |
6585 | ||
40bce90b | 6586 | if (mgr->cinfo.out_color_components > 2) |
4ef0d4d0 | 6587 | ir = 0, ig = 1, ib = 2; |
40bce90b | 6588 | else if (mgr->cinfo.out_color_components > 1) |
4ef0d4d0 KS |
6589 | ir = 0, ig = 1, ib = 0; |
6590 | else | |
6591 | ir = 0, ig = 0, ib = 0; | |
6592 | ||
6593 | /* Use the color table mechanism because it handles colors that | |
6594 | cannot be allocated nicely. Such colors will be replaced with | |
6595 | a default color, and we don't have to care about which colors | |
6596 | can be freed safely, and which can't. */ | |
6597 | init_color_table (); | |
40bce90b | 6598 | colors = alloca (mgr->cinfo.actual_number_of_colors * sizeof *colors); |
4ef0d4d0 | 6599 | |
40bce90b | 6600 | for (i = 0; i < mgr->cinfo.actual_number_of_colors; ++i) |
4ef0d4d0 KS |
6601 | { |
6602 | /* Multiply RGB values with 255 because X expects RGB values | |
6603 | in the range 0..0xffff. */ | |
40bce90b PE |
6604 | int r = mgr->cinfo.colormap[ir][i] << 8; |
6605 | int g = mgr->cinfo.colormap[ig][i] << 8; | |
6606 | int b = mgr->cinfo.colormap[ib][i] << 8; | |
4ef0d4d0 KS |
6607 | colors[i] = lookup_rgb_color (f, r, g, b); |
6608 | } | |
6609 | ||
6610 | #ifdef COLOR_TABLE_SUPPORT | |
6611 | /* Remember those colors actually allocated. */ | |
6612 | img->colors = colors_in_color_table (&img->ncolors); | |
6613 | free_color_table (); | |
6614 | #endif /* COLOR_TABLE_SUPPORT */ | |
6615 | } | |
6616 | ||
6617 | /* Read pixels. */ | |
40bce90b PE |
6618 | row_stride = width * mgr->cinfo.output_components; |
6619 | buffer = mgr->cinfo.mem->alloc_sarray ((j_common_ptr) &mgr->cinfo, | |
6620 | JPOOL_IMAGE, row_stride, 1); | |
4ef0d4d0 KS |
6621 | for (y = 0; y < height; ++y) |
6622 | { | |
40bce90b PE |
6623 | fn_jpeg_read_scanlines (&mgr->cinfo, buffer, 1); |
6624 | for (x = 0; x < mgr->cinfo.output_width; ++x) | |
4ef0d4d0 KS |
6625 | XPutPixel (ximg, x, y, colors[buffer[0][x]]); |
6626 | } | |
6627 | ||
6628 | /* Clean up. */ | |
40bce90b PE |
6629 | fn_jpeg_finish_decompress (&mgr->cinfo); |
6630 | fn_jpeg_destroy_decompress (&mgr->cinfo); | |
4ef0d4d0 | 6631 | if (fp) |
40bce90b | 6632 | fclose (fp); |
4ef0d4d0 KS |
6633 | |
6634 | /* Maybe fill in the background field while we have ximg handy. */ | |
6635 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
6636 | /* Casting avoids a GCC warning. */ |
6637 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 6638 | |
547c9269 YM |
6639 | /* Put ximg into the image. */ |
6640 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
6641 | return 1; |
6642 | } | |
6643 | ||
578098f3 | 6644 | static bool |
40bce90b PE |
6645 | jpeg_load (struct frame *f, struct image *img) |
6646 | { | |
6647 | struct my_jpeg_error_mgr mgr; | |
6648 | return jpeg_load_body (f, img, &mgr); | |
6649 | } | |
6650 | ||
4ef0d4d0 KS |
6651 | #else /* HAVE_JPEG */ |
6652 | ||
edfda783 | 6653 | #ifdef HAVE_NS |
578098f3 | 6654 | static bool |
edfda783 AR |
6655 | jpeg_load (struct frame *f, struct image *img) |
6656 | { | |
ed3751c8 JB |
6657 | return ns_load_image (f, img, |
6658 | image_spec_value (img->spec, QCfile, NULL), | |
6659 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
6660 | } |
6661 | #endif /* HAVE_NS */ | |
6662 | ||
4ef0d4d0 KS |
6663 | #endif /* !HAVE_JPEG */ |
6664 | ||
6665 | ||
6666 | \f | |
6667 | /*********************************************************************** | |
6668 | TIFF | |
6669 | ***********************************************************************/ | |
6670 | ||
9e2a2647 | 6671 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
4ef0d4d0 | 6672 | |
578098f3 PE |
6673 | static bool tiff_image_p (Lisp_Object object); |
6674 | static bool tiff_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
6675 | |
6676 | /* The symbol `tiff' identifying images of this type. */ | |
6677 | ||
955cbe7b | 6678 | static Lisp_Object Qtiff; |
4ef0d4d0 KS |
6679 | |
6680 | /* Indices of image specification fields in tiff_format, below. */ | |
6681 | ||
6682 | enum tiff_keyword_index | |
6683 | { | |
6684 | TIFF_TYPE, | |
6685 | TIFF_DATA, | |
6686 | TIFF_FILE, | |
6687 | TIFF_ASCENT, | |
6688 | TIFF_MARGIN, | |
6689 | TIFF_RELIEF, | |
6690 | TIFF_ALGORITHM, | |
6691 | TIFF_HEURISTIC_MASK, | |
6692 | TIFF_MASK, | |
6693 | TIFF_BACKGROUND, | |
58279503 | 6694 | TIFF_INDEX, |
4ef0d4d0 KS |
6695 | TIFF_LAST |
6696 | }; | |
6697 | ||
6698 | /* Vector of image_keyword structures describing the format | |
6699 | of valid user-defined image specifications. */ | |
6700 | ||
91433552 | 6701 | static const struct image_keyword tiff_format[TIFF_LAST] = |
4ef0d4d0 KS |
6702 | { |
6703 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6704 | {":data", IMAGE_STRING_VALUE, 0}, | |
6705 | {":file", IMAGE_STRING_VALUE, 0}, | |
6706 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 6707 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
6708 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
6709 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6710 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6711 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 JR |
6712 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, |
6713 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0} | |
4ef0d4d0 KS |
6714 | }; |
6715 | ||
33d4113c | 6716 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 6717 | static bool init_tiff_functions (void); |
bb4d86b4 CY |
6718 | #else |
6719 | #define init_tiff_functions NULL | |
6720 | #endif | |
6721 | ||
4ef0d4d0 KS |
6722 | /* Structure describing the image type `tiff'. */ |
6723 | ||
6724 | static struct image_type tiff_type = | |
6725 | { | |
6726 | &Qtiff, | |
6727 | tiff_image_p, | |
6728 | tiff_load, | |
6729 | x_clear_image, | |
bb4d86b4 | 6730 | init_tiff_functions, |
4ef0d4d0 KS |
6731 | NULL |
6732 | }; | |
6733 | ||
578098f3 | 6734 | /* Return true if OBJECT is a valid TIFF image specification. */ |
4ef0d4d0 | 6735 | |
578098f3 | 6736 | static bool |
d3da34e0 | 6737 | tiff_image_p (Lisp_Object object) |
4ef0d4d0 KS |
6738 | { |
6739 | struct image_keyword fmt[TIFF_LAST]; | |
72af86bd | 6740 | memcpy (fmt, tiff_format, sizeof fmt); |
4ef0d4d0 KS |
6741 | |
6742 | if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff)) | |
6743 | return 0; | |
6744 | ||
6745 | /* Must specify either the :data or :file keyword. */ | |
6746 | return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1; | |
6747 | } | |
6748 | ||
9e2a2647 | 6749 | #endif /* HAVE_TIFF || HAVE_NS */ |
4ef0d4d0 KS |
6750 | |
6751 | #ifdef HAVE_TIFF | |
6752 | ||
6753 | #include <tiffio.h> | |
6754 | ||
0fda9b75 | 6755 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
6756 | |
6757 | /* TIFF library details. */ | |
14beddf4 CY |
6758 | DEF_IMGLIB_FN (TIFFErrorHandler, TIFFSetErrorHandler, (TIFFErrorHandler)); |
6759 | DEF_IMGLIB_FN (TIFFErrorHandler, TIFFSetWarningHandler, (TIFFErrorHandler)); | |
6760 | DEF_IMGLIB_FN (TIFF *, TIFFOpen, (const char *, const char *)); | |
6761 | DEF_IMGLIB_FN (TIFF *, TIFFClientOpen, (const char *, const char *, thandle_t, | |
850690cc JB |
6762 | TIFFReadWriteProc, TIFFReadWriteProc, |
6763 | TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, | |
6764 | TIFFMapFileProc, TIFFUnmapFileProc)); | |
14beddf4 CY |
6765 | DEF_IMGLIB_FN (int, TIFFGetField, (TIFF *, ttag_t, ...)); |
6766 | DEF_IMGLIB_FN (int, TIFFReadRGBAImage, (TIFF *, uint32, uint32, uint32 *, int)); | |
6767 | DEF_IMGLIB_FN (void, TIFFClose, (TIFF *)); | |
6768 | DEF_IMGLIB_FN (int, TIFFSetDirectory, (TIFF *, tdir_t)); | |
4ef0d4d0 | 6769 | |
578098f3 | 6770 | static bool |
d07ff9db | 6771 | init_tiff_functions (void) |
4ef0d4d0 KS |
6772 | { |
6773 | HMODULE library; | |
6774 | ||
d07ff9db | 6775 | if (!(library = w32_delayed_load (Qtiff))) |
4ef0d4d0 KS |
6776 | return 0; |
6777 | ||
6778 | LOAD_IMGLIB_FN (library, TIFFSetErrorHandler); | |
6779 | LOAD_IMGLIB_FN (library, TIFFSetWarningHandler); | |
6780 | LOAD_IMGLIB_FN (library, TIFFOpen); | |
6781 | LOAD_IMGLIB_FN (library, TIFFClientOpen); | |
6782 | LOAD_IMGLIB_FN (library, TIFFGetField); | |
6783 | LOAD_IMGLIB_FN (library, TIFFReadRGBAImage); | |
6784 | LOAD_IMGLIB_FN (library, TIFFClose); | |
58279503 | 6785 | LOAD_IMGLIB_FN (library, TIFFSetDirectory); |
4ef0d4d0 KS |
6786 | return 1; |
6787 | } | |
6788 | ||
6789 | #else | |
6790 | ||
6791 | #define fn_TIFFSetErrorHandler TIFFSetErrorHandler | |
6792 | #define fn_TIFFSetWarningHandler TIFFSetWarningHandler | |
6793 | #define fn_TIFFOpen TIFFOpen | |
6794 | #define fn_TIFFClientOpen TIFFClientOpen | |
6795 | #define fn_TIFFGetField TIFFGetField | |
6796 | #define fn_TIFFReadRGBAImage TIFFReadRGBAImage | |
6797 | #define fn_TIFFClose TIFFClose | |
58279503 | 6798 | #define fn_TIFFSetDirectory TIFFSetDirectory |
0fda9b75 | 6799 | #endif /* WINDOWSNT */ |
4ef0d4d0 KS |
6800 | |
6801 | ||
6802 | /* Reading from a memory buffer for TIFF images Based on the PNG | |
6803 | memory source, but we have to provide a lot of extra functions. | |
6804 | Blah. | |
6805 | ||
6806 | We really only need to implement read and seek, but I am not | |
6807 | convinced that the TIFF library is smart enough not to destroy | |
6808 | itself if we only hand it the function pointers we need to | |
6809 | override. */ | |
6810 | ||
6811 | typedef struct | |
6812 | { | |
6813 | unsigned char *bytes; | |
3f791afe PE |
6814 | ptrdiff_t len; |
6815 | ptrdiff_t index; | |
4ef0d4d0 KS |
6816 | } |
6817 | tiff_memory_source; | |
6818 | ||
3f791afe | 6819 | static tsize_t |
d3da34e0 | 6820 | tiff_read_from_memory (thandle_t data, tdata_t buf, tsize_t size) |
4ef0d4d0 KS |
6821 | { |
6822 | tiff_memory_source *src = (tiff_memory_source *) data; | |
6823 | ||
3f791afe | 6824 | size = min (size, src->len - src->index); |
72af86bd | 6825 | memcpy (buf, src->bytes + src->index, size); |
4ef0d4d0 KS |
6826 | src->index += size; |
6827 | return size; | |
6828 | } | |
6829 | ||
3f791afe | 6830 | static tsize_t |
d3da34e0 | 6831 | tiff_write_from_memory (thandle_t data, tdata_t buf, tsize_t size) |
4ef0d4d0 | 6832 | { |
3f791afe | 6833 | return -1; |
4ef0d4d0 KS |
6834 | } |
6835 | ||
6836 | static toff_t | |
d3da34e0 | 6837 | tiff_seek_in_memory (thandle_t data, toff_t off, int whence) |
4ef0d4d0 KS |
6838 | { |
6839 | tiff_memory_source *src = (tiff_memory_source *) data; | |
3f791afe | 6840 | ptrdiff_t idx; |
4ef0d4d0 KS |
6841 | |
6842 | switch (whence) | |
6843 | { | |
6844 | case SEEK_SET: /* Go from beginning of source. */ | |
6845 | idx = off; | |
6846 | break; | |
6847 | ||
6848 | case SEEK_END: /* Go from end of source. */ | |
6849 | idx = src->len + off; | |
6850 | break; | |
6851 | ||
6852 | case SEEK_CUR: /* Go from current position. */ | |
6853 | idx = src->index + off; | |
6854 | break; | |
6855 | ||
6856 | default: /* Invalid `whence'. */ | |
6857 | return -1; | |
6858 | } | |
6859 | ||
6860 | if (idx > src->len || idx < 0) | |
6861 | return -1; | |
6862 | ||
6863 | src->index = idx; | |
6864 | return src->index; | |
6865 | } | |
6866 | ||
6867 | static int | |
d3da34e0 | 6868 | tiff_close_memory (thandle_t data) |
4ef0d4d0 KS |
6869 | { |
6870 | /* NOOP */ | |
6871 | return 0; | |
6872 | } | |
6873 | ||
6874 | static int | |
d3da34e0 | 6875 | tiff_mmap_memory (thandle_t data, tdata_t *pbase, toff_t *psize) |
4ef0d4d0 KS |
6876 | { |
6877 | /* It is already _IN_ memory. */ | |
6878 | return 0; | |
6879 | } | |
6880 | ||
6881 | static void | |
d3da34e0 | 6882 | tiff_unmap_memory (thandle_t data, tdata_t base, toff_t size) |
4ef0d4d0 KS |
6883 | { |
6884 | /* We don't need to do this. */ | |
6885 | } | |
6886 | ||
6887 | static toff_t | |
d3da34e0 | 6888 | tiff_size_of_memory (thandle_t data) |
4ef0d4d0 KS |
6889 | { |
6890 | return ((tiff_memory_source *) data)->len; | |
6891 | } | |
6892 | ||
b41c3a35 EZ |
6893 | /* GCC 3.x on x86 Windows targets has a bug that triggers an internal |
6894 | compiler error compiling tiff_handler, see Bugzilla bug #17406 | |
6895 | (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17406). Declaring | |
6896 | this function as external works around that problem. */ | |
6897 | #if defined (__MINGW32__) && __GNUC__ == 3 | |
6898 | # define MINGW_STATIC | |
6899 | #else | |
6900 | # define MINGW_STATIC static | |
6901 | #endif | |
4ef0d4d0 | 6902 | |
b41c3a35 EZ |
6903 | MINGW_STATIC void |
6904 | tiff_handler (const char *, const char *, const char *, va_list) | |
13464394 | 6905 | ATTRIBUTE_FORMAT_PRINTF (3, 0); |
b41c3a35 | 6906 | MINGW_STATIC void |
13464394 PE |
6907 | tiff_handler (const char *log_format, const char *title, |
6908 | const char *format, va_list ap) | |
6909 | { | |
6910 | /* doprnt is not suitable here, as TIFF handlers are called from | |
6911 | libtiff and are passed arbitrary printf directives. Instead, use | |
6912 | vsnprintf, taking care to be portable to nonstandard environments | |
6913 | where vsnprintf returns -1 on buffer overflow. Since it's just a | |
6914 | log entry, it's OK to truncate it. */ | |
6915 | char buf[4000]; | |
6916 | int len = vsnprintf (buf, sizeof buf, format, ap); | |
6917 | add_to_log (log_format, build_string (title), | |
6918 | make_string (buf, max (0, min (len, sizeof buf - 1)))); | |
6919 | } | |
b41c3a35 | 6920 | #undef MINGW_STATIC |
13464394 | 6921 | |
5e2d4a30 PE |
6922 | static void tiff_error_handler (const char *, const char *, va_list) |
6923 | ATTRIBUTE_FORMAT_PRINTF (2, 0); | |
4ef0d4d0 | 6924 | static void |
d3da34e0 | 6925 | tiff_error_handler (const char *title, const char *format, va_list ap) |
4ef0d4d0 | 6926 | { |
13464394 | 6927 | tiff_handler ("TIFF error: %s %s", title, format, ap); |
4ef0d4d0 KS |
6928 | } |
6929 | ||
6930 | ||
5e2d4a30 PE |
6931 | static void tiff_warning_handler (const char *, const char *, va_list) |
6932 | ATTRIBUTE_FORMAT_PRINTF (2, 0); | |
4ef0d4d0 | 6933 | static void |
d3da34e0 | 6934 | tiff_warning_handler (const char *title, const char *format, va_list ap) |
4ef0d4d0 | 6935 | { |
13464394 | 6936 | tiff_handler ("TIFF warning: %s %s", title, format, ap); |
4ef0d4d0 KS |
6937 | } |
6938 | ||
6939 | ||
578098f3 | 6940 | /* Load TIFF image IMG for use on frame F. Value is true if |
4ef0d4d0 KS |
6941 | successful. */ |
6942 | ||
578098f3 | 6943 | static bool |
d3da34e0 | 6944 | tiff_load (struct frame *f, struct image *img) |
4ef0d4d0 KS |
6945 | { |
6946 | Lisp_Object file, specified_file; | |
6947 | Lisp_Object specified_data; | |
6948 | TIFF *tiff; | |
58279503 | 6949 | int width, height, x, y, count; |
4ef0d4d0 | 6950 | uint32 *buf; |
03d0a109 | 6951 | int rc; |
4ef0d4d0 | 6952 | XImagePtr ximg; |
4ef0d4d0 | 6953 | tiff_memory_source memsrc; |
58279503 | 6954 | Lisp_Object image; |
4ef0d4d0 KS |
6955 | |
6956 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6957 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
4ef0d4d0 | 6958 | |
5e2d4a30 PE |
6959 | fn_TIFFSetErrorHandler ((TIFFErrorHandler) tiff_error_handler); |
6960 | fn_TIFFSetWarningHandler ((TIFFErrorHandler) tiff_warning_handler); | |
4ef0d4d0 KS |
6961 | |
6962 | if (NILP (specified_data)) | |
6963 | { | |
6964 | /* Read from a file */ | |
6965 | file = x_find_image_file (specified_file); | |
6966 | if (!STRINGP (file)) | |
6967 | { | |
6968 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
6969 | return 0; |
6970 | } | |
6971 | ||
df61c790 | 6972 | /* Try to open the image file. */ |
b19beacc | 6973 | tiff = fn_TIFFOpen (SSDATA (file), "r"); |
4ef0d4d0 KS |
6974 | if (tiff == NULL) |
6975 | { | |
6976 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
6977 | return 0; |
6978 | } | |
6979 | } | |
6980 | else | |
6981 | { | |
7574650a AS |
6982 | if (!STRINGP (specified_data)) |
6983 | { | |
6984 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
6985 | return 0; | |
6986 | } | |
6987 | ||
4ef0d4d0 KS |
6988 | /* Memory source! */ |
6989 | memsrc.bytes = SDATA (specified_data); | |
6990 | memsrc.len = SBYTES (specified_data); | |
6991 | memsrc.index = 0; | |
6992 | ||
3082f617 | 6993 | tiff = fn_TIFFClientOpen ("memory_source", "r", (thandle_t)&memsrc, |
3f791afe PE |
6994 | tiff_read_from_memory, |
6995 | tiff_write_from_memory, | |
df61c790 AS |
6996 | tiff_seek_in_memory, |
6997 | tiff_close_memory, | |
6998 | tiff_size_of_memory, | |
6999 | tiff_mmap_memory, | |
7000 | tiff_unmap_memory); | |
4ef0d4d0 KS |
7001 | |
7002 | if (!tiff) | |
7003 | { | |
7004 | image_error ("Cannot open memory source for `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
7005 | return 0; |
7006 | } | |
7007 | } | |
7008 | ||
58279503 JR |
7009 | image = image_spec_value (img->spec, QCindex, NULL); |
7010 | if (INTEGERP (image)) | |
7011 | { | |
13464394 PE |
7012 | EMACS_INT ino = XFASTINT (image); |
7013 | if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t) | |
7014 | && fn_TIFFSetDirectory (tiff, ino))) | |
58279503 JR |
7015 | { |
7016 | image_error ("Invalid image number `%s' in image `%s'", | |
7017 | image, img->spec); | |
7018 | fn_TIFFClose (tiff); | |
58279503 JR |
7019 | return 0; |
7020 | } | |
7021 | } | |
7022 | ||
4ef0d4d0 KS |
7023 | /* Get width and height of the image, and allocate a raster buffer |
7024 | of width x height 32-bit values. */ | |
7025 | fn_TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width); | |
7026 | fn_TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height); | |
f1f25b99 CY |
7027 | |
7028 | if (!check_image_size (f, width, height)) | |
7029 | { | |
10ea2b82 | 7030 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
58279503 | 7031 | fn_TIFFClose (tiff); |
f1f25b99 CY |
7032 | return 0; |
7033 | } | |
7034 | ||
ca4aa935 PE |
7035 | /* Create the X image and pixmap. */ |
7036 | if (! (height <= min (PTRDIFF_MAX, SIZE_MAX) / sizeof *buf / width | |
547c9269 YM |
7037 | && image_create_x_image_and_pixmap (f, img, width, height, 0, |
7038 | &ximg, 0))) | |
ca4aa935 PE |
7039 | { |
7040 | fn_TIFFClose (tiff); | |
7041 | return 0; | |
7042 | } | |
7043 | ||
23f86fce | 7044 | buf = xmalloc (sizeof *buf * width * height); |
4ef0d4d0 KS |
7045 | |
7046 | rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0); | |
58279503 JR |
7047 | |
7048 | /* Count the number of images in the file. */ | |
03d0a109 PE |
7049 | for (count = 1; fn_TIFFSetDirectory (tiff, count); count++) |
7050 | continue; | |
58279503 JR |
7051 | |
7052 | if (count > 1) | |
b50691aa CY |
7053 | img->lisp_data = Fcons (Qcount, |
7054 | Fcons (make_number (count), | |
7055 | img->lisp_data)); | |
58279503 | 7056 | |
4ef0d4d0 KS |
7057 | fn_TIFFClose (tiff); |
7058 | if (!rc) | |
7059 | { | |
7060 | image_error ("Error reading TIFF image `%s'", img->spec, Qnil); | |
7061 | xfree (buf); | |
4ef0d4d0 KS |
7062 | return 0; |
7063 | } | |
7064 | ||
4ef0d4d0 KS |
7065 | /* Initialize the color table. */ |
7066 | init_color_table (); | |
7067 | ||
7068 | /* Process the pixel raster. Origin is in the lower-left corner. */ | |
7069 | for (y = 0; y < height; ++y) | |
7070 | { | |
7071 | uint32 *row = buf + y * width; | |
7072 | ||
7073 | for (x = 0; x < width; ++x) | |
7074 | { | |
7075 | uint32 abgr = row[x]; | |
7076 | int r = TIFFGetR (abgr) << 8; | |
7077 | int g = TIFFGetG (abgr) << 8; | |
7078 | int b = TIFFGetB (abgr) << 8; | |
7079 | XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b)); | |
7080 | } | |
7081 | } | |
7082 | ||
7083 | #ifdef COLOR_TABLE_SUPPORT | |
7084 | /* Remember the colors allocated for the image. Free the color table. */ | |
7085 | img->colors = colors_in_color_table (&img->ncolors); | |
7086 | free_color_table (); | |
7087 | #endif /* COLOR_TABLE_SUPPORT */ | |
7088 | ||
7089 | img->width = width; | |
7090 | img->height = height; | |
7091 | ||
7092 | /* Maybe fill in the background field while we have ximg handy. */ | |
7093 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7094 | /* Casting avoids a GCC warning on W32. */ |
7095 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 7096 | |
547c9269 YM |
7097 | /* Put ximg into the image. */ |
7098 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
7099 | xfree (buf); |
7100 | ||
4ef0d4d0 KS |
7101 | return 1; |
7102 | } | |
7103 | ||
7104 | #else /* HAVE_TIFF */ | |
7105 | ||
edfda783 | 7106 | #ifdef HAVE_NS |
578098f3 | 7107 | static bool |
edfda783 AR |
7108 | tiff_load (struct frame *f, struct image *img) |
7109 | { | |
ed3751c8 JB |
7110 | return ns_load_image (f, img, |
7111 | image_spec_value (img->spec, QCfile, NULL), | |
7112 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
7113 | } |
7114 | #endif /* HAVE_NS */ | |
7115 | ||
4ef0d4d0 KS |
7116 | #endif /* !HAVE_TIFF */ |
7117 | ||
7118 | ||
7119 | \f | |
7120 | /*********************************************************************** | |
7121 | GIF | |
7122 | ***********************************************************************/ | |
7123 | ||
9e2a2647 | 7124 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
4ef0d4d0 | 7125 | |
578098f3 PE |
7126 | static bool gif_image_p (Lisp_Object object); |
7127 | static bool gif_load (struct frame *f, struct image *img); | |
f57e2426 | 7128 | static void gif_clear_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
7129 | |
7130 | /* The symbol `gif' identifying images of this type. */ | |
7131 | ||
955cbe7b | 7132 | static Lisp_Object Qgif; |
4ef0d4d0 KS |
7133 | |
7134 | /* Indices of image specification fields in gif_format, below. */ | |
7135 | ||
7136 | enum gif_keyword_index | |
7137 | { | |
7138 | GIF_TYPE, | |
7139 | GIF_DATA, | |
7140 | GIF_FILE, | |
7141 | GIF_ASCENT, | |
7142 | GIF_MARGIN, | |
7143 | GIF_RELIEF, | |
7144 | GIF_ALGORITHM, | |
7145 | GIF_HEURISTIC_MASK, | |
7146 | GIF_MASK, | |
7147 | GIF_IMAGE, | |
7148 | GIF_BACKGROUND, | |
7149 | GIF_LAST | |
7150 | }; | |
7151 | ||
7152 | /* Vector of image_keyword structures describing the format | |
7153 | of valid user-defined image specifications. */ | |
7154 | ||
91433552 | 7155 | static const struct image_keyword gif_format[GIF_LAST] = |
4ef0d4d0 KS |
7156 | { |
7157 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7158 | {":data", IMAGE_STRING_VALUE, 0}, | |
7159 | {":file", IMAGE_STRING_VALUE, 0}, | |
7160 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 7161 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
7162 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
7163 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7164 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7165 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 | 7166 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}, |
4ef0d4d0 KS |
7167 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} |
7168 | }; | |
7169 | ||
a9e7a9d5 | 7170 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 7171 | static bool init_gif_functions (void); |
bb4d86b4 CY |
7172 | #else |
7173 | #define init_gif_functions NULL | |
7174 | #endif | |
7175 | ||
4ef0d4d0 KS |
7176 | /* Structure describing the image type `gif'. */ |
7177 | ||
7178 | static struct image_type gif_type = | |
7179 | { | |
7180 | &Qgif, | |
7181 | gif_image_p, | |
7182 | gif_load, | |
6ac3c7bb | 7183 | gif_clear_image, |
bb4d86b4 | 7184 | init_gif_functions, |
4ef0d4d0 KS |
7185 | NULL |
7186 | }; | |
7187 | ||
6ac3c7bb KS |
7188 | /* Free X resources of GIF image IMG which is used on frame F. */ |
7189 | ||
7190 | static void | |
d3da34e0 | 7191 | gif_clear_image (struct frame *f, struct image *img) |
6ac3c7bb | 7192 | { |
b50691aa | 7193 | img->lisp_data = Qnil; |
6ac3c7bb KS |
7194 | x_clear_image (f, img); |
7195 | } | |
7196 | ||
578098f3 | 7197 | /* Return true if OBJECT is a valid GIF image specification. */ |
4ef0d4d0 | 7198 | |
578098f3 | 7199 | static bool |
d3da34e0 | 7200 | gif_image_p (Lisp_Object object) |
4ef0d4d0 KS |
7201 | { |
7202 | struct image_keyword fmt[GIF_LAST]; | |
72af86bd | 7203 | memcpy (fmt, gif_format, sizeof fmt); |
4ef0d4d0 KS |
7204 | |
7205 | if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)) | |
7206 | return 0; | |
7207 | ||
7208 | /* Must specify either the :data or :file keyword. */ | |
7209 | return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1; | |
7210 | } | |
7211 | ||
9e2a2647 | 7212 | #endif /* HAVE_GIF */ |
4ef0d4d0 KS |
7213 | |
7214 | #ifdef HAVE_GIF | |
7215 | ||
9e2a2647 | 7216 | #if defined (HAVE_NTGUI) |
99149625 | 7217 | |
2e09fef1 EZ |
7218 | /* winuser.h might define DrawText to DrawTextA or DrawTextW. |
7219 | Undefine before redefining to avoid a preprocessor warning. */ | |
7220 | #ifdef DrawText | |
7221 | #undef DrawText | |
7222 | #endif | |
4ef0d4d0 KS |
7223 | /* avoid conflict with QuickdrawText.h */ |
7224 | #define DrawText gif_DrawText | |
7225 | #include <gif_lib.h> | |
7226 | #undef DrawText | |
7227 | ||
567f95ec EZ |
7228 | /* Giflib before 5.0 didn't define these macros (used only if HAVE_NTGUI). */ |
7229 | #ifndef GIFLIB_MINOR | |
7230 | #define GIFLIB_MINOR 0 | |
7231 | #endif | |
7232 | #ifndef GIFLIB_RELEASE | |
7233 | #define GIFLIB_RELEASE 0 | |
7234 | #endif | |
7235 | ||
9e2a2647 | 7236 | #else /* HAVE_NTGUI */ |
4ef0d4d0 KS |
7237 | |
7238 | #include <gif_lib.h> | |
7239 | ||
9e2a2647 | 7240 | #endif /* HAVE_NTGUI */ |
4ef0d4d0 | 7241 | |
567f95ec EZ |
7242 | /* Giflib before 5.0 didn't define these macros. */ |
7243 | #ifndef GIFLIB_MAJOR | |
7244 | #define GIFLIB_MAJOR 4 | |
7245 | #endif | |
7246 | ||
0fda9b75 | 7247 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
7248 | |
7249 | /* GIF library details. */ | |
14beddf4 CY |
7250 | DEF_IMGLIB_FN (int, DGifCloseFile, (GifFileType *)); |
7251 | DEF_IMGLIB_FN (int, DGifSlurp, (GifFileType *)); | |
f3606ef7 | 7252 | #if GIFLIB_MAJOR < 5 |
14beddf4 CY |
7253 | DEF_IMGLIB_FN (GifFileType *, DGifOpen, (void *, InputFunc)); |
7254 | DEF_IMGLIB_FN (GifFileType *, DGifOpenFileName, (const char *)); | |
f3606ef7 BF |
7255 | #else |
7256 | DEF_IMGLIB_FN (GifFileType *, DGifOpen, (void *, InputFunc, int *)); | |
7257 | DEF_IMGLIB_FN (GifFileType *, DGifOpenFileName, (const char *, int *)); | |
a5dab159 | 7258 | DEF_IMGLIB_FN (char *, GifErrorString, (int)); |
f3606ef7 | 7259 | #endif |
4ef0d4d0 | 7260 | |
578098f3 | 7261 | static bool |
d07ff9db | 7262 | init_gif_functions (void) |
4ef0d4d0 KS |
7263 | { |
7264 | HMODULE library; | |
7265 | ||
d07ff9db | 7266 | if (!(library = w32_delayed_load (Qgif))) |
4ef0d4d0 KS |
7267 | return 0; |
7268 | ||
7269 | LOAD_IMGLIB_FN (library, DGifCloseFile); | |
7270 | LOAD_IMGLIB_FN (library, DGifSlurp); | |
7271 | LOAD_IMGLIB_FN (library, DGifOpen); | |
7272 | LOAD_IMGLIB_FN (library, DGifOpenFileName); | |
a5dab159 EZ |
7273 | #if GIFLIB_MAJOR >= 5 |
7274 | LOAD_IMGLIB_FN (library, GifErrorString); | |
7275 | #endif | |
4ef0d4d0 KS |
7276 | return 1; |
7277 | } | |
7278 | ||
7279 | #else | |
7280 | ||
7281 | #define fn_DGifCloseFile DGifCloseFile | |
7282 | #define fn_DGifSlurp DGifSlurp | |
7283 | #define fn_DGifOpen DGifOpen | |
7284 | #define fn_DGifOpenFileName DGifOpenFileName | |
99149625 PE |
7285 | #if 5 <= GIFLIB_MAJOR |
7286 | # define fn_GifErrorString GifErrorString | |
7287 | #endif | |
4ef0d4d0 | 7288 | |
0fda9b75 | 7289 | #endif /* WINDOWSNT */ |
4ef0d4d0 KS |
7290 | |
7291 | /* Reading a GIF image from memory | |
7292 | Based on the PNG memory stuff to a certain extent. */ | |
7293 | ||
7294 | typedef struct | |
7295 | { | |
7296 | unsigned char *bytes; | |
3f791afe PE |
7297 | ptrdiff_t len; |
7298 | ptrdiff_t index; | |
4ef0d4d0 KS |
7299 | } |
7300 | gif_memory_source; | |
7301 | ||
7302 | /* Make the current memory source available to gif_read_from_memory. | |
7303 | It's done this way because not all versions of libungif support | |
7304 | a UserData field in the GifFileType structure. */ | |
7305 | static gif_memory_source *current_gif_memory_src; | |
7306 | ||
7307 | static int | |
d3da34e0 | 7308 | gif_read_from_memory (GifFileType *file, GifByteType *buf, int len) |
4ef0d4d0 KS |
7309 | { |
7310 | gif_memory_source *src = current_gif_memory_src; | |
7311 | ||
7312 | if (len > src->len - src->index) | |
7313 | return -1; | |
7314 | ||
72af86bd | 7315 | memcpy (buf, src->bytes + src->index, len); |
4ef0d4d0 KS |
7316 | src->index += len; |
7317 | return len; | |
7318 | } | |
7319 | ||
7320 | ||
578098f3 | 7321 | /* Load GIF image IMG for use on frame F. Value is true if |
4ef0d4d0 KS |
7322 | successful. */ |
7323 | ||
91433552 DN |
7324 | static const int interlace_start[] = {0, 4, 2, 1}; |
7325 | static const int interlace_increment[] = {8, 8, 4, 2}; | |
aca946f3 | 7326 | |
17c0c952 JD |
7327 | #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249 |
7328 | ||
578098f3 | 7329 | static bool |
d3da34e0 | 7330 | gif_load (struct frame *f, struct image *img) |
4ef0d4d0 | 7331 | { |
60002bf5 CY |
7332 | Lisp_Object file; |
7333 | int rc, width, height, x, y, i, j; | |
4ef0d4d0 KS |
7334 | XImagePtr ximg; |
7335 | ColorMapObject *gif_color_map; | |
7336 | unsigned long pixel_colors[256]; | |
7337 | GifFileType *gif; | |
4ef0d4d0 | 7338 | gif_memory_source memsrc; |
60002bf5 CY |
7339 | Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL); |
7340 | Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL); | |
7341 | Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL); | |
7342 | unsigned long bgcolor = 0; | |
13464394 | 7343 | EMACS_INT idx; |
a5dab159 EZ |
7344 | #if GIFLIB_MAJOR >= 5 |
7345 | int gif_err; | |
7346 | #endif | |
4ef0d4d0 KS |
7347 | |
7348 | if (NILP (specified_data)) | |
7349 | { | |
7350 | file = x_find_image_file (specified_file); | |
7351 | if (!STRINGP (file)) | |
7352 | { | |
7353 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
7354 | return 0; |
7355 | } | |
7356 | ||
df61c790 | 7357 | /* Open the GIF file. */ |
f3606ef7 | 7358 | #if GIFLIB_MAJOR < 5 |
2037898d | 7359 | gif = fn_DGifOpenFileName (SSDATA (file)); |
4ef0d4d0 KS |
7360 | if (gif == NULL) |
7361 | { | |
7362 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
7363 | return 0; |
7364 | } | |
a5dab159 EZ |
7365 | #else |
7366 | gif = fn_DGifOpenFileName (SSDATA (file), &gif_err); | |
7367 | if (gif == NULL) | |
7368 | { | |
7369 | image_error ("Cannot open `%s': %s", | |
7370 | file, build_string (fn_GifErrorString (gif_err))); | |
7371 | return 0; | |
7372 | } | |
7373 | #endif | |
4ef0d4d0 KS |
7374 | } |
7375 | else | |
7376 | { | |
7574650a AS |
7377 | if (!STRINGP (specified_data)) |
7378 | { | |
7379 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
7380 | return 0; | |
7381 | } | |
7382 | ||
4ef0d4d0 KS |
7383 | /* Read from memory! */ |
7384 | current_gif_memory_src = &memsrc; | |
7385 | memsrc.bytes = SDATA (specified_data); | |
7386 | memsrc.len = SBYTES (specified_data); | |
7387 | memsrc.index = 0; | |
7388 | ||
f3606ef7 | 7389 | #if GIFLIB_MAJOR < 5 |
df61c790 | 7390 | gif = fn_DGifOpen (&memsrc, gif_read_from_memory); |
4ef0d4d0 KS |
7391 | if (!gif) |
7392 | { | |
7393 | image_error ("Cannot open memory source `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
7394 | return 0; |
7395 | } | |
a5dab159 EZ |
7396 | #else |
7397 | gif = fn_DGifOpen (&memsrc, gif_read_from_memory, &gif_err); | |
7398 | if (!gif) | |
7399 | { | |
7400 | image_error ("Cannot open memory source `%s': %s", | |
7401 | img->spec, build_string (fn_GifErrorString (gif_err))); | |
7402 | return 0; | |
7403 | } | |
7404 | #endif | |
4ef0d4d0 KS |
7405 | } |
7406 | ||
f1f25b99 CY |
7407 | /* Before reading entire contents, check the declared image size. */ |
7408 | if (!check_image_size (f, gif->SWidth, gif->SHeight)) | |
7409 | { | |
10ea2b82 | 7410 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 | 7411 | fn_DGifCloseFile (gif); |
f1f25b99 CY |
7412 | return 0; |
7413 | } | |
7414 | ||
4ef0d4d0 KS |
7415 | /* Read entire contents. */ |
7416 | rc = fn_DGifSlurp (gif); | |
60002bf5 | 7417 | if (rc == GIF_ERROR || gif->ImageCount <= 0) |
4ef0d4d0 KS |
7418 | { |
7419 | image_error ("Error reading `%s'", img->spec, Qnil); | |
7420 | fn_DGifCloseFile (gif); | |
4ef0d4d0 KS |
7421 | return 0; |
7422 | } | |
7423 | ||
60002bf5 CY |
7424 | /* Which sub-image are we to display? */ |
7425 | { | |
33290528 PE |
7426 | Lisp_Object image_number = image_spec_value (img->spec, QCindex, NULL); |
7427 | idx = INTEGERP (image_number) ? XFASTINT (image_number) : 0; | |
60002bf5 | 7428 | if (idx < 0 || idx >= gif->ImageCount) |
17c0c952 | 7429 | { |
60002bf5 | 7430 | image_error ("Invalid image number `%s' in image `%s'", |
33290528 | 7431 | image_number, img->spec); |
60002bf5 CY |
7432 | fn_DGifCloseFile (gif); |
7433 | return 0; | |
17c0c952 | 7434 | } |
60002bf5 | 7435 | } |
17c0c952 | 7436 | |
e013fb34 CY |
7437 | width = img->width = gif->SWidth; |
7438 | height = img->height = gif->SHeight; | |
0f4aca46 | 7439 | |
e013fb34 CY |
7440 | img->corners[TOP_CORNER] = gif->SavedImages[0].ImageDesc.Top; |
7441 | img->corners[LEFT_CORNER] = gif->SavedImages[0].ImageDesc.Left; | |
8d903f4e AS |
7442 | img->corners[BOT_CORNER] |
7443 | = img->corners[TOP_CORNER] + gif->SavedImages[0].ImageDesc.Height; | |
7444 | img->corners[RIGHT_CORNER] | |
7445 | = img->corners[LEFT_CORNER] + gif->SavedImages[0].ImageDesc.Width; | |
4ef0d4d0 | 7446 | |
f1f25b99 CY |
7447 | if (!check_image_size (f, width, height)) |
7448 | { | |
10ea2b82 | 7449 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 | 7450 | fn_DGifCloseFile (gif); |
f1f25b99 CY |
7451 | return 0; |
7452 | } | |
7453 | ||
44aa9ee6 PE |
7454 | /* Check that the selected subimages fit. It's not clear whether |
7455 | the GIF spec requires this, but Emacs can crash if they don't fit. */ | |
7456 | for (j = 0; j <= idx; ++j) | |
7457 | { | |
7458 | struct SavedImage *subimage = gif->SavedImages + j; | |
7459 | int subimg_width = subimage->ImageDesc.Width; | |
7460 | int subimg_height = subimage->ImageDesc.Height; | |
7461 | int subimg_top = subimage->ImageDesc.Top; | |
7462 | int subimg_left = subimage->ImageDesc.Left; | |
7463 | if (! (0 <= subimg_width && 0 <= subimg_height | |
7464 | && 0 <= subimg_top && subimg_top <= height - subimg_height | |
7465 | && 0 <= subimg_left && subimg_left <= width - subimg_width)) | |
7466 | { | |
7467 | image_error ("Subimage does not fit in image", Qnil, Qnil); | |
7468 | fn_DGifCloseFile (gif); | |
7469 | return 0; | |
7470 | } | |
7471 | } | |
7472 | ||
4ef0d4d0 | 7473 | /* Create the X image and pixmap. */ |
547c9269 | 7474 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
4ef0d4d0 KS |
7475 | { |
7476 | fn_DGifCloseFile (gif); | |
4ef0d4d0 KS |
7477 | return 0; |
7478 | } | |
7479 | ||
60002bf5 CY |
7480 | /* Clear the part of the screen image not covered by the image. |
7481 | Full animated GIF support requires more here (see the gif89 spec, | |
7482 | disposal methods). Let's simply assume that the part not covered | |
7483 | by a sub-image is in the frame's background color. */ | |
6ac3c7bb | 7484 | for (y = 0; y < img->corners[TOP_CORNER]; ++y) |
4ef0d4d0 KS |
7485 | for (x = 0; x < width; ++x) |
7486 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7487 | ||
6ac3c7bb | 7488 | for (y = img->corners[BOT_CORNER]; y < height; ++y) |
4ef0d4d0 KS |
7489 | for (x = 0; x < width; ++x) |
7490 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7491 | ||
6ac3c7bb | 7492 | for (y = img->corners[TOP_CORNER]; y < img->corners[BOT_CORNER]; ++y) |
4ef0d4d0 | 7493 | { |
6ac3c7bb | 7494 | for (x = 0; x < img->corners[LEFT_CORNER]; ++x) |
4ef0d4d0 | 7495 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
6ac3c7bb | 7496 | for (x = img->corners[RIGHT_CORNER]; x < width; ++x) |
4ef0d4d0 KS |
7497 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
7498 | } | |
7499 | ||
60002bf5 | 7500 | /* Read the GIF image into the X image. */ |
4ef0d4d0 | 7501 | |
60002bf5 CY |
7502 | /* FIXME: With the current implementation, loading an animated gif |
7503 | is quadratic in the number of animation frames, since each frame | |
7504 | is a separate struct image. We must provide a way for a single | |
7505 | gif_load call to construct and save all animation frames. */ | |
4ef0d4d0 | 7506 | |
60002bf5 CY |
7507 | init_color_table (); |
7508 | if (STRINGP (specified_bg)) | |
7509 | bgcolor = x_alloc_image_color (f, img, specified_bg, | |
7510 | FRAME_BACKGROUND_PIXEL (f)); | |
7511 | for (j = 0; j <= idx; ++j) | |
7512 | { | |
7513 | /* We use a local variable `raster' here because RasterBits is a | |
7514 | char *, which invites problems with bytes >= 0x80. */ | |
7515 | struct SavedImage *subimage = gif->SavedImages + j; | |
7516 | unsigned char *raster = (unsigned char *) subimage->RasterBits; | |
7517 | int transparency_color_index = -1; | |
7518 | int disposal = 0; | |
e013fb34 CY |
7519 | int subimg_width = subimage->ImageDesc.Width; |
7520 | int subimg_height = subimage->ImageDesc.Height; | |
7521 | int subimg_top = subimage->ImageDesc.Top; | |
7522 | int subimg_left = subimage->ImageDesc.Left; | |
60002bf5 CY |
7523 | |
7524 | /* Find the Graphic Control Extension block for this sub-image. | |
7525 | Extract the disposal method and transparency color. */ | |
7526 | for (i = 0; i < subimage->ExtensionBlockCount; i++) | |
4ef0d4d0 | 7527 | { |
60002bf5 | 7528 | ExtensionBlock *extblock = subimage->ExtensionBlocks + i; |
4ef0d4d0 | 7529 | |
60002bf5 CY |
7530 | if ((extblock->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION) |
7531 | && extblock->ByteCount == 4 | |
7532 | && extblock->Bytes[0] & 1) | |
4ef0d4d0 | 7533 | { |
60002bf5 CY |
7534 | /* From gif89a spec: 1 = "keep in place", 2 = "restore |
7535 | to background". Treat any other value like 2. */ | |
7536 | disposal = (extblock->Bytes[0] >> 2) & 7; | |
2547adb1 | 7537 | transparency_color_index = (unsigned char) extblock->Bytes[3]; |
60002bf5 | 7538 | break; |
4ef0d4d0 | 7539 | } |
4ef0d4d0 | 7540 | } |
60002bf5 CY |
7541 | |
7542 | /* We can't "keep in place" the first subimage. */ | |
7543 | if (j == 0) | |
7544 | disposal = 2; | |
7545 | ||
e013fb34 CY |
7546 | /* For disposal == 0, the spec says "No disposal specified. The |
7547 | decoder is not required to take any action." In practice, it | |
7548 | seems we need to treat this like "keep in place", see e.g. | |
7549 | http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */ | |
7550 | if (disposal == 0) | |
7551 | disposal = 1; | |
7552 | ||
60002bf5 CY |
7553 | /* Allocate subimage colors. */ |
7554 | memset (pixel_colors, 0, sizeof pixel_colors); | |
7555 | gif_color_map = subimage->ImageDesc.ColorMap; | |
7556 | if (!gif_color_map) | |
7557 | gif_color_map = gif->SColorMap; | |
7558 | ||
7559 | if (gif_color_map) | |
7560 | for (i = 0; i < gif_color_map->ColorCount; ++i) | |
4ef0d4d0 | 7561 | { |
60002bf5 CY |
7562 | if (transparency_color_index == i) |
7563 | pixel_colors[i] = STRINGP (specified_bg) | |
7564 | ? bgcolor : FRAME_BACKGROUND_PIXEL (f); | |
7565 | else | |
7566 | { | |
7567 | int r = gif_color_map->Colors[i].Red << 8; | |
7568 | int g = gif_color_map->Colors[i].Green << 8; | |
7569 | int b = gif_color_map->Colors[i].Blue << 8; | |
7570 | pixel_colors[i] = lookup_rgb_color (f, r, g, b); | |
7571 | } | |
4ef0d4d0 | 7572 | } |
60002bf5 CY |
7573 | |
7574 | /* Apply the pixel values. */ | |
7575 | if (gif->SavedImages[j].ImageDesc.Interlace) | |
7576 | { | |
7577 | int row, pass; | |
7578 | ||
7579 | for (y = 0, row = interlace_start[0], pass = 0; | |
e013fb34 | 7580 | y < subimg_height; |
60002bf5 CY |
7581 | y++, row += interlace_increment[pass]) |
7582 | { | |
a9ebfa0b | 7583 | while (subimg_height <= row) |
b9ff995e | 7584 | row = interlace_start[++pass]; |
60002bf5 | 7585 | |
e013fb34 | 7586 | for (x = 0; x < subimg_width; x++) |
60002bf5 | 7587 | { |
e013fb34 | 7588 | int c = raster[y * subimg_width + x]; |
60002bf5 | 7589 | if (transparency_color_index != c || disposal != 1) |
e013fb34 CY |
7590 | XPutPixel (ximg, x + subimg_left, row + subimg_top, |
7591 | pixel_colors[c]); | |
60002bf5 CY |
7592 | } |
7593 | } | |
7594 | } | |
7595 | else | |
7596 | { | |
e013fb34 CY |
7597 | for (y = 0; y < subimg_height; ++y) |
7598 | for (x = 0; x < subimg_width; ++x) | |
60002bf5 | 7599 | { |
e013fb34 | 7600 | int c = raster[y * subimg_width + x]; |
60002bf5 | 7601 | if (transparency_color_index != c || disposal != 1) |
e013fb34 CY |
7602 | XPutPixel (ximg, x + subimg_left, y + subimg_top, |
7603 | pixel_colors[c]); | |
60002bf5 CY |
7604 | } |
7605 | } | |
4ef0d4d0 KS |
7606 | } |
7607 | ||
60002bf5 CY |
7608 | #ifdef COLOR_TABLE_SUPPORT |
7609 | img->colors = colors_in_color_table (&img->ncolors); | |
7610 | free_color_table (); | |
7611 | #endif /* COLOR_TABLE_SUPPORT */ | |
7612 | ||
1546c559 JL |
7613 | /* Save GIF image extension data for `image-metadata'. |
7614 | Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */ | |
b50691aa | 7615 | img->lisp_data = Qnil; |
60002bf5 | 7616 | if (gif->SavedImages[idx].ExtensionBlockCount > 0) |
6ac3c7bb | 7617 | { |
13464394 | 7618 | int delay = 0; |
60002bf5 CY |
7619 | ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks; |
7620 | for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++) | |
6ac3c7bb | 7621 | /* Append (... FUNCTION "BYTES") */ |
1100a63c CY |
7622 | { |
7623 | img->lisp_data | |
7624 | = Fcons (make_number (ext->Function), | |
7625 | Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount), | |
7626 | img->lisp_data)); | |
7627 | if (ext->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION | |
7628 | && ext->ByteCount == 4) | |
7629 | { | |
7630 | delay = ext->Bytes[2] << CHAR_BIT; | |
7631 | delay |= ext->Bytes[1]; | |
7632 | } | |
7633 | } | |
6c6f1994 | 7634 | img->lisp_data = list2 (Qextension_data, img->lisp_data); |
1100a63c CY |
7635 | if (delay) |
7636 | img->lisp_data | |
7637 | = Fcons (Qdelay, | |
13464394 | 7638 | Fcons (make_float (delay / 100.0), |
1100a63c | 7639 | img->lisp_data)); |
6ac3c7bb | 7640 | } |
1100a63c | 7641 | |
6ac3c7bb | 7642 | if (gif->ImageCount > 1) |
b50691aa CY |
7643 | img->lisp_data = Fcons (Qcount, |
7644 | Fcons (make_number (gif->ImageCount), | |
7645 | img->lisp_data)); | |
6ac3c7bb | 7646 | |
4ef0d4d0 KS |
7647 | fn_DGifCloseFile (gif); |
7648 | ||
7649 | /* Maybe fill in the background field while we have ximg handy. */ | |
7650 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7651 | /* Casting avoids a GCC warning. */ |
7652 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 7653 | |
547c9269 YM |
7654 | /* Put ximg into the image. */ |
7655 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 | 7656 | |
4ef0d4d0 KS |
7657 | return 1; |
7658 | } | |
7659 | ||
2e09fef1 | 7660 | #else /* !HAVE_GIF */ |
4ef0d4d0 | 7661 | |
edfda783 | 7662 | #ifdef HAVE_NS |
578098f3 | 7663 | static bool |
edfda783 AR |
7664 | gif_load (struct frame *f, struct image *img) |
7665 | { | |
ed3751c8 JB |
7666 | return ns_load_image (f, img, |
7667 | image_spec_value (img->spec, QCfile, NULL), | |
7668 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
7669 | } |
7670 | #endif /* HAVE_NS */ | |
7671 | ||
4ef0d4d0 KS |
7672 | #endif /* HAVE_GIF */ |
7673 | ||
7674 | ||
b9ed53d5 PE |
7675 | #ifdef HAVE_IMAGEMAGICK |
7676 | ||
7677 | /*********************************************************************** | |
7678 | ImageMagick | |
7679 | ***********************************************************************/ | |
7680 | ||
7681 | /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER, | |
7682 | safely rounded and clipped to int range. */ | |
7683 | ||
7684 | static int | |
7685 | scale_image_size (int size, size_t divisor, size_t multiplier) | |
7686 | { | |
7687 | if (divisor != 0) | |
7688 | { | |
7689 | double s = size; | |
7690 | double scaled = s * multiplier / divisor + 0.5; | |
7691 | if (scaled < INT_MAX) | |
7692 | return scaled; | |
7693 | } | |
7694 | return INT_MAX; | |
7695 | } | |
7696 | ||
7697 | /* Compute the desired size of an image with native size WIDTH x HEIGHT. | |
7698 | Use SPEC to deduce the size. Store the desired size into | |
7699 | *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */ | |
f3f9606c LMI |
7700 | static void |
7701 | compute_image_size (size_t width, size_t height, | |
7702 | Lisp_Object spec, | |
7703 | int *d_width, int *d_height) | |
7704 | { | |
7705 | Lisp_Object value; | |
7706 | int desired_width, desired_height; | |
7707 | ||
7708 | /* If width and/or height is set in the display spec assume we want | |
7709 | to scale to those values. If either h or w is unspecified, the | |
7710 | unspecified should be calculated from the specified to preserve | |
7711 | aspect ratio. */ | |
7712 | value = image_spec_value (spec, QCwidth, NULL); | |
b9ed53d5 | 7713 | desired_width = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1; |
f3f9606c | 7714 | value = image_spec_value (spec, QCheight, NULL); |
b9ed53d5 | 7715 | desired_height = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1; |
f3f9606c LMI |
7716 | |
7717 | if (desired_width == -1) | |
7718 | { | |
7719 | value = image_spec_value (spec, QCmax_width, NULL); | |
b9ed53d5 | 7720 | if (NATNUMP (value)) |
f3f9606c | 7721 | { |
b9ed53d5 PE |
7722 | int max_width = min (XFASTINT (value), INT_MAX); |
7723 | if (max_width < width) | |
f3f9606c | 7724 | { |
b9ed53d5 PE |
7725 | /* The image is wider than :max-width. */ |
7726 | desired_width = max_width; | |
7727 | if (desired_height == -1) | |
f3f9606c | 7728 | { |
b9ed53d5 PE |
7729 | desired_height = scale_image_size (desired_width, |
7730 | width, height); | |
7731 | value = image_spec_value (spec, QCmax_height, NULL); | |
7732 | if (NATNUMP (value)) | |
f3f9606c | 7733 | { |
b9ed53d5 PE |
7734 | int max_height = min (XFASTINT (value), INT_MAX); |
7735 | if (max_height < desired_height) | |
7736 | { | |
7737 | desired_height = max_height; | |
7738 | desired_width = scale_image_size (desired_height, | |
7739 | height, width); | |
7740 | } | |
f3f9606c LMI |
7741 | } |
7742 | } | |
f3f9606c LMI |
7743 | } |
7744 | } | |
7745 | } | |
7746 | ||
7747 | if (desired_height == -1) | |
7748 | { | |
7749 | value = image_spec_value (spec, QCmax_height, NULL); | |
b9ed53d5 PE |
7750 | if (NATNUMP (value)) |
7751 | { | |
7752 | int max_height = min (XFASTINT (value), INT_MAX); | |
7753 | if (max_height < height) | |
7754 | desired_height = max_height; | |
7755 | } | |
f3f9606c LMI |
7756 | } |
7757 | ||
7758 | if (desired_width != -1 && desired_height == -1) | |
7759 | /* w known, calculate h. */ | |
b9ed53d5 | 7760 | desired_height = scale_image_size (desired_width, width, height); |
f3f9606c LMI |
7761 | |
7762 | if (desired_width == -1 && desired_height != -1) | |
7763 | /* h known, calculate w. */ | |
b9ed53d5 | 7764 | desired_width = scale_image_size (desired_height, height, width); |
f3f9606c LMI |
7765 | |
7766 | *d_width = desired_width; | |
7767 | *d_height = desired_height; | |
7768 | } | |
7769 | ||
4b66faf3 | 7770 | static Lisp_Object Qimagemagick; |
a32d4040 | 7771 | |
578098f3 PE |
7772 | static bool imagemagick_image_p (Lisp_Object); |
7773 | static bool imagemagick_load (struct frame *, struct image *); | |
d66c4c7c CY |
7774 | static void imagemagick_clear_image (struct frame *, struct image *); |
7775 | ||
a32d4040 | 7776 | /* Indices of image specification fields in imagemagick_format. */ |
3de25479 JV |
7777 | |
7778 | enum imagemagick_keyword_index | |
4917006b JV |
7779 | { |
7780 | IMAGEMAGICK_TYPE, | |
7781 | IMAGEMAGICK_DATA, | |
7782 | IMAGEMAGICK_FILE, | |
7783 | IMAGEMAGICK_ASCENT, | |
7784 | IMAGEMAGICK_MARGIN, | |
7785 | IMAGEMAGICK_RELIEF, | |
7786 | IMAGEMAGICK_ALGORITHM, | |
7787 | IMAGEMAGICK_HEURISTIC_MASK, | |
7788 | IMAGEMAGICK_MASK, | |
7789 | IMAGEMAGICK_BACKGROUND, | |
c7f514b5 JV |
7790 | IMAGEMAGICK_HEIGHT, |
7791 | IMAGEMAGICK_WIDTH, | |
f3f9606c LMI |
7792 | IMAGEMAGICK_MAX_HEIGHT, |
7793 | IMAGEMAGICK_MAX_WIDTH, | |
c6b7ccaa | 7794 | IMAGEMAGICK_FORMAT, |
c7f514b5 JV |
7795 | IMAGEMAGICK_ROTATION, |
7796 | IMAGEMAGICK_CROP, | |
4917006b JV |
7797 | IMAGEMAGICK_LAST |
7798 | }; | |
3de25479 JV |
7799 | |
7800 | /* Vector of image_keyword structures describing the format | |
7801 | of valid user-defined image specifications. */ | |
7802 | ||
7803 | static struct image_keyword imagemagick_format[IMAGEMAGICK_LAST] = | |
4917006b JV |
7804 | { |
7805 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7806 | {":data", IMAGE_STRING_VALUE, 0}, | |
7807 | {":file", IMAGE_STRING_VALUE, 0}, | |
7808 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 7809 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4917006b JV |
7810 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
7811 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7812 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7813 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
c7f514b5 | 7814 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, |
7574650a AS |
7815 | {":height", IMAGE_INTEGER_VALUE, 0}, |
7816 | {":width", IMAGE_INTEGER_VALUE, 0}, | |
f3f9606c LMI |
7817 | {":max-height", IMAGE_INTEGER_VALUE, 0}, |
7818 | {":max-width", IMAGE_INTEGER_VALUE, 0}, | |
c6b7ccaa | 7819 | {":format", IMAGE_SYMBOL_VALUE, 0}, |
7574650a | 7820 | {":rotation", IMAGE_NUMBER_VALUE, 0}, |
c7f514b5 | 7821 | {":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0} |
4917006b | 7822 | }; |
a32d4040 | 7823 | |
33d4113c | 7824 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 7825 | static bool init_imagemagick_functions (void); |
bb4d86b4 CY |
7826 | #else |
7827 | #define init_imagemagick_functions NULL | |
7828 | #endif | |
7829 | ||
d66c4c7c CY |
7830 | /* Structure describing the image type for any image handled via |
7831 | ImageMagick. */ | |
7832 | ||
7833 | static struct image_type imagemagick_type = | |
7834 | { | |
7835 | &Qimagemagick, | |
7836 | imagemagick_image_p, | |
7837 | imagemagick_load, | |
7838 | imagemagick_clear_image, | |
bb4d86b4 | 7839 | init_imagemagick_functions, |
d66c4c7c CY |
7840 | NULL |
7841 | }; | |
7842 | ||
3de25479 JV |
7843 | /* Free X resources of imagemagick image IMG which is used on frame F. */ |
7844 | ||
7845 | static void | |
4917006b JV |
7846 | imagemagick_clear_image (struct frame *f, |
7847 | struct image *img) | |
3de25479 | 7848 | { |
3de25479 JV |
7849 | x_clear_image (f, img); |
7850 | } | |
7851 | ||
578098f3 | 7852 | /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do |
3de25479 JV |
7853 | this by calling parse_image_spec and supplying the keywords that |
7854 | identify the IMAGEMAGICK format. */ | |
7855 | ||
578098f3 | 7856 | static bool |
4917006b | 7857 | imagemagick_image_p (Lisp_Object object) |
3de25479 JV |
7858 | { |
7859 | struct image_keyword fmt[IMAGEMAGICK_LAST]; | |
dbfe4532 | 7860 | memcpy (fmt, imagemagick_format, sizeof fmt); |
3de25479 JV |
7861 | |
7862 | if (!parse_image_spec (object, fmt, IMAGEMAGICK_LAST, Qimagemagick)) | |
7863 | return 0; | |
7864 | ||
7865 | /* Must specify either the :data or :file keyword. */ | |
7866 | return fmt[IMAGEMAGICK_FILE].count + fmt[IMAGEMAGICK_DATA].count == 1; | |
7867 | } | |
7868 | ||
7869 | /* The GIF library also defines DrawRectangle, but its never used in Emacs. | |
c7015153 | 7870 | Therefore rename the function so it doesn't collide with ImageMagick. */ |
3de25479 JV |
7871 | #define DrawRectangle DrawRectangleGif |
7872 | #include <wand/MagickWand.h> | |
7873 | ||
60737f02 PE |
7874 | /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason. |
7875 | Emacs seems to work fine with the hidden version, so unhide it. */ | |
7876 | #include <magick/version.h> | |
7877 | #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665 | |
7878 | extern WandExport void PixelGetMagickColor (const PixelWand *, | |
7879 | MagickPixelPacket *); | |
7880 | #endif | |
7881 | ||
d1d7b339 JL |
7882 | /* Log ImageMagick error message. |
7883 | Useful when a ImageMagick function returns the status `MagickFalse'. */ | |
7884 | ||
7885 | static void | |
7886 | imagemagick_error (MagickWand *wand) | |
7887 | { | |
7888 | char *description; | |
7889 | ExceptionType severity; | |
7890 | ||
7891 | description = MagickGetException (wand, &severity); | |
7892 | image_error ("ImageMagick error: %s", | |
1130ecfc | 7893 | build_string (description), |
d1d7b339 JL |
7894 | Qnil); |
7895 | description = (char *) MagickRelinquishMemory (description); | |
7896 | } | |
7897 | ||
8259030d | 7898 | /* Possibly give ImageMagick some extra help to determine the image |
01c3051f | 7899 | type by supplying a "dummy" filename based on the Content-Type. */ |
8259030d | 7900 | |
01c3051f PE |
7901 | static char * |
7902 | imagemagick_filename_hint (Lisp_Object spec, char hint_buffer[MaxTextExtent]) | |
8259030d | 7903 | { |
01c3051f PE |
7904 | Lisp_Object symbol = intern ("image-format-suffixes"); |
7905 | Lisp_Object val = find_symbol_value (symbol); | |
7906 | Lisp_Object format; | |
8259030d | 7907 | |
66e7901a LMI |
7908 | if (! CONSP (val)) |
7909 | return NULL; | |
cfa1f436 | 7910 | |
01c3051f PE |
7911 | format = image_spec_value (spec, intern (":format"), NULL); |
7912 | val = Fcar_safe (Fcdr_safe (Fassq (format, val))); | |
66e7901a LMI |
7913 | if (! STRINGP (val)) |
7914 | return NULL; | |
7915 | ||
01c3051f PE |
7916 | /* It's OK to truncate the hint if it has MaxTextExtent or more bytes, |
7917 | as ImageMagick would ignore the extra bytes anyway. */ | |
7918 | snprintf (hint_buffer, MaxTextExtent, "/tmp/foo.%s", SSDATA (val)); | |
7919 | return hint_buffer; | |
8259030d LMI |
7920 | } |
7921 | ||
6e856b69 LMI |
7922 | /* Animated images (e.g., GIF89a) are composed from one "master image" |
7923 | (which is the first one, and then there's a number of images that | |
7924 | follow. If following images have non-transparent colors, these are | |
7925 | composed "on top" of the master image. So, in general, one has to | |
7926 | compute ann the preceding images to be able to display a particular | |
274ff63f | 7927 | sub-image. |
6e856b69 | 7928 | |
274ff63f LMI |
7929 | Computing all the preceding images is too slow, so we maintain a |
7930 | cache of previously computed images. We have to maintain a cache | |
7931 | separate from the image cache, because the images may be scaled | |
7932 | before display. */ | |
7933 | ||
7934 | struct animation_cache | |
7935 | { | |
274ff63f LMI |
7936 | MagickWand *wand; |
7937 | int index; | |
43aac990 | 7938 | struct timespec update_time; |
274ff63f | 7939 | struct animation_cache *next; |
8c2f38aa | 7940 | char signature[FLEXIBLE_ARRAY_MEMBER]; |
274ff63f LMI |
7941 | }; |
7942 | ||
7943 | static struct animation_cache *animation_cache = NULL; | |
7944 | ||
deb31e75 | 7945 | static struct animation_cache * |
274ff63f LMI |
7946 | imagemagick_create_cache (char *signature) |
7947 | { | |
8c2f38aa PE |
7948 | struct animation_cache *cache |
7949 | = xmalloc (offsetof (struct animation_cache, signature) | |
7950 | + strlen (signature) + 1); | |
d0139df5 PE |
7951 | cache->wand = 0; |
7952 | cache->index = 0; | |
7953 | cache->next = 0; | |
8c2f38aa | 7954 | strcpy (cache->signature, signature); |
274ff63f LMI |
7955 | return cache; |
7956 | } | |
7957 | ||
7958 | /* Discard cached images that haven't been used for a minute. */ | |
deb31e75 DA |
7959 | static void |
7960 | imagemagick_prune_animation_cache (void) | |
274ff63f | 7961 | { |
d0139df5 | 7962 | struct animation_cache **pcache = &animation_cache; |
43aac990 PE |
7963 | struct timespec old = timespec_sub (current_timespec (), |
7964 | make_timespec (60, 0)); | |
274ff63f | 7965 | |
d0139df5 | 7966 | while (*pcache) |
274ff63f | 7967 | { |
d0139df5 | 7968 | struct animation_cache *cache = *pcache; |
43aac990 | 7969 | if (timespec_cmp (old, cache->update_time) <= 0) |
d0139df5 PE |
7970 | pcache = &cache->next; |
7971 | else | |
274ff63f | 7972 | { |
274ff63f LMI |
7973 | if (cache->wand) |
7974 | DestroyMagickWand (cache->wand); | |
d0139df5 PE |
7975 | *pcache = cache->next; |
7976 | xfree (cache); | |
8a6f875f | 7977 | } |
274ff63f LMI |
7978 | } |
7979 | } | |
7980 | ||
deb31e75 | 7981 | static struct animation_cache * |
274ff63f LMI |
7982 | imagemagick_get_animation_cache (MagickWand *wand) |
7983 | { | |
d0139df5 | 7984 | char *signature = MagickGetImageSignature (wand); |
319a4d03 | 7985 | struct animation_cache *cache; |
bb566cea | 7986 | struct animation_cache **pcache = &animation_cache; |
274ff63f LMI |
7987 | |
7988 | imagemagick_prune_animation_cache (); | |
7989 | ||
bb566cea | 7990 | while (1) |
274ff63f | 7991 | { |
bb566cea PE |
7992 | cache = *pcache; |
7993 | if (! cache) | |
7994 | { | |
7995 | *pcache = cache = imagemagick_create_cache (signature); | |
7996 | break; | |
7997 | } | |
8c2f38aa | 7998 | if (strcmp (signature, cache->signature) == 0) |
bb566cea PE |
7999 | break; |
8000 | pcache = &cache->next; | |
274ff63f LMI |
8001 | } |
8002 | ||
453f4022 | 8003 | DestroyString (signature); |
43aac990 | 8004 | cache->update_time = current_timespec (); |
274ff63f LMI |
8005 | return cache; |
8006 | } | |
995be755 | 8007 | |
6e856b69 LMI |
8008 | static MagickWand * |
8009 | imagemagick_compute_animated_image (MagickWand *super_wand, int ino) | |
8010 | { | |
691a357f | 8011 | int i; |
6e856b69 | 8012 | MagickWand *composite_wand; |
703dbeba | 8013 | size_t dest_width, dest_height; |
274ff63f | 8014 | struct animation_cache *cache = imagemagick_get_animation_cache (super_wand); |
6e856b69 LMI |
8015 | |
8016 | MagickSetIteratorIndex (super_wand, 0); | |
6e856b69 | 8017 | |
274ff63f LMI |
8018 | if (ino == 0 || cache->wand == NULL || cache->index > ino) |
8019 | { | |
8020 | composite_wand = MagickGetImage (super_wand); | |
8021 | if (cache->wand) | |
8022 | DestroyMagickWand (cache->wand); | |
8023 | } | |
995be755 | 8024 | else |
274ff63f | 8025 | composite_wand = cache->wand; |
995be755 | 8026 | |
703dbeba LMI |
8027 | dest_width = MagickGetImageWidth (composite_wand); |
8028 | dest_height = MagickGetImageHeight (composite_wand); | |
8029 | ||
274ff63f | 8030 | for (i = max (1, cache->index + 1); i <= ino; i++) |
691a357f PE |
8031 | { |
8032 | MagickWand *sub_wand; | |
8033 | PixelIterator *source_iterator, *dest_iterator; | |
8034 | PixelWand **source, **dest; | |
703dbeba LMI |
8035 | size_t source_width, source_height; |
8036 | ssize_t source_left, source_top; | |
691a357f | 8037 | MagickPixelPacket pixel; |
a5b5f73f | 8038 | DisposeType dispose; |
79670014 | 8039 | ptrdiff_t lines = 0; |
6e856b69 | 8040 | |
691a357f PE |
8041 | MagickSetIteratorIndex (super_wand, i); |
8042 | sub_wand = MagickGetImage (super_wand); | |
6e856b69 | 8043 | |
703dbeba LMI |
8044 | MagickGetImagePage (sub_wand, &source_width, &source_height, |
8045 | &source_left, &source_top); | |
8046 | ||
0f8a6926 | 8047 | /* This flag says how to handle transparent pixels. */ |
a5b5f73f LMI |
8048 | dispose = MagickGetImageDispose (sub_wand); |
8049 | ||
691a357f PE |
8050 | source_iterator = NewPixelIterator (sub_wand); |
8051 | if (! source_iterator) | |
8052 | { | |
8053 | DestroyMagickWand (composite_wand); | |
8054 | DestroyMagickWand (sub_wand); | |
274ff63f | 8055 | cache->wand = NULL; |
691a357f PE |
8056 | image_error ("Imagemagick pixel iterator creation failed", |
8057 | Qnil, Qnil); | |
8058 | return NULL; | |
8059 | } | |
6e856b69 | 8060 | |
691a357f PE |
8061 | dest_iterator = NewPixelIterator (composite_wand); |
8062 | if (! dest_iterator) | |
8063 | { | |
8064 | DestroyMagickWand (composite_wand); | |
8065 | DestroyMagickWand (sub_wand); | |
8066 | DestroyPixelIterator (source_iterator); | |
274ff63f | 8067 | cache->wand = NULL; |
691a357f PE |
8068 | image_error ("Imagemagick pixel iterator creation failed", |
8069 | Qnil, Qnil); | |
8070 | return NULL; | |
8071 | } | |
6e856b69 | 8072 | |
526e5233 | 8073 | /* The sub-image may not start at origin, so move the destination |
703dbeba | 8074 | iterator to where the sub-image should start. */ |
1d18b1e6 | 8075 | if (source_top > 0) |
79670014 LMI |
8076 | { |
8077 | PixelSetIteratorRow (dest_iterator, source_top); | |
8078 | lines = source_top; | |
8079 | } | |
703dbeba | 8080 | |
691a357f PE |
8081 | while ((source = PixelGetNextIteratorRow (source_iterator, &source_width)) |
8082 | != NULL) | |
6e856b69 | 8083 | { |
691a357f | 8084 | ptrdiff_t x; |
79670014 LMI |
8085 | |
8086 | /* Sanity check. This shouldn't happen, but apparently | |
8087 | does in some pictures. */ | |
8088 | if (++lines >= dest_height) | |
8089 | break; | |
8090 | ||
691a357f PE |
8091 | dest = PixelGetNextIteratorRow (dest_iterator, &dest_width); |
8092 | for (x = 0; x < source_width; x++) | |
6e856b69 | 8093 | { |
703dbeba | 8094 | /* Sanity check. This shouldn't happen, but apparently |
79670014 | 8095 | also does in some pictures. */ |
703dbeba LMI |
8096 | if (x + source_left > dest_width) |
8097 | break; | |
6f94cbbc LMI |
8098 | /* Normally we only copy over non-transparent pixels, |
8099 | but if the disposal method is "Background", then we | |
8100 | copy over all pixels. */ | |
8101 | if (dispose == BackgroundDispose || | |
8102 | PixelGetAlpha (source[x])) | |
691a357f PE |
8103 | { |
8104 | PixelGetMagickColor (source[x], &pixel); | |
703dbeba | 8105 | PixelSetMagickColor (dest[x + source_left], &pixel); |
691a357f | 8106 | } |
6e856b69 | 8107 | } |
1eade943 | 8108 | PixelSyncIterator (dest_iterator); |
6e856b69 | 8109 | } |
6e856b69 | 8110 | |
691a357f PE |
8111 | DestroyPixelIterator (source_iterator); |
8112 | DestroyPixelIterator (dest_iterator); | |
8113 | DestroyMagickWand (sub_wand); | |
8114 | } | |
6e856b69 | 8115 | |
995be755 LMI |
8116 | /* Cache a copy for the next iteration. The current wand will be |
8117 | destroyed by the caller. */ | |
274ff63f LMI |
8118 | cache->wand = CloneMagickWand (composite_wand); |
8119 | cache->index = ino; | |
995be755 | 8120 | |
6e856b69 LMI |
8121 | return composite_wand; |
8122 | } | |
8123 | ||
8124 | ||
d66c4c7c CY |
8125 | /* Helper function for imagemagick_load, which does the actual loading |
8126 | given contents and size, apart from frame and image structures, | |
8127 | passed from imagemagick_load. Uses librimagemagick to do most of | |
8128 | the image processing. | |
3de25479 | 8129 | |
d66c4c7c CY |
8130 | F is a pointer to the Emacs frame; IMG to the image structure to |
8131 | prepare; CONTENTS is the string containing the IMAGEMAGICK data to | |
8132 | be parsed; SIZE is the number of bytes of data; and FILENAME is | |
8133 | either the file name or the image data. | |
3de25479 | 8134 | |
578098f3 | 8135 | Return true if successful. */ |
3de25479 | 8136 | |
578098f3 | 8137 | static bool |
d66c4c7c CY |
8138 | imagemagick_load_image (struct frame *f, struct image *img, |
8139 | unsigned char *contents, unsigned int size, | |
33290528 | 8140 | char *filename) |
3de25479 | 8141 | { |
1b9b4cf4 | 8142 | size_t width, height; |
d66c4c7c | 8143 | MagickBooleanType status; |
3de25479 | 8144 | XImagePtr ximg; |
1b9b4cf4 CY |
8145 | int x, y; |
8146 | MagickWand *image_wand; | |
3de25479 | 8147 | PixelIterator *iterator; |
1b9b4cf4 | 8148 | PixelWand **pixels, *bg_wand = NULL; |
3de25479 | 8149 | MagickPixelPacket pixel; |
4917006b | 8150 | Lisp_Object image; |
7574650a | 8151 | Lisp_Object value; |
33290528 | 8152 | Lisp_Object crop; |
13464394 | 8153 | EMACS_INT ino; |
4917006b JV |
8154 | int desired_width, desired_height; |
8155 | double rotation; | |
7574650a | 8156 | int pixelwidth; |
01c3051f | 8157 | char hint_buffer[MaxTextExtent]; |
8259030d | 8158 | char *filename_hint = NULL; |
7574650a | 8159 | |
a32d4040 CY |
8160 | /* Handle image index for image types who can contain more than one image. |
8161 | Interface :index is same as for GIF. First we "ping" the image to see how | |
8162 | many sub-images it contains. Pinging is faster than loading the image to | |
8163 | find out things about it. */ | |
f853f599 | 8164 | |
a32d4040 | 8165 | /* Initialize the imagemagick environment. */ |
f853f599 | 8166 | MagickWandGenesis (); |
3de25479 JV |
8167 | image = image_spec_value (img->spec, QCindex, NULL); |
8168 | ino = INTEGERP (image) ? XFASTINT (image) : 0; | |
49ceb676 | 8169 | image_wand = NewMagickWand (); |
fa4fdb5c | 8170 | |
01c3051f | 8171 | if (filename) |
49ceb676 | 8172 | status = MagickReadImage (image_wand, filename); |
01c3051f PE |
8173 | else |
8174 | { | |
8175 | filename_hint = imagemagick_filename_hint (img->spec, hint_buffer); | |
49ceb676 LMI |
8176 | MagickSetFilename (image_wand, filename_hint); |
8177 | status = MagickReadImageBlob (image_wand, contents, size); | |
01c3051f | 8178 | } |
7574650a | 8179 | |
d1d7b339 JL |
8180 | if (status == MagickFalse) |
8181 | { | |
49ceb676 LMI |
8182 | imagemagick_error (image_wand); |
8183 | DestroyMagickWand (image_wand); | |
d1d7b339 JL |
8184 | return 0; |
8185 | } | |
fa4fdb5c | 8186 | |
49ceb676 | 8187 | if (ino < 0 || ino >= MagickGetNumberImages (image_wand)) |
7574650a AS |
8188 | { |
8189 | image_error ("Invalid image number `%s' in image `%s'", | |
8190 | image, img->spec); | |
49ceb676 | 8191 | DestroyMagickWand (image_wand); |
7574650a | 8192 | return 0; |
f3700365 | 8193 | } |
431dab04 | 8194 | |
49ceb676 | 8195 | if (MagickGetNumberImages (image_wand) > 1) |
b50691aa | 8196 | img->lisp_data = |
d2a2d277 | 8197 | Fcons (Qcount, |
49ceb676 | 8198 | Fcons (make_number (MagickGetNumberImages (image_wand)), |
b50691aa | 8199 | img->lisp_data)); |
f3700365 | 8200 | |
4ce82fb8 LMI |
8201 | /* If we have an animated image, get the new wand based on the |
8202 | "super-wand". */ | |
6e856b69 | 8203 | if (MagickGetNumberImages (image_wand) > 1) |
4ce82fb8 LMI |
8204 | { |
8205 | MagickWand *super_wand = image_wand; | |
6e856b69 LMI |
8206 | image_wand = imagemagick_compute_animated_image (super_wand, ino); |
8207 | if (! image_wand) | |
8208 | image_wand = super_wand; | |
8209 | else | |
8210 | DestroyMagickWand (super_wand); | |
4ce82fb8 LMI |
8211 | } |
8212 | ||
1b9b4cf4 CY |
8213 | /* Retrieve the frame's background color, for use later. */ |
8214 | { | |
8215 | XColor bgcolor; | |
8216 | Lisp_Object specified_bg; | |
8217 | ||
8218 | specified_bg = image_spec_value (img->spec, QCbackground, NULL); | |
8219 | if (!STRINGP (specified_bg) | |
8220 | || !x_defined_color (f, SSDATA (specified_bg), &bgcolor, 0)) | |
8221 | { | |
8222 | #ifndef HAVE_NS | |
8223 | bgcolor.pixel = FRAME_BACKGROUND_PIXEL (f); | |
8224 | x_query_color (f, &bgcolor); | |
8225 | #else | |
8226 | ns_query_color (FRAME_BACKGROUND_COLOR (f), &bgcolor, 1); | |
8227 | #endif | |
8228 | } | |
8229 | ||
8230 | bg_wand = NewPixelWand (); | |
8231 | PixelSetRed (bg_wand, (double) bgcolor.red / 65535); | |
8232 | PixelSetGreen (bg_wand, (double) bgcolor.green / 65535); | |
8233 | PixelSetBlue (bg_wand, (double) bgcolor.blue / 65535); | |
8234 | } | |
8235 | ||
f3f9606c LMI |
8236 | compute_image_size (MagickGetImageWidth (image_wand), |
8237 | MagickGetImageHeight (image_wand), | |
8238 | img->spec, &desired_width, &desired_height); | |
bdf6a35d | 8239 | |
a32d4040 | 8240 | if (desired_width != -1 && desired_height != -1) |
4917006b | 8241 | { |
7574650a AS |
8242 | status = MagickScaleImage (image_wand, desired_width, desired_height); |
8243 | if (status == MagickFalse) | |
8244 | { | |
8245 | image_error ("Imagemagick scale failed", Qnil, Qnil); | |
d1d7b339 | 8246 | imagemagick_error (image_wand); |
7574650a AS |
8247 | goto imagemagick_error; |
8248 | } | |
3de25479 | 8249 | } |
3de25479 | 8250 | |
f3700365 | 8251 | /* crop behaves similar to image slicing in Emacs but is more memory |
7574650a AS |
8252 | efficient. */ |
8253 | crop = image_spec_value (img->spec, QCcrop, NULL); | |
8254 | ||
13464394 | 8255 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop))) |
7574650a | 8256 | { |
a32d4040 CY |
8257 | /* After some testing, it seems MagickCropImage is the fastest crop |
8258 | function in ImageMagick. This crop function seems to do less copying | |
8259 | than the alternatives, but it still reads the entire image into memory | |
13464394 | 8260 | before cropping, which is apparently difficult to avoid when using |
a32d4040 | 8261 | imagemagick. */ |
13464394 | 8262 | size_t crop_width = XINT (XCAR (crop)); |
7574650a | 8263 | crop = XCDR (crop); |
13464394 | 8264 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop))) |
7574650a | 8265 | { |
13464394 | 8266 | size_t crop_height = XINT (XCAR (crop)); |
7574650a | 8267 | crop = XCDR (crop); |
13464394 | 8268 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop))) |
7574650a | 8269 | { |
13464394 | 8270 | ssize_t crop_x = XINT (XCAR (crop)); |
7574650a | 8271 | crop = XCDR (crop); |
13464394 | 8272 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop))) |
7574650a | 8273 | { |
13464394 PE |
8274 | ssize_t crop_y = XINT (XCAR (crop)); |
8275 | MagickCropImage (image_wand, crop_width, crop_height, | |
8276 | crop_x, crop_y); | |
7574650a AS |
8277 | } |
8278 | } | |
8279 | } | |
8280 | } | |
8281 | ||
d2a2d277 JV |
8282 | /* Furthermore :rotation. we need background color and angle for |
8283 | rotation. */ | |
3de25479 | 8284 | /* |
d2a2d277 JV |
8285 | TODO background handling for rotation specified_bg = |
8286 | image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP | |
549a73b9 | 8287 | (specified_bg). */ |
4917006b JV |
8288 | value = image_spec_value (img->spec, QCrotation, NULL); |
8289 | if (FLOATP (value)) | |
8290 | { | |
3de25479 | 8291 | rotation = extract_float (value); |
1b9b4cf4 | 8292 | status = MagickRotateImage (image_wand, bg_wand, rotation); |
4917006b JV |
8293 | if (status == MagickFalse) |
8294 | { | |
8295 | image_error ("Imagemagick image rotate failed", Qnil, Qnil); | |
d1d7b339 | 8296 | imagemagick_error (image_wand); |
4917006b JV |
8297 | goto imagemagick_error; |
8298 | } | |
3de25479 | 8299 | } |
7574650a | 8300 | |
1b9b4cf4 CY |
8301 | /* Set the canvas background color to the frame or specified |
8302 | background, and flatten the image. Note: as of ImageMagick | |
8303 | 6.6.0, SVG image transparency is not handled properly | |
8304 | (e.g. etc/images/splash.svg shows a white background always). */ | |
8305 | { | |
8306 | MagickWand *new_wand; | |
8307 | MagickSetImageBackgroundColor (image_wand, bg_wand); | |
0e25d334 | 8308 | #ifdef HAVE_MAGICKMERGEIMAGELAYERS |
1b9b4cf4 | 8309 | new_wand = MagickMergeImageLayers (image_wand, MergeLayer); |
0e25d334 CY |
8310 | #else |
8311 | new_wand = MagickFlattenImages (image_wand); | |
8312 | #endif | |
1b9b4cf4 CY |
8313 | DestroyMagickWand (image_wand); |
8314 | image_wand = new_wand; | |
8315 | } | |
8316 | ||
84ac6f9d PE |
8317 | /* Finally we are done manipulating the image. Figure out the |
8318 | resulting width/height and transfer ownership to Emacs. */ | |
8319 | height = MagickGetImageHeight (image_wand); | |
8320 | width = MagickGetImageWidth (image_wand); | |
8321 | ||
3f791afe PE |
8322 | if (! (width <= INT_MAX && height <= INT_MAX |
8323 | && check_image_size (f, width, height))) | |
3de25479 JV |
8324 | { |
8325 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
8326 | goto imagemagick_error; | |
8327 | } | |
7574650a | 8328 | |
3de25479 JV |
8329 | /* We can now get a valid pixel buffer from the imagemagick file, if all |
8330 | went ok. */ | |
3de25479 JV |
8331 | |
8332 | init_color_table (); | |
af008560 | 8333 | |
cf7a0de6 | 8334 | #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS) |
0e25d334 CY |
8335 | if (imagemagick_render_type != 0) |
8336 | { | |
8337 | /* Magicexportimage is normally faster than pixelpushing. This | |
8338 | method is also well tested. Some aspects of this method are | |
8339 | ad-hoc and needs to be more researched. */ | |
8340 | int imagedepth = 24; /*MagickGetImageDepth(image_wand);*/ | |
8341 | const char *exportdepth = imagedepth <= 8 ? "I" : "BGRP"; /*"RGBP";*/ | |
8342 | /* Try to create a x pixmap to hold the imagemagick pixmap. */ | |
547c9269 YM |
8343 | if (!image_create_x_image_and_pixmap (f, img, width, height, imagedepth, |
8344 | &ximg, 0)) | |
0e25d334 CY |
8345 | { |
8346 | #ifdef COLOR_TABLE_SUPPORT | |
8347 | free_color_table (); | |
8348 | #endif | |
8349 | image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil); | |
8350 | goto imagemagick_error; | |
8351 | } | |
8352 | ||
8353 | /* Oddly, the below code doesn't seem to work:*/ | |
8354 | /* switch(ximg->bitmap_unit){ */ | |
8355 | /* case 8: */ | |
8356 | /* pixelwidth=CharPixel; */ | |
8357 | /* break; */ | |
8358 | /* case 16: */ | |
8359 | /* pixelwidth=ShortPixel; */ | |
8360 | /* break; */ | |
8361 | /* case 32: */ | |
8362 | /* pixelwidth=LongPixel; */ | |
8363 | /* break; */ | |
8364 | /* } */ | |
8365 | /* | |
8366 | Here im just guessing the format of the bitmap. | |
8367 | happens to work fine for: | |
8368 | - bw djvu images | |
8369 | on rgb display. | |
8370 | seems about 3 times as fast as pixel pushing(not carefully measured) | |
8371 | */ | |
8372 | pixelwidth = CharPixel; /*??? TODO figure out*/ | |
8373 | MagickExportImagePixels (image_wand, 0, 0, width, height, | |
8374 | exportdepth, pixelwidth, ximg->data); | |
8375 | } | |
8376 | else | |
8377 | #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */ | |
4917006b | 8378 | { |
13464394 PE |
8379 | size_t image_height; |
8380 | ||
4917006b | 8381 | /* Try to create a x pixmap to hold the imagemagick pixmap. */ |
547c9269 YM |
8382 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, |
8383 | &ximg, 0)) | |
4917006b | 8384 | { |
466cbae9 AS |
8385 | #ifdef COLOR_TABLE_SUPPORT |
8386 | free_color_table (); | |
8387 | #endif | |
5e617bc2 | 8388 | image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil); |
4917006b JV |
8389 | goto imagemagick_error; |
8390 | } | |
7574650a | 8391 | |
53964682 | 8392 | /* Copy imagemagick image to x with primitive yet robust pixel |
4917006b | 8393 | pusher loop. This has been tested a lot with many different |
549a73b9 | 8394 | images. */ |
7574650a | 8395 | |
4917006b JV |
8396 | /* Copy pixels from the imagemagick image structure to the x image map. */ |
8397 | iterator = NewPixelIterator (image_wand); | |
7d652d97 | 8398 | if (! iterator) |
4917006b | 8399 | { |
466cbae9 AS |
8400 | #ifdef COLOR_TABLE_SUPPORT |
8401 | free_color_table (); | |
8402 | #endif | |
8403 | x_destroy_x_image (ximg); | |
d2a2d277 JV |
8404 | image_error ("Imagemagick pixel iterator creation failed", |
8405 | Qnil, Qnil); | |
3de25479 | 8406 | goto imagemagick_error; |
4917006b JV |
8407 | } |
8408 | ||
13464394 PE |
8409 | image_height = MagickGetImageHeight (image_wand); |
8410 | for (y = 0; y < image_height; y++) | |
4917006b JV |
8411 | { |
8412 | pixels = PixelGetNextIteratorRow (iterator, &width); | |
7d652d97 | 8413 | if (! pixels) |
4917006b JV |
8414 | break; |
8415 | for (x = 0; x < (long) width; x++) | |
8416 | { | |
8417 | PixelGetMagickColor (pixels[x], &pixel); | |
d2a2d277 JV |
8418 | XPutPixel (ximg, x, y, |
8419 | lookup_rgb_color (f, | |
8420 | pixel.red, | |
8421 | pixel.green, | |
8422 | pixel.blue)); | |
4917006b JV |
8423 | } |
8424 | } | |
8425 | DestroyPixelIterator (iterator); | |
3de25479 | 8426 | } |
3ed42149 | 8427 | |
3de25479 JV |
8428 | #ifdef COLOR_TABLE_SUPPORT |
8429 | /* Remember colors allocated for this image. */ | |
8430 | img->colors = colors_in_color_table (&img->ncolors); | |
8431 | free_color_table (); | |
8432 | #endif /* COLOR_TABLE_SUPPORT */ | |
8433 | ||
3de25479 JV |
8434 | img->width = width; |
8435 | img->height = height; | |
8436 | ||
547c9269 YM |
8437 | /* Put ximg into the image. */ |
8438 | image_put_x_image (f, img, ximg, 0); | |
3de25479 | 8439 | |
f3700365 | 8440 | /* Final cleanup. image_wand should be the only resource left. */ |
3de25479 | 8441 | DestroyMagickWand (image_wand); |
1b9b4cf4 CY |
8442 | if (bg_wand) DestroyPixelWand (bg_wand); |
8443 | ||
03d32f1b | 8444 | /* `MagickWandTerminus' terminates the imagemagick environment. */ |
f853f599 | 8445 | MagickWandTerminus (); |
3de25479 JV |
8446 | |
8447 | return 1; | |
8448 | ||
8449 | imagemagick_error: | |
466cbae9 | 8450 | DestroyMagickWand (image_wand); |
1b9b4cf4 CY |
8451 | if (bg_wand) DestroyPixelWand (bg_wand); |
8452 | ||
f853f599 | 8453 | MagickWandTerminus (); |
3de25479 JV |
8454 | /* TODO more cleanup. */ |
8455 | image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec, Qnil); | |
3de25479 JV |
8456 | return 0; |
8457 | } | |
8458 | ||
4917006b | 8459 | |
578098f3 | 8460 | /* Load IMAGEMAGICK image IMG for use on frame F. Value is true if |
4917006b JV |
8461 | successful. this function will go into the imagemagick_type structure, and |
8462 | the prototype thus needs to be compatible with that structure. */ | |
8463 | ||
578098f3 | 8464 | static bool |
d66c4c7c | 8465 | imagemagick_load (struct frame *f, struct image *img) |
4917006b | 8466 | { |
578098f3 | 8467 | bool success_p = 0; |
4917006b JV |
8468 | Lisp_Object file_name; |
8469 | ||
8470 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
8471 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
8472 | if (STRINGP (file_name)) | |
8473 | { | |
8474 | Lisp_Object file; | |
4917006b JV |
8475 | |
8476 | file = x_find_image_file (file_name); | |
4917006b JV |
8477 | if (!STRINGP (file)) |
8478 | { | |
8479 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
4917006b JV |
8480 | return 0; |
8481 | } | |
33290528 | 8482 | success_p = imagemagick_load_image (f, img, 0, 0, SSDATA (file)); |
4917006b JV |
8483 | } |
8484 | /* Else its not a file, its a lisp object. Load the image from a | |
8485 | lisp object rather than a file. */ | |
8486 | else | |
8487 | { | |
8488 | Lisp_Object data; | |
8489 | ||
8490 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
8491 | if (!STRINGP (data)) |
8492 | { | |
8493 | image_error ("Invalid image data `%s'", data, Qnil); | |
8494 | return 0; | |
8495 | } | |
d2a2d277 JV |
8496 | success_p = imagemagick_load_image (f, img, SDATA (data), |
8497 | SBYTES (data), NULL); | |
4917006b JV |
8498 | } |
8499 | ||
8500 | return success_p; | |
8501 | } | |
8502 | ||
a7ca3326 | 8503 | DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0, |
d66c4c7c CY |
8504 | doc: /* Return a list of image types supported by ImageMagick. |
8505 | Each entry in this list is a symbol named after an ImageMagick format | |
8506 | tag. See the ImageMagick manual for a list of ImageMagick formats and | |
8507 | their descriptions (http://www.imagemagick.org/script/formats.php). | |
dd605cc4 | 8508 | You can also try the shell command: `identify -list format'. |
d66c4c7c CY |
8509 | |
8510 | Note that ImageMagick recognizes many file-types that Emacs does not | |
32d72c2f GM |
8511 | recognize as images, such as C. See `imagemagick-types-enable' |
8512 | and `imagemagick-types-inhibit'. */) | |
a8101f66 | 8513 | (void) |
3de25479 JV |
8514 | { |
8515 | Lisp_Object typelist = Qnil; | |
1ab0dee5 | 8516 | size_t numf = 0; |
3de25479 | 8517 | ExceptionInfo ex; |
2277de02 | 8518 | char **imtypes; |
3f791afe | 8519 | size_t i; |
3de25479 | 8520 | Lisp_Object Qimagemagicktype; |
2277de02 JD |
8521 | |
8522 | GetExceptionInfo(&ex); | |
8523 | imtypes = GetMagickList ("*", &numf, &ex); | |
8524 | DestroyExceptionInfo(&ex); | |
8525 | ||
4917006b JV |
8526 | for (i = 0; i < numf; i++) |
8527 | { | |
8528 | Qimagemagicktype = intern (imtypes[i]); | |
f3700365 JV |
8529 | typelist = Fcons (Qimagemagicktype, typelist); |
8530 | } | |
d1d7b339 | 8531 | return Fnreverse (typelist); |
3de25479 | 8532 | } |
7574650a | 8533 | |
3de25479 JV |
8534 | #endif /* defined (HAVE_IMAGEMAGICK) */ |
8535 | ||
8536 | ||
4ef0d4d0 | 8537 | \f |
4ab27a43 GM |
8538 | /*********************************************************************** |
8539 | SVG | |
8540 | ***********************************************************************/ | |
8541 | ||
8542 | #if defined (HAVE_RSVG) | |
8543 | ||
8544 | /* Function prototypes. */ | |
8545 | ||
578098f3 PE |
8546 | static bool svg_image_p (Lisp_Object object); |
8547 | static bool svg_load (struct frame *f, struct image *img); | |
4ab27a43 | 8548 | |
578098f3 PE |
8549 | static bool svg_load_image (struct frame *, struct image *, |
8550 | unsigned char *, ptrdiff_t); | |
4ab27a43 GM |
8551 | |
8552 | /* The symbol `svg' identifying images of this type. */ | |
8553 | ||
4b66faf3 | 8554 | static Lisp_Object Qsvg; |
4ab27a43 GM |
8555 | |
8556 | /* Indices of image specification fields in svg_format, below. */ | |
8557 | ||
8558 | enum svg_keyword_index | |
8559 | { | |
8560 | SVG_TYPE, | |
8561 | SVG_DATA, | |
8562 | SVG_FILE, | |
8563 | SVG_ASCENT, | |
8564 | SVG_MARGIN, | |
8565 | SVG_RELIEF, | |
8566 | SVG_ALGORITHM, | |
8567 | SVG_HEURISTIC_MASK, | |
8568 | SVG_MASK, | |
8569 | SVG_BACKGROUND, | |
8570 | SVG_LAST | |
8571 | }; | |
8572 | ||
8573 | /* Vector of image_keyword structures describing the format | |
8574 | of valid user-defined image specifications. */ | |
8575 | ||
91433552 | 8576 | static const struct image_keyword svg_format[SVG_LAST] = |
4ab27a43 GM |
8577 | { |
8578 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8579 | {":data", IMAGE_STRING_VALUE, 0}, | |
8580 | {":file", IMAGE_STRING_VALUE, 0}, | |
8581 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 8582 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ab27a43 GM |
8583 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
8584 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8585 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8586 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8587 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8588 | }; | |
8589 | ||
33d4113c | 8590 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 8591 | static bool init_svg_functions (void); |
bb4d86b4 CY |
8592 | #else |
8593 | #define init_svg_functions NULL | |
8594 | #endif | |
8595 | ||
4ab27a43 GM |
8596 | /* Structure describing the image type `svg'. Its the same type of |
8597 | structure defined for all image formats, handled by emacs image | |
8598 | functions. See struct image_type in dispextern.h. */ | |
8599 | ||
8600 | static struct image_type svg_type = | |
8601 | { | |
4ab27a43 | 8602 | &Qsvg, |
4ab27a43 | 8603 | svg_image_p, |
4ab27a43 | 8604 | svg_load, |
4ab27a43 | 8605 | x_clear_image, |
bb4d86b4 | 8606 | init_svg_functions, |
c21e6151 | 8607 | NULL |
4ab27a43 GM |
8608 | }; |
8609 | ||
8610 | ||
578098f3 | 8611 | /* Return true if OBJECT is a valid SVG image specification. Do |
4ab27a43 GM |
8612 | this by calling parse_image_spec and supplying the keywords that |
8613 | identify the SVG format. */ | |
8614 | ||
578098f3 | 8615 | static bool |
d3da34e0 | 8616 | svg_image_p (Lisp_Object object) |
4ab27a43 GM |
8617 | { |
8618 | struct image_keyword fmt[SVG_LAST]; | |
72af86bd | 8619 | memcpy (fmt, svg_format, sizeof fmt); |
4ab27a43 GM |
8620 | |
8621 | if (!parse_image_spec (object, fmt, SVG_LAST, Qsvg)) | |
8622 | return 0; | |
8623 | ||
8624 | /* Must specify either the :data or :file keyword. */ | |
8625 | return fmt[SVG_FILE].count + fmt[SVG_DATA].count == 1; | |
8626 | } | |
8627 | ||
8628 | #include <librsvg/rsvg.h> | |
8629 | ||
0fda9b75 | 8630 | #ifdef WINDOWSNT |
5fc9fdeb JR |
8631 | |
8632 | /* SVG library functions. */ | |
40693bba | 8633 | DEF_IMGLIB_FN (RsvgHandle *, rsvg_handle_new, (void)); |
8634 | DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions, (RsvgHandle *, RsvgDimensionData *)); | |
8635 | DEF_IMGLIB_FN (gboolean, rsvg_handle_write, (RsvgHandle *, const guchar *, gsize, GError **)); | |
8636 | DEF_IMGLIB_FN (gboolean, rsvg_handle_close, (RsvgHandle *, GError **)); | |
8637 | DEF_IMGLIB_FN (GdkPixbuf *, rsvg_handle_get_pixbuf, (RsvgHandle *)); | |
8638 | DEF_IMGLIB_FN (void *, rsvg_handle_set_size_callback, (RsvgHandle *, RsvgSizeFunc, gpointer, GDestroyNotify)); | |
8639 | ||
8640 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_width, (const GdkPixbuf *)); | |
8641 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_height, (const GdkPixbuf *)); | |
8642 | DEF_IMGLIB_FN (guchar *, gdk_pixbuf_get_pixels, (const GdkPixbuf *)); | |
8643 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride, (const GdkPixbuf *)); | |
8644 | DEF_IMGLIB_FN (GdkColorspace, gdk_pixbuf_get_colorspace, (const GdkPixbuf *)); | |
8645 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels, (const GdkPixbuf *)); | |
8646 | DEF_IMGLIB_FN (gboolean, gdk_pixbuf_get_has_alpha, (const GdkPixbuf *)); | |
8647 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample, (const GdkPixbuf *)); | |
8648 | ||
19452b86 | 8649 | #if ! GLIB_CHECK_VERSION (2, 36, 0) |
40693bba | 8650 | DEF_IMGLIB_FN (void, g_type_init, (void)); |
19452b86 | 8651 | #endif |
40693bba | 8652 | DEF_IMGLIB_FN (void, g_object_unref, (gpointer)); |
121ab1cd | 8653 | DEF_IMGLIB_FN (void, g_error_free, (GError *)); |
5fc9fdeb | 8654 | |
df23bf08 | 8655 | Lisp_Object Qgdk_pixbuf, Qglib, Qgobject; |
5fc9fdeb | 8656 | |
578098f3 | 8657 | static bool |
d07ff9db | 8658 | init_svg_functions (void) |
5fc9fdeb | 8659 | { |
df23bf08 | 8660 | HMODULE library, gdklib, glib, gobject; |
5fc9fdeb | 8661 | |
d07ff9db CY |
8662 | if (!(glib = w32_delayed_load (Qglib)) |
8663 | || !(gobject = w32_delayed_load (Qgobject)) | |
8664 | || !(gdklib = w32_delayed_load (Qgdk_pixbuf)) | |
8665 | || !(library = w32_delayed_load (Qsvg))) | |
5fc9fdeb JR |
8666 | return 0; |
8667 | ||
8668 | LOAD_IMGLIB_FN (library, rsvg_handle_new); | |
e025c9c2 | 8669 | LOAD_IMGLIB_FN (library, rsvg_handle_get_dimensions); |
5fc9fdeb JR |
8670 | LOAD_IMGLIB_FN (library, rsvg_handle_write); |
8671 | LOAD_IMGLIB_FN (library, rsvg_handle_close); | |
8672 | LOAD_IMGLIB_FN (library, rsvg_handle_get_pixbuf); | |
5fc9fdeb JR |
8673 | |
8674 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_width); | |
8675 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_height); | |
8676 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_pixels); | |
8677 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_rowstride); | |
8678 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_colorspace); | |
8679 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_n_channels); | |
8680 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_has_alpha); | |
8681 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_bits_per_sample); | |
8682 | ||
19452b86 | 8683 | #if ! GLIB_CHECK_VERSION (2, 36, 0) |
df23bf08 | 8684 | LOAD_IMGLIB_FN (gobject, g_type_init); |
19452b86 | 8685 | #endif |
df23bf08 | 8686 | LOAD_IMGLIB_FN (gobject, g_object_unref); |
5fc9fdeb | 8687 | LOAD_IMGLIB_FN (glib, g_error_free); |
df23bf08 | 8688 | |
5fc9fdeb JR |
8689 | return 1; |
8690 | } | |
4ab27a43 | 8691 | |
5fc9fdeb JR |
8692 | #else |
8693 | /* The following aliases for library functions allow dynamic loading | |
8694 | to be used on some platforms. */ | |
4ab27a43 | 8695 | #define fn_rsvg_handle_new rsvg_handle_new |
e025c9c2 | 8696 | #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions |
4ab27a43 GM |
8697 | #define fn_rsvg_handle_write rsvg_handle_write |
8698 | #define fn_rsvg_handle_close rsvg_handle_close | |
8699 | #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf | |
4ab27a43 | 8700 | |
5fc9fdeb JR |
8701 | #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width |
8702 | #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height | |
8703 | #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels | |
8704 | #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride | |
8705 | #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace | |
8706 | #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels | |
8707 | #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha | |
4ab27a43 GM |
8708 | #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample |
8709 | ||
19452b86 | 8710 | #if ! GLIB_CHECK_VERSION (2, 36, 0) |
5fc9fdeb | 8711 | #define fn_g_type_init g_type_init |
19452b86 | 8712 | #endif |
5fc9fdeb JR |
8713 | #define fn_g_object_unref g_object_unref |
8714 | #define fn_g_error_free g_error_free | |
0fda9b75 | 8715 | #endif /* !WINDOWSNT */ |
4ab27a43 | 8716 | |
578098f3 PE |
8717 | /* Load SVG image IMG for use on frame F. Value is true if |
8718 | successful. */ | |
4ab27a43 | 8719 | |
578098f3 | 8720 | static bool |
d3da34e0 | 8721 | svg_load (struct frame *f, struct image *img) |
4ab27a43 | 8722 | { |
578098f3 | 8723 | bool success_p = 0; |
4ab27a43 GM |
8724 | Lisp_Object file_name; |
8725 | ||
8726 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
8727 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
8728 | if (STRINGP (file_name)) | |
8729 | { | |
8730 | Lisp_Object file; | |
8731 | unsigned char *contents; | |
dd52fcea | 8732 | ptrdiff_t size; |
4ab27a43 GM |
8733 | |
8734 | file = x_find_image_file (file_name); | |
4ab27a43 | 8735 | if (!STRINGP (file)) |
c21e6151 YM |
8736 | { |
8737 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
c21e6151 YM |
8738 | return 0; |
8739 | } | |
8740 | ||
4ab27a43 | 8741 | /* Read the entire file into memory. */ |
33290528 | 8742 | contents = slurp_file (SSDATA (file), &size); |
4ab27a43 | 8743 | if (contents == NULL) |
c21e6151 YM |
8744 | { |
8745 | image_error ("Error loading SVG image `%s'", img->spec, Qnil); | |
c21e6151 YM |
8746 | return 0; |
8747 | } | |
4ab27a43 | 8748 | /* If the file was slurped into memory properly, parse it. */ |
c21e6151 | 8749 | success_p = svg_load_image (f, img, contents, size); |
4ab27a43 | 8750 | xfree (contents); |
4ab27a43 GM |
8751 | } |
8752 | /* Else its not a file, its a lisp object. Load the image from a | |
8753 | lisp object rather than a file. */ | |
8754 | else | |
8755 | { | |
8756 | Lisp_Object data; | |
8757 | ||
8758 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
8759 | if (!STRINGP (data)) |
8760 | { | |
8761 | image_error ("Invalid image data `%s'", data, Qnil); | |
8762 | return 0; | |
8763 | } | |
4ab27a43 GM |
8764 | success_p = svg_load_image (f, img, SDATA (data), SBYTES (data)); |
8765 | } | |
8766 | ||
8767 | return success_p; | |
8768 | } | |
8769 | ||
bbe6f2aa JB |
8770 | /* svg_load_image is a helper function for svg_load, which does the |
8771 | actual loading given contents and size, apart from frame and image | |
8772 | structures, passed from svg_load. | |
4ab27a43 | 8773 | |
bbe6f2aa | 8774 | Uses librsvg to do most of the image processing. |
c21e6151 | 8775 | |
578098f3 PE |
8776 | Returns true when successful. */ |
8777 | static bool | |
d3da34e0 JB |
8778 | svg_load_image (struct frame *f, /* Pointer to emacs frame structure. */ |
8779 | struct image *img, /* Pointer to emacs image structure. */ | |
8780 | unsigned char *contents, /* String containing the SVG XML data to be parsed. */ | |
dd52fcea | 8781 | ptrdiff_t size) /* Size of data in bytes. */ |
4ab27a43 GM |
8782 | { |
8783 | RsvgHandle *rsvg_handle; | |
e025c9c2 | 8784 | RsvgDimensionData dimension_data; |
33290528 | 8785 | GError *err = NULL; |
4ab27a43 GM |
8786 | GdkPixbuf *pixbuf; |
8787 | int width; | |
8788 | int height; | |
8789 | const guint8 *pixels; | |
8790 | int rowstride; | |
8791 | XImagePtr ximg; | |
c21e6151 | 8792 | Lisp_Object specified_bg; |
4ab27a43 GM |
8793 | XColor background; |
8794 | int x; | |
8795 | int y; | |
8796 | ||
19452b86 DA |
8797 | #if ! GLIB_CHECK_VERSION (2, 36, 0) |
8798 | /* g_type_init is a glib function that must be called prior to | |
8799 | using gnome type library functions (obsolete since 2.36.0). */ | |
5fc9fdeb | 8800 | fn_g_type_init (); |
19452b86 DA |
8801 | #endif |
8802 | ||
4ab27a43 | 8803 | /* Make a handle to a new rsvg object. */ |
df61c790 | 8804 | rsvg_handle = fn_rsvg_handle_new (); |
4ab27a43 GM |
8805 | |
8806 | /* Parse the contents argument and fill in the rsvg_handle. */ | |
33290528 PE |
8807 | fn_rsvg_handle_write (rsvg_handle, contents, size, &err); |
8808 | if (err) goto rsvg_error; | |
4ab27a43 GM |
8809 | |
8810 | /* The parsing is complete, rsvg_handle is ready to used, close it | |
8811 | for further writes. */ | |
33290528 PE |
8812 | fn_rsvg_handle_close (rsvg_handle, &err); |
8813 | if (err) goto rsvg_error; | |
e025c9c2 CY |
8814 | |
8815 | fn_rsvg_handle_get_dimensions (rsvg_handle, &dimension_data); | |
8816 | if (! check_image_size (f, dimension_data.width, dimension_data.height)) | |
10ea2b82 JR |
8817 | { |
8818 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
8819 | goto rsvg_error; | |
8820 | } | |
b2996e57 | 8821 | |
4ab27a43 GM |
8822 | /* We can now get a valid pixel buffer from the svg file, if all |
8823 | went ok. */ | |
df61c790 | 8824 | pixbuf = fn_rsvg_handle_get_pixbuf (rsvg_handle); |
e025c9c2 CY |
8825 | if (!pixbuf) goto rsvg_error; |
8826 | fn_g_object_unref (rsvg_handle); | |
4ab27a43 GM |
8827 | |
8828 | /* Extract some meta data from the svg handle. */ | |
8829 | width = fn_gdk_pixbuf_get_width (pixbuf); | |
8830 | height = fn_gdk_pixbuf_get_height (pixbuf); | |
df61c790 | 8831 | pixels = fn_gdk_pixbuf_get_pixels (pixbuf); |
4ab27a43 GM |
8832 | rowstride = fn_gdk_pixbuf_get_rowstride (pixbuf); |
8833 | ||
8834 | /* Validate the svg meta data. */ | |
8835 | eassert (fn_gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB); | |
8836 | eassert (fn_gdk_pixbuf_get_n_channels (pixbuf) == 4); | |
8837 | eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf)); | |
8838 | eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf) == 8); | |
8839 | ||
8840 | /* Try to create a x pixmap to hold the svg pixmap. */ | |
547c9269 | 8841 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
c21e6151 | 8842 | { |
5fc9fdeb | 8843 | fn_g_object_unref (pixbuf); |
c21e6151 YM |
8844 | return 0; |
8845 | } | |
4ab27a43 GM |
8846 | |
8847 | init_color_table (); | |
8848 | ||
c21e6151 YM |
8849 | /* Handle alpha channel by combining the image with a background |
8850 | color. */ | |
8851 | specified_bg = image_spec_value (img->spec, QCbackground, NULL); | |
8d3c4e79 | 8852 | if (!STRINGP (specified_bg) |
33290528 | 8853 | || !x_defined_color (f, SSDATA (specified_bg), &background, 0)) |
c21e6151 | 8854 | { |
d7e008d0 | 8855 | #ifndef HAVE_NS |
c21e6151 YM |
8856 | background.pixel = FRAME_BACKGROUND_PIXEL (f); |
8857 | x_query_color (f, &background); | |
d7e008d0 | 8858 | #else |
ed3751c8 | 8859 | ns_query_color (FRAME_BACKGROUND_COLOR (f), &background, 1); |
d7e008d0 | 8860 | #endif |
c21e6151 | 8861 | } |
4ab27a43 | 8862 | |
8d3c4e79 CY |
8863 | /* SVG pixmaps specify transparency in the last byte, so right |
8864 | shift 8 bits to get rid of it, since emacs doesn't support | |
8865 | transparency. */ | |
8866 | background.red >>= 8; | |
8867 | background.green >>= 8; | |
8868 | background.blue >>= 8; | |
8869 | ||
4ab27a43 GM |
8870 | /* This loop handles opacity values, since Emacs assumes |
8871 | non-transparent images. Each pixel must be "flattened" by | |
bbe6f2aa JB |
8872 | calculating the resulting color, given the transparency of the |
8873 | pixel, and the image background color. */ | |
4ab27a43 GM |
8874 | for (y = 0; y < height; ++y) |
8875 | { | |
8876 | for (x = 0; x < width; ++x) | |
8877 | { | |
13464394 PE |
8878 | int red; |
8879 | int green; | |
8880 | int blue; | |
8881 | int opacity; | |
4ab27a43 GM |
8882 | |
8883 | red = *pixels++; | |
8884 | green = *pixels++; | |
8885 | blue = *pixels++; | |
8886 | opacity = *pixels++; | |
8887 | ||
8888 | red = ((red * opacity) | |
8889 | + (background.red * ((1 << 8) - opacity))); | |
8890 | green = ((green * opacity) | |
8891 | + (background.green * ((1 << 8) - opacity))); | |
8892 | blue = ((blue * opacity) | |
8893 | + (background.blue * ((1 << 8) - opacity))); | |
8894 | ||
8895 | XPutPixel (ximg, x, y, lookup_rgb_color (f, red, green, blue)); | |
8896 | } | |
8897 | ||
8898 | pixels += rowstride - 4 * width; | |
8899 | } | |
8900 | ||
8901 | #ifdef COLOR_TABLE_SUPPORT | |
8902 | /* Remember colors allocated for this image. */ | |
8903 | img->colors = colors_in_color_table (&img->ncolors); | |
8904 | free_color_table (); | |
8905 | #endif /* COLOR_TABLE_SUPPORT */ | |
8906 | ||
5fc9fdeb | 8907 | fn_g_object_unref (pixbuf); |
4ab27a43 | 8908 | |
c21e6151 YM |
8909 | img->width = width; |
8910 | img->height = height; | |
8911 | ||
8912 | /* Maybe fill in the background field while we have ximg handy. | |
8913 | Casting avoids a GCC warning. */ | |
8914 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
8915 | ||
547c9269 YM |
8916 | /* Put ximg into the image. */ |
8917 | image_put_x_image (f, img, ximg, 0); | |
4ab27a43 | 8918 | |
4ab27a43 GM |
8919 | return 1; |
8920 | ||
8921 | rsvg_error: | |
e025c9c2 | 8922 | fn_g_object_unref (rsvg_handle); |
4ab27a43 GM |
8923 | /* FIXME: Use error->message so the user knows what is the actual |
8924 | problem with the image. */ | |
8925 | image_error ("Error parsing SVG image `%s'", img->spec, Qnil); | |
33290528 | 8926 | fn_g_error_free (err); |
4ab27a43 GM |
8927 | return 0; |
8928 | } | |
8929 | ||
8930 | #endif /* defined (HAVE_RSVG) */ | |
8931 | ||
8932 | ||
8933 | ||
4ef0d4d0 KS |
8934 | \f |
8935 | /*********************************************************************** | |
8936 | Ghostscript | |
8937 | ***********************************************************************/ | |
8938 | ||
8939 | #ifdef HAVE_X_WINDOWS | |
8940 | #define HAVE_GHOSTSCRIPT 1 | |
8941 | #endif /* HAVE_X_WINDOWS */ | |
8942 | ||
4ef0d4d0 KS |
8943 | #ifdef HAVE_GHOSTSCRIPT |
8944 | ||
578098f3 PE |
8945 | static bool gs_image_p (Lisp_Object object); |
8946 | static bool gs_load (struct frame *f, struct image *img); | |
f57e2426 | 8947 | static void gs_clear_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
8948 | |
8949 | /* Keyword symbols. */ | |
8950 | ||
955cbe7b | 8951 | static Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height; |
4ef0d4d0 KS |
8952 | |
8953 | /* Indices of image specification fields in gs_format, below. */ | |
8954 | ||
8955 | enum gs_keyword_index | |
8956 | { | |
8957 | GS_TYPE, | |
8958 | GS_PT_WIDTH, | |
8959 | GS_PT_HEIGHT, | |
8960 | GS_FILE, | |
8961 | GS_LOADER, | |
8962 | GS_BOUNDING_BOX, | |
8963 | GS_ASCENT, | |
8964 | GS_MARGIN, | |
8965 | GS_RELIEF, | |
8966 | GS_ALGORITHM, | |
8967 | GS_HEURISTIC_MASK, | |
8968 | GS_MASK, | |
8969 | GS_BACKGROUND, | |
8970 | GS_LAST | |
8971 | }; | |
8972 | ||
8973 | /* Vector of image_keyword structures describing the format | |
8974 | of valid user-defined image specifications. */ | |
8975 | ||
91433552 | 8976 | static const struct image_keyword gs_format[GS_LAST] = |
4ef0d4d0 KS |
8977 | { |
8978 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8979 | {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8980 | {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8981 | {":file", IMAGE_STRING_VALUE, 1}, | |
8982 | {":loader", IMAGE_FUNCTION_VALUE, 0}, | |
8983 | {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1}, | |
8984 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 8985 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
8986 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
8987 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8988 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8989 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8990 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8991 | }; | |
8992 | ||
8993 | /* Structure describing the image type `ghostscript'. */ | |
8994 | ||
8995 | static struct image_type gs_type = | |
8996 | { | |
8997 | &Qpostscript, | |
8998 | gs_image_p, | |
8999 | gs_load, | |
9000 | gs_clear_image, | |
d07ff9db | 9001 | NULL, |
4ef0d4d0 KS |
9002 | NULL |
9003 | }; | |
9004 | ||
9005 | ||
9006 | /* Free X resources of Ghostscript image IMG which is used on frame F. */ | |
9007 | ||
9008 | static void | |
d3da34e0 | 9009 | gs_clear_image (struct frame *f, struct image *img) |
4ef0d4d0 | 9010 | { |
4ef0d4d0 KS |
9011 | x_clear_image (f, img); |
9012 | } | |
9013 | ||
9014 | ||
578098f3 | 9015 | /* Return true if OBJECT is a valid Ghostscript image |
4ef0d4d0 KS |
9016 | specification. */ |
9017 | ||
578098f3 | 9018 | static bool |
d3da34e0 | 9019 | gs_image_p (Lisp_Object object) |
4ef0d4d0 KS |
9020 | { |
9021 | struct image_keyword fmt[GS_LAST]; | |
9022 | Lisp_Object tem; | |
9023 | int i; | |
9024 | ||
72af86bd | 9025 | memcpy (fmt, gs_format, sizeof fmt); |
4ef0d4d0 KS |
9026 | |
9027 | if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)) | |
9028 | return 0; | |
9029 | ||
9030 | /* Bounding box must be a list or vector containing 4 integers. */ | |
9031 | tem = fmt[GS_BOUNDING_BOX].value; | |
9032 | if (CONSP (tem)) | |
9033 | { | |
9034 | for (i = 0; i < 4; ++i, tem = XCDR (tem)) | |
9035 | if (!CONSP (tem) || !INTEGERP (XCAR (tem))) | |
9036 | return 0; | |
9037 | if (!NILP (tem)) | |
9038 | return 0; | |
9039 | } | |
9040 | else if (VECTORP (tem)) | |
9041 | { | |
77b37c05 | 9042 | if (ASIZE (tem) != 4) |
4ef0d4d0 KS |
9043 | return 0; |
9044 | for (i = 0; i < 4; ++i) | |
28be1ada | 9045 | if (!INTEGERP (AREF (tem, i))) |
4ef0d4d0 KS |
9046 | return 0; |
9047 | } | |
9048 | else | |
9049 | return 0; | |
9050 | ||
9051 | return 1; | |
9052 | } | |
9053 | ||
9054 | ||
578098f3 | 9055 | /* Load Ghostscript image IMG for use on frame F. Value is true |
4ef0d4d0 KS |
9056 | if successful. */ |
9057 | ||
578098f3 | 9058 | static bool |
d3da34e0 | 9059 | gs_load (struct frame *f, struct image *img) |
4ef0d4d0 | 9060 | { |
13464394 PE |
9061 | uprintmax_t printnum1, printnum2; |
9062 | char buffer[sizeof " " + INT_STRLEN_BOUND (printmax_t)]; | |
4ef0d4d0 | 9063 | Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width; |
4ef0d4d0 KS |
9064 | Lisp_Object frame; |
9065 | double in_width, in_height; | |
9066 | Lisp_Object pixel_colors = Qnil; | |
9067 | ||
9068 | /* Compute pixel size of pixmap needed from the given size in the | |
9069 | image specification. Sizes in the specification are in pt. 1 pt | |
9070 | = 1/72 in, xdpi and ydpi are stored in the frame's X display | |
9071 | info. */ | |
9072 | pt_width = image_spec_value (img->spec, QCpt_width, NULL); | |
7574650a | 9073 | in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0; |
42143acd | 9074 | in_width *= FRAME_RES_X (f); |
4ef0d4d0 | 9075 | pt_height = image_spec_value (img->spec, QCpt_height, NULL); |
7574650a | 9076 | in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0; |
42143acd | 9077 | in_height *= FRAME_RES_Y (f); |
4ef0d4d0 | 9078 | |
13464394 PE |
9079 | if (! (in_width <= INT_MAX && in_height <= INT_MAX |
9080 | && check_image_size (f, in_width, in_height))) | |
f1f25b99 | 9081 | { |
10ea2b82 | 9082 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
9083 | return 0; |
9084 | } | |
13464394 PE |
9085 | img->width = in_width; |
9086 | img->height = in_height; | |
f1f25b99 | 9087 | |
4ef0d4d0 | 9088 | /* Create the pixmap. */ |
a54e2c05 | 9089 | eassert (img->pixmap == NO_PIXMAP); |
4ef0d4d0 | 9090 | |
ca4aa935 PE |
9091 | if (x_check_image_size (0, img->width, img->height)) |
9092 | { | |
9093 | /* Only W32 version did BLOCK_INPUT here. ++kfs */ | |
4d7e6e51 | 9094 | block_input (); |
ca4aa935 PE |
9095 | img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
9096 | img->width, img->height, | |
9097 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))); | |
4d7e6e51 | 9098 | unblock_input (); |
ca4aa935 | 9099 | } |
4ef0d4d0 KS |
9100 | |
9101 | if (!img->pixmap) | |
9102 | { | |
9103 | image_error ("Unable to create pixmap for `%s'", img->spec, Qnil); | |
9104 | return 0; | |
9105 | } | |
9106 | ||
9107 | /* Call the loader to fill the pixmap. It returns a process object | |
9108 | if successful. We do not record_unwind_protect here because | |
9109 | other places in redisplay like calling window scroll functions | |
9110 | don't either. Let the Lisp loader use `unwind-protect' instead. */ | |
13464394 PE |
9111 | printnum1 = FRAME_X_WINDOW (f); |
9112 | printnum2 = img->pixmap; | |
620f13b0 | 9113 | window_and_pixmap_id |
a8290ec3 | 9114 | = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2); |
4ef0d4d0 | 9115 | |
13464394 PE |
9116 | printnum1 = FRAME_FOREGROUND_PIXEL (f); |
9117 | printnum2 = FRAME_BACKGROUND_PIXEL (f); | |
a8290ec3 DA |
9118 | pixel_colors |
9119 | = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2); | |
4ef0d4d0 KS |
9120 | |
9121 | XSETFRAME (frame, f); | |
9122 | loader = image_spec_value (img->spec, QCloader, NULL); | |
9123 | if (NILP (loader)) | |
9124 | loader = intern ("gs-load-image"); | |
9125 | ||
b50691aa CY |
9126 | img->lisp_data = call6 (loader, frame, img->spec, |
9127 | make_number (img->width), | |
9128 | make_number (img->height), | |
9129 | window_and_pixmap_id, | |
9130 | pixel_colors); | |
9131 | return PROCESSP (img->lisp_data); | |
4ef0d4d0 KS |
9132 | } |
9133 | ||
9134 | ||
9135 | /* Kill the Ghostscript process that was started to fill PIXMAP on | |
9136 | frame F. Called from XTread_socket when receiving an event | |
9137 | telling Emacs that Ghostscript has finished drawing. */ | |
9138 | ||
9139 | void | |
d3da34e0 | 9140 | x_kill_gs_process (Pixmap pixmap, struct frame *f) |
4ef0d4d0 | 9141 | { |
354884c4 | 9142 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
13464394 PE |
9143 | int class; |
9144 | ptrdiff_t i; | |
4ef0d4d0 KS |
9145 | struct image *img; |
9146 | ||
9147 | /* Find the image containing PIXMAP. */ | |
9148 | for (i = 0; i < c->used; ++i) | |
9149 | if (c->images[i]->pixmap == pixmap) | |
9150 | break; | |
9151 | ||
9152 | /* Should someone in between have cleared the image cache, for | |
9153 | instance, give up. */ | |
9154 | if (i == c->used) | |
9155 | return; | |
9156 | ||
9157 | /* Kill the GS process. We should have found PIXMAP in the image | |
9158 | cache and its image should contain a process object. */ | |
9159 | img = c->images[i]; | |
a54e2c05 | 9160 | eassert (PROCESSP (img->lisp_data)); |
b50691aa CY |
9161 | Fkill_process (img->lisp_data, Qnil); |
9162 | img->lisp_data = Qnil; | |
4ef0d4d0 KS |
9163 | |
9164 | #if defined (HAVE_X_WINDOWS) | |
9165 | ||
9166 | /* On displays with a mutable colormap, figure out the colors | |
9167 | allocated for the image by looking at the pixels of an XImage for | |
9168 | img->pixmap. */ | |
9169 | class = FRAME_X_VISUAL (f)->class; | |
9170 | if (class != StaticColor && class != StaticGray && class != TrueColor) | |
9171 | { | |
9172 | XImagePtr ximg; | |
9173 | ||
4d7e6e51 | 9174 | block_input (); |
4ef0d4d0 KS |
9175 | |
9176 | /* Try to get an XImage for img->pixmep. */ | |
9177 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, | |
9178 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
9179 | if (ximg) | |
9180 | { | |
9181 | int x, y; | |
9182 | ||
9183 | /* Initialize the color table. */ | |
9184 | init_color_table (); | |
9185 | ||
9186 | /* For each pixel of the image, look its color up in the | |
9187 | color table. After having done so, the color table will | |
9188 | contain an entry for each color used by the image. */ | |
9189 | for (y = 0; y < img->height; ++y) | |
9190 | for (x = 0; x < img->width; ++x) | |
9191 | { | |
9192 | unsigned long pixel = XGetPixel (ximg, x, y); | |
9193 | lookup_pixel_color (f, pixel); | |
9194 | } | |
9195 | ||
9196 | /* Record colors in the image. Free color table and XImage. */ | |
9197 | #ifdef COLOR_TABLE_SUPPORT | |
9198 | img->colors = colors_in_color_table (&img->ncolors); | |
9199 | free_color_table (); | |
9200 | #endif | |
9201 | XDestroyImage (ximg); | |
9202 | ||
9203 | #if 0 /* This doesn't seem to be the case. If we free the colors | |
9204 | here, we get a BadAccess later in x_clear_image when | |
9205 | freeing the colors. */ | |
9206 | /* We have allocated colors once, but Ghostscript has also | |
9207 | allocated colors on behalf of us. So, to get the | |
9208 | reference counts right, free them once. */ | |
9209 | if (img->ncolors) | |
9210 | x_free_colors (f, img->colors, img->ncolors); | |
9211 | #endif | |
9212 | } | |
9213 | else | |
9214 | image_error ("Cannot get X image of `%s'; colors will not be freed", | |
9215 | img->spec, Qnil); | |
9216 | ||
4d7e6e51 | 9217 | unblock_input (); |
4ef0d4d0 KS |
9218 | } |
9219 | #endif /* HAVE_X_WINDOWS */ | |
9220 | ||
9221 | /* Now that we have the pixmap, compute mask and transform the | |
9222 | image if requested. */ | |
4d7e6e51 | 9223 | block_input (); |
4ef0d4d0 | 9224 | postprocess_image (f, img); |
4d7e6e51 | 9225 | unblock_input (); |
4ef0d4d0 KS |
9226 | } |
9227 | ||
9228 | #endif /* HAVE_GHOSTSCRIPT */ | |
9229 | ||
9230 | \f | |
9231 | /*********************************************************************** | |
9232 | Tests | |
9233 | ***********************************************************************/ | |
9234 | ||
e509cfa6 | 9235 | #ifdef GLYPH_DEBUG |
4ef0d4d0 | 9236 | |
a7ca3326 | 9237 | DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0, |
4ef0d4d0 | 9238 | doc: /* Value is non-nil if SPEC is a valid image specification. */) |
5842a27b | 9239 | (Lisp_Object spec) |
4ef0d4d0 KS |
9240 | { |
9241 | return valid_image_p (spec) ? Qt : Qnil; | |
9242 | } | |
9243 | ||
9244 | ||
a7ca3326 | 9245 | DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "") |
5842a27b | 9246 | (Lisp_Object spec) |
4ef0d4d0 | 9247 | { |
13464394 | 9248 | ptrdiff_t id = -1; |
4ef0d4d0 KS |
9249 | |
9250 | if (valid_image_p (spec)) | |
9251 | id = lookup_image (SELECTED_FRAME (), spec); | |
9252 | ||
9253 | debug_print (spec); | |
9254 | return make_number (id); | |
9255 | } | |
9256 | ||
e509cfa6 | 9257 | #endif /* GLYPH_DEBUG */ |
4ef0d4d0 KS |
9258 | |
9259 | ||
9260 | /*********************************************************************** | |
9261 | Initialization | |
9262 | ***********************************************************************/ | |
9263 | ||
d07ff9db | 9264 | DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 1, 1, 0, |
0855eb52 JB |
9265 | doc: /* Initialize image library implementing image type TYPE. |
9266 | Return non-nil if TYPE is a supported image type. | |
9267 | ||
d07ff9db CY |
9268 | If image libraries are loaded dynamically (currently only the case on |
9269 | MS-Windows), load the library for TYPE if it is not yet loaded, using | |
9270 | the library file(s) specified by `dynamic-library-alist'. */) | |
9271 | (Lisp_Object type) | |
0855eb52 | 9272 | { |
d07ff9db | 9273 | return lookup_image_type (type) ? Qt : Qnil; |
bb4d86b4 | 9274 | } |
0855eb52 | 9275 | |
bb4d86b4 | 9276 | /* Look up image type TYPE, and return a pointer to its image_type |
d07ff9db | 9277 | structure. Return 0 if TYPE is not a known image type. */ |
bb4d86b4 CY |
9278 | |
9279 | static struct image_type * | |
d07ff9db | 9280 | lookup_image_type (Lisp_Object type) |
bb4d86b4 | 9281 | { |
2f142cc5 | 9282 | /* Types pbm and xbm are built-in and always available. */ |
bb4d86b4 | 9283 | if (EQ (type, Qpbm)) |
d07ff9db | 9284 | return define_image_type (&pbm_type); |
bb4d86b4 CY |
9285 | |
9286 | if (EQ (type, Qxbm)) | |
d07ff9db | 9287 | return define_image_type (&xbm_type); |
2f142cc5 | 9288 | |
9e2a2647 | 9289 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
0855eb52 | 9290 | if (EQ (type, Qxpm)) |
d07ff9db | 9291 | return define_image_type (&xpm_type); |
0855eb52 JB |
9292 | #endif |
9293 | ||
9e2a2647 | 9294 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
0855eb52 | 9295 | if (EQ (type, Qjpeg)) |
d07ff9db | 9296 | return define_image_type (&jpeg_type); |
0855eb52 JB |
9297 | #endif |
9298 | ||
9e2a2647 | 9299 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
0855eb52 | 9300 | if (EQ (type, Qtiff)) |
d07ff9db | 9301 | return define_image_type (&tiff_type); |
0855eb52 JB |
9302 | #endif |
9303 | ||
9e2a2647 | 9304 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
0855eb52 | 9305 | if (EQ (type, Qgif)) |
d07ff9db | 9306 | return define_image_type (&gif_type); |
0855eb52 JB |
9307 | #endif |
9308 | ||
9e2a2647 | 9309 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
0855eb52 | 9310 | if (EQ (type, Qpng)) |
d07ff9db | 9311 | return define_image_type (&png_type); |
0855eb52 JB |
9312 | #endif |
9313 | ||
4ab27a43 GM |
9314 | #if defined (HAVE_RSVG) |
9315 | if (EQ (type, Qsvg)) | |
d07ff9db | 9316 | return define_image_type (&svg_type); |
4ab27a43 | 9317 | #endif |
c21e6151 | 9318 | |
3de25479 | 9319 | #if defined (HAVE_IMAGEMAGICK) |
7574650a | 9320 | if (EQ (type, Qimagemagick)) |
d07ff9db | 9321 | return define_image_type (&imagemagick_type); |
3de25479 JV |
9322 | #endif |
9323 | ||
0855eb52 JB |
9324 | #ifdef HAVE_GHOSTSCRIPT |
9325 | if (EQ (type, Qpostscript)) | |
d07ff9db | 9326 | return define_image_type (&gs_type); |
0855eb52 JB |
9327 | #endif |
9328 | ||
bb4d86b4 | 9329 | return NULL; |
0855eb52 JB |
9330 | } |
9331 | ||
aa15c6bb JB |
9332 | /* Reset image_types before dumping. |
9333 | Called from Fdump_emacs. */ | |
9334 | ||
9335 | void | |
9336 | reset_image_types (void) | |
9337 | { | |
9338 | while (image_types) | |
9339 | { | |
9340 | struct image_type *next = image_types->next; | |
9341 | xfree (image_types); | |
9342 | image_types = next; | |
9343 | } | |
0855eb52 JB |
9344 | } |
9345 | ||
4ef0d4d0 | 9346 | void |
d3da34e0 | 9347 | syms_of_image (void) |
4ef0d4d0 | 9348 | { |
aa15c6bb | 9349 | /* Initialize this only once; it will be reset before dumping. */ |
6e6fc3fd RS |
9350 | image_types = NULL; |
9351 | ||
91af3942 | 9352 | /* Must be defined now because we're going to update it below, while |
0855eb52 | 9353 | defining the supported image types. */ |
29208e82 | 9354 | DEFVAR_LISP ("image-types", Vimage_types, |
0855eb52 | 9355 | doc: /* List of potentially supported image types. |
bbe6f2aa | 9356 | Each element of the list is a symbol for an image type, like 'jpeg or 'png. |
0855eb52 JB |
9357 | To check whether it is really supported, use `image-type-available-p'. */); |
9358 | Vimage_types = Qnil; | |
9359 | ||
29208e82 | 9360 | DEFVAR_LISP ("max-image-size", Vmax_image_size, |
7df4765a KS |
9361 | doc: /* Maximum size of images. |
9362 | Emacs will not load an image into memory if its pixel width or | |
9363 | pixel height exceeds this limit. | |
9364 | ||
9365 | If the value is an integer, it directly specifies the maximum | |
9366 | image height and width, measured in pixels. If it is a floating | |
9367 | point number, it specifies the maximum image height and width | |
9368 | as a ratio to the frame height and width. If the value is | |
9369 | non-numeric, there is no explicit limit on the size of images. */); | |
f1f25b99 CY |
9370 | Vmax_image_size = make_float (MAX_IMAGE_SIZE); |
9371 | ||
1100a63c CY |
9372 | DEFSYM (Qcount, "count"); |
9373 | DEFSYM (Qextension_data, "extension-data"); | |
9374 | DEFSYM (Qdelay, "delay"); | |
9375 | ||
9376 | DEFSYM (QCascent, ":ascent"); | |
9377 | DEFSYM (QCmargin, ":margin"); | |
9378 | DEFSYM (QCrelief, ":relief"); | |
9379 | DEFSYM (QCconversion, ":conversion"); | |
9380 | DEFSYM (QCcolor_symbols, ":color-symbols"); | |
9381 | DEFSYM (QCheuristic_mask, ":heuristic-mask"); | |
9382 | DEFSYM (QCindex, ":index"); | |
9383 | DEFSYM (QCgeometry, ":geometry"); | |
9384 | DEFSYM (QCcrop, ":crop"); | |
9385 | DEFSYM (QCrotation, ":rotation"); | |
9386 | DEFSYM (QCmatrix, ":matrix"); | |
9387 | DEFSYM (QCcolor_adjustment, ":color-adjustment"); | |
9388 | DEFSYM (QCmask, ":mask"); | |
9389 | ||
9390 | DEFSYM (Qlaplace, "laplace"); | |
9391 | DEFSYM (Qemboss, "emboss"); | |
9392 | DEFSYM (Qedge_detection, "edge-detection"); | |
9393 | DEFSYM (Qheuristic, "heuristic"); | |
9394 | ||
9395 | DEFSYM (Qpostscript, "postscript"); | |
f3f9606c LMI |
9396 | DEFSYM (QCmax_width, ":max-width"); |
9397 | DEFSYM (QCmax_height, ":max-height"); | |
4ef0d4d0 | 9398 | #ifdef HAVE_GHOSTSCRIPT |
5fc9fdeb | 9399 | ADD_IMAGE_TYPE (Qpostscript); |
1100a63c CY |
9400 | DEFSYM (QCloader, ":loader"); |
9401 | DEFSYM (QCbounding_box, ":bounding-box"); | |
9402 | DEFSYM (QCpt_width, ":pt-width"); | |
9403 | DEFSYM (QCpt_height, ":pt-height"); | |
4ef0d4d0 KS |
9404 | #endif /* HAVE_GHOSTSCRIPT */ |
9405 | ||
5be1c984 | 9406 | #ifdef HAVE_NTGUI |
1100a63c | 9407 | DEFSYM (Qlibpng_version, "libpng-version"); |
0514b4be | 9408 | Fset (Qlibpng_version, |
5be1c984 | 9409 | #if HAVE_PNG |
0514b4be | 9410 | make_number (PNG_LIBPNG_VER) |
5be1c984 | 9411 | #else |
0514b4be | 9412 | make_number (-1) |
a5dab159 EZ |
9413 | #endif |
9414 | ); | |
9415 | DEFSYM (Qlibgif_version, "libgif-version"); | |
9416 | Fset (Qlibgif_version, | |
9417 | #ifdef HAVE_GIF | |
9418 | make_number (GIFLIB_MAJOR * 10000 | |
9419 | + GIFLIB_MINOR * 100 | |
9420 | + GIFLIB_RELEASE) | |
9421 | #else | |
9422 | make_number (-1) | |
d14c81ee CB |
9423 | #endif |
9424 | ); | |
9425 | DEFSYM (Qlibjpeg_version, "libjpeg-version"); | |
9426 | Fset (Qlibjpeg_version, | |
9427 | #if HAVE_JPEG | |
9428 | make_number (JPEG_LIB_VERSION) | |
9429 | #else | |
9430 | make_number (-1) | |
5be1c984 | 9431 | #endif |
0514b4be | 9432 | ); |
5be1c984 EZ |
9433 | #endif |
9434 | ||
bb4d86b4 CY |
9435 | DEFSYM (Qpbm, "pbm"); |
9436 | ADD_IMAGE_TYPE (Qpbm); | |
9437 | ||
9438 | DEFSYM (Qxbm, "xbm"); | |
9439 | ADD_IMAGE_TYPE (Qxbm); | |
9440 | ||
9e2a2647 | 9441 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
1100a63c | 9442 | DEFSYM (Qxpm, "xpm"); |
5fc9fdeb | 9443 | ADD_IMAGE_TYPE (Qxpm); |
4ef0d4d0 KS |
9444 | #endif |
9445 | ||
9e2a2647 | 9446 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
1100a63c | 9447 | DEFSYM (Qjpeg, "jpeg"); |
5fc9fdeb | 9448 | ADD_IMAGE_TYPE (Qjpeg); |
4ef0d4d0 KS |
9449 | #endif |
9450 | ||
9e2a2647 | 9451 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
1100a63c | 9452 | DEFSYM (Qtiff, "tiff"); |
5fc9fdeb | 9453 | ADD_IMAGE_TYPE (Qtiff); |
4ef0d4d0 KS |
9454 | #endif |
9455 | ||
9e2a2647 | 9456 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
1100a63c | 9457 | DEFSYM (Qgif, "gif"); |
5fc9fdeb | 9458 | ADD_IMAGE_TYPE (Qgif); |
4ef0d4d0 KS |
9459 | #endif |
9460 | ||
9e2a2647 | 9461 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
1100a63c | 9462 | DEFSYM (Qpng, "png"); |
5fc9fdeb | 9463 | ADD_IMAGE_TYPE (Qpng); |
4ef0d4d0 KS |
9464 | #endif |
9465 | ||
3de25479 | 9466 | #if defined (HAVE_IMAGEMAGICK) |
1100a63c | 9467 | DEFSYM (Qimagemagick, "imagemagick"); |
3de25479 JV |
9468 | ADD_IMAGE_TYPE (Qimagemagick); |
9469 | #endif | |
7574650a | 9470 | |
4ab27a43 | 9471 | #if defined (HAVE_RSVG) |
1100a63c | 9472 | DEFSYM (Qsvg, "svg"); |
5fc9fdeb JR |
9473 | ADD_IMAGE_TYPE (Qsvg); |
9474 | #ifdef HAVE_NTGUI | |
df23bf08 | 9475 | /* Other libraries used directly by svg code. */ |
1100a63c CY |
9476 | DEFSYM (Qgdk_pixbuf, "gdk-pixbuf"); |
9477 | DEFSYM (Qglib, "glib"); | |
9478 | DEFSYM (Qgobject, "gobject"); | |
5fc9fdeb JR |
9479 | #endif /* HAVE_NTGUI */ |
9480 | #endif /* HAVE_RSVG */ | |
4ab27a43 | 9481 | |
0855eb52 | 9482 | defsubr (&Sinit_image_library); |
7574650a | 9483 | #ifdef HAVE_IMAGEMAGICK |
3de25479 | 9484 | defsubr (&Simagemagick_types); |
7574650a | 9485 | #endif |
4ef0d4d0 | 9486 | defsubr (&Sclear_image_cache); |
110683ad | 9487 | defsubr (&Simage_flush); |
4ef0d4d0 KS |
9488 | defsubr (&Simage_size); |
9489 | defsubr (&Simage_mask_p); | |
1546c559 | 9490 | defsubr (&Simage_metadata); |
4ef0d4d0 | 9491 | |
e509cfa6 | 9492 | #ifdef GLYPH_DEBUG |
4ef0d4d0 KS |
9493 | defsubr (&Simagep); |
9494 | defsubr (&Slookup_image); | |
9495 | #endif | |
9496 | ||
29208e82 | 9497 | DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images, |
4ef0d4d0 | 9498 | doc: /* Non-nil means always draw a cross over disabled images. |
3046a84b | 9499 | Disabled images are those having a `:conversion disabled' property. |
4ef0d4d0 KS |
9500 | A cross is always drawn on black & white displays. */); |
9501 | cross_disabled_images = 0; | |
9502 | ||
29208e82 | 9503 | DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path, |
4ef0d4d0 | 9504 | doc: /* List of directories to search for window system bitmap files. */); |
7d652d97 | 9505 | Vx_bitmap_file_path = decode_env_path (0, PATH_BITMAPS); |
4ef0d4d0 | 9506 | |
29208e82 | 9507 | DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay, |
98fe5161 CY |
9508 | doc: /* Maximum time after which images are removed from the cache. |
9509 | When an image has not been displayed this many seconds, Emacs | |
9510 | automatically removes it from the image cache. If the cache contains | |
9511 | a large number of images, the actual eviction time may be shorter. | |
9512 | The value can also be nil, meaning the cache is never cleared. | |
9513 | ||
9514 | The function `clear-image-cache' disregards this variable. */); | |
9515 | Vimage_cache_eviction_delay = make_number (300); | |
7574650a | 9516 | #ifdef HAVE_IMAGEMAGICK |
af008560 GM |
9517 | DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type, |
9518 | doc: /* Integer indicating which ImageMagick rendering method to use. | |
9519 | The options are: | |
9520 | 0 -- the default method (pixel pushing) | |
9521 | 1 -- a newer method ("MagickExportImagePixels") that may perform | |
9522 | better (speed etc) in some cases, but has not been as thoroughly | |
9523 | tested with Emacs as the default method. This method requires | |
9524 | ImageMagick version 6.4.6 (approximately) or later. | |
9525 | */); | |
9526 | /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */ | |
9527 | imagemagick_render_type = 0; | |
7574650a | 9528 | #endif |
4ef0d4d0 | 9529 | |
4ef0d4d0 | 9530 | } |