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