aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Kangas2023-10-05 23:07:39 +0200
committerStefan Kangas2023-10-05 23:09:56 +0200
commitbf4d4ab4ddecffbee6d740f9c271dcca514d6a3d (patch)
treee928c6f5e4712ec8c296abdbbf57a33403738bc0
parenteb5a453a58a4d7fe1ab213d0fdb746ccb65b9909 (diff)
downloademacs-bf4d4ab4ddecffbee6d740f9c271dcca514d6a3d.tar.gz
emacs-bf4d4ab4ddecffbee6d740f9c271dcca514d6a3d.zip
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.
-rw-r--r--src/image.c99
1 files changed, 32 insertions, 67 deletions
diff --git a/src/image.c b/src/image.c
index 84db9bfb3b8..fcae13e2302 100644
--- a/src/image.c
+++ b/src/image.c
@@ -4355,6 +4355,27 @@ slurp_file (image_fd fd, ptrdiff_t *size)
4355 return buf; 4355 return buf;
4356} 4356}
4357 4357
4358/* Like slurp_file above, but with added error handling. Value is
4359 null if an error occurred. Set SIZE to the size of the file.
4360 IMAGE_TYPE describes the image type (e.g. "PNG"). */
4361
4362static char *
4363slurp_image (Lisp_Object filename, ptrdiff_t *size, const char *image_type)
4364{
4365 image_fd fd;
4366 Lisp_Object file = image_find_image_fd (filename, &fd);
4367 if (!STRINGP (file))
4368 {
4369 image_not_found_error (filename);
4370 return NULL;
4371 }
4372 char *result = slurp_file (fd, size);
4373 if (result == NULL)
4374 image_error ("Error loading %s image `%s'",
4375 make_unibyte_string (image_type, strlen (image_type)),
4376 file);
4377 return result;
4378}
4358 4379
4359 4380
4360/*********************************************************************** 4381/***********************************************************************
@@ -5073,22 +5094,10 @@ xbm_load (struct frame *f, struct image *img)
5073 file_name = image_spec_value (img->spec, QCfile, NULL); 5094 file_name = image_spec_value (img->spec, QCfile, NULL);
5074 if (STRINGP (file_name)) 5095 if (STRINGP (file_name))
5075 { 5096 {
5076 image_fd fd;
5077 Lisp_Object file = image_find_image_fd (file_name, &fd);
5078 if (!STRINGP (file))
5079 {
5080 image_not_found_error (file_name);
5081 return false;
5082 }
5083
5084 ptrdiff_t size; 5097 ptrdiff_t size;
5085 char *contents = slurp_file (fd, &size); 5098 char *contents = slurp_image (file_name, &size, "XBM");
5086 if (contents == NULL) 5099 if (contents == NULL)
5087 { 5100 return false;
5088 image_error ("Error loading XBM image `%s'", file);
5089 return 0;
5090 }
5091
5092 success_p = xbm_load_image (f, img, contents, contents + size); 5101 success_p = xbm_load_image (f, img, contents, contents + size);
5093 xfree (contents); 5102 xfree (contents);
5094 } 5103 }
@@ -6369,21 +6378,10 @@ xpm_load (struct frame *f,
6369 file_name = image_spec_value (img->spec, QCfile, NULL); 6378 file_name = image_spec_value (img->spec, QCfile, NULL);
6370 if (STRINGP (file_name)) 6379 if (STRINGP (file_name))
6371 { 6380 {
6372 image_fd fd;
6373 Lisp_Object file = image_find_image_fd (file_name, &fd);
6374 if (!STRINGP (file))
6375 {
6376 image_not_found_error (file_name);
6377 return false;
6378 }
6379
6380 ptrdiff_t size; 6381 ptrdiff_t size;
6381 char *contents = slurp_file (fd, &size); 6382 char *contents = slurp_image (file_name, &size, "XPM");
6382 if (contents == NULL) 6383 if (contents == NULL)
6383 { 6384 return false;
6384 image_error ("Error loading XPM image `%s'", file);
6385 return 0;
6386 }
6387 6385
6388 success_p = xpm_load_image (f, img, contents, contents + size); 6386 success_p = xpm_load_image (f, img, contents, contents + size);
6389 xfree (contents); 6387 xfree (contents);
@@ -7398,21 +7396,10 @@ pbm_load (struct frame *f, struct image *img)
7398 7396
7399 if (STRINGP (specified_file)) 7397 if (STRINGP (specified_file))
7400 { 7398 {
7401 image_fd fd;
7402 Lisp_Object file = image_find_image_fd (specified_file, &fd);
7403 if (!STRINGP (file))
7404 {
7405 image_not_found_error (specified_file);
7406 return false;
7407 }
7408
7409 ptrdiff_t size; 7399 ptrdiff_t size;
7410 contents = slurp_file (fd, &size); 7400 contents = slurp_image (specified_file, &size, "PBM");
7411 if (contents == NULL) 7401 if (contents == NULL)
7412 { 7402 return false;
7413 image_error ("Error reading `%s'", file);
7414 return 0;
7415 }
7416 7403
7417 p = contents; 7404 p = contents;
7418 end = contents + size; 7405 end = contents + size;
@@ -10302,20 +10289,9 @@ webp_load (struct frame *f, struct image *img)
10302 10289
10303 if (NILP (specified_data)) 10290 if (NILP (specified_data))
10304 { 10291 {
10305 image_fd fd; 10292 contents = (uint8_t *) slurp_image (specified_file, &size, "WebP");
10306 file = image_find_image_fd (specified_file, &fd);
10307 if (!STRINGP (file))
10308 {
10309 image_not_found_error (specified_file);
10310 return false;
10311 }
10312
10313 contents = (uint8_t *) slurp_file (fd, &size);
10314 if (contents == NULL) 10293 if (contents == NULL)
10315 { 10294 return false;
10316 image_error ("Error loading WebP image `%s'", file);
10317 return false;
10318 }
10319 } 10295 }
10320 else 10296 else
10321 { 10297 {
@@ -11708,22 +11684,11 @@ svg_load (struct frame *f, struct image *img)
11708 base_uri = image_spec_value (img->spec, QCbase_uri, NULL); 11684 base_uri = image_spec_value (img->spec, QCbase_uri, NULL);
11709 if (STRINGP (file_name)) 11685 if (STRINGP (file_name))
11710 { 11686 {
11711 image_fd fd;
11712 Lisp_Object file = image_find_image_fd (file_name, &fd);
11713 if (!STRINGP (file))
11714 {
11715 image_not_found_error (file_name);
11716 return false;
11717 }
11718
11719 /* Read the entire file into memory. */
11720 ptrdiff_t size; 11687 ptrdiff_t size;
11721 char *contents = slurp_file (fd, &size); 11688 char *contents = slurp_image (file_name, &size, "SVG");
11722 if (contents == NULL) 11689 if (contents == NULL)
11723 { 11690 return false;
11724 image_error ("Error loading SVG image `%s'", file); 11691
11725 return 0;
11726 }
11727 /* If the file was slurped into memory properly, parse it. */ 11692 /* If the file was slurped into memory properly, parse it. */
11728 if (!STRINGP (base_uri)) 11693 if (!STRINGP (base_uri))
11729 base_uri = file; 11694 base_uri = file;