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