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