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