Commit | Line | Data |
---|---|---|
4ef0d4d0 | 1 | /* Functions for image support on window system. |
32d72c2f | 2 | |
ba318903 | 3 | Copyright (C) 1989, 1992-2014 Free Software Foundation, Inc. |
4ef0d4d0 KS |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
4ef0d4d0 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
4ef0d4d0 KS |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ef0d4d0 KS |
19 | |
20 | #include <config.h> | |
406af475 | 21 | #include "sysstdio.h" |
4ef0d4d0 | 22 | #include <unistd.h> |
4ef0d4d0 | 23 | |
1ba38c42 PE |
24 | /* Include this before including <setjmp.h> to work around bugs with |
25 | older libpng; see Bug#17429. */ | |
26 | #if defined HAVE_PNG && !defined HAVE_NS | |
27 | # include <png.h> | |
28 | #endif | |
29 | ||
c3417a74 | 30 | #include <setjmp.h> |
620f13b0 PE |
31 | #include <c-ctype.h> |
32 | ||
4ef0d4d0 KS |
33 | #include "lisp.h" |
34 | #include "frame.h" | |
35 | #include "window.h" | |
36 | #include "dispextern.h" | |
37 | #include "blockinput.h" | |
38 | #include "systime.h" | |
39 | #include <epaths.h> | |
3dcc8b84 | 40 | #include "character.h" |
022af124 | 41 | #include "coding.h" |
6087292e | 42 | #include "termhooks.h" |
3dcc8b84 | 43 | #include "font.h" |
4ef0d4d0 | 44 | |
17a2cbbd | 45 | #ifdef HAVE_SYS_STAT_H |
4ef0d4d0 | 46 | #include <sys/stat.h> |
17a2cbbd DC |
47 | #endif /* HAVE_SYS_STAT_H */ |
48 | ||
49 | #ifdef HAVE_SYS_TYPES_H | |
50 | #include <sys/types.h> | |
51 | #endif /* HAVE_SYS_TYPES_H */ | |
52 | ||
53 | #ifdef HAVE_WINDOW_SYSTEM | |
54 | #include TERM_HEADER | |
55 | #endif /* HAVE_WINDOW_SYSTEM */ | |
4ef0d4d0 | 56 | |
17a2cbbd | 57 | #ifdef HAVE_X_WINDOWS |
4ef0d4d0 KS |
58 | #define COLOR_TABLE_SUPPORT 1 |
59 | ||
60 | typedef struct x_bitmap_record Bitmap_Record; | |
5e617bc2 | 61 | #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y) |
4ef0d4d0 | 62 | #define NO_PIXMAP None |
4ef0d4d0 | 63 | |
9ecf6403 YM |
64 | #define PIX_MASK_RETAIN 0 |
65 | #define PIX_MASK_DRAW 1 | |
4ef0d4d0 KS |
66 | #endif /* HAVE_X_WINDOWS */ |
67 | ||
4ef0d4d0 | 68 | #ifdef HAVE_NTGUI |
a9e7a9d5 PE |
69 | |
70 | /* We need (or want) w32.h only when we're _not_ compiling for Cygwin. */ | |
71 | #ifdef WINDOWSNT | |
0fda9b75 | 72 | # include "w32.h" |
a9e7a9d5 PE |
73 | #endif |
74 | ||
4ef0d4d0 KS |
75 | /* W32_TODO : Color tables on W32. */ |
76 | #undef COLOR_TABLE_SUPPORT | |
77 | ||
78 | typedef struct w32_bitmap_record Bitmap_Record; | |
5e617bc2 | 79 | #define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y) |
4ef0d4d0 | 80 | #define NO_PIXMAP 0 |
4ef0d4d0 | 81 | |
9ecf6403 YM |
82 | #define PIX_MASK_RETAIN 0 |
83 | #define PIX_MASK_DRAW 1 | |
4ef0d4d0 | 84 | |
4ef0d4d0 KS |
85 | #define x_defined_color w32_defined_color |
86 | #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits) | |
8b7d0a16 | 87 | |
d14c81ee CB |
88 | /* Versions of libpng, libgif, and libjpeg that we were compiled with, |
89 | or -1 if no PNG/GIF support was compiled in. This is tested by | |
90 | w32-win.el to correctly set up the alist used to search for the | |
91 | respective image libraries. */ | |
92 | Lisp_Object Qlibpng_version, Qlibgif_version, Qlibjpeg_version; | |
93 | ||
4ef0d4d0 KS |
94 | #endif /* HAVE_NTGUI */ |
95 | ||
edfda783 | 96 | #ifdef HAVE_NS |
edfda783 AR |
97 | #undef COLOR_TABLE_SUPPORT |
98 | ||
99 | typedef struct ns_bitmap_record Bitmap_Record; | |
100 | ||
5e617bc2 | 101 | #define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y) |
edfda783 AR |
102 | #define NO_PIXMAP 0 |
103 | ||
edfda783 AR |
104 | #define PIX_MASK_RETAIN 0 |
105 | #define PIX_MASK_DRAW 1 | |
106 | ||
edfda783 AR |
107 | #define x_defined_color(f, name, color_def, alloc) \ |
108 | ns_defined_color (f, name, color_def, alloc, 0) | |
9e50ff0c | 109 | #define DefaultDepthOfScreen(screen) x_display_list->n_planes |
edfda783 AR |
110 | #endif /* HAVE_NS */ |
111 | ||
112 | ||
97ec208f DN |
113 | /* The symbol `postscript' identifying images of this type. */ |
114 | ||
955cbe7b | 115 | static Lisp_Object Qpostscript; |
4ef0d4d0 | 116 | |
f57e2426 J |
117 | static void x_disable_image (struct frame *, struct image *); |
118 | static void x_edge_detection (struct frame *, struct image *, Lisp_Object, | |
119 | Lisp_Object); | |
4ef0d4d0 | 120 | |
f57e2426 J |
121 | static void init_color_table (void); |
122 | static unsigned long lookup_rgb_color (struct frame *f, int r, int g, int b); | |
4ef0d4d0 | 123 | #ifdef COLOR_TABLE_SUPPORT |
f57e2426 J |
124 | static void free_color_table (void); |
125 | static unsigned long *colors_in_color_table (int *n); | |
4ef0d4d0 KS |
126 | #endif |
127 | ||
9c90cc06 | 128 | static Lisp_Object QCmax_width, QCmax_height; |
f3f9606c | 129 | |
4ef0d4d0 KS |
130 | /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap |
131 | id, which is just an int that this section returns. Bitmaps are | |
132 | reference counted so they can be shared among frames. | |
133 | ||
134 | Bitmap indices are guaranteed to be > 0, so a negative number can | |
135 | be used to indicate no bitmap. | |
136 | ||
137 | If you use x_create_bitmap_from_data, then you must keep track of | |
138 | the bitmaps yourself. That is, creating a bitmap from the same | |
139 | data more than once will not be caught. */ | |
140 | ||
edfda783 | 141 | #ifdef HAVE_NS |
bad98418 | 142 | /* Use with images created by ns_image_for_XPM. */ |
f462f075 | 143 | static unsigned long |
edfda783 AR |
144 | XGetPixel (XImagePtr ximage, int x, int y) |
145 | { | |
ed3751c8 | 146 | return ns_get_pixel (ximage, x, y); |
edfda783 AR |
147 | } |
148 | ||
bad98418 PE |
149 | /* Use with images created by ns_image_for_XPM; alpha set to 1; |
150 | pixel is assumed to be in RGB form. */ | |
f462f075 | 151 | static void |
edfda783 AR |
152 | XPutPixel (XImagePtr ximage, int x, int y, unsigned long pixel) |
153 | { | |
ed3751c8 | 154 | ns_put_pixel (ximage, x, y, pixel); |
edfda783 AR |
155 | } |
156 | #endif /* HAVE_NS */ | |
157 | ||
4ef0d4d0 KS |
158 | |
159 | /* Functions to access the contents of a bitmap, given an id. */ | |
160 | ||
5af73b0f | 161 | #ifdef HAVE_X_WINDOWS |
b7e0ef72 | 162 | static int |
a10c8269 | 163 | x_bitmap_height (struct frame *f, ptrdiff_t id) |
4ef0d4d0 | 164 | { |
aad3612f | 165 | return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].height; |
4ef0d4d0 KS |
166 | } |
167 | ||
b7e0ef72 | 168 | static int |
a10c8269 | 169 | x_bitmap_width (struct frame *f, ptrdiff_t id) |
4ef0d4d0 | 170 | { |
aad3612f | 171 | return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].width; |
4ef0d4d0 | 172 | } |
5af73b0f | 173 | #endif |
4ef0d4d0 KS |
174 | |
175 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) | |
62aba0d4 | 176 | ptrdiff_t |
a10c8269 | 177 | x_bitmap_pixmap (struct frame *f, ptrdiff_t id) |
4ef0d4d0 | 178 | { |
f6cfbd8f | 179 | /* HAVE_NTGUI needs the explicit cast here. */ |
aad3612f | 180 | return (ptrdiff_t) FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap; |
4ef0d4d0 KS |
181 | } |
182 | #endif | |
183 | ||
184 | #ifdef HAVE_X_WINDOWS | |
185 | int | |
a10c8269 | 186 | x_bitmap_mask (struct frame *f, ptrdiff_t id) |
4ef0d4d0 | 187 | { |
aad3612f | 188 | return FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].mask; |
4ef0d4d0 KS |
189 | } |
190 | #endif | |
191 | ||
192 | /* Allocate a new bitmap record. Returns index of new record. */ | |
193 | ||
0766b489 | 194 | static ptrdiff_t |
a10c8269 | 195 | x_allocate_bitmap_record (struct frame *f) |
4ef0d4d0 | 196 | { |
aad3612f | 197 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
0766b489 | 198 | ptrdiff_t i; |
4ef0d4d0 | 199 | |
4ef0d4d0 KS |
200 | if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size) |
201 | return ++dpyinfo->bitmaps_last; | |
202 | ||
203 | for (i = 0; i < dpyinfo->bitmaps_size; ++i) | |
204 | if (dpyinfo->bitmaps[i].refcount == 0) | |
205 | return i + 1; | |
206 | ||
a69fbedb PE |
207 | dpyinfo->bitmaps = |
208 | xpalloc (dpyinfo->bitmaps, &dpyinfo->bitmaps_size, | |
209 | 10, -1, sizeof *dpyinfo->bitmaps); | |
4ef0d4d0 KS |
210 | return ++dpyinfo->bitmaps_last; |
211 | } | |
212 | ||
213 | /* Add one reference to the reference count of the bitmap with id ID. */ | |
214 | ||
215 | void | |
a10c8269 | 216 | x_reference_bitmap (struct frame *f, ptrdiff_t id) |
4ef0d4d0 | 217 | { |
aad3612f | 218 | ++FRAME_DISPLAY_INFO (f)->bitmaps[id - 1].refcount; |
4ef0d4d0 KS |
219 | } |
220 | ||
221 | /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */ | |
222 | ||
0766b489 | 223 | ptrdiff_t |
d3da34e0 | 224 | x_create_bitmap_from_data (struct frame *f, char *bits, unsigned int width, unsigned int height) |
4ef0d4d0 | 225 | { |
aad3612f | 226 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
0766b489 | 227 | ptrdiff_t id; |
4ef0d4d0 KS |
228 | |
229 | #ifdef HAVE_X_WINDOWS | |
230 | Pixmap bitmap; | |
231 | bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
232 | bits, width, height); | |
233 | if (! bitmap) | |
234 | return -1; | |
235 | #endif /* HAVE_X_WINDOWS */ | |
236 | ||
237 | #ifdef HAVE_NTGUI | |
238 | Pixmap bitmap; | |
239 | bitmap = CreateBitmap (width, height, | |
aad3612f DA |
240 | FRAME_DISPLAY_INFO (XFRAME (frame))->n_planes, |
241 | FRAME_DISPLAY_INFO (XFRAME (frame))->n_cbits, | |
4ef0d4d0 KS |
242 | bits); |
243 | if (! bitmap) | |
244 | return -1; | |
245 | #endif /* HAVE_NTGUI */ | |
246 | ||
edfda783 | 247 | #ifdef HAVE_NS |
ed3751c8 | 248 | void *bitmap = ns_image_from_XBM (bits, width, height); |
edfda783 AR |
249 | if (!bitmap) |
250 | return -1; | |
251 | #endif | |
252 | ||
4ef0d4d0 | 253 | id = x_allocate_bitmap_record (f); |
4ef0d4d0 | 254 | |
edfda783 AR |
255 | #ifdef HAVE_NS |
256 | dpyinfo->bitmaps[id - 1].img = bitmap; | |
257 | dpyinfo->bitmaps[id - 1].depth = 1; | |
258 | #endif | |
259 | ||
4ef0d4d0 KS |
260 | dpyinfo->bitmaps[id - 1].file = NULL; |
261 | dpyinfo->bitmaps[id - 1].height = height; | |
262 | dpyinfo->bitmaps[id - 1].width = width; | |
263 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
264 | ||
265 | #ifdef HAVE_X_WINDOWS | |
266 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
96c06863 | 267 | dpyinfo->bitmaps[id - 1].have_mask = false; |
4ef0d4d0 KS |
268 | dpyinfo->bitmaps[id - 1].depth = 1; |
269 | #endif /* HAVE_X_WINDOWS */ | |
270 | ||
271 | #ifdef HAVE_NTGUI | |
272 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
273 | dpyinfo->bitmaps[id - 1].hinst = NULL; | |
274 | dpyinfo->bitmaps[id - 1].depth = 1; | |
275 | #endif /* HAVE_NTGUI */ | |
276 | ||
277 | return id; | |
278 | } | |
279 | ||
280 | /* Create bitmap from file FILE for frame F. */ | |
281 | ||
0766b489 | 282 | ptrdiff_t |
d3da34e0 | 283 | x_create_bitmap_from_file (struct frame *f, Lisp_Object file) |
4ef0d4d0 | 284 | { |
aad3612f | 285 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
edfda783 | 286 | |
4ef0d4d0 KS |
287 | #ifdef HAVE_NTGUI |
288 | return -1; /* W32_TODO : bitmap support */ | |
289 | #endif /* HAVE_NTGUI */ | |
290 | ||
edfda783 | 291 | #ifdef HAVE_NS |
0766b489 | 292 | ptrdiff_t id; |
ed3751c8 | 293 | void *bitmap = ns_image_from_file (file); |
edfda783 AR |
294 | |
295 | if (!bitmap) | |
296 | return -1; | |
297 | ||
298 | ||
299 | id = x_allocate_bitmap_record (f); | |
300 | dpyinfo->bitmaps[id - 1].img = bitmap; | |
301 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
5b71542d | 302 | dpyinfo->bitmaps[id - 1].file = xlispstrdup (file); |
edfda783 | 303 | dpyinfo->bitmaps[id - 1].depth = 1; |
ed3751c8 JB |
304 | dpyinfo->bitmaps[id - 1].height = ns_image_width (bitmap); |
305 | dpyinfo->bitmaps[id - 1].width = ns_image_height (bitmap); | |
edfda783 AR |
306 | return id; |
307 | #endif | |
308 | ||
4ef0d4d0 | 309 | #ifdef HAVE_X_WINDOWS |
4ef0d4d0 KS |
310 | unsigned int width, height; |
311 | Pixmap bitmap; | |
0766b489 PE |
312 | int xhot, yhot, result; |
313 | ptrdiff_t id; | |
4ef0d4d0 | 314 | Lisp_Object found; |
4ef0d4d0 KS |
315 | char *filename; |
316 | ||
317 | /* Look for an existing bitmap with the same name. */ | |
318 | for (id = 0; id < dpyinfo->bitmaps_last; ++id) | |
319 | { | |
320 | if (dpyinfo->bitmaps[id].refcount | |
321 | && dpyinfo->bitmaps[id].file | |
51b59d79 | 322 | && !strcmp (dpyinfo->bitmaps[id].file, SSDATA (file))) |
4ef0d4d0 KS |
323 | { |
324 | ++dpyinfo->bitmaps[id].refcount; | |
325 | return id + 1; | |
326 | } | |
327 | } | |
328 | ||
329 | /* Search bitmap-file-path for the file, if appropriate. */ | |
518c40a2 PE |
330 | if (openp (Vx_bitmap_file_path, file, Qnil, &found, |
331 | make_number (R_OK), false) | |
332 | < 0) | |
4ef0d4d0 | 333 | return -1; |
4ef0d4d0 | 334 | |
51b59d79 | 335 | filename = SSDATA (found); |
4ef0d4d0 KS |
336 | |
337 | result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
338 | filename, &width, &height, &bitmap, &xhot, &yhot); | |
339 | if (result != BitmapSuccess) | |
340 | return -1; | |
341 | ||
342 | id = x_allocate_bitmap_record (f); | |
343 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; | |
96c06863 | 344 | dpyinfo->bitmaps[id - 1].have_mask = false; |
4ef0d4d0 | 345 | dpyinfo->bitmaps[id - 1].refcount = 1; |
5b71542d | 346 | dpyinfo->bitmaps[id - 1].file = xlispstrdup (file); |
4ef0d4d0 KS |
347 | dpyinfo->bitmaps[id - 1].depth = 1; |
348 | dpyinfo->bitmaps[id - 1].height = height; | |
349 | dpyinfo->bitmaps[id - 1].width = width; | |
4ef0d4d0 KS |
350 | |
351 | return id; | |
352 | #endif /* HAVE_X_WINDOWS */ | |
353 | } | |
354 | ||
355 | /* Free bitmap B. */ | |
356 | ||
357 | static void | |
d3da34e0 | 358 | free_bitmap_record (Display_Info *dpyinfo, Bitmap_Record *bm) |
4ef0d4d0 KS |
359 | { |
360 | #ifdef HAVE_X_WINDOWS | |
361 | XFreePixmap (dpyinfo->display, bm->pixmap); | |
362 | if (bm->have_mask) | |
363 | XFreePixmap (dpyinfo->display, bm->mask); | |
364 | #endif /* HAVE_X_WINDOWS */ | |
365 | ||
366 | #ifdef HAVE_NTGUI | |
367 | DeleteObject (bm->pixmap); | |
368 | #endif /* HAVE_NTGUI */ | |
369 | ||
edfda783 | 370 | #ifdef HAVE_NS |
ed3751c8 | 371 | ns_release_object (bm->img); |
edfda783 AR |
372 | #endif |
373 | ||
4ef0d4d0 KS |
374 | if (bm->file) |
375 | { | |
376 | xfree (bm->file); | |
377 | bm->file = NULL; | |
378 | } | |
379 | } | |
380 | ||
381 | /* Remove reference to bitmap with id number ID. */ | |
382 | ||
383 | void | |
a10c8269 | 384 | x_destroy_bitmap (struct frame *f, ptrdiff_t id) |
4ef0d4d0 | 385 | { |
aad3612f | 386 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
4ef0d4d0 KS |
387 | |
388 | if (id > 0) | |
389 | { | |
390 | Bitmap_Record *bm = &dpyinfo->bitmaps[id - 1]; | |
391 | ||
392 | if (--bm->refcount == 0) | |
393 | { | |
4d7e6e51 | 394 | block_input (); |
65342ae3 | 395 | free_bitmap_record (dpyinfo, bm); |
4d7e6e51 | 396 | unblock_input (); |
4ef0d4d0 KS |
397 | } |
398 | } | |
399 | } | |
400 | ||
401 | /* Free all the bitmaps for the display specified by DPYINFO. */ | |
402 | ||
403 | void | |
d3da34e0 | 404 | x_destroy_all_bitmaps (Display_Info *dpyinfo) |
4ef0d4d0 | 405 | { |
0766b489 | 406 | ptrdiff_t i; |
4ef0d4d0 KS |
407 | Bitmap_Record *bm = dpyinfo->bitmaps; |
408 | ||
409 | for (i = 0; i < dpyinfo->bitmaps_last; i++, bm++) | |
410 | if (bm->refcount > 0) | |
65342ae3 | 411 | free_bitmap_record (dpyinfo, bm); |
4ef0d4d0 KS |
412 | |
413 | dpyinfo->bitmaps_last = 0; | |
414 | } | |
415 | ||
578098f3 PE |
416 | static bool x_create_x_image_and_pixmap (struct frame *, int, int, int, |
417 | XImagePtr *, Pixmap *); | |
418 | static void x_destroy_x_image (XImagePtr ximg); | |
4ef0d4d0 | 419 | |
547c9269 YM |
420 | #ifdef HAVE_NTGUI |
421 | static XImagePtr_or_DC image_get_x_image_or_dc (struct frame *, struct image *, | |
422 | bool, HGDIOBJ *); | |
423 | static void image_unget_x_image_or_dc (struct image *, bool, XImagePtr_or_DC, | |
424 | HGDIOBJ); | |
425 | #else | |
426 | static XImagePtr image_get_x_image (struct frame *, struct image *, bool); | |
427 | static void image_unget_x_image (struct image *, bool, XImagePtr); | |
428 | #define image_get_x_image_or_dc(f, img, mask_p, dummy) \ | |
429 | image_get_x_image (f, img, mask_p) | |
430 | #define image_unget_x_image_or_dc(img, mask_p, ximg, dummy) \ | |
431 | image_unget_x_image (img, mask_p, ximg) | |
432 | #endif | |
433 | ||
4ef0d4d0 KS |
434 | #ifdef HAVE_X_WINDOWS |
435 | ||
547c9269 YM |
436 | static void image_sync_to_pixmaps (struct frame *, struct image *); |
437 | ||
4ef0d4d0 KS |
438 | /* Useful functions defined in the section |
439 | `Image type independent image structures' below. */ | |
440 | ||
f57e2426 J |
441 | static unsigned long four_corners_best (XImagePtr ximg, |
442 | int *corners, | |
443 | unsigned long width, | |
444 | unsigned long height); | |
4ef0d4d0 | 445 | |
4ef0d4d0 KS |
446 | |
447 | /* Create a mask of a bitmap. Note is this not a perfect mask. | |
448 | It's nicer with some borders in this context */ | |
449 | ||
578098f3 | 450 | void |
0766b489 | 451 | x_create_bitmap_mask (struct frame *f, ptrdiff_t id) |
4ef0d4d0 KS |
452 | { |
453 | Pixmap pixmap, mask; | |
454 | XImagePtr ximg, mask_img; | |
455 | unsigned long width, height; | |
578098f3 | 456 | bool result; |
4ef0d4d0 KS |
457 | unsigned long bg; |
458 | unsigned long x, y, xp, xm, yp, ym; | |
459 | GC gc; | |
460 | ||
aad3612f | 461 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
4ef0d4d0 KS |
462 | |
463 | if (!(id > 0)) | |
578098f3 | 464 | return; |
4ef0d4d0 KS |
465 | |
466 | pixmap = x_bitmap_pixmap (f, id); | |
467 | width = x_bitmap_width (f, id); | |
468 | height = x_bitmap_height (f, id); | |
469 | ||
4d7e6e51 | 470 | block_input (); |
4ef0d4d0 KS |
471 | ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height, |
472 | ~0, ZPixmap); | |
473 | ||
474 | if (!ximg) | |
475 | { | |
4d7e6e51 | 476 | unblock_input (); |
578098f3 | 477 | return; |
4ef0d4d0 KS |
478 | } |
479 | ||
480 | result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask); | |
481 | ||
4d7e6e51 | 482 | unblock_input (); |
4ef0d4d0 KS |
483 | if (!result) |
484 | { | |
485 | XDestroyImage (ximg); | |
578098f3 | 486 | return; |
4ef0d4d0 KS |
487 | } |
488 | ||
6ac3c7bb | 489 | bg = four_corners_best (ximg, NULL, width, height); |
4ef0d4d0 KS |
490 | |
491 | for (y = 0; y < ximg->height; ++y) | |
492 | { | |
493 | for (x = 0; x < ximg->width; ++x) | |
494 | { | |
495 | xp = x != ximg->width - 1 ? x + 1 : 0; | |
496 | xm = x != 0 ? x - 1 : ximg->width - 1; | |
497 | yp = y != ximg->height - 1 ? y + 1 : 0; | |
498 | ym = y != 0 ? y - 1 : ximg->height - 1; | |
499 | if (XGetPixel (ximg, x, y) == bg | |
500 | && XGetPixel (ximg, x, yp) == bg | |
501 | && XGetPixel (ximg, x, ym) == bg | |
502 | && XGetPixel (ximg, xp, y) == bg | |
503 | && XGetPixel (ximg, xp, yp) == bg | |
504 | && XGetPixel (ximg, xp, ym) == bg | |
505 | && XGetPixel (ximg, xm, y) == bg | |
506 | && XGetPixel (ximg, xm, yp) == bg | |
507 | && XGetPixel (ximg, xm, ym) == bg) | |
508 | XPutPixel (mask_img, x, y, 0); | |
509 | else | |
510 | XPutPixel (mask_img, x, y, 1); | |
511 | } | |
512 | } | |
513 | ||
4d7e6e51 | 514 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
515 | gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL); |
516 | XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0, | |
517 | width, height); | |
518 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
519 | ||
96c06863 | 520 | dpyinfo->bitmaps[id - 1].have_mask = true; |
4ef0d4d0 KS |
521 | dpyinfo->bitmaps[id - 1].mask = mask; |
522 | ||
523 | XDestroyImage (ximg); | |
524 | x_destroy_x_image (mask_img); | |
4ef0d4d0 KS |
525 | } |
526 | ||
527 | #endif /* HAVE_X_WINDOWS */ | |
528 | ||
529 | ||
530 | /*********************************************************************** | |
531 | Image types | |
532 | ***********************************************************************/ | |
533 | ||
4ef0d4d0 KS |
534 | /* List of supported image types. Use define_image_type to add new |
535 | types. Use lookup_image_type to find a type for a given symbol. */ | |
536 | ||
537 | static struct image_type *image_types; | |
538 | ||
539 | /* The symbol `xbm' which is used as the type symbol for XBM images. */ | |
540 | ||
955cbe7b | 541 | static Lisp_Object Qxbm; |
4ef0d4d0 KS |
542 | |
543 | /* Keywords. */ | |
544 | ||
955cbe7b | 545 | Lisp_Object QCascent, QCmargin, QCrelief; |
955cbe7b PE |
546 | Lisp_Object QCconversion; |
547 | static Lisp_Object QCheuristic_mask; | |
548 | static Lisp_Object QCcolor_symbols; | |
549 | static Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask, QCgeometry; | |
550 | static Lisp_Object QCcrop, QCrotation; | |
4ef0d4d0 KS |
551 | |
552 | /* Other symbols. */ | |
553 | ||
1100a63c | 554 | static Lisp_Object Qcount, Qextension_data, Qdelay; |
955cbe7b | 555 | static Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic; |
4ef0d4d0 | 556 | |
578098f3 | 557 | /* Forward function prototypes. */ |
4ef0d4d0 | 558 | |
641cfd14 | 559 | static struct image_type *lookup_image_type (Lisp_Object); |
f57e2426 J |
560 | static void x_laplace (struct frame *, struct image *); |
561 | static void x_emboss (struct frame *, struct image *); | |
578098f3 | 562 | static void x_build_heuristic_mask (struct frame *, struct image *, |
36a305a7 | 563 | Lisp_Object); |
0fda9b75 | 564 | #ifdef WINDOWSNT |
0855eb52 | 565 | #define CACHE_IMAGE_TYPE(type, status) \ |
0898ca10 | 566 | do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0) |
84d358f0 JB |
567 | #else |
568 | #define CACHE_IMAGE_TYPE(type, status) | |
a9e7a9d5 | 569 | #endif |
0855eb52 JB |
570 | |
571 | #define ADD_IMAGE_TYPE(type) \ | |
572 | do { Vimage_types = Fcons (type, Vimage_types); } while (0) | |
4ef0d4d0 KS |
573 | |
574 | /* Define a new image type from TYPE. This adds a copy of TYPE to | |
0855eb52 | 575 | image_types and caches the loading status of TYPE. */ |
4ef0d4d0 | 576 | |
bb4d86b4 | 577 | static struct image_type * |
d07ff9db | 578 | define_image_type (struct image_type *type) |
4ef0d4d0 | 579 | { |
bb4d86b4 CY |
580 | struct image_type *p = NULL; |
581 | Lisp_Object target_type = *type->type; | |
578098f3 | 582 | bool type_valid = 1; |
0855eb52 | 583 | |
4d7e6e51 | 584 | block_input (); |
bb4d86b4 CY |
585 | |
586 | for (p = image_types; p; p = p->next) | |
587 | if (EQ (*p->type, target_type)) | |
588 | goto done; | |
589 | ||
590 | if (type->init) | |
0855eb52 | 591 | { |
a9e7a9d5 | 592 | #if defined HAVE_NTGUI && defined WINDOWSNT |
bb4d86b4 CY |
593 | /* If we failed to load the library before, don't try again. */ |
594 | Lisp_Object tested = Fassq (target_type, Vlibrary_cache); | |
595 | if (CONSP (tested) && NILP (XCDR (tested))) | |
596 | type_valid = 0; | |
597 | else | |
598 | #endif | |
599 | { | |
d07ff9db | 600 | type_valid = type->init (); |
bb4d86b4 CY |
601 | CACHE_IMAGE_TYPE (target_type, type_valid ? Qt : Qnil); |
602 | } | |
603 | } | |
09c01941 | 604 | |
bb4d86b4 | 605 | if (type_valid) |
0855eb52 JB |
606 | { |
607 | /* Make a copy of TYPE to avoid a bus error in a dumped Emacs. | |
608 | The initialized data segment is read-only. */ | |
09c01941 | 609 | p = xmalloc (sizeof *p); |
ae1d87e2 | 610 | *p = *type; |
0855eb52 JB |
611 | p->next = image_types; |
612 | image_types = p; | |
0855eb52 JB |
613 | } |
614 | ||
bb4d86b4 | 615 | done: |
4d7e6e51 | 616 | unblock_input (); |
bb4d86b4 | 617 | return p; |
4ef0d4d0 KS |
618 | } |
619 | ||
620 | ||
578098f3 | 621 | /* Value is true if OBJECT is a valid Lisp image specification. A |
4ef0d4d0 KS |
622 | valid image specification is a list whose car is the symbol |
623 | `image', and whose rest is a property list. The property list must | |
624 | contain a value for key `:type'. That value must be the name of a | |
625 | supported image type. The rest of the property list depends on the | |
626 | image type. */ | |
627 | ||
578098f3 | 628 | bool |
d3da34e0 | 629 | valid_image_p (Lisp_Object object) |
4ef0d4d0 | 630 | { |
578098f3 | 631 | bool valid_p = 0; |
4ef0d4d0 KS |
632 | |
633 | if (IMAGEP (object)) | |
634 | { | |
635 | Lisp_Object tem; | |
636 | ||
637 | for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem)) | |
638 | if (EQ (XCAR (tem), QCtype)) | |
639 | { | |
640 | tem = XCDR (tem); | |
641 | if (CONSP (tem) && SYMBOLP (XCAR (tem))) | |
642 | { | |
643 | struct image_type *type; | |
644 | type = lookup_image_type (XCAR (tem)); | |
645 | if (type) | |
646 | valid_p = type->valid_p (object); | |
647 | } | |
648 | ||
649 | break; | |
650 | } | |
651 | } | |
652 | ||
653 | return valid_p; | |
654 | } | |
655 | ||
656 | ||
657 | /* Log error message with format string FORMAT and argument ARG. | |
658 | Signaling an error, e.g. when an image cannot be loaded, is not a | |
659 | good idea because this would interrupt redisplay, and the error | |
660 | message display would lead to another redisplay. This function | |
661 | therefore simply displays a message. */ | |
662 | ||
663 | static void | |
675e2c69 | 664 | image_error (const char *format, Lisp_Object arg1, Lisp_Object arg2) |
4ef0d4d0 KS |
665 | { |
666 | add_to_log (format, arg1, arg2); | |
667 | } | |
668 | ||
669 | ||
670 | \f | |
671 | /*********************************************************************** | |
672 | Image specifications | |
673 | ***********************************************************************/ | |
674 | ||
675 | enum image_value_type | |
676 | { | |
677 | IMAGE_DONT_CHECK_VALUE_TYPE, | |
678 | IMAGE_STRING_VALUE, | |
679 | IMAGE_STRING_OR_NIL_VALUE, | |
680 | IMAGE_SYMBOL_VALUE, | |
681 | IMAGE_POSITIVE_INTEGER_VALUE, | |
c4a07a4c | 682 | IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, |
4ef0d4d0 KS |
683 | IMAGE_NON_NEGATIVE_INTEGER_VALUE, |
684 | IMAGE_ASCENT_VALUE, | |
685 | IMAGE_INTEGER_VALUE, | |
686 | IMAGE_FUNCTION_VALUE, | |
687 | IMAGE_NUMBER_VALUE, | |
688 | IMAGE_BOOL_VALUE | |
689 | }; | |
690 | ||
691 | /* Structure used when parsing image specifications. */ | |
692 | ||
693 | struct image_keyword | |
694 | { | |
695 | /* Name of keyword. */ | |
675e2c69 | 696 | const char *name; |
4ef0d4d0 KS |
697 | |
698 | /* The type of value allowed. */ | |
699 | enum image_value_type type; | |
700 | ||
578098f3 PE |
701 | /* True means key must be present. */ |
702 | bool mandatory_p; | |
4ef0d4d0 KS |
703 | |
704 | /* Used to recognize duplicate keywords in a property list. */ | |
705 | int count; | |
706 | ||
707 | /* The value that was found. */ | |
708 | Lisp_Object value; | |
709 | }; | |
710 | ||
711 | ||
4ef0d4d0 KS |
712 | /* Parse image spec SPEC according to KEYWORDS. A valid image spec |
713 | has the format (image KEYWORD VALUE ...). One of the keyword/ | |
714 | value pairs must be `:type TYPE'. KEYWORDS is a vector of | |
715 | image_keywords structures of size NKEYWORDS describing other | |
578098f3 | 716 | allowed keyword/value pairs. Value is true if SPEC is valid. */ |
4ef0d4d0 | 717 | |
578098f3 | 718 | static bool |
d3da34e0 JB |
719 | parse_image_spec (Lisp_Object spec, struct image_keyword *keywords, |
720 | int nkeywords, Lisp_Object type) | |
4ef0d4d0 KS |
721 | { |
722 | int i; | |
723 | Lisp_Object plist; | |
724 | ||
725 | if (!IMAGEP (spec)) | |
726 | return 0; | |
727 | ||
728 | plist = XCDR (spec); | |
729 | while (CONSP (plist)) | |
730 | { | |
731 | Lisp_Object key, value; | |
732 | ||
733 | /* First element of a pair must be a symbol. */ | |
734 | key = XCAR (plist); | |
735 | plist = XCDR (plist); | |
736 | if (!SYMBOLP (key)) | |
737 | return 0; | |
738 | ||
739 | /* There must follow a value. */ | |
740 | if (!CONSP (plist)) | |
741 | return 0; | |
742 | value = XCAR (plist); | |
743 | plist = XCDR (plist); | |
744 | ||
745 | /* Find key in KEYWORDS. Error if not found. */ | |
746 | for (i = 0; i < nkeywords; ++i) | |
42a5b22f | 747 | if (strcmp (keywords[i].name, SSDATA (SYMBOL_NAME (key))) == 0) |
4ef0d4d0 KS |
748 | break; |
749 | ||
750 | if (i == nkeywords) | |
751 | continue; | |
752 | ||
753 | /* Record that we recognized the keyword. If a keywords | |
754 | was found more than once, it's an error. */ | |
755 | keywords[i].value = value; | |
4ef0d4d0 KS |
756 | if (keywords[i].count > 1) |
757 | return 0; | |
d311d28c | 758 | ++keywords[i].count; |
4ef0d4d0 KS |
759 | |
760 | /* Check type of value against allowed type. */ | |
761 | switch (keywords[i].type) | |
762 | { | |
763 | case IMAGE_STRING_VALUE: | |
764 | if (!STRINGP (value)) | |
765 | return 0; | |
766 | break; | |
767 | ||
768 | case IMAGE_STRING_OR_NIL_VALUE: | |
769 | if (!STRINGP (value) && !NILP (value)) | |
770 | return 0; | |
771 | break; | |
772 | ||
773 | case IMAGE_SYMBOL_VALUE: | |
774 | if (!SYMBOLP (value)) | |
775 | return 0; | |
776 | break; | |
777 | ||
778 | case IMAGE_POSITIVE_INTEGER_VALUE: | |
13464394 | 779 | if (! RANGED_INTEGERP (1, value, INT_MAX)) |
4ef0d4d0 KS |
780 | return 0; |
781 | break; | |
782 | ||
c4a07a4c PE |
783 | case IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR: |
784 | if (RANGED_INTEGERP (0, value, INT_MAX)) | |
4ef0d4d0 KS |
785 | break; |
786 | if (CONSP (value) | |
c4a07a4c PE |
787 | && RANGED_INTEGERP (0, XCAR (value), INT_MAX) |
788 | && RANGED_INTEGERP (0, XCDR (value), INT_MAX)) | |
4ef0d4d0 KS |
789 | break; |
790 | return 0; | |
791 | ||
792 | case IMAGE_ASCENT_VALUE: | |
793 | if (SYMBOLP (value) && EQ (value, Qcenter)) | |
794 | break; | |
13464394 | 795 | else if (RANGED_INTEGERP (0, value, 100)) |
4ef0d4d0 KS |
796 | break; |
797 | return 0; | |
798 | ||
799 | case IMAGE_NON_NEGATIVE_INTEGER_VALUE: | |
13464394 PE |
800 | /* Unlike the other integer-related cases, this one does not |
801 | verify that VALUE fits in 'int'. This is because callers | |
802 | want EMACS_INT. */ | |
4ef0d4d0 KS |
803 | if (!INTEGERP (value) || XINT (value) < 0) |
804 | return 0; | |
805 | break; | |
806 | ||
807 | case IMAGE_DONT_CHECK_VALUE_TYPE: | |
808 | break; | |
809 | ||
810 | case IMAGE_FUNCTION_VALUE: | |
811 | value = indirect_function (value); | |
ca105506 | 812 | if (!NILP (Ffunctionp (value))) |
4ef0d4d0 KS |
813 | break; |
814 | return 0; | |
815 | ||
816 | case IMAGE_NUMBER_VALUE: | |
817 | if (!INTEGERP (value) && !FLOATP (value)) | |
818 | return 0; | |
819 | break; | |
820 | ||
821 | case IMAGE_INTEGER_VALUE: | |
13464394 | 822 | if (! TYPE_RANGED_INTEGERP (int, value)) |
4ef0d4d0 KS |
823 | return 0; |
824 | break; | |
825 | ||
826 | case IMAGE_BOOL_VALUE: | |
827 | if (!NILP (value) && !EQ (value, Qt)) | |
828 | return 0; | |
829 | break; | |
830 | ||
831 | default: | |
1088b922 | 832 | emacs_abort (); |
4ef0d4d0 KS |
833 | break; |
834 | } | |
835 | ||
836 | if (EQ (key, QCtype) && !EQ (type, value)) | |
837 | return 0; | |
838 | } | |
839 | ||
840 | /* Check that all mandatory fields are present. */ | |
841 | for (i = 0; i < nkeywords; ++i) | |
842 | if (keywords[i].mandatory_p && keywords[i].count == 0) | |
843 | return 0; | |
844 | ||
845 | return NILP (plist); | |
846 | } | |
847 | ||
848 | ||
849 | /* Return the value of KEY in image specification SPEC. Value is nil | |
578098f3 PE |
850 | if KEY is not present in SPEC. Set *FOUND depending on whether KEY |
851 | was found in SPEC. */ | |
4ef0d4d0 KS |
852 | |
853 | static Lisp_Object | |
578098f3 | 854 | image_spec_value (Lisp_Object spec, Lisp_Object key, bool *found) |
4ef0d4d0 KS |
855 | { |
856 | Lisp_Object tail; | |
857 | ||
a54e2c05 | 858 | eassert (valid_image_p (spec)); |
4ef0d4d0 KS |
859 | |
860 | for (tail = XCDR (spec); | |
861 | CONSP (tail) && CONSP (XCDR (tail)); | |
862 | tail = XCDR (XCDR (tail))) | |
863 | { | |
864 | if (EQ (XCAR (tail), key)) | |
865 | { | |
866 | if (found) | |
867 | *found = 1; | |
868 | return XCAR (XCDR (tail)); | |
869 | } | |
870 | } | |
871 | ||
872 | if (found) | |
873 | *found = 0; | |
874 | return Qnil; | |
875 | } | |
876 | ||
877 | ||
878 | DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0, | |
879 | doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT). | |
880 | PIXELS non-nil means return the size in pixels, otherwise return the | |
881 | size in canonical character units. | |
882 | FRAME is the frame on which the image will be displayed. FRAME nil | |
883 | or omitted means use the selected frame. */) | |
5842a27b | 884 | (Lisp_Object spec, Lisp_Object pixels, Lisp_Object frame) |
4ef0d4d0 KS |
885 | { |
886 | Lisp_Object size; | |
887 | ||
888 | size = Qnil; | |
889 | if (valid_image_p (spec)) | |
890 | { | |
7452b7bd | 891 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 892 | ptrdiff_t id = lookup_image (f, spec); |
4ef0d4d0 KS |
893 | struct image *img = IMAGE_FROM_ID (f, id); |
894 | int width = img->width + 2 * img->hmargin; | |
895 | int height = img->height + 2 * img->vmargin; | |
896 | ||
897 | if (NILP (pixels)) | |
898 | size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)), | |
899 | make_float ((double) height / FRAME_LINE_HEIGHT (f))); | |
900 | else | |
901 | size = Fcons (make_number (width), make_number (height)); | |
902 | } | |
903 | else | |
904 | error ("Invalid image specification"); | |
905 | ||
906 | return size; | |
907 | } | |
908 | ||
909 | ||
910 | DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0, | |
911 | doc: /* Return t if image SPEC has a mask bitmap. | |
912 | FRAME is the frame on which the image will be displayed. FRAME nil | |
913 | or omitted means use the selected frame. */) | |
5842a27b | 914 | (Lisp_Object spec, Lisp_Object frame) |
4ef0d4d0 KS |
915 | { |
916 | Lisp_Object mask; | |
917 | ||
918 | mask = Qnil; | |
919 | if (valid_image_p (spec)) | |
920 | { | |
7452b7bd | 921 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 922 | ptrdiff_t id = lookup_image (f, spec); |
4ef0d4d0 KS |
923 | struct image *img = IMAGE_FROM_ID (f, id); |
924 | if (img->mask) | |
925 | mask = Qt; | |
926 | } | |
927 | else | |
928 | error ("Invalid image specification"); | |
929 | ||
930 | return mask; | |
931 | } | |
932 | ||
1546c559 JL |
933 | DEFUN ("image-metadata", Fimage_metadata, Simage_metadata, 1, 2, 0, |
934 | doc: /* Return metadata for image SPEC. | |
6ac3c7bb KS |
935 | FRAME is the frame on which the image will be displayed. FRAME nil |
936 | or omitted means use the selected frame. */) | |
5842a27b | 937 | (Lisp_Object spec, Lisp_Object frame) |
6ac3c7bb KS |
938 | { |
939 | Lisp_Object ext; | |
940 | ||
941 | ext = Qnil; | |
942 | if (valid_image_p (spec)) | |
943 | { | |
7452b7bd | 944 | struct frame *f = decode_window_system_frame (frame); |
13464394 | 945 | ptrdiff_t id = lookup_image (f, spec); |
6ac3c7bb | 946 | struct image *img = IMAGE_FROM_ID (f, id); |
b50691aa | 947 | ext = img->lisp_data; |
6ac3c7bb KS |
948 | } |
949 | ||
950 | return ext; | |
951 | } | |
952 | ||
4ef0d4d0 KS |
953 | \f |
954 | /*********************************************************************** | |
955 | Image type independent image structures | |
956 | ***********************************************************************/ | |
957 | ||
11273115 | 958 | #define MAX_IMAGE_SIZE 10.0 |
4ef0d4d0 KS |
959 | /* Allocate and return a new image structure for image specification |
960 | SPEC. SPEC has a hash value of HASH. */ | |
961 | ||
962 | static struct image * | |
0de4bb68 | 963 | make_image (Lisp_Object spec, EMACS_UINT hash) |
4ef0d4d0 | 964 | { |
23f86fce | 965 | struct image *img = xzalloc (sizeof *img); |
a2bc5bdd | 966 | Lisp_Object file = image_spec_value (spec, QCfile, NULL); |
4ef0d4d0 | 967 | |
a54e2c05 | 968 | eassert (valid_image_p (spec)); |
a2bc5bdd | 969 | img->dependencies = NILP (file) ? Qnil : list1 (file); |
4ef0d4d0 | 970 | img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL)); |
a54e2c05 | 971 | eassert (img->type != NULL); |
4ef0d4d0 | 972 | img->spec = spec; |
b50691aa | 973 | img->lisp_data = Qnil; |
4ef0d4d0 KS |
974 | img->ascent = DEFAULT_IMAGE_ASCENT; |
975 | img->hash = hash; | |
6ac3c7bb | 976 | img->corners[BOT_CORNER] = -1; /* Full image */ |
4ef0d4d0 KS |
977 | return img; |
978 | } | |
979 | ||
980 | ||
981 | /* Free image IMG which was used on frame F, including its resources. */ | |
982 | ||
983 | static void | |
d3da34e0 | 984 | free_image (struct frame *f, struct image *img) |
4ef0d4d0 KS |
985 | { |
986 | if (img) | |
987 | { | |
354884c4 | 988 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
989 | |
990 | /* Remove IMG from the hash table of its cache. */ | |
991 | if (img->prev) | |
992 | img->prev->next = img->next; | |
993 | else | |
994 | c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next; | |
995 | ||
996 | if (img->next) | |
997 | img->next->prev = img->prev; | |
998 | ||
999 | c->images[img->id] = NULL; | |
1000 | ||
587fd086 FP |
1001 | /* Windows NT redefines 'free', but in this file, we need to |
1002 | avoid the redefinition. */ | |
1003 | #ifdef WINDOWSNT | |
1004 | #undef free | |
1005 | #endif | |
4ef0d4d0 KS |
1006 | /* Free resources, then free IMG. */ |
1007 | img->type->free (f, img); | |
1008 | xfree (img); | |
1009 | } | |
1010 | } | |
1011 | ||
578098f3 | 1012 | /* Return true if the given widths and heights are valid for display. */ |
f1f25b99 | 1013 | |
578098f3 | 1014 | static bool |
d3da34e0 | 1015 | check_image_size (struct frame *f, int width, int height) |
f1f25b99 | 1016 | { |
7df4765a | 1017 | int w, h; |
f1f25b99 | 1018 | |
7df4765a | 1019 | if (width <= 0 || height <= 0) |
f1f25b99 CY |
1020 | return 0; |
1021 | ||
7df4765a | 1022 | if (INTEGERP (Vmax_image_size)) |
3f791afe PE |
1023 | return (width <= XINT (Vmax_image_size) |
1024 | && height <= XINT (Vmax_image_size)); | |
7df4765a KS |
1025 | else if (FLOATP (Vmax_image_size)) |
1026 | { | |
e5d76069 PE |
1027 | if (f != NULL) |
1028 | { | |
1029 | w = FRAME_PIXEL_WIDTH (f); | |
1030 | h = FRAME_PIXEL_HEIGHT (f); | |
1031 | } | |
1032 | else | |
1033 | w = h = 1024; /* Arbitrary size for unknown frame. */ | |
3f791afe PE |
1034 | return (width <= XFLOAT_DATA (Vmax_image_size) * w |
1035 | && height <= XFLOAT_DATA (Vmax_image_size) * h); | |
7df4765a KS |
1036 | } |
1037 | else | |
1038 | return 1; | |
f1f25b99 | 1039 | } |
4ef0d4d0 KS |
1040 | |
1041 | /* Prepare image IMG for display on frame F. Must be called before | |
1042 | drawing an image. */ | |
1043 | ||
1044 | void | |
d3da34e0 | 1045 | prepare_image_for_display (struct frame *f, struct image *img) |
4ef0d4d0 | 1046 | { |
4ef0d4d0 | 1047 | /* We're about to display IMG, so set its timestamp to `now'. */ |
43aac990 | 1048 | img->timestamp = current_timespec (); |
4ef0d4d0 KS |
1049 | |
1050 | /* If IMG doesn't have a pixmap yet, load it now, using the image | |
1051 | type dependent loader function. */ | |
1052 | if (img->pixmap == NO_PIXMAP && !img->load_failed_p) | |
578098f3 | 1053 | img->load_failed_p = ! img->type->load (f, img); |
c7fea325 | 1054 | |
547c9269 YM |
1055 | #ifdef HAVE_X_WINDOWS |
1056 | if (!img->load_failed_p) | |
1057 | { | |
1058 | block_input (); | |
1059 | image_sync_to_pixmaps (f, img); | |
1060 | unblock_input (); | |
1061 | } | |
1062 | #endif | |
4ef0d4d0 KS |
1063 | } |
1064 | ||
1065 | ||
1066 | /* Value is the number of pixels for the ascent of image IMG when | |
1067 | drawn in face FACE. */ | |
1068 | ||
1069 | int | |
d3da34e0 | 1070 | image_ascent (struct image *img, struct face *face, struct glyph_slice *slice) |
4ef0d4d0 | 1071 | { |
1938bc36 | 1072 | int height; |
4ef0d4d0 KS |
1073 | int ascent; |
1074 | ||
1938bc36 KS |
1075 | if (slice->height == img->height) |
1076 | height = img->height + img->vmargin; | |
1077 | else if (slice->y == 0) | |
1078 | height = slice->height + img->vmargin; | |
1079 | else | |
1080 | height = slice->height; | |
1081 | ||
4ef0d4d0 KS |
1082 | if (img->ascent == CENTERED_IMAGE_ASCENT) |
1083 | { | |
1084 | if (face->font) | |
1085 | { | |
1086 | #ifdef HAVE_NTGUI | |
1087 | /* W32 specific version. Why?. ++kfs */ | |
5fc9fdeb JR |
1088 | ascent = height / 2 - (FONT_DESCENT (face->font) |
1089 | - FONT_BASE (face->font)) / 2; | |
4ef0d4d0 KS |
1090 | #else |
1091 | /* This expression is arranged so that if the image can't be | |
1092 | exactly centered, it will be moved slightly up. This is | |
1093 | because a typical font is `top-heavy' (due to the presence | |
1094 | uppercase letters), so the image placement should err towards | |
1095 | being top-heavy too. It also just generally looks better. */ | |
5e617bc2 JB |
1096 | ascent = (height + FONT_BASE (face->font) |
1097 | - FONT_DESCENT (face->font) + 1) / 2; | |
4ef0d4d0 KS |
1098 | #endif /* HAVE_NTGUI */ |
1099 | } | |
1100 | else | |
1101 | ascent = height / 2; | |
1102 | } | |
1103 | else | |
13464394 | 1104 | ascent = height * (img->ascent / 100.0); |
4ef0d4d0 KS |
1105 | |
1106 | return ascent; | |
1107 | } | |
1108 | ||
1109 | \f | |
1110 | /* Image background colors. */ | |
1111 | ||
1112 | /* Find the "best" corner color of a bitmap. | |
1113 | On W32, XIMG is assumed to a device context with the bitmap selected. */ | |
1114 | ||
1115 | static RGB_PIXEL_COLOR | |
d3da34e0 JB |
1116 | four_corners_best (XImagePtr_or_DC ximg, int *corners, |
1117 | unsigned long width, unsigned long height) | |
4ef0d4d0 | 1118 | { |
f0c77cd1 | 1119 | RGB_PIXEL_COLOR corner_pixels[4], best IF_LINT (= 0); |
4ef0d4d0 KS |
1120 | int i, best_count; |
1121 | ||
6ac3c7bb KS |
1122 | if (corners && corners[BOT_CORNER] >= 0) |
1123 | { | |
1124 | /* Get the colors at the corner_pixels of ximg. */ | |
1125 | corner_pixels[0] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[TOP_CORNER]); | |
1126 | corner_pixels[1] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[TOP_CORNER]); | |
1127 | corner_pixels[2] = GET_PIXEL (ximg, corners[RIGHT_CORNER] - 1, corners[BOT_CORNER] - 1); | |
1128 | corner_pixels[3] = GET_PIXEL (ximg, corners[LEFT_CORNER], corners[BOT_CORNER] - 1); | |
1129 | } | |
1130 | else | |
1131 | { | |
1132 | /* Get the colors at the corner_pixels of ximg. */ | |
1133 | corner_pixels[0] = GET_PIXEL (ximg, 0, 0); | |
1134 | corner_pixels[1] = GET_PIXEL (ximg, width - 1, 0); | |
1135 | corner_pixels[2] = GET_PIXEL (ximg, width - 1, height - 1); | |
1136 | corner_pixels[3] = GET_PIXEL (ximg, 0, height - 1); | |
1137 | } | |
4ef0d4d0 KS |
1138 | /* Choose the most frequently found color as background. */ |
1139 | for (i = best_count = 0; i < 4; ++i) | |
1140 | { | |
1141 | int j, n; | |
1142 | ||
1143 | for (j = n = 0; j < 4; ++j) | |
6ac3c7bb | 1144 | if (corner_pixels[i] == corner_pixels[j]) |
4ef0d4d0 KS |
1145 | ++n; |
1146 | ||
1147 | if (n > best_count) | |
6ac3c7bb | 1148 | best = corner_pixels[i], best_count = n; |
4ef0d4d0 KS |
1149 | } |
1150 | ||
1151 | return best; | |
1152 | } | |
1153 | ||
1154 | /* Portability macros */ | |
1155 | ||
1156 | #ifdef HAVE_NTGUI | |
1157 | ||
4ef0d4d0 KS |
1158 | #define Free_Pixmap(display, pixmap) \ |
1159 | DeleteObject (pixmap) | |
1160 | ||
edfda783 AR |
1161 | #elif defined (HAVE_NS) |
1162 | ||
edfda783 | 1163 | #define Free_Pixmap(display, pixmap) \ |
ed3751c8 | 1164 | ns_release_object (pixmap) |
edfda783 | 1165 | |
4ef0d4d0 KS |
1166 | #else |
1167 | ||
4ef0d4d0 KS |
1168 | #define Free_Pixmap(display, pixmap) \ |
1169 | XFreePixmap (display, pixmap) | |
1170 | ||
edfda783 | 1171 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
1172 | |
1173 | ||
1174 | /* Return the `background' field of IMG. If IMG doesn't have one yet, | |
1175 | it is guessed heuristically. If non-zero, XIMG is an existing | |
1176 | XImage object (or device context with the image selected on W32) to | |
1f026899 | 1177 | use for the heuristic. */ |
4ef0d4d0 KS |
1178 | |
1179 | RGB_PIXEL_COLOR | |
d3da34e0 | 1180 | image_background (struct image *img, struct frame *f, XImagePtr_or_DC ximg) |
4ef0d4d0 KS |
1181 | { |
1182 | if (! img->background_valid) | |
1183 | /* IMG doesn't have a background yet, try to guess a reasonable value. */ | |
1184 | { | |
578098f3 | 1185 | bool free_ximg = !ximg; |
4ef0d4d0 KS |
1186 | #ifdef HAVE_NTGUI |
1187 | HGDIOBJ prev; | |
1188 | #endif /* HAVE_NTGUI */ | |
1189 | ||
1190 | if (free_ximg) | |
547c9269 | 1191 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); |
4ef0d4d0 | 1192 | |
6ac3c7bb | 1193 | img->background = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
1194 | |
1195 | if (free_ximg) | |
547c9269 | 1196 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
1f026899 | 1197 | |
4ef0d4d0 KS |
1198 | img->background_valid = 1; |
1199 | } | |
1200 | ||
1201 | return img->background; | |
1202 | } | |
1203 | ||
1204 | /* Return the `background_transparent' field of IMG. If IMG doesn't | |
1205 | have one yet, it is guessed heuristically. If non-zero, MASK is an | |
1206 | existing XImage object to use for the heuristic. */ | |
1207 | ||
1208 | int | |
d3da34e0 | 1209 | image_background_transparent (struct image *img, struct frame *f, XImagePtr_or_DC mask) |
4ef0d4d0 KS |
1210 | { |
1211 | if (! img->background_transparent_valid) | |
1212 | /* IMG doesn't have a background yet, try to guess a reasonable value. */ | |
1213 | { | |
1214 | if (img->mask) | |
1215 | { | |
578098f3 | 1216 | bool free_mask = !mask; |
4ef0d4d0 KS |
1217 | #ifdef HAVE_NTGUI |
1218 | HGDIOBJ prev; | |
1219 | #endif /* HAVE_NTGUI */ | |
1220 | ||
1221 | if (free_mask) | |
547c9269 | 1222 | mask = image_get_x_image_or_dc (f, img, 1, &prev); |
4ef0d4d0 KS |
1223 | |
1224 | img->background_transparent | |
6ac3c7bb | 1225 | = (four_corners_best (mask, img->corners, img->width, img->height) == PIX_MASK_RETAIN); |
4ef0d4d0 KS |
1226 | |
1227 | if (free_mask) | |
547c9269 | 1228 | image_unget_x_image_or_dc (img, 1, mask, prev); |
4ef0d4d0 KS |
1229 | } |
1230 | else | |
1231 | img->background_transparent = 0; | |
1232 | ||
1233 | img->background_transparent_valid = 1; | |
1234 | } | |
1235 | ||
1236 | return img->background_transparent; | |
1237 | } | |
1238 | ||
da11196a DA |
1239 | #if defined (HAVE_PNG) || defined (HAVE_NS) \ |
1240 | || defined (HAVE_IMAGEMAGICK) || defined (HAVE_RSVG) | |
1241 | ||
f62bc934 PE |
1242 | /* Store F's background color into *BGCOLOR. */ |
1243 | static void | |
1244 | x_query_frame_background_color (struct frame *f, XColor *bgcolor) | |
1245 | { | |
1246 | #ifndef HAVE_NS | |
1247 | bgcolor->pixel = FRAME_BACKGROUND_PIXEL (f); | |
1248 | x_query_color (f, bgcolor); | |
1249 | #else | |
1250 | ns_query_color (FRAME_BACKGROUND_COLOR (f), bgcolor, 1); | |
1251 | #endif | |
1252 | } | |
1253 | ||
da11196a DA |
1254 | #endif /* HAVE_PNG || HAVE_NS || HAVE_IMAGEMAGICK || HAVE_RSVG */ |
1255 | ||
4ef0d4d0 KS |
1256 | /*********************************************************************** |
1257 | Helper functions for X image types | |
1258 | ***********************************************************************/ | |
1259 | ||
547c9269 YM |
1260 | /* Clear X resources of image IMG on frame F according to FLAGS. |
1261 | FLAGS is bitwise-or of the following masks: | |
1262 | CLEAR_IMAGE_PIXMAP free the pixmap if any. | |
1263 | CLEAR_IMAGE_MASK means clear the mask pixmap if any. | |
1264 | CLEAR_IMAGE_COLORS means free colors allocated for the image, if | |
1265 | any. */ | |
1266 | ||
1267 | #define CLEAR_IMAGE_PIXMAP (1 << 0) | |
1268 | #define CLEAR_IMAGE_MASK (1 << 1) | |
1269 | #define CLEAR_IMAGE_COLORS (1 << 2) | |
4ef0d4d0 KS |
1270 | |
1271 | static void | |
547c9269 | 1272 | x_clear_image_1 (struct frame *f, struct image *img, int flags) |
4ef0d4d0 | 1273 | { |
547c9269 | 1274 | if (flags & CLEAR_IMAGE_PIXMAP) |
4ef0d4d0 | 1275 | { |
547c9269 YM |
1276 | if (img->pixmap) |
1277 | { | |
1278 | Free_Pixmap (FRAME_X_DISPLAY (f), img->pixmap); | |
1279 | img->pixmap = NO_PIXMAP; | |
1280 | /* NOTE (HAVE_NS): background color is NOT an indexed color! */ | |
1281 | img->background_valid = 0; | |
1282 | } | |
1283 | #ifdef HAVE_X_WINDOWS | |
1284 | if (img->ximg) | |
1285 | { | |
1286 | x_destroy_x_image (img->ximg); | |
1287 | img->ximg = NULL; | |
1288 | img->background_valid = 0; | |
1289 | } | |
1290 | #endif | |
4ef0d4d0 KS |
1291 | } |
1292 | ||
547c9269 | 1293 | if (flags & CLEAR_IMAGE_MASK) |
4ef0d4d0 | 1294 | { |
547c9269 YM |
1295 | if (img->mask) |
1296 | { | |
1297 | Free_Pixmap (FRAME_X_DISPLAY (f), img->mask); | |
1298 | img->mask = NO_PIXMAP; | |
1299 | img->background_transparent_valid = 0; | |
1300 | } | |
1301 | #ifdef HAVE_X_WINDOWS | |
1302 | if (img->mask_img) | |
1303 | { | |
1304 | x_destroy_x_image (img->mask_img); | |
1305 | img->mask_img = NULL; | |
1306 | img->background_transparent_valid = 0; | |
1307 | } | |
1308 | #endif | |
4ef0d4d0 KS |
1309 | } |
1310 | ||
547c9269 | 1311 | if ((flags & CLEAR_IMAGE_COLORS) && img->ncolors) |
4ef0d4d0 | 1312 | { |
4ef0d4d0 KS |
1313 | /* W32_TODO: color table support. */ |
1314 | #ifdef HAVE_X_WINDOWS | |
1315 | x_free_colors (f, img->colors, img->ncolors); | |
1316 | #endif /* HAVE_X_WINDOWS */ | |
1317 | xfree (img->colors); | |
1318 | img->colors = NULL; | |
1319 | img->ncolors = 0; | |
1320 | } | |
c7fea325 | 1321 | |
4ef0d4d0 KS |
1322 | } |
1323 | ||
1324 | /* Free X resources of image IMG which is used on frame F. */ | |
1325 | ||
1326 | static void | |
d3da34e0 | 1327 | x_clear_image (struct frame *f, struct image *img) |
4ef0d4d0 | 1328 | { |
4d7e6e51 | 1329 | block_input (); |
547c9269 YM |
1330 | x_clear_image_1 (f, img, |
1331 | CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_MASK | CLEAR_IMAGE_COLORS); | |
4d7e6e51 | 1332 | unblock_input (); |
4ef0d4d0 KS |
1333 | } |
1334 | ||
1335 | ||
1336 | /* Allocate color COLOR_NAME for image IMG on frame F. If color | |
1337 | cannot be allocated, use DFLT. Add a newly allocated color to | |
1338 | IMG->colors, so that it can be freed again. Value is the pixel | |
1339 | color. */ | |
1340 | ||
1341 | static unsigned long | |
d3da34e0 JB |
1342 | x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name, |
1343 | unsigned long dflt) | |
4ef0d4d0 KS |
1344 | { |
1345 | XColor color; | |
1346 | unsigned long result; | |
1347 | ||
a54e2c05 | 1348 | eassert (STRINGP (color_name)); |
4ef0d4d0 | 1349 | |
3f791afe PE |
1350 | if (x_defined_color (f, SSDATA (color_name), &color, 1) |
1351 | && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors, | |
1352 | INT_MAX)) | |
4ef0d4d0 KS |
1353 | { |
1354 | /* This isn't called frequently so we get away with simply | |
1355 | reallocating the color vector to the needed size, here. */ | |
ddff3151 | 1356 | ptrdiff_t ncolors = img->ncolors + 1; |
38182d90 | 1357 | img->colors = xrealloc (img->colors, ncolors * sizeof *img->colors); |
ddff3151 PE |
1358 | img->colors[ncolors - 1] = color.pixel; |
1359 | img->ncolors = ncolors; | |
4ef0d4d0 KS |
1360 | result = color.pixel; |
1361 | } | |
1362 | else | |
1363 | result = dflt; | |
1364 | ||
1365 | return result; | |
1366 | } | |
1367 | ||
1368 | ||
1369 | \f | |
1370 | /*********************************************************************** | |
1371 | Image Cache | |
1372 | ***********************************************************************/ | |
1373 | ||
f57e2426 | 1374 | static void cache_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
1375 | |
1376 | /* Return a new, initialized image cache that is allocated from the | |
1377 | heap. Call free_image_cache to free an image cache. */ | |
1378 | ||
1379 | struct image_cache * | |
d3da34e0 | 1380 | make_image_cache (void) |
4ef0d4d0 | 1381 | { |
9c253307 DA |
1382 | struct image_cache *c = xmalloc (sizeof *c); |
1383 | ||
1384 | c->size = 50; | |
1385 | c->used = c->refcount = 0; | |
1386 | c->images = xmalloc (c->size * sizeof *c->images); | |
1387 | c->buckets = xzalloc (IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets); | |
4ef0d4d0 KS |
1388 | return c; |
1389 | } | |
1390 | ||
1391 | ||
d3c35d17 CY |
1392 | /* Find an image matching SPEC in the cache, and return it. If no |
1393 | image is found, return NULL. */ | |
1394 | static struct image * | |
0de4bb68 | 1395 | search_image_cache (struct frame *f, Lisp_Object spec, EMACS_UINT hash) |
483e7ae0 | 1396 | { |
91be6b66 | 1397 | struct image *img; |
354884c4 | 1398 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
483e7ae0 CY |
1399 | int i = hash % IMAGE_CACHE_BUCKETS_SIZE; |
1400 | ||
8198352b CY |
1401 | if (!c) return NULL; |
1402 | ||
d3c35d17 CY |
1403 | /* If the image spec does not specify a background color, the cached |
1404 | image must have the same background color as the current frame. | |
cf26ebe8 CY |
1405 | The foreground color must also match, for the sake of monochrome |
1406 | images. | |
1407 | ||
1408 | In fact, we could ignore the foreground color matching condition | |
1409 | for color images, or if the image spec specifies :foreground; | |
1410 | similarly we could ignore the background color matching condition | |
1411 | for formats that don't use transparency (such as jpeg), or if the | |
1412 | image spec specifies :background. However, the extra memory | |
1413 | usage is probably negligible in practice, so we don't bother. */ | |
d3c35d17 | 1414 | |
483e7ae0 | 1415 | for (img = c->buckets[i]; img; img = img->next) |
d3c35d17 CY |
1416 | if (img->hash == hash |
1417 | && !NILP (Fequal (img->spec, spec)) | |
c5b8e0ea YM |
1418 | && img->frame_foreground == FRAME_FOREGROUND_PIXEL (f) |
1419 | && img->frame_background == FRAME_BACKGROUND_PIXEL (f)) | |
d3c35d17 CY |
1420 | break; |
1421 | return img; | |
1422 | } | |
1423 | ||
1424 | ||
1425 | /* Search frame F for an image with spec SPEC, and free it. */ | |
1426 | ||
1427 | static void | |
d3da34e0 | 1428 | uncache_image (struct frame *f, Lisp_Object spec) |
d3c35d17 CY |
1429 | { |
1430 | struct image *img = search_image_cache (f, spec, sxhash (spec, 0)); | |
1431 | if (img) | |
be3faa80 CY |
1432 | { |
1433 | free_image (f, img); | |
1434 | /* As display glyphs may still be referring to the image ID, we | |
1435 | must garbage the frame (Bug#6426). */ | |
1436 | SET_FRAME_GARBAGED (f); | |
1437 | } | |
483e7ae0 CY |
1438 | } |
1439 | ||
1440 | ||
4ef0d4d0 KS |
1441 | /* Free image cache of frame F. Be aware that X frames share images |
1442 | caches. */ | |
1443 | ||
1444 | void | |
d3da34e0 | 1445 | free_image_cache (struct frame *f) |
4ef0d4d0 | 1446 | { |
354884c4 | 1447 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 KS |
1448 | if (c) |
1449 | { | |
13464394 | 1450 | ptrdiff_t i; |
4ef0d4d0 KS |
1451 | |
1452 | /* Cache should not be referenced by any frame when freed. */ | |
a54e2c05 | 1453 | eassert (c->refcount == 0); |
4ef0d4d0 KS |
1454 | |
1455 | for (i = 0; i < c->used; ++i) | |
1456 | free_image (f, c->images[i]); | |
1457 | xfree (c->images); | |
1458 | xfree (c->buckets); | |
1459 | xfree (c); | |
354884c4 | 1460 | FRAME_IMAGE_CACHE (f) = NULL; |
4ef0d4d0 KS |
1461 | } |
1462 | } | |
1463 | ||
1464 | ||
a2bc5bdd SM |
1465 | /* Clear image cache of frame F. FILTER=t means free all images. |
1466 | FILTER=nil means clear only images that haven't been | |
1467 | displayed for some time. | |
1468 | Else, only free the images which have FILTER in their `dependencies'. | |
1469 | Should be called from time to time to reduce the number of loaded images. | |
1470 | If image-cache-eviction-delay is non-nil, this frees images in the cache | |
1471 | which weren't displayed for at least that many seconds. */ | |
4ef0d4d0 | 1472 | |
ce0ad53d | 1473 | static void |
a2bc5bdd | 1474 | clear_image_cache (struct frame *f, Lisp_Object filter) |
4ef0d4d0 | 1475 | { |
354884c4 | 1476 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
4ef0d4d0 | 1477 | |
98fe5161 | 1478 | if (c) |
4ef0d4d0 | 1479 | { |
13464394 | 1480 | ptrdiff_t i, nfreed = 0; |
4ef0d4d0 KS |
1481 | |
1482 | /* Block input so that we won't be interrupted by a SIGIO | |
1483 | while being in an inconsistent state. */ | |
4d7e6e51 | 1484 | block_input (); |
4ef0d4d0 | 1485 | |
98fe5161 CY |
1486 | if (!NILP (filter)) |
1487 | { | |
1488 | /* Filter image cache. */ | |
1489 | for (i = 0; i < c->used; ++i) | |
1490 | { | |
1491 | struct image *img = c->images[i]; | |
1492 | if (img && (EQ (Qt, filter) | |
1493 | || !NILP (Fmember (filter, img->dependencies)))) | |
1494 | { | |
1495 | free_image (f, img); | |
1496 | ++nfreed; | |
1497 | } | |
1498 | } | |
1499 | } | |
1500 | else if (INTEGERP (Vimage_cache_eviction_delay)) | |
4ef0d4d0 | 1501 | { |
98fe5161 | 1502 | /* Free cache based on timestamp. */ |
43aac990 | 1503 | struct timespec old, t; |
d35af63c | 1504 | double delay; |
13464394 | 1505 | ptrdiff_t nimages = 0; |
98fe5161 CY |
1506 | |
1507 | for (i = 0; i < c->used; ++i) | |
1508 | if (c->images[i]) | |
1509 | nimages++; | |
1510 | ||
1511 | /* If the number of cached images has grown unusually large, | |
1512 | decrease the cache eviction delay (Bug#6230). */ | |
13464394 | 1513 | delay = XINT (Vimage_cache_eviction_delay); |
98fe5161 | 1514 | if (nimages > 40) |
13464394 PE |
1515 | delay = 1600 * delay / nimages / nimages; |
1516 | delay = max (delay, 1); | |
98fe5161 | 1517 | |
43aac990 PE |
1518 | t = current_timespec (); |
1519 | old = timespec_sub (t, dtotimespec (delay)); | |
98fe5161 CY |
1520 | |
1521 | for (i = 0; i < c->used; ++i) | |
4ef0d4d0 | 1522 | { |
98fe5161 | 1523 | struct image *img = c->images[i]; |
43aac990 | 1524 | if (img && timespec_cmp (img->timestamp, old) < 0) |
98fe5161 CY |
1525 | { |
1526 | free_image (f, img); | |
1527 | ++nfreed; | |
1528 | } | |
4ef0d4d0 KS |
1529 | } |
1530 | } | |
1531 | ||
1532 | /* We may be clearing the image cache because, for example, | |
1533 | Emacs was iconified for a longer period of time. In that | |
1534 | case, current matrices may still contain references to | |
1535 | images freed above. So, clear these matrices. */ | |
1536 | if (nfreed) | |
1537 | { | |
1538 | Lisp_Object tail, frame; | |
1539 | ||
1540 | FOR_EACH_FRAME (tail, frame) | |
1541 | { | |
6ae141d6 PE |
1542 | struct frame *fr = XFRAME (frame); |
1543 | if (FRAME_IMAGE_CACHE (fr) == c) | |
1544 | clear_current_matrices (fr); | |
4ef0d4d0 KS |
1545 | } |
1546 | ||
94ae2ad4 | 1547 | windows_or_buffers_changed = 19; |
4ef0d4d0 KS |
1548 | } |
1549 | ||
4d7e6e51 | 1550 | unblock_input (); |
4ef0d4d0 KS |
1551 | } |
1552 | } | |
1553 | ||
354884c4 | 1554 | void |
a2bc5bdd | 1555 | clear_image_caches (Lisp_Object filter) |
354884c4 SM |
1556 | { |
1557 | /* FIXME: We want to do | |
1558 | * struct terminal *t; | |
1559 | * for (t = terminal_list; t; t = t->next_terminal) | |
1560 | * clear_image_cache (t, filter); */ | |
1561 | Lisp_Object tail, frame; | |
1562 | FOR_EACH_FRAME (tail, frame) | |
1563 | if (FRAME_WINDOW_P (XFRAME (frame))) | |
a2bc5bdd | 1564 | clear_image_cache (XFRAME (frame), filter); |
354884c4 | 1565 | } |
4ef0d4d0 KS |
1566 | |
1567 | DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache, | |
1568 | 0, 1, 0, | |
a2bc5bdd SM |
1569 | doc: /* Clear the image cache. |
1570 | FILTER nil or a frame means clear all images in the selected frame. | |
1571 | FILTER t means clear the image caches of all frames. | |
1572 | Anything else, means only clear those images which refer to FILTER, | |
1573 | which is then usually a filename. */) | |
5842a27b | 1574 | (Lisp_Object filter) |
4ef0d4d0 | 1575 | { |
a2bc5bdd SM |
1576 | if (!(EQ (filter, Qnil) || FRAMEP (filter))) |
1577 | clear_image_caches (filter); | |
4ef0d4d0 | 1578 | else |
7452b7bd | 1579 | clear_image_cache (decode_window_system_frame (filter), Qt); |
4ef0d4d0 KS |
1580 | |
1581 | return Qnil; | |
1582 | } | |
1583 | ||
1584 | ||
110683ad | 1585 | DEFUN ("image-flush", Fimage_flush, Simage_flush, |
483e7ae0 | 1586 | 1, 2, 0, |
8bfcc21a | 1587 | doc: /* Flush the image with specification SPEC on frame FRAME. |
110683ad CY |
1588 | This removes the image from the Emacs image cache. If SPEC specifies |
1589 | an image file, the next redisplay of this image will read from the | |
1590 | current contents of that file. | |
1591 | ||
483e7ae0 CY |
1592 | FRAME nil or omitted means use the selected frame. |
1593 | FRAME t means refresh the image on all frames. */) | |
5842a27b | 1594 | (Lisp_Object spec, Lisp_Object frame) |
483e7ae0 CY |
1595 | { |
1596 | if (!valid_image_p (spec)) | |
1597 | error ("Invalid image specification"); | |
1598 | ||
1599 | if (EQ (frame, Qt)) | |
1600 | { | |
1601 | Lisp_Object tail; | |
1602 | FOR_EACH_FRAME (tail, frame) | |
1603 | { | |
1604 | struct frame *f = XFRAME (frame); | |
1605 | if (FRAME_WINDOW_P (f)) | |
1606 | uncache_image (f, spec); | |
1607 | } | |
1608 | } | |
1609 | else | |
7452b7bd | 1610 | uncache_image (decode_window_system_frame (frame), spec); |
483e7ae0 CY |
1611 | |
1612 | return Qnil; | |
1613 | } | |
1614 | ||
1615 | ||
4ef0d4d0 KS |
1616 | /* Compute masks and transform image IMG on frame F, as specified |
1617 | by the image's specification, */ | |
1618 | ||
1619 | static void | |
d3da34e0 | 1620 | postprocess_image (struct frame *f, struct image *img) |
4ef0d4d0 KS |
1621 | { |
1622 | /* Manipulation of the image's mask. */ | |
1623 | if (img->pixmap) | |
1624 | { | |
1625 | Lisp_Object conversion, spec; | |
1626 | Lisp_Object mask; | |
1627 | ||
1628 | spec = img->spec; | |
1629 | ||
1630 | /* `:heuristic-mask t' | |
1631 | `:mask heuristic' | |
1632 | means build a mask heuristically. | |
1633 | `:heuristic-mask (R G B)' | |
1634 | `:mask (heuristic (R G B))' | |
1635 | means build a mask from color (R G B) in the | |
1636 | image. | |
1637 | `:mask nil' | |
1638 | means remove a mask, if any. */ | |
1639 | ||
1640 | mask = image_spec_value (spec, QCheuristic_mask, NULL); | |
1641 | if (!NILP (mask)) | |
1642 | x_build_heuristic_mask (f, img, mask); | |
1643 | else | |
1644 | { | |
578098f3 | 1645 | bool found_p; |
4ef0d4d0 KS |
1646 | |
1647 | mask = image_spec_value (spec, QCmask, &found_p); | |
1648 | ||
1649 | if (EQ (mask, Qheuristic)) | |
1650 | x_build_heuristic_mask (f, img, Qt); | |
1651 | else if (CONSP (mask) | |
1652 | && EQ (XCAR (mask), Qheuristic)) | |
1653 | { | |
1654 | if (CONSP (XCDR (mask))) | |
1655 | x_build_heuristic_mask (f, img, XCAR (XCDR (mask))); | |
1656 | else | |
1657 | x_build_heuristic_mask (f, img, XCDR (mask)); | |
1658 | } | |
1659 | else if (NILP (mask) && found_p && img->mask) | |
547c9269 | 1660 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
4ef0d4d0 KS |
1661 | } |
1662 | ||
1663 | ||
1664 | /* Should we apply an image transformation algorithm? */ | |
1665 | conversion = image_spec_value (spec, QCconversion, NULL); | |
1666 | if (EQ (conversion, Qdisabled)) | |
1667 | x_disable_image (f, img); | |
1668 | else if (EQ (conversion, Qlaplace)) | |
1669 | x_laplace (f, img); | |
1670 | else if (EQ (conversion, Qemboss)) | |
1671 | x_emboss (f, img); | |
1672 | else if (CONSP (conversion) | |
1673 | && EQ (XCAR (conversion), Qedge_detection)) | |
1674 | { | |
1675 | Lisp_Object tem; | |
1676 | tem = XCDR (conversion); | |
1677 | if (CONSP (tem)) | |
1678 | x_edge_detection (f, img, | |
1679 | Fplist_get (tem, QCmatrix), | |
1680 | Fplist_get (tem, QCcolor_adjustment)); | |
1681 | } | |
1682 | } | |
1683 | } | |
1684 | ||
1685 | ||
1686 | /* Return the id of image with Lisp specification SPEC on frame F. | |
1687 | SPEC must be a valid Lisp image specification (see valid_image_p). */ | |
1688 | ||
13464394 | 1689 | ptrdiff_t |
d3da34e0 | 1690 | lookup_image (struct frame *f, Lisp_Object spec) |
4ef0d4d0 | 1691 | { |
4ef0d4d0 | 1692 | struct image *img; |
0de4bb68 | 1693 | EMACS_UINT hash; |
4ef0d4d0 KS |
1694 | |
1695 | /* F must be a window-system frame, and SPEC must be a valid image | |
1696 | specification. */ | |
a54e2c05 DA |
1697 | eassert (FRAME_WINDOW_P (f)); |
1698 | eassert (valid_image_p (spec)); | |
4ef0d4d0 | 1699 | |
4ef0d4d0 KS |
1700 | /* Look up SPEC in the hash table of the image cache. */ |
1701 | hash = sxhash (spec, 0); | |
d3c35d17 | 1702 | img = search_image_cache (f, spec, hash); |
f1f25b99 CY |
1703 | if (img && img->load_failed_p) |
1704 | { | |
1705 | free_image (f, img); | |
1706 | img = NULL; | |
1707 | } | |
1708 | ||
4ef0d4d0 KS |
1709 | /* If not found, create a new image and cache it. */ |
1710 | if (img == NULL) | |
1711 | { | |
4d7e6e51 | 1712 | block_input (); |
4ef0d4d0 KS |
1713 | img = make_image (spec, hash); |
1714 | cache_image (f, img); | |
578098f3 | 1715 | img->load_failed_p = ! img->type->load (f, img); |
c5b8e0ea YM |
1716 | img->frame_foreground = FRAME_FOREGROUND_PIXEL (f); |
1717 | img->frame_background = FRAME_BACKGROUND_PIXEL (f); | |
4ef0d4d0 KS |
1718 | |
1719 | /* If we can't load the image, and we don't have a width and | |
1720 | height, use some arbitrary width and height so that we can | |
1721 | draw a rectangle for it. */ | |
1722 | if (img->load_failed_p) | |
1723 | { | |
1724 | Lisp_Object value; | |
1725 | ||
1726 | value = image_spec_value (spec, QCwidth, NULL); | |
1727 | img->width = (INTEGERP (value) | |
1728 | ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH); | |
1729 | value = image_spec_value (spec, QCheight, NULL); | |
1730 | img->height = (INTEGERP (value) | |
1731 | ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT); | |
1732 | } | |
1733 | else | |
1734 | { | |
1735 | /* Handle image type independent image attributes | |
1736 | `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF', | |
1737 | `:background COLOR'. */ | |
1738 | Lisp_Object ascent, margin, relief, bg; | |
d311d28c | 1739 | int relief_bound; |
4ef0d4d0 KS |
1740 | |
1741 | ascent = image_spec_value (spec, QCascent, NULL); | |
1742 | if (INTEGERP (ascent)) | |
1743 | img->ascent = XFASTINT (ascent); | |
1744 | else if (EQ (ascent, Qcenter)) | |
1745 | img->ascent = CENTERED_IMAGE_ASCENT; | |
1746 | ||
1747 | margin = image_spec_value (spec, QCmargin, NULL); | |
13464394 | 1748 | if (INTEGERP (margin)) |
4ef0d4d0 | 1749 | img->vmargin = img->hmargin = XFASTINT (margin); |
13464394 | 1750 | else if (CONSP (margin)) |
4ef0d4d0 | 1751 | { |
13464394 PE |
1752 | img->hmargin = XFASTINT (XCAR (margin)); |
1753 | img->vmargin = XFASTINT (XCDR (margin)); | |
4ef0d4d0 KS |
1754 | } |
1755 | ||
1756 | relief = image_spec_value (spec, QCrelief, NULL); | |
d311d28c PE |
1757 | relief_bound = INT_MAX - max (img->hmargin, img->vmargin); |
1758 | if (RANGED_INTEGERP (- relief_bound, relief, relief_bound)) | |
4ef0d4d0 KS |
1759 | { |
1760 | img->relief = XINT (relief); | |
1ea40aa2 EZ |
1761 | img->hmargin += eabs (img->relief); |
1762 | img->vmargin += eabs (img->relief); | |
4ef0d4d0 KS |
1763 | } |
1764 | ||
1765 | if (! img->background_valid) | |
1766 | { | |
1767 | bg = image_spec_value (img->spec, QCbackground, NULL); | |
1768 | if (!NILP (bg)) | |
1769 | { | |
1770 | img->background | |
1771 | = x_alloc_image_color (f, img, bg, | |
1772 | FRAME_BACKGROUND_PIXEL (f)); | |
1773 | img->background_valid = 1; | |
1774 | } | |
1775 | } | |
1776 | ||
1777 | /* Do image transformations and compute masks, unless we | |
1778 | don't have the image yet. */ | |
1779 | if (!EQ (*img->type->type, Qpostscript)) | |
1780 | postprocess_image (f, img); | |
1781 | } | |
1782 | ||
4d7e6e51 | 1783 | unblock_input (); |
4ef0d4d0 KS |
1784 | } |
1785 | ||
1786 | /* We're using IMG, so set its timestamp to `now'. */ | |
43aac990 | 1787 | img->timestamp = current_timespec (); |
4ef0d4d0 | 1788 | |
4ef0d4d0 KS |
1789 | /* Value is the image id. */ |
1790 | return img->id; | |
1791 | } | |
1792 | ||
1793 | ||
1794 | /* Cache image IMG in the image cache of frame F. */ | |
1795 | ||
1796 | static void | |
d3da34e0 | 1797 | cache_image (struct frame *f, struct image *img) |
4ef0d4d0 | 1798 | { |
354884c4 | 1799 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
13464394 | 1800 | ptrdiff_t i; |
4ef0d4d0 KS |
1801 | |
1802 | /* Find a free slot in c->images. */ | |
1803 | for (i = 0; i < c->used; ++i) | |
1804 | if (c->images[i] == NULL) | |
1805 | break; | |
1806 | ||
1807 | /* If no free slot found, maybe enlarge c->images. */ | |
1808 | if (i == c->used && c->used == c->size) | |
a69fbedb | 1809 | c->images = xpalloc (c->images, &c->size, 1, -1, sizeof *c->images); |
4ef0d4d0 KS |
1810 | |
1811 | /* Add IMG to c->images, and assign IMG an id. */ | |
1812 | c->images[i] = img; | |
1813 | img->id = i; | |
1814 | if (i == c->used) | |
1815 | ++c->used; | |
1816 | ||
1817 | /* Add IMG to the cache's hash table. */ | |
1818 | i = img->hash % IMAGE_CACHE_BUCKETS_SIZE; | |
1819 | img->next = c->buckets[i]; | |
1820 | if (img->next) | |
1821 | img->next->prev = img; | |
1822 | img->prev = NULL; | |
1823 | c->buckets[i] = img; | |
1824 | } | |
1825 | ||
1826 | ||
1827 | /* Call FN on every image in the image cache of frame F. Used to mark | |
1828 | Lisp Objects in the image cache. */ | |
1829 | ||
354884c4 SM |
1830 | /* Mark Lisp objects in image IMG. */ |
1831 | ||
1832 | static void | |
d3da34e0 | 1833 | mark_image (struct image *img) |
354884c4 SM |
1834 | { |
1835 | mark_object (img->spec); | |
a2bc5bdd | 1836 | mark_object (img->dependencies); |
354884c4 | 1837 | |
b50691aa CY |
1838 | if (!NILP (img->lisp_data)) |
1839 | mark_object (img->lisp_data); | |
354884c4 SM |
1840 | } |
1841 | ||
1842 | ||
4ef0d4d0 | 1843 | void |
354884c4 | 1844 | mark_image_cache (struct image_cache *c) |
4ef0d4d0 | 1845 | { |
354884c4 | 1846 | if (c) |
4ef0d4d0 | 1847 | { |
13464394 | 1848 | ptrdiff_t i; |
354884c4 SM |
1849 | for (i = 0; i < c->used; ++i) |
1850 | if (c->images[i]) | |
1851 | mark_image (c->images[i]); | |
4ef0d4d0 KS |
1852 | } |
1853 | } | |
1854 | ||
1855 | ||
1856 | \f | |
1857 | /*********************************************************************** | |
9e2a2647 | 1858 | X / NS / W32 support code |
4ef0d4d0 KS |
1859 | ***********************************************************************/ |
1860 | ||
0fda9b75 | 1861 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
1862 | |
1863 | /* Macro for defining functions that will be loaded from image DLLs. */ | |
dbdb9a7c | 1864 | #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args |
4ef0d4d0 KS |
1865 | |
1866 | /* Macro for loading those image functions from the library. */ | |
1867 | #define LOAD_IMGLIB_FN(lib,func) { \ | |
1868 | fn_##func = (void *) GetProcAddress (lib, #func); \ | |
1869 | if (!fn_##func) return 0; \ | |
1870 | } | |
1871 | ||
0fda9b75 | 1872 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 1873 | |
578098f3 | 1874 | /* Return true if XIMG's size WIDTH x HEIGHT doesn't break the |
476371c4 | 1875 | windowing system. |
ca4aa935 PE |
1876 | WIDTH and HEIGHT must both be positive. |
1877 | If XIMG is null, assume it is a bitmap. */ | |
578098f3 | 1878 | static bool |
ca4aa935 PE |
1879 | x_check_image_size (XImagePtr ximg, int width, int height) |
1880 | { | |
476371c4 | 1881 | #ifdef HAVE_X_WINDOWS |
ca4aa935 PE |
1882 | /* Respect Xlib's limits: it cannot deal with images that have more |
1883 | than INT_MAX (and/or UINT_MAX) bytes. And respect Emacs's limits | |
476371c4 | 1884 | of PTRDIFF_MAX (and/or SIZE_MAX) bytes for any object. */ |
ca4aa935 PE |
1885 | enum |
1886 | { | |
1887 | XLIB_BYTES_MAX = min (INT_MAX, UINT_MAX), | |
1888 | X_IMAGE_BYTES_MAX = min (XLIB_BYTES_MAX, min (PTRDIFF_MAX, SIZE_MAX)) | |
1889 | }; | |
1890 | ||
1891 | int bitmap_pad, depth, bytes_per_line; | |
1892 | if (ximg) | |
1893 | { | |
1894 | bitmap_pad = ximg->bitmap_pad; | |
1895 | depth = ximg->depth; | |
1896 | bytes_per_line = ximg->bytes_per_line; | |
1897 | } | |
1898 | else | |
1899 | { | |
1900 | bitmap_pad = 8; | |
1901 | depth = 1; | |
1902 | bytes_per_line = (width >> 3) + ((width & 7) != 0); | |
1903 | } | |
1904 | return (width <= (INT_MAX - (bitmap_pad - 1)) / depth | |
1905 | && height <= X_IMAGE_BYTES_MAX / bytes_per_line); | |
476371c4 PE |
1906 | #else |
1907 | /* FIXME: Implement this check for the HAVE_NS and HAVE_NTGUI cases. | |
1908 | For now, assume that every image size is allowed on these systems. */ | |
1909 | return 1; | |
1910 | #endif | |
ca4aa935 | 1911 | } |
4ef0d4d0 KS |
1912 | |
1913 | /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on | |
1914 | frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created. | |
1915 | Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated | |
1916 | via xmalloc. Print error messages via image_error if an error | |
578098f3 | 1917 | occurs. Value is true if successful. |
4ef0d4d0 KS |
1918 | |
1919 | On W32, a DEPTH of zero signifies a 24 bit image, otherwise DEPTH | |
1920 | should indicate the bit depth of the image. */ | |
1921 | ||
578098f3 | 1922 | static bool |
d3da34e0 JB |
1923 | x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth, |
1924 | XImagePtr *ximg, Pixmap *pixmap) | |
4ef0d4d0 KS |
1925 | { |
1926 | #ifdef HAVE_X_WINDOWS | |
1927 | Display *display = FRAME_X_DISPLAY (f); | |
1928 | Window window = FRAME_X_WINDOW (f); | |
1929 | Screen *screen = FRAME_X_SCREEN (f); | |
1930 | ||
4d7e6e51 | 1931 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
1932 | |
1933 | if (depth <= 0) | |
1934 | depth = DefaultDepthOfScreen (screen); | |
1935 | *ximg = XCreateImage (display, DefaultVisualOfScreen (screen), | |
1936 | depth, ZPixmap, 0, NULL, width, height, | |
1937 | depth > 16 ? 32 : depth > 8 ? 16 : 8, 0); | |
1938 | if (*ximg == NULL) | |
1939 | { | |
1940 | image_error ("Unable to allocate X image", Qnil, Qnil); | |
1941 | return 0; | |
1942 | } | |
1943 | ||
ca4aa935 PE |
1944 | if (! x_check_image_size (*ximg, width, height)) |
1945 | { | |
1946 | x_destroy_x_image (*ximg); | |
1947 | *ximg = NULL; | |
1948 | image_error ("Image too large (%dx%d)", | |
1949 | make_number (width), make_number (height)); | |
1950 | return 0; | |
1951 | } | |
1952 | ||
4ef0d4d0 | 1953 | /* Allocate image raster. */ |
23f86fce | 1954 | (*ximg)->data = xmalloc ((*ximg)->bytes_per_line * height); |
4ef0d4d0 KS |
1955 | |
1956 | /* Allocate a pixmap of the same size. */ | |
1957 | *pixmap = XCreatePixmap (display, window, width, height, depth); | |
1958 | if (*pixmap == NO_PIXMAP) | |
1959 | { | |
1960 | x_destroy_x_image (*ximg); | |
1961 | *ximg = NULL; | |
1962 | image_error ("Unable to create X pixmap", Qnil, Qnil); | |
1963 | return 0; | |
1964 | } | |
1965 | ||
1966 | return 1; | |
1967 | #endif /* HAVE_X_WINDOWS */ | |
1968 | ||
1969 | #ifdef HAVE_NTGUI | |
1970 | ||
1971 | BITMAPINFOHEADER *header; | |
1972 | HDC hdc; | |
1973 | int scanline_width_bits; | |
1974 | int remainder; | |
1975 | int palette_colors = 0; | |
1976 | ||
1977 | if (depth == 0) | |
1978 | depth = 24; | |
1979 | ||
1980 | if (depth != 1 && depth != 4 && depth != 8 | |
1981 | && depth != 16 && depth != 24 && depth != 32) | |
1982 | { | |
1983 | image_error ("Invalid image bit depth specified", Qnil, Qnil); | |
1984 | return 0; | |
1985 | } | |
1986 | ||
1987 | scanline_width_bits = width * depth; | |
1988 | remainder = scanline_width_bits % 32; | |
1989 | ||
1990 | if (remainder) | |
1991 | scanline_width_bits += 32 - remainder; | |
1992 | ||
1993 | /* Bitmaps with a depth less than 16 need a palette. */ | |
1994 | /* BITMAPINFO structure already contains the first RGBQUAD. */ | |
1995 | if (depth < 16) | |
657d08d3 | 1996 | palette_colors = 1 << (depth - 1); |
4ef0d4d0 KS |
1997 | |
1998 | *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD)); | |
4ef0d4d0 | 1999 | |
72af86bd AS |
2000 | header = &(*ximg)->info.bmiHeader; |
2001 | memset (&(*ximg)->info, 0, sizeof (BITMAPINFO)); | |
4ef0d4d0 KS |
2002 | header->biSize = sizeof (*header); |
2003 | header->biWidth = width; | |
2004 | header->biHeight = -height; /* negative indicates a top-down bitmap. */ | |
2005 | header->biPlanes = 1; | |
2006 | header->biBitCount = depth; | |
2007 | header->biCompression = BI_RGB; | |
2008 | header->biClrUsed = palette_colors; | |
2009 | ||
2010 | /* TODO: fill in palette. */ | |
2011 | if (depth == 1) | |
2012 | { | |
2013 | (*ximg)->info.bmiColors[0].rgbBlue = 0; | |
2014 | (*ximg)->info.bmiColors[0].rgbGreen = 0; | |
2015 | (*ximg)->info.bmiColors[0].rgbRed = 0; | |
2016 | (*ximg)->info.bmiColors[0].rgbReserved = 0; | |
2017 | (*ximg)->info.bmiColors[1].rgbBlue = 255; | |
2018 | (*ximg)->info.bmiColors[1].rgbGreen = 255; | |
2019 | (*ximg)->info.bmiColors[1].rgbRed = 255; | |
2020 | (*ximg)->info.bmiColors[1].rgbReserved = 0; | |
2021 | } | |
2022 | ||
2023 | hdc = get_frame_dc (f); | |
2024 | ||
2025 | /* Create a DIBSection and raster array for the bitmap, | |
2026 | and store its handle in *pixmap. */ | |
2027 | *pixmap = CreateDIBSection (hdc, &((*ximg)->info), | |
2028 | (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS, | |
2e09fef1 EZ |
2029 | /* casting avoids a GCC warning */ |
2030 | (void **)&((*ximg)->data), NULL, 0); | |
4ef0d4d0 KS |
2031 | |
2032 | /* Realize display palette and garbage all frames. */ | |
2033 | release_frame_dc (f, hdc); | |
2034 | ||
2035 | if (*pixmap == NULL) | |
2036 | { | |
5fc9fdeb | 2037 | DWORD err = GetLastError (); |
4ef0d4d0 KS |
2038 | Lisp_Object errcode; |
2039 | /* All system errors are < 10000, so the following is safe. */ | |
13464394 | 2040 | XSETINT (errcode, err); |
4ef0d4d0 KS |
2041 | image_error ("Unable to create bitmap, error code %d", errcode, Qnil); |
2042 | x_destroy_x_image (*ximg); | |
c0213820 | 2043 | *ximg = NULL; |
4ef0d4d0 KS |
2044 | return 0; |
2045 | } | |
2046 | ||
2047 | return 1; | |
2048 | ||
2049 | #endif /* HAVE_NTGUI */ | |
2050 | ||
edfda783 | 2051 | #ifdef HAVE_NS |
ed3751c8 | 2052 | *pixmap = ns_image_for_XPM (width, height, depth); |
edfda783 AR |
2053 | if (*pixmap == 0) |
2054 | { | |
2055 | *ximg = NULL; | |
2056 | image_error ("Unable to allocate NSImage for XPM pixmap", Qnil, Qnil); | |
2057 | return 0; | |
2058 | } | |
2059 | *ximg = *pixmap; | |
2060 | return 1; | |
2061 | #endif | |
4ef0d4d0 KS |
2062 | } |
2063 | ||
2064 | ||
2065 | /* Destroy XImage XIMG. Free XIMG->data. */ | |
2066 | ||
2067 | static void | |
d3da34e0 | 2068 | x_destroy_x_image (XImagePtr ximg) |
4ef0d4d0 | 2069 | { |
4d7e6e51 | 2070 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
2071 | if (ximg) |
2072 | { | |
2073 | #ifdef HAVE_X_WINDOWS | |
2074 | xfree (ximg->data); | |
2075 | ximg->data = NULL; | |
2076 | XDestroyImage (ximg); | |
2077 | #endif /* HAVE_X_WINDOWS */ | |
2078 | #ifdef HAVE_NTGUI | |
2079 | /* Data will be freed by DestroyObject. */ | |
2080 | ximg->data = NULL; | |
2081 | xfree (ximg); | |
2082 | #endif /* HAVE_NTGUI */ | |
edfda783 | 2083 | #ifdef HAVE_NS |
ed3751c8 | 2084 | ns_release_object (ximg); |
edfda783 | 2085 | #endif /* HAVE_NS */ |
4ef0d4d0 KS |
2086 | } |
2087 | } | |
2088 | ||
2089 | ||
2090 | /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT | |
2091 | are width and height of both the image and pixmap. */ | |
2092 | ||
2093 | static void | |
d3da34e0 | 2094 | x_put_x_image (struct frame *f, XImagePtr ximg, Pixmap pixmap, int width, int height) |
4ef0d4d0 KS |
2095 | { |
2096 | #ifdef HAVE_X_WINDOWS | |
2097 | GC gc; | |
2098 | ||
4d7e6e51 | 2099 | eassert (input_blocked_p ()); |
4ef0d4d0 KS |
2100 | gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL); |
2101 | XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height); | |
2102 | XFreeGC (FRAME_X_DISPLAY (f), gc); | |
2103 | #endif /* HAVE_X_WINDOWS */ | |
2104 | ||
2105 | #ifdef HAVE_NTGUI | |
2106 | #if 0 /* I don't think this is necessary looking at where it is used. */ | |
2107 | HDC hdc = get_frame_dc (f); | |
2108 | SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS); | |
2109 | release_frame_dc (f, hdc); | |
2110 | #endif | |
2111 | #endif /* HAVE_NTGUI */ | |
2112 | ||
edfda783 | 2113 | #ifdef HAVE_NS |
a54e2c05 | 2114 | eassert (ximg == pixmap); |
ed3751c8 | 2115 | ns_retain_object (ximg); |
edfda783 | 2116 | #endif |
4ef0d4d0 KS |
2117 | } |
2118 | ||
547c9269 YM |
2119 | /* Thin wrapper for x_create_x_image_and_pixmap, so that it matches |
2120 | with image_put_x_image. */ | |
2121 | ||
2122 | static bool | |
2123 | image_create_x_image_and_pixmap (struct frame *f, struct image *img, | |
2124 | int width, int height, int depth, | |
2125 | XImagePtr *ximg, bool mask_p) | |
2126 | { | |
2127 | eassert ((!mask_p ? img->pixmap : img->mask) == NO_PIXMAP); | |
2128 | ||
2129 | return x_create_x_image_and_pixmap (f, width, height, depth, ximg, | |
2130 | !mask_p ? &img->pixmap : &img->mask); | |
2131 | } | |
2132 | ||
2133 | /* Put X image XIMG into image IMG on frame F, as a mask if and only | |
2134 | if MASK_P. On X, this simply records XIMG on a member of IMG, so | |
2135 | it can be put into the pixmap afterwards via image_sync_to_pixmaps. | |
2136 | On the other platforms, it puts XIMG into the pixmap, then frees | |
2137 | the X image and its buffer. */ | |
2138 | ||
2139 | static void | |
2140 | image_put_x_image (struct frame *f, struct image *img, XImagePtr ximg, | |
2141 | bool mask_p) | |
2142 | { | |
2143 | #ifdef HAVE_X_WINDOWS | |
2144 | if (!mask_p) | |
2145 | { | |
2146 | eassert (img->ximg == NULL); | |
2147 | img->ximg = ximg; | |
2148 | } | |
2149 | else | |
2150 | { | |
2151 | eassert (img->mask_img == NULL); | |
2152 | img->mask_img = ximg; | |
2153 | } | |
2154 | #else | |
2155 | x_put_x_image (f, ximg, !mask_p ? img->pixmap : img->mask, | |
2156 | img->width, img->height); | |
2157 | x_destroy_x_image (ximg); | |
2158 | #endif | |
2159 | } | |
2160 | ||
2161 | #ifdef HAVE_X_WINDOWS | |
2162 | /* Put the X images recorded in IMG on frame F into pixmaps, then free | |
2163 | the X images and their buffers. */ | |
2164 | ||
2165 | static void | |
2166 | image_sync_to_pixmaps (struct frame *f, struct image *img) | |
2167 | { | |
2168 | if (img->ximg) | |
2169 | { | |
2170 | x_put_x_image (f, img->ximg, img->pixmap, img->width, img->height); | |
2171 | x_destroy_x_image (img->ximg); | |
2172 | img->ximg = NULL; | |
2173 | } | |
2174 | if (img->mask_img) | |
2175 | { | |
2176 | x_put_x_image (f, img->mask_img, img->mask, img->width, img->height); | |
2177 | x_destroy_x_image (img->mask_img); | |
2178 | img->mask_img = NULL; | |
2179 | } | |
2180 | } | |
2181 | #endif | |
2182 | ||
2183 | #ifdef HAVE_NTGUI | |
2184 | /* Create a memory device context for IMG on frame F. It stores the | |
2185 | currently selected GDI object into *PREV for future restoration by | |
2186 | image_unget_x_image_or_dc. */ | |
2187 | ||
2188 | static XImagePtr_or_DC | |
2189 | image_get_x_image_or_dc (struct frame *f, struct image *img, bool mask_p, | |
2190 | HGDIOBJ *prev) | |
2191 | { | |
2192 | HDC frame_dc = get_frame_dc (f); | |
2193 | XImagePtr_or_DC ximg = CreateCompatibleDC (frame_dc); | |
2194 | ||
2195 | release_frame_dc (f, frame_dc); | |
2196 | *prev = SelectObject (ximg, !mask_p ? img->pixmap : img->mask); | |
b8075603 YM |
2197 | |
2198 | return ximg; | |
547c9269 YM |
2199 | } |
2200 | ||
b8075603 YM |
2201 | static void |
2202 | image_unget_x_image_or_dc (struct image *img, bool mask_p, | |
2203 | XImagePtr_or_DC ximg, HGDIOBJ prev) | |
547c9269 YM |
2204 | { |
2205 | SelectObject (ximg, prev); | |
2206 | DeleteDC (ximg); | |
2207 | } | |
2208 | #else /* !HAVE_NTGUI */ | |
2209 | /* Get the X image for IMG on frame F. The resulting X image data | |
2210 | should be treated as read-only at least on X. */ | |
2211 | ||
2212 | static XImagePtr | |
2213 | image_get_x_image (struct frame *f, struct image *img, bool mask_p) | |
2214 | { | |
2215 | #ifdef HAVE_X_WINDOWS | |
2216 | XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img; | |
2217 | ||
2218 | if (ximg_in_img) | |
2219 | return ximg_in_img; | |
2220 | else | |
2221 | return XGetImage (FRAME_X_DISPLAY (f), !mask_p ? img->pixmap : img->mask, | |
2222 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
2223 | #elif defined (HAVE_NS) | |
2224 | XImagePtr pixmap = !mask_p ? img->pixmap : img->mask; | |
2225 | ||
2226 | ns_retain_object (pixmap); | |
2227 | return pixmap; | |
2228 | #endif | |
2229 | } | |
2230 | ||
df03dc8a YM |
2231 | static void |
2232 | image_unget_x_image (struct image *img, bool mask_p, XImagePtr ximg) | |
547c9269 YM |
2233 | { |
2234 | #ifdef HAVE_X_WINDOWS | |
2235 | XImagePtr ximg_in_img = !mask_p ? img->ximg : img->mask_img; | |
2236 | ||
2237 | if (ximg_in_img) | |
2238 | eassert (ximg == ximg_in_img); | |
2239 | else | |
2240 | XDestroyImage (ximg); | |
2241 | #elif defined (HAVE_NS) | |
2242 | ns_release_object (ximg); | |
2243 | #endif | |
2244 | } | |
2245 | #endif /* !HAVE_NTGUI */ | |
2246 | ||
4ef0d4d0 KS |
2247 | \f |
2248 | /*********************************************************************** | |
2249 | File Handling | |
2250 | ***********************************************************************/ | |
2251 | ||
6678dc11 | 2252 | /* Find image file FILE. Look in data-directory/images, then |
022af124 KH |
2253 | x-bitmap-file-path. Value is the encoded full name of the file |
2254 | found, or nil if not found. */ | |
4ef0d4d0 | 2255 | |
c640aa9b | 2256 | Lisp_Object |
d3da34e0 | 2257 | x_find_image_file (Lisp_Object file) |
4ef0d4d0 KS |
2258 | { |
2259 | Lisp_Object file_found, search_path; | |
4ef0d4d0 KS |
2260 | int fd; |
2261 | ||
6678dc11 GM |
2262 | /* TODO I think this should use something like image-load-path |
2263 | instead. Unfortunately, that can contain non-string elements. */ | |
2264 | search_path = Fcons (Fexpand_file_name (build_string ("images"), | |
2265 | Vdata_directory), | |
2266 | Vx_bitmap_file_path); | |
4ef0d4d0 | 2267 | |
050b82f6 | 2268 | /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */ |
518c40a2 | 2269 | fd = openp (search_path, file, Qnil, &file_found, Qnil, false); |
4ef0d4d0 KS |
2270 | |
2271 | if (fd == -1) | |
2272 | file_found = Qnil; | |
2273 | else | |
022af124 KH |
2274 | { |
2275 | file_found = ENCODE_FILE (file_found); | |
f6774c1a AS |
2276 | if (fd != -2) |
2277 | emacs_close (fd); | |
022af124 | 2278 | } |
4ef0d4d0 | 2279 | |
4ef0d4d0 KS |
2280 | return file_found; |
2281 | } | |
2282 | ||
2283 | ||
2284 | /* Read FILE into memory. Value is a pointer to a buffer allocated | |
2285 | with xmalloc holding FILE's contents. Value is null if an error | |
2286 | occurred. *SIZE is set to the size of the file. */ | |
2287 | ||
2288 | static unsigned char * | |
dd52fcea | 2289 | slurp_file (char *file, ptrdiff_t *size) |
4ef0d4d0 | 2290 | { |
406af475 | 2291 | FILE *fp = emacs_fopen (file, "rb"); |
4ef0d4d0 KS |
2292 | unsigned char *buf = NULL; |
2293 | struct stat st; | |
2294 | ||
3f5bef16 | 2295 | if (fp) |
4ef0d4d0 | 2296 | { |
3f5bef16 PE |
2297 | ptrdiff_t count = SPECPDL_INDEX (); |
2298 | record_unwind_protect_ptr (fclose_unwind, fp); | |
2299 | ||
2300 | if (fstat (fileno (fp), &st) == 0 | |
2301 | && 0 <= st.st_size && st.st_size < min (PTRDIFF_MAX, SIZE_MAX)) | |
4ef0d4d0 | 2302 | { |
3f5bef16 PE |
2303 | /* Report an error if we read past the purported EOF. |
2304 | This can happen if the file grows as we read it. */ | |
2305 | ptrdiff_t buflen = st.st_size; | |
2306 | buf = xmalloc (buflen + 1); | |
2307 | if (fread (buf, 1, buflen + 1, fp) == buflen) | |
2308 | *size = buflen; | |
2309 | else | |
2310 | { | |
2311 | xfree (buf); | |
2312 | buf = NULL; | |
2313 | } | |
4ef0d4d0 | 2314 | } |
3f5bef16 PE |
2315 | |
2316 | unbind_to (count, Qnil); | |
4ef0d4d0 KS |
2317 | } |
2318 | ||
2319 | return buf; | |
2320 | } | |
2321 | ||
2322 | ||
2323 | \f | |
4ef0d4d0 KS |
2324 | /*********************************************************************** |
2325 | XBM images | |
2326 | ***********************************************************************/ | |
2327 | ||
578098f3 PE |
2328 | static bool xbm_load (struct frame *f, struct image *img); |
2329 | static bool xbm_image_p (Lisp_Object object); | |
2330 | static bool xbm_file_p (Lisp_Object); | |
4ef0d4d0 KS |
2331 | |
2332 | ||
2333 | /* Indices of image specification fields in xbm_format, below. */ | |
2334 | ||
2335 | enum xbm_keyword_index | |
2336 | { | |
2337 | XBM_TYPE, | |
2338 | XBM_FILE, | |
2339 | XBM_WIDTH, | |
2340 | XBM_HEIGHT, | |
2341 | XBM_DATA, | |
2342 | XBM_FOREGROUND, | |
2343 | XBM_BACKGROUND, | |
2344 | XBM_ASCENT, | |
2345 | XBM_MARGIN, | |
2346 | XBM_RELIEF, | |
2347 | XBM_ALGORITHM, | |
2348 | XBM_HEURISTIC_MASK, | |
2349 | XBM_MASK, | |
2350 | XBM_LAST | |
2351 | }; | |
2352 | ||
2353 | /* Vector of image_keyword structures describing the format | |
2354 | of valid XBM image specifications. */ | |
2355 | ||
91433552 | 2356 | static const struct image_keyword xbm_format[XBM_LAST] = |
4ef0d4d0 KS |
2357 | { |
2358 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
2359 | {":file", IMAGE_STRING_VALUE, 0}, | |
2360 | {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2361 | {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0}, | |
2362 | {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2363 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2364 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
2365 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 2366 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
2367 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
2368 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2369 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
2370 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0} | |
2371 | }; | |
2372 | ||
2373 | /* Structure describing the image type XBM. */ | |
2374 | ||
2375 | static struct image_type xbm_type = | |
2376 | { | |
2377 | &Qxbm, | |
2378 | xbm_image_p, | |
2379 | xbm_load, | |
2380 | x_clear_image, | |
bb4d86b4 | 2381 | NULL, |
4ef0d4d0 KS |
2382 | NULL |
2383 | }; | |
2384 | ||
2385 | /* Tokens returned from xbm_scan. */ | |
2386 | ||
2387 | enum xbm_token | |
2388 | { | |
2389 | XBM_TK_IDENT = 256, | |
2390 | XBM_TK_NUMBER | |
2391 | }; | |
2392 | ||
2393 | ||
578098f3 | 2394 | /* Return true if OBJECT is a valid XBM-type image specification. |
4ef0d4d0 KS |
2395 | A valid specification is a list starting with the symbol `image' |
2396 | The rest of the list is a property list which must contain an | |
578098f3 | 2397 | entry `:type xbm'. |
4ef0d4d0 KS |
2398 | |
2399 | If the specification specifies a file to load, it must contain | |
2400 | an entry `:file FILENAME' where FILENAME is a string. | |
2401 | ||
2402 | If the specification is for a bitmap loaded from memory it must | |
2403 | contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where | |
2404 | WIDTH and HEIGHT are integers > 0. DATA may be: | |
2405 | ||
2406 | 1. a string large enough to hold the bitmap data, i.e. it must | |
2407 | have a size >= (WIDTH + 7) / 8 * HEIGHT | |
2408 | ||
2409 | 2. a bool-vector of size >= WIDTH * HEIGHT | |
2410 | ||
2411 | 3. a vector of strings or bool-vectors, one for each line of the | |
2412 | bitmap. | |
2413 | ||
bbe6f2aa | 2414 | 4. a string containing an in-memory XBM file. WIDTH and HEIGHT |
4ef0d4d0 KS |
2415 | may not be specified in this case because they are defined in the |
2416 | XBM file. | |
2417 | ||
2418 | Both the file and data forms may contain the additional entries | |
2419 | `:background COLOR' and `:foreground COLOR'. If not present, | |
2420 | foreground and background of the frame on which the image is | |
2421 | displayed is used. */ | |
2422 | ||
578098f3 | 2423 | static bool |
d3da34e0 | 2424 | xbm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
2425 | { |
2426 | struct image_keyword kw[XBM_LAST]; | |
2427 | ||
72af86bd | 2428 | memcpy (kw, xbm_format, sizeof kw); |
4ef0d4d0 KS |
2429 | if (!parse_image_spec (object, kw, XBM_LAST, Qxbm)) |
2430 | return 0; | |
2431 | ||
a54e2c05 | 2432 | eassert (EQ (kw[XBM_TYPE].value, Qxbm)); |
4ef0d4d0 KS |
2433 | |
2434 | if (kw[XBM_FILE].count) | |
2435 | { | |
2436 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count) | |
2437 | return 0; | |
2438 | } | |
2439 | else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value)) | |
2440 | { | |
2441 | /* In-memory XBM file. */ | |
2442 | if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count) | |
2443 | return 0; | |
2444 | } | |
2445 | else | |
2446 | { | |
2447 | Lisp_Object data; | |
13464394 | 2448 | int width, height; |
4ef0d4d0 KS |
2449 | |
2450 | /* Entries for `:width', `:height' and `:data' must be present. */ | |
2451 | if (!kw[XBM_WIDTH].count | |
2452 | || !kw[XBM_HEIGHT].count | |
2453 | || !kw[XBM_DATA].count) | |
2454 | return 0; | |
2455 | ||
2456 | data = kw[XBM_DATA].value; | |
2457 | width = XFASTINT (kw[XBM_WIDTH].value); | |
2458 | height = XFASTINT (kw[XBM_HEIGHT].value); | |
2459 | ||
2460 | /* Check type of data, and width and height against contents of | |
2461 | data. */ | |
2462 | if (VECTORP (data)) | |
2463 | { | |
45aebb64 | 2464 | EMACS_INT i; |
4ef0d4d0 KS |
2465 | |
2466 | /* Number of elements of the vector must be >= height. */ | |
77b37c05 | 2467 | if (ASIZE (data) < height) |
4ef0d4d0 KS |
2468 | return 0; |
2469 | ||
2470 | /* Each string or bool-vector in data must be large enough | |
2471 | for one line of the image. */ | |
2472 | for (i = 0; i < height; ++i) | |
2473 | { | |
28be1ada | 2474 | Lisp_Object elt = AREF (data, i); |
4ef0d4d0 KS |
2475 | |
2476 | if (STRINGP (elt)) | |
2477 | { | |
2478 | if (SCHARS (elt) | |
2479 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR) | |
2480 | return 0; | |
2481 | } | |
2482 | else if (BOOL_VECTOR_P (elt)) | |
2483 | { | |
1c0a7493 | 2484 | if (bool_vector_size (elt) < width) |
4ef0d4d0 KS |
2485 | return 0; |
2486 | } | |
2487 | else | |
2488 | return 0; | |
2489 | } | |
2490 | } | |
2491 | else if (STRINGP (data)) | |
2492 | { | |
2493 | if (SCHARS (data) | |
2494 | < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height) | |
2495 | return 0; | |
2496 | } | |
2497 | else if (BOOL_VECTOR_P (data)) | |
2498 | { | |
1c0a7493 | 2499 | if (bool_vector_size (data) / height < width) |
4ef0d4d0 KS |
2500 | return 0; |
2501 | } | |
2502 | else | |
2503 | return 0; | |
2504 | } | |
2505 | ||
2506 | return 1; | |
2507 | } | |
2508 | ||
2509 | ||
2510 | /* Scan a bitmap file. FP is the stream to read from. Value is | |
2511 | either an enumerator from enum xbm_token, or a character for a | |
2512 | single-character token, or 0 at end of file. If scanning an | |
2513 | identifier, store the lexeme of the identifier in SVAL. If | |
2514 | scanning a number, store its value in *IVAL. */ | |
2515 | ||
2516 | static int | |
d3da34e0 | 2517 | xbm_scan (unsigned char **s, unsigned char *end, char *sval, int *ival) |
4ef0d4d0 KS |
2518 | { |
2519 | unsigned int c; | |
2520 | ||
2521 | loop: | |
2522 | ||
2523 | /* Skip white space. */ | |
620f13b0 | 2524 | while (*s < end && (c = *(*s)++, c_isspace (c))) |
4ef0d4d0 KS |
2525 | ; |
2526 | ||
2527 | if (*s >= end) | |
2528 | c = 0; | |
620f13b0 | 2529 | else if (c_isdigit (c)) |
4ef0d4d0 KS |
2530 | { |
2531 | int value = 0, digit; | |
2532 | ||
2533 | if (c == '0' && *s < end) | |
2534 | { | |
2535 | c = *(*s)++; | |
2536 | if (c == 'x' || c == 'X') | |
2537 | { | |
2538 | while (*s < end) | |
2539 | { | |
2540 | c = *(*s)++; | |
620f13b0 | 2541 | if (c_isdigit (c)) |
4ef0d4d0 KS |
2542 | digit = c - '0'; |
2543 | else if (c >= 'a' && c <= 'f') | |
2544 | digit = c - 'a' + 10; | |
2545 | else if (c >= 'A' && c <= 'F') | |
2546 | digit = c - 'A' + 10; | |
2547 | else | |
2548 | break; | |
2549 | value = 16 * value + digit; | |
2550 | } | |
2551 | } | |
620f13b0 | 2552 | else if (c_isdigit (c)) |
4ef0d4d0 KS |
2553 | { |
2554 | value = c - '0'; | |
2555 | while (*s < end | |
620f13b0 | 2556 | && (c = *(*s)++, c_isdigit (c))) |
4ef0d4d0 KS |
2557 | value = 8 * value + c - '0'; |
2558 | } | |
2559 | } | |
2560 | else | |
2561 | { | |
2562 | value = c - '0'; | |
2563 | while (*s < end | |
620f13b0 | 2564 | && (c = *(*s)++, c_isdigit (c))) |
4ef0d4d0 KS |
2565 | value = 10 * value + c - '0'; |
2566 | } | |
2567 | ||
2568 | if (*s < end) | |
2569 | *s = *s - 1; | |
2570 | *ival = value; | |
2571 | c = XBM_TK_NUMBER; | |
2572 | } | |
620f13b0 | 2573 | else if (c_isalpha (c) || c == '_') |
4ef0d4d0 KS |
2574 | { |
2575 | *sval++ = c; | |
2576 | while (*s < end | |
620f13b0 | 2577 | && (c = *(*s)++, (c_isalnum (c) || c == '_'))) |
4ef0d4d0 KS |
2578 | *sval++ = c; |
2579 | *sval = 0; | |
2580 | if (*s < end) | |
2581 | *s = *s - 1; | |
2582 | c = XBM_TK_IDENT; | |
2583 | } | |
2584 | else if (c == '/' && **s == '*') | |
2585 | { | |
2586 | /* C-style comment. */ | |
2587 | ++*s; | |
2588 | while (**s && (**s != '*' || *(*s + 1) != '/')) | |
2589 | ++*s; | |
2590 | if (**s) | |
2591 | { | |
2592 | *s += 2; | |
2593 | goto loop; | |
2594 | } | |
2595 | } | |
2596 | ||
2597 | return c; | |
2598 | } | |
2599 | ||
2600 | #ifdef HAVE_NTGUI | |
2601 | ||
2602 | /* Create a Windows bitmap from X bitmap data. */ | |
2603 | static HBITMAP | |
2604 | w32_create_pixmap_from_bitmap_data (int width, int height, char *data) | |
2605 | { | |
2606 | static unsigned char swap_nibble[16] | |
2607 | = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */ | |
2608 | 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */ | |
2609 | 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */ | |
2610 | 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */ | |
2611 | int i, j, w1, w2; | |
2612 | unsigned char *bits, *p; | |
2613 | HBITMAP bmp; | |
2614 | ||
2615 | w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */ | |
2616 | w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */ | |
38182d90 | 2617 | bits = alloca (height * w2); |
72af86bd | 2618 | memset (bits, 0, height * w2); |
4ef0d4d0 KS |
2619 | for (i = 0; i < height; i++) |
2620 | { | |
2621 | p = bits + i*w2; | |
2622 | for (j = 0; j < w1; j++) | |
2623 | { | |
2624 | /* Bitswap XBM bytes to match how Windows does things. */ | |
2625 | unsigned char c = *data++; | |
2626 | *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4) | |
2627 | | (swap_nibble[(c>>4) & 0xf])); | |
2628 | } | |
2629 | } | |
2630 | bmp = CreateBitmap (width, height, 1, 1, (char *) bits); | |
2631 | ||
2632 | return bmp; | |
2633 | } | |
2634 | ||
3046a84b | 2635 | static void |
7c3320d8 JB |
2636 | convert_mono_to_color_image (struct frame *f, struct image *img, |
2637 | COLORREF foreground, COLORREF background) | |
4ef0d4d0 KS |
2638 | { |
2639 | HDC hdc, old_img_dc, new_img_dc; | |
2640 | HGDIOBJ old_prev, new_prev; | |
2641 | HBITMAP new_pixmap; | |
2642 | ||
2643 | hdc = get_frame_dc (f); | |
2644 | old_img_dc = CreateCompatibleDC (hdc); | |
2645 | new_img_dc = CreateCompatibleDC (hdc); | |
2646 | new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height); | |
2647 | release_frame_dc (f, hdc); | |
2648 | old_prev = SelectObject (old_img_dc, img->pixmap); | |
2649 | new_prev = SelectObject (new_img_dc, new_pixmap); | |
44b1dc2e JR |
2650 | /* Windows convention for mono bitmaps is black = background, |
2651 | white = foreground. */ | |
2386b1f1 JR |
2652 | SetTextColor (new_img_dc, background); |
2653 | SetBkColor (new_img_dc, foreground); | |
4ef0d4d0 KS |
2654 | |
2655 | BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc, | |
2656 | 0, 0, SRCCOPY); | |
2657 | ||
2658 | SelectObject (old_img_dc, old_prev); | |
2659 | SelectObject (new_img_dc, new_prev); | |
2660 | DeleteDC (old_img_dc); | |
2661 | DeleteDC (new_img_dc); | |
2662 | DeleteObject (img->pixmap); | |
2663 | if (new_pixmap == 0) | |
2664 | fprintf (stderr, "Failed to convert image to color.\n"); | |
2665 | else | |
2666 | img->pixmap = new_pixmap; | |
2667 | } | |
2668 | ||
2669 | #define XBM_BIT_SHUFFLE(b) (~(b)) | |
2670 | ||
2671 | #else | |
2672 | ||
2673 | #define XBM_BIT_SHUFFLE(b) (b) | |
2674 | ||
2675 | #endif /* HAVE_NTGUI */ | |
2676 | ||
2677 | ||
2678 | static void | |
d3da34e0 JB |
2679 | Create_Pixmap_From_Bitmap_Data (struct frame *f, struct image *img, char *data, |
2680 | RGB_PIXEL_COLOR fg, RGB_PIXEL_COLOR bg, | |
578098f3 | 2681 | bool non_default_colors) |
4ef0d4d0 KS |
2682 | { |
2683 | #ifdef HAVE_NTGUI | |
2684 | img->pixmap | |
2685 | = w32_create_pixmap_from_bitmap_data (img->width, img->height, data); | |
2686 | ||
2687 | /* If colors were specified, transfer the bitmap to a color one. */ | |
2688 | if (non_default_colors) | |
2689 | convert_mono_to_color_image (f, img, fg, bg); | |
edfda783 AR |
2690 | |
2691 | #elif defined (HAVE_NS) | |
ed3751c8 | 2692 | img->pixmap = ns_image_from_XBM (data, img->width, img->height); |
edfda783 | 2693 | |
4ef0d4d0 | 2694 | #else |
ca4aa935 PE |
2695 | img->pixmap = |
2696 | (x_check_image_size (0, img->width, img->height) | |
2697 | ? XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f), | |
4ef0d4d0 KS |
2698 | FRAME_X_WINDOW (f), |
2699 | data, | |
2700 | img->width, img->height, | |
2701 | fg, bg, | |
ca4aa935 PE |
2702 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))) |
2703 | : NO_PIXMAP); | |
edfda783 | 2704 | #endif /* !HAVE_NTGUI && !HAVE_NS */ |
4ef0d4d0 KS |
2705 | } |
2706 | ||
2707 | ||
2708 | ||
2709 | /* Replacement for XReadBitmapFileData which isn't available under old | |
2710 | X versions. CONTENTS is a pointer to a buffer to parse; END is the | |
2711 | buffer's end. Set *WIDTH and *HEIGHT to the width and height of | |
2712 | the image. Return in *DATA the bitmap data allocated with xmalloc. | |
578098f3 PE |
2713 | Value is true if successful. DATA null means just test if |
2714 | CONTENTS looks like an in-memory XBM file. If INHIBIT_IMAGE_ERROR, | |
2715 | inhibit the call to image_error when the image size is invalid (the | |
2716 | bitmap remains unread). */ | |
4ef0d4d0 | 2717 | |
578098f3 | 2718 | static bool |
d3da34e0 | 2719 | xbm_read_bitmap_data (struct frame *f, unsigned char *contents, unsigned char *end, |
b8dc29e9 | 2720 | int *width, int *height, char **data, |
578098f3 | 2721 | bool inhibit_image_error) |
4ef0d4d0 KS |
2722 | { |
2723 | unsigned char *s = contents; | |
2724 | char buffer[BUFSIZ]; | |
578098f3 PE |
2725 | bool padding_p = 0; |
2726 | bool v10 = 0; | |
ca4aa935 | 2727 | int bytes_per_line, i, nbytes; |
b8dc29e9 | 2728 | char *p; |
4ef0d4d0 KS |
2729 | int value; |
2730 | int LA1; | |
2731 | ||
2732 | #define match() \ | |
2733 | LA1 = xbm_scan (&s, end, buffer, &value) | |
2734 | ||
2735 | #define expect(TOKEN) \ | |
f462f075 PE |
2736 | do \ |
2737 | { \ | |
2738 | if (LA1 != (TOKEN)) \ | |
2739 | goto failure; \ | |
2740 | match (); \ | |
2741 | } \ | |
2742 | while (0) | |
4ef0d4d0 KS |
2743 | |
2744 | #define expect_ident(IDENT) \ | |
2745 | if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \ | |
2746 | match (); \ | |
2747 | else \ | |
2748 | goto failure | |
2749 | ||
2750 | *width = *height = -1; | |
2751 | if (data) | |
2752 | *data = NULL; | |
2753 | LA1 = xbm_scan (&s, end, buffer, &value); | |
2754 | ||
2755 | /* Parse defines for width, height and hot-spots. */ | |
2756 | while (LA1 == '#') | |
2757 | { | |
2758 | match (); | |
2759 | expect_ident ("define"); | |
2760 | expect (XBM_TK_IDENT); | |
2761 | ||
431feaf6 | 2762 | if (LA1 == XBM_TK_NUMBER) |
4ef0d4d0 | 2763 | { |
6ae141d6 PE |
2764 | char *q = strrchr (buffer, '_'); |
2765 | q = q ? q + 1 : buffer; | |
2766 | if (strcmp (q, "width") == 0) | |
4ef0d4d0 | 2767 | *width = value; |
6ae141d6 | 2768 | else if (strcmp (q, "height") == 0) |
4ef0d4d0 KS |
2769 | *height = value; |
2770 | } | |
2771 | expect (XBM_TK_NUMBER); | |
2772 | } | |
2773 | ||
f1f25b99 | 2774 | if (!check_image_size (f, *width, *height)) |
10ea2b82 | 2775 | { |
ce959360 CY |
2776 | if (!inhibit_image_error) |
2777 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
10ea2b82 JR |
2778 | goto failure; |
2779 | } | |
4ef0d4d0 KS |
2780 | else if (data == NULL) |
2781 | goto success; | |
2782 | ||
2783 | /* Parse bits. Must start with `static'. */ | |
2784 | expect_ident ("static"); | |
2785 | if (LA1 == XBM_TK_IDENT) | |
2786 | { | |
2787 | if (strcmp (buffer, "unsigned") == 0) | |
2788 | { | |
2789 | match (); | |
2790 | expect_ident ("char"); | |
2791 | } | |
2792 | else if (strcmp (buffer, "short") == 0) | |
2793 | { | |
2794 | match (); | |
2795 | v10 = 1; | |
2796 | if (*width % 16 && *width % 16 < 9) | |
2797 | padding_p = 1; | |
2798 | } | |
2799 | else if (strcmp (buffer, "char") == 0) | |
2800 | match (); | |
2801 | else | |
2802 | goto failure; | |
2803 | } | |
2804 | else | |
2805 | goto failure; | |
2806 | ||
2807 | expect (XBM_TK_IDENT); | |
2808 | expect ('['); | |
2809 | expect (']'); | |
2810 | expect ('='); | |
2811 | expect ('{'); | |
2812 | ||
ca4aa935 PE |
2813 | if (! x_check_image_size (0, *width, *height)) |
2814 | { | |
2815 | if (!inhibit_image_error) | |
2816 | image_error ("Image too large (%dx%d)", | |
2817 | make_number (*width), make_number (*height)); | |
2818 | goto failure; | |
2819 | } | |
4ef0d4d0 KS |
2820 | bytes_per_line = (*width + 7) / 8 + padding_p; |
2821 | nbytes = bytes_per_line * *height; | |
23f86fce | 2822 | p = *data = xmalloc (nbytes); |
4ef0d4d0 KS |
2823 | |
2824 | if (v10) | |
2825 | { | |
2826 | for (i = 0; i < nbytes; i += 2) | |
2827 | { | |
2828 | int val = value; | |
2829 | expect (XBM_TK_NUMBER); | |
2830 | ||
2831 | *p++ = XBM_BIT_SHUFFLE (val); | |
2832 | if (!padding_p || ((i + 2) % bytes_per_line)) | |
2833 | *p++ = XBM_BIT_SHUFFLE (value >> 8); | |
2834 | ||
2835 | if (LA1 == ',' || LA1 == '}') | |
2836 | match (); | |
2837 | else | |
2838 | goto failure; | |
2839 | } | |
2840 | } | |
2841 | else | |
2842 | { | |
2843 | for (i = 0; i < nbytes; ++i) | |
2844 | { | |
2845 | int val = value; | |
2846 | expect (XBM_TK_NUMBER); | |
2847 | ||
2848 | *p++ = XBM_BIT_SHUFFLE (val); | |
2849 | ||
2850 | if (LA1 == ',' || LA1 == '}') | |
2851 | match (); | |
2852 | else | |
2853 | goto failure; | |
2854 | } | |
2855 | } | |
2856 | ||
2857 | success: | |
2858 | return 1; | |
2859 | ||
2860 | failure: | |
2861 | ||
2862 | if (data && *data) | |
2863 | { | |
2864 | xfree (*data); | |
2865 | *data = NULL; | |
2866 | } | |
2867 | return 0; | |
2868 | ||
2869 | #undef match | |
2870 | #undef expect | |
2871 | #undef expect_ident | |
2872 | } | |
2873 | ||
2874 | ||
2875 | /* Load XBM image IMG which will be displayed on frame F from buffer | |
578098f3 | 2876 | CONTENTS. END is the end of the buffer. Value is true if |
4ef0d4d0 KS |
2877 | successful. */ |
2878 | ||
578098f3 | 2879 | static bool |
d3da34e0 JB |
2880 | xbm_load_image (struct frame *f, struct image *img, unsigned char *contents, |
2881 | unsigned char *end) | |
4ef0d4d0 | 2882 | { |
578098f3 | 2883 | bool rc; |
b8dc29e9 | 2884 | char *data; |
578098f3 | 2885 | bool success_p = 0; |
4ef0d4d0 | 2886 | |
ce959360 CY |
2887 | rc = xbm_read_bitmap_data (f, contents, end, &img->width, &img->height, |
2888 | &data, 0); | |
4ef0d4d0 KS |
2889 | if (rc) |
2890 | { | |
2891 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2892 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
578098f3 | 2893 | bool non_default_colors = 0; |
4ef0d4d0 KS |
2894 | Lisp_Object value; |
2895 | ||
a54e2c05 | 2896 | eassert (img->width > 0 && img->height > 0); |
4ef0d4d0 KS |
2897 | |
2898 | /* Get foreground and background colors, maybe allocate colors. */ | |
2899 | value = image_spec_value (img->spec, QCforeground, NULL); | |
2900 | if (!NILP (value)) | |
2901 | { | |
2902 | foreground = x_alloc_image_color (f, img, value, foreground); | |
2903 | non_default_colors = 1; | |
2904 | } | |
2905 | value = image_spec_value (img->spec, QCbackground, NULL); | |
2906 | if (!NILP (value)) | |
2907 | { | |
2908 | background = x_alloc_image_color (f, img, value, background); | |
2909 | img->background = background; | |
2910 | img->background_valid = 1; | |
2911 | non_default_colors = 1; | |
2912 | } | |
2913 | ||
1f026899 | 2914 | Create_Pixmap_From_Bitmap_Data (f, img, data, |
4ef0d4d0 KS |
2915 | foreground, background, |
2916 | non_default_colors); | |
2917 | xfree (data); | |
2918 | ||
2919 | if (img->pixmap == NO_PIXMAP) | |
2920 | { | |
2921 | x_clear_image (f, img); | |
2922 | image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil); | |
2923 | } | |
2924 | else | |
2925 | success_p = 1; | |
2926 | } | |
2927 | else | |
2928 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
2929 | ||
2930 | return success_p; | |
2931 | } | |
2932 | ||
2933 | ||
578098f3 | 2934 | /* Value is true if DATA looks like an in-memory XBM file. */ |
4ef0d4d0 | 2935 | |
578098f3 | 2936 | static bool |
d3da34e0 | 2937 | xbm_file_p (Lisp_Object data) |
4ef0d4d0 KS |
2938 | { |
2939 | int w, h; | |
2940 | return (STRINGP (data) | |
f1f25b99 | 2941 | && xbm_read_bitmap_data (NULL, SDATA (data), |
ce959360 CY |
2942 | (SDATA (data) + SBYTES (data)), |
2943 | &w, &h, NULL, 1)); | |
4ef0d4d0 KS |
2944 | } |
2945 | ||
2946 | ||
2947 | /* Fill image IMG which is used on frame F with pixmap data. Value is | |
578098f3 | 2948 | true if successful. */ |
4ef0d4d0 | 2949 | |
578098f3 | 2950 | static bool |
d3da34e0 | 2951 | xbm_load (struct frame *f, struct image *img) |
4ef0d4d0 | 2952 | { |
578098f3 | 2953 | bool success_p = 0; |
4ef0d4d0 KS |
2954 | Lisp_Object file_name; |
2955 | ||
a54e2c05 | 2956 | eassert (xbm_image_p (img->spec)); |
4ef0d4d0 KS |
2957 | |
2958 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
2959 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
2960 | if (STRINGP (file_name)) | |
2961 | { | |
2962 | Lisp_Object file; | |
2963 | unsigned char *contents; | |
dd52fcea | 2964 | ptrdiff_t size; |
4ef0d4d0 KS |
2965 | |
2966 | file = x_find_image_file (file_name); | |
4ef0d4d0 KS |
2967 | if (!STRINGP (file)) |
2968 | { | |
2969 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
4ef0d4d0 KS |
2970 | return 0; |
2971 | } | |
2972 | ||
42a5b22f | 2973 | contents = slurp_file (SSDATA (file), &size); |
4ef0d4d0 KS |
2974 | if (contents == NULL) |
2975 | { | |
2976 | image_error ("Error loading XBM image `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
2977 | return 0; |
2978 | } | |
2979 | ||
2980 | success_p = xbm_load_image (f, img, contents, contents + size); | |
d5a19415 | 2981 | xfree (contents); |
4ef0d4d0 KS |
2982 | } |
2983 | else | |
2984 | { | |
2985 | struct image_keyword fmt[XBM_LAST]; | |
2986 | Lisp_Object data; | |
2987 | unsigned long foreground = FRAME_FOREGROUND_PIXEL (f); | |
2988 | unsigned long background = FRAME_BACKGROUND_PIXEL (f); | |
578098f3 | 2989 | bool non_default_colors = 0; |
4ef0d4d0 | 2990 | char *bits; |
578098f3 PE |
2991 | bool parsed_p; |
2992 | bool in_memory_file_p = 0; | |
4ef0d4d0 KS |
2993 | |
2994 | /* See if data looks like an in-memory XBM file. */ | |
2995 | data = image_spec_value (img->spec, QCdata, NULL); | |
2996 | in_memory_file_p = xbm_file_p (data); | |
2997 | ||
2998 | /* Parse the image specification. */ | |
72af86bd | 2999 | memcpy (fmt, xbm_format, sizeof fmt); |
4ef0d4d0 | 3000 | parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm); |
a54e2c05 | 3001 | eassert (parsed_p); |
4ef0d4d0 KS |
3002 | |
3003 | /* Get specified width, and height. */ | |
3004 | if (!in_memory_file_p) | |
3005 | { | |
3006 | img->width = XFASTINT (fmt[XBM_WIDTH].value); | |
3007 | img->height = XFASTINT (fmt[XBM_HEIGHT].value); | |
a54e2c05 | 3008 | eassert (img->width > 0 && img->height > 0); |
ca4aa935 PE |
3009 | if (!check_image_size (f, img->width, img->height)) |
3010 | { | |
3011 | image_error ("Invalid image size (see `max-image-size')", | |
3012 | Qnil, Qnil); | |
3013 | return 0; | |
3014 | } | |
4ef0d4d0 KS |
3015 | } |
3016 | ||
3017 | /* Get foreground and background colors, maybe allocate colors. */ | |
3018 | if (fmt[XBM_FOREGROUND].count | |
3019 | && STRINGP (fmt[XBM_FOREGROUND].value)) | |
3020 | { | |
3021 | foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value, | |
3022 | foreground); | |
3023 | non_default_colors = 1; | |
3024 | } | |
3025 | ||
3026 | if (fmt[XBM_BACKGROUND].count | |
3027 | && STRINGP (fmt[XBM_BACKGROUND].value)) | |
3028 | { | |
3029 | background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value, | |
3030 | background); | |
3031 | non_default_colors = 1; | |
3032 | } | |
3033 | ||
3034 | if (in_memory_file_p) | |
3035 | success_p = xbm_load_image (f, img, SDATA (data), | |
3036 | (SDATA (data) | |
3037 | + SBYTES (data))); | |
3038 | else | |
3039 | { | |
3040 | if (VECTORP (data)) | |
3041 | { | |
3042 | int i; | |
3043 | char *p; | |
3044 | int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR; | |
3045 | ||
38182d90 | 3046 | p = bits = alloca (nbytes * img->height); |
4ef0d4d0 KS |
3047 | for (i = 0; i < img->height; ++i, p += nbytes) |
3048 | { | |
28be1ada | 3049 | Lisp_Object line = AREF (data, i); |
4ef0d4d0 | 3050 | if (STRINGP (line)) |
72af86bd | 3051 | memcpy (p, SDATA (line), nbytes); |
4ef0d4d0 | 3052 | else |
df5b4930 | 3053 | memcpy (p, bool_vector_data (line), nbytes); |
4ef0d4d0 KS |
3054 | } |
3055 | } | |
3056 | else if (STRINGP (data)) | |
42a5b22f | 3057 | bits = SSDATA (data); |
4ef0d4d0 | 3058 | else |
df5b4930 | 3059 | bits = (char *) bool_vector_data (data); |
4ef0d4d0 | 3060 | |
0fda9b75 | 3061 | #ifdef HAVE_NTGUI |
44b1dc2e JR |
3062 | { |
3063 | char *invertedBits; | |
3064 | int nbytes, i; | |
3065 | /* Windows mono bitmaps are reversed compared with X. */ | |
3066 | invertedBits = bits; | |
285d07e2 | 3067 | nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR |
44b1dc2e | 3068 | * img->height; |
38182d90 | 3069 | bits = alloca (nbytes); |
44b1dc2e JR |
3070 | for (i = 0; i < nbytes; i++) |
3071 | bits[i] = XBM_BIT_SHUFFLE (invertedBits[i]); | |
3072 | } | |
3073 | #endif | |
4ef0d4d0 KS |
3074 | /* Create the pixmap. */ |
3075 | ||
ca4aa935 PE |
3076 | if (x_check_image_size (0, img->width, img->height)) |
3077 | Create_Pixmap_From_Bitmap_Data (f, img, bits, | |
3078 | foreground, background, | |
3079 | non_default_colors); | |
3080 | else | |
3081 | img->pixmap = NO_PIXMAP; | |
3082 | ||
4ef0d4d0 KS |
3083 | if (img->pixmap) |
3084 | success_p = 1; | |
3085 | else | |
3086 | { | |
3087 | image_error ("Unable to create pixmap for XBM image `%s'", | |
3088 | img->spec, Qnil); | |
3089 | x_clear_image (f, img); | |
3090 | } | |
3091 | } | |
3092 | } | |
3093 | ||
3094 | return success_p; | |
3095 | } | |
3096 | ||
3097 | ||
3098 | \f | |
3099 | /*********************************************************************** | |
3100 | XPM images | |
3101 | ***********************************************************************/ | |
3102 | ||
9e2a2647 | 3103 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 | 3104 | |
578098f3 PE |
3105 | static bool xpm_image_p (Lisp_Object object); |
3106 | static bool xpm_load (struct frame *f, struct image *img); | |
4ef0d4d0 | 3107 | |
9e2a2647 | 3108 | #endif /* HAVE_XPM || HAVE_NS */ |
ea1aaa6f ST |
3109 | |
3110 | #ifdef HAVE_XPM | |
4ef0d4d0 KS |
3111 | #ifdef HAVE_NTGUI |
3112 | /* Indicate to xpm.h that we don't have Xlib. */ | |
3113 | #define FOR_MSW | |
3114 | /* simx.h in xpm defines XColor and XImage differently than Emacs. */ | |
279d3293 | 3115 | /* It also defines Display the same way as Emacs, but gcc 3.3 still barfs. */ |
4ef0d4d0 KS |
3116 | #define XColor xpm_XColor |
3117 | #define XImage xpm_XImage | |
279d3293 | 3118 | #define Display xpm_Display |
4ef0d4d0 KS |
3119 | #define PIXEL_ALREADY_TYPEDEFED |
3120 | #include "X11/xpm.h" | |
3121 | #undef FOR_MSW | |
3122 | #undef XColor | |
3123 | #undef XImage | |
279d3293 | 3124 | #undef Display |
4ef0d4d0 KS |
3125 | #undef PIXEL_ALREADY_TYPEDEFED |
3126 | #else | |
3127 | #include "X11/xpm.h" | |
3128 | #endif /* HAVE_NTGUI */ | |
ea1aaa6f | 3129 | #endif /* HAVE_XPM */ |
4ef0d4d0 | 3130 | |
9e2a2647 | 3131 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
4ef0d4d0 KS |
3132 | /* The symbol `xpm' identifying XPM-format images. */ |
3133 | ||
955cbe7b | 3134 | static Lisp_Object Qxpm; |
4ef0d4d0 KS |
3135 | |
3136 | /* Indices of image specification fields in xpm_format, below. */ | |
3137 | ||
3138 | enum xpm_keyword_index | |
3139 | { | |
3140 | XPM_TYPE, | |
3141 | XPM_FILE, | |
3142 | XPM_DATA, | |
3143 | XPM_ASCENT, | |
3144 | XPM_MARGIN, | |
3145 | XPM_RELIEF, | |
3146 | XPM_ALGORITHM, | |
3147 | XPM_HEURISTIC_MASK, | |
3148 | XPM_MASK, | |
3149 | XPM_COLOR_SYMBOLS, | |
3150 | XPM_BACKGROUND, | |
3151 | XPM_LAST | |
3152 | }; | |
3153 | ||
3154 | /* Vector of image_keyword structures describing the format | |
3155 | of valid XPM image specifications. */ | |
3156 | ||
91433552 | 3157 | static const struct image_keyword xpm_format[XPM_LAST] = |
4ef0d4d0 KS |
3158 | { |
3159 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
3160 | {":file", IMAGE_STRING_VALUE, 0}, | |
3161 | {":data", IMAGE_STRING_VALUE, 0}, | |
3162 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 3163 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
3164 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
3165 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3166 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3167 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3168 | {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
3169 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
3170 | }; | |
3171 | ||
a9e7a9d5 | 3172 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 3173 | static bool init_xpm_functions (void); |
bb4d86b4 CY |
3174 | #else |
3175 | #define init_xpm_functions NULL | |
3176 | #endif | |
3177 | ||
4ef0d4d0 KS |
3178 | /* Structure describing the image type XPM. */ |
3179 | ||
3180 | static struct image_type xpm_type = | |
3181 | { | |
3182 | &Qxpm, | |
3183 | xpm_image_p, | |
3184 | xpm_load, | |
3185 | x_clear_image, | |
bb4d86b4 | 3186 | init_xpm_functions, |
4ef0d4d0 KS |
3187 | NULL |
3188 | }; | |
3189 | ||
3190 | #ifdef HAVE_X_WINDOWS | |
3191 | ||
3192 | /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation | |
3193 | functions for allocating image colors. Our own functions handle | |
3194 | color allocation failures more gracefully than the ones on the XPM | |
3195 | lib. */ | |
3196 | ||
3197 | #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure | |
3198 | #define ALLOC_XPM_COLORS | |
3199 | #endif | |
3200 | #endif /* HAVE_X_WINDOWS */ | |
3201 | ||
3202 | #ifdef ALLOC_XPM_COLORS | |
3203 | ||
f57e2426 J |
3204 | static struct xpm_cached_color *xpm_cache_color (struct frame *, char *, |
3205 | XColor *, int); | |
4ef0d4d0 KS |
3206 | |
3207 | /* An entry in a hash table used to cache color definitions of named | |
3208 | colors. This cache is necessary to speed up XPM image loading in | |
3209 | case we do color allocations ourselves. Without it, we would need | |
3210 | a call to XParseColor per pixel in the image. */ | |
3211 | ||
3212 | struct xpm_cached_color | |
3213 | { | |
3214 | /* Next in collision chain. */ | |
3215 | struct xpm_cached_color *next; | |
3216 | ||
3217 | /* Color definition (RGB and pixel color). */ | |
3218 | XColor color; | |
3219 | ||
3220 | /* Color name. */ | |
fbe9e0b9 | 3221 | char name[FLEXIBLE_ARRAY_MEMBER]; |
4ef0d4d0 KS |
3222 | }; |
3223 | ||
3224 | /* The hash table used for the color cache, and its bucket vector | |
3225 | size. */ | |
3226 | ||
3227 | #define XPM_COLOR_CACHE_BUCKETS 1001 | |
cd44d2eb | 3228 | static struct xpm_cached_color **xpm_color_cache; |
4ef0d4d0 KS |
3229 | |
3230 | /* Initialize the color cache. */ | |
3231 | ||
3232 | static void | |
d3da34e0 | 3233 | xpm_init_color_cache (struct frame *f, XpmAttributes *attrs) |
4ef0d4d0 KS |
3234 | { |
3235 | size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache; | |
23f86fce | 3236 | xpm_color_cache = xzalloc (nbytes); |
4ef0d4d0 KS |
3237 | init_color_table (); |
3238 | ||
3239 | if (attrs->valuemask & XpmColorSymbols) | |
3240 | { | |
3241 | int i; | |
3242 | XColor color; | |
3243 | ||
3244 | for (i = 0; i < attrs->numsymbols; ++i) | |
3245 | if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3246 | attrs->colorsymbols[i].value, &color)) | |
3247 | { | |
3248 | color.pixel = lookup_rgb_color (f, color.red, color.green, | |
3249 | color.blue); | |
3250 | xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1); | |
3251 | } | |
3252 | } | |
3253 | } | |
3254 | ||
3255 | /* Free the color cache. */ | |
3256 | ||
3257 | static void | |
d3da34e0 | 3258 | xpm_free_color_cache (void) |
4ef0d4d0 KS |
3259 | { |
3260 | struct xpm_cached_color *p, *next; | |
3261 | int i; | |
3262 | ||
3263 | for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i) | |
3264 | for (p = xpm_color_cache[i]; p; p = next) | |
3265 | { | |
3266 | next = p->next; | |
3267 | xfree (p); | |
3268 | } | |
3269 | ||
3270 | xfree (xpm_color_cache); | |
3271 | xpm_color_cache = NULL; | |
3272 | free_color_table (); | |
3273 | } | |
3274 | ||
3275 | /* Return the bucket index for color named COLOR_NAME in the color | |
3276 | cache. */ | |
3277 | ||
3278 | static int | |
d3da34e0 | 3279 | xpm_color_bucket (char *color_name) |
4ef0d4d0 | 3280 | { |
3f791afe PE |
3281 | EMACS_UINT hash = hash_string (color_name, strlen (color_name)); |
3282 | return hash % XPM_COLOR_CACHE_BUCKETS; | |
4ef0d4d0 KS |
3283 | } |
3284 | ||
3285 | ||
3286 | /* On frame F, cache values COLOR for color with name COLOR_NAME. | |
3287 | BUCKET, if >= 0, is a precomputed bucket index. Value is the cache | |
3288 | entry added. */ | |
3289 | ||
3290 | static struct xpm_cached_color * | |
d3da34e0 | 3291 | xpm_cache_color (struct frame *f, char *color_name, XColor *color, int bucket) |
4ef0d4d0 KS |
3292 | { |
3293 | size_t nbytes; | |
3294 | struct xpm_cached_color *p; | |
3295 | ||
3296 | if (bucket < 0) | |
3297 | bucket = xpm_color_bucket (color_name); | |
3298 | ||
3f791afe | 3299 | nbytes = offsetof (struct xpm_cached_color, name) + strlen (color_name) + 1; |
23f86fce | 3300 | p = xmalloc (nbytes); |
4ef0d4d0 KS |
3301 | strcpy (p->name, color_name); |
3302 | p->color = *color; | |
3303 | p->next = xpm_color_cache[bucket]; | |
3304 | xpm_color_cache[bucket] = p; | |
3305 | return p; | |
3306 | } | |
3307 | ||
3308 | /* Look up color COLOR_NAME for frame F in the color cache. If found, | |
3309 | return the cached definition in *COLOR. Otherwise, make a new | |
578098f3 | 3310 | entry in the cache and allocate the color. Value is false if color |
4ef0d4d0 KS |
3311 | allocation failed. */ |
3312 | ||
578098f3 | 3313 | static bool |
d3da34e0 | 3314 | xpm_lookup_color (struct frame *f, char *color_name, XColor *color) |
4ef0d4d0 KS |
3315 | { |
3316 | struct xpm_cached_color *p; | |
3317 | int h = xpm_color_bucket (color_name); | |
3318 | ||
3319 | for (p = xpm_color_cache[h]; p; p = p->next) | |
3320 | if (strcmp (p->name, color_name) == 0) | |
3321 | break; | |
3322 | ||
3323 | if (p != NULL) | |
3324 | *color = p->color; | |
3325 | else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), | |
3326 | color_name, color)) | |
3327 | { | |
3328 | color->pixel = lookup_rgb_color (f, color->red, color->green, | |
3329 | color->blue); | |
3330 | p = xpm_cache_color (f, color_name, color, h); | |
3331 | } | |
3332 | /* You get `opaque' at least from ImageMagick converting pbm to xpm | |
3333 | with transparency, and it's useful. */ | |
3334 | else if (strcmp ("opaque", color_name) == 0) | |
3335 | { | |
72af86bd | 3336 | memset (color, 0, sizeof (XColor)); /* Is this necessary/correct? */ |
4ef0d4d0 KS |
3337 | color->pixel = FRAME_FOREGROUND_PIXEL (f); |
3338 | p = xpm_cache_color (f, color_name, color, h); | |
3339 | } | |
3340 | ||
3341 | return p != NULL; | |
3342 | } | |
3343 | ||
3344 | ||
3345 | /* Callback for allocating color COLOR_NAME. Called from the XPM lib. | |
3346 | CLOSURE is a pointer to the frame on which we allocate the | |
3347 | color. Return in *COLOR the allocated color. Value is non-zero | |
3348 | if successful. */ | |
3349 | ||
3350 | static int | |
d3da34e0 JB |
3351 | xpm_alloc_color (Display *dpy, Colormap cmap, char *color_name, XColor *color, |
3352 | void *closure) | |
4ef0d4d0 | 3353 | { |
7d652d97 | 3354 | return xpm_lookup_color (closure, color_name, color); |
4ef0d4d0 KS |
3355 | } |
3356 | ||
3357 | ||
3358 | /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE | |
3359 | is a pointer to the frame on which we allocate the color. Value is | |
3360 | non-zero if successful. */ | |
3361 | ||
3362 | static int | |
d3da34e0 | 3363 | xpm_free_colors (Display *dpy, Colormap cmap, Pixel *pixels, int npixels, void *closure) |
4ef0d4d0 KS |
3364 | { |
3365 | return 1; | |
3366 | } | |
3367 | ||
3368 | #endif /* ALLOC_XPM_COLORS */ | |
3369 | ||
3370 | ||
0fda9b75 | 3371 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
3372 | |
3373 | /* XPM library details. */ | |
3374 | ||
14beddf4 CY |
3375 | DEF_IMGLIB_FN (void, XpmFreeAttributes, (XpmAttributes *)); |
3376 | DEF_IMGLIB_FN (int, XpmCreateImageFromBuffer, (Display *, char *, xpm_XImage **, | |
850690cc | 3377 | xpm_XImage **, XpmAttributes *)); |
14beddf4 | 3378 | DEF_IMGLIB_FN (int, XpmReadFileToImage, (Display *, char *, xpm_XImage **, |
850690cc | 3379 | xpm_XImage **, XpmAttributes *)); |
14beddf4 | 3380 | DEF_IMGLIB_FN (void, XImageFree, (xpm_XImage *)); |
4ef0d4d0 | 3381 | |
578098f3 | 3382 | static bool |
d07ff9db | 3383 | init_xpm_functions (void) |
4ef0d4d0 KS |
3384 | { |
3385 | HMODULE library; | |
3386 | ||
d07ff9db | 3387 | if (!(library = w32_delayed_load (Qxpm))) |
4ef0d4d0 KS |
3388 | return 0; |
3389 | ||
3390 | LOAD_IMGLIB_FN (library, XpmFreeAttributes); | |
3391 | LOAD_IMGLIB_FN (library, XpmCreateImageFromBuffer); | |
3392 | LOAD_IMGLIB_FN (library, XpmReadFileToImage); | |
3393 | LOAD_IMGLIB_FN (library, XImageFree); | |
3394 | return 1; | |
3395 | } | |
3396 | ||
0fda9b75 | 3397 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 3398 | |
a9e7a9d5 | 3399 | #if defined HAVE_NTGUI && !defined WINDOWSNT |
0fda9b75 DC |
3400 | /* Glue for code below */ |
3401 | #define fn_XpmReadFileToImage XpmReadFileToImage | |
3402 | #define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer | |
3403 | #define fn_XImageFree XImageFree | |
3404 | #define fn_XpmFreeAttributes XpmFreeAttributes | |
2ae3a8a7 | 3405 | #endif /* HAVE_NTGUI && !WINDOWSNT */ |
4ef0d4d0 | 3406 | |
578098f3 | 3407 | /* Value is true if COLOR_SYMBOLS is a valid color symbols list |
4ef0d4d0 KS |
3408 | for XPM images. Such a list must consist of conses whose car and |
3409 | cdr are strings. */ | |
3410 | ||
578098f3 | 3411 | static bool |
d3da34e0 | 3412 | xpm_valid_color_symbols_p (Lisp_Object color_symbols) |
4ef0d4d0 KS |
3413 | { |
3414 | while (CONSP (color_symbols)) | |
3415 | { | |
3416 | Lisp_Object sym = XCAR (color_symbols); | |
3417 | if (!CONSP (sym) | |
3418 | || !STRINGP (XCAR (sym)) | |
3419 | || !STRINGP (XCDR (sym))) | |
3420 | break; | |
3421 | color_symbols = XCDR (color_symbols); | |
3422 | } | |
3423 | ||
3424 | return NILP (color_symbols); | |
3425 | } | |
3426 | ||
3427 | ||
578098f3 | 3428 | /* Value is true if OBJECT is a valid XPM image specification. */ |
4ef0d4d0 | 3429 | |
578098f3 | 3430 | static bool |
d3da34e0 | 3431 | xpm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
3432 | { |
3433 | struct image_keyword fmt[XPM_LAST]; | |
72af86bd | 3434 | memcpy (fmt, xpm_format, sizeof fmt); |
4ef0d4d0 KS |
3435 | return (parse_image_spec (object, fmt, XPM_LAST, Qxpm) |
3436 | /* Either `:file' or `:data' must be present. */ | |
3437 | && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1 | |
3438 | /* Either no `:color-symbols' or it's a list of conses | |
3439 | whose car and cdr are strings. */ | |
3440 | && (fmt[XPM_COLOR_SYMBOLS].count == 0 | |
3441 | || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))); | |
3442 | } | |
3443 | ||
9e2a2647 | 3444 | #endif /* HAVE_XPM || HAVE_NS */ |
4ef0d4d0 | 3445 | |
cd44d2eb | 3446 | #if defined HAVE_XPM && defined HAVE_X_WINDOWS && !defined USE_GTK |
0766b489 | 3447 | ptrdiff_t |
ef1b0ba7 | 3448 | x_create_bitmap_from_xpm_data (struct frame *f, const char **bits) |
786a43d6 | 3449 | { |
aad3612f | 3450 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
0766b489 PE |
3451 | ptrdiff_t id; |
3452 | int rc; | |
786a43d6 CY |
3453 | XpmAttributes attrs; |
3454 | Pixmap bitmap, mask; | |
3455 | ||
72af86bd | 3456 | memset (&attrs, 0, sizeof attrs); |
786a43d6 | 3457 | |
54188d8f CY |
3458 | attrs.visual = FRAME_X_VISUAL (f); |
3459 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3460 | attrs.valuemask |= XpmVisual; | |
3461 | attrs.valuemask |= XpmColormap; | |
3462 | ||
786a43d6 | 3463 | rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
f77fabaf | 3464 | (char **) bits, &bitmap, &mask, &attrs); |
786a43d6 | 3465 | if (rc != XpmSuccess) |
fe45ad15 CY |
3466 | { |
3467 | XpmFreeAttributes (&attrs); | |
3468 | return -1; | |
3469 | } | |
786a43d6 CY |
3470 | |
3471 | id = x_allocate_bitmap_record (f); | |
786a43d6 | 3472 | dpyinfo->bitmaps[id - 1].pixmap = bitmap; |
96c06863 | 3473 | dpyinfo->bitmaps[id - 1].have_mask = true; |
786a43d6 CY |
3474 | dpyinfo->bitmaps[id - 1].mask = mask; |
3475 | dpyinfo->bitmaps[id - 1].file = NULL; | |
3476 | dpyinfo->bitmaps[id - 1].height = attrs.height; | |
3477 | dpyinfo->bitmaps[id - 1].width = attrs.width; | |
3478 | dpyinfo->bitmaps[id - 1].depth = attrs.depth; | |
3479 | dpyinfo->bitmaps[id - 1].refcount = 1; | |
3480 | ||
3481 | XpmFreeAttributes (&attrs); | |
3482 | return id; | |
3483 | } | |
0d876a14 | 3484 | #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */ |
786a43d6 | 3485 | |
4ef0d4d0 | 3486 | /* Load image IMG which will be displayed on frame F. Value is |
578098f3 | 3487 | true if successful. */ |
4ef0d4d0 | 3488 | |
ea1aaa6f ST |
3489 | #ifdef HAVE_XPM |
3490 | ||
578098f3 | 3491 | static bool |
d3da34e0 | 3492 | xpm_load (struct frame *f, struct image *img) |
4ef0d4d0 KS |
3493 | { |
3494 | int rc; | |
3495 | XpmAttributes attrs; | |
3496 | Lisp_Object specified_file, color_symbols; | |
3497 | #ifdef HAVE_NTGUI | |
3498 | HDC hdc; | |
3499 | xpm_XImage * xpm_image = NULL, * xpm_mask = NULL; | |
3500 | #endif /* HAVE_NTGUI */ | |
3501 | ||
3502 | /* Configure the XPM lib. Use the visual of frame F. Allocate | |
3503 | close colors. Return colors allocated. */ | |
72af86bd | 3504 | memset (&attrs, 0, sizeof attrs); |
4ef0d4d0 KS |
3505 | |
3506 | #ifndef HAVE_NTGUI | |
3507 | attrs.visual = FRAME_X_VISUAL (f); | |
3508 | attrs.colormap = FRAME_X_COLORMAP (f); | |
3509 | attrs.valuemask |= XpmVisual; | |
3510 | attrs.valuemask |= XpmColormap; | |
3511 | #endif /* HAVE_NTGUI */ | |
3512 | ||
3513 | #ifdef ALLOC_XPM_COLORS | |
3514 | /* Allocate colors with our own functions which handle | |
3515 | failing color allocation more gracefully. */ | |
3516 | attrs.color_closure = f; | |
3517 | attrs.alloc_color = xpm_alloc_color; | |
3518 | attrs.free_colors = xpm_free_colors; | |
3519 | attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure; | |
3520 | #else /* not ALLOC_XPM_COLORS */ | |
3521 | /* Let the XPM lib allocate colors. */ | |
3522 | attrs.valuemask |= XpmReturnAllocPixels; | |
3523 | #ifdef XpmAllocCloseColors | |
3524 | attrs.alloc_close_colors = 1; | |
3525 | attrs.valuemask |= XpmAllocCloseColors; | |
3526 | #else /* not XpmAllocCloseColors */ | |
3527 | attrs.closeness = 600; | |
3528 | attrs.valuemask |= XpmCloseness; | |
3529 | #endif /* not XpmAllocCloseColors */ | |
3530 | #endif /* ALLOC_XPM_COLORS */ | |
3531 | ||
3532 | /* If image specification contains symbolic color definitions, add | |
3533 | these to `attrs'. */ | |
3534 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
3535 | if (CONSP (color_symbols)) | |
3536 | { | |
3537 | Lisp_Object tail; | |
3538 | XpmColorSymbol *xpm_syms; | |
3539 | int i, size; | |
3540 | ||
3541 | attrs.valuemask |= XpmColorSymbols; | |
3542 | ||
3543 | /* Count number of symbols. */ | |
3544 | attrs.numsymbols = 0; | |
3545 | for (tail = color_symbols; CONSP (tail); tail = XCDR (tail)) | |
3546 | ++attrs.numsymbols; | |
3547 | ||
3548 | /* Allocate an XpmColorSymbol array. */ | |
3549 | size = attrs.numsymbols * sizeof *xpm_syms; | |
38182d90 | 3550 | xpm_syms = alloca (size); |
72af86bd | 3551 | memset (xpm_syms, 0, size); |
4ef0d4d0 KS |
3552 | attrs.colorsymbols = xpm_syms; |
3553 | ||
3554 | /* Fill the color symbol array. */ | |
3555 | for (tail = color_symbols, i = 0; | |
3556 | CONSP (tail); | |
3557 | ++i, tail = XCDR (tail)) | |
3558 | { | |
7574650a AS |
3559 | Lisp_Object name; |
3560 | Lisp_Object color; | |
e22cffbc | 3561 | char *empty_string = (char *) ""; |
7574650a AS |
3562 | |
3563 | if (!CONSP (XCAR (tail))) | |
3564 | { | |
e22cffbc PE |
3565 | xpm_syms[i].name = empty_string; |
3566 | xpm_syms[i].value = empty_string; | |
7574650a AS |
3567 | continue; |
3568 | } | |
3569 | name = XCAR (XCAR (tail)); | |
3570 | color = XCDR (XCAR (tail)); | |
3571 | if (STRINGP (name)) | |
3572 | { | |
38182d90 | 3573 | xpm_syms[i].name = alloca (SCHARS (name) + 1); |
42a5b22f | 3574 | strcpy (xpm_syms[i].name, SSDATA (name)); |
7574650a AS |
3575 | } |
3576 | else | |
e22cffbc | 3577 | xpm_syms[i].name = empty_string; |
7574650a AS |
3578 | if (STRINGP (color)) |
3579 | { | |
38182d90 | 3580 | xpm_syms[i].value = alloca (SCHARS (color) + 1); |
42a5b22f | 3581 | strcpy (xpm_syms[i].value, SSDATA (color)); |
7574650a AS |
3582 | } |
3583 | else | |
e22cffbc | 3584 | xpm_syms[i].value = empty_string; |
4ef0d4d0 KS |
3585 | } |
3586 | } | |
3587 | ||
3588 | /* Create a pixmap for the image, either from a file, or from a | |
3589 | string buffer containing data in the same format as an XPM file. */ | |
3590 | #ifdef ALLOC_XPM_COLORS | |
3591 | xpm_init_color_cache (f, &attrs); | |
3592 | #endif | |
3593 | ||
3594 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
3595 | ||
3596 | #ifdef HAVE_NTGUI | |
3597 | { | |
3598 | HDC frame_dc = get_frame_dc (f); | |
3599 | hdc = CreateCompatibleDC (frame_dc); | |
3600 | release_frame_dc (f, frame_dc); | |
3601 | } | |
3602 | #endif /* HAVE_NTGUI */ | |
3603 | ||
3604 | if (STRINGP (specified_file)) | |
3605 | { | |
3606 | Lisp_Object file = x_find_image_file (specified_file); | |
3607 | if (!STRINGP (file)) | |
3608 | { | |
3609 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
7574650a AS |
3610 | #ifdef ALLOC_XPM_COLORS |
3611 | xpm_free_color_cache (); | |
3612 | #endif | |
4ef0d4d0 KS |
3613 | return 0; |
3614 | } | |
3615 | ||
3616 | #ifdef HAVE_NTGUI | |
9ab69568 EZ |
3617 | #ifdef WINDOWSNT |
3618 | /* FILE is encoded in UTF-8, but image libraries on Windows | |
3619 | support neither UTF-8 nor UTF-16 encoded file names. So we | |
3620 | need to re-encode it in ANSI. */ | |
3621 | file = ansi_encode_filename (file); | |
3622 | #endif | |
4ef0d4d0 KS |
3623 | /* XpmReadFileToPixmap is not available in the Windows port of |
3624 | libxpm. But XpmReadFileToImage almost does what we want. */ | |
3625 | rc = fn_XpmReadFileToImage (&hdc, SDATA (file), | |
3626 | &xpm_image, &xpm_mask, | |
3627 | &attrs); | |
3628 | #else | |
547c9269 YM |
3629 | rc = XpmReadFileToImage (FRAME_X_DISPLAY (f), SSDATA (file), |
3630 | &img->ximg, &img->mask_img, | |
3631 | &attrs); | |
4ef0d4d0 KS |
3632 | #endif /* HAVE_NTGUI */ |
3633 | } | |
3634 | else | |
3635 | { | |
3636 | Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
3637 | if (!STRINGP (buffer)) |
3638 | { | |
3639 | image_error ("Invalid image data `%s'", buffer, Qnil); | |
3640 | #ifdef ALLOC_XPM_COLORS | |
3641 | xpm_free_color_cache (); | |
3642 | #endif | |
3643 | return 0; | |
3644 | } | |
4ef0d4d0 KS |
3645 | #ifdef HAVE_NTGUI |
3646 | /* XpmCreatePixmapFromBuffer is not available in the Windows port | |
3647 | of libxpm. But XpmCreateImageFromBuffer almost does what we want. */ | |
3648 | rc = fn_XpmCreateImageFromBuffer (&hdc, SDATA (buffer), | |
3649 | &xpm_image, &xpm_mask, | |
3650 | &attrs); | |
3651 | #else | |
547c9269 YM |
3652 | rc = XpmCreateImageFromBuffer (FRAME_X_DISPLAY (f), SSDATA (buffer), |
3653 | &img->ximg, &img->mask_img, | |
3654 | &attrs); | |
4ef0d4d0 KS |
3655 | #endif /* HAVE_NTGUI */ |
3656 | } | |
3657 | ||
547c9269 YM |
3658 | #ifdef HAVE_X_WINDOWS |
3659 | if (rc == XpmSuccess) | |
3660 | { | |
3661 | img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3662 | img->ximg->width, img->ximg->height, | |
3663 | img->ximg->depth); | |
3664 | if (img->pixmap == NO_PIXMAP) | |
3665 | { | |
3666 | x_clear_image (f, img); | |
3667 | rc = XpmNoMemory; | |
3668 | } | |
3669 | else if (img->mask_img) | |
3670 | { | |
3671 | img->mask = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
3672 | img->mask_img->width, | |
3673 | img->mask_img->height, | |
3674 | img->mask_img->depth); | |
3675 | if (img->mask == NO_PIXMAP) | |
3676 | { | |
3677 | x_clear_image (f, img); | |
3678 | rc = XpmNoMemory; | |
3679 | } | |
3680 | } | |
3681 | } | |
3682 | #endif | |
3683 | ||
4ef0d4d0 KS |
3684 | if (rc == XpmSuccess) |
3685 | { | |
3686 | #if defined (COLOR_TABLE_SUPPORT) && defined (ALLOC_XPM_COLORS) | |
3687 | img->colors = colors_in_color_table (&img->ncolors); | |
3688 | #else /* not ALLOC_XPM_COLORS */ | |
3689 | int i; | |
3690 | ||
3691 | #ifdef HAVE_NTGUI | |
3692 | /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap, | |
3693 | plus some duplicate attributes. */ | |
3694 | if (xpm_image && xpm_image->bitmap) | |
3695 | { | |
3696 | img->pixmap = xpm_image->bitmap; | |
3697 | /* XImageFree in libXpm frees XImage struct without destroying | |
3698 | the bitmap, which is what we want. */ | |
3699 | fn_XImageFree (xpm_image); | |
3700 | } | |
3701 | if (xpm_mask && xpm_mask->bitmap) | |
3702 | { | |
3703 | /* The mask appears to be inverted compared with what we expect. | |
3704 | TODO: invert our expectations. See other places where we | |
3705 | have to invert bits because our idea of masks is backwards. */ | |
3706 | HGDIOBJ old_obj; | |
3707 | old_obj = SelectObject (hdc, xpm_mask->bitmap); | |
3708 | ||
3709 | PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT); | |
3710 | SelectObject (hdc, old_obj); | |
3711 | ||
3712 | img->mask = xpm_mask->bitmap; | |
3713 | fn_XImageFree (xpm_mask); | |
3714 | DeleteDC (hdc); | |
3715 | } | |
3716 | ||
3717 | DeleteDC (hdc); | |
3718 | #endif /* HAVE_NTGUI */ | |
3719 | ||
3720 | /* Remember allocated colors. */ | |
0065d054 | 3721 | img->colors = xnmalloc (attrs.nalloc_pixels, sizeof *img->colors); |
ddff3151 | 3722 | img->ncolors = attrs.nalloc_pixels; |
4ef0d4d0 KS |
3723 | for (i = 0; i < attrs.nalloc_pixels; ++i) |
3724 | { | |
3725 | img->colors[i] = attrs.alloc_pixels[i]; | |
3726 | #ifdef DEBUG_X_COLORS | |
3727 | register_color (img->colors[i]); | |
3728 | #endif | |
3729 | } | |
3730 | #endif /* not ALLOC_XPM_COLORS */ | |
3731 | ||
3732 | img->width = attrs.width; | |
3733 | img->height = attrs.height; | |
a54e2c05 | 3734 | eassert (img->width > 0 && img->height > 0); |
4ef0d4d0 KS |
3735 | |
3736 | /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */ | |
3737 | #ifdef HAVE_NTGUI | |
3738 | fn_XpmFreeAttributes (&attrs); | |
3739 | #else | |
3740 | XpmFreeAttributes (&attrs); | |
3741 | #endif /* HAVE_NTGUI */ | |
547c9269 YM |
3742 | |
3743 | #ifdef HAVE_X_WINDOWS | |
3744 | /* Maybe fill in the background field while we have ximg handy. */ | |
3745 | IMAGE_BACKGROUND (img, f, img->ximg); | |
3746 | if (img->mask_img) | |
3747 | /* Fill in the background_transparent field while we have the | |
3748 | mask handy. */ | |
3749 | image_background_transparent (img, f, img->mask_img); | |
3750 | #endif | |
4ef0d4d0 KS |
3751 | } |
3752 | else | |
3753 | { | |
3754 | #ifdef HAVE_NTGUI | |
3755 | DeleteDC (hdc); | |
3756 | #endif /* HAVE_NTGUI */ | |
3757 | ||
3758 | switch (rc) | |
3759 | { | |
3760 | case XpmOpenFailed: | |
3761 | image_error ("Error opening XPM file (%s)", img->spec, Qnil); | |
3762 | break; | |
3763 | ||
3764 | case XpmFileInvalid: | |
3765 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
3766 | break; | |
3767 | ||
3768 | case XpmNoMemory: | |
3769 | image_error ("Out of memory (%s)", img->spec, Qnil); | |
3770 | break; | |
3771 | ||
3772 | case XpmColorFailed: | |
3773 | image_error ("Color allocation error (%s)", img->spec, Qnil); | |
3774 | break; | |
3775 | ||
3776 | default: | |
3777 | image_error ("Unknown error (%s)", img->spec, Qnil); | |
3778 | break; | |
3779 | } | |
3780 | } | |
3781 | ||
3782 | #ifdef ALLOC_XPM_COLORS | |
3783 | xpm_free_color_cache (); | |
3784 | #endif | |
3785 | return rc == XpmSuccess; | |
3786 | } | |
3787 | ||
3788 | #endif /* HAVE_XPM */ | |
3789 | ||
9e2a2647 | 3790 | #if defined (HAVE_NS) && !defined (HAVE_XPM) |
ea1aaa6f | 3791 | |
9e2a2647 | 3792 | /* XPM support functions for NS where libxpm is not available. |
ea1aaa6f ST |
3793 | Only XPM version 3 (without any extensions) is supported. */ |
3794 | ||
f57e2426 J |
3795 | static void xpm_put_color_table_v (Lisp_Object, const unsigned char *, |
3796 | int, Lisp_Object); | |
3797 | static Lisp_Object xpm_get_color_table_v (Lisp_Object, | |
3798 | const unsigned char *, int); | |
f57e2426 J |
3799 | static void xpm_put_color_table_h (Lisp_Object, const unsigned char *, |
3800 | int, Lisp_Object); | |
3801 | static Lisp_Object xpm_get_color_table_h (Lisp_Object, | |
3802 | const unsigned char *, int); | |
ea1aaa6f ST |
3803 | |
3804 | /* Tokens returned from xpm_scan. */ | |
3805 | ||
3806 | enum xpm_token | |
3807 | { | |
3808 | XPM_TK_IDENT = 256, | |
3809 | XPM_TK_STRING, | |
3810 | XPM_TK_EOF | |
3811 | }; | |
3812 | ||
3813 | /* Scan an XPM data and return a character (< 256) or a token defined | |
3814 | by enum xpm_token above. *S and END are the start (inclusive) and | |
3815 | the end (exclusive) addresses of the data, respectively. Advance | |
3816 | *S while scanning. If token is either XPM_TK_IDENT or | |
3817 | XPM_TK_STRING, *BEG and *LEN are set to the start address and the | |
3818 | length of the corresponding token, respectively. */ | |
3819 | ||
3820 | static int | |
3d608a86 J |
3821 | xpm_scan (const unsigned char **s, |
3822 | const unsigned char *end, | |
3823 | const unsigned char **beg, | |
74ca2eb3 | 3824 | ptrdiff_t *len) |
ea1aaa6f ST |
3825 | { |
3826 | int c; | |
3827 | ||
3828 | while (*s < end) | |
3829 | { | |
3830 | /* Skip white-space. */ | |
620f13b0 | 3831 | while (*s < end && (c = *(*s)++, c_isspace (c))) |
9ecf6403 | 3832 | ; |
ea1aaa6f ST |
3833 | |
3834 | /* gnus-pointer.xpm uses '-' in its identifier. | |
9ecf6403 | 3835 | sb-dir-plus.xpm uses '+' in its identifier. */ |
620f13b0 | 3836 | if (c_isalpha (c) || c == '_' || c == '-' || c == '+') |
9ecf6403 YM |
3837 | { |
3838 | *beg = *s - 1; | |
2ff0845e | 3839 | while (*s < end |
620f13b0 PE |
3840 | && (c = **s, c_isalnum (c) |
3841 | || c == '_' || c == '-' || c == '+')) | |
9ecf6403 YM |
3842 | ++*s; |
3843 | *len = *s - *beg; | |
3844 | return XPM_TK_IDENT; | |
3845 | } | |
ea1aaa6f | 3846 | else if (c == '"') |
9ecf6403 YM |
3847 | { |
3848 | *beg = *s; | |
3849 | while (*s < end && **s != '"') | |
3850 | ++*s; | |
3851 | *len = *s - *beg; | |
3852 | if (*s < end) | |
3853 | ++*s; | |
3854 | return XPM_TK_STRING; | |
3855 | } | |
ea1aaa6f | 3856 | else if (c == '/') |
9ecf6403 YM |
3857 | { |
3858 | if (*s < end && **s == '*') | |
3859 | { | |
3860 | /* C-style comment. */ | |
3861 | ++*s; | |
3862 | do | |
3863 | { | |
3864 | while (*s < end && *(*s)++ != '*') | |
3865 | ; | |
3866 | } | |
3867 | while (*s < end && **s != '/'); | |
3868 | if (*s < end) | |
3869 | ++*s; | |
3870 | } | |
3871 | else | |
3872 | return c; | |
3873 | } | |
ea1aaa6f | 3874 | else |
9ecf6403 | 3875 | return c; |
ea1aaa6f ST |
3876 | } |
3877 | ||
3878 | return XPM_TK_EOF; | |
3879 | } | |
3880 | ||
bbe6f2aa | 3881 | /* Functions for color table lookup in XPM data. A key is a string |
ea1aaa6f ST |
3882 | specifying the color of each pixel in XPM data. A value is either |
3883 | an integer that specifies a pixel color, Qt that specifies | |
3884 | transparency, or Qnil for the unspecified color. If the length of | |
3885 | the key string is one, a vector is used as a table. Otherwise, a | |
3886 | hash table is used. */ | |
3887 | ||
3888 | static Lisp_Object | |
3d608a86 J |
3889 | xpm_make_color_table_v (void (**put_func) (Lisp_Object, |
3890 | const unsigned char *, | |
3891 | int, | |
3892 | Lisp_Object), | |
3893 | Lisp_Object (**get_func) (Lisp_Object, | |
3894 | const unsigned char *, | |
3895 | int)) | |
ea1aaa6f ST |
3896 | { |
3897 | *put_func = xpm_put_color_table_v; | |
3898 | *get_func = xpm_get_color_table_v; | |
3899 | return Fmake_vector (make_number (256), Qnil); | |
3900 | } | |
3901 | ||
3902 | static void | |
3d608a86 J |
3903 | xpm_put_color_table_v (Lisp_Object color_table, |
3904 | const unsigned char *chars_start, | |
3905 | int chars_len, | |
3906 | Lisp_Object color) | |
ea1aaa6f | 3907 | { |
28be1ada | 3908 | ASET (color_table, *chars_start, color); |
ea1aaa6f ST |
3909 | } |
3910 | ||
3911 | static Lisp_Object | |
3d608a86 J |
3912 | xpm_get_color_table_v (Lisp_Object color_table, |
3913 | const unsigned char *chars_start, | |
3914 | int chars_len) | |
ea1aaa6f | 3915 | { |
28be1ada | 3916 | return AREF (color_table, *chars_start); |
ea1aaa6f ST |
3917 | } |
3918 | ||
3919 | static Lisp_Object | |
3d608a86 J |
3920 | xpm_make_color_table_h (void (**put_func) (Lisp_Object, |
3921 | const unsigned char *, | |
3922 | int, | |
3923 | Lisp_Object), | |
3924 | Lisp_Object (**get_func) (Lisp_Object, | |
3925 | const unsigned char *, | |
3926 | int)) | |
ea1aaa6f ST |
3927 | { |
3928 | *put_func = xpm_put_color_table_h; | |
3929 | *get_func = xpm_get_color_table_h; | |
4ab15c3e | 3930 | return make_hash_table (hashtest_equal, make_number (DEFAULT_HASH_SIZE), |
9ecf6403 YM |
3931 | make_float (DEFAULT_REHASH_SIZE), |
3932 | make_float (DEFAULT_REHASH_THRESHOLD), | |
a23c4171 | 3933 | Qnil); |
ea1aaa6f ST |
3934 | } |
3935 | ||
3936 | static void | |
3d608a86 J |
3937 | xpm_put_color_table_h (Lisp_Object color_table, |
3938 | const unsigned char *chars_start, | |
3939 | int chars_len, | |
3940 | Lisp_Object color) | |
ea1aaa6f ST |
3941 | { |
3942 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
0de4bb68 | 3943 | EMACS_UINT hash_code; |
ea1aaa6f ST |
3944 | Lisp_Object chars = make_unibyte_string (chars_start, chars_len); |
3945 | ||
3946 | hash_lookup (table, chars, &hash_code); | |
3947 | hash_put (table, chars, color, hash_code); | |
3948 | } | |
3949 | ||
3950 | static Lisp_Object | |
3d608a86 J |
3951 | xpm_get_color_table_h (Lisp_Object color_table, |
3952 | const unsigned char *chars_start, | |
3953 | int chars_len) | |
ea1aaa6f ST |
3954 | { |
3955 | struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); | |
d3411f89 PE |
3956 | ptrdiff_t i = |
3957 | hash_lookup (table, make_unibyte_string (chars_start, chars_len), NULL); | |
ea1aaa6f ST |
3958 | |
3959 | return i >= 0 ? HASH_VALUE (table, i) : Qnil; | |
3960 | } | |
3961 | ||
3962 | enum xpm_color_key { | |
3963 | XPM_COLOR_KEY_S, | |
3964 | XPM_COLOR_KEY_M, | |
3965 | XPM_COLOR_KEY_G4, | |
3966 | XPM_COLOR_KEY_G, | |
3967 | XPM_COLOR_KEY_C | |
3968 | }; | |
3969 | ||
46accb8f | 3970 | static const char xpm_color_key_strings[][4] = {"s", "m", "g4", "g", "c"}; |
ea1aaa6f ST |
3971 | |
3972 | static int | |
3d608a86 | 3973 | xpm_str_to_color_key (const char *s) |
ea1aaa6f ST |
3974 | { |
3975 | int i; | |
3976 | ||
c72d972c | 3977 | for (i = 0; i < ARRAYELTS (xpm_color_key_strings); i++) |
ea1aaa6f ST |
3978 | if (strcmp (xpm_color_key_strings[i], s) == 0) |
3979 | return i; | |
3980 | return -1; | |
3981 | } | |
3982 | ||
578098f3 | 3983 | static bool |
3d608a86 J |
3984 | xpm_load_image (struct frame *f, |
3985 | struct image *img, | |
3986 | const unsigned char *contents, | |
3987 | const unsigned char *end) | |
ea1aaa6f | 3988 | { |
46accb8f | 3989 | const unsigned char *s = contents, *beg, *str; |
ea1aaa6f ST |
3990 | unsigned char buffer[BUFSIZ]; |
3991 | int width, height, x, y; | |
3992 | int num_colors, chars_per_pixel; | |
74ca2eb3 PE |
3993 | ptrdiff_t len; |
3994 | int LA1; | |
46accb8f YM |
3995 | void (*put_color_table) (Lisp_Object, const unsigned char *, int, Lisp_Object); |
3996 | Lisp_Object (*get_color_table) (Lisp_Object, const unsigned char *, int); | |
ea1aaa6f | 3997 | Lisp_Object frame, color_symbols, color_table; |
578098f3 | 3998 | int best_key; |
96c06863 | 3999 | bool have_mask = false; |
ea1aaa6f ST |
4000 | XImagePtr ximg = NULL, mask_img = NULL; |
4001 | ||
4002 | #define match() \ | |
4003 | LA1 = xpm_scan (&s, end, &beg, &len) | |
4004 | ||
9ecf6403 | 4005 | #define expect(TOKEN) \ |
f462f075 PE |
4006 | do \ |
4007 | { \ | |
4008 | if (LA1 != (TOKEN)) \ | |
4009 | goto failure; \ | |
4010 | match (); \ | |
4011 | } \ | |
4012 | while (0) | |
ea1aaa6f | 4013 | |
9ecf6403 | 4014 | #define expect_ident(IDENT) \ |
ea1aaa6f | 4015 | if (LA1 == XPM_TK_IDENT \ |
9ecf6403 YM |
4016 | && strlen ((IDENT)) == len && memcmp ((IDENT), beg, len) == 0) \ |
4017 | match (); \ | |
4018 | else \ | |
ea1aaa6f ST |
4019 | goto failure |
4020 | ||
4021 | if (!(end - s >= 9 && memcmp (s, "/* XPM */", 9) == 0)) | |
4022 | goto failure; | |
4023 | s += 9; | |
ed3751c8 | 4024 | match (); |
ea1aaa6f ST |
4025 | expect_ident ("static"); |
4026 | expect_ident ("char"); | |
4027 | expect ('*'); | |
4028 | expect (XPM_TK_IDENT); | |
4029 | expect ('['); | |
4030 | expect (']'); | |
4031 | expect ('='); | |
4032 | expect ('{'); | |
4033 | expect (XPM_TK_STRING); | |
4034 | if (len >= BUFSIZ) | |
4035 | goto failure; | |
4036 | memcpy (buffer, beg, len); | |
4037 | buffer[len] = '\0'; | |
4038 | if (sscanf (buffer, "%d %d %d %d", &width, &height, | |
9ecf6403 | 4039 | &num_colors, &chars_per_pixel) != 4 |
ea1aaa6f ST |
4040 | || width <= 0 || height <= 0 |
4041 | || num_colors <= 0 || chars_per_pixel <= 0) | |
4042 | goto failure; | |
de297941 YM |
4043 | |
4044 | if (!check_image_size (f, width, height)) | |
4045 | { | |
10ea2b82 | 4046 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
de297941 YM |
4047 | goto failure; |
4048 | } | |
4049 | ||
547c9269 | 4050 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0) |
ca4aa935 | 4051 | #ifndef HAVE_NS |
547c9269 YM |
4052 | || !image_create_x_image_and_pixmap (f, img, width, height, 1, |
4053 | &mask_img, 1) | |
ca4aa935 PE |
4054 | #endif |
4055 | ) | |
4056 | { | |
4057 | image_error ("Image too large", Qnil, Qnil); | |
4058 | goto failure; | |
4059 | } | |
4060 | ||
ea1aaa6f ST |
4061 | expect (','); |
4062 | ||
4063 | XSETFRAME (frame, f); | |
4064 | if (!NILP (Fxw_display_color_p (frame))) | |
4065 | best_key = XPM_COLOR_KEY_C; | |
4066 | else if (!NILP (Fx_display_grayscale_p (frame))) | |
4067 | best_key = (XFASTINT (Fx_display_planes (frame)) > 2 | |
9ecf6403 | 4068 | ? XPM_COLOR_KEY_G : XPM_COLOR_KEY_G4); |
ea1aaa6f ST |
4069 | else |
4070 | best_key = XPM_COLOR_KEY_M; | |
4071 | ||
4072 | color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL); | |
4073 | if (chars_per_pixel == 1) | |
4074 | color_table = xpm_make_color_table_v (&put_color_table, | |
9ecf6403 | 4075 | &get_color_table); |
ea1aaa6f ST |
4076 | else |
4077 | color_table = xpm_make_color_table_h (&put_color_table, | |
9ecf6403 | 4078 | &get_color_table); |
ea1aaa6f ST |
4079 | |
4080 | while (num_colors-- > 0) | |
4081 | { | |
25a48bd0 | 4082 | char *color, *max_color; |
ea1aaa6f ST |
4083 | int key, next_key, max_key = 0; |
4084 | Lisp_Object symbol_color = Qnil, color_val; | |
4085 | XColor cdef; | |
4086 | ||
4087 | expect (XPM_TK_STRING); | |
4088 | if (len <= chars_per_pixel || len >= BUFSIZ + chars_per_pixel) | |
9ecf6403 | 4089 | goto failure; |
ea1aaa6f ST |
4090 | memcpy (buffer, beg + chars_per_pixel, len - chars_per_pixel); |
4091 | buffer[len - chars_per_pixel] = '\0'; | |
4092 | ||
4093 | str = strtok (buffer, " \t"); | |
4094 | if (str == NULL) | |
9ecf6403 | 4095 | goto failure; |
ea1aaa6f ST |
4096 | key = xpm_str_to_color_key (str); |
4097 | if (key < 0) | |
9ecf6403 | 4098 | goto failure; |
ea1aaa6f | 4099 | do |
9ecf6403 YM |
4100 | { |
4101 | color = strtok (NULL, " \t"); | |
4102 | if (color == NULL) | |
4103 | goto failure; | |
ea1aaa6f | 4104 | |
9db94f82 | 4105 | while ((str = strtok (NULL, " \t")) != NULL) |
9ecf6403 YM |
4106 | { |
4107 | next_key = xpm_str_to_color_key (str); | |
4108 | if (next_key >= 0) | |
4109 | break; | |
4110 | color[strlen (color)] = ' '; | |
4111 | } | |
ea1aaa6f | 4112 | |
9ecf6403 YM |
4113 | if (key == XPM_COLOR_KEY_S) |
4114 | { | |
4115 | if (NILP (symbol_color)) | |
4116 | symbol_color = build_string (color); | |
4117 | } | |
4118 | else if (max_key < key && key <= best_key) | |
4119 | { | |
4120 | max_key = key; | |
4121 | max_color = color; | |
4122 | } | |
4123 | key = next_key; | |
4124 | } | |
ea1aaa6f ST |
4125 | while (str); |
4126 | ||
4127 | color_val = Qnil; | |
4128 | if (!NILP (color_symbols) && !NILP (symbol_color)) | |
9ecf6403 YM |
4129 | { |
4130 | Lisp_Object specified_color = Fassoc (symbol_color, color_symbols); | |
4131 | ||
4132 | if (CONSP (specified_color) && STRINGP (XCDR (specified_color))) | |
9db94f82 | 4133 | { |
25a48bd0 | 4134 | if (xstrcasecmp (SSDATA (XCDR (specified_color)), "None") == 0) |
9db94f82 | 4135 | color_val = Qt; |
6045c4fd | 4136 | else if (x_defined_color (f, SSDATA (XCDR (specified_color)), |
9db94f82 YM |
4137 | &cdef, 0)) |
4138 | color_val = make_number (cdef.pixel); | |
4139 | } | |
9ecf6403 | 4140 | } |
ea1aaa6f | 4141 | if (NILP (color_val) && max_key > 0) |
9db94f82 | 4142 | { |
05131107 | 4143 | if (xstrcasecmp (max_color, "None") == 0) |
9db94f82 YM |
4144 | color_val = Qt; |
4145 | else if (x_defined_color (f, max_color, &cdef, 0)) | |
4146 | color_val = make_number (cdef.pixel); | |
4147 | } | |
ea1aaa6f | 4148 | if (!NILP (color_val)) |
9ecf6403 | 4149 | (*put_color_table) (color_table, beg, chars_per_pixel, color_val); |
ea1aaa6f ST |
4150 | |
4151 | expect (','); | |
4152 | } | |
4153 | ||
ea1aaa6f ST |
4154 | for (y = 0; y < height; y++) |
4155 | { | |
4156 | expect (XPM_TK_STRING); | |
4157 | str = beg; | |
4158 | if (len < width * chars_per_pixel) | |
9ecf6403 | 4159 | goto failure; |
ea1aaa6f | 4160 | for (x = 0; x < width; x++, str += chars_per_pixel) |
9ecf6403 YM |
4161 | { |
4162 | Lisp_Object color_val = | |
4163 | (*get_color_table) (color_table, str, chars_per_pixel); | |
4164 | ||
4165 | XPutPixel (ximg, x, y, | |
4166 | (INTEGERP (color_val) ? XINT (color_val) | |
4167 | : FRAME_FOREGROUND_PIXEL (f))); | |
edfda783 | 4168 | #ifndef HAVE_NS |
9ecf6403 YM |
4169 | XPutPixel (mask_img, x, y, |
4170 | (!EQ (color_val, Qt) ? PIX_MASK_DRAW | |
96c06863 | 4171 | : (have_mask = true, PIX_MASK_RETAIN))); |
edfda783 | 4172 | #else |
ed3751c8 JB |
4173 | if (EQ (color_val, Qt)) |
4174 | ns_set_alpha (ximg, x, y, 0); | |
edfda783 | 4175 | #endif |
9ecf6403 | 4176 | } |
ea1aaa6f | 4177 | if (y + 1 < height) |
9ecf6403 | 4178 | expect (','); |
ea1aaa6f ST |
4179 | } |
4180 | ||
4181 | img->width = width; | |
4182 | img->height = height; | |
4183 | ||
0b8ac842 YM |
4184 | /* Maybe fill in the background field while we have ximg handy. */ |
4185 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
4186 | IMAGE_BACKGROUND (img, f, ximg); | |
4187 | ||
547c9269 | 4188 | image_put_x_image (f, img, ximg, 0); |
edfda783 | 4189 | #ifndef HAVE_NS |
ea1aaa6f ST |
4190 | if (have_mask) |
4191 | { | |
9ecf6403 YM |
4192 | /* Fill in the background_transparent field while we have the |
4193 | mask handy. */ | |
4194 | image_background_transparent (img, f, mask_img); | |
4195 | ||
547c9269 | 4196 | image_put_x_image (f, img, mask_img, 1); |
ea1aaa6f ST |
4197 | } |
4198 | else | |
4199 | { | |
4200 | x_destroy_x_image (mask_img); | |
547c9269 | 4201 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
ea1aaa6f | 4202 | } |
edfda783 | 4203 | #endif |
ea1aaa6f ST |
4204 | return 1; |
4205 | ||
4206 | failure: | |
4207 | image_error ("Invalid XPM file (%s)", img->spec, Qnil); | |
ea1aaa6f ST |
4208 | x_destroy_x_image (ximg); |
4209 | x_destroy_x_image (mask_img); | |
4210 | x_clear_image (f, img); | |
4211 | return 0; | |
4212 | ||
4213 | #undef match | |
4214 | #undef expect | |
4215 | #undef expect_ident | |
4216 | } | |
4217 | ||
578098f3 | 4218 | static bool |
3d608a86 J |
4219 | xpm_load (struct frame *f, |
4220 | struct image *img) | |
ea1aaa6f | 4221 | { |
578098f3 | 4222 | bool success_p = 0; |
ea1aaa6f ST |
4223 | Lisp_Object file_name; |
4224 | ||
4225 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
4226 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
4227 | if (STRINGP (file_name)) | |
4228 | { | |
4229 | Lisp_Object file; | |
4230 | unsigned char *contents; | |
dd52fcea | 4231 | ptrdiff_t size; |
ea1aaa6f ST |
4232 | |
4233 | file = x_find_image_file (file_name); | |
ea1aaa6f | 4234 | if (!STRINGP (file)) |
9ecf6403 YM |
4235 | { |
4236 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
9ecf6403 YM |
4237 | return 0; |
4238 | } | |
ea1aaa6f | 4239 | |
6045c4fd | 4240 | contents = slurp_file (SSDATA (file), &size); |
ea1aaa6f | 4241 | if (contents == NULL) |
9ecf6403 YM |
4242 | { |
4243 | image_error ("Error loading XPM image `%s'", img->spec, Qnil); | |
9ecf6403 YM |
4244 | return 0; |
4245 | } | |
ea1aaa6f ST |
4246 | |
4247 | success_p = xpm_load_image (f, img, contents, contents + size); | |
4248 | xfree (contents); | |
ea1aaa6f ST |
4249 | } |
4250 | else | |
4251 | { | |
4252 | Lisp_Object data; | |
4253 | ||
4254 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
4255 | if (!STRINGP (data)) |
4256 | { | |
4257 | image_error ("Invalid image data `%s'", data, Qnil); | |
4258 | return 0; | |
4259 | } | |
ea1aaa6f | 4260 | success_p = xpm_load_image (f, img, SDATA (data), |
9ecf6403 | 4261 | SDATA (data) + SBYTES (data)); |
ea1aaa6f ST |
4262 | } |
4263 | ||
4264 | return success_p; | |
4265 | } | |
4266 | ||
9e2a2647 | 4267 | #endif /* HAVE_NS && !HAVE_XPM */ |
b0da69a7 | 4268 | |
ea1aaa6f | 4269 | |
4ef0d4d0 KS |
4270 | \f |
4271 | /*********************************************************************** | |
4272 | Color table | |
4273 | ***********************************************************************/ | |
4274 | ||
4275 | #ifdef COLOR_TABLE_SUPPORT | |
4276 | ||
4277 | /* An entry in the color table mapping an RGB color to a pixel color. */ | |
4278 | ||
4279 | struct ct_color | |
4280 | { | |
4281 | int r, g, b; | |
4282 | unsigned long pixel; | |
4283 | ||
4284 | /* Next in color table collision list. */ | |
4285 | struct ct_color *next; | |
4286 | }; | |
4287 | ||
4288 | /* The bucket vector size to use. Must be prime. */ | |
4289 | ||
4290 | #define CT_SIZE 101 | |
4291 | ||
4292 | /* Value is a hash of the RGB color given by R, G, and B. */ | |
4293 | ||
4294 | #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B)) | |
4295 | ||
4296 | /* The color hash table. */ | |
4297 | ||
cd44d2eb | 4298 | static struct ct_color **ct_table; |
4ef0d4d0 KS |
4299 | |
4300 | /* Number of entries in the color table. */ | |
4301 | ||
cd44d2eb | 4302 | static int ct_colors_allocated; |
ddff3151 PE |
4303 | enum |
4304 | { | |
4305 | ct_colors_allocated_max = | |
4306 | min (INT_MAX, | |
4307 | min (PTRDIFF_MAX, SIZE_MAX) / sizeof (unsigned long)) | |
4308 | }; | |
4ef0d4d0 KS |
4309 | |
4310 | /* Initialize the color table. */ | |
4311 | ||
4312 | static void | |
d3da34e0 | 4313 | init_color_table (void) |
4ef0d4d0 KS |
4314 | { |
4315 | int size = CT_SIZE * sizeof (*ct_table); | |
23f86fce | 4316 | ct_table = xzalloc (size); |
4ef0d4d0 KS |
4317 | ct_colors_allocated = 0; |
4318 | } | |
4319 | ||
4320 | ||
4321 | /* Free memory associated with the color table. */ | |
4322 | ||
4323 | static void | |
d3da34e0 | 4324 | free_color_table (void) |
4ef0d4d0 KS |
4325 | { |
4326 | int i; | |
4327 | struct ct_color *p, *next; | |
4328 | ||
4329 | for (i = 0; i < CT_SIZE; ++i) | |
4330 | for (p = ct_table[i]; p; p = next) | |
4331 | { | |
4332 | next = p->next; | |
4333 | xfree (p); | |
4334 | } | |
4335 | ||
4336 | xfree (ct_table); | |
4337 | ct_table = NULL; | |
4338 | } | |
4339 | ||
4340 | ||
4341 | /* Value is a pixel color for RGB color R, G, B on frame F. If an | |
4342 | entry for that color already is in the color table, return the | |
4343 | pixel color of that entry. Otherwise, allocate a new color for R, | |
4344 | G, B, and make an entry in the color table. */ | |
4345 | ||
4346 | static unsigned long | |
d3da34e0 | 4347 | lookup_rgb_color (struct frame *f, int r, int g, int b) |
4ef0d4d0 KS |
4348 | { |
4349 | unsigned hash = CT_HASH_RGB (r, g, b); | |
4350 | int i = hash % CT_SIZE; | |
4351 | struct ct_color *p; | |
4352 | Display_Info *dpyinfo; | |
4353 | ||
4354 | /* Handle TrueColor visuals specially, which improves performance by | |
4355 | two orders of magnitude. Freeing colors on TrueColor visuals is | |
4356 | a nop, and pixel colors specify RGB values directly. See also | |
4357 | the Xlib spec, chapter 3.1. */ | |
aad3612f | 4358 | dpyinfo = FRAME_DISPLAY_INFO (f); |
4ef0d4d0 KS |
4359 | if (dpyinfo->red_bits > 0) |
4360 | { | |
4361 | unsigned long pr, pg, pb; | |
4362 | ||
4363 | /* Apply gamma-correction like normal color allocation does. */ | |
4364 | if (f->gamma) | |
4365 | { | |
4366 | XColor color; | |
4367 | color.red = r, color.green = g, color.blue = b; | |
4368 | gamma_correct (f, &color); | |
4369 | r = color.red, g = color.green, b = color.blue; | |
4370 | } | |
4371 | ||
4372 | /* Scale down RGB values to the visual's bits per RGB, and shift | |
4373 | them to the right position in the pixel color. Note that the | |
4374 | original RGB values are 16-bit values, as usual in X. */ | |
4375 | pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset; | |
4376 | pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset; | |
4377 | pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset; | |
4378 | ||
4379 | /* Assemble the pixel color. */ | |
4380 | return pr | pg | pb; | |
4381 | } | |
1f026899 | 4382 | |
4ef0d4d0 KS |
4383 | for (p = ct_table[i]; p; p = p->next) |
4384 | if (p->r == r && p->g == g && p->b == b) | |
4385 | break; | |
4386 | ||
4387 | if (p == NULL) | |
4388 | { | |
4389 | ||
4390 | #ifdef HAVE_X_WINDOWS | |
4391 | XColor color; | |
4392 | Colormap cmap; | |
578098f3 | 4393 | bool rc; |
ddff3151 PE |
4394 | #else |
4395 | COLORREF color; | |
4396 | #endif | |
4ef0d4d0 | 4397 | |
ddff3151 PE |
4398 | if (ct_colors_allocated_max <= ct_colors_allocated) |
4399 | return FRAME_FOREGROUND_PIXEL (f); | |
4400 | ||
4401 | #ifdef HAVE_X_WINDOWS | |
4ef0d4d0 KS |
4402 | color.red = r; |
4403 | color.green = g; | |
4404 | color.blue = b; | |
4405 | ||
4406 | cmap = FRAME_X_COLORMAP (f); | |
4407 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4408 | if (rc) | |
4409 | { | |
4410 | ++ct_colors_allocated; | |
23f86fce | 4411 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4412 | p->r = r; |
4413 | p->g = g; | |
4414 | p->b = b; | |
4415 | p->pixel = color.pixel; | |
4416 | p->next = ct_table[i]; | |
4417 | ct_table[i] = p; | |
4418 | } | |
4419 | else | |
4420 | return FRAME_FOREGROUND_PIXEL (f); | |
4421 | ||
4422 | #else | |
4ef0d4d0 KS |
4423 | #ifdef HAVE_NTGUI |
4424 | color = PALETTERGB (r, g, b); | |
4425 | #else | |
4426 | color = RGB_TO_ULONG (r, g, b); | |
4427 | #endif /* HAVE_NTGUI */ | |
4428 | ++ct_colors_allocated; | |
23f86fce | 4429 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4430 | p->r = r; |
4431 | p->g = g; | |
4432 | p->b = b; | |
4433 | p->pixel = color; | |
4434 | p->next = ct_table[i]; | |
4435 | ct_table[i] = p; | |
4436 | #endif /* HAVE_X_WINDOWS */ | |
4437 | ||
4438 | } | |
4439 | ||
4440 | return p->pixel; | |
4441 | } | |
4442 | ||
4443 | ||
4444 | /* Look up pixel color PIXEL which is used on frame F in the color | |
4445 | table. If not already present, allocate it. Value is PIXEL. */ | |
4446 | ||
4447 | static unsigned long | |
d3da34e0 | 4448 | lookup_pixel_color (struct frame *f, unsigned long pixel) |
4ef0d4d0 KS |
4449 | { |
4450 | int i = pixel % CT_SIZE; | |
4451 | struct ct_color *p; | |
4452 | ||
4453 | for (p = ct_table[i]; p; p = p->next) | |
4454 | if (p->pixel == pixel) | |
4455 | break; | |
4456 | ||
4457 | if (p == NULL) | |
4458 | { | |
4459 | XColor color; | |
4460 | Colormap cmap; | |
578098f3 | 4461 | bool rc; |
4ef0d4d0 | 4462 | |
ddff3151 PE |
4463 | if (ct_colors_allocated_max <= ct_colors_allocated) |
4464 | return FRAME_FOREGROUND_PIXEL (f); | |
4465 | ||
4ef0d4d0 KS |
4466 | #ifdef HAVE_X_WINDOWS |
4467 | cmap = FRAME_X_COLORMAP (f); | |
4468 | color.pixel = pixel; | |
4469 | x_query_color (f, &color); | |
4470 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4471 | #else | |
4d7e6e51 | 4472 | block_input (); |
4ef0d4d0 KS |
4473 | cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f)); |
4474 | color.pixel = pixel; | |
4475 | XQueryColor (NULL, cmap, &color); | |
4476 | rc = x_alloc_nearest_color (f, cmap, &color); | |
4d7e6e51 | 4477 | unblock_input (); |
4ef0d4d0 KS |
4478 | #endif /* HAVE_X_WINDOWS */ |
4479 | ||
4480 | if (rc) | |
4481 | { | |
4482 | ++ct_colors_allocated; | |
4483 | ||
23f86fce | 4484 | p = xmalloc (sizeof *p); |
4ef0d4d0 KS |
4485 | p->r = color.red; |
4486 | p->g = color.green; | |
4487 | p->b = color.blue; | |
4488 | p->pixel = pixel; | |
4489 | p->next = ct_table[i]; | |
4490 | ct_table[i] = p; | |
4491 | } | |
4492 | else | |
4493 | return FRAME_FOREGROUND_PIXEL (f); | |
4494 | } | |
4495 | return p->pixel; | |
4496 | } | |
4497 | ||
4498 | ||
4499 | /* Value is a vector of all pixel colors contained in the color table, | |
4500 | allocated via xmalloc. Set *N to the number of colors. */ | |
4501 | ||
4502 | static unsigned long * | |
d3da34e0 | 4503 | colors_in_color_table (int *n) |
4ef0d4d0 KS |
4504 | { |
4505 | int i, j; | |
4506 | struct ct_color *p; | |
4507 | unsigned long *colors; | |
4508 | ||
4509 | if (ct_colors_allocated == 0) | |
4510 | { | |
4511 | *n = 0; | |
4512 | colors = NULL; | |
4513 | } | |
4514 | else | |
4515 | { | |
23f86fce | 4516 | colors = xmalloc (ct_colors_allocated * sizeof *colors); |
4ef0d4d0 KS |
4517 | *n = ct_colors_allocated; |
4518 | ||
4519 | for (i = j = 0; i < CT_SIZE; ++i) | |
4520 | for (p = ct_table[i]; p; p = p->next) | |
4521 | colors[j++] = p->pixel; | |
4522 | } | |
4523 | ||
4524 | return colors; | |
4525 | } | |
4526 | ||
4527 | #else /* COLOR_TABLE_SUPPORT */ | |
4528 | ||
4529 | static unsigned long | |
7c3320d8 | 4530 | lookup_rgb_color (struct frame *f, int r, int g, int b) |
4ef0d4d0 KS |
4531 | { |
4532 | unsigned long pixel; | |
4533 | ||
4ef0d4d0 KS |
4534 | #ifdef HAVE_NTGUI |
4535 | pixel = PALETTERGB (r >> 8, g >> 8, b >> 8); | |
4536 | #endif /* HAVE_NTGUI */ | |
4537 | ||
edfda783 AR |
4538 | #ifdef HAVE_NS |
4539 | pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8); | |
4540 | #endif /* HAVE_NS */ | |
4ef0d4d0 KS |
4541 | return pixel; |
4542 | } | |
4543 | ||
4544 | static void | |
7c3320d8 | 4545 | init_color_table (void) |
4ef0d4d0 KS |
4546 | { |
4547 | } | |
4548 | #endif /* COLOR_TABLE_SUPPORT */ | |
4549 | ||
4550 | \f | |
4551 | /*********************************************************************** | |
4552 | Algorithms | |
4553 | ***********************************************************************/ | |
4554 | ||
4ef0d4d0 KS |
4555 | /* Edge detection matrices for different edge-detection |
4556 | strategies. */ | |
4557 | ||
4558 | static int emboss_matrix[9] = { | |
4559 | /* x - 1 x x + 1 */ | |
4560 | 2, -1, 0, /* y - 1 */ | |
4561 | -1, 0, 1, /* y */ | |
4562 | 0, 1, -2 /* y + 1 */ | |
4563 | }; | |
4564 | ||
4565 | static int laplace_matrix[9] = { | |
4566 | /* x - 1 x x + 1 */ | |
4567 | 1, 0, 0, /* y - 1 */ | |
4568 | 0, 0, 0, /* y */ | |
4569 | 0, 0, -1 /* y + 1 */ | |
4570 | }; | |
4571 | ||
4572 | /* Value is the intensity of the color whose red/green/blue values | |
4573 | are R, G, and B. */ | |
4574 | ||
4575 | #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6) | |
4576 | ||
4577 | ||
4578 | /* On frame F, return an array of XColor structures describing image | |
4579 | IMG->pixmap. Each XColor structure has its pixel color set. RGB_P | |
578098f3 | 4580 | means also fill the red/green/blue members of the XColor |
4ef0d4d0 KS |
4581 | structures. Value is a pointer to the array of XColors structures, |
4582 | allocated with xmalloc; it must be freed by the caller. */ | |
4583 | ||
4584 | static XColor * | |
578098f3 | 4585 | x_to_xcolors (struct frame *f, struct image *img, bool rgb_p) |
4ef0d4d0 KS |
4586 | { |
4587 | int x, y; | |
4588 | XColor *colors, *p; | |
4589 | XImagePtr_or_DC ximg; | |
4590 | #ifdef HAVE_NTGUI | |
4ef0d4d0 KS |
4591 | HGDIOBJ prev; |
4592 | #endif /* HAVE_NTGUI */ | |
4593 | ||
ddff3151 PE |
4594 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width < img->height) |
4595 | memory_full (SIZE_MAX); | |
23f86fce | 4596 | colors = xmalloc (sizeof *colors * img->width * img->height); |
4ef0d4d0 | 4597 | |
547c9269 YM |
4598 | /* Get the X image or create a memory device context for IMG. */ |
4599 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); | |
4ef0d4d0 KS |
4600 | |
4601 | /* Fill the `pixel' members of the XColor array. I wished there | |
4602 | were an easy and portable way to circumvent XGetPixel. */ | |
4603 | p = colors; | |
4604 | for (y = 0; y < img->height; ++y) | |
4605 | { | |
8b7d0a16 | 4606 | #if defined (HAVE_X_WINDOWS) || defined (HAVE_NTGUI) |
6045c4fd | 4607 | XColor *row = p; |
4ef0d4d0 | 4608 | for (x = 0; x < img->width; ++x, ++p) |
8b7d0a16 | 4609 | p->pixel = GET_PIXEL (ximg, x, y); |
4ef0d4d0 KS |
4610 | if (rgb_p) |
4611 | x_query_colors (f, row, img->width); | |
4612 | ||
4613 | #else | |
4614 | ||
4615 | for (x = 0; x < img->width; ++x, ++p) | |
4616 | { | |
4617 | /* W32_TODO: palette support needed here? */ | |
4618 | p->pixel = GET_PIXEL (ximg, x, y); | |
4619 | if (rgb_p) | |
4620 | { | |
4ef0d4d0 KS |
4621 | p->red = RED16_FROM_ULONG (p->pixel); |
4622 | p->green = GREEN16_FROM_ULONG (p->pixel); | |
4623 | p->blue = BLUE16_FROM_ULONG (p->pixel); | |
4ef0d4d0 KS |
4624 | } |
4625 | } | |
4626 | #endif /* HAVE_X_WINDOWS */ | |
4627 | } | |
4628 | ||
547c9269 | 4629 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
4ef0d4d0 KS |
4630 | |
4631 | return colors; | |
4632 | } | |
4633 | ||
4634 | #ifdef HAVE_NTGUI | |
4635 | ||
4636 | /* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been | |
4637 | created with CreateDIBSection, with the pointer to the bit values | |
4638 | stored in ximg->data. */ | |
4639 | ||
3046a84b | 4640 | static void |
7c3320d8 | 4641 | XPutPixel (XImagePtr ximg, int x, int y, COLORREF color) |
4ef0d4d0 KS |
4642 | { |
4643 | int width = ximg->info.bmiHeader.biWidth; | |
4ef0d4d0 KS |
4644 | unsigned char * pixel; |
4645 | ||
4646 | /* True color images. */ | |
4647 | if (ximg->info.bmiHeader.biBitCount == 24) | |
4648 | { | |
4649 | int rowbytes = width * 3; | |
4650 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4651 | if (rowbytes % 4) | |
4652 | rowbytes += 4 - (rowbytes % 4); | |
4653 | ||
4654 | pixel = ximg->data + y * rowbytes + x * 3; | |
4655 | /* Windows bitmaps are in BGR order. */ | |
4656 | *pixel = GetBValue (color); | |
4657 | *(pixel + 1) = GetGValue (color); | |
4658 | *(pixel + 2) = GetRValue (color); | |
4659 | } | |
4660 | /* Monochrome images. */ | |
4661 | else if (ximg->info.bmiHeader.biBitCount == 1) | |
4662 | { | |
4663 | int rowbytes = width / 8; | |
4664 | /* Ensure scanlines are aligned on 4 byte boundaries. */ | |
4665 | if (rowbytes % 4) | |
4666 | rowbytes += 4 - (rowbytes % 4); | |
4667 | pixel = ximg->data + y * rowbytes + x / 8; | |
4668 | /* Filter out palette info. */ | |
4669 | if (color & 0x00ffffff) | |
4670 | *pixel = *pixel | (1 << x % 8); | |
4671 | else | |
4672 | *pixel = *pixel & ~(1 << x % 8); | |
4673 | } | |
4674 | else | |
4675 | image_error ("XPutPixel: palette image not supported", Qnil, Qnil); | |
4676 | } | |
4677 | ||
4678 | #endif /* HAVE_NTGUI */ | |
4679 | ||
4680 | /* Create IMG->pixmap from an array COLORS of XColor structures, whose | |
4681 | RGB members are set. F is the frame on which this all happens. | |
4682 | COLORS will be freed; an existing IMG->pixmap will be freed, too. */ | |
4683 | ||
4684 | static void | |
d3da34e0 | 4685 | x_from_xcolors (struct frame *f, struct image *img, XColor *colors) |
4ef0d4d0 KS |
4686 | { |
4687 | int x, y; | |
edfda783 | 4688 | XImagePtr oimg = NULL; |
4ef0d4d0 KS |
4689 | XColor *p; |
4690 | ||
4691 | init_color_table (); | |
4692 | ||
547c9269 YM |
4693 | x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP | CLEAR_IMAGE_COLORS); |
4694 | image_create_x_image_and_pixmap (f, img, img->width, img->height, 0, | |
4695 | &oimg, 0); | |
4ef0d4d0 KS |
4696 | p = colors; |
4697 | for (y = 0; y < img->height; ++y) | |
4698 | for (x = 0; x < img->width; ++x, ++p) | |
4699 | { | |
4700 | unsigned long pixel; | |
4701 | pixel = lookup_rgb_color (f, p->red, p->green, p->blue); | |
4702 | XPutPixel (oimg, x, y, pixel); | |
4703 | } | |
4704 | ||
4705 | xfree (colors); | |
4ef0d4d0 | 4706 | |
547c9269 | 4707 | image_put_x_image (f, img, oimg, 0); |
4ef0d4d0 KS |
4708 | #ifdef COLOR_TABLE_SUPPORT |
4709 | img->colors = colors_in_color_table (&img->ncolors); | |
4710 | free_color_table (); | |
4711 | #endif /* COLOR_TABLE_SUPPORT */ | |
4712 | } | |
4713 | ||
4714 | ||
4715 | /* On frame F, perform edge-detection on image IMG. | |
4716 | ||
4717 | MATRIX is a nine-element array specifying the transformation | |
4718 | matrix. See emboss_matrix for an example. | |
4719 | ||
4720 | COLOR_ADJUST is a color adjustment added to each pixel of the | |
4721 | outgoing image. */ | |
4722 | ||
4723 | static void | |
d3da34e0 | 4724 | x_detect_edges (struct frame *f, struct image *img, int *matrix, int color_adjust) |
4ef0d4d0 KS |
4725 | { |
4726 | XColor *colors = x_to_xcolors (f, img, 1); | |
4727 | XColor *new, *p; | |
4728 | int x, y, i, sum; | |
4729 | ||
4730 | for (i = sum = 0; i < 9; ++i) | |
1ea40aa2 | 4731 | sum += eabs (matrix[i]); |
4ef0d4d0 KS |
4732 | |
4733 | #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X)) | |
4734 | ||
ddff3151 PE |
4735 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width < img->height) |
4736 | memory_full (SIZE_MAX); | |
23f86fce | 4737 | new = xmalloc (sizeof *new * img->width * img->height); |
4ef0d4d0 KS |
4738 | |
4739 | for (y = 0; y < img->height; ++y) | |
4740 | { | |
4741 | p = COLOR (new, 0, y); | |
4742 | p->red = p->green = p->blue = 0xffff/2; | |
4743 | p = COLOR (new, img->width - 1, y); | |
4744 | p->red = p->green = p->blue = 0xffff/2; | |
4745 | } | |
4746 | ||
4747 | for (x = 1; x < img->width - 1; ++x) | |
4748 | { | |
4749 | p = COLOR (new, x, 0); | |
4750 | p->red = p->green = p->blue = 0xffff/2; | |
4751 | p = COLOR (new, x, img->height - 1); | |
4752 | p->red = p->green = p->blue = 0xffff/2; | |
4753 | } | |
4754 | ||
4755 | for (y = 1; y < img->height - 1; ++y) | |
4756 | { | |
4757 | p = COLOR (new, 1, y); | |
4758 | ||
4759 | for (x = 1; x < img->width - 1; ++x, ++p) | |
4760 | { | |
6ae141d6 | 4761 | int r, g, b, yy, xx; |
4ef0d4d0 KS |
4762 | |
4763 | r = g = b = i = 0; | |
6ae141d6 PE |
4764 | for (yy = y - 1; yy < y + 2; ++yy) |
4765 | for (xx = x - 1; xx < x + 2; ++xx, ++i) | |
4ef0d4d0 KS |
4766 | if (matrix[i]) |
4767 | { | |
6ae141d6 | 4768 | XColor *t = COLOR (colors, xx, yy); |
4ef0d4d0 KS |
4769 | r += matrix[i] * t->red; |
4770 | g += matrix[i] * t->green; | |
4771 | b += matrix[i] * t->blue; | |
4772 | } | |
4773 | ||
4774 | r = (r / sum + color_adjust) & 0xffff; | |
4775 | g = (g / sum + color_adjust) & 0xffff; | |
4776 | b = (b / sum + color_adjust) & 0xffff; | |
4777 | p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b); | |
4778 | } | |
4779 | } | |
4780 | ||
4781 | xfree (colors); | |
4782 | x_from_xcolors (f, img, new); | |
4783 | ||
4784 | #undef COLOR | |
4785 | } | |
4786 | ||
4787 | ||
4788 | /* Perform the pre-defined `emboss' edge-detection on image IMG | |
4789 | on frame F. */ | |
4790 | ||
4791 | static void | |
d3da34e0 | 4792 | x_emboss (struct frame *f, struct image *img) |
4ef0d4d0 KS |
4793 | { |
4794 | x_detect_edges (f, img, emboss_matrix, 0xffff / 2); | |
4795 | } | |
4796 | ||
4797 | ||
4798 | /* Transform image IMG which is used on frame F with a Laplace | |
4799 | edge-detection algorithm. The result is an image that can be used | |
4800 | to draw disabled buttons, for example. */ | |
4801 | ||
4802 | static void | |
d3da34e0 | 4803 | x_laplace (struct frame *f, struct image *img) |
4ef0d4d0 KS |
4804 | { |
4805 | x_detect_edges (f, img, laplace_matrix, 45000); | |
4806 | } | |
4807 | ||
4808 | ||
4809 | /* Perform edge-detection on image IMG on frame F, with specified | |
4810 | transformation matrix MATRIX and color-adjustment COLOR_ADJUST. | |
4811 | ||
4812 | MATRIX must be either | |
4813 | ||
4814 | - a list of at least 9 numbers in row-major form | |
4815 | - a vector of at least 9 numbers | |
4816 | ||
4817 | COLOR_ADJUST nil means use a default; otherwise it must be a | |
4818 | number. */ | |
4819 | ||
4820 | static void | |
d3da34e0 JB |
4821 | x_edge_detection (struct frame *f, struct image *img, Lisp_Object matrix, |
4822 | Lisp_Object color_adjust) | |
4ef0d4d0 KS |
4823 | { |
4824 | int i = 0; | |
4825 | int trans[9]; | |
4826 | ||
4827 | if (CONSP (matrix)) | |
4828 | { | |
4829 | for (i = 0; | |
4830 | i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix)); | |
4831 | ++i, matrix = XCDR (matrix)) | |
4832 | trans[i] = XFLOATINT (XCAR (matrix)); | |
4833 | } | |
4834 | else if (VECTORP (matrix) && ASIZE (matrix) >= 9) | |
4835 | { | |
4836 | for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i) | |
4837 | trans[i] = XFLOATINT (AREF (matrix, i)); | |
4838 | } | |
4839 | ||
4840 | if (NILP (color_adjust)) | |
4841 | color_adjust = make_number (0xffff / 2); | |
4842 | ||
4843 | if (i == 9 && NUMBERP (color_adjust)) | |
77a765fd | 4844 | x_detect_edges (f, img, trans, XFLOATINT (color_adjust)); |
4ef0d4d0 KS |
4845 | } |
4846 | ||
4847 | ||
4848 | /* Transform image IMG on frame F so that it looks disabled. */ | |
4849 | ||
4850 | static void | |
d3da34e0 | 4851 | x_disable_image (struct frame *f, struct image *img) |
4ef0d4d0 | 4852 | { |
aad3612f | 4853 | Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); |
4ef0d4d0 KS |
4854 | #ifdef HAVE_NTGUI |
4855 | int n_planes = dpyinfo->n_planes * dpyinfo->n_cbits; | |
4856 | #else | |
4857 | int n_planes = dpyinfo->n_planes; | |
4858 | #endif /* HAVE_NTGUI */ | |
4859 | ||
4860 | if (n_planes >= 2) | |
4861 | { | |
4862 | /* Color (or grayscale). Convert to gray, and equalize. Just | |
4863 | drawing such images with a stipple can look very odd, so | |
4864 | we're using this method instead. */ | |
4865 | XColor *colors = x_to_xcolors (f, img, 1); | |
4866 | XColor *p, *end; | |
4867 | const int h = 15000; | |
4868 | const int l = 30000; | |
4869 | ||
4870 | for (p = colors, end = colors + img->width * img->height; | |
4871 | p < end; | |
4872 | ++p) | |
4873 | { | |
4874 | int i = COLOR_INTENSITY (p->red, p->green, p->blue); | |
4875 | int i2 = (0xffff - h - l) * i / 0xffff + l; | |
4876 | p->red = p->green = p->blue = i2; | |
4877 | } | |
4878 | ||
4879 | x_from_xcolors (f, img, colors); | |
4880 | } | |
4881 | ||
4882 | /* Draw a cross over the disabled image, if we must or if we | |
4883 | should. */ | |
4884 | if (n_planes < 2 || cross_disabled_images) | |
4885 | { | |
4886 | #ifndef HAVE_NTGUI | |
5afa3a81 | 4887 | #ifndef HAVE_NS /* TODO: NS support, however this not needed for toolbars */ |
edfda783 | 4888 | |
4ef0d4d0 | 4889 | #define MaskForeground(f) WHITE_PIX_DEFAULT (f) |
4ef0d4d0 | 4890 | |
6045c4fd | 4891 | Display *dpy = FRAME_X_DISPLAY (f); |
547c9269 YM |
4892 | GC gc; |
4893 | ||
4894 | image_sync_to_pixmaps (f, img); | |
4895 | gc = XCreateGC (dpy, img->pixmap, 0, NULL); | |
4ef0d4d0 KS |
4896 | XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f)); |
4897 | XDrawLine (dpy, img->pixmap, gc, 0, 0, | |
4898 | img->width - 1, img->height - 1); | |
4899 | XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1, | |
4900 | img->width - 1, 0); | |
4901 | XFreeGC (dpy, gc); | |
4902 | ||
4903 | if (img->mask) | |
4904 | { | |
ae8279db | 4905 | gc = XCreateGC (dpy, img->mask, 0, NULL); |
4ef0d4d0 KS |
4906 | XSetForeground (dpy, gc, MaskForeground (f)); |
4907 | XDrawLine (dpy, img->mask, gc, 0, 0, | |
4908 | img->width - 1, img->height - 1); | |
4909 | XDrawLine (dpy, img->mask, gc, 0, img->height - 1, | |
4910 | img->width - 1, 0); | |
4911 | XFreeGC (dpy, gc); | |
4912 | } | |
edfda783 | 4913 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4914 | #else |
4915 | HDC hdc, bmpdc; | |
4916 | HGDIOBJ prev; | |
4917 | ||
4918 | hdc = get_frame_dc (f); | |
4919 | bmpdc = CreateCompatibleDC (hdc); | |
4920 | release_frame_dc (f, hdc); | |
4921 | ||
4922 | prev = SelectObject (bmpdc, img->pixmap); | |
4923 | ||
4924 | SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f)); | |
4925 | MoveToEx (bmpdc, 0, 0, NULL); | |
4926 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4927 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4928 | LineTo (bmpdc, img->width - 1, 0); | |
4929 | ||
4930 | if (img->mask) | |
4931 | { | |
4932 | SelectObject (bmpdc, img->mask); | |
4933 | SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f)); | |
4934 | MoveToEx (bmpdc, 0, 0, NULL); | |
4935 | LineTo (bmpdc, img->width - 1, img->height - 1); | |
4936 | MoveToEx (bmpdc, 0, img->height - 1, NULL); | |
4937 | LineTo (bmpdc, img->width - 1, 0); | |
4938 | } | |
4939 | SelectObject (bmpdc, prev); | |
4940 | DeleteDC (bmpdc); | |
4941 | #endif /* HAVE_NTGUI */ | |
4942 | } | |
4943 | } | |
4944 | ||
4945 | ||
4946 | /* Build a mask for image IMG which is used on frame F. FILE is the | |
4947 | name of an image file, for error messages. HOW determines how to | |
4948 | determine the background color of IMG. If it is a list '(R G B)', | |
4949 | with R, G, and B being integers >= 0, take that as the color of the | |
4950 | background. Otherwise, determine the background color of IMG | |
578098f3 | 4951 | heuristically. */ |
4ef0d4d0 | 4952 | |
578098f3 | 4953 | static void |
d3da34e0 | 4954 | x_build_heuristic_mask (struct frame *f, struct image *img, Lisp_Object how) |
4ef0d4d0 KS |
4955 | { |
4956 | XImagePtr_or_DC ximg; | |
4957 | #ifndef HAVE_NTGUI | |
4958 | XImagePtr mask_img; | |
4959 | #else | |
4ef0d4d0 KS |
4960 | HGDIOBJ prev; |
4961 | char *mask_img; | |
4962 | int row_width; | |
4963 | #endif /* HAVE_NTGUI */ | |
578098f3 | 4964 | int x, y; |
f462f075 | 4965 | bool use_img_background; |
4ef0d4d0 KS |
4966 | unsigned long bg = 0; |
4967 | ||
4968 | if (img->mask) | |
547c9269 | 4969 | x_clear_image_1 (f, img, CLEAR_IMAGE_MASK); |
4ef0d4d0 KS |
4970 | |
4971 | #ifndef HAVE_NTGUI | |
edfda783 | 4972 | #ifndef HAVE_NS |
4ef0d4d0 | 4973 | /* Create an image and pixmap serving as mask. */ |
f462f075 PE |
4974 | if (! image_create_x_image_and_pixmap (f, img, img->width, img->height, 1, |
4975 | &mask_img, 1)) | |
578098f3 | 4976 | return; |
edfda783 | 4977 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
4978 | #else |
4979 | /* Create the bit array serving as mask. */ | |
4980 | row_width = (img->width + 7) / 8; | |
23f86fce | 4981 | mask_img = xzalloc (row_width * img->height); |
4ef0d4d0 KS |
4982 | #endif /* HAVE_NTGUI */ |
4983 | ||
547c9269 YM |
4984 | /* Get the X image or create a memory device context for IMG. */ |
4985 | ximg = image_get_x_image_or_dc (f, img, 0, &prev); | |
4986 | ||
4ef0d4d0 KS |
4987 | /* Determine the background color of ximg. If HOW is `(R G B)' |
4988 | take that as color. Otherwise, use the image's background color. */ | |
4989 | use_img_background = 1; | |
4990 | ||
4991 | if (CONSP (how)) | |
4992 | { | |
4993 | int rgb[3], i; | |
4994 | ||
4995 | for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i) | |
4996 | { | |
4997 | rgb[i] = XFASTINT (XCAR (how)) & 0xffff; | |
4998 | how = XCDR (how); | |
4999 | } | |
5000 | ||
5001 | if (i == 3 && NILP (how)) | |
5002 | { | |
5003 | char color_name[30]; | |
5004 | sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]); | |
5005 | bg = ( | |
5006 | #ifdef HAVE_NTGUI | |
5007 | 0x00ffffff & /* Filter out palette info. */ | |
5008 | #endif /* HAVE_NTGUI */ | |
5009 | x_alloc_image_color (f, img, build_string (color_name), 0)); | |
5010 | use_img_background = 0; | |
5011 | } | |
5012 | } | |
5013 | ||
5014 | if (use_img_background) | |
6ac3c7bb | 5015 | bg = four_corners_best (ximg, img->corners, img->width, img->height); |
4ef0d4d0 KS |
5016 | |
5017 | /* Set all bits in mask_img to 1 whose color in ximg is different | |
5018 | from the background color bg. */ | |
5019 | #ifndef HAVE_NTGUI | |
5020 | for (y = 0; y < img->height; ++y) | |
5021 | for (x = 0; x < img->width; ++x) | |
edfda783 | 5022 | #ifndef HAVE_NS |
4ef0d4d0 | 5023 | XPutPixel (mask_img, x, y, (XGetPixel (ximg, x, y) != bg |
9ecf6403 | 5024 | ? PIX_MASK_DRAW : PIX_MASK_RETAIN)); |
edfda783 AR |
5025 | #else |
5026 | if (XGetPixel (ximg, x, y) == bg) | |
ed3751c8 | 5027 | ns_set_alpha (ximg, x, y, 0); |
edfda783 AR |
5028 | #endif /* HAVE_NS */ |
5029 | #ifndef HAVE_NS | |
4ef0d4d0 KS |
5030 | /* Fill in the background_transparent field while we have the mask handy. */ |
5031 | image_background_transparent (img, f, mask_img); | |
5032 | ||
547c9269 YM |
5033 | /* Put mask_img into the image. */ |
5034 | image_put_x_image (f, img, mask_img, 1); | |
edfda783 | 5035 | #endif /* !HAVE_NS */ |
4ef0d4d0 KS |
5036 | #else |
5037 | for (y = 0; y < img->height; ++y) | |
5038 | for (x = 0; x < img->width; ++x) | |
5039 | { | |
5040 | COLORREF p = GetPixel (ximg, x, y); | |
5041 | if (p != bg) | |
5042 | mask_img[y * row_width + x / 8] |= 1 << (x % 8); | |
5043 | } | |
5044 | ||
5045 | /* Create the mask image. */ | |
5046 | img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height, | |
5047 | mask_img); | |
5048 | /* Fill in the background_transparent field while we have the mask handy. */ | |
5049 | SelectObject (ximg, img->mask); | |
5050 | image_background_transparent (img, f, ximg); | |
5051 | ||
5052 | /* Was: x_destroy_x_image ((XImagePtr )mask_img); which seems bogus ++kfs */ | |
5053 | xfree (mask_img); | |
5054 | #endif /* HAVE_NTGUI */ | |
5055 | ||
547c9269 | 5056 | image_unget_x_image_or_dc (img, 0, ximg, prev); |
4ef0d4d0 KS |
5057 | } |
5058 | ||
5059 | \f | |
5060 | /*********************************************************************** | |
5061 | PBM (mono, gray, color) | |
5062 | ***********************************************************************/ | |
5063 | ||
578098f3 PE |
5064 | static bool pbm_image_p (Lisp_Object object); |
5065 | static bool pbm_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
5066 | |
5067 | /* The symbol `pbm' identifying images of this type. */ | |
5068 | ||
955cbe7b | 5069 | static Lisp_Object Qpbm; |
4ef0d4d0 KS |
5070 | |
5071 | /* Indices of image specification fields in gs_format, below. */ | |
5072 | ||
5073 | enum pbm_keyword_index | |
5074 | { | |
5075 | PBM_TYPE, | |
5076 | PBM_FILE, | |
5077 | PBM_DATA, | |
5078 | PBM_ASCENT, | |
5079 | PBM_MARGIN, | |
5080 | PBM_RELIEF, | |
5081 | PBM_ALGORITHM, | |
5082 | PBM_HEURISTIC_MASK, | |
5083 | PBM_MASK, | |
5084 | PBM_FOREGROUND, | |
5085 | PBM_BACKGROUND, | |
5086 | PBM_LAST | |
5087 | }; | |
5088 | ||
5089 | /* Vector of image_keyword structures describing the format | |
5090 | of valid user-defined image specifications. */ | |
5091 | ||
91433552 | 5092 | static const struct image_keyword pbm_format[PBM_LAST] = |
4ef0d4d0 KS |
5093 | { |
5094 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5095 | {":file", IMAGE_STRING_VALUE, 0}, | |
5096 | {":data", IMAGE_STRING_VALUE, 0}, | |
5097 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 5098 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
5099 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
5100 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5101 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5102 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5103 | {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0}, | |
5104 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5105 | }; | |
5106 | ||
5107 | /* Structure describing the image type `pbm'. */ | |
5108 | ||
5109 | static struct image_type pbm_type = | |
5110 | { | |
5111 | &Qpbm, | |
5112 | pbm_image_p, | |
5113 | pbm_load, | |
5114 | x_clear_image, | |
bb4d86b4 | 5115 | NULL, |
4ef0d4d0 KS |
5116 | NULL |
5117 | }; | |
5118 | ||
5119 | ||
578098f3 | 5120 | /* Return true if OBJECT is a valid PBM image specification. */ |
4ef0d4d0 | 5121 | |
578098f3 | 5122 | static bool |
d3da34e0 | 5123 | pbm_image_p (Lisp_Object object) |
4ef0d4d0 KS |
5124 | { |
5125 | struct image_keyword fmt[PBM_LAST]; | |
5126 | ||
72af86bd | 5127 | memcpy (fmt, pbm_format, sizeof fmt); |
4ef0d4d0 KS |
5128 | |
5129 | if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)) | |
5130 | return 0; | |
5131 | ||
5132 | /* Must specify either :data or :file. */ | |
5133 | return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1; | |
5134 | } | |
5135 | ||
5136 | ||
0e7690de CB |
5137 | /* Get next char skipping comments in Netpbm header. Returns -1 at |
5138 | end of input. */ | |
5139 | ||
5140 | static int | |
5141 | pbm_next_char (unsigned char **s, unsigned char *end) | |
5142 | { | |
5143 | int c = -1; | |
5144 | ||
5145 | while (*s < end && (c = *(*s)++, c == '#')) | |
5146 | { | |
5147 | /* Skip to the next line break. */ | |
5148 | while (*s < end && (c = *(*s)++, c != '\n' && c != '\r')) | |
5149 | ; | |
5150 | ||
5151 | c = -1; | |
5152 | } | |
5153 | ||
5154 | return c; | |
5155 | } | |
5156 | ||
5157 | ||
4ef0d4d0 KS |
5158 | /* Scan a decimal number from *S and return it. Advance *S while |
5159 | reading the number. END is the end of the string. Value is -1 at | |
5160 | end of input. */ | |
5161 | ||
5162 | static int | |
d3da34e0 | 5163 | pbm_scan_number (unsigned char **s, unsigned char *end) |
4ef0d4d0 KS |
5164 | { |
5165 | int c = 0, val = -1; | |
5166 | ||
0e7690de CB |
5167 | /* Skip white-space. */ |
5168 | while ((c = pbm_next_char (s, end)) != -1 && c_isspace (c)) | |
5169 | ; | |
4ef0d4d0 | 5170 | |
0e7690de CB |
5171 | if (c_isdigit (c)) |
5172 | { | |
5173 | /* Read decimal number. */ | |
5174 | val = c - '0'; | |
5175 | while ((c = pbm_next_char (s, end)) != -1 && c_isdigit (c)) | |
5176 | val = 10 * val + c - '0'; | |
4ef0d4d0 KS |
5177 | } |
5178 | ||
5179 | return val; | |
5180 | } | |
5181 | ||
5182 | ||
4ef0d4d0 KS |
5183 | /* Load PBM image IMG for use on frame F. */ |
5184 | ||
578098f3 | 5185 | static bool |
d3da34e0 | 5186 | pbm_load (struct frame *f, struct image *img) |
4ef0d4d0 | 5187 | { |
578098f3 PE |
5188 | bool raw_p; |
5189 | int x, y; | |
4ef0d4d0 KS |
5190 | int width, height, max_color_idx = 0; |
5191 | XImagePtr ximg; | |
5192 | Lisp_Object file, specified_file; | |
5193 | enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type; | |
4ef0d4d0 KS |
5194 | unsigned char *contents = NULL; |
5195 | unsigned char *end, *p; | |
dd52fcea | 5196 | ptrdiff_t size; |
4ef0d4d0 KS |
5197 | |
5198 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
4ef0d4d0 KS |
5199 | |
5200 | if (STRINGP (specified_file)) | |
5201 | { | |
5202 | file = x_find_image_file (specified_file); | |
5203 | if (!STRINGP (file)) | |
5204 | { | |
5205 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
5206 | return 0; |
5207 | } | |
5208 | ||
42a5b22f | 5209 | contents = slurp_file (SSDATA (file), &size); |
4ef0d4d0 KS |
5210 | if (contents == NULL) |
5211 | { | |
5212 | image_error ("Error reading `%s'", file, Qnil); | |
4ef0d4d0 KS |
5213 | return 0; |
5214 | } | |
5215 | ||
5216 | p = contents; | |
5217 | end = contents + size; | |
5218 | } | |
5219 | else | |
5220 | { | |
5221 | Lisp_Object data; | |
5222 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
5223 | if (!STRINGP (data)) |
5224 | { | |
5225 | image_error ("Invalid image data `%s'", data, Qnil); | |
5226 | return 0; | |
5227 | } | |
4ef0d4d0 KS |
5228 | p = SDATA (data); |
5229 | end = p + SBYTES (data); | |
5230 | } | |
5231 | ||
5232 | /* Check magic number. */ | |
5233 | if (end - p < 2 || *p++ != 'P') | |
5234 | { | |
5235 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5236 | error: | |
5237 | xfree (contents); | |
a9a61c77 | 5238 | img->pixmap = NO_PIXMAP; |
4ef0d4d0 KS |
5239 | return 0; |
5240 | } | |
5241 | ||
5242 | switch (*p++) | |
5243 | { | |
5244 | case '1': | |
5245 | raw_p = 0, type = PBM_MONO; | |
5246 | break; | |
5247 | ||
5248 | case '2': | |
5249 | raw_p = 0, type = PBM_GRAY; | |
5250 | break; | |
5251 | ||
5252 | case '3': | |
5253 | raw_p = 0, type = PBM_COLOR; | |
5254 | break; | |
5255 | ||
5256 | case '4': | |
5257 | raw_p = 1, type = PBM_MONO; | |
5258 | break; | |
5259 | ||
5260 | case '5': | |
5261 | raw_p = 1, type = PBM_GRAY; | |
5262 | break; | |
5263 | ||
5264 | case '6': | |
5265 | raw_p = 1, type = PBM_COLOR; | |
5266 | break; | |
5267 | ||
5268 | default: | |
5269 | image_error ("Not a PBM image: `%s'", img->spec, Qnil); | |
5270 | goto error; | |
5271 | } | |
5272 | ||
5273 | /* Read width, height, maximum color-component. Characters | |
5274 | starting with `#' up to the end of a line are ignored. */ | |
5275 | width = pbm_scan_number (&p, end); | |
5276 | height = pbm_scan_number (&p, end); | |
5277 | ||
5278 | if (type != PBM_MONO) | |
5279 | { | |
5280 | max_color_idx = pbm_scan_number (&p, end); | |
b9c89e11 JR |
5281 | if (max_color_idx > 65535 || max_color_idx < 0) |
5282 | { | |
5283 | image_error ("Unsupported maximum PBM color value", Qnil, Qnil); | |
5284 | goto error; | |
5285 | } | |
4ef0d4d0 KS |
5286 | } |
5287 | ||
b9c89e11 JR |
5288 | if (!check_image_size (f, width, height)) |
5289 | { | |
10ea2b82 | 5290 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
b9c89e11 JR |
5291 | goto error; |
5292 | } | |
4ef0d4d0 | 5293 | |
547c9269 | 5294 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
4ef0d4d0 KS |
5295 | goto error; |
5296 | ||
5297 | /* Initialize the color hash table. */ | |
5298 | init_color_table (); | |
5299 | ||
5300 | if (type == PBM_MONO) | |
5301 | { | |
5302 | int c = 0, g; | |
5303 | struct image_keyword fmt[PBM_LAST]; | |
5304 | unsigned long fg = FRAME_FOREGROUND_PIXEL (f); | |
5305 | unsigned long bg = FRAME_BACKGROUND_PIXEL (f); | |
5306 | ||
5307 | /* Parse the image specification. */ | |
72af86bd | 5308 | memcpy (fmt, pbm_format, sizeof fmt); |
4ef0d4d0 KS |
5309 | parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm); |
5310 | ||
5311 | /* Get foreground and background colors, maybe allocate colors. */ | |
5312 | if (fmt[PBM_FOREGROUND].count | |
5313 | && STRINGP (fmt[PBM_FOREGROUND].value)) | |
5314 | fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg); | |
5315 | if (fmt[PBM_BACKGROUND].count | |
5316 | && STRINGP (fmt[PBM_BACKGROUND].value)) | |
5317 | { | |
5318 | bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg); | |
5319 | img->background = bg; | |
5320 | img->background_valid = 1; | |
5321 | } | |
5322 | ||
5323 | for (y = 0; y < height; ++y) | |
5324 | for (x = 0; x < width; ++x) | |
5325 | { | |
5326 | if (raw_p) | |
5327 | { | |
5328 | if ((x & 7) == 0) | |
c295e710 CY |
5329 | { |
5330 | if (p >= end) | |
5331 | { | |
5332 | x_destroy_x_image (ximg); | |
5333 | x_clear_image (f, img); | |
5334 | image_error ("Invalid image size in image `%s'", | |
5335 | img->spec, Qnil); | |
5336 | goto error; | |
5337 | } | |
5338 | c = *p++; | |
5339 | } | |
4ef0d4d0 KS |
5340 | g = c & 0x80; |
5341 | c <<= 1; | |
5342 | } | |
5343 | else | |
5344 | g = pbm_scan_number (&p, end); | |
5345 | ||
5346 | XPutPixel (ximg, x, y, g ? fg : bg); | |
5347 | } | |
5348 | } | |
5349 | else | |
5350 | { | |
b9c89e11 JR |
5351 | int expected_size = height * width; |
5352 | if (max_color_idx > 255) | |
5353 | expected_size *= 2; | |
5354 | if (type == PBM_COLOR) | |
5355 | expected_size *= 3; | |
5356 | ||
5357 | if (raw_p && p + expected_size > end) | |
39e653ea CY |
5358 | { |
5359 | x_destroy_x_image (ximg); | |
fb0b0862 | 5360 | x_clear_image (f, img); |
39e653ea CY |
5361 | image_error ("Invalid image size in image `%s'", |
5362 | img->spec, Qnil); | |
5363 | goto error; | |
5364 | } | |
5365 | ||
4ef0d4d0 KS |
5366 | for (y = 0; y < height; ++y) |
5367 | for (x = 0; x < width; ++x) | |
5368 | { | |
5369 | int r, g, b; | |
5370 | ||
b9c89e11 JR |
5371 | if (type == PBM_GRAY && raw_p) |
5372 | { | |
5373 | r = g = b = *p++; | |
5374 | if (max_color_idx > 255) | |
5375 | r = g = b = r * 256 + *p++; | |
5376 | } | |
5377 | else if (type == PBM_GRAY) | |
5378 | r = g = b = pbm_scan_number (&p, end); | |
4ef0d4d0 KS |
5379 | else if (raw_p) |
5380 | { | |
5381 | r = *p++; | |
b9c89e11 JR |
5382 | if (max_color_idx > 255) |
5383 | r = r * 256 + *p++; | |
4ef0d4d0 | 5384 | g = *p++; |
b9c89e11 JR |
5385 | if (max_color_idx > 255) |
5386 | g = g * 256 + *p++; | |
4ef0d4d0 | 5387 | b = *p++; |
b9c89e11 JR |
5388 | if (max_color_idx > 255) |
5389 | b = b * 256 + *p++; | |
4ef0d4d0 KS |
5390 | } |
5391 | else | |
5392 | { | |
5393 | r = pbm_scan_number (&p, end); | |
5394 | g = pbm_scan_number (&p, end); | |
5395 | b = pbm_scan_number (&p, end); | |
5396 | } | |
5397 | ||
5398 | if (r < 0 || g < 0 || b < 0) | |
5399 | { | |
5400 | x_destroy_x_image (ximg); | |
5401 | image_error ("Invalid pixel value in image `%s'", | |
5402 | img->spec, Qnil); | |
5403 | goto error; | |
5404 | } | |
5405 | ||
5406 | /* RGB values are now in the range 0..max_color_idx. | |
5407 | Scale this to the range 0..0xffff supported by X. */ | |
5408 | r = (double) r * 65535 / max_color_idx; | |
5409 | g = (double) g * 65535 / max_color_idx; | |
5410 | b = (double) b * 65535 / max_color_idx; | |
5411 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
5412 | } | |
5413 | } | |
5414 | ||
5415 | #ifdef COLOR_TABLE_SUPPORT | |
5416 | /* Store in IMG->colors the colors allocated for the image, and | |
5417 | free the color table. */ | |
5418 | img->colors = colors_in_color_table (&img->ncolors); | |
5419 | free_color_table (); | |
5420 | #endif /* COLOR_TABLE_SUPPORT */ | |
5421 | ||
5422 | img->width = width; | |
5423 | img->height = height; | |
5424 | ||
5425 | /* Maybe fill in the background field while we have ximg handy. */ | |
5426 | ||
5427 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
5428 | /* Casting avoids a GCC warning. */ |
5429 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 5430 | |
547c9269 YM |
5431 | /* Put ximg into the image. */ |
5432 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
5433 | |
5434 | /* X and W32 versions did it here, MAC version above. ++kfs | |
1f026899 | 5435 | img->width = width; |
4ef0d4d0 KS |
5436 | img->height = height; */ |
5437 | ||
4ef0d4d0 KS |
5438 | xfree (contents); |
5439 | return 1; | |
5440 | } | |
5441 | ||
5442 | \f | |
5443 | /*********************************************************************** | |
5444 | PNG | |
5445 | ***********************************************************************/ | |
5446 | ||
9e2a2647 | 5447 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
4ef0d4d0 KS |
5448 | |
5449 | /* Function prototypes. */ | |
5450 | ||
578098f3 PE |
5451 | static bool png_image_p (Lisp_Object object); |
5452 | static bool png_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
5453 | |
5454 | /* The symbol `png' identifying images of this type. */ | |
5455 | ||
955cbe7b | 5456 | static Lisp_Object Qpng; |
4ef0d4d0 KS |
5457 | |
5458 | /* Indices of image specification fields in png_format, below. */ | |
5459 | ||
5460 | enum png_keyword_index | |
5461 | { | |
5462 | PNG_TYPE, | |
5463 | PNG_DATA, | |
5464 | PNG_FILE, | |
5465 | PNG_ASCENT, | |
5466 | PNG_MARGIN, | |
5467 | PNG_RELIEF, | |
5468 | PNG_ALGORITHM, | |
5469 | PNG_HEURISTIC_MASK, | |
5470 | PNG_MASK, | |
5471 | PNG_BACKGROUND, | |
5472 | PNG_LAST | |
5473 | }; | |
5474 | ||
5475 | /* Vector of image_keyword structures describing the format | |
5476 | of valid user-defined image specifications. */ | |
5477 | ||
91433552 | 5478 | static const struct image_keyword png_format[PNG_LAST] = |
4ef0d4d0 KS |
5479 | { |
5480 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
5481 | {":data", IMAGE_STRING_VALUE, 0}, | |
5482 | {":file", IMAGE_STRING_VALUE, 0}, | |
5483 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 5484 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
5485 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
5486 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5487 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5488 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
5489 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
5490 | }; | |
5491 | ||
a9e7a9d5 | 5492 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 5493 | static bool init_png_functions (void); |
bb4d86b4 CY |
5494 | #else |
5495 | #define init_png_functions NULL | |
5496 | #endif | |
5497 | ||
4ef0d4d0 KS |
5498 | /* Structure describing the image type `png'. */ |
5499 | ||
5500 | static struct image_type png_type = | |
5501 | { | |
5502 | &Qpng, | |
5503 | png_image_p, | |
5504 | png_load, | |
5505 | x_clear_image, | |
bb4d86b4 | 5506 | init_png_functions, |
4ef0d4d0 KS |
5507 | NULL |
5508 | }; | |
5509 | ||
578098f3 | 5510 | /* Return true if OBJECT is a valid PNG image specification. */ |
4ef0d4d0 | 5511 | |
578098f3 | 5512 | static bool |
d3da34e0 | 5513 | png_image_p (Lisp_Object object) |
4ef0d4d0 KS |
5514 | { |
5515 | struct image_keyword fmt[PNG_LAST]; | |
72af86bd | 5516 | memcpy (fmt, png_format, sizeof fmt); |
4ef0d4d0 KS |
5517 | |
5518 | if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)) | |
5519 | return 0; | |
5520 | ||
5521 | /* Must specify either the :data or :file keyword. */ | |
5522 | return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1; | |
5523 | } | |
5524 | ||
9e2a2647 | 5525 | #endif /* HAVE_PNG || HAVE_NS */ |
4ef0d4d0 KS |
5526 | |
5527 | ||
f62bc934 PE |
5528 | #if defined HAVE_PNG && !defined HAVE_NS |
5529 | ||
0fda9b75 | 5530 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
5531 | /* PNG library details. */ |
5532 | ||
14beddf4 CY |
5533 | DEF_IMGLIB_FN (png_voidp, png_get_io_ptr, (png_structp)); |
5534 | DEF_IMGLIB_FN (int, png_sig_cmp, (png_bytep, png_size_t, png_size_t)); | |
5535 | DEF_IMGLIB_FN (png_structp, png_create_read_struct, (png_const_charp, png_voidp, | |
5536 | png_error_ptr, png_error_ptr)); | |
5537 | DEF_IMGLIB_FN (png_infop, png_create_info_struct, (png_structp)); | |
5538 | DEF_IMGLIB_FN (void, png_destroy_read_struct, (png_structpp, png_infopp, png_infopp)); | |
5539 | DEF_IMGLIB_FN (void, png_set_read_fn, (png_structp, png_voidp, png_rw_ptr)); | |
5540 | DEF_IMGLIB_FN (void, png_set_sig_bytes, (png_structp, int)); | |
5541 | DEF_IMGLIB_FN (void, png_read_info, (png_structp, png_infop)); | |
5542 | DEF_IMGLIB_FN (png_uint_32, png_get_IHDR, (png_structp, png_infop, | |
850690cc JB |
5543 | png_uint_32 *, png_uint_32 *, |
5544 | int *, int *, int *, int *, int *)); | |
14beddf4 CY |
5545 | DEF_IMGLIB_FN (png_uint_32, png_get_valid, (png_structp, png_infop, png_uint_32)); |
5546 | DEF_IMGLIB_FN (void, png_set_strip_16, (png_structp)); | |
5547 | DEF_IMGLIB_FN (void, png_set_expand, (png_structp)); | |
5548 | DEF_IMGLIB_FN (void, png_set_gray_to_rgb, (png_structp)); | |
5549 | DEF_IMGLIB_FN (void, png_set_background, (png_structp, png_color_16p, | |
850690cc | 5550 | int, int, double)); |
14beddf4 CY |
5551 | DEF_IMGLIB_FN (png_uint_32, png_get_bKGD, (png_structp, png_infop, png_color_16p *)); |
5552 | DEF_IMGLIB_FN (void, png_read_update_info, (png_structp, png_infop)); | |
5553 | DEF_IMGLIB_FN (png_byte, png_get_channels, (png_structp, png_infop)); | |
5554 | DEF_IMGLIB_FN (png_size_t, png_get_rowbytes, (png_structp, png_infop)); | |
5555 | DEF_IMGLIB_FN (void, png_read_image, (png_structp, png_bytepp)); | |
5556 | DEF_IMGLIB_FN (void, png_read_end, (png_structp, png_infop)); | |
5557 | DEF_IMGLIB_FN (void, png_error, (png_structp, png_const_charp)); | |
4ef0d4d0 | 5558 | |
7f9c5df9 | 5559 | #if (PNG_LIBPNG_VER >= 10500) |
7667eeb9 | 5560 | DEF_IMGLIB_FN (void, png_longjmp, (png_structp, int)) PNG_NORETURN; |
14beddf4 | 5561 | DEF_IMGLIB_FN (jmp_buf *, png_set_longjmp_fn, (png_structp, png_longjmp_ptr, size_t)); |
7f9c5df9 | 5562 | #endif /* libpng version >= 1.5 */ |
4ef0d4d0 | 5563 | |
578098f3 | 5564 | static bool |
d07ff9db | 5565 | init_png_functions (void) |
4ef0d4d0 KS |
5566 | { |
5567 | HMODULE library; | |
5568 | ||
d07ff9db | 5569 | if (!(library = w32_delayed_load (Qpng))) |
4ef0d4d0 KS |
5570 | return 0; |
5571 | ||
5572 | LOAD_IMGLIB_FN (library, png_get_io_ptr); | |
285d07e2 | 5573 | LOAD_IMGLIB_FN (library, png_sig_cmp); |
4ef0d4d0 KS |
5574 | LOAD_IMGLIB_FN (library, png_create_read_struct); |
5575 | LOAD_IMGLIB_FN (library, png_create_info_struct); | |
5576 | LOAD_IMGLIB_FN (library, png_destroy_read_struct); | |
5577 | LOAD_IMGLIB_FN (library, png_set_read_fn); | |
4ef0d4d0 KS |
5578 | LOAD_IMGLIB_FN (library, png_set_sig_bytes); |
5579 | LOAD_IMGLIB_FN (library, png_read_info); | |
5580 | LOAD_IMGLIB_FN (library, png_get_IHDR); | |
5581 | LOAD_IMGLIB_FN (library, png_get_valid); | |
5582 | LOAD_IMGLIB_FN (library, png_set_strip_16); | |
5583 | LOAD_IMGLIB_FN (library, png_set_expand); | |
5584 | LOAD_IMGLIB_FN (library, png_set_gray_to_rgb); | |
5585 | LOAD_IMGLIB_FN (library, png_set_background); | |
5586 | LOAD_IMGLIB_FN (library, png_get_bKGD); | |
5587 | LOAD_IMGLIB_FN (library, png_read_update_info); | |
5588 | LOAD_IMGLIB_FN (library, png_get_channels); | |
5589 | LOAD_IMGLIB_FN (library, png_get_rowbytes); | |
5590 | LOAD_IMGLIB_FN (library, png_read_image); | |
5591 | LOAD_IMGLIB_FN (library, png_read_end); | |
5592 | LOAD_IMGLIB_FN (library, png_error); | |
7f9c5df9 CY |
5593 | |
5594 | #if (PNG_LIBPNG_VER >= 10500) | |
5595 | LOAD_IMGLIB_FN (library, png_longjmp); | |
5596 | LOAD_IMGLIB_FN (library, png_set_longjmp_fn); | |
5597 | #endif /* libpng version >= 1.5 */ | |
5598 | ||
4ef0d4d0 KS |
5599 | return 1; |
5600 | } | |
5601 | #else | |
5602 | ||
5603 | #define fn_png_get_io_ptr png_get_io_ptr | |
285d07e2 | 5604 | #define fn_png_sig_cmp png_sig_cmp |
4ef0d4d0 KS |
5605 | #define fn_png_create_read_struct png_create_read_struct |
5606 | #define fn_png_create_info_struct png_create_info_struct | |
5607 | #define fn_png_destroy_read_struct png_destroy_read_struct | |
5608 | #define fn_png_set_read_fn png_set_read_fn | |
4ef0d4d0 KS |
5609 | #define fn_png_set_sig_bytes png_set_sig_bytes |
5610 | #define fn_png_read_info png_read_info | |
5611 | #define fn_png_get_IHDR png_get_IHDR | |
5612 | #define fn_png_get_valid png_get_valid | |
5613 | #define fn_png_set_strip_16 png_set_strip_16 | |
5614 | #define fn_png_set_expand png_set_expand | |
5615 | #define fn_png_set_gray_to_rgb png_set_gray_to_rgb | |
5616 | #define fn_png_set_background png_set_background | |
5617 | #define fn_png_get_bKGD png_get_bKGD | |
5618 | #define fn_png_read_update_info png_read_update_info | |
5619 | #define fn_png_get_channels png_get_channels | |
5620 | #define fn_png_get_rowbytes png_get_rowbytes | |
5621 | #define fn_png_read_image png_read_image | |
5622 | #define fn_png_read_end png_read_end | |
5623 | #define fn_png_error png_error | |
5624 | ||
7f9c5df9 CY |
5625 | #if (PNG_LIBPNG_VER >= 10500) |
5626 | #define fn_png_longjmp png_longjmp | |
5627 | #define fn_png_set_longjmp_fn png_set_longjmp_fn | |
5628 | #endif /* libpng version >= 1.5 */ | |
5629 | ||
0fda9b75 | 5630 | #endif /* WINDOWSNT */ |
4ef0d4d0 | 5631 | |
d1eaf34b PE |
5632 | /* Fast implementations of setjmp and longjmp. Although setjmp and longjmp |
5633 | will do, POSIX _setjmp and _longjmp (if available) are often faster. | |
5634 | Do not use sys_setjmp, as PNG supports only jmp_buf. | |
5635 | It's OK if the longjmp substitute restores the signal mask. */ | |
5636 | #ifdef HAVE__SETJMP | |
5637 | # define FAST_SETJMP(j) _setjmp (j) | |
5638 | # define FAST_LONGJMP _longjmp | |
5639 | #else | |
5640 | # define FAST_SETJMP(j) setjmp (j) | |
5641 | # define FAST_LONGJMP longjmp | |
0328b6de | 5642 | #endif |
7f9c5df9 | 5643 | |
d1eaf34b PE |
5644 | #if PNG_LIBPNG_VER < 10500 |
5645 | #define PNG_LONGJMP(ptr) FAST_LONGJMP ((ptr)->jmpbuf, 1) | |
7f9c5df9 CY |
5646 | #define PNG_JMPBUF(ptr) ((ptr)->jmpbuf) |
5647 | #else | |
df61c790 | 5648 | /* In libpng version 1.5, the jmpbuf member is hidden. (Bug#7908) */ |
d1eaf34b | 5649 | #define PNG_LONGJMP(ptr) fn_png_longjmp (ptr, 1) |
7f9c5df9 | 5650 | #define PNG_JMPBUF(ptr) \ |
d1eaf34b | 5651 | (*fn_png_set_longjmp_fn (ptr, FAST_LONGJMP, sizeof (jmp_buf))) |
7f9c5df9 CY |
5652 | #endif |
5653 | ||
4ef0d4d0 KS |
5654 | /* Error and warning handlers installed when the PNG library |
5655 | is initialized. */ | |
5656 | ||
845ca893 | 5657 | static _Noreturn void |
d3da34e0 | 5658 | my_png_error (png_struct *png_ptr, const char *msg) |
4ef0d4d0 | 5659 | { |
a54e2c05 | 5660 | eassert (png_ptr != NULL); |
5be1c984 EZ |
5661 | /* Avoid compiler warning about deprecated direct access to |
5662 | png_ptr's fields in libpng versions 1.4.x. */ | |
4ef0d4d0 | 5663 | image_error ("PNG error: %s", build_string (msg), Qnil); |
7f9c5df9 | 5664 | PNG_LONGJMP (png_ptr); |
4ef0d4d0 KS |
5665 | } |
5666 | ||
5667 | ||
5668 | static void | |
d3da34e0 | 5669 | my_png_warning (png_struct *png_ptr, const char *msg) |
4ef0d4d0 | 5670 | { |
a54e2c05 | 5671 | eassert (png_ptr != NULL); |
4ef0d4d0 KS |
5672 | image_error ("PNG warning: %s", build_string (msg), Qnil); |
5673 | } | |
5674 | ||
5675 | /* Memory source for PNG decoding. */ | |
5676 | ||
5677 | struct png_memory_storage | |
5678 | { | |
5679 | unsigned char *bytes; /* The data */ | |
3f791afe PE |
5680 | ptrdiff_t len; /* How big is it? */ |
5681 | ptrdiff_t index; /* Where are we? */ | |
4ef0d4d0 KS |
5682 | }; |
5683 | ||
5684 | ||
5685 | /* Function set as reader function when reading PNG image from memory. | |
5686 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5687 | bytes from the input to DATA. */ | |
5688 | ||
5689 | static void | |
d3da34e0 | 5690 | png_read_from_memory (png_structp png_ptr, png_bytep data, png_size_t length) |
4ef0d4d0 | 5691 | { |
7d652d97 | 5692 | struct png_memory_storage *tbr = fn_png_get_io_ptr (png_ptr); |
4ef0d4d0 KS |
5693 | |
5694 | if (length > tbr->len - tbr->index) | |
5695 | fn_png_error (png_ptr, "Read error"); | |
5696 | ||
72af86bd | 5697 | memcpy (data, tbr->bytes + tbr->index, length); |
4ef0d4d0 KS |
5698 | tbr->index = tbr->index + length; |
5699 | } | |
5700 | ||
b0da69a7 | 5701 | |
9a22a004 JB |
5702 | /* Function set as reader function when reading PNG image from a file. |
5703 | PNG_PTR is a pointer to the PNG control structure. Copy LENGTH | |
5704 | bytes from the input to DATA. */ | |
5705 | ||
5706 | static void | |
d3da34e0 | 5707 | png_read_from_file (png_structp png_ptr, png_bytep data, png_size_t length) |
9a22a004 | 5708 | { |
7d652d97 | 5709 | FILE *fp = fn_png_get_io_ptr (png_ptr); |
9a22a004 JB |
5710 | |
5711 | if (fread (data, 1, length, fp) < length) | |
5712 | fn_png_error (png_ptr, "Read error"); | |
5713 | } | |
5714 | ||
5715 | ||
578098f3 | 5716 | /* Load PNG image IMG for use on frame F. Value is true if |
4ef0d4d0 KS |
5717 | successful. */ |
5718 | ||
40bce90b PE |
5719 | struct png_load_context |
5720 | { | |
0328b6de | 5721 | /* These are members so that longjmp doesn't munge local variables. */ |
40bce90b PE |
5722 | png_struct *png_ptr; |
5723 | png_info *info_ptr; | |
5724 | png_info *end_info; | |
5725 | FILE *fp; | |
5726 | png_byte *pixels; | |
5727 | png_byte **rows; | |
5728 | }; | |
5729 | ||
578098f3 | 5730 | static bool |
40bce90b | 5731 | png_load_body (struct frame *f, struct image *img, struct png_load_context *c) |
4ef0d4d0 KS |
5732 | { |
5733 | Lisp_Object file, specified_file; | |
5734 | Lisp_Object specified_data; | |
5adf60bc PE |
5735 | int x, y; |
5736 | ptrdiff_t i; | |
4ef0d4d0 | 5737 | XImagePtr ximg, mask_img = NULL; |
40bce90b | 5738 | png_struct *png_ptr; |
4ef0d4d0 | 5739 | png_info *info_ptr = NULL, *end_info = NULL; |
40bce90b | 5740 | FILE *fp = NULL; |
4ef0d4d0 | 5741 | png_byte sig[8]; |
40bce90b PE |
5742 | png_byte *pixels = NULL; |
5743 | png_byte **rows = NULL; | |
4ef0d4d0 KS |
5744 | png_uint_32 width, height; |
5745 | int bit_depth, color_type, interlace_type; | |
5746 | png_byte channels; | |
5747 | png_uint_32 row_bytes; | |
578098f3 | 5748 | bool transparent_p; |
4ef0d4d0 KS |
5749 | struct png_memory_storage tbr; /* Data to be read */ |
5750 | ||
5751 | /* Find out what file to load. */ | |
5752 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
5753 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
4ef0d4d0 KS |
5754 | |
5755 | if (NILP (specified_data)) | |
5756 | { | |
5757 | file = x_find_image_file (specified_file); | |
5758 | if (!STRINGP (file)) | |
5759 | { | |
5760 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
5761 | return 0; |
5762 | } | |
5763 | ||
5764 | /* Open the image file. */ | |
406af475 | 5765 | fp = emacs_fopen (SSDATA (file), "rb"); |
4ef0d4d0 KS |
5766 | if (!fp) |
5767 | { | |
5768 | image_error ("Cannot open image file `%s'", file, Qnil); | |
4ef0d4d0 KS |
5769 | return 0; |
5770 | } | |
5771 | ||
5772 | /* Check PNG signature. */ | |
5773 | if (fread (sig, 1, sizeof sig, fp) != sizeof sig | |
285d07e2 | 5774 | || fn_png_sig_cmp (sig, 0, sizeof sig)) |
4ef0d4d0 | 5775 | { |
4ef0d4d0 | 5776 | fclose (fp); |
3f5bef16 | 5777 | image_error ("Not a PNG file: `%s'", file, Qnil); |
4ef0d4d0 KS |
5778 | return 0; |
5779 | } | |
5780 | } | |
5781 | else | |
5782 | { | |
7574650a AS |
5783 | if (!STRINGP (specified_data)) |
5784 | { | |
5785 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
5786 | return 0; | |
5787 | } | |
5788 | ||
4ef0d4d0 KS |
5789 | /* Read from memory. */ |
5790 | tbr.bytes = SDATA (specified_data); | |
5791 | tbr.len = SBYTES (specified_data); | |
5792 | tbr.index = 0; | |
5793 | ||
5794 | /* Check PNG signature. */ | |
5795 | if (tbr.len < sizeof sig | |
285d07e2 | 5796 | || fn_png_sig_cmp (tbr.bytes, 0, sizeof sig)) |
4ef0d4d0 KS |
5797 | { |
5798 | image_error ("Not a PNG image: `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
5799 | return 0; |
5800 | } | |
5801 | ||
5802 | /* Need to skip past the signature. */ | |
5803 | tbr.bytes += sizeof (sig); | |
5804 | } | |
5805 | ||
df61c790 AS |
5806 | /* Initialize read and info structs for PNG lib. */ |
5807 | png_ptr = fn_png_create_read_struct (PNG_LIBPNG_VER_STRING, | |
5808 | NULL, my_png_error, | |
5809 | my_png_warning); | |
40bce90b | 5810 | if (png_ptr) |
4ef0d4d0 | 5811 | { |
40bce90b PE |
5812 | info_ptr = fn_png_create_info_struct (png_ptr); |
5813 | end_info = fn_png_create_info_struct (png_ptr); | |
4ef0d4d0 KS |
5814 | } |
5815 | ||
40bce90b PE |
5816 | c->png_ptr = png_ptr; |
5817 | c->info_ptr = info_ptr; | |
5818 | c->end_info = end_info; | |
5819 | c->fp = fp; | |
5820 | c->pixels = pixels; | |
5821 | c->rows = rows; | |
5822 | ||
5823 | if (! (info_ptr && end_info)) | |
4ef0d4d0 | 5824 | { |
40bce90b PE |
5825 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); |
5826 | png_ptr = 0; | |
4ef0d4d0 | 5827 | } |
40bce90b | 5828 | if (! png_ptr) |
4ef0d4d0 | 5829 | { |
4ef0d4d0 | 5830 | if (fp) fclose (fp); |
4ef0d4d0 KS |
5831 | return 0; |
5832 | } | |
5833 | ||
5834 | /* Set error jump-back. We come back here when the PNG library | |
5835 | detects an error. */ | |
d1eaf34b | 5836 | if (FAST_SETJMP (PNG_JMPBUF (png_ptr))) |
4ef0d4d0 KS |
5837 | { |
5838 | error: | |
40bce90b PE |
5839 | if (c->png_ptr) |
5840 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); | |
5841 | xfree (c->pixels); | |
5842 | xfree (c->rows); | |
5843 | if (c->fp) | |
5844 | fclose (c->fp); | |
4ef0d4d0 KS |
5845 | return 0; |
5846 | } | |
5847 | ||
40bce90b PE |
5848 | /* Silence a bogus diagnostic; see GCC bug 54561. */ |
5849 | IF_LINT (fp = c->fp); | |
5850 | ||
4ef0d4d0 KS |
5851 | /* Read image info. */ |
5852 | if (!NILP (specified_data)) | |
7d652d97 | 5853 | fn_png_set_read_fn (png_ptr, &tbr, png_read_from_memory); |
4ef0d4d0 | 5854 | else |
7d652d97 | 5855 | fn_png_set_read_fn (png_ptr, fp, png_read_from_file); |
4ef0d4d0 KS |
5856 | |
5857 | fn_png_set_sig_bytes (png_ptr, sizeof sig); | |
5858 | fn_png_read_info (png_ptr, info_ptr); | |
5859 | fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, | |
5860 | &interlace_type, NULL, NULL); | |
5861 | ||
3f791afe PE |
5862 | if (! (width <= INT_MAX && height <= INT_MAX |
5863 | && check_image_size (f, width, height))) | |
10ea2b82 JR |
5864 | { |
5865 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
5866 | goto error; | |
5867 | } | |
ca4aa935 PE |
5868 | |
5869 | /* Create the X image and pixmap now, so that the work below can be | |
5870 | omitted if the image is too large for X. */ | |
547c9269 | 5871 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
ca4aa935 PE |
5872 | goto error; |
5873 | ||
4ef0d4d0 KS |
5874 | /* If image contains simply transparency data, we prefer to |
5875 | construct a clipping mask. */ | |
5876 | if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) | |
5877 | transparent_p = 1; | |
5878 | else | |
5879 | transparent_p = 0; | |
5880 | ||
5881 | /* This function is easier to write if we only have to handle | |
5882 | one data format: RGB or RGBA with 8 bits per channel. Let's | |
5883 | transform other formats into that format. */ | |
5884 | ||
5885 | /* Strip more than 8 bits per channel. */ | |
5886 | if (bit_depth == 16) | |
5887 | fn_png_set_strip_16 (png_ptr); | |
5888 | ||
5889 | /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel | |
5890 | if available. */ | |
5891 | fn_png_set_expand (png_ptr); | |
5892 | ||
5893 | /* Convert grayscale images to RGB. */ | |
5894 | if (color_type == PNG_COLOR_TYPE_GRAY | |
5895 | || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | |
5896 | fn_png_set_gray_to_rgb (png_ptr); | |
5897 | ||
4ef0d4d0 KS |
5898 | /* Handle alpha channel by combining the image with a background |
5899 | color. Do this only if a real alpha channel is supplied. For | |
5900 | simple transparency, we prefer a clipping mask. */ | |
5901 | if (!transparent_p) | |
5902 | { | |
917ac1a9 | 5903 | /* png_color_16 *image_bg; */ |
4ef0d4d0 KS |
5904 | Lisp_Object specified_bg |
5905 | = image_spec_value (img->spec, QCbackground, NULL); | |
f62bc934 | 5906 | XColor color; |
4ef0d4d0 | 5907 | |
f62bc934 PE |
5908 | /* If the user specified a color, try to use it; if not, use the |
5909 | current frame background, ignoring any default background | |
5910 | color set by the image. */ | |
5911 | if (STRINGP (specified_bg) | |
5912 | ? x_defined_color (f, SSDATA (specified_bg), &color, false) | |
5913 | : (x_query_frame_background_color (f, &color), true)) | |
4ef0d4d0 KS |
5914 | /* The user specified `:background', use that. */ |
5915 | { | |
f62bc934 PE |
5916 | int shift = bit_depth == 16 ? 0 : 8; |
5917 | png_color_16 bg = { 0 }; | |
5918 | bg.red = color.red >> shift; | |
5919 | bg.green = color.green >> shift; | |
5920 | bg.blue = color.blue >> shift; | |
4ef0d4d0 | 5921 | |
f62bc934 | 5922 | fn_png_set_background (png_ptr, &bg, |
4ef0d4d0 KS |
5923 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); |
5924 | } | |
5925 | } | |
5926 | ||
5927 | /* Update info structure. */ | |
5928 | fn_png_read_update_info (png_ptr, info_ptr); | |
5929 | ||
5930 | /* Get number of channels. Valid values are 1 for grayscale images | |
5931 | and images with a palette, 2 for grayscale images with transparency | |
5932 | information (alpha channel), 3 for RGB images, and 4 for RGB | |
5933 | images with alpha channel, i.e. RGBA. If conversions above were | |
5934 | sufficient we should only have 3 or 4 channels here. */ | |
5935 | channels = fn_png_get_channels (png_ptr, info_ptr); | |
a54e2c05 | 5936 | eassert (channels == 3 || channels == 4); |
4ef0d4d0 KS |
5937 | |
5938 | /* Number of bytes needed for one row of the image. */ | |
5939 | row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr); | |
5940 | ||
5941 | /* Allocate memory for the image. */ | |
3f791afe PE |
5942 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *rows < height |
5943 | || min (PTRDIFF_MAX, SIZE_MAX) / sizeof *pixels / height < row_bytes) | |
5944 | memory_full (SIZE_MAX); | |
40bce90b PE |
5945 | c->pixels = pixels = xmalloc (sizeof *pixels * row_bytes * height); |
5946 | c->rows = rows = xmalloc (height * sizeof *rows); | |
4ef0d4d0 KS |
5947 | for (i = 0; i < height; ++i) |
5948 | rows[i] = pixels + i * row_bytes; | |
5949 | ||
5950 | /* Read the entire image. */ | |
5951 | fn_png_read_image (png_ptr, rows); | |
5952 | fn_png_read_end (png_ptr, info_ptr); | |
5953 | if (fp) | |
5954 | { | |
5955 | fclose (fp); | |
40bce90b | 5956 | c->fp = NULL; |
4ef0d4d0 KS |
5957 | } |
5958 | ||
4ef0d4d0 KS |
5959 | /* Create an image and pixmap serving as mask if the PNG image |
5960 | contains an alpha channel. */ | |
5961 | if (channels == 4 | |
5962 | && !transparent_p | |
547c9269 YM |
5963 | && !image_create_x_image_and_pixmap (f, img, width, height, 1, |
5964 | &mask_img, 1)) | |
4ef0d4d0 KS |
5965 | { |
5966 | x_destroy_x_image (ximg); | |
547c9269 | 5967 | x_clear_image_1 (f, img, CLEAR_IMAGE_PIXMAP); |
4ef0d4d0 KS |
5968 | goto error; |
5969 | } | |
5970 | ||
5971 | /* Fill the X image and mask from PNG data. */ | |
5972 | init_color_table (); | |
5973 | ||
5974 | for (y = 0; y < height; ++y) | |
5975 | { | |
5976 | png_byte *p = rows[y]; | |
5977 | ||
5978 | for (x = 0; x < width; ++x) | |
5979 | { | |
13464394 | 5980 | int r, g, b; |
4ef0d4d0 KS |
5981 | |
5982 | r = *p++ << 8; | |
5983 | g = *p++ << 8; | |
5984 | b = *p++ << 8; | |
5985 | XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b)); | |
5986 | /* An alpha channel, aka mask channel, associates variable | |
5987 | transparency with an image. Where other image formats | |
5988 | support binary transparency---fully transparent or fully | |
5989 | opaque---PNG allows up to 254 levels of partial transparency. | |
5990 | The PNG library implements partial transparency by combining | |
5991 | the image with a specified background color. | |
5992 | ||
5993 | I'm not sure how to handle this here nicely: because the | |
5994 | background on which the image is displayed may change, for | |
5995 | real alpha channel support, it would be necessary to create | |
5996 | a new image for each possible background. | |
5997 | ||
5998 | What I'm doing now is that a mask is created if we have | |
5999 | boolean transparency information. Otherwise I'm using | |
6000 | the frame's background color to combine the image with. */ | |
6001 | ||
6002 | if (channels == 4) | |
6003 | { | |
6004 | if (mask_img) | |
9ecf6403 | 6005 | XPutPixel (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN); |
4ef0d4d0 KS |
6006 | ++p; |
6007 | } | |
6008 | } | |
6009 | } | |
6010 | ||
6011 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
6012 | /* Set IMG's background color from the PNG image, unless the user | |
6013 | overrode it. */ | |
6014 | { | |
6015 | png_color_16 *bg; | |
6016 | if (fn_png_get_bKGD (png_ptr, info_ptr, &bg)) | |
6017 | { | |
6018 | img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue); | |
6019 | img->background_valid = 1; | |
6020 | } | |
6021 | } | |
6022 | ||
6023 | #ifdef COLOR_TABLE_SUPPORT | |
6024 | /* Remember colors allocated for this image. */ | |
6025 | img->colors = colors_in_color_table (&img->ncolors); | |
6026 | free_color_table (); | |
6027 | #endif /* COLOR_TABLE_SUPPORT */ | |
6028 | ||
6029 | /* Clean up. */ | |
40bce90b | 6030 | fn_png_destroy_read_struct (&c->png_ptr, &c->info_ptr, &c->end_info); |
4ef0d4d0 KS |
6031 | xfree (rows); |
6032 | xfree (pixels); | |
6033 | ||
6034 | img->width = width; | |
6035 | img->height = height; | |
6036 | ||
2e09fef1 EZ |
6037 | /* Maybe fill in the background field while we have ximg handy. |
6038 | Casting avoids a GCC warning. */ | |
6039 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 6040 | |
547c9269 YM |
6041 | /* Put ximg into the image. */ |
6042 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
6043 | |
6044 | /* Same for the mask. */ | |
6045 | if (mask_img) | |
6046 | { | |
2e09fef1 EZ |
6047 | /* Fill in the background_transparent field while we have the |
6048 | mask handy. Casting avoids a GCC warning. */ | |
6049 | image_background_transparent (img, f, (XImagePtr_or_DC)mask_img); | |
4ef0d4d0 | 6050 | |
547c9269 | 6051 | image_put_x_image (f, img, mask_img, 1); |
4ef0d4d0 KS |
6052 | } |
6053 | ||
4ef0d4d0 KS |
6054 | return 1; |
6055 | } | |
6056 | ||
578098f3 | 6057 | static bool |
40bce90b PE |
6058 | png_load (struct frame *f, struct image *img) |
6059 | { | |
6060 | struct png_load_context c; | |
6061 | return png_load_body (f, img, &c); | |
6062 | } | |
6063 | ||
f62bc934 | 6064 | #elif defined HAVE_NS |
4ef0d4d0 | 6065 | |
578098f3 | 6066 | static bool |
edfda783 AR |
6067 | png_load (struct frame *f, struct image *img) |
6068 | { | |
5e617bc2 JB |
6069 | return ns_load_image (f, img, |
6070 | image_spec_value (img->spec, QCfile, NULL), | |
6071 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 | 6072 | } |
edfda783 | 6073 | |
f62bc934 | 6074 | #endif /* HAVE_NS */ |
4ef0d4d0 KS |
6075 | |
6076 | ||
6077 | \f | |
6078 | /*********************************************************************** | |
6079 | JPEG | |
6080 | ***********************************************************************/ | |
6081 | ||
9e2a2647 | 6082 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
4ef0d4d0 | 6083 | |
578098f3 PE |
6084 | static bool jpeg_image_p (Lisp_Object object); |
6085 | static bool jpeg_load (struct frame *f, struct image *img); | |
4ef0d4d0 KS |
6086 | |
6087 | /* The symbol `jpeg' identifying images of this type. */ | |
6088 | ||
955cbe7b | 6089 | static Lisp_Object Qjpeg; |
4ef0d4d0 KS |
6090 | |
6091 | /* Indices of image specification fields in gs_format, below. */ | |
6092 | ||
6093 | enum jpeg_keyword_index | |
6094 | { | |
6095 | JPEG_TYPE, | |
6096 | JPEG_DATA, | |
6097 | JPEG_FILE, | |
6098 | JPEG_ASCENT, | |
6099 | JPEG_MARGIN, | |
6100 | JPEG_RELIEF, | |
6101 | JPEG_ALGORITHM, | |
6102 | JPEG_HEURISTIC_MASK, | |
6103 | JPEG_MASK, | |
6104 | JPEG_BACKGROUND, | |
6105 | JPEG_LAST | |
6106 | }; | |
6107 | ||
6108 | /* Vector of image_keyword structures describing the format | |
6109 | of valid user-defined image specifications. */ | |
6110 | ||
91433552 | 6111 | static const struct image_keyword jpeg_format[JPEG_LAST] = |
4ef0d4d0 KS |
6112 | { |
6113 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
6114 | {":data", IMAGE_STRING_VALUE, 0}, | |
6115 | {":file", IMAGE_STRING_VALUE, 0}, | |
6116 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 6117 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
6118 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
6119 | {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6120 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6121 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
6122 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
6123 | }; | |
6124 | ||
a9e7a9d5 | 6125 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 6126 | static bool init_jpeg_functions (void); |
bb4d86b4 CY |
6127 | #else |
6128 | #define init_jpeg_functions NULL | |
6129 | #endif | |
6130 | ||
4ef0d4d0 KS |
6131 | /* Structure describing the image type `jpeg'. */ |
6132 | ||
6133 | static struct image_type jpeg_type = | |
6134 | { | |
6135 | &Qjpeg, | |
6136 | jpeg_image_p, | |
6137 | jpeg_load, | |
6138 | x_clear_image, | |
bb4d86b4 | 6139 | init_jpeg_functions, |
4ef0d4d0 KS |
6140 | NULL |
6141 | }; | |
6142 | ||
578098f3 | 6143 | /* Return true if OBJECT is a valid JPEG image specification. */ |
4ef0d4d0 | 6144 | |
578098f3 | 6145 | static bool |
d3da34e0 | 6146 | jpeg_image_p (Lisp_Object object) |
4ef0d4d0 KS |
6147 | { |
6148 | struct image_keyword fmt[JPEG_LAST]; | |
6149 | ||
72af86bd | 6150 | memcpy (fmt, jpeg_format, sizeof fmt); |
4ef0d4d0 KS |
6151 | |
6152 | if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)) | |
6153 | return 0; | |
6154 | ||
6155 | /* Must specify either the :data or :file keyword. */ | |
6156 | return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1; | |
6157 | } | |
6158 | ||
9e2a2647 | 6159 | #endif /* HAVE_JPEG || HAVE_NS */ |
4ef0d4d0 KS |
6160 | |
6161 | #ifdef HAVE_JPEG | |
6162 | ||
6163 | /* Work around a warning about HAVE_STDLIB_H being redefined in | |
6164 | jconfig.h. */ | |
6165 | #ifdef HAVE_STDLIB_H | |
4ef0d4d0 KS |
6166 | #undef HAVE_STDLIB_H |
6167 | #endif /* HAVE_STLIB_H */ | |
6168 | ||
66354420 | 6169 | #if defined (HAVE_NTGUI) && !defined (__WIN32__) |
28893db2 JB |
6170 | /* In older releases of the jpeg library, jpeglib.h will define boolean |
6171 | differently depending on __WIN32__, so make sure it is defined. */ | |
66354420 JR |
6172 | #define __WIN32__ 1 |
6173 | #endif | |
6174 | ||
93aa5c81 DC |
6175 | /* rpcndr.h (via windows.h) and jpeglib.h both define boolean types. |
6176 | Some versions of jpeglib try to detect whether rpcndr.h is loaded, | |
6177 | using the Windows boolean type instead of the jpeglib boolean type | |
6178 | if so. Cygwin jpeglib, however, doesn't try to detect whether its | |
6179 | headers are included along with windows.h, so under Cygwin, jpeglib | |
6180 | attempts to define a conflicting boolean type. Worse, forcing | |
6181 | Cygwin jpeglib headers to use the Windows boolean type doesn't work | |
6182 | because it created an ABI incompatibility between the | |
6183 | already-compiled jpeg library and the header interface definition. | |
6184 | ||
6185 | The best we can do is to define jpeglib's boolean type to a | |
6186 | different name. This name, jpeg_boolean, remains in effect through | |
6187 | the rest of image.c. | |
6188 | */ | |
a9e7a9d5 | 6189 | #if defined CYGWIN && defined HAVE_NTGUI |
0fda9b75 | 6190 | #define boolean jpeg_boolean |
2ae3a8a7 | 6191 | #endif |
4ef0d4d0 KS |
6192 | #include <jpeglib.h> |
6193 | #include <jerror.h> | |
4ef0d4d0 | 6194 | |
0fda9b75 | 6195 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
6196 | |
6197 | /* JPEG library details. */ | |
14beddf4 CY |
6198 | DEF_IMGLIB_FN (void, jpeg_CreateDecompress, (j_decompress_ptr, int, size_t)); |
6199 | DEF_IMGLIB_FN (boolean, jpeg_start_decompress, (j_decompress_ptr)); | |
6200 | DEF_IMGLIB_FN (boolean, jpeg_finish_decompress, (j_decompress_ptr)); | |
6201 | DEF_IMGLIB_FN (void, jpeg_destroy_decompress, (j_decompress_ptr)); | |
6202 | DEF_IMGLIB_FN (int, jpeg_read_header, (j_decompress_ptr, boolean)); | |
6203 | DEF_IMGLIB_FN (JDIMENSION, jpeg_read_scanlines, (j_decompress_ptr, JSAMPARRAY, JDIMENSION)); | |
6204 | DEF_IMGLIB_FN (struct jpeg_error_mgr *, jpeg_std_error, (struct jpeg_error_mgr *)); | |
6205 | DEF_IMGLIB_FN (boolean, jpeg_resync_to_restart, (j_decompress_ptr, int)); | |
4ef0d4d0 | 6206 | |
578098f3 | 6207 | static bool |
d07ff9db | 6208 | init_jpeg_functions (void) |
4ef0d4d0 KS |
6209 | { |
6210 | HMODULE library; | |
6211 | ||
d07ff9db | 6212 | if (!(library = w32_delayed_load (Qjpeg))) |
4ef0d4d0 KS |
6213 | return 0; |
6214 | ||
6215 | LOAD_IMGLIB_FN (library, jpeg_finish_decompress); | |
6216 | LOAD_IMGLIB_FN (library, jpeg_read_scanlines); | |
6217 | LOAD_IMGLIB_FN (library, jpeg_start_decompress); | |
6218 | LOAD_IMGLIB_FN (library, jpeg_read_header); | |
4ef0d4d0 KS |
6219 | LOAD_IMGLIB_FN (library, jpeg_CreateDecompress); |
6220 | LOAD_IMGLIB_FN (library, jpeg_destroy_decompress); | |
6221 | LOAD_IMGLIB_FN (library, jpeg_std_error); | |
6222 | LOAD_IMGLIB_FN (library, jpeg_resync_to_restart); | |
6223 | return 1; | |
6224 | } | |
6225 | ||
6226 | /* Wrapper since we can't directly assign the function pointer | |
6227 | to another function pointer that was declared more completely easily. */ | |
6228 | static boolean | |
7c3320d8 | 6229 | jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo, int desired) |
4ef0d4d0 KS |
6230 | { |
6231 | return fn_jpeg_resync_to_restart (cinfo, desired); | |
6232 | } | |
6233 | ||
6234 | #else | |
6235 | ||
5e617bc2 | 6236 | #define fn_jpeg_CreateDecompress(a,b,c) jpeg_create_decompress (a) |
4ef0d4d0 KS |
6237 | #define fn_jpeg_start_decompress jpeg_start_decompress |
6238 | #define fn_jpeg_finish_decompress jpeg_finish_decompress | |
6239 | #define fn_jpeg_destroy_decompress jpeg_destroy_decompress | |
6240 | #define fn_jpeg_read_header jpeg_read_header | |
6241 | #define fn_jpeg_read_scanlines jpeg_read_scanlines | |
4ef0d4d0 KS |
6242 | #define fn_jpeg_std_error jpeg_std_error |
6243 | #define jpeg_resync_to_restart_wrapper jpeg_resync_to_restart | |
6244 | ||
0fda9b75 | 6245 | #endif /* WINDOWSNT */ |
4ef0d4d0 KS |
6246 | |
6247 | struct my_jpeg_error_mgr | |
6248 | { | |
6249 | struct jpeg_error_mgr pub; | |
0328b6de | 6250 | sys_jmp_buf setjmp_buffer; |
40bce90b | 6251 | |
0328b6de | 6252 | /* The remaining members are so that longjmp doesn't munge local |
40bce90b PE |
6253 | variables. */ |
6254 | struct jpeg_decompress_struct cinfo; | |
6255 | enum | |
6256 | { | |
6257 | MY_JPEG_ERROR_EXIT, | |
6258 | MY_JPEG_INVALID_IMAGE_SIZE, | |
6259 | MY_JPEG_CANNOT_CREATE_X | |
6260 | } failure_code; | |
4ef0d4d0 KS |
6261 | }; |
6262 | ||
6263 | ||
845ca893 | 6264 | static _Noreturn void |
d3da34e0 | 6265 | my_error_exit (j_common_ptr cinfo) |
4ef0d4d0 KS |
6266 | { |
6267 | struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err; | |
40bce90b | 6268 | mgr->failure_code = MY_JPEG_ERROR_EXIT; |
0328b6de | 6269 | sys_longjmp (mgr->setjmp_buffer, 1); |
4ef0d4d0 KS |
6270 | } |
6271 | ||
6272 | ||
6273 | /* Init source method for JPEG data source manager. Called by | |
6274 | jpeg_read_header() before any data is actually read. See | |
6275 | libjpeg.doc from the JPEG lib distribution. */ | |
6276 | ||
6277 | static void | |
d3da34e0 | 6278 | our_common_init_source (j_decompress_ptr cinfo) |
b25a72ab JB |
6279 | { |
6280 | } | |
6281 | ||
6282 | ||
6283 | /* Method to terminate data source. Called by | |
6284 | jpeg_finish_decompress() after all data has been processed. */ | |
6285 | ||
6286 | static void | |
d3da34e0 | 6287 | our_common_term_source (j_decompress_ptr cinfo) |
4ef0d4d0 KS |
6288 | { |
6289 | } | |
6290 | ||
6291 | ||
6292 | /* Fill input buffer method for JPEG data source manager. Called | |
6293 | whenever more data is needed. We read the whole image in one step, | |
6294 | so this only adds a fake end of input marker at the end. */ | |
6295 | ||
5a1539f3 | 6296 | static JOCTET our_memory_buffer[2]; |
aca946f3 | 6297 | |
4ef0d4d0 | 6298 | static boolean |
d3da34e0 | 6299 | our_memory_fill_input_buffer (j_decompress_ptr cinfo) |
4ef0d4d0 KS |
6300 | { |
6301 | /* Insert a fake EOI marker. */ | |
6302 | struct jpeg_source_mgr *src = cinfo->src; | |
4ef0d4d0 | 6303 | |
5a1539f3 MB |
6304 | our_memory_buffer[0] = (JOCTET) 0xFF; |
6305 | our_memory_buffer[1] = (JOCTET) JPEG_EOI; | |
4ef0d4d0 | 6306 | |
5a1539f3 | 6307 | src->next_input_byte = our_memory_buffer; |
4ef0d4d0 | 6308 | src->bytes_in_buffer = 2; |
e0e30823 | 6309 | return 1; |
4ef0d4d0 KS |
6310 | } |
6311 | ||
6312 | ||
6313 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6314 | is the JPEG data source manager. */ | |
6315 | ||
6316 | static void | |
d3da34e0 | 6317 | our_memory_skip_input_data (j_decompress_ptr cinfo, long int num_bytes) |
4ef0d4d0 | 6318 | { |
7d652d97 | 6319 | struct jpeg_source_mgr *src = cinfo->src; |
4ef0d4d0 KS |
6320 | |
6321 | if (src) | |
6322 | { | |
6323 | if (num_bytes > src->bytes_in_buffer) | |
6324 | ERREXIT (cinfo, JERR_INPUT_EOF); | |
6325 | ||
6326 | src->bytes_in_buffer -= num_bytes; | |
6327 | src->next_input_byte += num_bytes; | |
6328 | } | |
6329 | } | |
6330 | ||
6331 | ||
4ef0d4d0 KS |
6332 | /* Set up the JPEG lib for reading an image from DATA which contains |
6333 | LEN bytes. CINFO is the decompression info structure created for | |
6334 | reading the image. */ | |
6335 | ||
6336 | static void | |
92547ff9 | 6337 | jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, ptrdiff_t len) |
4ef0d4d0 | 6338 | { |
7d652d97 | 6339 | struct jpeg_source_mgr *src = cinfo->src; |
4ef0d4d0 | 6340 | |
7d652d97 | 6341 | if (! src) |
4ef0d4d0 KS |
6342 | { |
6343 | /* First time for this JPEG object? */ | |
7d652d97 PE |
6344 | src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, |
6345 | JPOOL_PERMANENT, sizeof *src); | |
6346 | cinfo->src = src; | |
4ef0d4d0 KS |
6347 | src->next_input_byte = data; |
6348 | } | |
6349 | ||
b25a72ab JB |
6350 | src->init_source = our_common_init_source; |
6351 | src->fill_input_buffer = our_memory_fill_input_buffer; | |
6352 | src->skip_input_data = our_memory_skip_input_data; | |
4ef0d4d0 | 6353 | src->resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ |
b25a72ab | 6354 | src->term_source = our_common_term_source; |
4ef0d4d0 KS |
6355 | src->bytes_in_buffer = len; |
6356 | src->next_input_byte = data; | |
6357 | } | |
6358 | ||
6359 | ||
b25a72ab JB |
6360 | struct jpeg_stdio_mgr |
6361 | { | |
6362 | struct jpeg_source_mgr mgr; | |
6363 | boolean finished; | |
6364 | FILE *file; | |
6365 | JOCTET *buffer; | |
6366 | }; | |
6367 | ||
6368 | ||
6369 | /* Size of buffer to read JPEG from file. | |
6370 | Not too big, as we want to use alloc_small. */ | |
6371 | #define JPEG_STDIO_BUFFER_SIZE 8192 | |
6372 | ||
6373 | ||
6374 | /* Fill input buffer method for JPEG data source manager. Called | |
6375 | whenever more data is needed. The data is read from a FILE *. */ | |
6376 | ||
6377 | static boolean | |
d3da34e0 | 6378 | our_stdio_fill_input_buffer (j_decompress_ptr cinfo) |
b25a72ab JB |
6379 | { |
6380 | struct jpeg_stdio_mgr *src; | |
6381 | ||
6382 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6383 | if (!src->finished) | |
6384 | { | |
3f791afe | 6385 | ptrdiff_t bytes; |
b25a72ab JB |
6386 | |
6387 | bytes = fread (src->buffer, 1, JPEG_STDIO_BUFFER_SIZE, src->file); | |
6388 | if (bytes > 0) | |
6389 | src->mgr.bytes_in_buffer = bytes; | |
6390 | else | |
6391 | { | |
6392 | WARNMS (cinfo, JWRN_JPEG_EOF); | |
6393 | src->finished = 1; | |
6394 | src->buffer[0] = (JOCTET) 0xFF; | |
6395 | src->buffer[1] = (JOCTET) JPEG_EOI; | |
6396 | src->mgr.bytes_in_buffer = 2; | |
6397 | } | |
6398 | src->mgr.next_input_byte = src->buffer; | |
6399 | } | |
6400 | ||
6401 | return 1; | |
6402 | } | |
6403 | ||
6404 | ||
6405 | /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src | |
6406 | is the JPEG data source manager. */ | |
6407 | ||
6408 | static void | |
d3da34e0 | 6409 | our_stdio_skip_input_data (j_decompress_ptr cinfo, long int num_bytes) |
b25a72ab JB |
6410 | { |
6411 | struct jpeg_stdio_mgr *src; | |
6412 | src = (struct jpeg_stdio_mgr *) cinfo->src; | |
6413 | ||
6414 | while (num_bytes > 0 && !src->finished) | |
6415 | { | |
6416 | if (num_bytes <= src->mgr.bytes_in_buffer) | |
6417 | { | |
6418 | src->mgr.bytes_in_buffer -= num_bytes; | |
6419 | src->mgr.next_input_byte += num_bytes; | |
6420 | break; | |
6421 | } | |
6422 | else | |
6423 | { | |
6424 | num_bytes -= src->mgr.bytes_in_buffer; | |
6425 | src->mgr.bytes_in_buffer = 0; | |
6426 | src->mgr.next_input_byte = NULL; | |
6427 | ||
6428 | our_stdio_fill_input_buffer (cinfo); | |
6429 | } | |
6430 | } | |
6431 | } | |
6432 | ||
6433 | ||
6434 | /* Set up the JPEG lib for reading an image from a FILE *. | |
6435 | CINFO is the decompression info structure created for | |
6436 | reading the image. */ | |
6437 | ||
6438 | static void | |
d3da34e0 | 6439 | jpeg_file_src (j_decompress_ptr cinfo, FILE *fp) |
b25a72ab | 6440 | { |
7d652d97 | 6441 | struct jpeg_stdio_mgr *src = (struct jpeg_stdio_mgr *) cinfo->src; |
b25a72ab | 6442 | |
7d652d97 | 6443 | if (! src) |
b25a72ab JB |
6444 | { |
6445 | /* First time for this JPEG object? */ | |
7d652d97 PE |
6446 | src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, |
6447 | JPOOL_PERMANENT, sizeof *src); | |
6448 | cinfo->src = (struct jpeg_source_mgr *) src; | |
6449 | src->buffer = cinfo->mem->alloc_small ((j_common_ptr) cinfo, | |
6450 | JPOOL_PERMANENT, | |
6451 | JPEG_STDIO_BUFFER_SIZE); | |
b25a72ab JB |
6452 | } |
6453 | ||
6454 | src->file = fp; | |
6455 | src->finished = 0; | |
6456 | src->mgr.init_source = our_common_init_source; | |
6457 | src->mgr.fill_input_buffer = our_stdio_fill_input_buffer; | |
6458 | src->mgr.skip_input_data = our_stdio_skip_input_data; | |
6459 | src->mgr.resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */ | |
6460 | src->mgr.term_source = our_common_term_source; | |
6461 | src->mgr.bytes_in_buffer = 0; | |
6462 | src->mgr.next_input_byte = NULL; | |
6463 | } | |
6464 | ||
4ef0d4d0 KS |
6465 | /* Load image IMG for use on frame F. Patterned after example.c |
6466 | from the JPEG lib. */ | |
6467 | ||
578098f3 | 6468 | static bool |
40bce90b PE |
6469 | jpeg_load_body (struct frame *f, struct image *img, |
6470 | struct my_jpeg_error_mgr *mgr) | |
4ef0d4d0 | 6471 | { |
4ef0d4d0 KS |
6472 | Lisp_Object file, specified_file; |
6473 | Lisp_Object specified_data; | |
34e856d5 PE |
6474 | /* The 'volatile' silences a bogus diagnostic; see GCC bug 54561. */ |
6475 | FILE * IF_LINT (volatile) fp = NULL; | |
4ef0d4d0 KS |
6476 | JSAMPARRAY buffer; |
6477 | int row_stride, x, y; | |
6478 | XImagePtr ximg = NULL; | |
4ef0d4d0 KS |
6479 | unsigned long *colors; |
6480 | int width, height; | |
4ef0d4d0 KS |
6481 | |
6482 | /* Open the JPEG file. */ | |
6483 | specified_file = image_spec_value (img->spec, QCfile, NULL); | |
6484 | specified_data = image_spec_value (img->spec, QCdata, NULL); | |
4ef0d4d0 KS |
6485 | |
6486 | if (NILP (specified_data)) | |
6487 | { | |
6488 | file = x_find_image_file (specified_file); | |
6489 | if (!STRINGP (file)) | |
6490 | { | |
6491 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
6492 | return 0; |
6493 | } | |
6494 | ||
406af475 | 6495 | fp = emacs_fopen (SSDATA (file), "rb"); |
4ef0d4d0 KS |
6496 | if (fp == NULL) |
6497 | { | |
6498 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
6499 | return 0; |
6500 | } | |
6501 | } | |
7574650a AS |
6502 | else if (!STRINGP (specified_data)) |
6503 | { | |
6504 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
6505 | return 0; | |
6506 | } | |
4ef0d4d0 KS |
6507 | |
6508 | /* Customize libjpeg's error handling to call my_error_exit when an | |
df61c790 | 6509 | error is detected. This function will perform a longjmp. */ |
40bce90b PE |
6510 | mgr->cinfo.err = fn_jpeg_std_error (&mgr->pub); |
6511 | mgr->pub.error_exit = my_error_exit; | |
0328b6de | 6512 | if (sys_setjmp (mgr->setjmp_buffer)) |
4ef0d4d0 | 6513 | { |
40bce90b | 6514 | switch (mgr->failure_code) |
4ef0d4d0 | 6515 | { |
40bce90b PE |
6516 | case MY_JPEG_ERROR_EXIT: |
6517 | { | |
6518 | char buf[JMSG_LENGTH_MAX]; | |
6519 | mgr->cinfo.err->format_message ((j_common_ptr) &mgr->cinfo, buf); | |
6520 | image_error ("Error reading JPEG image `%s': %s", img->spec, | |
6521 | build_string (buf)); | |
6522 | break; | |
6523 | } | |
6524 | ||
6525 | case MY_JPEG_INVALID_IMAGE_SIZE: | |
6526 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
6527 | break; | |
6528 | ||
6529 | case MY_JPEG_CANNOT_CREATE_X: | |
6530 | break; | |
4ef0d4d0 KS |
6531 | } |
6532 | ||
6533 | /* Close the input file and destroy the JPEG object. */ | |
6534 | if (fp) | |
40bce90b PE |
6535 | fclose (fp); |
6536 | fn_jpeg_destroy_decompress (&mgr->cinfo); | |
4ef0d4d0 KS |
6537 | |
6538 | /* If we already have an XImage, free that. */ | |
6539 | x_destroy_x_image (ximg); | |
6540 | ||
6541 | /* Free pixmap and colors. */ | |
6542 | x_clear_image (f, img); | |
4ef0d4d0 KS |
6543 | return 0; |
6544 | } | |
6545 | ||
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 | } | |
9ab69568 EZ |
6971 | #ifdef WINDOWSNT |
6972 | file = ansi_encode_filename (file); | |
6973 | #endif | |
4ef0d4d0 | 6974 | |
df61c790 | 6975 | /* Try to open the image file. */ |
b19beacc | 6976 | tiff = fn_TIFFOpen (SSDATA (file), "r"); |
4ef0d4d0 KS |
6977 | if (tiff == NULL) |
6978 | { | |
6979 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
6980 | return 0; |
6981 | } | |
6982 | } | |
6983 | else | |
6984 | { | |
7574650a AS |
6985 | if (!STRINGP (specified_data)) |
6986 | { | |
6987 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
6988 | return 0; | |
6989 | } | |
6990 | ||
4ef0d4d0 KS |
6991 | /* Memory source! */ |
6992 | memsrc.bytes = SDATA (specified_data); | |
6993 | memsrc.len = SBYTES (specified_data); | |
6994 | memsrc.index = 0; | |
6995 | ||
3082f617 | 6996 | tiff = fn_TIFFClientOpen ("memory_source", "r", (thandle_t)&memsrc, |
3f791afe PE |
6997 | tiff_read_from_memory, |
6998 | tiff_write_from_memory, | |
df61c790 AS |
6999 | tiff_seek_in_memory, |
7000 | tiff_close_memory, | |
7001 | tiff_size_of_memory, | |
7002 | tiff_mmap_memory, | |
7003 | tiff_unmap_memory); | |
4ef0d4d0 KS |
7004 | |
7005 | if (!tiff) | |
7006 | { | |
7007 | image_error ("Cannot open memory source for `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
7008 | return 0; |
7009 | } | |
7010 | } | |
7011 | ||
58279503 JR |
7012 | image = image_spec_value (img->spec, QCindex, NULL); |
7013 | if (INTEGERP (image)) | |
7014 | { | |
13464394 PE |
7015 | EMACS_INT ino = XFASTINT (image); |
7016 | if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t) | |
7017 | && fn_TIFFSetDirectory (tiff, ino))) | |
58279503 JR |
7018 | { |
7019 | image_error ("Invalid image number `%s' in image `%s'", | |
7020 | image, img->spec); | |
7021 | fn_TIFFClose (tiff); | |
58279503 JR |
7022 | return 0; |
7023 | } | |
7024 | } | |
7025 | ||
4ef0d4d0 KS |
7026 | /* Get width and height of the image, and allocate a raster buffer |
7027 | of width x height 32-bit values. */ | |
7028 | fn_TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width); | |
7029 | fn_TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height); | |
f1f25b99 CY |
7030 | |
7031 | if (!check_image_size (f, width, height)) | |
7032 | { | |
10ea2b82 | 7033 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
58279503 | 7034 | fn_TIFFClose (tiff); |
f1f25b99 CY |
7035 | return 0; |
7036 | } | |
7037 | ||
ca4aa935 PE |
7038 | /* Create the X image and pixmap. */ |
7039 | if (! (height <= min (PTRDIFF_MAX, SIZE_MAX) / sizeof *buf / width | |
547c9269 YM |
7040 | && image_create_x_image_and_pixmap (f, img, width, height, 0, |
7041 | &ximg, 0))) | |
ca4aa935 PE |
7042 | { |
7043 | fn_TIFFClose (tiff); | |
7044 | return 0; | |
7045 | } | |
7046 | ||
23f86fce | 7047 | buf = xmalloc (sizeof *buf * width * height); |
4ef0d4d0 KS |
7048 | |
7049 | rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0); | |
58279503 JR |
7050 | |
7051 | /* Count the number of images in the file. */ | |
03d0a109 PE |
7052 | for (count = 1; fn_TIFFSetDirectory (tiff, count); count++) |
7053 | continue; | |
58279503 JR |
7054 | |
7055 | if (count > 1) | |
b50691aa CY |
7056 | img->lisp_data = Fcons (Qcount, |
7057 | Fcons (make_number (count), | |
7058 | img->lisp_data)); | |
58279503 | 7059 | |
4ef0d4d0 KS |
7060 | fn_TIFFClose (tiff); |
7061 | if (!rc) | |
7062 | { | |
7063 | image_error ("Error reading TIFF image `%s'", img->spec, Qnil); | |
7064 | xfree (buf); | |
4ef0d4d0 KS |
7065 | return 0; |
7066 | } | |
7067 | ||
4ef0d4d0 KS |
7068 | /* Initialize the color table. */ |
7069 | init_color_table (); | |
7070 | ||
7071 | /* Process the pixel raster. Origin is in the lower-left corner. */ | |
7072 | for (y = 0; y < height; ++y) | |
7073 | { | |
7074 | uint32 *row = buf + y * width; | |
7075 | ||
7076 | for (x = 0; x < width; ++x) | |
7077 | { | |
7078 | uint32 abgr = row[x]; | |
7079 | int r = TIFFGetR (abgr) << 8; | |
7080 | int g = TIFFGetG (abgr) << 8; | |
7081 | int b = TIFFGetB (abgr) << 8; | |
7082 | XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b)); | |
7083 | } | |
7084 | } | |
7085 | ||
7086 | #ifdef COLOR_TABLE_SUPPORT | |
7087 | /* Remember the colors allocated for the image. Free the color table. */ | |
7088 | img->colors = colors_in_color_table (&img->ncolors); | |
7089 | free_color_table (); | |
7090 | #endif /* COLOR_TABLE_SUPPORT */ | |
7091 | ||
7092 | img->width = width; | |
7093 | img->height = height; | |
7094 | ||
7095 | /* Maybe fill in the background field while we have ximg handy. */ | |
7096 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7097 | /* Casting avoids a GCC warning on W32. */ |
7098 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 7099 | |
547c9269 YM |
7100 | /* Put ximg into the image. */ |
7101 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 KS |
7102 | xfree (buf); |
7103 | ||
4ef0d4d0 KS |
7104 | return 1; |
7105 | } | |
7106 | ||
7107 | #else /* HAVE_TIFF */ | |
7108 | ||
edfda783 | 7109 | #ifdef HAVE_NS |
578098f3 | 7110 | static bool |
edfda783 AR |
7111 | tiff_load (struct frame *f, struct image *img) |
7112 | { | |
ed3751c8 JB |
7113 | return ns_load_image (f, img, |
7114 | image_spec_value (img->spec, QCfile, NULL), | |
7115 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
7116 | } |
7117 | #endif /* HAVE_NS */ | |
7118 | ||
4ef0d4d0 KS |
7119 | #endif /* !HAVE_TIFF */ |
7120 | ||
7121 | ||
7122 | \f | |
7123 | /*********************************************************************** | |
7124 | GIF | |
7125 | ***********************************************************************/ | |
7126 | ||
9e2a2647 | 7127 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
4ef0d4d0 | 7128 | |
578098f3 PE |
7129 | static bool gif_image_p (Lisp_Object object); |
7130 | static bool gif_load (struct frame *f, struct image *img); | |
f57e2426 | 7131 | static void gif_clear_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
7132 | |
7133 | /* The symbol `gif' identifying images of this type. */ | |
7134 | ||
955cbe7b | 7135 | static Lisp_Object Qgif; |
4ef0d4d0 KS |
7136 | |
7137 | /* Indices of image specification fields in gif_format, below. */ | |
7138 | ||
7139 | enum gif_keyword_index | |
7140 | { | |
7141 | GIF_TYPE, | |
7142 | GIF_DATA, | |
7143 | GIF_FILE, | |
7144 | GIF_ASCENT, | |
7145 | GIF_MARGIN, | |
7146 | GIF_RELIEF, | |
7147 | GIF_ALGORITHM, | |
7148 | GIF_HEURISTIC_MASK, | |
7149 | GIF_MASK, | |
7150 | GIF_IMAGE, | |
7151 | GIF_BACKGROUND, | |
7152 | GIF_LAST | |
7153 | }; | |
7154 | ||
7155 | /* Vector of image_keyword structures describing the format | |
7156 | of valid user-defined image specifications. */ | |
7157 | ||
91433552 | 7158 | static const struct image_keyword gif_format[GIF_LAST] = |
4ef0d4d0 KS |
7159 | { |
7160 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7161 | {":data", IMAGE_STRING_VALUE, 0}, | |
7162 | {":file", IMAGE_STRING_VALUE, 0}, | |
7163 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 7164 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
7165 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
7166 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7167 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7168 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
58279503 | 7169 | {":index", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}, |
4ef0d4d0 KS |
7170 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} |
7171 | }; | |
7172 | ||
a9e7a9d5 | 7173 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 7174 | static bool init_gif_functions (void); |
bb4d86b4 CY |
7175 | #else |
7176 | #define init_gif_functions NULL | |
7177 | #endif | |
7178 | ||
4ef0d4d0 KS |
7179 | /* Structure describing the image type `gif'. */ |
7180 | ||
7181 | static struct image_type gif_type = | |
7182 | { | |
7183 | &Qgif, | |
7184 | gif_image_p, | |
7185 | gif_load, | |
6ac3c7bb | 7186 | gif_clear_image, |
bb4d86b4 | 7187 | init_gif_functions, |
4ef0d4d0 KS |
7188 | NULL |
7189 | }; | |
7190 | ||
6ac3c7bb KS |
7191 | /* Free X resources of GIF image IMG which is used on frame F. */ |
7192 | ||
7193 | static void | |
d3da34e0 | 7194 | gif_clear_image (struct frame *f, struct image *img) |
6ac3c7bb | 7195 | { |
b50691aa | 7196 | img->lisp_data = Qnil; |
6ac3c7bb KS |
7197 | x_clear_image (f, img); |
7198 | } | |
7199 | ||
578098f3 | 7200 | /* Return true if OBJECT is a valid GIF image specification. */ |
4ef0d4d0 | 7201 | |
578098f3 | 7202 | static bool |
d3da34e0 | 7203 | gif_image_p (Lisp_Object object) |
4ef0d4d0 KS |
7204 | { |
7205 | struct image_keyword fmt[GIF_LAST]; | |
72af86bd | 7206 | memcpy (fmt, gif_format, sizeof fmt); |
4ef0d4d0 KS |
7207 | |
7208 | if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)) | |
7209 | return 0; | |
7210 | ||
7211 | /* Must specify either the :data or :file keyword. */ | |
7212 | return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1; | |
7213 | } | |
7214 | ||
9e2a2647 | 7215 | #endif /* HAVE_GIF */ |
4ef0d4d0 KS |
7216 | |
7217 | #ifdef HAVE_GIF | |
7218 | ||
9e2a2647 | 7219 | #if defined (HAVE_NTGUI) |
99149625 | 7220 | |
2e09fef1 EZ |
7221 | /* winuser.h might define DrawText to DrawTextA or DrawTextW. |
7222 | Undefine before redefining to avoid a preprocessor warning. */ | |
7223 | #ifdef DrawText | |
7224 | #undef DrawText | |
7225 | #endif | |
4ef0d4d0 KS |
7226 | /* avoid conflict with QuickdrawText.h */ |
7227 | #define DrawText gif_DrawText | |
7228 | #include <gif_lib.h> | |
7229 | #undef DrawText | |
7230 | ||
567f95ec EZ |
7231 | /* Giflib before 5.0 didn't define these macros (used only if HAVE_NTGUI). */ |
7232 | #ifndef GIFLIB_MINOR | |
7233 | #define GIFLIB_MINOR 0 | |
7234 | #endif | |
7235 | #ifndef GIFLIB_RELEASE | |
7236 | #define GIFLIB_RELEASE 0 | |
7237 | #endif | |
7238 | ||
9e2a2647 | 7239 | #else /* HAVE_NTGUI */ |
4ef0d4d0 KS |
7240 | |
7241 | #include <gif_lib.h> | |
7242 | ||
9e2a2647 | 7243 | #endif /* HAVE_NTGUI */ |
4ef0d4d0 | 7244 | |
567f95ec EZ |
7245 | /* Giflib before 5.0 didn't define these macros. */ |
7246 | #ifndef GIFLIB_MAJOR | |
7247 | #define GIFLIB_MAJOR 4 | |
7248 | #endif | |
7249 | ||
0fda9b75 | 7250 | #ifdef WINDOWSNT |
4ef0d4d0 KS |
7251 | |
7252 | /* GIF library details. */ | |
14beddf4 CY |
7253 | DEF_IMGLIB_FN (int, DGifCloseFile, (GifFileType *)); |
7254 | DEF_IMGLIB_FN (int, DGifSlurp, (GifFileType *)); | |
f3606ef7 | 7255 | #if GIFLIB_MAJOR < 5 |
14beddf4 CY |
7256 | DEF_IMGLIB_FN (GifFileType *, DGifOpen, (void *, InputFunc)); |
7257 | DEF_IMGLIB_FN (GifFileType *, DGifOpenFileName, (const char *)); | |
f3606ef7 BF |
7258 | #else |
7259 | DEF_IMGLIB_FN (GifFileType *, DGifOpen, (void *, InputFunc, int *)); | |
7260 | DEF_IMGLIB_FN (GifFileType *, DGifOpenFileName, (const char *, int *)); | |
a5dab159 | 7261 | DEF_IMGLIB_FN (char *, GifErrorString, (int)); |
f3606ef7 | 7262 | #endif |
4ef0d4d0 | 7263 | |
578098f3 | 7264 | static bool |
d07ff9db | 7265 | init_gif_functions (void) |
4ef0d4d0 KS |
7266 | { |
7267 | HMODULE library; | |
7268 | ||
d07ff9db | 7269 | if (!(library = w32_delayed_load (Qgif))) |
4ef0d4d0 KS |
7270 | return 0; |
7271 | ||
7272 | LOAD_IMGLIB_FN (library, DGifCloseFile); | |
7273 | LOAD_IMGLIB_FN (library, DGifSlurp); | |
7274 | LOAD_IMGLIB_FN (library, DGifOpen); | |
7275 | LOAD_IMGLIB_FN (library, DGifOpenFileName); | |
a5dab159 EZ |
7276 | #if GIFLIB_MAJOR >= 5 |
7277 | LOAD_IMGLIB_FN (library, GifErrorString); | |
7278 | #endif | |
4ef0d4d0 KS |
7279 | return 1; |
7280 | } | |
7281 | ||
7282 | #else | |
7283 | ||
7284 | #define fn_DGifCloseFile DGifCloseFile | |
7285 | #define fn_DGifSlurp DGifSlurp | |
7286 | #define fn_DGifOpen DGifOpen | |
7287 | #define fn_DGifOpenFileName DGifOpenFileName | |
99149625 PE |
7288 | #if 5 <= GIFLIB_MAJOR |
7289 | # define fn_GifErrorString GifErrorString | |
7290 | #endif | |
4ef0d4d0 | 7291 | |
0fda9b75 | 7292 | #endif /* WINDOWSNT */ |
4ef0d4d0 KS |
7293 | |
7294 | /* Reading a GIF image from memory | |
7295 | Based on the PNG memory stuff to a certain extent. */ | |
7296 | ||
7297 | typedef struct | |
7298 | { | |
7299 | unsigned char *bytes; | |
3f791afe PE |
7300 | ptrdiff_t len; |
7301 | ptrdiff_t index; | |
4ef0d4d0 KS |
7302 | } |
7303 | gif_memory_source; | |
7304 | ||
7305 | /* Make the current memory source available to gif_read_from_memory. | |
7306 | It's done this way because not all versions of libungif support | |
7307 | a UserData field in the GifFileType structure. */ | |
7308 | static gif_memory_source *current_gif_memory_src; | |
7309 | ||
7310 | static int | |
d3da34e0 | 7311 | gif_read_from_memory (GifFileType *file, GifByteType *buf, int len) |
4ef0d4d0 KS |
7312 | { |
7313 | gif_memory_source *src = current_gif_memory_src; | |
7314 | ||
7315 | if (len > src->len - src->index) | |
7316 | return -1; | |
7317 | ||
72af86bd | 7318 | memcpy (buf, src->bytes + src->index, len); |
4ef0d4d0 KS |
7319 | src->index += len; |
7320 | return len; | |
7321 | } | |
7322 | ||
7323 | ||
578098f3 | 7324 | /* Load GIF image IMG for use on frame F. Value is true if |
4ef0d4d0 KS |
7325 | successful. */ |
7326 | ||
91433552 DN |
7327 | static const int interlace_start[] = {0, 4, 2, 1}; |
7328 | static const int interlace_increment[] = {8, 8, 4, 2}; | |
aca946f3 | 7329 | |
17c0c952 JD |
7330 | #define GIF_LOCAL_DESCRIPTOR_EXTENSION 249 |
7331 | ||
578098f3 | 7332 | static bool |
d3da34e0 | 7333 | gif_load (struct frame *f, struct image *img) |
4ef0d4d0 | 7334 | { |
60002bf5 CY |
7335 | Lisp_Object file; |
7336 | int rc, width, height, x, y, i, j; | |
4ef0d4d0 KS |
7337 | XImagePtr ximg; |
7338 | ColorMapObject *gif_color_map; | |
7339 | unsigned long pixel_colors[256]; | |
7340 | GifFileType *gif; | |
4ef0d4d0 | 7341 | gif_memory_source memsrc; |
60002bf5 CY |
7342 | Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL); |
7343 | Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL); | |
7344 | Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL); | |
7345 | unsigned long bgcolor = 0; | |
13464394 | 7346 | EMACS_INT idx; |
a5dab159 EZ |
7347 | #if GIFLIB_MAJOR >= 5 |
7348 | int gif_err; | |
7349 | #endif | |
4ef0d4d0 KS |
7350 | |
7351 | if (NILP (specified_data)) | |
7352 | { | |
7353 | file = x_find_image_file (specified_file); | |
7354 | if (!STRINGP (file)) | |
7355 | { | |
7356 | image_error ("Cannot find image file `%s'", specified_file, Qnil); | |
4ef0d4d0 KS |
7357 | return 0; |
7358 | } | |
9ab69568 EZ |
7359 | #ifdef WINDOWSNT |
7360 | file = ansi_encode_filename (file); | |
7361 | #endif | |
4ef0d4d0 | 7362 | |
df61c790 | 7363 | /* Open the GIF file. */ |
f3606ef7 | 7364 | #if GIFLIB_MAJOR < 5 |
2037898d | 7365 | gif = fn_DGifOpenFileName (SSDATA (file)); |
4ef0d4d0 KS |
7366 | if (gif == NULL) |
7367 | { | |
7368 | image_error ("Cannot open `%s'", file, Qnil); | |
4ef0d4d0 KS |
7369 | return 0; |
7370 | } | |
a5dab159 EZ |
7371 | #else |
7372 | gif = fn_DGifOpenFileName (SSDATA (file), &gif_err); | |
7373 | if (gif == NULL) | |
7374 | { | |
7375 | image_error ("Cannot open `%s': %s", | |
7376 | file, build_string (fn_GifErrorString (gif_err))); | |
7377 | return 0; | |
7378 | } | |
7379 | #endif | |
4ef0d4d0 KS |
7380 | } |
7381 | else | |
7382 | { | |
7574650a AS |
7383 | if (!STRINGP (specified_data)) |
7384 | { | |
7385 | image_error ("Invalid image data `%s'", specified_data, Qnil); | |
7386 | return 0; | |
7387 | } | |
7388 | ||
4ef0d4d0 KS |
7389 | /* Read from memory! */ |
7390 | current_gif_memory_src = &memsrc; | |
7391 | memsrc.bytes = SDATA (specified_data); | |
7392 | memsrc.len = SBYTES (specified_data); | |
7393 | memsrc.index = 0; | |
7394 | ||
f3606ef7 | 7395 | #if GIFLIB_MAJOR < 5 |
df61c790 | 7396 | gif = fn_DGifOpen (&memsrc, gif_read_from_memory); |
4ef0d4d0 KS |
7397 | if (!gif) |
7398 | { | |
7399 | image_error ("Cannot open memory source `%s'", img->spec, Qnil); | |
4ef0d4d0 KS |
7400 | return 0; |
7401 | } | |
a5dab159 EZ |
7402 | #else |
7403 | gif = fn_DGifOpen (&memsrc, gif_read_from_memory, &gif_err); | |
7404 | if (!gif) | |
7405 | { | |
7406 | image_error ("Cannot open memory source `%s': %s", | |
7407 | img->spec, build_string (fn_GifErrorString (gif_err))); | |
7408 | return 0; | |
7409 | } | |
7410 | #endif | |
4ef0d4d0 KS |
7411 | } |
7412 | ||
f1f25b99 CY |
7413 | /* Before reading entire contents, check the declared image size. */ |
7414 | if (!check_image_size (f, gif->SWidth, gif->SHeight)) | |
7415 | { | |
10ea2b82 | 7416 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 | 7417 | fn_DGifCloseFile (gif); |
f1f25b99 CY |
7418 | return 0; |
7419 | } | |
7420 | ||
4ef0d4d0 KS |
7421 | /* Read entire contents. */ |
7422 | rc = fn_DGifSlurp (gif); | |
60002bf5 | 7423 | if (rc == GIF_ERROR || gif->ImageCount <= 0) |
4ef0d4d0 KS |
7424 | { |
7425 | image_error ("Error reading `%s'", img->spec, Qnil); | |
7426 | fn_DGifCloseFile (gif); | |
4ef0d4d0 KS |
7427 | return 0; |
7428 | } | |
7429 | ||
60002bf5 CY |
7430 | /* Which sub-image are we to display? */ |
7431 | { | |
33290528 PE |
7432 | Lisp_Object image_number = image_spec_value (img->spec, QCindex, NULL); |
7433 | idx = INTEGERP (image_number) ? XFASTINT (image_number) : 0; | |
60002bf5 | 7434 | if (idx < 0 || idx >= gif->ImageCount) |
17c0c952 | 7435 | { |
60002bf5 | 7436 | image_error ("Invalid image number `%s' in image `%s'", |
33290528 | 7437 | image_number, img->spec); |
60002bf5 CY |
7438 | fn_DGifCloseFile (gif); |
7439 | return 0; | |
17c0c952 | 7440 | } |
60002bf5 | 7441 | } |
17c0c952 | 7442 | |
e013fb34 CY |
7443 | width = img->width = gif->SWidth; |
7444 | height = img->height = gif->SHeight; | |
0f4aca46 | 7445 | |
e013fb34 CY |
7446 | img->corners[TOP_CORNER] = gif->SavedImages[0].ImageDesc.Top; |
7447 | img->corners[LEFT_CORNER] = gif->SavedImages[0].ImageDesc.Left; | |
8d903f4e AS |
7448 | img->corners[BOT_CORNER] |
7449 | = img->corners[TOP_CORNER] + gif->SavedImages[0].ImageDesc.Height; | |
7450 | img->corners[RIGHT_CORNER] | |
7451 | = img->corners[LEFT_CORNER] + gif->SavedImages[0].ImageDesc.Width; | |
4ef0d4d0 | 7452 | |
f1f25b99 CY |
7453 | if (!check_image_size (f, width, height)) |
7454 | { | |
10ea2b82 | 7455 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 | 7456 | fn_DGifCloseFile (gif); |
f1f25b99 CY |
7457 | return 0; |
7458 | } | |
7459 | ||
44aa9ee6 PE |
7460 | /* Check that the selected subimages fit. It's not clear whether |
7461 | the GIF spec requires this, but Emacs can crash if they don't fit. */ | |
7462 | for (j = 0; j <= idx; ++j) | |
7463 | { | |
7464 | struct SavedImage *subimage = gif->SavedImages + j; | |
7465 | int subimg_width = subimage->ImageDesc.Width; | |
7466 | int subimg_height = subimage->ImageDesc.Height; | |
7467 | int subimg_top = subimage->ImageDesc.Top; | |
7468 | int subimg_left = subimage->ImageDesc.Left; | |
7469 | if (! (0 <= subimg_width && 0 <= subimg_height | |
7470 | && 0 <= subimg_top && subimg_top <= height - subimg_height | |
7471 | && 0 <= subimg_left && subimg_left <= width - subimg_width)) | |
7472 | { | |
7473 | image_error ("Subimage does not fit in image", Qnil, Qnil); | |
7474 | fn_DGifCloseFile (gif); | |
7475 | return 0; | |
7476 | } | |
7477 | } | |
7478 | ||
4ef0d4d0 | 7479 | /* Create the X image and pixmap. */ |
547c9269 | 7480 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
4ef0d4d0 KS |
7481 | { |
7482 | fn_DGifCloseFile (gif); | |
4ef0d4d0 KS |
7483 | return 0; |
7484 | } | |
7485 | ||
60002bf5 CY |
7486 | /* Clear the part of the screen image not covered by the image. |
7487 | Full animated GIF support requires more here (see the gif89 spec, | |
7488 | disposal methods). Let's simply assume that the part not covered | |
7489 | by a sub-image is in the frame's background color. */ | |
6ac3c7bb | 7490 | for (y = 0; y < img->corners[TOP_CORNER]; ++y) |
4ef0d4d0 KS |
7491 | for (x = 0; x < width; ++x) |
7492 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7493 | ||
6ac3c7bb | 7494 | for (y = img->corners[BOT_CORNER]; y < height; ++y) |
4ef0d4d0 KS |
7495 | for (x = 0; x < width; ++x) |
7496 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); | |
7497 | ||
6ac3c7bb | 7498 | for (y = img->corners[TOP_CORNER]; y < img->corners[BOT_CORNER]; ++y) |
4ef0d4d0 | 7499 | { |
6ac3c7bb | 7500 | for (x = 0; x < img->corners[LEFT_CORNER]; ++x) |
4ef0d4d0 | 7501 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
6ac3c7bb | 7502 | for (x = img->corners[RIGHT_CORNER]; x < width; ++x) |
4ef0d4d0 KS |
7503 | XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f)); |
7504 | } | |
7505 | ||
60002bf5 | 7506 | /* Read the GIF image into the X image. */ |
4ef0d4d0 | 7507 | |
60002bf5 CY |
7508 | /* FIXME: With the current implementation, loading an animated gif |
7509 | is quadratic in the number of animation frames, since each frame | |
7510 | is a separate struct image. We must provide a way for a single | |
7511 | gif_load call to construct and save all animation frames. */ | |
4ef0d4d0 | 7512 | |
60002bf5 CY |
7513 | init_color_table (); |
7514 | if (STRINGP (specified_bg)) | |
7515 | bgcolor = x_alloc_image_color (f, img, specified_bg, | |
7516 | FRAME_BACKGROUND_PIXEL (f)); | |
7517 | for (j = 0; j <= idx; ++j) | |
7518 | { | |
7519 | /* We use a local variable `raster' here because RasterBits is a | |
7520 | char *, which invites problems with bytes >= 0x80. */ | |
7521 | struct SavedImage *subimage = gif->SavedImages + j; | |
7522 | unsigned char *raster = (unsigned char *) subimage->RasterBits; | |
7523 | int transparency_color_index = -1; | |
7524 | int disposal = 0; | |
e013fb34 CY |
7525 | int subimg_width = subimage->ImageDesc.Width; |
7526 | int subimg_height = subimage->ImageDesc.Height; | |
7527 | int subimg_top = subimage->ImageDesc.Top; | |
7528 | int subimg_left = subimage->ImageDesc.Left; | |
60002bf5 CY |
7529 | |
7530 | /* Find the Graphic Control Extension block for this sub-image. | |
7531 | Extract the disposal method and transparency color. */ | |
7532 | for (i = 0; i < subimage->ExtensionBlockCount; i++) | |
4ef0d4d0 | 7533 | { |
60002bf5 | 7534 | ExtensionBlock *extblock = subimage->ExtensionBlocks + i; |
4ef0d4d0 | 7535 | |
60002bf5 CY |
7536 | if ((extblock->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION) |
7537 | && extblock->ByteCount == 4 | |
7538 | && extblock->Bytes[0] & 1) | |
4ef0d4d0 | 7539 | { |
60002bf5 CY |
7540 | /* From gif89a spec: 1 = "keep in place", 2 = "restore |
7541 | to background". Treat any other value like 2. */ | |
7542 | disposal = (extblock->Bytes[0] >> 2) & 7; | |
2547adb1 | 7543 | transparency_color_index = (unsigned char) extblock->Bytes[3]; |
60002bf5 | 7544 | break; |
4ef0d4d0 | 7545 | } |
4ef0d4d0 | 7546 | } |
60002bf5 CY |
7547 | |
7548 | /* We can't "keep in place" the first subimage. */ | |
7549 | if (j == 0) | |
7550 | disposal = 2; | |
7551 | ||
e013fb34 CY |
7552 | /* For disposal == 0, the spec says "No disposal specified. The |
7553 | decoder is not required to take any action." In practice, it | |
7554 | seems we need to treat this like "keep in place", see e.g. | |
7555 | http://upload.wikimedia.org/wikipedia/commons/3/37/Clock.gif */ | |
7556 | if (disposal == 0) | |
7557 | disposal = 1; | |
7558 | ||
60002bf5 CY |
7559 | /* Allocate subimage colors. */ |
7560 | memset (pixel_colors, 0, sizeof pixel_colors); | |
7561 | gif_color_map = subimage->ImageDesc.ColorMap; | |
7562 | if (!gif_color_map) | |
7563 | gif_color_map = gif->SColorMap; | |
7564 | ||
7565 | if (gif_color_map) | |
7566 | for (i = 0; i < gif_color_map->ColorCount; ++i) | |
4ef0d4d0 | 7567 | { |
60002bf5 CY |
7568 | if (transparency_color_index == i) |
7569 | pixel_colors[i] = STRINGP (specified_bg) | |
7570 | ? bgcolor : FRAME_BACKGROUND_PIXEL (f); | |
7571 | else | |
7572 | { | |
7573 | int r = gif_color_map->Colors[i].Red << 8; | |
7574 | int g = gif_color_map->Colors[i].Green << 8; | |
7575 | int b = gif_color_map->Colors[i].Blue << 8; | |
7576 | pixel_colors[i] = lookup_rgb_color (f, r, g, b); | |
7577 | } | |
4ef0d4d0 | 7578 | } |
60002bf5 CY |
7579 | |
7580 | /* Apply the pixel values. */ | |
be316ede | 7581 | if (GIFLIB_MAJOR < 5 && gif->SavedImages[j].ImageDesc.Interlace) |
60002bf5 CY |
7582 | { |
7583 | int row, pass; | |
7584 | ||
7585 | for (y = 0, row = interlace_start[0], pass = 0; | |
e013fb34 | 7586 | y < subimg_height; |
60002bf5 CY |
7587 | y++, row += interlace_increment[pass]) |
7588 | { | |
a9ebfa0b | 7589 | while (subimg_height <= row) |
b9ff995e | 7590 | row = interlace_start[++pass]; |
60002bf5 | 7591 | |
e013fb34 | 7592 | for (x = 0; x < subimg_width; x++) |
60002bf5 | 7593 | { |
e013fb34 | 7594 | int c = raster[y * subimg_width + x]; |
60002bf5 | 7595 | if (transparency_color_index != c || disposal != 1) |
e013fb34 CY |
7596 | XPutPixel (ximg, x + subimg_left, row + subimg_top, |
7597 | pixel_colors[c]); | |
60002bf5 CY |
7598 | } |
7599 | } | |
7600 | } | |
7601 | else | |
7602 | { | |
e013fb34 CY |
7603 | for (y = 0; y < subimg_height; ++y) |
7604 | for (x = 0; x < subimg_width; ++x) | |
60002bf5 | 7605 | { |
e013fb34 | 7606 | int c = raster[y * subimg_width + x]; |
60002bf5 | 7607 | if (transparency_color_index != c || disposal != 1) |
e013fb34 CY |
7608 | XPutPixel (ximg, x + subimg_left, y + subimg_top, |
7609 | pixel_colors[c]); | |
60002bf5 CY |
7610 | } |
7611 | } | |
4ef0d4d0 KS |
7612 | } |
7613 | ||
60002bf5 CY |
7614 | #ifdef COLOR_TABLE_SUPPORT |
7615 | img->colors = colors_in_color_table (&img->ncolors); | |
7616 | free_color_table (); | |
7617 | #endif /* COLOR_TABLE_SUPPORT */ | |
7618 | ||
1546c559 JL |
7619 | /* Save GIF image extension data for `image-metadata'. |
7620 | Format is (count IMAGES extension-data (FUNCTION "BYTES" ...)). */ | |
b50691aa | 7621 | img->lisp_data = Qnil; |
60002bf5 | 7622 | if (gif->SavedImages[idx].ExtensionBlockCount > 0) |
6ac3c7bb | 7623 | { |
13464394 | 7624 | int delay = 0; |
60002bf5 CY |
7625 | ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks; |
7626 | for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++) | |
6ac3c7bb | 7627 | /* Append (... FUNCTION "BYTES") */ |
1100a63c CY |
7628 | { |
7629 | img->lisp_data | |
7630 | = Fcons (make_number (ext->Function), | |
7631 | Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount), | |
7632 | img->lisp_data)); | |
7633 | if (ext->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION | |
7634 | && ext->ByteCount == 4) | |
7635 | { | |
7636 | delay = ext->Bytes[2] << CHAR_BIT; | |
7637 | delay |= ext->Bytes[1]; | |
7638 | } | |
7639 | } | |
6c6f1994 | 7640 | img->lisp_data = list2 (Qextension_data, img->lisp_data); |
1100a63c CY |
7641 | if (delay) |
7642 | img->lisp_data | |
7643 | = Fcons (Qdelay, | |
13464394 | 7644 | Fcons (make_float (delay / 100.0), |
1100a63c | 7645 | img->lisp_data)); |
6ac3c7bb | 7646 | } |
1100a63c | 7647 | |
6ac3c7bb | 7648 | if (gif->ImageCount > 1) |
b50691aa CY |
7649 | img->lisp_data = Fcons (Qcount, |
7650 | Fcons (make_number (gif->ImageCount), | |
7651 | img->lisp_data)); | |
6ac3c7bb | 7652 | |
4ef0d4d0 KS |
7653 | fn_DGifCloseFile (gif); |
7654 | ||
7655 | /* Maybe fill in the background field while we have ximg handy. */ | |
7656 | if (NILP (image_spec_value (img->spec, QCbackground, NULL))) | |
2e09fef1 EZ |
7657 | /* Casting avoids a GCC warning. */ |
7658 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
4ef0d4d0 | 7659 | |
547c9269 YM |
7660 | /* Put ximg into the image. */ |
7661 | image_put_x_image (f, img, ximg, 0); | |
4ef0d4d0 | 7662 | |
4ef0d4d0 KS |
7663 | return 1; |
7664 | } | |
7665 | ||
2e09fef1 | 7666 | #else /* !HAVE_GIF */ |
4ef0d4d0 | 7667 | |
edfda783 | 7668 | #ifdef HAVE_NS |
578098f3 | 7669 | static bool |
edfda783 AR |
7670 | gif_load (struct frame *f, struct image *img) |
7671 | { | |
ed3751c8 JB |
7672 | return ns_load_image (f, img, |
7673 | image_spec_value (img->spec, QCfile, NULL), | |
7674 | image_spec_value (img->spec, QCdata, NULL)); | |
edfda783 AR |
7675 | } |
7676 | #endif /* HAVE_NS */ | |
7677 | ||
4ef0d4d0 KS |
7678 | #endif /* HAVE_GIF */ |
7679 | ||
7680 | ||
b9ed53d5 PE |
7681 | #ifdef HAVE_IMAGEMAGICK |
7682 | ||
7683 | /*********************************************************************** | |
7684 | ImageMagick | |
7685 | ***********************************************************************/ | |
7686 | ||
7687 | /* Scale an image size by returning SIZE / DIVISOR * MULTIPLIER, | |
7688 | safely rounded and clipped to int range. */ | |
7689 | ||
7690 | static int | |
7691 | scale_image_size (int size, size_t divisor, size_t multiplier) | |
7692 | { | |
7693 | if (divisor != 0) | |
7694 | { | |
7695 | double s = size; | |
7696 | double scaled = s * multiplier / divisor + 0.5; | |
7697 | if (scaled < INT_MAX) | |
7698 | return scaled; | |
7699 | } | |
7700 | return INT_MAX; | |
7701 | } | |
7702 | ||
7703 | /* Compute the desired size of an image with native size WIDTH x HEIGHT. | |
7704 | Use SPEC to deduce the size. Store the desired size into | |
7705 | *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */ | |
f3f9606c LMI |
7706 | static void |
7707 | compute_image_size (size_t width, size_t height, | |
7708 | Lisp_Object spec, | |
7709 | int *d_width, int *d_height) | |
7710 | { | |
7711 | Lisp_Object value; | |
7712 | int desired_width, desired_height; | |
7713 | ||
7714 | /* If width and/or height is set in the display spec assume we want | |
7715 | to scale to those values. If either h or w is unspecified, the | |
7716 | unspecified should be calculated from the specified to preserve | |
7717 | aspect ratio. */ | |
7718 | value = image_spec_value (spec, QCwidth, NULL); | |
b9ed53d5 | 7719 | desired_width = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1; |
f3f9606c | 7720 | value = image_spec_value (spec, QCheight, NULL); |
b9ed53d5 | 7721 | desired_height = NATNUMP (value) ? min (XFASTINT (value), INT_MAX) : -1; |
f3f9606c LMI |
7722 | |
7723 | if (desired_width == -1) | |
7724 | { | |
7725 | value = image_spec_value (spec, QCmax_width, NULL); | |
b9ed53d5 | 7726 | if (NATNUMP (value)) |
f3f9606c | 7727 | { |
b9ed53d5 PE |
7728 | int max_width = min (XFASTINT (value), INT_MAX); |
7729 | if (max_width < width) | |
f3f9606c | 7730 | { |
b9ed53d5 PE |
7731 | /* The image is wider than :max-width. */ |
7732 | desired_width = max_width; | |
7733 | if (desired_height == -1) | |
f3f9606c | 7734 | { |
b9ed53d5 PE |
7735 | desired_height = scale_image_size (desired_width, |
7736 | width, height); | |
7737 | value = image_spec_value (spec, QCmax_height, NULL); | |
7738 | if (NATNUMP (value)) | |
f3f9606c | 7739 | { |
b9ed53d5 PE |
7740 | int max_height = min (XFASTINT (value), INT_MAX); |
7741 | if (max_height < desired_height) | |
7742 | { | |
7743 | desired_height = max_height; | |
7744 | desired_width = scale_image_size (desired_height, | |
7745 | height, width); | |
7746 | } | |
f3f9606c LMI |
7747 | } |
7748 | } | |
f3f9606c LMI |
7749 | } |
7750 | } | |
7751 | } | |
7752 | ||
7753 | if (desired_height == -1) | |
7754 | { | |
7755 | value = image_spec_value (spec, QCmax_height, NULL); | |
b9ed53d5 PE |
7756 | if (NATNUMP (value)) |
7757 | { | |
7758 | int max_height = min (XFASTINT (value), INT_MAX); | |
7759 | if (max_height < height) | |
7760 | desired_height = max_height; | |
7761 | } | |
f3f9606c LMI |
7762 | } |
7763 | ||
7764 | if (desired_width != -1 && desired_height == -1) | |
7765 | /* w known, calculate h. */ | |
b9ed53d5 | 7766 | desired_height = scale_image_size (desired_width, width, height); |
f3f9606c LMI |
7767 | |
7768 | if (desired_width == -1 && desired_height != -1) | |
7769 | /* h known, calculate w. */ | |
b9ed53d5 | 7770 | desired_width = scale_image_size (desired_height, height, width); |
f3f9606c LMI |
7771 | |
7772 | *d_width = desired_width; | |
7773 | *d_height = desired_height; | |
7774 | } | |
7775 | ||
4b66faf3 | 7776 | static Lisp_Object Qimagemagick; |
a32d4040 | 7777 | |
578098f3 PE |
7778 | static bool imagemagick_image_p (Lisp_Object); |
7779 | static bool imagemagick_load (struct frame *, struct image *); | |
d66c4c7c CY |
7780 | static void imagemagick_clear_image (struct frame *, struct image *); |
7781 | ||
a32d4040 | 7782 | /* Indices of image specification fields in imagemagick_format. */ |
3de25479 JV |
7783 | |
7784 | enum imagemagick_keyword_index | |
4917006b JV |
7785 | { |
7786 | IMAGEMAGICK_TYPE, | |
7787 | IMAGEMAGICK_DATA, | |
7788 | IMAGEMAGICK_FILE, | |
7789 | IMAGEMAGICK_ASCENT, | |
7790 | IMAGEMAGICK_MARGIN, | |
7791 | IMAGEMAGICK_RELIEF, | |
7792 | IMAGEMAGICK_ALGORITHM, | |
7793 | IMAGEMAGICK_HEURISTIC_MASK, | |
7794 | IMAGEMAGICK_MASK, | |
7795 | IMAGEMAGICK_BACKGROUND, | |
c7f514b5 JV |
7796 | IMAGEMAGICK_HEIGHT, |
7797 | IMAGEMAGICK_WIDTH, | |
f3f9606c LMI |
7798 | IMAGEMAGICK_MAX_HEIGHT, |
7799 | IMAGEMAGICK_MAX_WIDTH, | |
c6b7ccaa | 7800 | IMAGEMAGICK_FORMAT, |
c7f514b5 JV |
7801 | IMAGEMAGICK_ROTATION, |
7802 | IMAGEMAGICK_CROP, | |
4917006b JV |
7803 | IMAGEMAGICK_LAST |
7804 | }; | |
3de25479 JV |
7805 | |
7806 | /* Vector of image_keyword structures describing the format | |
7807 | of valid user-defined image specifications. */ | |
7808 | ||
7809 | static struct image_keyword imagemagick_format[IMAGEMAGICK_LAST] = | |
4917006b JV |
7810 | { |
7811 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
7812 | {":data", IMAGE_STRING_VALUE, 0}, | |
7813 | {":file", IMAGE_STRING_VALUE, 0}, | |
7814 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 7815 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4917006b JV |
7816 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
7817 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7818 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
7819 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
c7f514b5 | 7820 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0}, |
7574650a AS |
7821 | {":height", IMAGE_INTEGER_VALUE, 0}, |
7822 | {":width", IMAGE_INTEGER_VALUE, 0}, | |
f3f9606c LMI |
7823 | {":max-height", IMAGE_INTEGER_VALUE, 0}, |
7824 | {":max-width", IMAGE_INTEGER_VALUE, 0}, | |
c6b7ccaa | 7825 | {":format", IMAGE_SYMBOL_VALUE, 0}, |
7574650a | 7826 | {":rotation", IMAGE_NUMBER_VALUE, 0}, |
c7f514b5 | 7827 | {":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0} |
4917006b | 7828 | }; |
a32d4040 | 7829 | |
33d4113c | 7830 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 7831 | static bool init_imagemagick_functions (void); |
bb4d86b4 CY |
7832 | #else |
7833 | #define init_imagemagick_functions NULL | |
7834 | #endif | |
7835 | ||
d66c4c7c CY |
7836 | /* Structure describing the image type for any image handled via |
7837 | ImageMagick. */ | |
7838 | ||
7839 | static struct image_type imagemagick_type = | |
7840 | { | |
7841 | &Qimagemagick, | |
7842 | imagemagick_image_p, | |
7843 | imagemagick_load, | |
7844 | imagemagick_clear_image, | |
bb4d86b4 | 7845 | init_imagemagick_functions, |
d66c4c7c CY |
7846 | NULL |
7847 | }; | |
7848 | ||
3de25479 JV |
7849 | /* Free X resources of imagemagick image IMG which is used on frame F. */ |
7850 | ||
7851 | static void | |
4917006b JV |
7852 | imagemagick_clear_image (struct frame *f, |
7853 | struct image *img) | |
3de25479 | 7854 | { |
3de25479 JV |
7855 | x_clear_image (f, img); |
7856 | } | |
7857 | ||
578098f3 | 7858 | /* Return true if OBJECT is a valid IMAGEMAGICK image specification. Do |
3de25479 JV |
7859 | this by calling parse_image_spec and supplying the keywords that |
7860 | identify the IMAGEMAGICK format. */ | |
7861 | ||
578098f3 | 7862 | static bool |
4917006b | 7863 | imagemagick_image_p (Lisp_Object object) |
3de25479 JV |
7864 | { |
7865 | struct image_keyword fmt[IMAGEMAGICK_LAST]; | |
dbfe4532 | 7866 | memcpy (fmt, imagemagick_format, sizeof fmt); |
3de25479 JV |
7867 | |
7868 | if (!parse_image_spec (object, fmt, IMAGEMAGICK_LAST, Qimagemagick)) | |
7869 | return 0; | |
7870 | ||
7871 | /* Must specify either the :data or :file keyword. */ | |
7872 | return fmt[IMAGEMAGICK_FILE].count + fmt[IMAGEMAGICK_DATA].count == 1; | |
7873 | } | |
7874 | ||
7875 | /* The GIF library also defines DrawRectangle, but its never used in Emacs. | |
c7015153 | 7876 | Therefore rename the function so it doesn't collide with ImageMagick. */ |
3de25479 JV |
7877 | #define DrawRectangle DrawRectangleGif |
7878 | #include <wand/MagickWand.h> | |
7879 | ||
60737f02 PE |
7880 | /* ImageMagick 6.5.3 through 6.6.5 hid PixelGetMagickColor for some reason. |
7881 | Emacs seems to work fine with the hidden version, so unhide it. */ | |
7882 | #include <magick/version.h> | |
7883 | #if 0x653 <= MagickLibVersion && MagickLibVersion <= 0x665 | |
7884 | extern WandExport void PixelGetMagickColor (const PixelWand *, | |
7885 | MagickPixelPacket *); | |
7886 | #endif | |
7887 | ||
d1d7b339 JL |
7888 | /* Log ImageMagick error message. |
7889 | Useful when a ImageMagick function returns the status `MagickFalse'. */ | |
7890 | ||
7891 | static void | |
7892 | imagemagick_error (MagickWand *wand) | |
7893 | { | |
7894 | char *description; | |
7895 | ExceptionType severity; | |
7896 | ||
7897 | description = MagickGetException (wand, &severity); | |
7898 | image_error ("ImageMagick error: %s", | |
1130ecfc | 7899 | build_string (description), |
d1d7b339 | 7900 | Qnil); |
ed44be70 | 7901 | MagickRelinquishMemory (description); |
d1d7b339 JL |
7902 | } |
7903 | ||
8259030d | 7904 | /* Possibly give ImageMagick some extra help to determine the image |
01c3051f | 7905 | type by supplying a "dummy" filename based on the Content-Type. */ |
8259030d | 7906 | |
01c3051f PE |
7907 | static char * |
7908 | imagemagick_filename_hint (Lisp_Object spec, char hint_buffer[MaxTextExtent]) | |
8259030d | 7909 | { |
01c3051f PE |
7910 | Lisp_Object symbol = intern ("image-format-suffixes"); |
7911 | Lisp_Object val = find_symbol_value (symbol); | |
7912 | Lisp_Object format; | |
8259030d | 7913 | |
66e7901a LMI |
7914 | if (! CONSP (val)) |
7915 | return NULL; | |
cfa1f436 | 7916 | |
01c3051f PE |
7917 | format = image_spec_value (spec, intern (":format"), NULL); |
7918 | val = Fcar_safe (Fcdr_safe (Fassq (format, val))); | |
66e7901a LMI |
7919 | if (! STRINGP (val)) |
7920 | return NULL; | |
7921 | ||
01c3051f PE |
7922 | /* It's OK to truncate the hint if it has MaxTextExtent or more bytes, |
7923 | as ImageMagick would ignore the extra bytes anyway. */ | |
7924 | snprintf (hint_buffer, MaxTextExtent, "/tmp/foo.%s", SSDATA (val)); | |
7925 | return hint_buffer; | |
8259030d LMI |
7926 | } |
7927 | ||
6e856b69 LMI |
7928 | /* Animated images (e.g., GIF89a) are composed from one "master image" |
7929 | (which is the first one, and then there's a number of images that | |
7930 | follow. If following images have non-transparent colors, these are | |
7931 | composed "on top" of the master image. So, in general, one has to | |
7932 | compute ann the preceding images to be able to display a particular | |
274ff63f | 7933 | sub-image. |
6e856b69 | 7934 | |
274ff63f LMI |
7935 | Computing all the preceding images is too slow, so we maintain a |
7936 | cache of previously computed images. We have to maintain a cache | |
7937 | separate from the image cache, because the images may be scaled | |
7938 | before display. */ | |
7939 | ||
7940 | struct animation_cache | |
7941 | { | |
274ff63f LMI |
7942 | MagickWand *wand; |
7943 | int index; | |
43aac990 | 7944 | struct timespec update_time; |
274ff63f | 7945 | struct animation_cache *next; |
8c2f38aa | 7946 | char signature[FLEXIBLE_ARRAY_MEMBER]; |
274ff63f LMI |
7947 | }; |
7948 | ||
7949 | static struct animation_cache *animation_cache = NULL; | |
7950 | ||
deb31e75 | 7951 | static struct animation_cache * |
274ff63f LMI |
7952 | imagemagick_create_cache (char *signature) |
7953 | { | |
8c2f38aa PE |
7954 | struct animation_cache *cache |
7955 | = xmalloc (offsetof (struct animation_cache, signature) | |
7956 | + strlen (signature) + 1); | |
d0139df5 PE |
7957 | cache->wand = 0; |
7958 | cache->index = 0; | |
7959 | cache->next = 0; | |
8c2f38aa | 7960 | strcpy (cache->signature, signature); |
274ff63f LMI |
7961 | return cache; |
7962 | } | |
7963 | ||
7964 | /* Discard cached images that haven't been used for a minute. */ | |
deb31e75 DA |
7965 | static void |
7966 | imagemagick_prune_animation_cache (void) | |
274ff63f | 7967 | { |
d0139df5 | 7968 | struct animation_cache **pcache = &animation_cache; |
43aac990 PE |
7969 | struct timespec old = timespec_sub (current_timespec (), |
7970 | make_timespec (60, 0)); | |
274ff63f | 7971 | |
d0139df5 | 7972 | while (*pcache) |
274ff63f | 7973 | { |
d0139df5 | 7974 | struct animation_cache *cache = *pcache; |
43aac990 | 7975 | if (timespec_cmp (old, cache->update_time) <= 0) |
d0139df5 PE |
7976 | pcache = &cache->next; |
7977 | else | |
274ff63f | 7978 | { |
274ff63f LMI |
7979 | if (cache->wand) |
7980 | DestroyMagickWand (cache->wand); | |
d0139df5 PE |
7981 | *pcache = cache->next; |
7982 | xfree (cache); | |
8a6f875f | 7983 | } |
274ff63f LMI |
7984 | } |
7985 | } | |
7986 | ||
deb31e75 | 7987 | static struct animation_cache * |
274ff63f LMI |
7988 | imagemagick_get_animation_cache (MagickWand *wand) |
7989 | { | |
d0139df5 | 7990 | char *signature = MagickGetImageSignature (wand); |
319a4d03 | 7991 | struct animation_cache *cache; |
bb566cea | 7992 | struct animation_cache **pcache = &animation_cache; |
274ff63f LMI |
7993 | |
7994 | imagemagick_prune_animation_cache (); | |
7995 | ||
bb566cea | 7996 | while (1) |
274ff63f | 7997 | { |
bb566cea PE |
7998 | cache = *pcache; |
7999 | if (! cache) | |
8000 | { | |
8001 | *pcache = cache = imagemagick_create_cache (signature); | |
8002 | break; | |
8003 | } | |
8c2f38aa | 8004 | if (strcmp (signature, cache->signature) == 0) |
bb566cea PE |
8005 | break; |
8006 | pcache = &cache->next; | |
274ff63f LMI |
8007 | } |
8008 | ||
453f4022 | 8009 | DestroyString (signature); |
43aac990 | 8010 | cache->update_time = current_timespec (); |
274ff63f LMI |
8011 | return cache; |
8012 | } | |
995be755 | 8013 | |
6e856b69 LMI |
8014 | static MagickWand * |
8015 | imagemagick_compute_animated_image (MagickWand *super_wand, int ino) | |
8016 | { | |
691a357f | 8017 | int i; |
6e856b69 | 8018 | MagickWand *composite_wand; |
703dbeba | 8019 | size_t dest_width, dest_height; |
274ff63f | 8020 | struct animation_cache *cache = imagemagick_get_animation_cache (super_wand); |
6e856b69 LMI |
8021 | |
8022 | MagickSetIteratorIndex (super_wand, 0); | |
6e856b69 | 8023 | |
274ff63f LMI |
8024 | if (ino == 0 || cache->wand == NULL || cache->index > ino) |
8025 | { | |
8026 | composite_wand = MagickGetImage (super_wand); | |
8027 | if (cache->wand) | |
8028 | DestroyMagickWand (cache->wand); | |
8029 | } | |
995be755 | 8030 | else |
274ff63f | 8031 | composite_wand = cache->wand; |
995be755 | 8032 | |
703dbeba LMI |
8033 | dest_width = MagickGetImageWidth (composite_wand); |
8034 | dest_height = MagickGetImageHeight (composite_wand); | |
8035 | ||
274ff63f | 8036 | for (i = max (1, cache->index + 1); i <= ino; i++) |
691a357f PE |
8037 | { |
8038 | MagickWand *sub_wand; | |
8039 | PixelIterator *source_iterator, *dest_iterator; | |
8040 | PixelWand **source, **dest; | |
703dbeba LMI |
8041 | size_t source_width, source_height; |
8042 | ssize_t source_left, source_top; | |
691a357f | 8043 | MagickPixelPacket pixel; |
a5b5f73f | 8044 | DisposeType dispose; |
79670014 | 8045 | ptrdiff_t lines = 0; |
6e856b69 | 8046 | |
691a357f PE |
8047 | MagickSetIteratorIndex (super_wand, i); |
8048 | sub_wand = MagickGetImage (super_wand); | |
6e856b69 | 8049 | |
703dbeba LMI |
8050 | MagickGetImagePage (sub_wand, &source_width, &source_height, |
8051 | &source_left, &source_top); | |
8052 | ||
0f8a6926 | 8053 | /* This flag says how to handle transparent pixels. */ |
a5b5f73f LMI |
8054 | dispose = MagickGetImageDispose (sub_wand); |
8055 | ||
691a357f PE |
8056 | source_iterator = NewPixelIterator (sub_wand); |
8057 | if (! source_iterator) | |
8058 | { | |
8059 | DestroyMagickWand (composite_wand); | |
8060 | DestroyMagickWand (sub_wand); | |
274ff63f | 8061 | cache->wand = NULL; |
691a357f PE |
8062 | image_error ("Imagemagick pixel iterator creation failed", |
8063 | Qnil, Qnil); | |
8064 | return NULL; | |
8065 | } | |
6e856b69 | 8066 | |
691a357f PE |
8067 | dest_iterator = NewPixelIterator (composite_wand); |
8068 | if (! dest_iterator) | |
8069 | { | |
8070 | DestroyMagickWand (composite_wand); | |
8071 | DestroyMagickWand (sub_wand); | |
8072 | DestroyPixelIterator (source_iterator); | |
274ff63f | 8073 | cache->wand = NULL; |
691a357f PE |
8074 | image_error ("Imagemagick pixel iterator creation failed", |
8075 | Qnil, Qnil); | |
8076 | return NULL; | |
8077 | } | |
6e856b69 | 8078 | |
526e5233 | 8079 | /* The sub-image may not start at origin, so move the destination |
703dbeba | 8080 | iterator to where the sub-image should start. */ |
1d18b1e6 | 8081 | if (source_top > 0) |
79670014 LMI |
8082 | { |
8083 | PixelSetIteratorRow (dest_iterator, source_top); | |
8084 | lines = source_top; | |
8085 | } | |
703dbeba | 8086 | |
691a357f PE |
8087 | while ((source = PixelGetNextIteratorRow (source_iterator, &source_width)) |
8088 | != NULL) | |
6e856b69 | 8089 | { |
691a357f | 8090 | ptrdiff_t x; |
79670014 LMI |
8091 | |
8092 | /* Sanity check. This shouldn't happen, but apparently | |
8093 | does in some pictures. */ | |
8094 | if (++lines >= dest_height) | |
8095 | break; | |
8096 | ||
691a357f PE |
8097 | dest = PixelGetNextIteratorRow (dest_iterator, &dest_width); |
8098 | for (x = 0; x < source_width; x++) | |
6e856b69 | 8099 | { |
703dbeba | 8100 | /* Sanity check. This shouldn't happen, but apparently |
79670014 | 8101 | also does in some pictures. */ |
5f9aee6f | 8102 | if (x + source_left > dest_width - 1) |
703dbeba | 8103 | break; |
6f94cbbc LMI |
8104 | /* Normally we only copy over non-transparent pixels, |
8105 | but if the disposal method is "Background", then we | |
8106 | copy over all pixels. */ | |
8107 | if (dispose == BackgroundDispose || | |
8108 | PixelGetAlpha (source[x])) | |
691a357f PE |
8109 | { |
8110 | PixelGetMagickColor (source[x], &pixel); | |
703dbeba | 8111 | PixelSetMagickColor (dest[x + source_left], &pixel); |
691a357f | 8112 | } |
6e856b69 | 8113 | } |
1eade943 | 8114 | PixelSyncIterator (dest_iterator); |
6e856b69 | 8115 | } |
6e856b69 | 8116 | |
691a357f PE |
8117 | DestroyPixelIterator (source_iterator); |
8118 | DestroyPixelIterator (dest_iterator); | |
8119 | DestroyMagickWand (sub_wand); | |
8120 | } | |
6e856b69 | 8121 | |
995be755 LMI |
8122 | /* Cache a copy for the next iteration. The current wand will be |
8123 | destroyed by the caller. */ | |
274ff63f LMI |
8124 | cache->wand = CloneMagickWand (composite_wand); |
8125 | cache->index = ino; | |
995be755 | 8126 | |
6e856b69 LMI |
8127 | return composite_wand; |
8128 | } | |
8129 | ||
8130 | ||
d66c4c7c CY |
8131 | /* Helper function for imagemagick_load, which does the actual loading |
8132 | given contents and size, apart from frame and image structures, | |
8133 | passed from imagemagick_load. Uses librimagemagick to do most of | |
8134 | the image processing. | |
3de25479 | 8135 | |
d66c4c7c CY |
8136 | F is a pointer to the Emacs frame; IMG to the image structure to |
8137 | prepare; CONTENTS is the string containing the IMAGEMAGICK data to | |
8138 | be parsed; SIZE is the number of bytes of data; and FILENAME is | |
8139 | either the file name or the image data. | |
3de25479 | 8140 | |
578098f3 | 8141 | Return true if successful. */ |
3de25479 | 8142 | |
578098f3 | 8143 | static bool |
d66c4c7c CY |
8144 | imagemagick_load_image (struct frame *f, struct image *img, |
8145 | unsigned char *contents, unsigned int size, | |
33290528 | 8146 | char *filename) |
3de25479 | 8147 | { |
1b9b4cf4 | 8148 | size_t width, height; |
d66c4c7c | 8149 | MagickBooleanType status; |
3de25479 | 8150 | XImagePtr ximg; |
1b9b4cf4 CY |
8151 | int x, y; |
8152 | MagickWand *image_wand; | |
3de25479 | 8153 | PixelIterator *iterator; |
1b9b4cf4 | 8154 | PixelWand **pixels, *bg_wand = NULL; |
3de25479 | 8155 | MagickPixelPacket pixel; |
4917006b | 8156 | Lisp_Object image; |
7574650a | 8157 | Lisp_Object value; |
33290528 | 8158 | Lisp_Object crop; |
13464394 | 8159 | EMACS_INT ino; |
4917006b JV |
8160 | int desired_width, desired_height; |
8161 | double rotation; | |
7574650a | 8162 | int pixelwidth; |
01c3051f | 8163 | char hint_buffer[MaxTextExtent]; |
8259030d | 8164 | char *filename_hint = NULL; |
7574650a | 8165 | |
a32d4040 CY |
8166 | /* Handle image index for image types who can contain more than one image. |
8167 | Interface :index is same as for GIF. First we "ping" the image to see how | |
8168 | many sub-images it contains. Pinging is faster than loading the image to | |
8169 | find out things about it. */ | |
f853f599 | 8170 | |
a32d4040 | 8171 | /* Initialize the imagemagick environment. */ |
f853f599 | 8172 | MagickWandGenesis (); |
3de25479 JV |
8173 | image = image_spec_value (img->spec, QCindex, NULL); |
8174 | ino = INTEGERP (image) ? XFASTINT (image) : 0; | |
49ceb676 | 8175 | image_wand = NewMagickWand (); |
fa4fdb5c | 8176 | |
01c3051f | 8177 | if (filename) |
49ceb676 | 8178 | status = MagickReadImage (image_wand, filename); |
01c3051f PE |
8179 | else |
8180 | { | |
8181 | filename_hint = imagemagick_filename_hint (img->spec, hint_buffer); | |
49ceb676 LMI |
8182 | MagickSetFilename (image_wand, filename_hint); |
8183 | status = MagickReadImageBlob (image_wand, contents, size); | |
01c3051f | 8184 | } |
7574650a | 8185 | |
d1d7b339 JL |
8186 | if (status == MagickFalse) |
8187 | { | |
49ceb676 LMI |
8188 | imagemagick_error (image_wand); |
8189 | DestroyMagickWand (image_wand); | |
d1d7b339 JL |
8190 | return 0; |
8191 | } | |
fa4fdb5c | 8192 | |
49ceb676 | 8193 | if (ino < 0 || ino >= MagickGetNumberImages (image_wand)) |
7574650a AS |
8194 | { |
8195 | image_error ("Invalid image number `%s' in image `%s'", | |
8196 | image, img->spec); | |
49ceb676 | 8197 | DestroyMagickWand (image_wand); |
7574650a | 8198 | return 0; |
f3700365 | 8199 | } |
431dab04 | 8200 | |
49ceb676 | 8201 | if (MagickGetNumberImages (image_wand) > 1) |
b50691aa | 8202 | img->lisp_data = |
d2a2d277 | 8203 | Fcons (Qcount, |
49ceb676 | 8204 | Fcons (make_number (MagickGetNumberImages (image_wand)), |
b50691aa | 8205 | img->lisp_data)); |
f3700365 | 8206 | |
4ce82fb8 LMI |
8207 | /* If we have an animated image, get the new wand based on the |
8208 | "super-wand". */ | |
6e856b69 | 8209 | if (MagickGetNumberImages (image_wand) > 1) |
4ce82fb8 LMI |
8210 | { |
8211 | MagickWand *super_wand = image_wand; | |
6e856b69 LMI |
8212 | image_wand = imagemagick_compute_animated_image (super_wand, ino); |
8213 | if (! image_wand) | |
8214 | image_wand = super_wand; | |
8215 | else | |
8216 | DestroyMagickWand (super_wand); | |
4ce82fb8 LMI |
8217 | } |
8218 | ||
1b9b4cf4 CY |
8219 | /* Retrieve the frame's background color, for use later. */ |
8220 | { | |
8221 | XColor bgcolor; | |
8222 | Lisp_Object specified_bg; | |
8223 | ||
8224 | specified_bg = image_spec_value (img->spec, QCbackground, NULL); | |
8225 | if (!STRINGP (specified_bg) | |
8226 | || !x_defined_color (f, SSDATA (specified_bg), &bgcolor, 0)) | |
f62bc934 | 8227 | x_query_frame_background_color (f, &bgcolor); |
1b9b4cf4 CY |
8228 | |
8229 | bg_wand = NewPixelWand (); | |
8230 | PixelSetRed (bg_wand, (double) bgcolor.red / 65535); | |
8231 | PixelSetGreen (bg_wand, (double) bgcolor.green / 65535); | |
8232 | PixelSetBlue (bg_wand, (double) bgcolor.blue / 65535); | |
8233 | } | |
8234 | ||
f3f9606c LMI |
8235 | compute_image_size (MagickGetImageWidth (image_wand), |
8236 | MagickGetImageHeight (image_wand), | |
8237 | img->spec, &desired_width, &desired_height); | |
bdf6a35d | 8238 | |
a32d4040 | 8239 | if (desired_width != -1 && desired_height != -1) |
4917006b | 8240 | { |
7574650a AS |
8241 | status = MagickScaleImage (image_wand, desired_width, desired_height); |
8242 | if (status == MagickFalse) | |
8243 | { | |
8244 | image_error ("Imagemagick scale failed", Qnil, Qnil); | |
d1d7b339 | 8245 | imagemagick_error (image_wand); |
7574650a AS |
8246 | goto imagemagick_error; |
8247 | } | |
3de25479 | 8248 | } |
3de25479 | 8249 | |
f3700365 | 8250 | /* crop behaves similar to image slicing in Emacs but is more memory |
7574650a AS |
8251 | efficient. */ |
8252 | crop = image_spec_value (img->spec, QCcrop, NULL); | |
8253 | ||
13464394 | 8254 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop))) |
7574650a | 8255 | { |
a32d4040 CY |
8256 | /* After some testing, it seems MagickCropImage is the fastest crop |
8257 | function in ImageMagick. This crop function seems to do less copying | |
8258 | than the alternatives, but it still reads the entire image into memory | |
13464394 | 8259 | before cropping, which is apparently difficult to avoid when using |
a32d4040 | 8260 | imagemagick. */ |
13464394 | 8261 | size_t crop_width = XINT (XCAR (crop)); |
7574650a | 8262 | crop = XCDR (crop); |
13464394 | 8263 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop))) |
7574650a | 8264 | { |
13464394 | 8265 | size_t crop_height = XINT (XCAR (crop)); |
7574650a | 8266 | crop = XCDR (crop); |
13464394 | 8267 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop))) |
7574650a | 8268 | { |
13464394 | 8269 | ssize_t crop_x = XINT (XCAR (crop)); |
7574650a | 8270 | crop = XCDR (crop); |
13464394 | 8271 | if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop))) |
7574650a | 8272 | { |
13464394 PE |
8273 | ssize_t crop_y = XINT (XCAR (crop)); |
8274 | MagickCropImage (image_wand, crop_width, crop_height, | |
8275 | crop_x, crop_y); | |
7574650a AS |
8276 | } |
8277 | } | |
8278 | } | |
8279 | } | |
8280 | ||
d2a2d277 JV |
8281 | /* Furthermore :rotation. we need background color and angle for |
8282 | rotation. */ | |
3de25479 | 8283 | /* |
d2a2d277 JV |
8284 | TODO background handling for rotation specified_bg = |
8285 | image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP | |
549a73b9 | 8286 | (specified_bg). */ |
4917006b JV |
8287 | value = image_spec_value (img->spec, QCrotation, NULL); |
8288 | if (FLOATP (value)) | |
8289 | { | |
3de25479 | 8290 | rotation = extract_float (value); |
1b9b4cf4 | 8291 | status = MagickRotateImage (image_wand, bg_wand, rotation); |
4917006b JV |
8292 | if (status == MagickFalse) |
8293 | { | |
8294 | image_error ("Imagemagick image rotate failed", Qnil, Qnil); | |
d1d7b339 | 8295 | imagemagick_error (image_wand); |
4917006b JV |
8296 | goto imagemagick_error; |
8297 | } | |
3de25479 | 8298 | } |
7574650a | 8299 | |
1b9b4cf4 CY |
8300 | /* Set the canvas background color to the frame or specified |
8301 | background, and flatten the image. Note: as of ImageMagick | |
8302 | 6.6.0, SVG image transparency is not handled properly | |
8303 | (e.g. etc/images/splash.svg shows a white background always). */ | |
8304 | { | |
8305 | MagickWand *new_wand; | |
8306 | MagickSetImageBackgroundColor (image_wand, bg_wand); | |
0e25d334 | 8307 | #ifdef HAVE_MAGICKMERGEIMAGELAYERS |
1b9b4cf4 | 8308 | new_wand = MagickMergeImageLayers (image_wand, MergeLayer); |
0e25d334 CY |
8309 | #else |
8310 | new_wand = MagickFlattenImages (image_wand); | |
8311 | #endif | |
1b9b4cf4 CY |
8312 | DestroyMagickWand (image_wand); |
8313 | image_wand = new_wand; | |
8314 | } | |
8315 | ||
84ac6f9d PE |
8316 | /* Finally we are done manipulating the image. Figure out the |
8317 | resulting width/height and transfer ownership to Emacs. */ | |
8318 | height = MagickGetImageHeight (image_wand); | |
8319 | width = MagickGetImageWidth (image_wand); | |
8320 | ||
3f791afe PE |
8321 | if (! (width <= INT_MAX && height <= INT_MAX |
8322 | && check_image_size (f, width, height))) | |
3de25479 JV |
8323 | { |
8324 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
8325 | goto imagemagick_error; | |
8326 | } | |
7574650a | 8327 | |
3de25479 JV |
8328 | /* We can now get a valid pixel buffer from the imagemagick file, if all |
8329 | went ok. */ | |
3de25479 JV |
8330 | |
8331 | init_color_table (); | |
af008560 | 8332 | |
cf7a0de6 | 8333 | #if defined (HAVE_MAGICKEXPORTIMAGEPIXELS) && ! defined (HAVE_NS) |
0e25d334 CY |
8334 | if (imagemagick_render_type != 0) |
8335 | { | |
8336 | /* Magicexportimage is normally faster than pixelpushing. This | |
8337 | method is also well tested. Some aspects of this method are | |
8338 | ad-hoc and needs to be more researched. */ | |
8339 | int imagedepth = 24; /*MagickGetImageDepth(image_wand);*/ | |
8340 | const char *exportdepth = imagedepth <= 8 ? "I" : "BGRP"; /*"RGBP";*/ | |
8341 | /* Try to create a x pixmap to hold the imagemagick pixmap. */ | |
547c9269 YM |
8342 | if (!image_create_x_image_and_pixmap (f, img, width, height, imagedepth, |
8343 | &ximg, 0)) | |
0e25d334 CY |
8344 | { |
8345 | #ifdef COLOR_TABLE_SUPPORT | |
8346 | free_color_table (); | |
8347 | #endif | |
8348 | image_error ("Imagemagick X bitmap allocation failure", Qnil, Qnil); | |
8349 | goto imagemagick_error; | |
8350 | } | |
8351 | ||
8352 | /* Oddly, the below code doesn't seem to work:*/ | |
8353 | /* switch(ximg->bitmap_unit){ */ | |
8354 | /* case 8: */ | |
8355 | /* pixelwidth=CharPixel; */ | |
8356 | /* break; */ | |
8357 | /* case 16: */ | |
8358 | /* pixelwidth=ShortPixel; */ | |
8359 | /* break; */ | |
8360 | /* case 32: */ | |
8361 | /* pixelwidth=LongPixel; */ | |
8362 | /* break; */ | |
8363 | /* } */ | |
8364 | /* | |
8365 | Here im just guessing the format of the bitmap. | |
8366 | happens to work fine for: | |
8367 | - bw djvu images | |
8368 | on rgb display. | |
8369 | seems about 3 times as fast as pixel pushing(not carefully measured) | |
8370 | */ | |
8371 | pixelwidth = CharPixel; /*??? TODO figure out*/ | |
8372 | MagickExportImagePixels (image_wand, 0, 0, width, height, | |
8373 | exportdepth, pixelwidth, ximg->data); | |
8374 | } | |
8375 | else | |
8376 | #endif /* HAVE_MAGICKEXPORTIMAGEPIXELS */ | |
4917006b | 8377 | { |
13464394 | 8378 | size_t image_height; |
c24163d4 | 8379 | MagickRealType color_scale = 65535.0 / QuantumRange; |
13464394 | 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, | |
6c596cb2 PE |
8420 | color_scale * pixel.red, |
8421 | color_scale * pixel.green, | |
8422 | color_scale * 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 | } | |
9ab69568 EZ |
8482 | #ifdef WINDOWSNT |
8483 | file = ansi_encode_filename (file); | |
8484 | #endif | |
33290528 | 8485 | success_p = imagemagick_load_image (f, img, 0, 0, SSDATA (file)); |
4917006b JV |
8486 | } |
8487 | /* Else its not a file, its a lisp object. Load the image from a | |
8488 | lisp object rather than a file. */ | |
8489 | else | |
8490 | { | |
8491 | Lisp_Object data; | |
8492 | ||
8493 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
8494 | if (!STRINGP (data)) |
8495 | { | |
8496 | image_error ("Invalid image data `%s'", data, Qnil); | |
8497 | return 0; | |
8498 | } | |
d2a2d277 JV |
8499 | success_p = imagemagick_load_image (f, img, SDATA (data), |
8500 | SBYTES (data), NULL); | |
4917006b JV |
8501 | } |
8502 | ||
8503 | return success_p; | |
8504 | } | |
8505 | ||
a7ca3326 | 8506 | DEFUN ("imagemagick-types", Fimagemagick_types, Simagemagick_types, 0, 0, 0, |
d66c4c7c CY |
8507 | doc: /* Return a list of image types supported by ImageMagick. |
8508 | Each entry in this list is a symbol named after an ImageMagick format | |
8509 | tag. See the ImageMagick manual for a list of ImageMagick formats and | |
8510 | their descriptions (http://www.imagemagick.org/script/formats.php). | |
dd605cc4 | 8511 | You can also try the shell command: `identify -list format'. |
d66c4c7c CY |
8512 | |
8513 | Note that ImageMagick recognizes many file-types that Emacs does not | |
32d72c2f GM |
8514 | recognize as images, such as C. See `imagemagick-types-enable' |
8515 | and `imagemagick-types-inhibit'. */) | |
a8101f66 | 8516 | (void) |
3de25479 JV |
8517 | { |
8518 | Lisp_Object typelist = Qnil; | |
1ab0dee5 | 8519 | size_t numf = 0; |
3de25479 | 8520 | ExceptionInfo ex; |
2277de02 | 8521 | char **imtypes; |
3f791afe | 8522 | size_t i; |
3de25479 | 8523 | Lisp_Object Qimagemagicktype; |
2277de02 JD |
8524 | |
8525 | GetExceptionInfo(&ex); | |
8526 | imtypes = GetMagickList ("*", &numf, &ex); | |
8527 | DestroyExceptionInfo(&ex); | |
8528 | ||
4917006b JV |
8529 | for (i = 0; i < numf; i++) |
8530 | { | |
8531 | Qimagemagicktype = intern (imtypes[i]); | |
f3700365 | 8532 | typelist = Fcons (Qimagemagicktype, typelist); |
ed44be70 | 8533 | imtypes[i] = MagickRelinquishMemory (imtypes[i]); |
f3700365 | 8534 | } |
565935c8 | 8535 | |
ed44be70 | 8536 | MagickRelinquishMemory (imtypes); |
d1d7b339 | 8537 | return Fnreverse (typelist); |
3de25479 | 8538 | } |
7574650a | 8539 | |
3de25479 JV |
8540 | #endif /* defined (HAVE_IMAGEMAGICK) */ |
8541 | ||
8542 | ||
4ef0d4d0 | 8543 | \f |
4ab27a43 GM |
8544 | /*********************************************************************** |
8545 | SVG | |
8546 | ***********************************************************************/ | |
8547 | ||
8548 | #if defined (HAVE_RSVG) | |
8549 | ||
8550 | /* Function prototypes. */ | |
8551 | ||
578098f3 PE |
8552 | static bool svg_image_p (Lisp_Object object); |
8553 | static bool svg_load (struct frame *f, struct image *img); | |
4ab27a43 | 8554 | |
578098f3 PE |
8555 | static bool svg_load_image (struct frame *, struct image *, |
8556 | unsigned char *, ptrdiff_t); | |
4ab27a43 GM |
8557 | |
8558 | /* The symbol `svg' identifying images of this type. */ | |
8559 | ||
4b66faf3 | 8560 | static Lisp_Object Qsvg; |
4ab27a43 GM |
8561 | |
8562 | /* Indices of image specification fields in svg_format, below. */ | |
8563 | ||
8564 | enum svg_keyword_index | |
8565 | { | |
8566 | SVG_TYPE, | |
8567 | SVG_DATA, | |
8568 | SVG_FILE, | |
8569 | SVG_ASCENT, | |
8570 | SVG_MARGIN, | |
8571 | SVG_RELIEF, | |
8572 | SVG_ALGORITHM, | |
8573 | SVG_HEURISTIC_MASK, | |
8574 | SVG_MASK, | |
8575 | SVG_BACKGROUND, | |
8576 | SVG_LAST | |
8577 | }; | |
8578 | ||
8579 | /* Vector of image_keyword structures describing the format | |
8580 | of valid user-defined image specifications. */ | |
8581 | ||
91433552 | 8582 | static const struct image_keyword svg_format[SVG_LAST] = |
4ab27a43 GM |
8583 | { |
8584 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8585 | {":data", IMAGE_STRING_VALUE, 0}, | |
8586 | {":file", IMAGE_STRING_VALUE, 0}, | |
8587 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 8588 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ab27a43 GM |
8589 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
8590 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8591 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8592 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8593 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8594 | }; | |
8595 | ||
33d4113c | 8596 | #if defined HAVE_NTGUI && defined WINDOWSNT |
578098f3 | 8597 | static bool init_svg_functions (void); |
bb4d86b4 CY |
8598 | #else |
8599 | #define init_svg_functions NULL | |
8600 | #endif | |
8601 | ||
4ab27a43 GM |
8602 | /* Structure describing the image type `svg'. Its the same type of |
8603 | structure defined for all image formats, handled by emacs image | |
8604 | functions. See struct image_type in dispextern.h. */ | |
8605 | ||
8606 | static struct image_type svg_type = | |
8607 | { | |
4ab27a43 | 8608 | &Qsvg, |
4ab27a43 | 8609 | svg_image_p, |
4ab27a43 | 8610 | svg_load, |
4ab27a43 | 8611 | x_clear_image, |
bb4d86b4 | 8612 | init_svg_functions, |
c21e6151 | 8613 | NULL |
4ab27a43 GM |
8614 | }; |
8615 | ||
8616 | ||
578098f3 | 8617 | /* Return true if OBJECT is a valid SVG image specification. Do |
4ab27a43 GM |
8618 | this by calling parse_image_spec and supplying the keywords that |
8619 | identify the SVG format. */ | |
8620 | ||
578098f3 | 8621 | static bool |
d3da34e0 | 8622 | svg_image_p (Lisp_Object object) |
4ab27a43 GM |
8623 | { |
8624 | struct image_keyword fmt[SVG_LAST]; | |
72af86bd | 8625 | memcpy (fmt, svg_format, sizeof fmt); |
4ab27a43 GM |
8626 | |
8627 | if (!parse_image_spec (object, fmt, SVG_LAST, Qsvg)) | |
8628 | return 0; | |
8629 | ||
8630 | /* Must specify either the :data or :file keyword. */ | |
8631 | return fmt[SVG_FILE].count + fmt[SVG_DATA].count == 1; | |
8632 | } | |
8633 | ||
8634 | #include <librsvg/rsvg.h> | |
8635 | ||
0fda9b75 | 8636 | #ifdef WINDOWSNT |
5fc9fdeb JR |
8637 | |
8638 | /* SVG library functions. */ | |
40693bba | 8639 | DEF_IMGLIB_FN (RsvgHandle *, rsvg_handle_new, (void)); |
8640 | DEF_IMGLIB_FN (void, rsvg_handle_get_dimensions, (RsvgHandle *, RsvgDimensionData *)); | |
8641 | DEF_IMGLIB_FN (gboolean, rsvg_handle_write, (RsvgHandle *, const guchar *, gsize, GError **)); | |
8642 | DEF_IMGLIB_FN (gboolean, rsvg_handle_close, (RsvgHandle *, GError **)); | |
8643 | DEF_IMGLIB_FN (GdkPixbuf *, rsvg_handle_get_pixbuf, (RsvgHandle *)); | |
40693bba | 8644 | |
8645 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_width, (const GdkPixbuf *)); | |
8646 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_height, (const GdkPixbuf *)); | |
8647 | DEF_IMGLIB_FN (guchar *, gdk_pixbuf_get_pixels, (const GdkPixbuf *)); | |
8648 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_rowstride, (const GdkPixbuf *)); | |
8649 | DEF_IMGLIB_FN (GdkColorspace, gdk_pixbuf_get_colorspace, (const GdkPixbuf *)); | |
8650 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_n_channels, (const GdkPixbuf *)); | |
8651 | DEF_IMGLIB_FN (gboolean, gdk_pixbuf_get_has_alpha, (const GdkPixbuf *)); | |
8652 | DEF_IMGLIB_FN (int, gdk_pixbuf_get_bits_per_sample, (const GdkPixbuf *)); | |
8653 | ||
19452b86 | 8654 | #if ! GLIB_CHECK_VERSION (2, 36, 0) |
40693bba | 8655 | DEF_IMGLIB_FN (void, g_type_init, (void)); |
19452b86 | 8656 | #endif |
40693bba | 8657 | DEF_IMGLIB_FN (void, g_object_unref, (gpointer)); |
121ab1cd | 8658 | DEF_IMGLIB_FN (void, g_error_free, (GError *)); |
5fc9fdeb | 8659 | |
df23bf08 | 8660 | Lisp_Object Qgdk_pixbuf, Qglib, Qgobject; |
5fc9fdeb | 8661 | |
578098f3 | 8662 | static bool |
d07ff9db | 8663 | init_svg_functions (void) |
5fc9fdeb | 8664 | { |
b9b37dd0 | 8665 | HMODULE library, gdklib = NULL, glib = NULL, gobject = NULL; |
5fc9fdeb | 8666 | |
d07ff9db CY |
8667 | if (!(glib = w32_delayed_load (Qglib)) |
8668 | || !(gobject = w32_delayed_load (Qgobject)) | |
8669 | || !(gdklib = w32_delayed_load (Qgdk_pixbuf)) | |
8670 | || !(library = w32_delayed_load (Qsvg))) | |
b9b37dd0 JB |
8671 | { |
8672 | if (gdklib) FreeLibrary (gdklib); | |
8673 | if (gobject) FreeLibrary (gobject); | |
8674 | if (glib) FreeLibrary (glib); | |
8675 | return 0; | |
8676 | } | |
5fc9fdeb JR |
8677 | |
8678 | LOAD_IMGLIB_FN (library, rsvg_handle_new); | |
e025c9c2 | 8679 | LOAD_IMGLIB_FN (library, rsvg_handle_get_dimensions); |
5fc9fdeb JR |
8680 | LOAD_IMGLIB_FN (library, rsvg_handle_write); |
8681 | LOAD_IMGLIB_FN (library, rsvg_handle_close); | |
8682 | LOAD_IMGLIB_FN (library, rsvg_handle_get_pixbuf); | |
5fc9fdeb JR |
8683 | |
8684 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_width); | |
8685 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_height); | |
8686 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_pixels); | |
8687 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_rowstride); | |
8688 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_colorspace); | |
8689 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_n_channels); | |
8690 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_has_alpha); | |
8691 | LOAD_IMGLIB_FN (gdklib, gdk_pixbuf_get_bits_per_sample); | |
8692 | ||
19452b86 | 8693 | #if ! GLIB_CHECK_VERSION (2, 36, 0) |
df23bf08 | 8694 | LOAD_IMGLIB_FN (gobject, g_type_init); |
19452b86 | 8695 | #endif |
df23bf08 | 8696 | LOAD_IMGLIB_FN (gobject, g_object_unref); |
5fc9fdeb | 8697 | LOAD_IMGLIB_FN (glib, g_error_free); |
df23bf08 | 8698 | |
5fc9fdeb JR |
8699 | return 1; |
8700 | } | |
4ab27a43 | 8701 | |
5fc9fdeb JR |
8702 | #else |
8703 | /* The following aliases for library functions allow dynamic loading | |
8704 | to be used on some platforms. */ | |
4ab27a43 | 8705 | #define fn_rsvg_handle_new rsvg_handle_new |
e025c9c2 | 8706 | #define fn_rsvg_handle_get_dimensions rsvg_handle_get_dimensions |
4ab27a43 GM |
8707 | #define fn_rsvg_handle_write rsvg_handle_write |
8708 | #define fn_rsvg_handle_close rsvg_handle_close | |
8709 | #define fn_rsvg_handle_get_pixbuf rsvg_handle_get_pixbuf | |
4ab27a43 | 8710 | |
5fc9fdeb JR |
8711 | #define fn_gdk_pixbuf_get_width gdk_pixbuf_get_width |
8712 | #define fn_gdk_pixbuf_get_height gdk_pixbuf_get_height | |
8713 | #define fn_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels | |
8714 | #define fn_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride | |
8715 | #define fn_gdk_pixbuf_get_colorspace gdk_pixbuf_get_colorspace | |
8716 | #define fn_gdk_pixbuf_get_n_channels gdk_pixbuf_get_n_channels | |
8717 | #define fn_gdk_pixbuf_get_has_alpha gdk_pixbuf_get_has_alpha | |
4ab27a43 GM |
8718 | #define fn_gdk_pixbuf_get_bits_per_sample gdk_pixbuf_get_bits_per_sample |
8719 | ||
19452b86 | 8720 | #if ! GLIB_CHECK_VERSION (2, 36, 0) |
5fc9fdeb | 8721 | #define fn_g_type_init g_type_init |
19452b86 | 8722 | #endif |
5fc9fdeb JR |
8723 | #define fn_g_object_unref g_object_unref |
8724 | #define fn_g_error_free g_error_free | |
0fda9b75 | 8725 | #endif /* !WINDOWSNT */ |
4ab27a43 | 8726 | |
578098f3 PE |
8727 | /* Load SVG image IMG for use on frame F. Value is true if |
8728 | successful. */ | |
4ab27a43 | 8729 | |
578098f3 | 8730 | static bool |
d3da34e0 | 8731 | svg_load (struct frame *f, struct image *img) |
4ab27a43 | 8732 | { |
578098f3 | 8733 | bool success_p = 0; |
4ab27a43 GM |
8734 | Lisp_Object file_name; |
8735 | ||
8736 | /* If IMG->spec specifies a file name, create a non-file spec from it. */ | |
8737 | file_name = image_spec_value (img->spec, QCfile, NULL); | |
8738 | if (STRINGP (file_name)) | |
8739 | { | |
8740 | Lisp_Object file; | |
8741 | unsigned char *contents; | |
dd52fcea | 8742 | ptrdiff_t size; |
4ab27a43 GM |
8743 | |
8744 | file = x_find_image_file (file_name); | |
4ab27a43 | 8745 | if (!STRINGP (file)) |
c21e6151 YM |
8746 | { |
8747 | image_error ("Cannot find image file `%s'", file_name, Qnil); | |
c21e6151 YM |
8748 | return 0; |
8749 | } | |
8750 | ||
4ab27a43 | 8751 | /* Read the entire file into memory. */ |
33290528 | 8752 | contents = slurp_file (SSDATA (file), &size); |
4ab27a43 | 8753 | if (contents == NULL) |
c21e6151 YM |
8754 | { |
8755 | image_error ("Error loading SVG image `%s'", img->spec, Qnil); | |
c21e6151 YM |
8756 | return 0; |
8757 | } | |
4ab27a43 | 8758 | /* If the file was slurped into memory properly, parse it. */ |
c21e6151 | 8759 | success_p = svg_load_image (f, img, contents, size); |
4ab27a43 | 8760 | xfree (contents); |
4ab27a43 GM |
8761 | } |
8762 | /* Else its not a file, its a lisp object. Load the image from a | |
8763 | lisp object rather than a file. */ | |
8764 | else | |
8765 | { | |
8766 | Lisp_Object data; | |
8767 | ||
8768 | data = image_spec_value (img->spec, QCdata, NULL); | |
7574650a AS |
8769 | if (!STRINGP (data)) |
8770 | { | |
8771 | image_error ("Invalid image data `%s'", data, Qnil); | |
8772 | return 0; | |
8773 | } | |
4ab27a43 GM |
8774 | success_p = svg_load_image (f, img, SDATA (data), SBYTES (data)); |
8775 | } | |
8776 | ||
8777 | return success_p; | |
8778 | } | |
8779 | ||
bbe6f2aa JB |
8780 | /* svg_load_image is a helper function for svg_load, which does the |
8781 | actual loading given contents and size, apart from frame and image | |
8782 | structures, passed from svg_load. | |
4ab27a43 | 8783 | |
bbe6f2aa | 8784 | Uses librsvg to do most of the image processing. |
c21e6151 | 8785 | |
578098f3 PE |
8786 | Returns true when successful. */ |
8787 | static bool | |
d3da34e0 JB |
8788 | svg_load_image (struct frame *f, /* Pointer to emacs frame structure. */ |
8789 | struct image *img, /* Pointer to emacs image structure. */ | |
8790 | unsigned char *contents, /* String containing the SVG XML data to be parsed. */ | |
dd52fcea | 8791 | ptrdiff_t size) /* Size of data in bytes. */ |
4ab27a43 GM |
8792 | { |
8793 | RsvgHandle *rsvg_handle; | |
e025c9c2 | 8794 | RsvgDimensionData dimension_data; |
33290528 | 8795 | GError *err = NULL; |
4ab27a43 GM |
8796 | GdkPixbuf *pixbuf; |
8797 | int width; | |
8798 | int height; | |
8799 | const guint8 *pixels; | |
8800 | int rowstride; | |
8801 | XImagePtr ximg; | |
c21e6151 | 8802 | Lisp_Object specified_bg; |
4ab27a43 GM |
8803 | XColor background; |
8804 | int x; | |
8805 | int y; | |
8806 | ||
19452b86 DA |
8807 | #if ! GLIB_CHECK_VERSION (2, 36, 0) |
8808 | /* g_type_init is a glib function that must be called prior to | |
8809 | using gnome type library functions (obsolete since 2.36.0). */ | |
5fc9fdeb | 8810 | fn_g_type_init (); |
19452b86 DA |
8811 | #endif |
8812 | ||
4ab27a43 | 8813 | /* Make a handle to a new rsvg object. */ |
df61c790 | 8814 | rsvg_handle = fn_rsvg_handle_new (); |
4ab27a43 GM |
8815 | |
8816 | /* Parse the contents argument and fill in the rsvg_handle. */ | |
33290528 PE |
8817 | fn_rsvg_handle_write (rsvg_handle, contents, size, &err); |
8818 | if (err) goto rsvg_error; | |
4ab27a43 GM |
8819 | |
8820 | /* The parsing is complete, rsvg_handle is ready to used, close it | |
8821 | for further writes. */ | |
33290528 PE |
8822 | fn_rsvg_handle_close (rsvg_handle, &err); |
8823 | if (err) goto rsvg_error; | |
e025c9c2 CY |
8824 | |
8825 | fn_rsvg_handle_get_dimensions (rsvg_handle, &dimension_data); | |
8826 | if (! check_image_size (f, dimension_data.width, dimension_data.height)) | |
10ea2b82 JR |
8827 | { |
8828 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); | |
8829 | goto rsvg_error; | |
8830 | } | |
b2996e57 | 8831 | |
4ab27a43 GM |
8832 | /* We can now get a valid pixel buffer from the svg file, if all |
8833 | went ok. */ | |
df61c790 | 8834 | pixbuf = fn_rsvg_handle_get_pixbuf (rsvg_handle); |
e025c9c2 CY |
8835 | if (!pixbuf) goto rsvg_error; |
8836 | fn_g_object_unref (rsvg_handle); | |
4ab27a43 GM |
8837 | |
8838 | /* Extract some meta data from the svg handle. */ | |
8839 | width = fn_gdk_pixbuf_get_width (pixbuf); | |
8840 | height = fn_gdk_pixbuf_get_height (pixbuf); | |
df61c790 | 8841 | pixels = fn_gdk_pixbuf_get_pixels (pixbuf); |
4ab27a43 GM |
8842 | rowstride = fn_gdk_pixbuf_get_rowstride (pixbuf); |
8843 | ||
8844 | /* Validate the svg meta data. */ | |
8845 | eassert (fn_gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB); | |
8846 | eassert (fn_gdk_pixbuf_get_n_channels (pixbuf) == 4); | |
8847 | eassert (fn_gdk_pixbuf_get_has_alpha (pixbuf)); | |
8848 | eassert (fn_gdk_pixbuf_get_bits_per_sample (pixbuf) == 8); | |
8849 | ||
8850 | /* Try to create a x pixmap to hold the svg pixmap. */ | |
547c9269 | 8851 | if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0)) |
c21e6151 | 8852 | { |
5fc9fdeb | 8853 | fn_g_object_unref (pixbuf); |
c21e6151 YM |
8854 | return 0; |
8855 | } | |
4ab27a43 GM |
8856 | |
8857 | init_color_table (); | |
8858 | ||
c21e6151 YM |
8859 | /* Handle alpha channel by combining the image with a background |
8860 | color. */ | |
8861 | specified_bg = image_spec_value (img->spec, QCbackground, NULL); | |
8d3c4e79 | 8862 | if (!STRINGP (specified_bg) |
33290528 | 8863 | || !x_defined_color (f, SSDATA (specified_bg), &background, 0)) |
f62bc934 | 8864 | x_query_frame_background_color (f, &background); |
4ab27a43 | 8865 | |
8d3c4e79 CY |
8866 | /* SVG pixmaps specify transparency in the last byte, so right |
8867 | shift 8 bits to get rid of it, since emacs doesn't support | |
8868 | transparency. */ | |
8869 | background.red >>= 8; | |
8870 | background.green >>= 8; | |
8871 | background.blue >>= 8; | |
8872 | ||
4ab27a43 GM |
8873 | /* This loop handles opacity values, since Emacs assumes |
8874 | non-transparent images. Each pixel must be "flattened" by | |
bbe6f2aa JB |
8875 | calculating the resulting color, given the transparency of the |
8876 | pixel, and the image background color. */ | |
4ab27a43 GM |
8877 | for (y = 0; y < height; ++y) |
8878 | { | |
8879 | for (x = 0; x < width; ++x) | |
8880 | { | |
13464394 PE |
8881 | int red; |
8882 | int green; | |
8883 | int blue; | |
8884 | int opacity; | |
4ab27a43 GM |
8885 | |
8886 | red = *pixels++; | |
8887 | green = *pixels++; | |
8888 | blue = *pixels++; | |
8889 | opacity = *pixels++; | |
8890 | ||
8891 | red = ((red * opacity) | |
8892 | + (background.red * ((1 << 8) - opacity))); | |
8893 | green = ((green * opacity) | |
8894 | + (background.green * ((1 << 8) - opacity))); | |
8895 | blue = ((blue * opacity) | |
8896 | + (background.blue * ((1 << 8) - opacity))); | |
8897 | ||
8898 | XPutPixel (ximg, x, y, lookup_rgb_color (f, red, green, blue)); | |
8899 | } | |
8900 | ||
8901 | pixels += rowstride - 4 * width; | |
8902 | } | |
8903 | ||
8904 | #ifdef COLOR_TABLE_SUPPORT | |
8905 | /* Remember colors allocated for this image. */ | |
8906 | img->colors = colors_in_color_table (&img->ncolors); | |
8907 | free_color_table (); | |
8908 | #endif /* COLOR_TABLE_SUPPORT */ | |
8909 | ||
5fc9fdeb | 8910 | fn_g_object_unref (pixbuf); |
4ab27a43 | 8911 | |
c21e6151 YM |
8912 | img->width = width; |
8913 | img->height = height; | |
8914 | ||
8915 | /* Maybe fill in the background field while we have ximg handy. | |
8916 | Casting avoids a GCC warning. */ | |
8917 | IMAGE_BACKGROUND (img, f, (XImagePtr_or_DC)ximg); | |
8918 | ||
547c9269 YM |
8919 | /* Put ximg into the image. */ |
8920 | image_put_x_image (f, img, ximg, 0); | |
4ab27a43 | 8921 | |
4ab27a43 GM |
8922 | return 1; |
8923 | ||
8924 | rsvg_error: | |
e025c9c2 | 8925 | fn_g_object_unref (rsvg_handle); |
4ab27a43 GM |
8926 | /* FIXME: Use error->message so the user knows what is the actual |
8927 | problem with the image. */ | |
8928 | image_error ("Error parsing SVG image `%s'", img->spec, Qnil); | |
33290528 | 8929 | fn_g_error_free (err); |
4ab27a43 GM |
8930 | return 0; |
8931 | } | |
8932 | ||
8933 | #endif /* defined (HAVE_RSVG) */ | |
8934 | ||
8935 | ||
8936 | ||
4ef0d4d0 KS |
8937 | \f |
8938 | /*********************************************************************** | |
8939 | Ghostscript | |
8940 | ***********************************************************************/ | |
8941 | ||
8942 | #ifdef HAVE_X_WINDOWS | |
8943 | #define HAVE_GHOSTSCRIPT 1 | |
8944 | #endif /* HAVE_X_WINDOWS */ | |
8945 | ||
4ef0d4d0 KS |
8946 | #ifdef HAVE_GHOSTSCRIPT |
8947 | ||
578098f3 PE |
8948 | static bool gs_image_p (Lisp_Object object); |
8949 | static bool gs_load (struct frame *f, struct image *img); | |
f57e2426 | 8950 | static void gs_clear_image (struct frame *f, struct image *img); |
4ef0d4d0 KS |
8951 | |
8952 | /* Keyword symbols. */ | |
8953 | ||
955cbe7b | 8954 | static Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height; |
4ef0d4d0 KS |
8955 | |
8956 | /* Indices of image specification fields in gs_format, below. */ | |
8957 | ||
8958 | enum gs_keyword_index | |
8959 | { | |
8960 | GS_TYPE, | |
8961 | GS_PT_WIDTH, | |
8962 | GS_PT_HEIGHT, | |
8963 | GS_FILE, | |
8964 | GS_LOADER, | |
8965 | GS_BOUNDING_BOX, | |
8966 | GS_ASCENT, | |
8967 | GS_MARGIN, | |
8968 | GS_RELIEF, | |
8969 | GS_ALGORITHM, | |
8970 | GS_HEURISTIC_MASK, | |
8971 | GS_MASK, | |
8972 | GS_BACKGROUND, | |
8973 | GS_LAST | |
8974 | }; | |
8975 | ||
8976 | /* Vector of image_keyword structures describing the format | |
8977 | of valid user-defined image specifications. */ | |
8978 | ||
91433552 | 8979 | static const struct image_keyword gs_format[GS_LAST] = |
4ef0d4d0 KS |
8980 | { |
8981 | {":type", IMAGE_SYMBOL_VALUE, 1}, | |
8982 | {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8983 | {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1}, | |
8984 | {":file", IMAGE_STRING_VALUE, 1}, | |
8985 | {":loader", IMAGE_FUNCTION_VALUE, 0}, | |
8986 | {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1}, | |
8987 | {":ascent", IMAGE_ASCENT_VALUE, 0}, | |
c4a07a4c | 8988 | {":margin", IMAGE_NON_NEGATIVE_INTEGER_VALUE_OR_PAIR, 0}, |
4ef0d4d0 KS |
8989 | {":relief", IMAGE_INTEGER_VALUE, 0}, |
8990 | {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8991 | {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8992 | {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}, | |
8993 | {":background", IMAGE_STRING_OR_NIL_VALUE, 0} | |
8994 | }; | |
8995 | ||
8996 | /* Structure describing the image type `ghostscript'. */ | |
8997 | ||
8998 | static struct image_type gs_type = | |
8999 | { | |
9000 | &Qpostscript, | |
9001 | gs_image_p, | |
9002 | gs_load, | |
9003 | gs_clear_image, | |
d07ff9db | 9004 | NULL, |
4ef0d4d0 KS |
9005 | NULL |
9006 | }; | |
9007 | ||
9008 | ||
9009 | /* Free X resources of Ghostscript image IMG which is used on frame F. */ | |
9010 | ||
9011 | static void | |
d3da34e0 | 9012 | gs_clear_image (struct frame *f, struct image *img) |
4ef0d4d0 | 9013 | { |
4ef0d4d0 KS |
9014 | x_clear_image (f, img); |
9015 | } | |
9016 | ||
9017 | ||
578098f3 | 9018 | /* Return true if OBJECT is a valid Ghostscript image |
4ef0d4d0 KS |
9019 | specification. */ |
9020 | ||
578098f3 | 9021 | static bool |
d3da34e0 | 9022 | gs_image_p (Lisp_Object object) |
4ef0d4d0 KS |
9023 | { |
9024 | struct image_keyword fmt[GS_LAST]; | |
9025 | Lisp_Object tem; | |
9026 | int i; | |
9027 | ||
72af86bd | 9028 | memcpy (fmt, gs_format, sizeof fmt); |
4ef0d4d0 KS |
9029 | |
9030 | if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)) | |
9031 | return 0; | |
9032 | ||
9033 | /* Bounding box must be a list or vector containing 4 integers. */ | |
9034 | tem = fmt[GS_BOUNDING_BOX].value; | |
9035 | if (CONSP (tem)) | |
9036 | { | |
9037 | for (i = 0; i < 4; ++i, tem = XCDR (tem)) | |
9038 | if (!CONSP (tem) || !INTEGERP (XCAR (tem))) | |
9039 | return 0; | |
9040 | if (!NILP (tem)) | |
9041 | return 0; | |
9042 | } | |
9043 | else if (VECTORP (tem)) | |
9044 | { | |
77b37c05 | 9045 | if (ASIZE (tem) != 4) |
4ef0d4d0 KS |
9046 | return 0; |
9047 | for (i = 0; i < 4; ++i) | |
28be1ada | 9048 | if (!INTEGERP (AREF (tem, i))) |
4ef0d4d0 KS |
9049 | return 0; |
9050 | } | |
9051 | else | |
9052 | return 0; | |
9053 | ||
9054 | return 1; | |
9055 | } | |
9056 | ||
9057 | ||
578098f3 | 9058 | /* Load Ghostscript image IMG for use on frame F. Value is true |
4ef0d4d0 KS |
9059 | if successful. */ |
9060 | ||
578098f3 | 9061 | static bool |
d3da34e0 | 9062 | gs_load (struct frame *f, struct image *img) |
4ef0d4d0 | 9063 | { |
13464394 PE |
9064 | uprintmax_t printnum1, printnum2; |
9065 | char buffer[sizeof " " + INT_STRLEN_BOUND (printmax_t)]; | |
4ef0d4d0 | 9066 | Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width; |
4ef0d4d0 KS |
9067 | Lisp_Object frame; |
9068 | double in_width, in_height; | |
9069 | Lisp_Object pixel_colors = Qnil; | |
9070 | ||
9071 | /* Compute pixel size of pixmap needed from the given size in the | |
9072 | image specification. Sizes in the specification are in pt. 1 pt | |
9073 | = 1/72 in, xdpi and ydpi are stored in the frame's X display | |
9074 | info. */ | |
9075 | pt_width = image_spec_value (img->spec, QCpt_width, NULL); | |
7574650a | 9076 | in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0; |
42143acd | 9077 | in_width *= FRAME_RES_X (f); |
4ef0d4d0 | 9078 | pt_height = image_spec_value (img->spec, QCpt_height, NULL); |
7574650a | 9079 | in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0; |
42143acd | 9080 | in_height *= FRAME_RES_Y (f); |
4ef0d4d0 | 9081 | |
13464394 PE |
9082 | if (! (in_width <= INT_MAX && in_height <= INT_MAX |
9083 | && check_image_size (f, in_width, in_height))) | |
f1f25b99 | 9084 | { |
10ea2b82 | 9085 | image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); |
f1f25b99 CY |
9086 | return 0; |
9087 | } | |
13464394 PE |
9088 | img->width = in_width; |
9089 | img->height = in_height; | |
f1f25b99 | 9090 | |
4ef0d4d0 | 9091 | /* Create the pixmap. */ |
a54e2c05 | 9092 | eassert (img->pixmap == NO_PIXMAP); |
4ef0d4d0 | 9093 | |
ca4aa935 PE |
9094 | if (x_check_image_size (0, img->width, img->height)) |
9095 | { | |
9096 | /* Only W32 version did BLOCK_INPUT here. ++kfs */ | |
4d7e6e51 | 9097 | block_input (); |
ca4aa935 PE |
9098 | img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
9099 | img->width, img->height, | |
9100 | DefaultDepthOfScreen (FRAME_X_SCREEN (f))); | |
4d7e6e51 | 9101 | unblock_input (); |
ca4aa935 | 9102 | } |
4ef0d4d0 KS |
9103 | |
9104 | if (!img->pixmap) | |
9105 | { | |
9106 | image_error ("Unable to create pixmap for `%s'", img->spec, Qnil); | |
9107 | return 0; | |
9108 | } | |
9109 | ||
9110 | /* Call the loader to fill the pixmap. It returns a process object | |
9111 | if successful. We do not record_unwind_protect here because | |
9112 | other places in redisplay like calling window scroll functions | |
9113 | don't either. Let the Lisp loader use `unwind-protect' instead. */ | |
13464394 PE |
9114 | printnum1 = FRAME_X_WINDOW (f); |
9115 | printnum2 = img->pixmap; | |
620f13b0 | 9116 | window_and_pixmap_id |
a8290ec3 | 9117 | = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2); |
4ef0d4d0 | 9118 | |
13464394 PE |
9119 | printnum1 = FRAME_FOREGROUND_PIXEL (f); |
9120 | printnum2 = FRAME_BACKGROUND_PIXEL (f); | |
a8290ec3 DA |
9121 | pixel_colors |
9122 | = make_formatted_string (buffer, "%"pMu" %"pMu, printnum1, printnum2); | |
4ef0d4d0 KS |
9123 | |
9124 | XSETFRAME (frame, f); | |
9125 | loader = image_spec_value (img->spec, QCloader, NULL); | |
9126 | if (NILP (loader)) | |
9127 | loader = intern ("gs-load-image"); | |
9128 | ||
b50691aa CY |
9129 | img->lisp_data = call6 (loader, frame, img->spec, |
9130 | make_number (img->width), | |
9131 | make_number (img->height), | |
9132 | window_and_pixmap_id, | |
9133 | pixel_colors); | |
9134 | return PROCESSP (img->lisp_data); | |
4ef0d4d0 KS |
9135 | } |
9136 | ||
9137 | ||
9138 | /* Kill the Ghostscript process that was started to fill PIXMAP on | |
9139 | frame F. Called from XTread_socket when receiving an event | |
9140 | telling Emacs that Ghostscript has finished drawing. */ | |
9141 | ||
9142 | void | |
d3da34e0 | 9143 | x_kill_gs_process (Pixmap pixmap, struct frame *f) |
4ef0d4d0 | 9144 | { |
354884c4 | 9145 | struct image_cache *c = FRAME_IMAGE_CACHE (f); |
13464394 PE |
9146 | int class; |
9147 | ptrdiff_t i; | |
4ef0d4d0 KS |
9148 | struct image *img; |
9149 | ||
9150 | /* Find the image containing PIXMAP. */ | |
9151 | for (i = 0; i < c->used; ++i) | |
9152 | if (c->images[i]->pixmap == pixmap) | |
9153 | break; | |
9154 | ||
9155 | /* Should someone in between have cleared the image cache, for | |
9156 | instance, give up. */ | |
9157 | if (i == c->used) | |
9158 | return; | |
9159 | ||
9160 | /* Kill the GS process. We should have found PIXMAP in the image | |
9161 | cache and its image should contain a process object. */ | |
9162 | img = c->images[i]; | |
a54e2c05 | 9163 | eassert (PROCESSP (img->lisp_data)); |
b50691aa CY |
9164 | Fkill_process (img->lisp_data, Qnil); |
9165 | img->lisp_data = Qnil; | |
4ef0d4d0 KS |
9166 | |
9167 | #if defined (HAVE_X_WINDOWS) | |
9168 | ||
9169 | /* On displays with a mutable colormap, figure out the colors | |
9170 | allocated for the image by looking at the pixels of an XImage for | |
9171 | img->pixmap. */ | |
9172 | class = FRAME_X_VISUAL (f)->class; | |
9173 | if (class != StaticColor && class != StaticGray && class != TrueColor) | |
9174 | { | |
9175 | XImagePtr ximg; | |
9176 | ||
4d7e6e51 | 9177 | block_input (); |
4ef0d4d0 KS |
9178 | |
9179 | /* Try to get an XImage for img->pixmep. */ | |
9180 | ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap, | |
9181 | 0, 0, img->width, img->height, ~0, ZPixmap); | |
9182 | if (ximg) | |
9183 | { | |
9184 | int x, y; | |
9185 | ||
9186 | /* Initialize the color table. */ | |
9187 | init_color_table (); | |
9188 | ||
9189 | /* For each pixel of the image, look its color up in the | |
9190 | color table. After having done so, the color table will | |
9191 | contain an entry for each color used by the image. */ | |
9192 | for (y = 0; y < img->height; ++y) | |
9193 | for (x = 0; x < img->width; ++x) | |
9194 | { | |
9195 | unsigned long pixel = XGetPixel (ximg, x, y); | |
9196 | lookup_pixel_color (f, pixel); | |
9197 | } | |
9198 | ||
9199 | /* Record colors in the image. Free color table and XImage. */ | |
9200 | #ifdef COLOR_TABLE_SUPPORT | |
9201 | img->colors = colors_in_color_table (&img->ncolors); | |
9202 | free_color_table (); | |
9203 | #endif | |
9204 | XDestroyImage (ximg); | |
9205 | ||
9206 | #if 0 /* This doesn't seem to be the case. If we free the colors | |
9207 | here, we get a BadAccess later in x_clear_image when | |
9208 | freeing the colors. */ | |
9209 | /* We have allocated colors once, but Ghostscript has also | |
9210 | allocated colors on behalf of us. So, to get the | |
9211 | reference counts right, free them once. */ | |
9212 | if (img->ncolors) | |
9213 | x_free_colors (f, img->colors, img->ncolors); | |
9214 | #endif | |
9215 | } | |
9216 | else | |
9217 | image_error ("Cannot get X image of `%s'; colors will not be freed", | |
9218 | img->spec, Qnil); | |
9219 | ||
4d7e6e51 | 9220 | unblock_input (); |
4ef0d4d0 KS |
9221 | } |
9222 | #endif /* HAVE_X_WINDOWS */ | |
9223 | ||
9224 | /* Now that we have the pixmap, compute mask and transform the | |
9225 | image if requested. */ | |
4d7e6e51 | 9226 | block_input (); |
4ef0d4d0 | 9227 | postprocess_image (f, img); |
4d7e6e51 | 9228 | unblock_input (); |
4ef0d4d0 KS |
9229 | } |
9230 | ||
9231 | #endif /* HAVE_GHOSTSCRIPT */ | |
9232 | ||
9233 | \f | |
9234 | /*********************************************************************** | |
9235 | Tests | |
9236 | ***********************************************************************/ | |
9237 | ||
e509cfa6 | 9238 | #ifdef GLYPH_DEBUG |
4ef0d4d0 | 9239 | |
a7ca3326 | 9240 | DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0, |
4ef0d4d0 | 9241 | doc: /* Value is non-nil if SPEC is a valid image specification. */) |
5842a27b | 9242 | (Lisp_Object spec) |
4ef0d4d0 KS |
9243 | { |
9244 | return valid_image_p (spec) ? Qt : Qnil; | |
9245 | } | |
9246 | ||
9247 | ||
a7ca3326 | 9248 | DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "") |
5842a27b | 9249 | (Lisp_Object spec) |
4ef0d4d0 | 9250 | { |
13464394 | 9251 | ptrdiff_t id = -1; |
4ef0d4d0 KS |
9252 | |
9253 | if (valid_image_p (spec)) | |
9254 | id = lookup_image (SELECTED_FRAME (), spec); | |
9255 | ||
9256 | debug_print (spec); | |
9257 | return make_number (id); | |
9258 | } | |
9259 | ||
e509cfa6 | 9260 | #endif /* GLYPH_DEBUG */ |
4ef0d4d0 KS |
9261 | |
9262 | ||
9263 | /*********************************************************************** | |
9264 | Initialization | |
9265 | ***********************************************************************/ | |
9266 | ||
d07ff9db | 9267 | DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 1, 1, 0, |
0855eb52 JB |
9268 | doc: /* Initialize image library implementing image type TYPE. |
9269 | Return non-nil if TYPE is a supported image type. | |
9270 | ||
d07ff9db CY |
9271 | If image libraries are loaded dynamically (currently only the case on |
9272 | MS-Windows), load the library for TYPE if it is not yet loaded, using | |
9273 | the library file(s) specified by `dynamic-library-alist'. */) | |
9274 | (Lisp_Object type) | |
0855eb52 | 9275 | { |
d07ff9db | 9276 | return lookup_image_type (type) ? Qt : Qnil; |
bb4d86b4 | 9277 | } |
0855eb52 | 9278 | |
bb4d86b4 | 9279 | /* Look up image type TYPE, and return a pointer to its image_type |
d07ff9db | 9280 | structure. Return 0 if TYPE is not a known image type. */ |
bb4d86b4 CY |
9281 | |
9282 | static struct image_type * | |
d07ff9db | 9283 | lookup_image_type (Lisp_Object type) |
bb4d86b4 | 9284 | { |
2f142cc5 | 9285 | /* Types pbm and xbm are built-in and always available. */ |
bb4d86b4 | 9286 | if (EQ (type, Qpbm)) |
d07ff9db | 9287 | return define_image_type (&pbm_type); |
bb4d86b4 CY |
9288 | |
9289 | if (EQ (type, Qxbm)) | |
d07ff9db | 9290 | return define_image_type (&xbm_type); |
2f142cc5 | 9291 | |
9e2a2647 | 9292 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
0855eb52 | 9293 | if (EQ (type, Qxpm)) |
d07ff9db | 9294 | return define_image_type (&xpm_type); |
0855eb52 JB |
9295 | #endif |
9296 | ||
9e2a2647 | 9297 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
0855eb52 | 9298 | if (EQ (type, Qjpeg)) |
d07ff9db | 9299 | return define_image_type (&jpeg_type); |
0855eb52 JB |
9300 | #endif |
9301 | ||
9e2a2647 | 9302 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
0855eb52 | 9303 | if (EQ (type, Qtiff)) |
d07ff9db | 9304 | return define_image_type (&tiff_type); |
0855eb52 JB |
9305 | #endif |
9306 | ||
9e2a2647 | 9307 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
0855eb52 | 9308 | if (EQ (type, Qgif)) |
d07ff9db | 9309 | return define_image_type (&gif_type); |
0855eb52 JB |
9310 | #endif |
9311 | ||
9e2a2647 | 9312 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
0855eb52 | 9313 | if (EQ (type, Qpng)) |
d07ff9db | 9314 | return define_image_type (&png_type); |
0855eb52 JB |
9315 | #endif |
9316 | ||
4ab27a43 GM |
9317 | #if defined (HAVE_RSVG) |
9318 | if (EQ (type, Qsvg)) | |
d07ff9db | 9319 | return define_image_type (&svg_type); |
4ab27a43 | 9320 | #endif |
c21e6151 | 9321 | |
3de25479 | 9322 | #if defined (HAVE_IMAGEMAGICK) |
7574650a | 9323 | if (EQ (type, Qimagemagick)) |
d07ff9db | 9324 | return define_image_type (&imagemagick_type); |
3de25479 JV |
9325 | #endif |
9326 | ||
0855eb52 JB |
9327 | #ifdef HAVE_GHOSTSCRIPT |
9328 | if (EQ (type, Qpostscript)) | |
d07ff9db | 9329 | return define_image_type (&gs_type); |
0855eb52 JB |
9330 | #endif |
9331 | ||
bb4d86b4 | 9332 | return NULL; |
0855eb52 JB |
9333 | } |
9334 | ||
aa15c6bb JB |
9335 | /* Reset image_types before dumping. |
9336 | Called from Fdump_emacs. */ | |
9337 | ||
9338 | void | |
9339 | reset_image_types (void) | |
9340 | { | |
9341 | while (image_types) | |
9342 | { | |
9343 | struct image_type *next = image_types->next; | |
9344 | xfree (image_types); | |
9345 | image_types = next; | |
9346 | } | |
0855eb52 JB |
9347 | } |
9348 | ||
4ef0d4d0 | 9349 | void |
d3da34e0 | 9350 | syms_of_image (void) |
4ef0d4d0 | 9351 | { |
aa15c6bb | 9352 | /* Initialize this only once; it will be reset before dumping. */ |
6e6fc3fd RS |
9353 | image_types = NULL; |
9354 | ||
91af3942 | 9355 | /* Must be defined now because we're going to update it below, while |
0855eb52 | 9356 | defining the supported image types. */ |
29208e82 | 9357 | DEFVAR_LISP ("image-types", Vimage_types, |
0855eb52 | 9358 | doc: /* List of potentially supported image types. |
bbe6f2aa | 9359 | Each element of the list is a symbol for an image type, like 'jpeg or 'png. |
0855eb52 JB |
9360 | To check whether it is really supported, use `image-type-available-p'. */); |
9361 | Vimage_types = Qnil; | |
9362 | ||
29208e82 | 9363 | DEFVAR_LISP ("max-image-size", Vmax_image_size, |
7df4765a KS |
9364 | doc: /* Maximum size of images. |
9365 | Emacs will not load an image into memory if its pixel width or | |
9366 | pixel height exceeds this limit. | |
9367 | ||
9368 | If the value is an integer, it directly specifies the maximum | |
9369 | image height and width, measured in pixels. If it is a floating | |
9370 | point number, it specifies the maximum image height and width | |
9371 | as a ratio to the frame height and width. If the value is | |
9372 | non-numeric, there is no explicit limit on the size of images. */); | |
f1f25b99 CY |
9373 | Vmax_image_size = make_float (MAX_IMAGE_SIZE); |
9374 | ||
1100a63c CY |
9375 | DEFSYM (Qcount, "count"); |
9376 | DEFSYM (Qextension_data, "extension-data"); | |
9377 | DEFSYM (Qdelay, "delay"); | |
9378 | ||
9379 | DEFSYM (QCascent, ":ascent"); | |
9380 | DEFSYM (QCmargin, ":margin"); | |
9381 | DEFSYM (QCrelief, ":relief"); | |
9382 | DEFSYM (QCconversion, ":conversion"); | |
9383 | DEFSYM (QCcolor_symbols, ":color-symbols"); | |
9384 | DEFSYM (QCheuristic_mask, ":heuristic-mask"); | |
9385 | DEFSYM (QCindex, ":index"); | |
9386 | DEFSYM (QCgeometry, ":geometry"); | |
9387 | DEFSYM (QCcrop, ":crop"); | |
9388 | DEFSYM (QCrotation, ":rotation"); | |
9389 | DEFSYM (QCmatrix, ":matrix"); | |
9390 | DEFSYM (QCcolor_adjustment, ":color-adjustment"); | |
9391 | DEFSYM (QCmask, ":mask"); | |
9392 | ||
9393 | DEFSYM (Qlaplace, "laplace"); | |
9394 | DEFSYM (Qemboss, "emboss"); | |
9395 | DEFSYM (Qedge_detection, "edge-detection"); | |
9396 | DEFSYM (Qheuristic, "heuristic"); | |
9397 | ||
9398 | DEFSYM (Qpostscript, "postscript"); | |
f3f9606c LMI |
9399 | DEFSYM (QCmax_width, ":max-width"); |
9400 | DEFSYM (QCmax_height, ":max-height"); | |
4ef0d4d0 | 9401 | #ifdef HAVE_GHOSTSCRIPT |
5fc9fdeb | 9402 | ADD_IMAGE_TYPE (Qpostscript); |
1100a63c CY |
9403 | DEFSYM (QCloader, ":loader"); |
9404 | DEFSYM (QCbounding_box, ":bounding-box"); | |
9405 | DEFSYM (QCpt_width, ":pt-width"); | |
9406 | DEFSYM (QCpt_height, ":pt-height"); | |
4ef0d4d0 KS |
9407 | #endif /* HAVE_GHOSTSCRIPT */ |
9408 | ||
5be1c984 | 9409 | #ifdef HAVE_NTGUI |
1100a63c | 9410 | DEFSYM (Qlibpng_version, "libpng-version"); |
0514b4be | 9411 | Fset (Qlibpng_version, |
5be1c984 | 9412 | #if HAVE_PNG |
0514b4be | 9413 | make_number (PNG_LIBPNG_VER) |
5be1c984 | 9414 | #else |
0514b4be | 9415 | make_number (-1) |
a5dab159 EZ |
9416 | #endif |
9417 | ); | |
9418 | DEFSYM (Qlibgif_version, "libgif-version"); | |
9419 | Fset (Qlibgif_version, | |
9420 | #ifdef HAVE_GIF | |
9421 | make_number (GIFLIB_MAJOR * 10000 | |
9422 | + GIFLIB_MINOR * 100 | |
9423 | + GIFLIB_RELEASE) | |
9424 | #else | |
9425 | make_number (-1) | |
d14c81ee CB |
9426 | #endif |
9427 | ); | |
9428 | DEFSYM (Qlibjpeg_version, "libjpeg-version"); | |
9429 | Fset (Qlibjpeg_version, | |
9430 | #if HAVE_JPEG | |
9431 | make_number (JPEG_LIB_VERSION) | |
9432 | #else | |
9433 | make_number (-1) | |
5be1c984 | 9434 | #endif |
0514b4be | 9435 | ); |
5be1c984 EZ |
9436 | #endif |
9437 | ||
bb4d86b4 CY |
9438 | DEFSYM (Qpbm, "pbm"); |
9439 | ADD_IMAGE_TYPE (Qpbm); | |
9440 | ||
9441 | DEFSYM (Qxbm, "xbm"); | |
9442 | ADD_IMAGE_TYPE (Qxbm); | |
9443 | ||
9e2a2647 | 9444 | #if defined (HAVE_XPM) || defined (HAVE_NS) |
1100a63c | 9445 | DEFSYM (Qxpm, "xpm"); |
5fc9fdeb | 9446 | ADD_IMAGE_TYPE (Qxpm); |
4ef0d4d0 KS |
9447 | #endif |
9448 | ||
9e2a2647 | 9449 | #if defined (HAVE_JPEG) || defined (HAVE_NS) |
1100a63c | 9450 | DEFSYM (Qjpeg, "jpeg"); |
5fc9fdeb | 9451 | ADD_IMAGE_TYPE (Qjpeg); |
4ef0d4d0 KS |
9452 | #endif |
9453 | ||
9e2a2647 | 9454 | #if defined (HAVE_TIFF) || defined (HAVE_NS) |
1100a63c | 9455 | DEFSYM (Qtiff, "tiff"); |
5fc9fdeb | 9456 | ADD_IMAGE_TYPE (Qtiff); |
4ef0d4d0 KS |
9457 | #endif |
9458 | ||
9e2a2647 | 9459 | #if defined (HAVE_GIF) || defined (HAVE_NS) |
1100a63c | 9460 | DEFSYM (Qgif, "gif"); |
5fc9fdeb | 9461 | ADD_IMAGE_TYPE (Qgif); |
4ef0d4d0 KS |
9462 | #endif |
9463 | ||
9e2a2647 | 9464 | #if defined (HAVE_PNG) || defined (HAVE_NS) |
1100a63c | 9465 | DEFSYM (Qpng, "png"); |
5fc9fdeb | 9466 | ADD_IMAGE_TYPE (Qpng); |
4ef0d4d0 KS |
9467 | #endif |
9468 | ||
3de25479 | 9469 | #if defined (HAVE_IMAGEMAGICK) |
1100a63c | 9470 | DEFSYM (Qimagemagick, "imagemagick"); |
3de25479 JV |
9471 | ADD_IMAGE_TYPE (Qimagemagick); |
9472 | #endif | |
7574650a | 9473 | |
4ab27a43 | 9474 | #if defined (HAVE_RSVG) |
1100a63c | 9475 | DEFSYM (Qsvg, "svg"); |
5fc9fdeb JR |
9476 | ADD_IMAGE_TYPE (Qsvg); |
9477 | #ifdef HAVE_NTGUI | |
df23bf08 | 9478 | /* Other libraries used directly by svg code. */ |
1100a63c CY |
9479 | DEFSYM (Qgdk_pixbuf, "gdk-pixbuf"); |
9480 | DEFSYM (Qglib, "glib"); | |
9481 | DEFSYM (Qgobject, "gobject"); | |
5fc9fdeb JR |
9482 | #endif /* HAVE_NTGUI */ |
9483 | #endif /* HAVE_RSVG */ | |
4ab27a43 | 9484 | |
0855eb52 | 9485 | defsubr (&Sinit_image_library); |
7574650a | 9486 | #ifdef HAVE_IMAGEMAGICK |
3de25479 | 9487 | defsubr (&Simagemagick_types); |
7574650a | 9488 | #endif |
4ef0d4d0 | 9489 | defsubr (&Sclear_image_cache); |
110683ad | 9490 | defsubr (&Simage_flush); |
4ef0d4d0 KS |
9491 | defsubr (&Simage_size); |
9492 | defsubr (&Simage_mask_p); | |
1546c559 | 9493 | defsubr (&Simage_metadata); |
4ef0d4d0 | 9494 | |
e509cfa6 | 9495 | #ifdef GLYPH_DEBUG |
4ef0d4d0 KS |
9496 | defsubr (&Simagep); |
9497 | defsubr (&Slookup_image); | |
9498 | #endif | |
9499 | ||
29208e82 | 9500 | DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images, |
4ef0d4d0 | 9501 | doc: /* Non-nil means always draw a cross over disabled images. |
3046a84b | 9502 | Disabled images are those having a `:conversion disabled' property. |
4ef0d4d0 KS |
9503 | A cross is always drawn on black & white displays. */); |
9504 | cross_disabled_images = 0; | |
9505 | ||
29208e82 | 9506 | DEFVAR_LISP ("x-bitmap-file-path", Vx_bitmap_file_path, |
4ef0d4d0 | 9507 | doc: /* List of directories to search for window system bitmap files. */); |
17e0445b | 9508 | Vx_bitmap_file_path = decode_env_path (0, PATH_BITMAPS, 0); |
4ef0d4d0 | 9509 | |
29208e82 | 9510 | DEFVAR_LISP ("image-cache-eviction-delay", Vimage_cache_eviction_delay, |
98fe5161 CY |
9511 | doc: /* Maximum time after which images are removed from the cache. |
9512 | When an image has not been displayed this many seconds, Emacs | |
9513 | automatically removes it from the image cache. If the cache contains | |
9514 | a large number of images, the actual eviction time may be shorter. | |
9515 | The value can also be nil, meaning the cache is never cleared. | |
9516 | ||
9517 | The function `clear-image-cache' disregards this variable. */); | |
9518 | Vimage_cache_eviction_delay = make_number (300); | |
7574650a | 9519 | #ifdef HAVE_IMAGEMAGICK |
af008560 GM |
9520 | DEFVAR_INT ("imagemagick-render-type", imagemagick_render_type, |
9521 | doc: /* Integer indicating which ImageMagick rendering method to use. | |
9522 | The options are: | |
9523 | 0 -- the default method (pixel pushing) | |
9524 | 1 -- a newer method ("MagickExportImagePixels") that may perform | |
9525 | better (speed etc) in some cases, but has not been as thoroughly | |
9526 | tested with Emacs as the default method. This method requires | |
9527 | ImageMagick version 6.4.6 (approximately) or later. | |
9528 | */); | |
9529 | /* MagickExportImagePixels is in 6.4.6-9, but not 6.4.4-10. */ | |
9530 | imagemagick_render_type = 0; | |
7574650a | 9531 | #endif |
4ef0d4d0 | 9532 | |
4ef0d4d0 | 9533 | } |