Extract function slurp_image from image loading

* src/image.c (slurp_image): New function...
(xbm_load, xpm_load, pbm_load, webp_load, svg_load): ...extracted from
here.
This commit is contained in:
Stefan Kangas 2023-10-05 23:07:39 +02:00
parent eb5a453a58
commit bf4d4ab4dd

View file

@ -4355,6 +4355,27 @@ slurp_file (image_fd fd, ptrdiff_t *size)
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;
}
/***********************************************************************
@ -5073,22 +5094,10 @@ xbm_load (struct frame *f, struct image *img)
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);
}
@ -6369,21 +6378,10 @@ xpm_load (struct frame *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, "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);
@ -7398,21 +7396,10 @@ pbm_load (struct frame *f, struct image *img)
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;
@ -10302,20 +10289,9 @@ webp_load (struct frame *f, struct image *img)
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
{
@ -11708,22 +11684,11 @@ svg_load (struct frame *f, struct image *img)
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;