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