428
|
1 /* EImage-specific Lisp objects.
|
|
2 Copyright (C) 1993, 1994, 1998 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Board of Trustees, University of Illinois.
|
|
4 Copyright (C) 1995 Tinker Systems
|
793
|
5 Copyright (C) 1995, 1996, 2001, 2002 Ben Wing
|
428
|
6 Copyright (C) 1995 Sun Microsystems
|
|
7
|
|
8 This file is part of XEmacs.
|
|
9
|
|
10 XEmacs is free software; you can redistribute it and/or modify it
|
|
11 under the terms of the GNU General Public License as published by the
|
|
12 Free Software Foundation; either version 2, or (at your option) any
|
|
13 later version.
|
|
14
|
|
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
18 for more details.
|
|
19
|
|
20 You should have received a copy of the GNU General Public License
|
|
21 along with XEmacs; see the file COPYING. If not, write to
|
|
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 Boston, MA 02111-1307, USA. */
|
|
24
|
|
25 /* Synched up with: Not in FSF. */
|
|
26
|
|
27 /* Original author: Jamie Zawinski for 19.8
|
|
28 font-truename stuff added by Jamie Zawinski for 19.10
|
|
29 subwindow support added by Chuck Thompson
|
|
30 additional XPM support added by Chuck Thompson
|
|
31 initial X-Face support added by Stig
|
|
32 rewritten/restructured by Ben Wing for 19.12/19.13
|
|
33 GIF/JPEG support added by Ben Wing for 19.14
|
|
34 PNG support added by Bill Perry for 19.14
|
|
35 Improved GIF/JPEG support added by Bill Perry for 19.14
|
|
36 Cleanup/simplification of error handling by Ben Wing for 19.14
|
|
37 Pointer/icon overhaul, more restructuring by Ben Wing for 19.14
|
|
38 GIF support changed to external Gifreader lib by Jareth Hein for 21.0
|
|
39 Many changes for color work and optimizations by Jareth Hein for 21.0
|
|
40 Switch of GIF/JPEG/PNG to new EImage intermediate code by Jareth Hein for 21.0
|
|
41 TIFF code by Jareth Hein for 21.0
|
|
42 Generalization for ms-windows by Andy Piper for 21.0
|
|
43 TODO:
|
|
44 Convert images.el to C and stick it in here?
|
|
45 */
|
|
46
|
|
47 #include <config.h>
|
|
48 #include "lisp.h"
|
|
49 #include "lstream.h"
|
|
50 #include "console.h"
|
872
|
51 #include "device-impl.h"
|
428
|
52 #include "faces.h"
|
|
53 #include "glyphs.h"
|
872
|
54 #include "objects-impl.h"
|
428
|
55
|
|
56 #include "buffer.h"
|
|
57 #include "frame.h"
|
|
58 #include "opaque.h"
|
442
|
59 #include "window.h"
|
428
|
60
|
|
61 #include "sysfile.h"
|
|
62
|
|
63 #ifdef HAVE_PNG
|
1743
|
64
|
|
65 BEGIN_C_DECLS
|
|
66
|
647
|
67 #define message message_ /* Yuck */
|
428
|
68 #include <png.h>
|
647
|
69 #undef message
|
1743
|
70
|
|
71 END_C_DECLS
|
|
72
|
428
|
73 #else
|
|
74 #include <setjmp.h>
|
|
75 #endif
|
|
76 #include "file-coding.h"
|
|
77
|
|
78 #ifdef HAVE_TIFF
|
|
79 DEFINE_IMAGE_INSTANTIATOR_FORMAT (tiff);
|
|
80 Lisp_Object Qtiff;
|
|
81 #endif
|
|
82
|
|
83 #ifdef HAVE_JPEG
|
|
84 DEFINE_IMAGE_INSTANTIATOR_FORMAT (jpeg);
|
|
85 Lisp_Object Qjpeg;
|
|
86 #endif
|
|
87
|
|
88 #ifdef HAVE_GIF
|
|
89 DEFINE_IMAGE_INSTANTIATOR_FORMAT (gif);
|
|
90 Lisp_Object Qgif;
|
|
91 #endif
|
|
92
|
|
93 #ifdef HAVE_PNG
|
|
94 DEFINE_IMAGE_INSTANTIATOR_FORMAT (png);
|
|
95 Lisp_Object Qpng;
|
|
96 #endif
|
|
97
|
|
98
|
|
99 #ifdef HAVE_JPEG
|
|
100
|
|
101 /**********************************************************************
|
|
102 * JPEG *
|
|
103 **********************************************************************/
|
|
104
|
1743
|
105 BEGIN_C_DECLS
|
|
106
|
428
|
107 #include <jpeglib.h>
|
|
108 #include <jerror.h>
|
1743
|
109
|
|
110 END_C_DECLS
|
428
|
111
|
|
112 /*#define USE_TEMP_FILES_FOR_JPEG_IMAGES 1*/
|
|
113 static void
|
|
114 jpeg_validate (Lisp_Object instantiator)
|
|
115 {
|
|
116 file_or_data_must_be_present (instantiator);
|
|
117 }
|
|
118
|
|
119 static Lisp_Object
|
442
|
120 jpeg_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
121 Lisp_Object dest_mask)
|
428
|
122 {
|
|
123 return simple_image_type_normalize (inst, console_type, Qjpeg);
|
|
124 }
|
|
125
|
|
126 static int
|
|
127 jpeg_possible_dest_types (void)
|
|
128 {
|
|
129 return IMAGE_COLOR_PIXMAP_MASK;
|
|
130 }
|
|
131
|
|
132 /* To survive the otherwise baffling complexity of making sure
|
|
133 everything gets cleaned up in the presence of an error, we
|
|
134 use an unwind_protect(). */
|
|
135
|
|
136 struct jpeg_unwind_data
|
|
137 {
|
|
138 /* Stream that we need to close */
|
|
139 FILE *instream;
|
|
140 /* Object that holds state info for JPEG decoding */
|
|
141 struct jpeg_decompress_struct *cinfo_ptr;
|
|
142 /* EImage data */
|
771
|
143 UChar_Binary *eimage;
|
428
|
144 };
|
|
145
|
|
146 static Lisp_Object
|
|
147 jpeg_instantiate_unwind (Lisp_Object unwind_obj)
|
|
148 {
|
|
149 struct jpeg_unwind_data *data =
|
|
150 (struct jpeg_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
151
|
|
152 free_opaque_ptr (unwind_obj);
|
|
153 if (data->cinfo_ptr)
|
|
154 jpeg_destroy_decompress (data->cinfo_ptr);
|
|
155
|
|
156 if (data->instream)
|
771
|
157 retry_fclose (data->instream);
|
428
|
158
|
1726
|
159 if (data->eimage)
|
|
160 xfree (data->eimage, UChar_Binary *);
|
428
|
161
|
|
162 return Qnil;
|
|
163 }
|
|
164
|
|
165 /*
|
|
166 * ERROR HANDLING:
|
|
167 *
|
|
168 * The JPEG library's standard error handler (jerror.c) is divided into
|
|
169 * several "methods" which you can override individually. This lets you
|
|
170 * adjust the behavior without duplicating a lot of code, which you might
|
|
171 * have to update with each future release.
|
|
172 *
|
|
173 * Our example here shows how to override the "error_exit" method so that
|
|
174 * control is returned to the library's caller when a fatal error occurs,
|
|
175 * rather than calling exit() as the standard error_exit method does.
|
|
176 *
|
|
177 * We use C's setjmp/longjmp facility to return control. This means that the
|
|
178 * routine which calls the JPEG library must first execute a setjmp() call to
|
|
179 * establish the return point. We want the replacement error_exit to do a
|
|
180 * longjmp(). But we need to make the setjmp buffer accessible to the
|
|
181 * error_exit routine. To do this, we make a private extension of the
|
|
182 * standard JPEG error handler object. (If we were using C++, we'd say we
|
|
183 * were making a subclass of the regular error handler.)
|
|
184 *
|
|
185 * Here's the extended error handler struct:
|
|
186 */
|
|
187
|
|
188 struct my_jpeg_error_mgr
|
|
189 {
|
|
190 struct jpeg_error_mgr pub; /* "public" fields */
|
|
191 jmp_buf setjmp_buffer; /* for return to caller */
|
|
192 };
|
|
193
|
|
194 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
195 METHODDEF(void)
|
|
196 #else
|
|
197 METHODDEF void
|
|
198 #endif
|
|
199 our_init_source (j_decompress_ptr cinfo)
|
|
200 {
|
|
201 }
|
|
202
|
|
203 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
204 METHODDEF(boolean)
|
|
205 #else
|
|
206 METHODDEF boolean
|
|
207 #endif
|
|
208 our_fill_input_buffer (j_decompress_ptr cinfo)
|
|
209 {
|
|
210 /* Insert a fake EOI marker */
|
|
211 struct jpeg_source_mgr *src = cinfo->src;
|
|
212 static JOCTET buffer[2];
|
|
213
|
|
214 buffer[0] = (JOCTET) 0xFF;
|
|
215 buffer[1] = (JOCTET) JPEG_EOI;
|
|
216
|
|
217 src->next_input_byte = buffer;
|
|
218 src->bytes_in_buffer = 2;
|
|
219 return TRUE;
|
|
220 }
|
|
221
|
|
222 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
223 METHODDEF(void)
|
|
224 #else
|
|
225 METHODDEF void
|
|
226 #endif
|
|
227 our_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
|
|
228 {
|
|
229 struct jpeg_source_mgr *src = NULL;
|
|
230
|
|
231 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
232
|
|
233 if (!src)
|
647
|
234 return;
|
|
235 else if (num_bytes > (long) src->bytes_in_buffer)
|
428
|
236 {
|
647
|
237 ERREXIT (cinfo, JERR_INPUT_EOF);
|
|
238 /*NOTREACHED*/
|
|
239 }
|
428
|
240
|
|
241 src->bytes_in_buffer -= num_bytes;
|
|
242 src->next_input_byte += num_bytes;
|
|
243 }
|
|
244
|
|
245 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
246 METHODDEF(void)
|
|
247 #else
|
|
248 METHODDEF void
|
|
249 #endif
|
|
250 our_term_source (j_decompress_ptr cinfo)
|
|
251 {
|
|
252 }
|
|
253
|
|
254 typedef struct
|
|
255 {
|
|
256 struct jpeg_source_mgr pub;
|
|
257 } our_jpeg_source_mgr;
|
|
258
|
|
259 static void
|
665
|
260 jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, Bytecount len)
|
428
|
261 {
|
|
262 struct jpeg_source_mgr *src;
|
|
263
|
|
264 if (cinfo->src == NULL)
|
|
265 { /* first time for this JPEG object? */
|
|
266 cinfo->src = (struct jpeg_source_mgr *)
|
|
267 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
|
|
268 sizeof(our_jpeg_source_mgr));
|
|
269 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
270 src->next_input_byte = data;
|
|
271 }
|
|
272 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
273 src->init_source = our_init_source;
|
|
274 src->fill_input_buffer = our_fill_input_buffer;
|
|
275 src->skip_input_data = our_skip_input_data;
|
|
276 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
|
|
277 src->term_source = our_term_source;
|
|
278 src->bytes_in_buffer = len;
|
|
279 src->next_input_byte = data;
|
|
280 }
|
|
281
|
|
282 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
283 METHODDEF(void)
|
|
284 #else
|
|
285 METHODDEF void
|
|
286 #endif
|
|
287 my_jpeg_error_exit (j_common_ptr cinfo)
|
|
288 {
|
|
289 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
|
|
290 struct my_jpeg_error_mgr *myerr = (struct my_jpeg_error_mgr *) cinfo->err;
|
|
291
|
|
292 /* Return control to the setjmp point */
|
|
293 longjmp (myerr->setjmp_buffer, 1);
|
|
294 }
|
|
295
|
|
296 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
297 METHODDEF(void)
|
|
298 #else
|
|
299 METHODDEF void
|
|
300 #endif
|
|
301 my_jpeg_output_message (j_common_ptr cinfo)
|
|
302 {
|
771
|
303 Extbyte buffer[JMSG_LENGTH_MAX];
|
867
|
304 Ibyte *intbuf;
|
428
|
305
|
|
306 /* Create the message */
|
|
307 (*cinfo->err->format_message) (cinfo, buffer);
|
771
|
308 EXTERNAL_TO_C_STRING (buffer, intbuf, Qnative);
|
|
309 warn_when_safe (Qjpeg, Qinfo, "%s", intbuf);
|
428
|
310 }
|
|
311
|
|
312 /* The code in this routine is based on example.c from the JPEG library
|
|
313 source code and from gif_instantiate() */
|
|
314 static void
|
|
315 jpeg_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
316 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
317 int dest_mask, Lisp_Object domain)
|
|
318 {
|
440
|
319 Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
428
|
320 /* It is OK for the unwind data to be local to this function,
|
|
321 because the unwind-protect is always executed when this
|
|
322 stack frame is still valid. */
|
|
323 struct jpeg_unwind_data unwind;
|
|
324 int speccount = specpdl_depth ();
|
|
325
|
|
326 /* This struct contains the JPEG decompression parameters and pointers to
|
|
327 * working space (which is allocated as needed by the JPEG library).
|
|
328 */
|
|
329 struct jpeg_decompress_struct cinfo;
|
|
330 /* We use our private extension JPEG error handler.
|
|
331 * Note that this struct must live as long as the main JPEG parameter
|
|
332 * struct, to avoid dangling-pointer problems.
|
|
333 */
|
|
334 struct my_jpeg_error_mgr jerr;
|
|
335
|
|
336 /* Step -1: First record our unwind-protect, which will clean up after
|
|
337 any exit, normal or not */
|
|
338
|
|
339 xzero (unwind);
|
|
340 record_unwind_protect (jpeg_instantiate_unwind, make_opaque_ptr (&unwind));
|
|
341
|
|
342 /* Step 1: allocate and initialize JPEG decompression object */
|
|
343
|
|
344 /* We set up the normal JPEG error routines, then override error_exit. */
|
|
345 cinfo.err = jpeg_std_error (&jerr.pub);
|
|
346 jerr.pub.error_exit = my_jpeg_error_exit;
|
|
347 jerr.pub.output_message = my_jpeg_output_message;
|
|
348
|
|
349 /* Establish the setjmp return context for my_error_exit to use. */
|
|
350 if (setjmp (jerr.setjmp_buffer))
|
|
351 {
|
|
352 /* If we get here, the JPEG code has signaled an error.
|
|
353 * We need to clean up the JPEG object, close the input file, and return.
|
|
354 */
|
|
355
|
|
356 {
|
|
357 Lisp_Object errstring;
|
771
|
358 Extbyte buffer[JMSG_LENGTH_MAX];
|
428
|
359
|
|
360 /* Create the message */
|
|
361 (*cinfo.err->format_message) ((j_common_ptr) &cinfo, buffer);
|
771
|
362 errstring = build_ext_string (buffer, Qnative);
|
428
|
363
|
|
364 signal_image_error_2 ("JPEG decoding error",
|
|
365 errstring, instantiator);
|
|
366 }
|
|
367 }
|
|
368
|
|
369 /* Now we can initialize the JPEG decompression object. */
|
|
370 jpeg_create_decompress (&cinfo);
|
|
371 unwind.cinfo_ptr = &cinfo;
|
|
372
|
|
373 /* Step 2: specify data source (eg, a file) */
|
|
374
|
|
375 {
|
|
376 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
647
|
377 const UChar_Binary *bytes;
|
665
|
378 Bytecount len;
|
428
|
379
|
|
380 /* #### This is a definite problem under Mule due to the amount of
|
|
381 stack data it might allocate. Need to be able to convert and
|
|
382 write out to a file. */
|
440
|
383 TO_EXTERNAL_FORMAT (LISP_STRING, data, ALLOCA, (bytes, len), Qbinary);
|
428
|
384 jpeg_memory_src (&cinfo, (JOCTET *) bytes, len);
|
|
385 }
|
|
386
|
|
387 /* Step 3: read file parameters with jpeg_read_header() */
|
|
388
|
|
389 jpeg_read_header (&cinfo, TRUE);
|
|
390 /* We can ignore the return value from jpeg_read_header since
|
|
391 * (a) suspension is not possible with the stdio data source, and
|
|
392 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
|
|
393 * See libjpeg.doc for more info.
|
|
394 */
|
|
395
|
|
396 {
|
|
397 int jpeg_gray = 0; /* if we're dealing with a grayscale */
|
|
398 /* Step 4: set parameters for decompression. */
|
|
399
|
|
400 /* Now that we're using EImages, send all data as 24bit color.
|
|
401 The backend routine will take care of any necessary reductions.
|
|
402 We do have to handle the grayscale case ourselves, however. */
|
|
403 if (cinfo.jpeg_color_space == JCS_GRAYSCALE)
|
|
404 {
|
|
405 cinfo.out_color_space = JCS_GRAYSCALE;
|
|
406 jpeg_gray = 1;
|
|
407 }
|
|
408 else
|
|
409 {
|
|
410 /* we're relying on the jpeg driver to do any other conversions,
|
|
411 or signal an error if the conversion isn't supported. */
|
|
412 cinfo.out_color_space = JCS_RGB;
|
|
413 }
|
|
414
|
|
415 /* Step 5: Start decompressor */
|
|
416 jpeg_start_decompress (&cinfo);
|
|
417
|
|
418 /* Step 6: Read in the data and put into EImage format (8bit RGB triples)*/
|
|
419
|
771
|
420 unwind.eimage = (UChar_Binary*) xmalloc (cinfo.output_width * cinfo.output_height * 3);
|
428
|
421 if (!unwind.eimage)
|
|
422 signal_image_error("Unable to allocate enough memory for image", instantiator);
|
|
423
|
|
424 {
|
|
425 JSAMPARRAY row_buffer; /* Output row buffer */
|
|
426 JSAMPLE *jp;
|
|
427 int row_stride; /* physical row width in output buffer */
|
771
|
428 UChar_Binary *op = unwind.eimage;
|
428
|
429
|
|
430 /* We may need to do some setup of our own at this point before reading
|
|
431 * the data. After jpeg_start_decompress() we have the correct scaled
|
|
432 * output image dimensions available
|
|
433 * We need to make an output work buffer of the right size.
|
|
434 */
|
|
435 /* JSAMPLEs per row in output buffer. */
|
|
436 row_stride = cinfo.output_width * cinfo.output_components;
|
|
437 /* Make a one-row-high sample array that will go away when done
|
|
438 with image */
|
|
439 row_buffer = ((*cinfo.mem->alloc_sarray)
|
|
440 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1));
|
|
441
|
|
442 /* Here we use the library's state variable cinfo.output_scanline as the
|
|
443 * loop counter, so that we don't have to keep track ourselves.
|
|
444 */
|
|
445 while (cinfo.output_scanline < cinfo.output_height)
|
|
446 {
|
|
447 int i;
|
|
448
|
|
449 /* jpeg_read_scanlines expects an array of pointers to scanlines.
|
|
450 * Here the array is only one element long, but you could ask for
|
|
451 * more than one scanline at a time if that's more convenient.
|
|
452 */
|
|
453 (void) jpeg_read_scanlines (&cinfo, row_buffer, 1);
|
|
454 jp = row_buffer[0];
|
647
|
455 for (i = 0; i < (int) cinfo.output_width; i++)
|
428
|
456 {
|
|
457 int clr;
|
|
458 if (jpeg_gray)
|
|
459 {
|
771
|
460 UChar_Binary val;
|
428
|
461 #if (BITS_IN_JSAMPLE == 8)
|
771
|
462 val = (UChar_Binary) *jp++;
|
428
|
463 #else /* other option is 12 */
|
771
|
464 val = (UChar_Binary) (*jp++ >> 4);
|
428
|
465 #endif
|
|
466 for (clr = 0; clr < 3; clr++) /* copy the same value into RGB */
|
|
467 *op++ = val;
|
|
468 }
|
|
469 else
|
|
470 {
|
|
471 for (clr = 0; clr < 3; clr++)
|
|
472 #if (BITS_IN_JSAMPLE == 8)
|
771
|
473 *op++ = (UChar_Binary)*jp++;
|
428
|
474 #else /* other option is 12 */
|
771
|
475 *op++ = (UChar_Binary)(*jp++ >> 4);
|
428
|
476 #endif
|
|
477 }
|
|
478 }
|
|
479 }
|
|
480 }
|
|
481 }
|
|
482
|
|
483 /* Step 6.5: Create the pixmap and set up the image instance */
|
|
484 /* now instantiate */
|
442
|
485 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
486 init_image_instance_from_eimage,
|
|
487 (ii, cinfo.output_width, cinfo.output_height, 1,
|
|
488 unwind.eimage, dest_mask,
|
|
489 instantiator, domain));
|
|
490
|
|
491 /* Step 7: Finish decompression */
|
|
492
|
|
493 jpeg_finish_decompress (&cinfo);
|
|
494 /* We can ignore the return value since suspension is not possible
|
|
495 * with the stdio data source.
|
|
496 */
|
|
497
|
|
498 /* And we're done! */
|
|
499 /* This will clean up everything else. */
|
771
|
500 unbind_to (speccount);
|
428
|
501 }
|
|
502
|
|
503 #endif /* HAVE_JPEG */
|
|
504
|
|
505 #ifdef HAVE_GIF
|
|
506 /**********************************************************************
|
|
507 * GIF *
|
|
508 **********************************************************************/
|
|
509
|
|
510 #include "gifrlib.h"
|
|
511
|
|
512 static void
|
|
513 gif_validate (Lisp_Object instantiator)
|
|
514 {
|
|
515 file_or_data_must_be_present (instantiator);
|
|
516 }
|
|
517
|
|
518 static Lisp_Object
|
442
|
519 gif_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
520 Lisp_Object dest_mask)
|
428
|
521 {
|
|
522 return simple_image_type_normalize (inst, console_type, Qgif);
|
|
523 }
|
|
524
|
|
525 static int
|
|
526 gif_possible_dest_types (void)
|
|
527 {
|
|
528 return IMAGE_COLOR_PIXMAP_MASK;
|
|
529 }
|
|
530
|
|
531 /* To survive the otherwise baffling complexity of making sure
|
|
532 everything gets cleaned up in the presence of an error, we
|
|
533 use an unwind_protect(). */
|
|
534
|
|
535 struct gif_unwind_data
|
|
536 {
|
771
|
537 UChar_Binary *eimage;
|
428
|
538 /* Object that holds the decoded data from a GIF file */
|
|
539 GifFileType *giffile;
|
|
540 };
|
|
541
|
|
542 static Lisp_Object
|
|
543 gif_instantiate_unwind (Lisp_Object unwind_obj)
|
|
544 {
|
|
545 struct gif_unwind_data *data =
|
|
546 (struct gif_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
547
|
|
548 free_opaque_ptr (unwind_obj);
|
|
549 if (data->giffile)
|
|
550 {
|
|
551 DGifCloseFile (data->giffile);
|
|
552 GifFree(data->giffile);
|
|
553 }
|
647
|
554 if (data->eimage)
|
1726
|
555 xfree (data->eimage, UChar_Binary *);
|
428
|
556
|
|
557 return Qnil;
|
|
558 }
|
|
559
|
|
560 typedef struct gif_memory_storage
|
|
561 {
|
647
|
562 UChar_Binary *bytes; /* The data */
|
665
|
563 Bytecount len; /* How big is it? */
|
|
564 Bytecount index; /* Where are we? */
|
428
|
565 } gif_memory_storage;
|
|
566
|
665
|
567 static Bytecount
|
|
568 gif_read_from_memory (GifByteType *buf, Bytecount size, VoidPtr data)
|
428
|
569 {
|
647
|
570 gif_memory_storage *mem = (gif_memory_storage *) data;
|
428
|
571
|
|
572 if (size > (mem->len - mem->index))
|
647
|
573 return -1;
|
|
574 memcpy (buf, mem->bytes + mem->index, size);
|
428
|
575 mem->index = mem->index + size;
|
|
576 return size;
|
|
577 }
|
|
578
|
|
579 static int
|
647
|
580 gif_memory_close (VoidPtr data)
|
428
|
581 {
|
|
582 return 0;
|
|
583 }
|
|
584
|
|
585 struct gif_error_struct
|
|
586 {
|
771
|
587 const Extbyte *err_str; /* return the error string */
|
428
|
588 jmp_buf setjmp_buffer; /* for return to caller */
|
|
589 };
|
|
590
|
|
591 static void
|
771
|
592 gif_error_func (const Extbyte *err_str, VoidPtr error_ptr)
|
428
|
593 {
|
647
|
594 struct gif_error_struct *error_data = (struct gif_error_struct *) error_ptr;
|
428
|
595
|
|
596 /* return to setjmp point */
|
|
597 error_data->err_str = err_str;
|
|
598 longjmp (error_data->setjmp_buffer, 1);
|
|
599 }
|
|
600
|
|
601 static void
|
|
602 gif_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
603 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
604 int dest_mask, Lisp_Object domain)
|
|
605 {
|
440
|
606 Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
428
|
607 /* It is OK for the unwind data to be local to this function,
|
|
608 because the unwind-protect is always executed when this
|
|
609 stack frame is still valid. */
|
|
610 struct gif_unwind_data unwind;
|
|
611 int speccount = specpdl_depth ();
|
|
612 gif_memory_storage mem_struct;
|
|
613 struct gif_error_struct gif_err;
|
647
|
614 UChar_Binary *bytes;
|
665
|
615 Bytecount len;
|
428
|
616 int height = 0;
|
|
617 int width = 0;
|
|
618
|
|
619 xzero (unwind);
|
|
620 record_unwind_protect (gif_instantiate_unwind, make_opaque_ptr (&unwind));
|
|
621
|
|
622 /* 1. Now decode the data. */
|
|
623
|
|
624 {
|
|
625 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
|
626
|
|
627 assert (!NILP (data));
|
|
628
|
|
629 if (!(unwind.giffile = GifSetup()))
|
440
|
630 signal_image_error ("Insufficient memory to instantiate GIF image", instantiator);
|
428
|
631
|
|
632 /* set up error facilities */
|
|
633 if (setjmp(gif_err.setjmp_buffer))
|
|
634 {
|
|
635 /* An error was signaled. No clean up is needed, as unwind handles that
|
|
636 for us. Just pass the error along. */
|
867
|
637 Ibyte *interr;
|
428
|
638 Lisp_Object errstring;
|
771
|
639 EXTERNAL_TO_C_STRING (gif_err.err_str, interr, Qnative);
|
|
640 errstring = build_msg_intstring (interr);
|
428
|
641 signal_image_error_2 ("GIF decoding error", errstring, instantiator);
|
|
642 }
|
|
643 GifSetErrorFunc(unwind.giffile, (Gif_error_func)gif_error_func, (VoidPtr)&gif_err);
|
|
644
|
440
|
645 TO_EXTERNAL_FORMAT (LISP_STRING, data, ALLOCA, (bytes, len), Qbinary);
|
428
|
646 mem_struct.bytes = bytes;
|
|
647 mem_struct.len = len;
|
|
648 mem_struct.index = 0;
|
|
649 GifSetReadFunc(unwind.giffile, gif_read_from_memory, (VoidPtr)&mem_struct);
|
|
650 GifSetCloseFunc(unwind.giffile, gif_memory_close, (VoidPtr)&mem_struct);
|
|
651 DGifInitRead(unwind.giffile);
|
|
652
|
|
653 /* Then slurp the image into memory, decoding along the way.
|
|
654 The result is the image in a simple one-byte-per-pixel
|
|
655 format (#### the GIF routines only support 8-bit GIFs,
|
|
656 it appears). */
|
|
657 DGifSlurp (unwind.giffile);
|
|
658 }
|
|
659
|
|
660 /* 3. Now create the EImage(s) */
|
|
661 {
|
|
662 ColorMapObject *cmo = unwind.giffile->SColorMap;
|
|
663 int i, j, row, pass, interlace, slice;
|
771
|
664 UChar_Binary *eip;
|
428
|
665 /* interlaced gifs have rows in this order:
|
|
666 0, 8, 16, ..., 4, 12, 20, ..., 2, 6, 10, ..., 1, 3, 5, ... */
|
|
667 static int InterlacedOffset[] = { 0, 4, 2, 1 };
|
|
668 static int InterlacedJumps[] = { 8, 8, 4, 2 };
|
|
669
|
|
670 height = unwind.giffile->SHeight;
|
|
671 width = unwind.giffile->SWidth;
|
771
|
672 unwind.eimage = (UChar_Binary*)
|
428
|
673 xmalloc (width * height * 3 * unwind.giffile->ImageCount);
|
|
674 if (!unwind.eimage)
|
|
675 signal_image_error("Unable to allocate enough memory for image", instantiator);
|
|
676
|
|
677 /* write the data in EImage format (8bit RGB triples) */
|
|
678
|
|
679 for (slice = 0; slice < unwind.giffile->ImageCount; slice++)
|
|
680 {
|
638
|
681 /* We check here that the current image covers the full "screen" size. */
|
428
|
682 if (unwind.giffile->SavedImages[slice].ImageDesc.Height != height
|
|
683 || unwind.giffile->SavedImages[slice].ImageDesc.Width != width
|
|
684 || unwind.giffile->SavedImages[slice].ImageDesc.Left != 0
|
|
685 || unwind.giffile->SavedImages[slice].ImageDesc.Top != 0)
|
|
686 signal_image_error ("Image in GIF file is not full size",
|
|
687 instantiator);
|
|
688
|
|
689 interlace = unwind.giffile->SavedImages[slice].ImageDesc.Interlace;
|
|
690 pass = 0;
|
|
691 row = interlace ? InterlacedOffset[pass] : 0;
|
|
692 eip = unwind.eimage + (width * height * 3 * slice);
|
|
693 for (i = 0; i < height; i++)
|
|
694 {
|
|
695 if (interlace)
|
|
696 if (row >= height) {
|
|
697 row = InterlacedOffset[++pass];
|
|
698 while (row >= height)
|
|
699 row = InterlacedOffset[++pass];
|
|
700 }
|
|
701 eip = unwind.eimage + (width * height * 3 * slice) + (row * width * 3);
|
|
702 for (j = 0; j < width; j++)
|
|
703 {
|
771
|
704 UChar_Binary pixel =
|
428
|
705 unwind.giffile->SavedImages[slice].RasterBits[(i * width) + j];
|
|
706 *eip++ = cmo->Colors[pixel].Red;
|
|
707 *eip++ = cmo->Colors[pixel].Green;
|
|
708 *eip++ = cmo->Colors[pixel].Blue;
|
|
709 }
|
|
710 row += interlace ? InterlacedJumps[pass] : 1;
|
|
711 }
|
|
712 }
|
|
713
|
|
714 /* now instantiate */
|
442
|
715 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
716 init_image_instance_from_eimage,
|
|
717 (ii, width, height, unwind.giffile->ImageCount, unwind.eimage, dest_mask,
|
|
718 instantiator, domain));
|
|
719 }
|
|
720
|
|
721 /* We read the gif successfully. If we have more than one slice then
|
|
722 animate the gif. */
|
|
723 if (unwind.giffile->ImageCount > 1)
|
|
724 {
|
|
725 /* See if there is a timeout value. In theory there could be one
|
|
726 for every image - but that makes the implementation way to
|
|
727 complicated for now so we just take the first. */
|
|
728 unsigned short timeout = 0;
|
|
729 Lisp_Object tid;
|
|
730
|
|
731 if (unwind.giffile->SavedImages[0].Function == GRAPHICS_EXT_FUNC_CODE
|
|
732 &&
|
|
733 unwind.giffile->SavedImages[0].ExtensionBlockCount)
|
|
734 {
|
|
735 timeout = (unsigned short)
|
438
|
736 ((unwind.giffile->SavedImages[0].ExtensionBlocks[0].Bytes[2] << 8) +
|
428
|
737 unwind.giffile-> SavedImages[0].ExtensionBlocks[0].Bytes[1]) * 10;
|
|
738 }
|
|
739
|
|
740 /* Too short a timeout will crucify us performance-wise. */
|
|
741 tid = add_glyph_animated_timeout (timeout > 10 ? timeout : 10, image_instance);
|
|
742
|
|
743 if (!NILP (tid))
|
|
744 IMAGE_INSTANCE_PIXMAP_TIMEOUT (ii) = XINT (tid);
|
|
745 }
|
438
|
746
|
771
|
747 unbind_to (speccount);
|
428
|
748 }
|
|
749
|
|
750 #endif /* HAVE_GIF */
|
|
751
|
|
752
|
|
753 #ifdef HAVE_PNG
|
|
754
|
|
755 /**********************************************************************
|
|
756 * PNG *
|
|
757 **********************************************************************/
|
|
758 static void
|
|
759 png_validate (Lisp_Object instantiator)
|
|
760 {
|
|
761 file_or_data_must_be_present (instantiator);
|
|
762 }
|
|
763
|
|
764 static Lisp_Object
|
442
|
765 png_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
766 Lisp_Object dest_mask)
|
428
|
767 {
|
|
768 return simple_image_type_normalize (inst, console_type, Qpng);
|
|
769 }
|
|
770
|
|
771 static int
|
|
772 png_possible_dest_types (void)
|
|
773 {
|
|
774 return IMAGE_COLOR_PIXMAP_MASK;
|
|
775 }
|
|
776
|
|
777 struct png_memory_storage
|
|
778 {
|
647
|
779 const UChar_Binary *bytes; /* The data */
|
665
|
780 Bytecount len; /* How big is it? */
|
|
781 Bytecount index; /* Where are we? */
|
428
|
782 };
|
|
783
|
|
784 static void
|
647
|
785 png_read_from_memory (png_structp png_ptr, png_bytep data,
|
|
786 png_size_t length)
|
428
|
787 {
|
|
788 struct png_memory_storage *tbr =
|
|
789 (struct png_memory_storage *) png_get_io_ptr (png_ptr);
|
|
790
|
665
|
791 if ((Bytecount) length > (tbr->len - tbr->index))
|
428
|
792 png_error (png_ptr, (png_const_charp) "Read Error");
|
647
|
793 memcpy (data, tbr->bytes + tbr->index,length);
|
428
|
794 tbr->index = tbr->index + length;
|
|
795 }
|
|
796
|
|
797 struct png_error_struct
|
|
798 {
|
442
|
799 const char *err_str;
|
428
|
800 jmp_buf setjmp_buffer; /* for return to caller */
|
|
801 };
|
|
802
|
|
803 /* jh 98/03/12 - #### AARRRGH! libpng includes jmp_buf inside its own
|
|
804 structure, and there are cases where the size can be different from
|
|
805 between inside the library, and inside the code! To do an end run
|
|
806 around this, use our own error functions, and don't rely on things
|
|
807 passed in the png_ptr to them. This is an ugly hack and must
|
|
808 go away when the lisp engine is threaded! */
|
|
809 static struct png_error_struct png_err_stct;
|
|
810
|
|
811 static void
|
|
812 png_error_func (png_structp png_ptr, png_const_charp msg)
|
|
813 {
|
|
814 png_err_stct.err_str = msg;
|
|
815 longjmp (png_err_stct.setjmp_buffer, 1);
|
|
816 }
|
|
817
|
|
818 static void
|
|
819 png_warning_func (png_structp png_ptr, png_const_charp msg)
|
|
820 {
|
|
821 warn_when_safe (Qpng, Qinfo, "%s", msg);
|
|
822 }
|
|
823
|
|
824 struct png_unwind_data
|
|
825 {
|
|
826 FILE *instream;
|
771
|
827 UChar_Binary *eimage;
|
428
|
828 png_structp png_ptr;
|
|
829 png_infop info_ptr;
|
|
830 };
|
|
831
|
|
832 static Lisp_Object
|
|
833 png_instantiate_unwind (Lisp_Object unwind_obj)
|
|
834 {
|
|
835 struct png_unwind_data *data =
|
|
836 (struct png_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
837
|
|
838 free_opaque_ptr (unwind_obj);
|
|
839 if (data->png_ptr)
|
|
840 png_destroy_read_struct (&(data->png_ptr), &(data->info_ptr), (png_infopp)NULL);
|
|
841 if (data->instream)
|
771
|
842 retry_fclose (data->instream);
|
428
|
843
|
1726
|
844 if (data->eimage)
|
|
845 xfree(data->eimage, UChar_Binary *);
|
428
|
846
|
|
847 return Qnil;
|
|
848 }
|
|
849
|
|
850 static void
|
|
851 png_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
852 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
853 int dest_mask, Lisp_Object domain)
|
|
854 {
|
440
|
855 Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
428
|
856 struct png_unwind_data unwind;
|
|
857 int speccount = specpdl_depth ();
|
|
858 int height, width;
|
|
859 struct png_memory_storage tbr; /* Data to be read */
|
|
860
|
|
861 /* PNG variables */
|
|
862 png_structp png_ptr;
|
|
863 png_infop info_ptr;
|
|
864
|
|
865 /* Initialize all PNG structures */
|
|
866 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, (void*)&png_err_stct,
|
|
867 png_error_func, png_warning_func);
|
|
868 if (!png_ptr)
|
|
869 signal_image_error ("Error obtaining memory for png_read", instantiator);
|
|
870 info_ptr = png_create_info_struct (png_ptr);
|
|
871 if (!info_ptr)
|
|
872 {
|
|
873 png_destroy_read_struct (&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
|
|
874 signal_image_error ("Error obtaining memory for png_read", instantiator);
|
|
875 }
|
|
876
|
|
877 xzero (unwind);
|
|
878 unwind.png_ptr = png_ptr;
|
|
879 unwind.info_ptr = info_ptr;
|
|
880
|
|
881 record_unwind_protect (png_instantiate_unwind, make_opaque_ptr (&unwind));
|
|
882
|
|
883 /* This code is a mixture of stuff from Ben's GIF/JPEG stuff from
|
|
884 this file, example.c from the libpng 0.81 distribution, and the
|
|
885 pngtopnm sources. -WMP-
|
|
886 */
|
|
887 /* It has been further modified to handle the API changes for 0.96,
|
|
888 and is no longer usable for previous versions. jh
|
|
889 */
|
|
890
|
|
891 /* Set the jmp_buf return context for png_error ... if this returns !0, then
|
|
892 we ran into a problem somewhere, and need to clean up after ourselves. */
|
|
893 if (setjmp (png_err_stct.setjmp_buffer))
|
|
894 {
|
|
895 /* Something blew up: just display the error (cleanup happens in the unwind) */
|
|
896 signal_image_error_2 ("Error decoding PNG",
|
|
897 build_string(png_err_stct.err_str),
|
|
898 instantiator);
|
|
899 }
|
|
900
|
|
901 /* Initialize the IO layer and read in header information */
|
|
902 {
|
|
903 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
647
|
904 const UChar_Binary *bytes;
|
665
|
905 Bytecount len;
|
428
|
906
|
|
907 assert (!NILP (data));
|
|
908
|
|
909 /* #### This is a definite problem under Mule due to the amount of
|
|
910 stack data it might allocate. Need to think about using Lstreams */
|
440
|
911 TO_EXTERNAL_FORMAT (LISP_STRING, data, ALLOCA, (bytes, len), Qbinary);
|
428
|
912 tbr.bytes = bytes;
|
|
913 tbr.len = len;
|
|
914 tbr.index = 0;
|
|
915 png_set_read_fn (png_ptr,(void *) &tbr, png_read_from_memory);
|
|
916 }
|
|
917
|
|
918 png_read_info (png_ptr, info_ptr);
|
|
919
|
|
920 {
|
|
921 int y;
|
771
|
922 UChar_Binary **row_pointers;
|
428
|
923 height = info_ptr->height;
|
|
924 width = info_ptr->width;
|
|
925
|
|
926 /* Wow, allocate all the memory. Truly, exciting. */
|
771
|
927 unwind.eimage = xnew_array_and_zero (UChar_Binary, width * height * 3);
|
428
|
928 /* libpng expects that the image buffer passed in contains a
|
|
929 picture to draw on top of if the png has any transparencies.
|
|
930 This could be a good place to pass that in... */
|
|
931
|
|
932 row_pointers = xnew_array (png_byte *, height);
|
|
933
|
|
934 for (y = 0; y < height; y++)
|
|
935 row_pointers[y] = unwind.eimage + (width * 3 * y);
|
|
936
|
|
937 {
|
|
938 /* if the png specifies a background chunk, go ahead and
|
|
939 use it, else use what we can get from the default face. */
|
|
940 png_color_16 my_background, *image_background;
|
|
941 Lisp_Object bkgd = Qnil;
|
|
942
|
|
943 my_background.red = 0x7fff;
|
|
944 my_background.green = 0x7fff;
|
|
945 my_background.blue = 0x7fff;
|
|
946 bkgd = FACE_BACKGROUND (Vdefault_face, domain);
|
|
947 if (!COLOR_INSTANCEP (bkgd))
|
|
948 {
|
|
949 warn_when_safe (Qpng, Qinfo, "Couldn't get background color!");
|
|
950 }
|
|
951 else
|
|
952 {
|
440
|
953 Lisp_Color_Instance *c;
|
428
|
954 Lisp_Object rgblist;
|
|
955
|
|
956 c = XCOLOR_INSTANCE (bkgd);
|
|
957 rgblist = MAYBE_LISP_DEVMETH (XDEVICE (c->device),
|
|
958 color_instance_rgb_components,
|
|
959 (c));
|
442
|
960 my_background.red = (unsigned short) XINT (XCAR (rgblist));
|
|
961 my_background.green = (unsigned short) XINT (XCAR (XCDR (rgblist)));
|
|
962 my_background.blue = (unsigned short) XINT (XCAR (XCDR (XCDR (rgblist))));
|
428
|
963 }
|
|
964
|
|
965 if (png_get_bKGD (png_ptr, info_ptr, &image_background))
|
|
966 png_set_background (png_ptr, image_background,
|
|
967 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
|
|
968 else
|
|
969 png_set_background (png_ptr, &my_background,
|
|
970 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
|
|
971 }
|
|
972
|
|
973 /* Now that we're using EImage, ask for 8bit RGB triples for any type
|
|
974 of image*/
|
|
975 /* convert palette images to full RGB */
|
|
976 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
|
|
977 png_set_expand (png_ptr);
|
|
978 /* send grayscale images to RGB too */
|
|
979 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
|
|
980 info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
|
981 png_set_gray_to_rgb (png_ptr);
|
|
982 /* we can't handle alpha values */
|
|
983 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
|
|
984 png_set_strip_alpha (png_ptr);
|
|
985 /* tell libpng to strip 16 bit depth files down to 8 bits */
|
|
986 if (info_ptr->bit_depth == 16)
|
|
987 png_set_strip_16 (png_ptr);
|
|
988 /* if the image is < 8 bits, pad it out */
|
|
989 if (info_ptr->bit_depth < 8)
|
|
990 {
|
|
991 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)
|
|
992 png_set_expand (png_ptr);
|
|
993 else
|
|
994 png_set_packing (png_ptr);
|
|
995 }
|
|
996
|
|
997 png_read_image (png_ptr, row_pointers);
|
|
998 png_read_end (png_ptr, info_ptr);
|
|
999
|
793
|
1000 #if 1 /* def PNG_SHOW_COMMENTS */
|
428
|
1001 /* ####
|
|
1002 * I turn this off by default now, because the !%^@#!% comments
|
|
1003 * show up every time the image is instantiated, which can get
|
|
1004 * really really annoying. There should be some way to pass this
|
|
1005 * type of data down into the glyph code, where you can get to it
|
|
1006 * from lisp anyway. - WMP
|
|
1007 */
|
793
|
1008 /* #### I've turned this on, since these warnings are now
|
|
1009 unobtrusive. */
|
428
|
1010 {
|
|
1011 int i;
|
|
1012
|
|
1013 for (i = 0 ; i < info_ptr->num_text ; i++)
|
|
1014 {
|
|
1015 /* How paranoid do I have to be about no trailing NULLs, and
|
|
1016 using (int)info_ptr->text[i].text_length, and strncpy and a temp
|
|
1017 string somewhere? */
|
|
1018
|
|
1019 warn_when_safe (Qpng, Qinfo, "%s - %s",
|
|
1020 info_ptr->text[i].key,
|
|
1021 info_ptr->text[i].text);
|
|
1022 }
|
|
1023 }
|
|
1024 #endif
|
|
1025
|
1726
|
1026 xfree (row_pointers, UChar_Binary **);
|
428
|
1027 }
|
|
1028
|
|
1029 /* now instantiate */
|
442
|
1030 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
1031 init_image_instance_from_eimage,
|
|
1032 (ii, width, height, 1, unwind.eimage, dest_mask,
|
|
1033 instantiator, domain));
|
|
1034
|
|
1035 /* This will clean up everything else. */
|
771
|
1036 unbind_to (speccount);
|
428
|
1037 }
|
|
1038
|
|
1039 #endif /* HAVE_PNG */
|
|
1040
|
|
1041
|
|
1042 #ifdef HAVE_TIFF
|
|
1043 #include "tiffio.h"
|
|
1044
|
|
1045 /**********************************************************************
|
|
1046 * TIFF *
|
|
1047 **********************************************************************/
|
|
1048 static void
|
|
1049 tiff_validate (Lisp_Object instantiator)
|
|
1050 {
|
|
1051 file_or_data_must_be_present (instantiator);
|
|
1052 }
|
|
1053
|
|
1054 static Lisp_Object
|
442
|
1055 tiff_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
1056 Lisp_Object dest_mask)
|
428
|
1057 {
|
|
1058 return simple_image_type_normalize (inst, console_type, Qtiff);
|
|
1059 }
|
|
1060
|
|
1061 static int
|
|
1062 tiff_possible_dest_types (void)
|
|
1063 {
|
|
1064 return IMAGE_COLOR_PIXMAP_MASK;
|
|
1065 }
|
|
1066
|
|
1067 struct tiff_unwind_data
|
|
1068 {
|
771
|
1069 UChar_Binary *eimage;
|
428
|
1070 /* Object that holds the decoded data from a TIFF file */
|
|
1071 TIFF *tiff;
|
|
1072 };
|
|
1073
|
|
1074 static Lisp_Object
|
|
1075 tiff_instantiate_unwind (Lisp_Object unwind_obj)
|
|
1076 {
|
|
1077 struct tiff_unwind_data *data =
|
|
1078 (struct tiff_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
1079
|
|
1080 free_opaque_ptr (unwind_obj);
|
|
1081 if (data->tiff)
|
|
1082 {
|
|
1083 TIFFClose(data->tiff);
|
|
1084 }
|
|
1085 if (data->eimage)
|
1726
|
1086 xfree (data->eimage, UChar_Binary *);
|
428
|
1087
|
|
1088 return Qnil;
|
|
1089 }
|
|
1090
|
|
1091 typedef struct tiff_memory_storage
|
|
1092 {
|
647
|
1093 UChar_Binary *bytes; /* The data */
|
665
|
1094 Bytecount len; /* How big is it? */
|
|
1095 Bytecount index; /* Where are we? */
|
428
|
1096 } tiff_memory_storage;
|
|
1097
|
|
1098 static size_t
|
647
|
1099 tiff_memory_read (thandle_t data, tdata_t buf, tsize_t size)
|
428
|
1100 {
|
647
|
1101 tiff_memory_storage *mem = (tiff_memory_storage *) data;
|
428
|
1102
|
665
|
1103 if ((Bytecount) size > (mem->len - mem->index))
|
428
|
1104 return (size_t) -1;
|
647
|
1105 memcpy (buf, mem->bytes + mem->index, size);
|
428
|
1106 mem->index = mem->index + size;
|
|
1107 return size;
|
|
1108 }
|
|
1109
|
647
|
1110 static size_t
|
|
1111 tiff_memory_write (thandle_t data, tdata_t buf, tsize_t size)
|
428
|
1112 {
|
|
1113 abort();
|
|
1114 return 0; /* Shut up warnings. */
|
|
1115 }
|
|
1116
|
647
|
1117 static toff_t
|
|
1118 tiff_memory_seek (thandle_t data, toff_t off, int whence)
|
428
|
1119 {
|
647
|
1120 tiff_memory_storage *mem = (tiff_memory_storage *) data;
|
428
|
1121 int newidx;
|
647
|
1122 switch(whence)
|
|
1123 {
|
|
1124 case SEEK_SET:
|
|
1125 newidx = off;
|
|
1126 break;
|
|
1127 case SEEK_END:
|
|
1128 newidx = mem->len + off;
|
|
1129 break;
|
|
1130 case SEEK_CUR:
|
|
1131 newidx = mem->index + off;
|
|
1132 break;
|
|
1133 default:
|
|
1134 fprintf (stderr, "Eh? invalid seek mode in tiff_memory_seek\n");
|
|
1135 return (toff_t) -1;
|
|
1136 }
|
428
|
1137
|
|
1138 if ((newidx > mem->len) || (newidx < 0))
|
593
|
1139 return (toff_t) -1;
|
428
|
1140
|
|
1141 mem->index = newidx;
|
|
1142 return newidx;
|
|
1143 }
|
|
1144
|
|
1145 static int
|
647
|
1146 tiff_memory_close (thandle_t data)
|
428
|
1147 {
|
|
1148 return 0;
|
|
1149 }
|
|
1150
|
|
1151 static int
|
647
|
1152 tiff_map_noop (thandle_t data, tdata_t* pbase, toff_t* psize)
|
428
|
1153 {
|
|
1154 return 0;
|
|
1155 }
|
|
1156
|
|
1157 static void
|
647
|
1158 tiff_unmap_noop (thandle_t data, tdata_t pbase, toff_t psize)
|
428
|
1159 {
|
|
1160 return;
|
|
1161 }
|
|
1162
|
|
1163 static toff_t
|
647
|
1164 tiff_memory_size (thandle_t data)
|
428
|
1165 {
|
|
1166 tiff_memory_storage *mem = (tiff_memory_storage*)data;
|
|
1167 return mem->len;
|
|
1168 }
|
|
1169
|
|
1170 struct tiff_error_struct
|
|
1171 {
|
438
|
1172 #ifdef HAVE_VSNPRINTF
|
428
|
1173 char err_str[256];
|
|
1174 #else
|
|
1175 char err_str[1024]; /* return the error string */
|
|
1176 #endif
|
|
1177 jmp_buf setjmp_buffer; /* for return to caller */
|
|
1178 };
|
|
1179
|
|
1180 /* jh 98/03/12 - ###This struct for passing data to the error functions
|
|
1181 is an ugly hack caused by the fact that libtiff (as of v3.4) doesn't
|
|
1182 have any place to store error func data. This should be rectified
|
|
1183 before XEmacs gets threads! */
|
|
1184 static struct tiff_error_struct tiff_err_data;
|
|
1185
|
|
1186 static void
|
647
|
1187 tiff_error_func (const char *module, const char *fmt, ...)
|
428
|
1188 {
|
|
1189 va_list vargs;
|
|
1190
|
|
1191 va_start (vargs, fmt);
|
438
|
1192 #ifdef HAVE_VSNPRINTF
|
428
|
1193 vsnprintf (tiff_err_data.err_str, 255, fmt, vargs);
|
|
1194 #else
|
|
1195 /* pray this doesn't overflow... */
|
|
1196 vsprintf (tiff_err_data.err_str, fmt, vargs);
|
|
1197 #endif
|
|
1198 va_end (vargs);
|
|
1199 /* return to setjmp point */
|
|
1200 longjmp (tiff_err_data.setjmp_buffer, 1);
|
|
1201 }
|
|
1202
|
|
1203 static void
|
647
|
1204 tiff_warning_func (const char *module, const char *fmt, ...)
|
428
|
1205 {
|
|
1206 va_list vargs;
|
438
|
1207 #ifdef HAVE_VSNPRINTF
|
428
|
1208 char warn_str[256];
|
|
1209 #else
|
|
1210 char warn_str[1024];
|
|
1211 #endif
|
|
1212
|
|
1213 va_start (vargs, fmt);
|
438
|
1214 #ifdef HAVE_VSNPRINTF
|
428
|
1215 vsnprintf (warn_str, 255, fmt, vargs);
|
|
1216 #else
|
|
1217 vsprintf (warn_str, fmt, vargs);
|
|
1218 #endif
|
|
1219 va_end (vargs);
|
|
1220 warn_when_safe (Qtiff, Qinfo, "%s - %s",
|
|
1221 module, warn_str);
|
|
1222 }
|
|
1223
|
|
1224 static void
|
|
1225 tiff_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
1226 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
1227 int dest_mask, Lisp_Object domain)
|
|
1228 {
|
440
|
1229 Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
428
|
1230 tiff_memory_storage mem_struct;
|
|
1231 /* It is OK for the unwind data to be local to this function,
|
|
1232 because the unwind-protect is always executed when this
|
|
1233 stack frame is still valid. */
|
|
1234 struct tiff_unwind_data unwind;
|
|
1235 int speccount = specpdl_depth ();
|
|
1236 uint32 width, height;
|
|
1237
|
|
1238 xzero (unwind);
|
|
1239 record_unwind_protect (tiff_instantiate_unwind, make_opaque_ptr (&unwind));
|
|
1240
|
|
1241 /* set up error facilities */
|
|
1242 if (setjmp (tiff_err_data.setjmp_buffer))
|
|
1243 {
|
|
1244 /* An error was signaled. No clean up is needed, as unwind handles that
|
|
1245 for us. Just pass the error along. */
|
|
1246 signal_image_error_2 ("TIFF decoding error",
|
|
1247 build_string(tiff_err_data.err_str),
|
|
1248 instantiator);
|
|
1249 }
|
|
1250 TIFFSetErrorHandler ((TIFFErrorHandler)tiff_error_func);
|
|
1251 TIFFSetWarningHandler ((TIFFErrorHandler)tiff_warning_func);
|
|
1252 {
|
|
1253 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
647
|
1254 UChar_Binary *bytes;
|
665
|
1255 Bytecount len;
|
428
|
1256
|
|
1257 uint32 *raster;
|
771
|
1258 UChar_Binary *ep;
|
428
|
1259
|
|
1260 assert (!NILP (data));
|
|
1261
|
|
1262 /* #### This is a definite problem under Mule due to the amount of
|
|
1263 stack data it might allocate. Think about Lstreams... */
|
440
|
1264 TO_EXTERNAL_FORMAT (LISP_STRING, data,
|
|
1265 ALLOCA, (bytes, len),
|
|
1266 Qbinary);
|
428
|
1267 mem_struct.bytes = bytes;
|
|
1268 mem_struct.len = len;
|
|
1269 mem_struct.index = 0;
|
|
1270
|
442
|
1271 unwind.tiff = TIFFClientOpen ("memfile", "r", (thandle_t) &mem_struct,
|
428
|
1272 (TIFFReadWriteProc)tiff_memory_read,
|
|
1273 (TIFFReadWriteProc)tiff_memory_write,
|
|
1274 tiff_memory_seek, tiff_memory_close, tiff_memory_size,
|
|
1275 tiff_map_noop, tiff_unmap_noop);
|
|
1276 if (!unwind.tiff)
|
440
|
1277 signal_image_error ("Insufficient memory to instantiate TIFF image", instantiator);
|
428
|
1278
|
|
1279 TIFFGetField (unwind.tiff, TIFFTAG_IMAGEWIDTH, &width);
|
|
1280 TIFFGetField (unwind.tiff, TIFFTAG_IMAGELENGTH, &height);
|
771
|
1281 unwind.eimage = (UChar_Binary *) xmalloc (width * height * 3);
|
428
|
1282
|
440
|
1283 /* #### This is little more than proof-of-concept/function testing.
|
428
|
1284 It needs to be reimplemented via scanline reads for both memory
|
|
1285 compactness. */
|
|
1286 raster = (uint32*) _TIFFmalloc (width * height * sizeof (uint32));
|
|
1287 if (raster != NULL)
|
|
1288 {
|
647
|
1289 int i, j;
|
428
|
1290 uint32 *rp;
|
|
1291 ep = unwind.eimage;
|
|
1292 rp = raster;
|
|
1293 if (TIFFReadRGBAImage (unwind.tiff, width, height, raster, 0))
|
|
1294 {
|
|
1295 for (i = height - 1; i >= 0; i--)
|
|
1296 {
|
|
1297 /* This is to get around weirdness in the libtiff library where properly
|
|
1298 made TIFFs will come out upside down. libtiff bug or jhod-brainlock? */
|
|
1299 rp = raster + (i * width);
|
647
|
1300 for (j = 0; j < (int) width; j++)
|
428
|
1301 {
|
771
|
1302 *ep++ = (UChar_Binary)TIFFGetR(*rp);
|
|
1303 *ep++ = (UChar_Binary)TIFFGetG(*rp);
|
|
1304 *ep++ = (UChar_Binary)TIFFGetB(*rp);
|
428
|
1305 rp++;
|
|
1306 }
|
|
1307 }
|
|
1308 }
|
|
1309 _TIFFfree (raster);
|
|
1310 } else
|
|
1311 signal_image_error ("Unable to allocate memory for TIFFReadRGBA", instantiator);
|
|
1312
|
|
1313 }
|
|
1314
|
|
1315 /* now instantiate */
|
442
|
1316 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
1317 init_image_instance_from_eimage,
|
|
1318 (ii, width, height, 1, unwind.eimage, dest_mask,
|
|
1319 instantiator, domain));
|
|
1320
|
771
|
1321 unbind_to (speccount);
|
428
|
1322 }
|
|
1323
|
|
1324 #endif /* HAVE_TIFF */
|
|
1325
|
|
1326
|
|
1327 /************************************************************************/
|
|
1328 /* initialization */
|
|
1329 /************************************************************************/
|
|
1330
|
|
1331 void
|
|
1332 syms_of_glyphs_eimage (void)
|
|
1333 {
|
|
1334 }
|
|
1335
|
|
1336 void
|
|
1337 image_instantiator_format_create_glyphs_eimage (void)
|
|
1338 {
|
|
1339 /* image-instantiator types */
|
|
1340 #ifdef HAVE_JPEG
|
|
1341 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (jpeg, "jpeg");
|
|
1342
|
|
1343 IIFORMAT_HAS_METHOD (jpeg, validate);
|
|
1344 IIFORMAT_HAS_METHOD (jpeg, normalize);
|
|
1345 IIFORMAT_HAS_METHOD (jpeg, possible_dest_types);
|
|
1346 IIFORMAT_HAS_METHOD (jpeg, instantiate);
|
|
1347
|
|
1348 IIFORMAT_VALID_KEYWORD (jpeg, Q_data, check_valid_string);
|
|
1349 IIFORMAT_VALID_KEYWORD (jpeg, Q_file, check_valid_string);
|
|
1350 #endif
|
|
1351
|
|
1352 #ifdef HAVE_GIF
|
|
1353 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (gif, "gif");
|
|
1354
|
|
1355 IIFORMAT_HAS_METHOD (gif, validate);
|
|
1356 IIFORMAT_HAS_METHOD (gif, normalize);
|
|
1357 IIFORMAT_HAS_METHOD (gif, possible_dest_types);
|
|
1358 IIFORMAT_HAS_METHOD (gif, instantiate);
|
|
1359
|
|
1360 IIFORMAT_VALID_KEYWORD (gif, Q_data, check_valid_string);
|
|
1361 IIFORMAT_VALID_KEYWORD (gif, Q_file, check_valid_string);
|
|
1362 #endif
|
|
1363
|
|
1364 #ifdef HAVE_PNG
|
|
1365 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (png, "png");
|
|
1366
|
|
1367 IIFORMAT_HAS_METHOD (png, validate);
|
|
1368 IIFORMAT_HAS_METHOD (png, normalize);
|
|
1369 IIFORMAT_HAS_METHOD (png, possible_dest_types);
|
|
1370 IIFORMAT_HAS_METHOD (png, instantiate);
|
|
1371
|
|
1372 IIFORMAT_VALID_KEYWORD (png, Q_data, check_valid_string);
|
|
1373 IIFORMAT_VALID_KEYWORD (png, Q_file, check_valid_string);
|
|
1374 #endif
|
|
1375
|
|
1376 #ifdef HAVE_TIFF
|
|
1377 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (tiff, "tiff");
|
|
1378
|
|
1379 IIFORMAT_HAS_METHOD (tiff, validate);
|
|
1380 IIFORMAT_HAS_METHOD (tiff, normalize);
|
|
1381 IIFORMAT_HAS_METHOD (tiff, possible_dest_types);
|
|
1382 IIFORMAT_HAS_METHOD (tiff, instantiate);
|
|
1383
|
|
1384 IIFORMAT_VALID_KEYWORD (tiff, Q_data, check_valid_string);
|
|
1385 IIFORMAT_VALID_KEYWORD (tiff, Q_file, check_valid_string);
|
|
1386 #endif
|
|
1387
|
|
1388 }
|
|
1389
|
|
1390 void
|
|
1391 vars_of_glyphs_eimage (void)
|
|
1392 {
|
|
1393 #ifdef HAVE_JPEG
|
|
1394 Fprovide (Qjpeg);
|
|
1395 #endif
|
|
1396
|
|
1397 #ifdef HAVE_GIF
|
|
1398 Fprovide (Qgif);
|
|
1399 #endif
|
|
1400
|
|
1401 #ifdef HAVE_PNG
|
|
1402 Fprovide (Qpng);
|
|
1403 #endif
|
|
1404
|
|
1405 #ifdef HAVE_TIFF
|
|
1406 Fprovide (Qtiff);
|
|
1407 #endif
|
|
1408
|
|
1409 }
|