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