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"
|
|
51 #include "device.h"
|
|
52 #include "faces.h"
|
|
53 #include "glyphs.h"
|
|
54 #include "objects.h"
|
|
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
|
|
161 if (data->eimage) xfree (data->eimage);
|
|
162
|
|
163 return Qnil;
|
|
164 }
|
|
165
|
|
166 /*
|
|
167 * ERROR HANDLING:
|
|
168 *
|
|
169 * The JPEG library's standard error handler (jerror.c) is divided into
|
|
170 * several "methods" which you can override individually. This lets you
|
|
171 * adjust the behavior without duplicating a lot of code, which you might
|
|
172 * have to update with each future release.
|
|
173 *
|
|
174 * Our example here shows how to override the "error_exit" method so that
|
|
175 * control is returned to the library's caller when a fatal error occurs,
|
|
176 * rather than calling exit() as the standard error_exit method does.
|
|
177 *
|
|
178 * We use C's setjmp/longjmp facility to return control. This means that the
|
|
179 * routine which calls the JPEG library must first execute a setjmp() call to
|
|
180 * establish the return point. We want the replacement error_exit to do a
|
|
181 * longjmp(). But we need to make the setjmp buffer accessible to the
|
|
182 * error_exit routine. To do this, we make a private extension of the
|
|
183 * standard JPEG error handler object. (If we were using C++, we'd say we
|
|
184 * were making a subclass of the regular error handler.)
|
|
185 *
|
|
186 * Here's the extended error handler struct:
|
|
187 */
|
|
188
|
|
189 struct my_jpeg_error_mgr
|
|
190 {
|
|
191 struct jpeg_error_mgr pub; /* "public" fields */
|
|
192 jmp_buf setjmp_buffer; /* for return to caller */
|
|
193 };
|
|
194
|
|
195 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
196 METHODDEF(void)
|
|
197 #else
|
|
198 METHODDEF void
|
|
199 #endif
|
|
200 our_init_source (j_decompress_ptr cinfo)
|
|
201 {
|
|
202 }
|
|
203
|
|
204 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
205 METHODDEF(boolean)
|
|
206 #else
|
|
207 METHODDEF boolean
|
|
208 #endif
|
|
209 our_fill_input_buffer (j_decompress_ptr cinfo)
|
|
210 {
|
|
211 /* Insert a fake EOI marker */
|
|
212 struct jpeg_source_mgr *src = cinfo->src;
|
|
213 static JOCTET buffer[2];
|
|
214
|
|
215 buffer[0] = (JOCTET) 0xFF;
|
|
216 buffer[1] = (JOCTET) JPEG_EOI;
|
|
217
|
|
218 src->next_input_byte = buffer;
|
|
219 src->bytes_in_buffer = 2;
|
|
220 return TRUE;
|
|
221 }
|
|
222
|
|
223 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
224 METHODDEF(void)
|
|
225 #else
|
|
226 METHODDEF void
|
|
227 #endif
|
|
228 our_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
|
|
229 {
|
|
230 struct jpeg_source_mgr *src = NULL;
|
|
231
|
|
232 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
233
|
|
234 if (!src)
|
647
|
235 return;
|
|
236 else if (num_bytes > (long) src->bytes_in_buffer)
|
428
|
237 {
|
647
|
238 ERREXIT (cinfo, JERR_INPUT_EOF);
|
|
239 /*NOTREACHED*/
|
|
240 }
|
428
|
241
|
|
242 src->bytes_in_buffer -= num_bytes;
|
|
243 src->next_input_byte += num_bytes;
|
|
244 }
|
|
245
|
|
246 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
247 METHODDEF(void)
|
|
248 #else
|
|
249 METHODDEF void
|
|
250 #endif
|
|
251 our_term_source (j_decompress_ptr cinfo)
|
|
252 {
|
|
253 }
|
|
254
|
|
255 typedef struct
|
|
256 {
|
|
257 struct jpeg_source_mgr pub;
|
|
258 } our_jpeg_source_mgr;
|
|
259
|
|
260 static void
|
665
|
261 jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, Bytecount len)
|
428
|
262 {
|
|
263 struct jpeg_source_mgr *src;
|
|
264
|
|
265 if (cinfo->src == NULL)
|
|
266 { /* first time for this JPEG object? */
|
|
267 cinfo->src = (struct jpeg_source_mgr *)
|
|
268 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
|
|
269 sizeof(our_jpeg_source_mgr));
|
|
270 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
271 src->next_input_byte = data;
|
|
272 }
|
|
273 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
274 src->init_source = our_init_source;
|
|
275 src->fill_input_buffer = our_fill_input_buffer;
|
|
276 src->skip_input_data = our_skip_input_data;
|
|
277 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
|
|
278 src->term_source = our_term_source;
|
|
279 src->bytes_in_buffer = len;
|
|
280 src->next_input_byte = data;
|
|
281 }
|
|
282
|
|
283 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
284 METHODDEF(void)
|
|
285 #else
|
|
286 METHODDEF void
|
|
287 #endif
|
|
288 my_jpeg_error_exit (j_common_ptr cinfo)
|
|
289 {
|
|
290 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
|
|
291 struct my_jpeg_error_mgr *myerr = (struct my_jpeg_error_mgr *) cinfo->err;
|
|
292
|
|
293 /* Return control to the setjmp point */
|
|
294 longjmp (myerr->setjmp_buffer, 1);
|
|
295 }
|
|
296
|
|
297 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
298 METHODDEF(void)
|
|
299 #else
|
|
300 METHODDEF void
|
|
301 #endif
|
|
302 my_jpeg_output_message (j_common_ptr cinfo)
|
|
303 {
|
771
|
304 Extbyte buffer[JMSG_LENGTH_MAX];
|
|
305 Intbyte *intbuf;
|
428
|
306
|
|
307 /* Create the message */
|
|
308 (*cinfo->err->format_message) (cinfo, buffer);
|
771
|
309 EXTERNAL_TO_C_STRING (buffer, intbuf, Qnative);
|
|
310 warn_when_safe (Qjpeg, Qinfo, "%s", intbuf);
|
428
|
311 }
|
|
312
|
|
313 /* The code in this routine is based on example.c from the JPEG library
|
|
314 source code and from gif_instantiate() */
|
|
315 static void
|
|
316 jpeg_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
317 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
318 int dest_mask, Lisp_Object domain)
|
|
319 {
|
440
|
320 Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
428
|
321 /* It is OK for the unwind data to be local to this function,
|
|
322 because the unwind-protect is always executed when this
|
|
323 stack frame is still valid. */
|
|
324 struct jpeg_unwind_data unwind;
|
|
325 int speccount = specpdl_depth ();
|
|
326
|
|
327 /* This struct contains the JPEG decompression parameters and pointers to
|
|
328 * working space (which is allocated as needed by the JPEG library).
|
|
329 */
|
|
330 struct jpeg_decompress_struct cinfo;
|
|
331 /* We use our private extension JPEG error handler.
|
|
332 * Note that this struct must live as long as the main JPEG parameter
|
|
333 * struct, to avoid dangling-pointer problems.
|
|
334 */
|
|
335 struct my_jpeg_error_mgr jerr;
|
|
336
|
|
337 /* Step -1: First record our unwind-protect, which will clean up after
|
|
338 any exit, normal or not */
|
|
339
|
|
340 xzero (unwind);
|
|
341 record_unwind_protect (jpeg_instantiate_unwind, make_opaque_ptr (&unwind));
|
|
342
|
|
343 /* Step 1: allocate and initialize JPEG decompression object */
|
|
344
|
|
345 /* We set up the normal JPEG error routines, then override error_exit. */
|
|
346 cinfo.err = jpeg_std_error (&jerr.pub);
|
|
347 jerr.pub.error_exit = my_jpeg_error_exit;
|
|
348 jerr.pub.output_message = my_jpeg_output_message;
|
|
349
|
|
350 /* Establish the setjmp return context for my_error_exit to use. */
|
|
351 if (setjmp (jerr.setjmp_buffer))
|
|
352 {
|
|
353 /* If we get here, the JPEG code has signaled an error.
|
|
354 * We need to clean up the JPEG object, close the input file, and return.
|
|
355 */
|
|
356
|
|
357 {
|
|
358 Lisp_Object errstring;
|
771
|
359 Extbyte buffer[JMSG_LENGTH_MAX];
|
428
|
360
|
|
361 /* Create the message */
|
|
362 (*cinfo.err->format_message) ((j_common_ptr) &cinfo, buffer);
|
771
|
363 errstring = build_ext_string (buffer, Qnative);
|
428
|
364
|
|
365 signal_image_error_2 ("JPEG decoding error",
|
|
366 errstring, instantiator);
|
|
367 }
|
|
368 }
|
|
369
|
|
370 /* Now we can initialize the JPEG decompression object. */
|
|
371 jpeg_create_decompress (&cinfo);
|
|
372 unwind.cinfo_ptr = &cinfo;
|
|
373
|
|
374 /* Step 2: specify data source (eg, a file) */
|
|
375
|
|
376 {
|
|
377 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
647
|
378 const UChar_Binary *bytes;
|
665
|
379 Bytecount len;
|
428
|
380
|
|
381 /* #### This is a definite problem under Mule due to the amount of
|
|
382 stack data it might allocate. Need to be able to convert and
|
|
383 write out to a file. */
|
440
|
384 TO_EXTERNAL_FORMAT (LISP_STRING, data, ALLOCA, (bytes, len), Qbinary);
|
428
|
385 jpeg_memory_src (&cinfo, (JOCTET *) bytes, len);
|
|
386 }
|
|
387
|
|
388 /* Step 3: read file parameters with jpeg_read_header() */
|
|
389
|
|
390 jpeg_read_header (&cinfo, TRUE);
|
|
391 /* We can ignore the return value from jpeg_read_header since
|
|
392 * (a) suspension is not possible with the stdio data source, and
|
|
393 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
|
|
394 * See libjpeg.doc for more info.
|
|
395 */
|
|
396
|
|
397 {
|
|
398 int jpeg_gray = 0; /* if we're dealing with a grayscale */
|
|
399 /* Step 4: set parameters for decompression. */
|
|
400
|
|
401 /* Now that we're using EImages, send all data as 24bit color.
|
|
402 The backend routine will take care of any necessary reductions.
|
|
403 We do have to handle the grayscale case ourselves, however. */
|
|
404 if (cinfo.jpeg_color_space == JCS_GRAYSCALE)
|
|
405 {
|
|
406 cinfo.out_color_space = JCS_GRAYSCALE;
|
|
407 jpeg_gray = 1;
|
|
408 }
|
|
409 else
|
|
410 {
|
|
411 /* we're relying on the jpeg driver to do any other conversions,
|
|
412 or signal an error if the conversion isn't supported. */
|
|
413 cinfo.out_color_space = JCS_RGB;
|
|
414 }
|
|
415
|
|
416 /* Step 5: Start decompressor */
|
|
417 jpeg_start_decompress (&cinfo);
|
|
418
|
|
419 /* Step 6: Read in the data and put into EImage format (8bit RGB triples)*/
|
|
420
|
771
|
421 unwind.eimage = (UChar_Binary*) xmalloc (cinfo.output_width * cinfo.output_height * 3);
|
428
|
422 if (!unwind.eimage)
|
|
423 signal_image_error("Unable to allocate enough memory for image", instantiator);
|
|
424
|
|
425 {
|
|
426 JSAMPARRAY row_buffer; /* Output row buffer */
|
|
427 JSAMPLE *jp;
|
|
428 int row_stride; /* physical row width in output buffer */
|
771
|
429 UChar_Binary *op = unwind.eimage;
|
428
|
430
|
|
431 /* We may need to do some setup of our own at this point before reading
|
|
432 * the data. After jpeg_start_decompress() we have the correct scaled
|
|
433 * output image dimensions available
|
|
434 * We need to make an output work buffer of the right size.
|
|
435 */
|
|
436 /* JSAMPLEs per row in output buffer. */
|
|
437 row_stride = cinfo.output_width * cinfo.output_components;
|
|
438 /* Make a one-row-high sample array that will go away when done
|
|
439 with image */
|
|
440 row_buffer = ((*cinfo.mem->alloc_sarray)
|
|
441 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1));
|
|
442
|
|
443 /* Here we use the library's state variable cinfo.output_scanline as the
|
|
444 * loop counter, so that we don't have to keep track ourselves.
|
|
445 */
|
|
446 while (cinfo.output_scanline < cinfo.output_height)
|
|
447 {
|
|
448 int i;
|
|
449
|
|
450 /* jpeg_read_scanlines expects an array of pointers to scanlines.
|
|
451 * Here the array is only one element long, but you could ask for
|
|
452 * more than one scanline at a time if that's more convenient.
|
|
453 */
|
|
454 (void) jpeg_read_scanlines (&cinfo, row_buffer, 1);
|
|
455 jp = row_buffer[0];
|
647
|
456 for (i = 0; i < (int) cinfo.output_width; i++)
|
428
|
457 {
|
|
458 int clr;
|
|
459 if (jpeg_gray)
|
|
460 {
|
771
|
461 UChar_Binary val;
|
428
|
462 #if (BITS_IN_JSAMPLE == 8)
|
771
|
463 val = (UChar_Binary) *jp++;
|
428
|
464 #else /* other option is 12 */
|
771
|
465 val = (UChar_Binary) (*jp++ >> 4);
|
428
|
466 #endif
|
|
467 for (clr = 0; clr < 3; clr++) /* copy the same value into RGB */
|
|
468 *op++ = val;
|
|
469 }
|
|
470 else
|
|
471 {
|
|
472 for (clr = 0; clr < 3; clr++)
|
|
473 #if (BITS_IN_JSAMPLE == 8)
|
771
|
474 *op++ = (UChar_Binary)*jp++;
|
428
|
475 #else /* other option is 12 */
|
771
|
476 *op++ = (UChar_Binary)(*jp++ >> 4);
|
428
|
477 #endif
|
|
478 }
|
|
479 }
|
|
480 }
|
|
481 }
|
|
482 }
|
|
483
|
|
484 /* Step 6.5: Create the pixmap and set up the image instance */
|
|
485 /* now instantiate */
|
442
|
486 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
487 init_image_instance_from_eimage,
|
|
488 (ii, cinfo.output_width, cinfo.output_height, 1,
|
|
489 unwind.eimage, dest_mask,
|
|
490 instantiator, domain));
|
|
491
|
|
492 /* Step 7: Finish decompression */
|
|
493
|
|
494 jpeg_finish_decompress (&cinfo);
|
|
495 /* We can ignore the return value since suspension is not possible
|
|
496 * with the stdio data source.
|
|
497 */
|
|
498
|
|
499 /* And we're done! */
|
|
500 /* This will clean up everything else. */
|
771
|
501 unbind_to (speccount);
|
428
|
502 }
|
|
503
|
|
504 #endif /* HAVE_JPEG */
|
|
505
|
|
506 #ifdef HAVE_GIF
|
|
507 /**********************************************************************
|
|
508 * GIF *
|
|
509 **********************************************************************/
|
|
510
|
|
511 #include "gifrlib.h"
|
|
512
|
|
513 static void
|
|
514 gif_validate (Lisp_Object instantiator)
|
|
515 {
|
|
516 file_or_data_must_be_present (instantiator);
|
|
517 }
|
|
518
|
|
519 static Lisp_Object
|
442
|
520 gif_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
521 Lisp_Object dest_mask)
|
428
|
522 {
|
|
523 return simple_image_type_normalize (inst, console_type, Qgif);
|
|
524 }
|
|
525
|
|
526 static int
|
|
527 gif_possible_dest_types (void)
|
|
528 {
|
|
529 return IMAGE_COLOR_PIXMAP_MASK;
|
|
530 }
|
|
531
|
|
532 /* To survive the otherwise baffling complexity of making sure
|
|
533 everything gets cleaned up in the presence of an error, we
|
|
534 use an unwind_protect(). */
|
|
535
|
|
536 struct gif_unwind_data
|
|
537 {
|
771
|
538 UChar_Binary *eimage;
|
428
|
539 /* Object that holds the decoded data from a GIF file */
|
|
540 GifFileType *giffile;
|
|
541 };
|
|
542
|
|
543 static Lisp_Object
|
|
544 gif_instantiate_unwind (Lisp_Object unwind_obj)
|
|
545 {
|
|
546 struct gif_unwind_data *data =
|
|
547 (struct gif_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
548
|
|
549 free_opaque_ptr (unwind_obj);
|
|
550 if (data->giffile)
|
|
551 {
|
|
552 DGifCloseFile (data->giffile);
|
|
553 GifFree(data->giffile);
|
|
554 }
|
647
|
555 if (data->eimage)
|
|
556 xfree (data->eimage);
|
428
|
557
|
|
558 return Qnil;
|
|
559 }
|
|
560
|
|
561 typedef struct gif_memory_storage
|
|
562 {
|
647
|
563 UChar_Binary *bytes; /* The data */
|
665
|
564 Bytecount len; /* How big is it? */
|
|
565 Bytecount index; /* Where are we? */
|
428
|
566 } gif_memory_storage;
|
|
567
|
665
|
568 static Bytecount
|
|
569 gif_read_from_memory (GifByteType *buf, Bytecount size, VoidPtr data)
|
428
|
570 {
|
647
|
571 gif_memory_storage *mem = (gif_memory_storage *) data;
|
428
|
572
|
|
573 if (size > (mem->len - mem->index))
|
647
|
574 return -1;
|
|
575 memcpy (buf, mem->bytes + mem->index, size);
|
428
|
576 mem->index = mem->index + size;
|
|
577 return size;
|
|
578 }
|
|
579
|
|
580 static int
|
647
|
581 gif_memory_close (VoidPtr data)
|
428
|
582 {
|
|
583 return 0;
|
|
584 }
|
|
585
|
|
586 struct gif_error_struct
|
|
587 {
|
771
|
588 const Extbyte *err_str; /* return the error string */
|
428
|
589 jmp_buf setjmp_buffer; /* for return to caller */
|
|
590 };
|
|
591
|
|
592 static void
|
771
|
593 gif_error_func (const Extbyte *err_str, VoidPtr error_ptr)
|
428
|
594 {
|
647
|
595 struct gif_error_struct *error_data = (struct gif_error_struct *) error_ptr;
|
428
|
596
|
|
597 /* return to setjmp point */
|
|
598 error_data->err_str = err_str;
|
|
599 longjmp (error_data->setjmp_buffer, 1);
|
|
600 }
|
|
601
|
|
602 static void
|
|
603 gif_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
604 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
605 int dest_mask, Lisp_Object domain)
|
|
606 {
|
440
|
607 Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
428
|
608 /* It is OK for the unwind data to be local to this function,
|
|
609 because the unwind-protect is always executed when this
|
|
610 stack frame is still valid. */
|
|
611 struct gif_unwind_data unwind;
|
|
612 int speccount = specpdl_depth ();
|
|
613 gif_memory_storage mem_struct;
|
|
614 struct gif_error_struct gif_err;
|
647
|
615 UChar_Binary *bytes;
|
665
|
616 Bytecount len;
|
428
|
617 int height = 0;
|
|
618 int width = 0;
|
|
619
|
|
620 xzero (unwind);
|
|
621 record_unwind_protect (gif_instantiate_unwind, make_opaque_ptr (&unwind));
|
|
622
|
|
623 /* 1. Now decode the data. */
|
|
624
|
|
625 {
|
|
626 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
|
627
|
|
628 assert (!NILP (data));
|
|
629
|
|
630 if (!(unwind.giffile = GifSetup()))
|
440
|
631 signal_image_error ("Insufficient memory to instantiate GIF image", instantiator);
|
428
|
632
|
|
633 /* set up error facilities */
|
|
634 if (setjmp(gif_err.setjmp_buffer))
|
|
635 {
|
|
636 /* An error was signaled. No clean up is needed, as unwind handles that
|
|
637 for us. Just pass the error along. */
|
771
|
638 Intbyte *interr;
|
428
|
639 Lisp_Object errstring;
|
771
|
640 EXTERNAL_TO_C_STRING (gif_err.err_str, interr, Qnative);
|
|
641 errstring = build_msg_intstring (interr);
|
428
|
642 signal_image_error_2 ("GIF decoding error", errstring, instantiator);
|
|
643 }
|
|
644 GifSetErrorFunc(unwind.giffile, (Gif_error_func)gif_error_func, (VoidPtr)&gif_err);
|
|
645
|
440
|
646 TO_EXTERNAL_FORMAT (LISP_STRING, data, ALLOCA, (bytes, len), Qbinary);
|
428
|
647 mem_struct.bytes = bytes;
|
|
648 mem_struct.len = len;
|
|
649 mem_struct.index = 0;
|
|
650 GifSetReadFunc(unwind.giffile, gif_read_from_memory, (VoidPtr)&mem_struct);
|
|
651 GifSetCloseFunc(unwind.giffile, gif_memory_close, (VoidPtr)&mem_struct);
|
|
652 DGifInitRead(unwind.giffile);
|
|
653
|
|
654 /* Then slurp the image into memory, decoding along the way.
|
|
655 The result is the image in a simple one-byte-per-pixel
|
|
656 format (#### the GIF routines only support 8-bit GIFs,
|
|
657 it appears). */
|
|
658 DGifSlurp (unwind.giffile);
|
|
659 }
|
|
660
|
|
661 /* 3. Now create the EImage(s) */
|
|
662 {
|
|
663 ColorMapObject *cmo = unwind.giffile->SColorMap;
|
|
664 int i, j, row, pass, interlace, slice;
|
771
|
665 UChar_Binary *eip;
|
428
|
666 /* interlaced gifs have rows in this order:
|
|
667 0, 8, 16, ..., 4, 12, 20, ..., 2, 6, 10, ..., 1, 3, 5, ... */
|
|
668 static int InterlacedOffset[] = { 0, 4, 2, 1 };
|
|
669 static int InterlacedJumps[] = { 8, 8, 4, 2 };
|
|
670
|
|
671 height = unwind.giffile->SHeight;
|
|
672 width = unwind.giffile->SWidth;
|
771
|
673 unwind.eimage = (UChar_Binary*)
|
428
|
674 xmalloc (width * height * 3 * unwind.giffile->ImageCount);
|
|
675 if (!unwind.eimage)
|
|
676 signal_image_error("Unable to allocate enough memory for image", instantiator);
|
|
677
|
|
678 /* write the data in EImage format (8bit RGB triples) */
|
|
679
|
|
680 for (slice = 0; slice < unwind.giffile->ImageCount; slice++)
|
|
681 {
|
638
|
682 /* We check here that the current image covers the full "screen" size. */
|
428
|
683 if (unwind.giffile->SavedImages[slice].ImageDesc.Height != height
|
|
684 || unwind.giffile->SavedImages[slice].ImageDesc.Width != width
|
|
685 || unwind.giffile->SavedImages[slice].ImageDesc.Left != 0
|
|
686 || unwind.giffile->SavedImages[slice].ImageDesc.Top != 0)
|
|
687 signal_image_error ("Image in GIF file is not full size",
|
|
688 instantiator);
|
|
689
|
|
690 interlace = unwind.giffile->SavedImages[slice].ImageDesc.Interlace;
|
|
691 pass = 0;
|
|
692 row = interlace ? InterlacedOffset[pass] : 0;
|
|
693 eip = unwind.eimage + (width * height * 3 * slice);
|
|
694 for (i = 0; i < height; i++)
|
|
695 {
|
|
696 if (interlace)
|
|
697 if (row >= height) {
|
|
698 row = InterlacedOffset[++pass];
|
|
699 while (row >= height)
|
|
700 row = InterlacedOffset[++pass];
|
|
701 }
|
|
702 eip = unwind.eimage + (width * height * 3 * slice) + (row * width * 3);
|
|
703 for (j = 0; j < width; j++)
|
|
704 {
|
771
|
705 UChar_Binary pixel =
|
428
|
706 unwind.giffile->SavedImages[slice].RasterBits[(i * width) + j];
|
|
707 *eip++ = cmo->Colors[pixel].Red;
|
|
708 *eip++ = cmo->Colors[pixel].Green;
|
|
709 *eip++ = cmo->Colors[pixel].Blue;
|
|
710 }
|
|
711 row += interlace ? InterlacedJumps[pass] : 1;
|
|
712 }
|
|
713 }
|
|
714
|
|
715 /* now instantiate */
|
442
|
716 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
717 init_image_instance_from_eimage,
|
|
718 (ii, width, height, unwind.giffile->ImageCount, unwind.eimage, dest_mask,
|
|
719 instantiator, domain));
|
|
720 }
|
|
721
|
|
722 /* We read the gif successfully. If we have more than one slice then
|
|
723 animate the gif. */
|
|
724 if (unwind.giffile->ImageCount > 1)
|
|
725 {
|
|
726 /* See if there is a timeout value. In theory there could be one
|
|
727 for every image - but that makes the implementation way to
|
|
728 complicated for now so we just take the first. */
|
|
729 unsigned short timeout = 0;
|
|
730 Lisp_Object tid;
|
|
731
|
|
732 if (unwind.giffile->SavedImages[0].Function == GRAPHICS_EXT_FUNC_CODE
|
|
733 &&
|
|
734 unwind.giffile->SavedImages[0].ExtensionBlockCount)
|
|
735 {
|
|
736 timeout = (unsigned short)
|
438
|
737 ((unwind.giffile->SavedImages[0].ExtensionBlocks[0].Bytes[2] << 8) +
|
428
|
738 unwind.giffile-> SavedImages[0].ExtensionBlocks[0].Bytes[1]) * 10;
|
|
739 }
|
|
740
|
|
741 /* Too short a timeout will crucify us performance-wise. */
|
|
742 tid = add_glyph_animated_timeout (timeout > 10 ? timeout : 10, image_instance);
|
|
743
|
|
744 if (!NILP (tid))
|
|
745 IMAGE_INSTANCE_PIXMAP_TIMEOUT (ii) = XINT (tid);
|
|
746 }
|
438
|
747
|
771
|
748 unbind_to (speccount);
|
428
|
749 }
|
|
750
|
|
751 #endif /* HAVE_GIF */
|
|
752
|
|
753
|
|
754 #ifdef HAVE_PNG
|
|
755
|
|
756 /**********************************************************************
|
|
757 * PNG *
|
|
758 **********************************************************************/
|
|
759 static void
|
|
760 png_validate (Lisp_Object instantiator)
|
|
761 {
|
|
762 file_or_data_must_be_present (instantiator);
|
|
763 }
|
|
764
|
|
765 static Lisp_Object
|
442
|
766 png_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
767 Lisp_Object dest_mask)
|
428
|
768 {
|
|
769 return simple_image_type_normalize (inst, console_type, Qpng);
|
|
770 }
|
|
771
|
|
772 static int
|
|
773 png_possible_dest_types (void)
|
|
774 {
|
|
775 return IMAGE_COLOR_PIXMAP_MASK;
|
|
776 }
|
|
777
|
|
778 struct png_memory_storage
|
|
779 {
|
647
|
780 const UChar_Binary *bytes; /* The data */
|
665
|
781 Bytecount len; /* How big is it? */
|
|
782 Bytecount index; /* Where are we? */
|
428
|
783 };
|
|
784
|
|
785 static void
|
647
|
786 png_read_from_memory (png_structp png_ptr, png_bytep data,
|
|
787 png_size_t length)
|
428
|
788 {
|
|
789 struct png_memory_storage *tbr =
|
|
790 (struct png_memory_storage *) png_get_io_ptr (png_ptr);
|
|
791
|
665
|
792 if ((Bytecount) length > (tbr->len - tbr->index))
|
428
|
793 png_error (png_ptr, (png_const_charp) "Read Error");
|
647
|
794 memcpy (data, tbr->bytes + tbr->index,length);
|
428
|
795 tbr->index = tbr->index + length;
|
|
796 }
|
|
797
|
|
798 struct png_error_struct
|
|
799 {
|
442
|
800 const char *err_str;
|
428
|
801 jmp_buf setjmp_buffer; /* for return to caller */
|
|
802 };
|
|
803
|
|
804 /* jh 98/03/12 - #### AARRRGH! libpng includes jmp_buf inside its own
|
|
805 structure, and there are cases where the size can be different from
|
|
806 between inside the library, and inside the code! To do an end run
|
|
807 around this, use our own error functions, and don't rely on things
|
|
808 passed in the png_ptr to them. This is an ugly hack and must
|
|
809 go away when the lisp engine is threaded! */
|
|
810 static struct png_error_struct png_err_stct;
|
|
811
|
|
812 static void
|
|
813 png_error_func (png_structp png_ptr, png_const_charp msg)
|
|
814 {
|
|
815 png_err_stct.err_str = msg;
|
|
816 longjmp (png_err_stct.setjmp_buffer, 1);
|
|
817 }
|
|
818
|
|
819 static void
|
|
820 png_warning_func (png_structp png_ptr, png_const_charp msg)
|
|
821 {
|
|
822 warn_when_safe (Qpng, Qinfo, "%s", msg);
|
|
823 }
|
|
824
|
|
825 struct png_unwind_data
|
|
826 {
|
|
827 FILE *instream;
|
771
|
828 UChar_Binary *eimage;
|
428
|
829 png_structp png_ptr;
|
|
830 png_infop info_ptr;
|
|
831 };
|
|
832
|
|
833 static Lisp_Object
|
|
834 png_instantiate_unwind (Lisp_Object unwind_obj)
|
|
835 {
|
|
836 struct png_unwind_data *data =
|
|
837 (struct png_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
838
|
|
839 free_opaque_ptr (unwind_obj);
|
|
840 if (data->png_ptr)
|
|
841 png_destroy_read_struct (&(data->png_ptr), &(data->info_ptr), (png_infopp)NULL);
|
|
842 if (data->instream)
|
771
|
843 retry_fclose (data->instream);
|
428
|
844
|
|
845 if (data->eimage) xfree(data->eimage);
|
|
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
|
|
1026 xfree (row_pointers);
|
|
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)
|
|
1086 xfree (data->eimage);
|
|
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 }
|