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
|
|
5 Copyright (C) 1995, 1996 Ben Wing
|
|
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 #ifdef FILE_CODING
|
|
77 #include "file-coding.h"
|
|
78 #endif
|
|
79
|
|
80 #ifdef HAVE_TIFF
|
|
81 DEFINE_IMAGE_INSTANTIATOR_FORMAT (tiff);
|
|
82 Lisp_Object Qtiff;
|
|
83 #endif
|
|
84
|
|
85 #ifdef HAVE_JPEG
|
|
86 DEFINE_IMAGE_INSTANTIATOR_FORMAT (jpeg);
|
|
87 Lisp_Object Qjpeg;
|
|
88 #endif
|
|
89
|
|
90 #ifdef HAVE_GIF
|
|
91 DEFINE_IMAGE_INSTANTIATOR_FORMAT (gif);
|
|
92 Lisp_Object Qgif;
|
|
93 #endif
|
|
94
|
|
95 #ifdef HAVE_PNG
|
|
96 DEFINE_IMAGE_INSTANTIATOR_FORMAT (png);
|
|
97 Lisp_Object Qpng;
|
|
98 #endif
|
|
99
|
|
100
|
|
101 #ifdef HAVE_JPEG
|
|
102
|
|
103 /**********************************************************************
|
|
104 * JPEG *
|
|
105 **********************************************************************/
|
|
106
|
|
107 #ifdef __cplusplus
|
|
108 extern "C" {
|
|
109 #endif
|
|
110 #include <jpeglib.h>
|
|
111 #include <jerror.h>
|
|
112 #ifdef __cplusplus
|
|
113 }
|
|
114 #endif
|
|
115
|
|
116 /*#define USE_TEMP_FILES_FOR_JPEG_IMAGES 1*/
|
|
117 static void
|
|
118 jpeg_validate (Lisp_Object instantiator)
|
|
119 {
|
|
120 file_or_data_must_be_present (instantiator);
|
|
121 }
|
|
122
|
|
123 static Lisp_Object
|
442
|
124 jpeg_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
125 Lisp_Object dest_mask)
|
428
|
126 {
|
|
127 return simple_image_type_normalize (inst, console_type, Qjpeg);
|
|
128 }
|
|
129
|
|
130 static int
|
|
131 jpeg_possible_dest_types (void)
|
|
132 {
|
|
133 return IMAGE_COLOR_PIXMAP_MASK;
|
|
134 }
|
|
135
|
|
136 /* To survive the otherwise baffling complexity of making sure
|
|
137 everything gets cleaned up in the presence of an error, we
|
|
138 use an unwind_protect(). */
|
|
139
|
|
140 struct jpeg_unwind_data
|
|
141 {
|
|
142 /* Stream that we need to close */
|
|
143 FILE *instream;
|
|
144 /* Object that holds state info for JPEG decoding */
|
|
145 struct jpeg_decompress_struct *cinfo_ptr;
|
|
146 /* EImage data */
|
|
147 unsigned char *eimage;
|
|
148 };
|
|
149
|
|
150 static Lisp_Object
|
|
151 jpeg_instantiate_unwind (Lisp_Object unwind_obj)
|
|
152 {
|
|
153 struct jpeg_unwind_data *data =
|
|
154 (struct jpeg_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
155
|
|
156 free_opaque_ptr (unwind_obj);
|
|
157 if (data->cinfo_ptr)
|
|
158 jpeg_destroy_decompress (data->cinfo_ptr);
|
|
159
|
|
160 if (data->instream)
|
|
161 fclose (data->instream);
|
|
162
|
|
163 if (data->eimage) xfree (data->eimage);
|
|
164
|
|
165 return Qnil;
|
|
166 }
|
|
167
|
|
168 /*
|
|
169 * ERROR HANDLING:
|
|
170 *
|
|
171 * The JPEG library's standard error handler (jerror.c) is divided into
|
|
172 * several "methods" which you can override individually. This lets you
|
|
173 * adjust the behavior without duplicating a lot of code, which you might
|
|
174 * have to update with each future release.
|
|
175 *
|
|
176 * Our example here shows how to override the "error_exit" method so that
|
|
177 * control is returned to the library's caller when a fatal error occurs,
|
|
178 * rather than calling exit() as the standard error_exit method does.
|
|
179 *
|
|
180 * We use C's setjmp/longjmp facility to return control. This means that the
|
|
181 * routine which calls the JPEG library must first execute a setjmp() call to
|
|
182 * establish the return point. We want the replacement error_exit to do a
|
|
183 * longjmp(). But we need to make the setjmp buffer accessible to the
|
|
184 * error_exit routine. To do this, we make a private extension of the
|
|
185 * standard JPEG error handler object. (If we were using C++, we'd say we
|
|
186 * were making a subclass of the regular error handler.)
|
|
187 *
|
|
188 * Here's the extended error handler struct:
|
|
189 */
|
|
190
|
|
191 struct my_jpeg_error_mgr
|
|
192 {
|
|
193 struct jpeg_error_mgr pub; /* "public" fields */
|
|
194 jmp_buf setjmp_buffer; /* for return to caller */
|
|
195 };
|
|
196
|
|
197 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
198 METHODDEF(void)
|
|
199 #else
|
|
200 METHODDEF void
|
|
201 #endif
|
|
202 our_init_source (j_decompress_ptr cinfo)
|
|
203 {
|
|
204 }
|
|
205
|
|
206 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
207 METHODDEF(boolean)
|
|
208 #else
|
|
209 METHODDEF boolean
|
|
210 #endif
|
|
211 our_fill_input_buffer (j_decompress_ptr cinfo)
|
|
212 {
|
|
213 /* Insert a fake EOI marker */
|
|
214 struct jpeg_source_mgr *src = cinfo->src;
|
|
215 static JOCTET buffer[2];
|
|
216
|
|
217 buffer[0] = (JOCTET) 0xFF;
|
|
218 buffer[1] = (JOCTET) JPEG_EOI;
|
|
219
|
|
220 src->next_input_byte = buffer;
|
|
221 src->bytes_in_buffer = 2;
|
|
222 return TRUE;
|
|
223 }
|
|
224
|
|
225 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
226 METHODDEF(void)
|
|
227 #else
|
|
228 METHODDEF void
|
|
229 #endif
|
|
230 our_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
|
|
231 {
|
|
232 struct jpeg_source_mgr *src = NULL;
|
|
233
|
|
234 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
235
|
|
236 if (!src)
|
647
|
237 return;
|
|
238 else if (num_bytes > (long) src->bytes_in_buffer)
|
428
|
239 {
|
647
|
240 ERREXIT (cinfo, JERR_INPUT_EOF);
|
|
241 /*NOTREACHED*/
|
|
242 }
|
428
|
243
|
|
244 src->bytes_in_buffer -= num_bytes;
|
|
245 src->next_input_byte += num_bytes;
|
|
246 }
|
|
247
|
|
248 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
249 METHODDEF(void)
|
|
250 #else
|
|
251 METHODDEF void
|
|
252 #endif
|
|
253 our_term_source (j_decompress_ptr cinfo)
|
|
254 {
|
|
255 }
|
|
256
|
|
257 typedef struct
|
|
258 {
|
|
259 struct jpeg_source_mgr pub;
|
|
260 } our_jpeg_source_mgr;
|
|
261
|
|
262 static void
|
647
|
263 jpeg_memory_src (j_decompress_ptr cinfo, JOCTET *data, Memory_Count len)
|
428
|
264 {
|
|
265 struct jpeg_source_mgr *src;
|
|
266
|
|
267 if (cinfo->src == NULL)
|
|
268 { /* first time for this JPEG object? */
|
|
269 cinfo->src = (struct jpeg_source_mgr *)
|
|
270 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
|
|
271 sizeof(our_jpeg_source_mgr));
|
|
272 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
273 src->next_input_byte = data;
|
|
274 }
|
|
275 src = (struct jpeg_source_mgr *) cinfo->src;
|
|
276 src->init_source = our_init_source;
|
|
277 src->fill_input_buffer = our_fill_input_buffer;
|
|
278 src->skip_input_data = our_skip_input_data;
|
|
279 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
|
|
280 src->term_source = our_term_source;
|
|
281 src->bytes_in_buffer = len;
|
|
282 src->next_input_byte = data;
|
|
283 }
|
|
284
|
|
285 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
286 METHODDEF(void)
|
|
287 #else
|
|
288 METHODDEF void
|
|
289 #endif
|
|
290 my_jpeg_error_exit (j_common_ptr cinfo)
|
|
291 {
|
|
292 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
|
|
293 struct my_jpeg_error_mgr *myerr = (struct my_jpeg_error_mgr *) cinfo->err;
|
|
294
|
|
295 /* Return control to the setjmp point */
|
|
296 longjmp (myerr->setjmp_buffer, 1);
|
|
297 }
|
|
298
|
|
299 #if defined(JPEG_LIB_VERSION) && (JPEG_LIB_VERSION >= 61)
|
|
300 METHODDEF(void)
|
|
301 #else
|
|
302 METHODDEF void
|
|
303 #endif
|
|
304 my_jpeg_output_message (j_common_ptr cinfo)
|
|
305 {
|
|
306 char buffer[JMSG_LENGTH_MAX];
|
|
307
|
|
308 /* Create the message */
|
|
309 (*cinfo->err->format_message) (cinfo, buffer);
|
|
310 warn_when_safe (Qjpeg, Qinfo, "%s", buffer);
|
|
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;
|
|
359 char buffer[JMSG_LENGTH_MAX];
|
|
360
|
|
361 /* Create the message */
|
|
362 (*cinfo.err->format_message) ((j_common_ptr) &cinfo, buffer);
|
|
363 errstring = build_string (buffer);
|
|
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;
|
|
379 Memory_Count 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
|
|
421 unwind.eimage = (unsigned char*) xmalloc (cinfo.output_width * cinfo.output_height * 3);
|
|
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 */
|
|
429 unsigned char *op = unwind.eimage;
|
|
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 {
|
|
461 unsigned char val;
|
|
462 #if (BITS_IN_JSAMPLE == 8)
|
647
|
463 val = (unsigned char) *jp++;
|
428
|
464 #else /* other option is 12 */
|
647
|
465 val = (unsigned char) (*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)
|
|
474 *op++ = (unsigned char)*jp++;
|
|
475 #else /* other option is 12 */
|
|
476 *op++ = (unsigned char)(*jp++ >> 4);
|
|
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. */
|
|
501 unbind_to (speccount, Qnil);
|
|
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 {
|
|
538 unsigned char *eimage;
|
|
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 */
|
|
564 Memory_Count len; /* How big is it? */
|
|
565 Memory_Count index; /* Where are we? */
|
428
|
566 } gif_memory_storage;
|
|
567
|
647
|
568 static Memory_Count
|
|
569 gif_read_from_memory (GifByteType *buf, Memory_Count 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 {
|
442
|
588 const char *err_str; /* return the error string */
|
428
|
589 jmp_buf setjmp_buffer; /* for return to caller */
|
|
590 };
|
|
591
|
|
592 static void
|
647
|
593 gif_error_func (const char *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;
|
|
616 Memory_Count 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. */
|
|
638 Lisp_Object errstring;
|
|
639 errstring = build_string (gif_err.err_str);
|
|
640 signal_image_error_2 ("GIF decoding error", errstring, instantiator);
|
|
641 }
|
|
642 GifSetErrorFunc(unwind.giffile, (Gif_error_func)gif_error_func, (VoidPtr)&gif_err);
|
|
643
|
440
|
644 TO_EXTERNAL_FORMAT (LISP_STRING, data, ALLOCA, (bytes, len), Qbinary);
|
428
|
645 mem_struct.bytes = bytes;
|
|
646 mem_struct.len = len;
|
|
647 mem_struct.index = 0;
|
|
648 GifSetReadFunc(unwind.giffile, gif_read_from_memory, (VoidPtr)&mem_struct);
|
|
649 GifSetCloseFunc(unwind.giffile, gif_memory_close, (VoidPtr)&mem_struct);
|
|
650 DGifInitRead(unwind.giffile);
|
|
651
|
|
652 /* Then slurp the image into memory, decoding along the way.
|
|
653 The result is the image in a simple one-byte-per-pixel
|
|
654 format (#### the GIF routines only support 8-bit GIFs,
|
|
655 it appears). */
|
|
656 DGifSlurp (unwind.giffile);
|
|
657 }
|
|
658
|
|
659 /* 3. Now create the EImage(s) */
|
|
660 {
|
|
661 ColorMapObject *cmo = unwind.giffile->SColorMap;
|
|
662 int i, j, row, pass, interlace, slice;
|
|
663 unsigned char *eip;
|
|
664 /* interlaced gifs have rows in this order:
|
|
665 0, 8, 16, ..., 4, 12, 20, ..., 2, 6, 10, ..., 1, 3, 5, ... */
|
|
666 static int InterlacedOffset[] = { 0, 4, 2, 1 };
|
|
667 static int InterlacedJumps[] = { 8, 8, 4, 2 };
|
|
668
|
|
669 height = unwind.giffile->SHeight;
|
|
670 width = unwind.giffile->SWidth;
|
438
|
671 unwind.eimage = (unsigned char*)
|
428
|
672 xmalloc (width * height * 3 * unwind.giffile->ImageCount);
|
|
673 if (!unwind.eimage)
|
|
674 signal_image_error("Unable to allocate enough memory for image", instantiator);
|
|
675
|
|
676 /* write the data in EImage format (8bit RGB triples) */
|
|
677
|
|
678 for (slice = 0; slice < unwind.giffile->ImageCount; slice++)
|
|
679 {
|
638
|
680 /* We check here that the current image covers the full "screen" size. */
|
428
|
681 if (unwind.giffile->SavedImages[slice].ImageDesc.Height != height
|
|
682 || unwind.giffile->SavedImages[slice].ImageDesc.Width != width
|
|
683 || unwind.giffile->SavedImages[slice].ImageDesc.Left != 0
|
|
684 || unwind.giffile->SavedImages[slice].ImageDesc.Top != 0)
|
|
685 signal_image_error ("Image in GIF file is not full size",
|
|
686 instantiator);
|
|
687
|
|
688 interlace = unwind.giffile->SavedImages[slice].ImageDesc.Interlace;
|
|
689 pass = 0;
|
|
690 row = interlace ? InterlacedOffset[pass] : 0;
|
|
691 eip = unwind.eimage + (width * height * 3 * slice);
|
|
692 for (i = 0; i < height; i++)
|
|
693 {
|
|
694 if (interlace)
|
|
695 if (row >= height) {
|
|
696 row = InterlacedOffset[++pass];
|
|
697 while (row >= height)
|
|
698 row = InterlacedOffset[++pass];
|
|
699 }
|
|
700 eip = unwind.eimage + (width * height * 3 * slice) + (row * width * 3);
|
|
701 for (j = 0; j < width; j++)
|
|
702 {
|
438
|
703 unsigned char pixel =
|
428
|
704 unwind.giffile->SavedImages[slice].RasterBits[(i * width) + j];
|
|
705 *eip++ = cmo->Colors[pixel].Red;
|
|
706 *eip++ = cmo->Colors[pixel].Green;
|
|
707 *eip++ = cmo->Colors[pixel].Blue;
|
|
708 }
|
|
709 row += interlace ? InterlacedJumps[pass] : 1;
|
|
710 }
|
|
711 }
|
|
712
|
|
713 /* now instantiate */
|
442
|
714 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
715 init_image_instance_from_eimage,
|
|
716 (ii, width, height, unwind.giffile->ImageCount, unwind.eimage, dest_mask,
|
|
717 instantiator, domain));
|
|
718 }
|
|
719
|
|
720 /* We read the gif successfully. If we have more than one slice then
|
|
721 animate the gif. */
|
|
722 if (unwind.giffile->ImageCount > 1)
|
|
723 {
|
|
724 /* See if there is a timeout value. In theory there could be one
|
|
725 for every image - but that makes the implementation way to
|
|
726 complicated for now so we just take the first. */
|
|
727 unsigned short timeout = 0;
|
|
728 Lisp_Object tid;
|
|
729
|
|
730 if (unwind.giffile->SavedImages[0].Function == GRAPHICS_EXT_FUNC_CODE
|
|
731 &&
|
|
732 unwind.giffile->SavedImages[0].ExtensionBlockCount)
|
|
733 {
|
|
734 timeout = (unsigned short)
|
438
|
735 ((unwind.giffile->SavedImages[0].ExtensionBlocks[0].Bytes[2] << 8) +
|
428
|
736 unwind.giffile-> SavedImages[0].ExtensionBlocks[0].Bytes[1]) * 10;
|
|
737 }
|
|
738
|
|
739 /* Too short a timeout will crucify us performance-wise. */
|
|
740 tid = add_glyph_animated_timeout (timeout > 10 ? timeout : 10, image_instance);
|
|
741
|
|
742 if (!NILP (tid))
|
|
743 IMAGE_INSTANCE_PIXMAP_TIMEOUT (ii) = XINT (tid);
|
|
744 }
|
438
|
745
|
428
|
746 unbind_to (speccount, Qnil);
|
|
747 }
|
|
748
|
|
749 #endif /* HAVE_GIF */
|
|
750
|
|
751
|
|
752 #ifdef HAVE_PNG
|
|
753
|
|
754 /**********************************************************************
|
|
755 * PNG *
|
|
756 **********************************************************************/
|
|
757 static void
|
|
758 png_validate (Lisp_Object instantiator)
|
|
759 {
|
|
760 file_or_data_must_be_present (instantiator);
|
|
761 }
|
|
762
|
|
763 static Lisp_Object
|
442
|
764 png_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
765 Lisp_Object dest_mask)
|
428
|
766 {
|
|
767 return simple_image_type_normalize (inst, console_type, Qpng);
|
|
768 }
|
|
769
|
|
770 static int
|
|
771 png_possible_dest_types (void)
|
|
772 {
|
|
773 return IMAGE_COLOR_PIXMAP_MASK;
|
|
774 }
|
|
775
|
|
776 struct png_memory_storage
|
|
777 {
|
647
|
778 const UChar_Binary *bytes; /* The data */
|
|
779 Memory_Count len; /* How big is it? */
|
|
780 Memory_Count index; /* Where are we? */
|
428
|
781 };
|
|
782
|
|
783 static void
|
647
|
784 png_read_from_memory (png_structp png_ptr, png_bytep data,
|
|
785 png_size_t length)
|
428
|
786 {
|
|
787 struct png_memory_storage *tbr =
|
|
788 (struct png_memory_storage *) png_get_io_ptr (png_ptr);
|
|
789
|
647
|
790 if ((Memory_Count) length > (tbr->len - tbr->index))
|
428
|
791 png_error (png_ptr, (png_const_charp) "Read Error");
|
647
|
792 memcpy (data, tbr->bytes + tbr->index,length);
|
428
|
793 tbr->index = tbr->index + length;
|
|
794 }
|
|
795
|
|
796 struct png_error_struct
|
|
797 {
|
442
|
798 const char *err_str;
|
428
|
799 jmp_buf setjmp_buffer; /* for return to caller */
|
|
800 };
|
|
801
|
|
802 /* jh 98/03/12 - #### AARRRGH! libpng includes jmp_buf inside its own
|
|
803 structure, and there are cases where the size can be different from
|
|
804 between inside the library, and inside the code! To do an end run
|
|
805 around this, use our own error functions, and don't rely on things
|
|
806 passed in the png_ptr to them. This is an ugly hack and must
|
|
807 go away when the lisp engine is threaded! */
|
|
808 static struct png_error_struct png_err_stct;
|
|
809
|
|
810 static void
|
|
811 png_error_func (png_structp png_ptr, png_const_charp msg)
|
|
812 {
|
|
813 png_err_stct.err_str = msg;
|
|
814 longjmp (png_err_stct.setjmp_buffer, 1);
|
|
815 }
|
|
816
|
|
817 static void
|
|
818 png_warning_func (png_structp png_ptr, png_const_charp msg)
|
|
819 {
|
|
820 warn_when_safe (Qpng, Qinfo, "%s", msg);
|
|
821 }
|
|
822
|
|
823 struct png_unwind_data
|
|
824 {
|
|
825 FILE *instream;
|
|
826 unsigned char *eimage;
|
|
827 png_structp png_ptr;
|
|
828 png_infop info_ptr;
|
|
829 };
|
|
830
|
|
831 static Lisp_Object
|
|
832 png_instantiate_unwind (Lisp_Object unwind_obj)
|
|
833 {
|
|
834 struct png_unwind_data *data =
|
|
835 (struct png_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
836
|
|
837 free_opaque_ptr (unwind_obj);
|
|
838 if (data->png_ptr)
|
|
839 png_destroy_read_struct (&(data->png_ptr), &(data->info_ptr), (png_infopp)NULL);
|
|
840 if (data->instream)
|
|
841 fclose (data->instream);
|
|
842
|
|
843 if (data->eimage) xfree(data->eimage);
|
|
844
|
|
845 return Qnil;
|
|
846 }
|
|
847
|
|
848 static void
|
|
849 png_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
850 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
851 int dest_mask, Lisp_Object domain)
|
|
852 {
|
440
|
853 Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
428
|
854 struct png_unwind_data unwind;
|
|
855 int speccount = specpdl_depth ();
|
|
856 int height, width;
|
|
857 struct png_memory_storage tbr; /* Data to be read */
|
|
858
|
|
859 /* PNG variables */
|
|
860 png_structp png_ptr;
|
|
861 png_infop info_ptr;
|
|
862
|
|
863 /* Initialize all PNG structures */
|
|
864 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, (void*)&png_err_stct,
|
|
865 png_error_func, png_warning_func);
|
|
866 if (!png_ptr)
|
|
867 signal_image_error ("Error obtaining memory for png_read", instantiator);
|
|
868 info_ptr = png_create_info_struct (png_ptr);
|
|
869 if (!info_ptr)
|
|
870 {
|
|
871 png_destroy_read_struct (&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
|
|
872 signal_image_error ("Error obtaining memory for png_read", instantiator);
|
|
873 }
|
|
874
|
|
875 xzero (unwind);
|
|
876 unwind.png_ptr = png_ptr;
|
|
877 unwind.info_ptr = info_ptr;
|
|
878
|
|
879 record_unwind_protect (png_instantiate_unwind, make_opaque_ptr (&unwind));
|
|
880
|
|
881 /* This code is a mixture of stuff from Ben's GIF/JPEG stuff from
|
|
882 this file, example.c from the libpng 0.81 distribution, and the
|
|
883 pngtopnm sources. -WMP-
|
|
884 */
|
|
885 /* It has been further modified to handle the API changes for 0.96,
|
|
886 and is no longer usable for previous versions. jh
|
|
887 */
|
|
888
|
|
889 /* Set the jmp_buf return context for png_error ... if this returns !0, then
|
|
890 we ran into a problem somewhere, and need to clean up after ourselves. */
|
|
891 if (setjmp (png_err_stct.setjmp_buffer))
|
|
892 {
|
|
893 /* Something blew up: just display the error (cleanup happens in the unwind) */
|
|
894 signal_image_error_2 ("Error decoding PNG",
|
|
895 build_string(png_err_stct.err_str),
|
|
896 instantiator);
|
|
897 }
|
|
898
|
|
899 /* Initialize the IO layer and read in header information */
|
|
900 {
|
|
901 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
647
|
902 const UChar_Binary *bytes;
|
|
903 Memory_Count len;
|
428
|
904
|
|
905 assert (!NILP (data));
|
|
906
|
|
907 /* #### This is a definite problem under Mule due to the amount of
|
|
908 stack data it might allocate. Need to think about using Lstreams */
|
440
|
909 TO_EXTERNAL_FORMAT (LISP_STRING, data, ALLOCA, (bytes, len), Qbinary);
|
428
|
910 tbr.bytes = bytes;
|
|
911 tbr.len = len;
|
|
912 tbr.index = 0;
|
|
913 png_set_read_fn (png_ptr,(void *) &tbr, png_read_from_memory);
|
|
914 }
|
|
915
|
|
916 png_read_info (png_ptr, info_ptr);
|
|
917
|
|
918 {
|
|
919 int y;
|
|
920 unsigned char **row_pointers;
|
|
921 height = info_ptr->height;
|
|
922 width = info_ptr->width;
|
|
923
|
|
924 /* Wow, allocate all the memory. Truly, exciting. */
|
|
925 unwind.eimage = xnew_array_and_zero (unsigned char, width * height * 3);
|
|
926 /* libpng expects that the image buffer passed in contains a
|
|
927 picture to draw on top of if the png has any transparencies.
|
|
928 This could be a good place to pass that in... */
|
|
929
|
|
930 row_pointers = xnew_array (png_byte *, height);
|
|
931
|
|
932 for (y = 0; y < height; y++)
|
|
933 row_pointers[y] = unwind.eimage + (width * 3 * y);
|
|
934
|
|
935 {
|
|
936 /* if the png specifies a background chunk, go ahead and
|
|
937 use it, else use what we can get from the default face. */
|
|
938 png_color_16 my_background, *image_background;
|
|
939 Lisp_Object bkgd = Qnil;
|
|
940
|
|
941 my_background.red = 0x7fff;
|
|
942 my_background.green = 0x7fff;
|
|
943 my_background.blue = 0x7fff;
|
|
944 bkgd = FACE_BACKGROUND (Vdefault_face, domain);
|
|
945 if (!COLOR_INSTANCEP (bkgd))
|
|
946 {
|
|
947 warn_when_safe (Qpng, Qinfo, "Couldn't get background color!");
|
|
948 }
|
|
949 else
|
|
950 {
|
440
|
951 Lisp_Color_Instance *c;
|
428
|
952 Lisp_Object rgblist;
|
|
953
|
|
954 c = XCOLOR_INSTANCE (bkgd);
|
|
955 rgblist = MAYBE_LISP_DEVMETH (XDEVICE (c->device),
|
|
956 color_instance_rgb_components,
|
|
957 (c));
|
442
|
958 my_background.red = (unsigned short) XINT (XCAR (rgblist));
|
|
959 my_background.green = (unsigned short) XINT (XCAR (XCDR (rgblist)));
|
|
960 my_background.blue = (unsigned short) XINT (XCAR (XCDR (XCDR (rgblist))));
|
428
|
961 }
|
|
962
|
|
963 if (png_get_bKGD (png_ptr, info_ptr, &image_background))
|
|
964 png_set_background (png_ptr, image_background,
|
|
965 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
|
|
966 else
|
|
967 png_set_background (png_ptr, &my_background,
|
|
968 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
|
|
969 }
|
|
970
|
|
971 /* Now that we're using EImage, ask for 8bit RGB triples for any type
|
|
972 of image*/
|
|
973 /* convert palette images to full RGB */
|
|
974 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
|
|
975 png_set_expand (png_ptr);
|
|
976 /* send grayscale images to RGB too */
|
|
977 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
|
|
978 info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
|
979 png_set_gray_to_rgb (png_ptr);
|
|
980 /* we can't handle alpha values */
|
|
981 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
|
|
982 png_set_strip_alpha (png_ptr);
|
|
983 /* tell libpng to strip 16 bit depth files down to 8 bits */
|
|
984 if (info_ptr->bit_depth == 16)
|
|
985 png_set_strip_16 (png_ptr);
|
|
986 /* if the image is < 8 bits, pad it out */
|
|
987 if (info_ptr->bit_depth < 8)
|
|
988 {
|
|
989 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)
|
|
990 png_set_expand (png_ptr);
|
|
991 else
|
|
992 png_set_packing (png_ptr);
|
|
993 }
|
|
994
|
|
995 png_read_image (png_ptr, row_pointers);
|
|
996 png_read_end (png_ptr, info_ptr);
|
|
997
|
|
998 #ifdef PNG_SHOW_COMMENTS
|
|
999 /* ####
|
|
1000 * I turn this off by default now, because the !%^@#!% comments
|
|
1001 * show up every time the image is instantiated, which can get
|
|
1002 * really really annoying. There should be some way to pass this
|
|
1003 * type of data down into the glyph code, where you can get to it
|
|
1004 * from lisp anyway. - WMP
|
|
1005 */
|
|
1006 {
|
|
1007 int i;
|
|
1008
|
|
1009 for (i = 0 ; i < info_ptr->num_text ; i++)
|
|
1010 {
|
|
1011 /* How paranoid do I have to be about no trailing NULLs, and
|
|
1012 using (int)info_ptr->text[i].text_length, and strncpy and a temp
|
|
1013 string somewhere? */
|
|
1014
|
|
1015 warn_when_safe (Qpng, Qinfo, "%s - %s",
|
|
1016 info_ptr->text[i].key,
|
|
1017 info_ptr->text[i].text);
|
|
1018 }
|
|
1019 }
|
|
1020 #endif
|
|
1021
|
|
1022 xfree (row_pointers);
|
|
1023 }
|
|
1024
|
|
1025 /* now instantiate */
|
442
|
1026 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
1027 init_image_instance_from_eimage,
|
|
1028 (ii, width, height, 1, unwind.eimage, dest_mask,
|
|
1029 instantiator, domain));
|
|
1030
|
|
1031 /* This will clean up everything else. */
|
|
1032 unbind_to (speccount, Qnil);
|
|
1033 }
|
|
1034
|
|
1035 #endif /* HAVE_PNG */
|
|
1036
|
|
1037
|
|
1038 #ifdef HAVE_TIFF
|
|
1039 #include "tiffio.h"
|
|
1040
|
|
1041 /**********************************************************************
|
|
1042 * TIFF *
|
|
1043 **********************************************************************/
|
|
1044 static void
|
|
1045 tiff_validate (Lisp_Object instantiator)
|
|
1046 {
|
|
1047 file_or_data_must_be_present (instantiator);
|
|
1048 }
|
|
1049
|
|
1050 static Lisp_Object
|
442
|
1051 tiff_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
1052 Lisp_Object dest_mask)
|
428
|
1053 {
|
|
1054 return simple_image_type_normalize (inst, console_type, Qtiff);
|
|
1055 }
|
|
1056
|
|
1057 static int
|
|
1058 tiff_possible_dest_types (void)
|
|
1059 {
|
|
1060 return IMAGE_COLOR_PIXMAP_MASK;
|
|
1061 }
|
|
1062
|
|
1063 struct tiff_unwind_data
|
|
1064 {
|
|
1065 unsigned char *eimage;
|
|
1066 /* Object that holds the decoded data from a TIFF file */
|
|
1067 TIFF *tiff;
|
|
1068 };
|
|
1069
|
|
1070 static Lisp_Object
|
|
1071 tiff_instantiate_unwind (Lisp_Object unwind_obj)
|
|
1072 {
|
|
1073 struct tiff_unwind_data *data =
|
|
1074 (struct tiff_unwind_data *) get_opaque_ptr (unwind_obj);
|
|
1075
|
|
1076 free_opaque_ptr (unwind_obj);
|
|
1077 if (data->tiff)
|
|
1078 {
|
|
1079 TIFFClose(data->tiff);
|
|
1080 }
|
|
1081 if (data->eimage)
|
|
1082 xfree (data->eimage);
|
|
1083
|
|
1084 return Qnil;
|
|
1085 }
|
|
1086
|
|
1087 typedef struct tiff_memory_storage
|
|
1088 {
|
647
|
1089 UChar_Binary *bytes; /* The data */
|
|
1090 Memory_Count len; /* How big is it? */
|
|
1091 Memory_Count index; /* Where are we? */
|
428
|
1092 } tiff_memory_storage;
|
|
1093
|
|
1094 static size_t
|
647
|
1095 tiff_memory_read (thandle_t data, tdata_t buf, tsize_t size)
|
428
|
1096 {
|
647
|
1097 tiff_memory_storage *mem = (tiff_memory_storage *) data;
|
428
|
1098
|
647
|
1099 if ((Memory_Count) size > (mem->len - mem->index))
|
428
|
1100 return (size_t) -1;
|
647
|
1101 memcpy (buf, mem->bytes + mem->index, size);
|
428
|
1102 mem->index = mem->index + size;
|
|
1103 return size;
|
|
1104 }
|
|
1105
|
647
|
1106 static size_t
|
|
1107 tiff_memory_write (thandle_t data, tdata_t buf, tsize_t size)
|
428
|
1108 {
|
|
1109 abort();
|
|
1110 return 0; /* Shut up warnings. */
|
|
1111 }
|
|
1112
|
647
|
1113 static toff_t
|
|
1114 tiff_memory_seek (thandle_t data, toff_t off, int whence)
|
428
|
1115 {
|
647
|
1116 tiff_memory_storage *mem = (tiff_memory_storage *) data;
|
428
|
1117 int newidx;
|
647
|
1118 switch(whence)
|
|
1119 {
|
|
1120 case SEEK_SET:
|
|
1121 newidx = off;
|
|
1122 break;
|
|
1123 case SEEK_END:
|
|
1124 newidx = mem->len + off;
|
|
1125 break;
|
|
1126 case SEEK_CUR:
|
|
1127 newidx = mem->index + off;
|
|
1128 break;
|
|
1129 default:
|
|
1130 fprintf (stderr, "Eh? invalid seek mode in tiff_memory_seek\n");
|
|
1131 return (toff_t) -1;
|
|
1132 }
|
428
|
1133
|
|
1134 if ((newidx > mem->len) || (newidx < 0))
|
593
|
1135 return (toff_t) -1;
|
428
|
1136
|
|
1137 mem->index = newidx;
|
|
1138 return newidx;
|
|
1139 }
|
|
1140
|
|
1141 static int
|
647
|
1142 tiff_memory_close (thandle_t data)
|
428
|
1143 {
|
|
1144 return 0;
|
|
1145 }
|
|
1146
|
|
1147 static int
|
647
|
1148 tiff_map_noop (thandle_t data, tdata_t* pbase, toff_t* psize)
|
428
|
1149 {
|
|
1150 return 0;
|
|
1151 }
|
|
1152
|
|
1153 static void
|
647
|
1154 tiff_unmap_noop (thandle_t data, tdata_t pbase, toff_t psize)
|
428
|
1155 {
|
|
1156 return;
|
|
1157 }
|
|
1158
|
|
1159 static toff_t
|
647
|
1160 tiff_memory_size (thandle_t data)
|
428
|
1161 {
|
|
1162 tiff_memory_storage *mem = (tiff_memory_storage*)data;
|
|
1163 return mem->len;
|
|
1164 }
|
|
1165
|
|
1166 struct tiff_error_struct
|
|
1167 {
|
438
|
1168 #ifdef HAVE_VSNPRINTF
|
428
|
1169 char err_str[256];
|
|
1170 #else
|
|
1171 char err_str[1024]; /* return the error string */
|
|
1172 #endif
|
|
1173 jmp_buf setjmp_buffer; /* for return to caller */
|
|
1174 };
|
|
1175
|
|
1176 /* jh 98/03/12 - ###This struct for passing data to the error functions
|
|
1177 is an ugly hack caused by the fact that libtiff (as of v3.4) doesn't
|
|
1178 have any place to store error func data. This should be rectified
|
|
1179 before XEmacs gets threads! */
|
|
1180 static struct tiff_error_struct tiff_err_data;
|
|
1181
|
|
1182 static void
|
647
|
1183 tiff_error_func (const char *module, const char *fmt, ...)
|
428
|
1184 {
|
|
1185 va_list vargs;
|
|
1186
|
|
1187 va_start (vargs, fmt);
|
438
|
1188 #ifdef HAVE_VSNPRINTF
|
428
|
1189 vsnprintf (tiff_err_data.err_str, 255, fmt, vargs);
|
|
1190 #else
|
|
1191 /* pray this doesn't overflow... */
|
|
1192 vsprintf (tiff_err_data.err_str, fmt, vargs);
|
|
1193 #endif
|
|
1194 va_end (vargs);
|
|
1195 /* return to setjmp point */
|
|
1196 longjmp (tiff_err_data.setjmp_buffer, 1);
|
|
1197 }
|
|
1198
|
|
1199 static void
|
647
|
1200 tiff_warning_func (const char *module, const char *fmt, ...)
|
428
|
1201 {
|
|
1202 va_list vargs;
|
438
|
1203 #ifdef HAVE_VSNPRINTF
|
428
|
1204 char warn_str[256];
|
|
1205 #else
|
|
1206 char warn_str[1024];
|
|
1207 #endif
|
|
1208
|
|
1209 va_start (vargs, fmt);
|
438
|
1210 #ifdef HAVE_VSNPRINTF
|
428
|
1211 vsnprintf (warn_str, 255, fmt, vargs);
|
|
1212 #else
|
|
1213 vsprintf (warn_str, fmt, vargs);
|
|
1214 #endif
|
|
1215 va_end (vargs);
|
|
1216 warn_when_safe (Qtiff, Qinfo, "%s - %s",
|
|
1217 module, warn_str);
|
|
1218 }
|
|
1219
|
|
1220 static void
|
|
1221 tiff_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
1222 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
1223 int dest_mask, Lisp_Object domain)
|
|
1224 {
|
440
|
1225 Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
428
|
1226 tiff_memory_storage mem_struct;
|
|
1227 /* It is OK for the unwind data to be local to this function,
|
|
1228 because the unwind-protect is always executed when this
|
|
1229 stack frame is still valid. */
|
|
1230 struct tiff_unwind_data unwind;
|
|
1231 int speccount = specpdl_depth ();
|
|
1232 uint32 width, height;
|
|
1233
|
|
1234 xzero (unwind);
|
|
1235 record_unwind_protect (tiff_instantiate_unwind, make_opaque_ptr (&unwind));
|
|
1236
|
|
1237 /* set up error facilities */
|
|
1238 if (setjmp (tiff_err_data.setjmp_buffer))
|
|
1239 {
|
|
1240 /* An error was signaled. No clean up is needed, as unwind handles that
|
|
1241 for us. Just pass the error along. */
|
|
1242 signal_image_error_2 ("TIFF decoding error",
|
|
1243 build_string(tiff_err_data.err_str),
|
|
1244 instantiator);
|
|
1245 }
|
|
1246 TIFFSetErrorHandler ((TIFFErrorHandler)tiff_error_func);
|
|
1247 TIFFSetWarningHandler ((TIFFErrorHandler)tiff_warning_func);
|
|
1248 {
|
|
1249 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
647
|
1250 UChar_Binary *bytes;
|
|
1251 Memory_Count len;
|
428
|
1252
|
|
1253 uint32 *raster;
|
|
1254 unsigned char *ep;
|
|
1255
|
|
1256 assert (!NILP (data));
|
|
1257
|
|
1258 /* #### This is a definite problem under Mule due to the amount of
|
|
1259 stack data it might allocate. Think about Lstreams... */
|
440
|
1260 TO_EXTERNAL_FORMAT (LISP_STRING, data,
|
|
1261 ALLOCA, (bytes, len),
|
|
1262 Qbinary);
|
428
|
1263 mem_struct.bytes = bytes;
|
|
1264 mem_struct.len = len;
|
|
1265 mem_struct.index = 0;
|
|
1266
|
442
|
1267 unwind.tiff = TIFFClientOpen ("memfile", "r", (thandle_t) &mem_struct,
|
428
|
1268 (TIFFReadWriteProc)tiff_memory_read,
|
|
1269 (TIFFReadWriteProc)tiff_memory_write,
|
|
1270 tiff_memory_seek, tiff_memory_close, tiff_memory_size,
|
|
1271 tiff_map_noop, tiff_unmap_noop);
|
|
1272 if (!unwind.tiff)
|
440
|
1273 signal_image_error ("Insufficient memory to instantiate TIFF image", instantiator);
|
428
|
1274
|
|
1275 TIFFGetField (unwind.tiff, TIFFTAG_IMAGEWIDTH, &width);
|
|
1276 TIFFGetField (unwind.tiff, TIFFTAG_IMAGELENGTH, &height);
|
|
1277 unwind.eimage = (unsigned char *) xmalloc (width * height * 3);
|
|
1278
|
440
|
1279 /* #### This is little more than proof-of-concept/function testing.
|
428
|
1280 It needs to be reimplemented via scanline reads for both memory
|
|
1281 compactness. */
|
|
1282 raster = (uint32*) _TIFFmalloc (width * height * sizeof (uint32));
|
|
1283 if (raster != NULL)
|
|
1284 {
|
647
|
1285 int i, j;
|
428
|
1286 uint32 *rp;
|
|
1287 ep = unwind.eimage;
|
|
1288 rp = raster;
|
|
1289 if (TIFFReadRGBAImage (unwind.tiff, width, height, raster, 0))
|
|
1290 {
|
|
1291 for (i = height - 1; i >= 0; i--)
|
|
1292 {
|
|
1293 /* This is to get around weirdness in the libtiff library where properly
|
|
1294 made TIFFs will come out upside down. libtiff bug or jhod-brainlock? */
|
|
1295 rp = raster + (i * width);
|
647
|
1296 for (j = 0; j < (int) width; j++)
|
428
|
1297 {
|
|
1298 *ep++ = (unsigned char)TIFFGetR(*rp);
|
|
1299 *ep++ = (unsigned char)TIFFGetG(*rp);
|
|
1300 *ep++ = (unsigned char)TIFFGetB(*rp);
|
|
1301 rp++;
|
|
1302 }
|
|
1303 }
|
|
1304 }
|
|
1305 _TIFFfree (raster);
|
|
1306 } else
|
|
1307 signal_image_error ("Unable to allocate memory for TIFFReadRGBA", instantiator);
|
|
1308
|
|
1309 }
|
|
1310
|
|
1311 /* now instantiate */
|
442
|
1312 MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
|
428
|
1313 init_image_instance_from_eimage,
|
|
1314 (ii, width, height, 1, unwind.eimage, dest_mask,
|
|
1315 instantiator, domain));
|
|
1316
|
|
1317 unbind_to (speccount, Qnil);
|
|
1318 }
|
|
1319
|
|
1320 #endif /* HAVE_TIFF */
|
|
1321
|
|
1322
|
|
1323 /************************************************************************/
|
|
1324 /* initialization */
|
|
1325 /************************************************************************/
|
|
1326
|
|
1327 void
|
|
1328 syms_of_glyphs_eimage (void)
|
|
1329 {
|
|
1330 }
|
|
1331
|
|
1332 void
|
|
1333 image_instantiator_format_create_glyphs_eimage (void)
|
|
1334 {
|
|
1335 /* image-instantiator types */
|
|
1336 #ifdef HAVE_JPEG
|
|
1337 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (jpeg, "jpeg");
|
|
1338
|
|
1339 IIFORMAT_HAS_METHOD (jpeg, validate);
|
|
1340 IIFORMAT_HAS_METHOD (jpeg, normalize);
|
|
1341 IIFORMAT_HAS_METHOD (jpeg, possible_dest_types);
|
|
1342 IIFORMAT_HAS_METHOD (jpeg, instantiate);
|
|
1343
|
|
1344 IIFORMAT_VALID_KEYWORD (jpeg, Q_data, check_valid_string);
|
|
1345 IIFORMAT_VALID_KEYWORD (jpeg, Q_file, check_valid_string);
|
|
1346 #endif
|
|
1347
|
|
1348 #ifdef HAVE_GIF
|
|
1349 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (gif, "gif");
|
|
1350
|
|
1351 IIFORMAT_HAS_METHOD (gif, validate);
|
|
1352 IIFORMAT_HAS_METHOD (gif, normalize);
|
|
1353 IIFORMAT_HAS_METHOD (gif, possible_dest_types);
|
|
1354 IIFORMAT_HAS_METHOD (gif, instantiate);
|
|
1355
|
|
1356 IIFORMAT_VALID_KEYWORD (gif, Q_data, check_valid_string);
|
|
1357 IIFORMAT_VALID_KEYWORD (gif, Q_file, check_valid_string);
|
|
1358 #endif
|
|
1359
|
|
1360 #ifdef HAVE_PNG
|
|
1361 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (png, "png");
|
|
1362
|
|
1363 IIFORMAT_HAS_METHOD (png, validate);
|
|
1364 IIFORMAT_HAS_METHOD (png, normalize);
|
|
1365 IIFORMAT_HAS_METHOD (png, possible_dest_types);
|
|
1366 IIFORMAT_HAS_METHOD (png, instantiate);
|
|
1367
|
|
1368 IIFORMAT_VALID_KEYWORD (png, Q_data, check_valid_string);
|
|
1369 IIFORMAT_VALID_KEYWORD (png, Q_file, check_valid_string);
|
|
1370 #endif
|
|
1371
|
|
1372 #ifdef HAVE_TIFF
|
|
1373 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (tiff, "tiff");
|
|
1374
|
|
1375 IIFORMAT_HAS_METHOD (tiff, validate);
|
|
1376 IIFORMAT_HAS_METHOD (tiff, normalize);
|
|
1377 IIFORMAT_HAS_METHOD (tiff, possible_dest_types);
|
|
1378 IIFORMAT_HAS_METHOD (tiff, instantiate);
|
|
1379
|
|
1380 IIFORMAT_VALID_KEYWORD (tiff, Q_data, check_valid_string);
|
|
1381 IIFORMAT_VALID_KEYWORD (tiff, Q_file, check_valid_string);
|
|
1382 #endif
|
|
1383
|
|
1384 }
|
|
1385
|
|
1386 void
|
|
1387 vars_of_glyphs_eimage (void)
|
|
1388 {
|
|
1389 #ifdef HAVE_JPEG
|
|
1390 Fprovide (Qjpeg);
|
|
1391 #endif
|
|
1392
|
|
1393 #ifdef HAVE_GIF
|
|
1394 Fprovide (Qgif);
|
|
1395 #endif
|
|
1396
|
|
1397 #ifdef HAVE_PNG
|
|
1398 Fprovide (Qpng);
|
|
1399 #endif
|
|
1400
|
|
1401 #ifdef HAVE_TIFF
|
|
1402 Fprovide (Qtiff);
|
|
1403 #endif
|
|
1404
|
|
1405 }
|