return buf;
}
+/* Like slurp_file above, but with added error handling. Value is
+ null if an error occurred. Set SIZE to the size of the file.
+ IMAGE_TYPE describes the image type (e.g. "PNG"). */
+
+static char *
+slurp_image (Lisp_Object filename, ptrdiff_t *size, const char *image_type)
+{
+ image_fd fd;
+ Lisp_Object file = image_find_image_fd (filename, &fd);
+ if (!STRINGP (file))
+ {
+ image_not_found_error (filename);
+ return NULL;
+ }
+ char *result = slurp_file (fd, size);
+ if (result == NULL)
+ image_error ("Error loading %s image `%s'",
+ make_unibyte_string (image_type, strlen (image_type)),
+ file);
+ return result;
+}
\f
/***********************************************************************
file_name = image_spec_value (img->spec, QCfile, NULL);
if (STRINGP (file_name))
{
- image_fd fd;
- Lisp_Object file = image_find_image_fd (file_name, &fd);
- if (!STRINGP (file))
- {
- image_not_found_error (file_name);
- return false;
- }
-
ptrdiff_t size;
- char *contents = slurp_file (fd, &size);
+ char *contents = slurp_image (file_name, &size, "XBM");
if (contents == NULL)
- {
- image_error ("Error loading XBM image `%s'", file);
- return 0;
- }
-
+ return false;
success_p = xbm_load_image (f, img, contents, contents + size);
xfree (contents);
}
file_name = image_spec_value (img->spec, QCfile, NULL);
if (STRINGP (file_name))
{
- image_fd fd;
- Lisp_Object file = image_find_image_fd (file_name, &fd);
- if (!STRINGP (file))
- {
- image_not_found_error (file_name);
- return false;
- }
-
ptrdiff_t size;
- char *contents = slurp_file (fd, &size);
+ char *contents = slurp_image (file_name, &size, "XPM");
if (contents == NULL)
- {
- image_error ("Error loading XPM image `%s'", file);
- return 0;
- }
+ return false;
success_p = xpm_load_image (f, img, contents, contents + size);
xfree (contents);
if (STRINGP (specified_file))
{
- image_fd fd;
- Lisp_Object file = image_find_image_fd (specified_file, &fd);
- if (!STRINGP (file))
- {
- image_not_found_error (specified_file);
- return false;
- }
-
ptrdiff_t size;
- contents = slurp_file (fd, &size);
+ contents = slurp_image (specified_file, &size, "PBM");
if (contents == NULL)
- {
- image_error ("Error reading `%s'", file);
- return 0;
- }
+ return false;
p = contents;
end = contents + size;
if (NILP (specified_data))
{
- image_fd fd;
- file = image_find_image_fd (specified_file, &fd);
- if (!STRINGP (file))
- {
- image_not_found_error (specified_file);
- return false;
- }
-
- contents = (uint8_t *) slurp_file (fd, &size);
+ contents = (uint8_t *) slurp_image (specified_file, &size, "WebP");
if (contents == NULL)
- {
- image_error ("Error loading WebP image `%s'", file);
- return false;
- }
+ return false;
}
else
{
base_uri = image_spec_value (img->spec, QCbase_uri, NULL);
if (STRINGP (file_name))
{
- image_fd fd;
- Lisp_Object file = image_find_image_fd (file_name, &fd);
- if (!STRINGP (file))
- {
- image_not_found_error (file_name);
- return false;
- }
-
- /* Read the entire file into memory. */
ptrdiff_t size;
- char *contents = slurp_file (fd, &size);
+ char *contents = slurp_image (file_name, &size, "SVG");
if (contents == NULL)
- {
- image_error ("Error loading SVG image `%s'", file);
- return 0;
- }
+ return false;
+
/* If the file was slurped into memory properly, parse it. */
if (!STRINGP (base_uri))
base_uri = file;