/* Functions for image support on window system.
-Copyright (C) 1989, 1992-2015 Free Software Foundation, Inc.
+Copyright (C) 1989, 1992-2016 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, either version 3 of the License, or (at
+your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
-#include "sysstdio.h"
+
+#include <fcntl.h>
+#include <stdio.h>
#include <unistd.h>
/* Include this before including <setjmp.h> to work around bugs with
#include "blockinput.h"
#include "systime.h"
#include <epaths.h>
-#include "character.h"
#include "coding.h"
#include "termhooks.h"
#include "font.h"
#endif /* HAVE_WINDOW_SYSTEM */
#ifdef HAVE_X_WINDOWS
-#define COLOR_TABLE_SUPPORT 1
-
typedef struct x_bitmap_record Bitmap_Record;
#define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
#define NO_PIXMAP None
# include "w32.h"
#endif
-/* W32_TODO : Color tables on W32. */
-#undef COLOR_TABLE_SUPPORT
-
typedef struct w32_bitmap_record Bitmap_Record;
#define GET_PIXEL(ximg, x, y) GetPixel (ximg, x, y)
#define NO_PIXMAP 0
#endif /* HAVE_NTGUI */
-#ifdef USE_CAIRO
-#undef COLOR_TABLE_SUPPORT
-#endif
-
#ifdef HAVE_NS
-#undef COLOR_TABLE_SUPPORT
-
typedef struct ns_bitmap_record Bitmap_Record;
#define GET_PIXEL(ximg, x, y) XGetPixel (ximg, x, y)
#define DefaultDepthOfScreen(screen) x_display_list->n_planes
#endif /* HAVE_NS */
+#if (defined HAVE_X_WINDOWS \
+ && ! (defined HAVE_NTGUI || defined USE_CAIRO || defined HAVE_NS))
+/* W32_TODO : Color tables on W32. */
+# define COLOR_TABLE_SUPPORT 1
+#endif
+
static void x_disable_image (struct frame *, struct image *);
static void x_edge_detection (struct frame *, struct image *, Lisp_Object,
Lisp_Object);
va_end (ap);
}
+static void
+image_size_error (void)
+{
+ image_error ("Invalid image size (see `max-image-size')");
+}
\f
/***********************************************************************
return 0;
case IMAGE_NUMBER_VALUE:
- if (!INTEGERP (value) && !FLOATP (value))
+ if (! NUMBERP (value))
return 0;
break;
XColor xbgcolor;
Lisp_Object bg = image_spec_value (img->spec, QCbackground, NULL);
- if (STRINGP (bg) && XParseColor (FRAME_X_DISPLAY (f),
- FRAME_X_COLORMAP (f),
- SSDATA (bg),
- &xbgcolor))
+ if (STRINGP (bg) && x_parse_color (f, SSDATA (bg), &xbgcolor))
bgcolor = xcolor_to_argb32 (xbgcolor);
return bgcolor;
struct image_cache *c = FRAME_IMAGE_CACHE (f);
ptrdiff_t i;
+ if (!c)
+ c = FRAME_IMAGE_CACHE (f) = make_image_cache ();
+
/* Find a free slot in c->images. */
for (i = 0; i < c->used; ++i)
if (c->images[i] == NULL)
***********************************************************************/
/* Find image file FILE. Look in data-directory/images, then
- x-bitmap-file-path. Value is the encoded full name of the file
- found, or nil if not found. */
+ x-bitmap-file-path. Value is the full name of the file
+ found, or nil if not found. If PFD is nonnull store into *PFD a
+ readable file descriptor for the file, opened in binary mode. If
+ PFD is null, do not open the file. */
-Lisp_Object
-x_find_image_file (Lisp_Object file)
+static Lisp_Object
+x_find_image_fd (Lisp_Object file, int *pfd)
{
Lisp_Object file_found, search_path;
int fd;
Vx_bitmap_file_path);
/* Try to find FILE in data-directory/images, then x-bitmap-file-path. */
- fd = openp (search_path, file, Qnil, &file_found, Qnil, false);
-
- if (fd == -1)
- file_found = Qnil;
- else
+ fd = openp (search_path, file, Qnil, &file_found,
+ pfd ? Qt : make_number (R_OK), false);
+ if (fd >= 0 || fd == -2)
{
file_found = ENCODE_FILE (file_found);
- if (fd != -2)
- emacs_close (fd);
+ if (fd == -2)
+ {
+ /* The file exists locally, but has a file handler. (This
+ happens, e.g., under Auto Image File Mode.) 'openp'
+ didn't open the file, so we should, because the caller
+ expects that. */
+ fd = emacs_open (SSDATA (file_found), O_RDONLY | O_BINARY, 0);
+ }
}
-
+ else /* fd < 0, but not -2 */
+ return Qnil;
+ if (pfd)
+ *pfd = fd;
return file_found;
}
+/* Find image file FILE. Look in data-directory/images, then
+ x-bitmap-file-path. Value is the encoded full name of the file
+ found, or nil if not found. */
+
+Lisp_Object
+x_find_image_file (Lisp_Object file)
+{
+ return x_find_image_fd (file, 0);
+}
/* Read FILE into memory. Value is a pointer to a buffer allocated
with xmalloc holding FILE's contents. Value is null if an error
- occurred. *SIZE is set to the size of the file. */
+ occurred. FD is a file descriptor open for reading FILE. Set
+ *SIZE to the size of the file. */
static unsigned char *
-slurp_file (char *file, ptrdiff_t *size)
+slurp_file (int fd, ptrdiff_t *size)
{
- FILE *fp = emacs_fopen (file, "rb");
+ FILE *fp = fdopen (fd, "rb");
+
unsigned char *buf = NULL;
struct stat st;
if (!check_image_size (f, *width, *height))
{
if (!inhibit_image_error)
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
goto failure;
}
else if (data == NULL)
if (img->pixmap == NO_PIXMAP)
{
x_clear_image (f, img);
- image_error ("Unable to create X pixmap for "uLSQM"%s"uRSQM,
- img->spec);
+ image_error ("Unable to create X pixmap for `%s'", img->spec);
}
else
success_p = 1;
}
else
- image_error ("Error loading XBM image "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Error loading XBM image `%s'", img->spec);
return success_p;
}
file_name = image_spec_value (img->spec, QCfile, NULL);
if (STRINGP (file_name))
{
- Lisp_Object file;
- unsigned char *contents;
- ptrdiff_t size;
-
- file = x_find_image_file (file_name);
+ int fd;
+ Lisp_Object file = x_find_image_fd (file_name, &fd);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM, file_name);
+ image_error ("Cannot find image file `%s'", file_name);
return 0;
}
- contents = slurp_file (SSDATA (file), &size);
+ ptrdiff_t size;
+ unsigned char *contents = slurp_file (fd, &size);
if (contents == NULL)
{
- image_error ("Error loading XBM image "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Error loading XBM image `%s'", file);
return 0;
}
eassert (img->width > 0 && img->height > 0);
if (!check_image_size (f, img->width, img->height))
{
- image_error ("Invalid image size (see "
- uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
return 0;
}
}
success_p = 1;
else
{
- image_error (("Unable to create pixmap for XBM image "
- uLSQM"%s"uRSQM),
+ image_error ("Unable to create pixmap for XBM image `%s'",
img->spec);
x_clear_image (f, img);
}
/* An entry in a hash table used to cache color definitions of named
colors. This cache is necessary to speed up XPM image loading in
case we do color allocations ourselves. Without it, we would need
- a call to XParseColor per pixel in the image. */
+ a call to XParseColor per pixel in the image.
+
+ FIXME Now that we're using x_parse_color and its cache, reevaluate
+ the need for this caching layer. */
struct xpm_cached_color
{
XColor color;
for (i = 0; i < attrs->numsymbols; ++i)
- if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
- attrs->colorsymbols[i].value, &color))
+ if (x_parse_color (f, attrs->colorsymbols[i].value, &color))
{
color.pixel = lookup_rgb_color (f, color.red, color.green,
color.blue);
if (p != NULL)
*color = p->color;
- else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
- color_name, color))
+ else if (x_parse_color (f, color_name, color))
{
color->pixel = lookup_rgb_color (f, color->red, color->green,
color->blue);
attrs.valuemask |= XpmVisual;
attrs.valuemask |= XpmColormap;
+#ifdef ALLOC_XPM_COLORS
+ attrs.color_closure = f;
+ attrs.alloc_color = xpm_alloc_color;
+ attrs.free_colors = xpm_free_colors;
+ attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
+ xpm_init_color_cache (f, &attrs);
+#endif
+
rc = XpmCreatePixmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
(char **) bits, &bitmap, &mask, &attrs);
if (rc != XpmSuccess)
dpyinfo->bitmaps[id - 1].depth = attrs.depth;
dpyinfo->bitmaps[id - 1].refcount = 1;
+#ifdef ALLOC_XPM_COLORS
+ xpm_free_color_cache ();
+#endif
XpmFreeAttributes (&attrs);
return id;
}
Lisp_Object file = x_find_image_file (specified_file);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM,
- specified_file);
+ image_error ("Cannot find image file `%s'", specified_file);
#ifdef ALLOC_XPM_COLORS
xpm_free_color_cache ();
#endif
return 0;
}
+ file = ENCODE_FILE (file);
#ifdef HAVE_NTGUI
#ifdef WINDOWSNT
/* FILE is encoded in UTF-8, but image libraries on Windows
Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
if (!STRINGP (buffer))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, buffer);
+ image_error ("Invalid image data `%s'", buffer);
#ifdef ALLOC_XPM_COLORS
xpm_free_color_cache ();
#endif
if (!check_image_size (f, width, height))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
goto failure;
}
file_name = image_spec_value (img->spec, QCfile, NULL);
if (STRINGP (file_name))
{
- Lisp_Object file;
- unsigned char *contents;
- ptrdiff_t size;
-
- file = x_find_image_file (file_name);
+ int fd;
+ Lisp_Object file = x_find_image_fd (file_name, &fd);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM, file_name);
+ image_error ("Cannot find image file `%s'", file_name);
return 0;
}
- contents = slurp_file (SSDATA (file), &size);
+ ptrdiff_t size;
+ unsigned char *contents = slurp_file (fd, &size);
if (contents == NULL)
{
- image_error ("Error loading XPM image "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Error loading XPM image `%s'", file);
return 0;
}
data = image_spec_value (img->spec, QCdata, NULL);
if (!STRINGP (data))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, data);
+ image_error ("Invalid image data `%s'", data);
return 0;
}
success_p = xpm_load_image (f, img, SDATA (data),
dpyinfo = FRAME_DISPLAY_INFO (f);
if (dpyinfo->red_bits > 0)
{
- unsigned long pr, pg, pb;
-
/* Apply gamma-correction like normal color allocation does. */
if (f->gamma)
{
r = color.red, g = color.green, b = color.blue;
}
- /* Scale down RGB values to the visual's bits per RGB, and shift
- them to the right position in the pixel color. Note that the
- original RGB values are 16-bit values, as usual in X. */
- pr = (r >> (16 - dpyinfo->red_bits)) << dpyinfo->red_offset;
- pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset;
- pb = (b >> (16 - dpyinfo->blue_bits)) << dpyinfo->blue_offset;
-
- /* Assemble the pixel color. */
- return pr | pg | pb;
+ return x_make_truecolor_pixel (dpyinfo, r, g, b);
}
for (p = ct_table[i]; p; p = p->next)
static unsigned long
lookup_rgb_color (struct frame *f, int r, int g, int b)
{
- unsigned long pixel;
-
#ifdef HAVE_NTGUI
- pixel = PALETTERGB (r >> 8, g >> 8, b >> 8);
-#endif /* HAVE_NTGUI */
-
-#ifdef HAVE_NS
- pixel = RGB_TO_ULONG (r >> 8, g >> 8, b >> 8);
-#endif /* HAVE_NS */
- return pixel;
+ return PALETTERGB (r >> 8, g >> 8, b >> 8);
+#elif defined HAVE_NS
+ return RGB_TO_ULONG (r >> 8, g >> 8, b >> 8);
+#else
+ xsignal1 (Qfile_error,
+ build_string ("This Emacs mishandles this image file type"));
+#endif
}
static void
int x, y;
XColor *colors, *p;
XImagePtr_or_DC ximg;
+ ptrdiff_t nbytes;
#ifdef HAVE_NTGUI
HGDIOBJ prev;
#endif /* HAVE_NTGUI */
- if (img->height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width)
+ if (INT_MULTIPLY_WRAPV (sizeof *colors, img->width, &nbytes)
+ || INT_MULTIPLY_WRAPV (img->height, nbytes, &nbytes)
+ || SIZE_MAX < nbytes)
memory_full (SIZE_MAX);
- colors = xmalloc (sizeof *colors * img->width * img->height);
+ colors = xmalloc (nbytes);
/* Get the X image or create a memory device context for IMG. */
ximg = image_get_x_image_or_dc (f, img, 0, &prev);
XColor *colors = x_to_xcolors (f, img, 1);
XColor *new, *p;
int x, y, i, sum;
+ ptrdiff_t nbytes;
for (i = sum = 0; i < 9; ++i)
sum += eabs (matrix[i]);
#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
- if (img->height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width)
+ if (INT_MULTIPLY_WRAPV (sizeof *new, img->width, &nbytes)
+ || INT_MULTIPLY_WRAPV (img->height, nbytes, &nbytes))
memory_full (SIZE_MAX);
- new = xmalloc (sizeof *new * img->width * img->height);
+ new = xmalloc (nbytes);
for (y = 0; y < img->height; ++y)
{
bool raw_p;
int x, y;
int width, height, max_color_idx = 0;
- Lisp_Object file, specified_file;
+ Lisp_Object specified_file;
enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
unsigned char *contents = NULL;
unsigned char *end, *p;
- ptrdiff_t size;
#ifdef USE_CAIRO
unsigned char *data = 0;
uint32_t *dataptr;
if (STRINGP (specified_file))
{
- file = x_find_image_file (specified_file);
+ int fd;
+ Lisp_Object file = x_find_image_fd (specified_file, &fd);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM,
- specified_file);
+ image_error ("Cannot find image file `%s'", specified_file);
return 0;
}
- contents = slurp_file (SSDATA (file), &size);
+ ptrdiff_t size;
+ contents = slurp_file (fd, &size);
if (contents == NULL)
{
- image_error ("Error reading "uLSQM"%s"uRSQM, file);
+ image_error ("Error reading `%s'", file);
return 0;
}
data = image_spec_value (img->spec, QCdata, NULL);
if (!STRINGP (data))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, data);
+ image_error ("Invalid image data `%s'", data);
return 0;
}
p = SDATA (data);
/* Check magic number. */
if (end - p < 2 || *p++ != 'P')
{
- image_error ("Not a PBM image: "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Not a PBM image: `%s'", img->spec);
error:
xfree (contents);
img->pixmap = NO_PIXMAP;
break;
default:
- image_error ("Not a PBM image: "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Not a PBM image: `%s'", img->spec);
goto error;
}
if (!check_image_size (f, width, height))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
goto error;
}
x_destroy_x_image (ximg);
#endif
x_clear_image (f, img);
- image_error (("Invalid image size in image "
- uLSQM"%s"uRSQM),
+ image_error ("Invalid image size in image `%s'",
img->spec);
goto error;
}
x_destroy_x_image (ximg);
#endif
x_clear_image (f, img);
- image_error ("Invalid image size in image "uLSQM"%s"uRSQM,
- img->spec);
+ image_error ("Invalid image size in image `%s'", img->spec);
goto error;
}
#else
x_destroy_x_image (ximg);
#endif
- image_error ("Invalid pixel value in image "uLSQM"%s"uRSQM,
- img->spec);
+ image_error ("Invalid pixel value in image `%s'", img->spec);
goto error;
}
static bool
png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
{
- Lisp_Object file, specified_file;
+ Lisp_Object specified_file;
Lisp_Object specified_data;
int x, y;
ptrdiff_t i;
png_uint_32 row_bytes;
bool transparent_p;
struct png_memory_storage tbr; /* Data to be read */
+ ptrdiff_t nbytes;
#ifdef USE_CAIRO
unsigned char *data = 0;
if (NILP (specified_data))
{
- file = x_find_image_file (specified_file);
+ int fd;
+ Lisp_Object file = x_find_image_fd (specified_file, &fd);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM,
- specified_file);
+ image_error ("Cannot find image file `%s'", specified_file);
return 0;
}
/* Open the image file. */
- fp = emacs_fopen (SSDATA (file), "rb");
+ fp = fdopen (fd, "rb");
if (!fp)
{
- image_error ("Cannot open image file "uLSQM"%s"uRSQM, file);
+ image_error ("Cannot open image file `%s'", file);
return 0;
}
|| png_sig_cmp (sig, 0, sizeof sig))
{
fclose (fp);
- image_error ("Not a PNG file: "uLSQM"%s"uRSQM, file);
+ image_error ("Not a PNG file: `%s'", file);
return 0;
}
}
{
if (!STRINGP (specified_data))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, specified_data);
+ image_error ("Invalid image data `%s'", specified_data);
return 0;
}
if (tbr.len < sizeof sig
|| png_sig_cmp (tbr.bytes, 0, sizeof sig))
{
- image_error ("Not a PNG image: "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Not a PNG image: `%s'", img->spec);
return 0;
}
if (! (width <= INT_MAX && height <= INT_MAX
&& check_image_size (f, width, height)))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
goto error;
}
row_bytes = png_get_rowbytes (png_ptr, info_ptr);
/* Allocate memory for the image. */
- if (height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *rows
- || row_bytes > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *pixels / height)
+ if (INT_MULTIPLY_WRAPV (row_bytes, sizeof *pixels, &nbytes)
+ || INT_MULTIPLY_WRAPV (nbytes, height, &nbytes))
memory_full (SIZE_MAX);
- c->pixels = pixels = xmalloc (sizeof *pixels * row_bytes * height);
+ c->pixels = pixels = xmalloc (nbytes);
c->rows = rows = xmalloc (height * sizeof *rows);
for (i = 0; i < height; ++i)
rows[i] = pixels + i * row_bytes;
jpeg_load_body (struct frame *f, struct image *img,
struct my_jpeg_error_mgr *mgr)
{
- Lisp_Object file, specified_file;
+ Lisp_Object specified_file;
Lisp_Object specified_data;
/* The 'volatile' silences a bogus diagnostic; see GCC bug 54561. */
FILE * IF_LINT (volatile) fp = NULL;
if (NILP (specified_data))
{
- file = x_find_image_file (specified_file);
+ int fd;
+ Lisp_Object file = x_find_image_fd (specified_file, &fd);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM,
- specified_file);
+ image_error ("Cannot find image file `%s'", specified_file);
return 0;
}
- fp = emacs_fopen (SSDATA (file), "rb");
+ fp = fdopen (fd, "rb");
if (fp == NULL)
{
- image_error ("Cannot open "uLSQM"%s"uRSQM, file);
+ image_error ("Cannot open `%s'", file);
return 0;
}
}
else if (!STRINGP (specified_data))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, specified_data);
+ image_error ("Invalid image data `%s'", specified_data);
return 0;
}
{
char buf[JMSG_LENGTH_MAX];
mgr->cinfo.err->format_message ((j_common_ptr) &mgr->cinfo, buf);
- image_error ("Error reading JPEG image "uLSQM"%s"uRSQM": %s",
+ image_error ("Error reading JPEG image `%s': %s",
img->spec, build_string (buf));
break;
}
case MY_JPEG_INVALID_IMAGE_SIZE:
- image_error ("Invalid image size (see "
- uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
break;
case MY_JPEG_CANNOT_CREATE_X:
static bool
tiff_load (struct frame *f, struct image *img)
{
- Lisp_Object file, specified_file;
+ Lisp_Object specified_file;
Lisp_Object specified_data;
TIFF *tiff;
int width, height, x, y, count;
if (NILP (specified_data))
{
/* Read from a file */
- file = x_find_image_file (specified_file);
+ Lisp_Object file = x_find_image_file (specified_file);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM,
- specified_file);
+ image_error ("Cannot find image file `%s'", specified_file);
return 0;
}
+
+ Lisp_Object encoded_file = ENCODE_FILE (file);
# ifdef WINDOWSNT
- file = ansi_encode_filename (file);
+ encoded_file = ansi_encode_filename (encoded_file);
# endif
/* Try to open the image file. */
- tiff = TIFFOpen (SSDATA (file), "r");
+ tiff = TIFFOpen (SSDATA (encoded_file), "r");
if (tiff == NULL)
{
- image_error ("Cannot open "uLSQM"%s"uRSQM, file);
+ image_error ("Cannot open `%s'", file);
return 0;
}
}
{
if (!STRINGP (specified_data))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, specified_data);
+ image_error ("Invalid image data `%s'", specified_data);
return 0;
}
if (!tiff)
{
- image_error ("Cannot open memory source for "uLSQM"%s"uRSQM,
- img->spec);
+ image_error ("Cannot open memory source for `%s'", img->spec);
return 0;
}
}
if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t)
&& TIFFSetDirectory (tiff, ino)))
{
- image_error
- ("Invalid image number "uLSQM"%s"uRSQM" in image "uLSQM"%s"uRSQM,
- image, img->spec);
+ image_error ("Invalid image number `%s' in image `%s'",
+ image, img->spec);
TIFFClose (tiff);
return 0;
}
if (!check_image_size (f, width, height))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
TIFFClose (tiff);
return 0;
}
TIFFClose (tiff);
if (!rc)
{
- image_error ("Error reading TIFF image "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Error reading TIFF image `%s'", img->spec);
xfree (buf);
return 0;
}
{
unsigned char *data = (unsigned char *) xmalloc (width*height*4);
uint32_t *dataptr = (uint32_t *) data;
- int r, g, b, a;
for (y = 0; y < height; ++y)
{
# define GIFLIB_MAJOR 4
# endif
+/* GifErrorString is declared to return char const * when GIFLIB_MAJOR
+ and GIFLIB_MINOR indicate 5.1 or later. Do not bother using it in
+ earlier releases, where either it returns char * or GIFLIB_MINOR
+ may be incorrect. */
+# define HAVE_GIFERRORSTRING (5 < GIFLIB_MAJOR + (1 <= GIFLIB_MINOR))
+
# ifdef WINDOWSNT
/* GIF library details. */
# else
DEF_DLL_FN (GifFileType *, DGifOpen, (void *, InputFunc, int *));
DEF_DLL_FN (GifFileType *, DGifOpenFileName, (const char *, int *));
-DEF_DLL_FN (char *, GifErrorString, (int));
+# endif
+# if HAVE_GIFERRORSTRING
+DEF_DLL_FN (char const *, GifErrorString, (int));
# endif
static bool
LOAD_DLL_FN (library, DGifSlurp);
LOAD_DLL_FN (library, DGifOpen);
LOAD_DLL_FN (library, DGifOpenFileName);
-# if GIFLIB_MAJOR >= 5
+# if HAVE_GIFERRORSTRING
LOAD_DLL_FN (library, GifErrorString);
# endif
return 1;
static bool
gif_load (struct frame *f, struct image *img)
{
- Lisp_Object file;
int rc, width, height, x, y, i, j;
ColorMapObject *gif_color_map;
- unsigned long pixel_colors[256];
GifFileType *gif;
gif_memory_source memsrc;
Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL);
Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL);
Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL);
- unsigned long bgcolor = 0;
EMACS_INT idx;
int gif_err;
#ifdef USE_CAIRO
unsigned char *data = 0;
#else
+ unsigned long pixel_colors[256];
+ unsigned long bgcolor = 0;
XImagePtr ximg;
#endif
if (NILP (specified_data))
{
- file = x_find_image_file (specified_file);
+ Lisp_Object file = x_find_image_file (specified_file);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM,
- specified_file);
+ image_error ("Cannot find image file `%s'", specified_file);
return 0;
}
+
+ Lisp_Object encoded_file = ENCODE_FILE (file);
#ifdef WINDOWSNT
- file = ansi_encode_filename (file);
+ encoded_file = ansi_encode_filename (encoded_file);
#endif
/* Open the GIF file. */
#if GIFLIB_MAJOR < 5
- gif = DGifOpenFileName (SSDATA (file));
- if (gif == NULL)
- {
- image_error ("Cannot open "uLSQM"%s"uRSQM, file);
- return 0;
- }
+ gif = DGifOpenFileName (SSDATA (encoded_file));
#else
- gif = DGifOpenFileName (SSDATA (file), &gif_err);
+ gif = DGifOpenFileName (SSDATA (encoded_file), &gif_err);
+#endif
if (gif == NULL)
{
- image_error ("Cannot open "uLSQM"%s"uRSQM": %s",
+#if HAVE_GIFERRORSTRING
+ image_error ("Cannot open `%s': %s",
file, build_string (GifErrorString (gif_err)));
+#else
+ image_error ("Cannot open `%s'", file);
+#endif
return 0;
}
-#endif
}
else
{
if (!STRINGP (specified_data))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, specified_data);
+ image_error ("Invalid image data `%s'", specified_data);
return 0;
}
#if GIFLIB_MAJOR < 5
gif = DGifOpen (&memsrc, gif_read_from_memory);
- if (!gif)
- {
- image_error ("Cannot open memory source "uLSQM"%s"uRSQM, img->spec);
- return 0;
- }
#else
gif = DGifOpen (&memsrc, gif_read_from_memory, &gif_err);
+#endif
if (!gif)
{
- image_error ("Cannot open memory source "uLSQM"%s"uRSQM": %s",
+#if HAVE_GIFERRORSTRING
+ image_error ("Cannot open memory source `%s': %s",
img->spec, build_string (GifErrorString (gif_err)));
+#else
+ image_error ("Cannot open memory source `%s'", img->spec);
+#endif
return 0;
}
-#endif
}
/* Before reading entire contents, check the declared image size. */
if (!check_image_size (f, gif->SWidth, gif->SHeight))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
gif_close (gif, NULL);
return 0;
}
rc = DGifSlurp (gif);
if (rc == GIF_ERROR || gif->ImageCount <= 0)
{
- image_error ("Error reading "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Error reading `%s'", img->spec);
gif_close (gif, NULL);
return 0;
}
idx = INTEGERP (image_number) ? XFASTINT (image_number) : 0;
if (idx < 0 || idx >= gif->ImageCount)
{
- image_error
- ("Invalid image number "uLSQM"%s"uRSQM" in image "uLSQM"%s"uRSQM,
- image_number, img->spec);
+ image_error ("Invalid image number `%s' in image `%s'",
+ image_number, img->spec);
gif_close (gif, NULL);
return 0;
}
if (!check_image_size (f, width, height))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
gif_close (gif, NULL);
return 0;
}
gif_load call to construct and save all animation frames. */
init_color_table ();
+
+#ifndef USE_CAIRO
if (STRINGP (specified_bg))
bgcolor = x_alloc_image_color (f, img, specified_bg,
FRAME_BACKGROUND_PIXEL (f));
+#endif
+
for (j = 0; j <= idx; ++j)
{
/* We use a local variable `raster' here because RasterBits is a
{
img->lisp_data
= Fcons (make_number (ext->Function),
- Fcons (make_unibyte_string (ext->Bytes, ext->ByteCount),
+ Fcons (make_unibyte_string ((char *) ext->Bytes,
+ ext->ByteCount),
img->lisp_data));
if (ext->Function == GIF_LOCAL_DESCRIPTOR_EXTENSION
&& ext->ByteCount == 4)
if (gif_close (gif, &gif_err) == GIF_ERROR)
{
-#if 5 <= GIFLIB_MAJOR
- char *error_text = GifErrorString (gif_err);
+#if HAVE_GIFERRORSTRING
+ char const *error_text = GifErrorString (gif_err);
if (error_text)
- image_error ("Error closing "uLSQM"%s"uRSQM": %s",
+ image_error ("Error closing `%s': %s",
img->spec, build_string (error_text));
#else
- image_error ("Error closing "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Error closing `%s'", img->spec);
#endif
}
if (ino < 0 || ino >= MagickGetNumberImages (image_wand))
{
- image_error
- ("Invalid image number "uLSQM"%s"uRSQM" in image "uLSQM"%s"uRSQM,
- image, img->spec);
+ image_error ("Invalid image number `%s' in image `%s'", image, img->spec);
DestroyMagickWand (image_wand);
return 0;
}
if (! (image_width <= INT_MAX && image_height <= INT_MAX
&& check_image_size (f, image_width, image_height)))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
goto imagemagick_error;
}
MagickWandTerminus ();
/* TODO more cleanup. */
- image_error ("Error parsing IMAGEMAGICK image "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Error parsing IMAGEMAGICK image `%s'", img->spec);
return 0;
}
file_name = image_spec_value (img->spec, QCfile, NULL);
if (STRINGP (file_name))
{
- Lisp_Object file;
-
- file = x_find_image_file (file_name);
+ Lisp_Object file = x_find_image_file (file_name);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM, file_name);
+ image_error ("Cannot find image file `%s'", file_name);
return 0;
}
+ file = ENCODE_FILE (file);
#ifdef WINDOWSNT
file = ansi_encode_filename (file);
#endif
data = image_spec_value (img->spec, QCdata, NULL);
if (!STRINGP (data))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, data);
+ image_error ("Invalid image data `%s'", data);
return 0;
}
success_p = imagemagick_load_image (f, img, SDATA (data),
DEF_DLL_FN (void, g_type_init, (void));
# endif
DEF_DLL_FN (void, g_object_unref, (gpointer));
-DEF_DLL_FN (void, g_error_free, (GError *));
+DEF_DLL_FN (void, g_clear_error, (GError **));
static bool
init_svg_functions (void)
LOAD_DLL_FN (gobject, g_type_init);
# endif
LOAD_DLL_FN (gobject, g_object_unref);
- LOAD_DLL_FN (glib, g_error_free);
+ LOAD_DLL_FN (glib, g_clear_error);
return 1;
}
# undef gdk_pixbuf_get_pixels
# undef gdk_pixbuf_get_rowstride
# undef gdk_pixbuf_get_width
-# undef g_error_free
+# undef g_clear_error
# undef g_object_unref
# undef g_type_init
# undef rsvg_handle_close
# define gdk_pixbuf_get_pixels fn_gdk_pixbuf_get_pixels
# define gdk_pixbuf_get_rowstride fn_gdk_pixbuf_get_rowstride
# define gdk_pixbuf_get_width fn_gdk_pixbuf_get_width
-# define g_error_free fn_g_error_free
+# define g_clear_error fn_g_clear_error
# define g_object_unref fn_g_object_unref
# define g_type_init fn_g_type_init
# define rsvg_handle_close fn_rsvg_handle_close
file_name = image_spec_value (img->spec, QCfile, NULL);
if (STRINGP (file_name))
{
- Lisp_Object file;
- unsigned char *contents;
- ptrdiff_t size;
-
- file = x_find_image_file (file_name);
+ int fd;
+ Lisp_Object file = x_find_image_fd (file_name, &fd);
if (!STRINGP (file))
{
- image_error ("Cannot find image file "uLSQM"%s"uRSQM, file_name);
+ image_error ("Cannot find image file `%s'", file_name);
return 0;
}
/* Read the entire file into memory. */
- contents = slurp_file (SSDATA (file), &size);
+ ptrdiff_t size;
+ unsigned char *contents = slurp_file (fd, &size);
if (contents == NULL)
{
- image_error ("Error loading SVG image "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Error loading SVG image `%s'", file);
return 0;
}
/* If the file was slurped into memory properly, parse it. */
- success_p = svg_load_image (f, img, contents, size, SSDATA (file));
+ success_p = svg_load_image (f, img, contents, size,
+ SSDATA (ENCODE_FILE (file)));
xfree (contents);
}
/* Else its not a file, its a lisp object. Load the image from a
data = image_spec_value (img->spec, QCdata, NULL);
if (!STRINGP (data))
{
- image_error ("Invalid image data "uLSQM"%s"uRSQM, data);
+ image_error ("Invalid image data `%s'", data);
return 0;
}
original_filename = BVAR (current_buffer, filename);
int height;
const guint8 *pixels;
int rowstride;
- XImagePtr ximg;
- Lisp_Object specified_bg;
- XColor background;
- int x;
- int y;
#if ! GLIB_CHECK_VERSION (2, 36, 0)
/* g_type_init is a glib function that must be called prior to
rsvg_handle_get_dimensions (rsvg_handle, &dimension_data);
if (! check_image_size (f, dimension_data.width, dimension_data.height))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
goto rsvg_error;
}
#ifdef USE_CAIRO
{
unsigned char *data = (unsigned char *) xmalloc (width*height*4);
- int y;
uint32_t bgcolor = get_spec_bg_or_alpha_as_argb (img, f);
- for (y = 0; y < height; ++y)
+ for (int y = 0; y < height; ++y)
{
const guchar *iconptr = pixels + y * rowstride;
uint32_t *dataptr = (uint32_t *) (data + y * rowstride);
- int x;
- for (x = 0; x < width; ++x)
+ for (int x = 0; x < width; ++x)
{
if (iconptr[3] == 0)
*dataptr = bgcolor;
}
#else
/* Try to create a x pixmap to hold the svg pixmap. */
+ XImagePtr ximg;
if (!image_create_x_image_and_pixmap (f, img, width, height, 0, &ximg, 0))
{
g_object_unref (pixbuf);
/* Handle alpha channel by combining the image with a background
color. */
- specified_bg = image_spec_value (img->spec, QCbackground, NULL);
+ XColor background;
+ Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL);
if (!STRINGP (specified_bg)
|| !x_defined_color (f, SSDATA (specified_bg), &background, 0))
x_query_frame_background_color (f, &background);
non-transparent images. Each pixel must be "flattened" by
calculating the resulting color, given the transparency of the
pixel, and the image background color. */
- for (y = 0; y < height; ++y)
+ for (int y = 0; y < height; ++y)
{
- for (x = 0; x < width; ++x)
+ for (int x = 0; x < width; ++x)
{
int red;
int green;
g_object_unref (rsvg_handle);
/* FIXME: Use error->message so the user knows what is the actual
problem with the image. */
- image_error ("Error parsing SVG image "uLSQM"%s"uRSQM, img->spec);
- g_error_free (err);
+ image_error ("Error parsing SVG image `%s'", img->spec);
+ g_clear_error (&err);
return 0;
}
if (! (in_width <= INT_MAX && in_height <= INT_MAX
&& check_image_size (f, in_width, in_height)))
{
- image_error ("Invalid image size (see "uLSQM"max-image-size"uRSQM")");
+ image_size_error ();
return 0;
}
img->width = in_width;
if (!img->pixmap)
{
- image_error ("Unable to create pixmap for "uLSQM"%s"uRSQM, img->spec);
+ image_error ("Unable to create pixmap for `%s'" , img->spec);
return 0;
}
x_kill_gs_process (Pixmap pixmap, struct frame *f)
{
struct image_cache *c = FRAME_IMAGE_CACHE (f);
- int class;
ptrdiff_t i;
struct image *img;
/* On displays with a mutable colormap, figure out the colors
allocated for the image by looking at the pixels of an XImage for
img->pixmap. */
- class = FRAME_X_VISUAL (f)->class;
- if (class != StaticColor && class != StaticGray && class != TrueColor)
+ if (x_mutable_colormap (FRAME_X_VISUAL (f)))
{
XImagePtr ximg;
0, 0, img->width, img->height, ~0, ZPixmap);
if (ximg)
{
- int x, y;
-
/* Initialize the color table. */
init_color_table ();
color table. After having done so, the color table will
contain an entry for each color used by the image. */
#ifdef COLOR_TABLE_SUPPORT
- for (y = 0; y < img->height; ++y)
- for (x = 0; x < img->width; ++x)
+ for (int y = 0; y < img->height; ++y)
+ for (int x = 0; x < img->width; ++x)
{
unsigned long pixel = XGetPixel (ximg, x, y);
#endif
}
else
- image_error (("Cannot get X image of "uLSQM"%s"uRSQM";"
- " colors will not be freed"),
+ image_error ("Cannot get X image of `%s'; colors will not be freed",
img->spec);
unblock_input ();
defining the supported image types. */
DEFVAR_LISP ("image-types", Vimage_types,
doc: /* List of potentially supported image types.
-Each element of the list is a symbol for an image type, like 'jpeg or 'png.
+Each element of the list is a symbol for an image type, like `jpeg' or `png'.
To check whether it is really supported, use `image-type-available-p'. */);
Vimage_types = Qnil;