aboutsummaryrefslogtreecommitdiffstats
path: root/src/image.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/image.c')
-rw-r--r--src/image.c250
1 files changed, 133 insertions, 117 deletions
diff --git a/src/image.c b/src/image.c
index 65be22b087a..d0d28bea582 100644
--- a/src/image.c
+++ b/src/image.c
@@ -196,7 +196,7 @@ x_bitmap_width (FRAME_PTR f, ptrdiff_t id)
196int 196int
197x_bitmap_pixmap (FRAME_PTR f, ptrdiff_t id) 197x_bitmap_pixmap (FRAME_PTR f, ptrdiff_t id)
198{ 198{
199 return (int) FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap; 199 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
200} 200}
201#endif 201#endif
202 202
@@ -216,15 +216,6 @@ x_allocate_bitmap_record (FRAME_PTR f)
216 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); 216 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
217 ptrdiff_t i; 217 ptrdiff_t i;
218 218
219 if (dpyinfo->bitmaps == NULL)
220 {
221 dpyinfo->bitmaps_size = 10;
222 dpyinfo->bitmaps
223 = (Bitmap_Record *) xmalloc (dpyinfo->bitmaps_size * sizeof (Bitmap_Record));
224 dpyinfo->bitmaps_last = 1;
225 return 1;
226 }
227
228 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size) 219 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
229 return ++dpyinfo->bitmaps_last; 220 return ++dpyinfo->bitmaps_last;
230 221
@@ -232,20 +223,16 @@ x_allocate_bitmap_record (FRAME_PTR f)
232 if (dpyinfo->bitmaps[i].refcount == 0) 223 if (dpyinfo->bitmaps[i].refcount == 0)
233 return i + 1; 224 return i + 1;
234 225
235 if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (Bitmap_Record) / 2 226 dpyinfo->bitmaps =
236 < dpyinfo->bitmaps_size) 227 xpalloc (dpyinfo->bitmaps, &dpyinfo->bitmaps_size,
237 memory_full (SIZE_MAX); 228 10, -1, sizeof *dpyinfo->bitmaps);
238 dpyinfo->bitmaps_size *= 2;
239 dpyinfo->bitmaps
240 = (Bitmap_Record *) xrealloc (dpyinfo->bitmaps,
241 dpyinfo->bitmaps_size * sizeof (Bitmap_Record));
242 return ++dpyinfo->bitmaps_last; 229 return ++dpyinfo->bitmaps_last;
243} 230}
244 231
245/* Add one reference to the reference count of the bitmap with id ID. */ 232/* Add one reference to the reference count of the bitmap with id ID. */
246 233
247void 234void
248x_reference_bitmap (FRAME_PTR f, int id) 235x_reference_bitmap (FRAME_PTR f, ptrdiff_t id)
249{ 236{
250 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount; 237 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
251} 238}
@@ -807,29 +794,30 @@ parse_image_spec (Lisp_Object spec, struct image_keyword *keywords,
807 break; 794 break;
808 795
809 case IMAGE_POSITIVE_INTEGER_VALUE: 796 case IMAGE_POSITIVE_INTEGER_VALUE:
810 if (!INTEGERP (value) || XINT (value) <= 0) 797 if (! RANGED_INTEGERP (1, value, INT_MAX))
811 return 0; 798 return 0;
812 break; 799 break;
813 800
814 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR: 801 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
815 if (INTEGERP (value) && XINT (value) >= 0) 802 if (RANGED_INTEGERP (1, value, INT_MAX))
816 break; 803 break;
817 if (CONSP (value) 804 if (CONSP (value)
818 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value)) 805 && RANGED_INTEGERP (1, XCAR (value), INT_MAX)
819 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0) 806 && RANGED_INTEGERP (1, XCDR (value), INT_MAX))
820 break; 807 break;
821 return 0; 808 return 0;
822 809
823 case IMAGE_ASCENT_VALUE: 810 case IMAGE_ASCENT_VALUE:
824 if (SYMBOLP (value) && EQ (value, Qcenter)) 811 if (SYMBOLP (value) && EQ (value, Qcenter))
825 break; 812 break;
826 else if (INTEGERP (value) 813 else if (RANGED_INTEGERP (0, value, 100))
827 && XINT (value) >= 0
828 && XINT (value) <= 100)
829 break; 814 break;
830 return 0; 815 return 0;
831 816
832 case IMAGE_NON_NEGATIVE_INTEGER_VALUE: 817 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
818 /* Unlike the other integer-related cases, this one does not
819 verify that VALUE fits in 'int'. This is because callers
820 want EMACS_INT. */
833 if (!INTEGERP (value) || XINT (value) < 0) 821 if (!INTEGERP (value) || XINT (value) < 0)
834 return 0; 822 return 0;
835 break; 823 break;
@@ -849,7 +837,7 @@ parse_image_spec (Lisp_Object spec, struct image_keyword *keywords,
849 break; 837 break;
850 838
851 case IMAGE_INTEGER_VALUE: 839 case IMAGE_INTEGER_VALUE:
852 if (!INTEGERP (value)) 840 if (! TYPE_RANGED_INTEGERP (int, value))
853 return 0; 841 return 0;
854 break; 842 break;
855 843
@@ -919,7 +907,7 @@ or omitted means use the selected frame. */)
919 if (valid_image_p (spec)) 907 if (valid_image_p (spec))
920 { 908 {
921 struct frame *f = check_x_frame (frame); 909 struct frame *f = check_x_frame (frame);
922 int id = lookup_image (f, spec); 910 ptrdiff_t id = lookup_image (f, spec);
923 struct image *img = IMAGE_FROM_ID (f, id); 911 struct image *img = IMAGE_FROM_ID (f, id);
924 int width = img->width + 2 * img->hmargin; 912 int width = img->width + 2 * img->hmargin;
925 int height = img->height + 2 * img->vmargin; 913 int height = img->height + 2 * img->vmargin;
@@ -949,7 +937,7 @@ or omitted means use the selected frame. */)
949 if (valid_image_p (spec)) 937 if (valid_image_p (spec))
950 { 938 {
951 struct frame *f = check_x_frame (frame); 939 struct frame *f = check_x_frame (frame);
952 int id = lookup_image (f, spec); 940 ptrdiff_t id = lookup_image (f, spec);
953 struct image *img = IMAGE_FROM_ID (f, id); 941 struct image *img = IMAGE_FROM_ID (f, id);
954 if (img->mask) 942 if (img->mask)
955 mask = Qt; 943 mask = Qt;
@@ -972,7 +960,7 @@ or omitted means use the selected frame. */)
972 if (valid_image_p (spec)) 960 if (valid_image_p (spec))
973 { 961 {
974 struct frame *f = check_x_frame (frame); 962 struct frame *f = check_x_frame (frame);
975 int id = lookup_image (f, spec); 963 ptrdiff_t id = lookup_image (f, spec);
976 struct image *img = IMAGE_FROM_ID (f, id); 964 struct image *img = IMAGE_FROM_ID (f, id);
977 ext = img->lisp_data; 965 ext = img->lisp_data;
978 } 966 }
@@ -1125,7 +1113,7 @@ image_ascent (struct image *img, struct face *face, struct glyph_slice *slice)
1125 ascent = height / 2; 1113 ascent = height / 2;
1126 } 1114 }
1127 else 1115 else
1128 ascent = (int) (height * img->ascent / 100.0); 1116 ascent = height * (img->ascent / 100.0);
1129 1117
1130 return ascent; 1118 return ascent;
1131} 1119}
@@ -1371,11 +1359,12 @@ x_alloc_image_color (struct frame *f, struct image *img, Lisp_Object color_name,
1371 { 1359 {
1372 /* This isn't called frequently so we get away with simply 1360 /* This isn't called frequently so we get away with simply
1373 reallocating the color vector to the needed size, here. */ 1361 reallocating the color vector to the needed size, here. */
1374 ++img->ncolors; 1362 ptrdiff_t ncolors = img->ncolors + 1;
1375 img->colors = 1363 img->colors =
1376 (unsigned long *) xrealloc (img->colors, 1364 (unsigned long *) xrealloc (img->colors,
1377 img->ncolors * sizeof *img->colors); 1365 ncolors * sizeof *img->colors);
1378 img->colors[img->ncolors - 1] = color.pixel; 1366 img->colors[ncolors - 1] = color.pixel;
1367 img->ncolors = ncolors;
1379 result = color.pixel; 1368 result = color.pixel;
1380 } 1369 }
1381 else 1370 else
@@ -1403,8 +1392,9 @@ make_image_cache (void)
1403 int size; 1392 int size;
1404 1393
1405 memset (c, 0, sizeof *c); 1394 memset (c, 0, sizeof *c);
1406 c->size = 50; 1395 size = 50;
1407 c->images = (struct image **) xmalloc (c->size * sizeof *c->images); 1396 c->images = (struct image **) xmalloc (size * sizeof *c->images);
1397 c->size = size;
1408 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets; 1398 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
1409 c->buckets = (struct image **) xmalloc (size); 1399 c->buckets = (struct image **) xmalloc (size);
1410 memset (c->buckets, 0, size); 1400 memset (c->buckets, 0, size);
@@ -1470,7 +1460,7 @@ free_image_cache (struct frame *f)
1470 struct image_cache *c = FRAME_IMAGE_CACHE (f); 1460 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1471 if (c) 1461 if (c)
1472 { 1462 {
1473 int i; 1463 ptrdiff_t i;
1474 1464
1475 /* Cache should not be referenced by any frame when freed. */ 1465 /* Cache should not be referenced by any frame when freed. */
1476 xassert (c->refcount == 0); 1466 xassert (c->refcount == 0);
@@ -1500,7 +1490,7 @@ clear_image_cache (struct frame *f, Lisp_Object filter)
1500 1490
1501 if (c) 1491 if (c)
1502 { 1492 {
1503 int i, nfreed = 0; 1493 ptrdiff_t i, nfreed = 0;
1504 1494
1505 /* Block input so that we won't be interrupted by a SIGIO 1495 /* Block input so that we won't be interrupted by a SIGIO
1506 while being in an inconsistent state. */ 1496 while being in an inconsistent state. */
@@ -1524,8 +1514,8 @@ clear_image_cache (struct frame *f, Lisp_Object filter)
1524 { 1514 {
1525 /* Free cache based on timestamp. */ 1515 /* Free cache based on timestamp. */
1526 EMACS_TIME t; 1516 EMACS_TIME t;
1527 time_t old; 1517 double old, delay;
1528 int delay, nimages = 0; 1518 ptrdiff_t nimages = 0;
1529 1519
1530 for (i = 0; i < c->used; ++i) 1520 for (i = 0; i < c->used; ++i)
1531 if (c->images[i]) 1521 if (c->images[i])
@@ -1533,9 +1523,10 @@ clear_image_cache (struct frame *f, Lisp_Object filter)
1533 1523
1534 /* If the number of cached images has grown unusually large, 1524 /* If the number of cached images has grown unusually large,
1535 decrease the cache eviction delay (Bug#6230). */ 1525 decrease the cache eviction delay (Bug#6230). */
1536 delay = XFASTINT (Vimage_cache_eviction_delay); 1526 delay = XINT (Vimage_cache_eviction_delay);
1537 if (nimages > 40) 1527 if (nimages > 40)
1538 delay = max (1, 1600 * delay / (nimages*nimages)); 1528 delay = 1600 * delay / nimages / nimages;
1529 delay = max (delay, 1);
1539 1530
1540 EMACS_GET_TIME (t); 1531 EMACS_GET_TIME (t);
1541 old = EMACS_SECS (t) - delay; 1532 old = EMACS_SECS (t) - delay;
@@ -1711,7 +1702,7 @@ postprocess_image (struct frame *f, struct image *img)
1711/* Return the id of image with Lisp specification SPEC on frame F. 1702/* Return the id of image with Lisp specification SPEC on frame F.
1712 SPEC must be a valid Lisp image specification (see valid_image_p). */ 1703 SPEC must be a valid Lisp image specification (see valid_image_p). */
1713 1704
1714int 1705ptrdiff_t
1715lookup_image (struct frame *f, Lisp_Object spec) 1706lookup_image (struct frame *f, Lisp_Object spec)
1716{ 1707{
1717 struct image *img; 1708 struct image *img;
@@ -1770,15 +1761,12 @@ lookup_image (struct frame *f, Lisp_Object spec)
1770 img->ascent = CENTERED_IMAGE_ASCENT; 1761 img->ascent = CENTERED_IMAGE_ASCENT;
1771 1762
1772 margin = image_spec_value (spec, QCmargin, NULL); 1763 margin = image_spec_value (spec, QCmargin, NULL);
1773 if (INTEGERP (margin) && XINT (margin) >= 0) 1764 if (INTEGERP (margin))
1774 img->vmargin = img->hmargin = XFASTINT (margin); 1765 img->vmargin = img->hmargin = XFASTINT (margin);
1775 else if (CONSP (margin) && INTEGERP (XCAR (margin)) 1766 else if (CONSP (margin))
1776 && INTEGERP (XCDR (margin)))
1777 { 1767 {
1778 if (XINT (XCAR (margin)) > 0) 1768 img->hmargin = XFASTINT (XCAR (margin));
1779 img->hmargin = XFASTINT (XCAR (margin)); 1769 img->vmargin = XFASTINT (XCDR (margin));
1780 if (XINT (XCDR (margin)) > 0)
1781 img->vmargin = XFASTINT (XCDR (margin));
1782 } 1770 }
1783 1771
1784 relief = image_spec_value (spec, QCrelief, NULL); 1772 relief = image_spec_value (spec, QCrelief, NULL);
@@ -1825,7 +1813,7 @@ static void
1825cache_image (struct frame *f, struct image *img) 1813cache_image (struct frame *f, struct image *img)
1826{ 1814{
1827 struct image_cache *c = FRAME_IMAGE_CACHE (f); 1815 struct image_cache *c = FRAME_IMAGE_CACHE (f);
1828 int i; 1816 ptrdiff_t i;
1829 1817
1830 /* Find a free slot in c->images. */ 1818 /* Find a free slot in c->images. */
1831 for (i = 0; i < c->used; ++i) 1819 for (i = 0; i < c->used; ++i)
@@ -1834,13 +1822,7 @@ cache_image (struct frame *f, struct image *img)
1834 1822
1835 /* If no free slot found, maybe enlarge c->images. */ 1823 /* If no free slot found, maybe enlarge c->images. */
1836 if (i == c->used && c->used == c->size) 1824 if (i == c->used && c->used == c->size)
1837 { 1825 c->images = xpalloc (c->images, &c->size, 1, -1, sizeof *c->images);
1838 if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *c->images / 2 < c->size)
1839 memory_full (SIZE_MAX);
1840 c->size *= 2;
1841 c->images = (struct image **) xrealloc (c->images,
1842 c->size * sizeof *c->images);
1843 }
1844 1826
1845 /* Add IMG to c->images, and assign IMG an id. */ 1827 /* Add IMG to c->images, and assign IMG an id. */
1846 c->images[i] = img; 1828 c->images[i] = img;
@@ -1879,7 +1861,7 @@ mark_image_cache (struct image_cache *c)
1879{ 1861{
1880 if (c) 1862 if (c)
1881 { 1863 {
1882 int i; 1864 ptrdiff_t i;
1883 for (i = 0; i < c->used; ++i) 1865 for (i = 0; i < c->used; ++i)
1884 if (c->images[i]) 1866 if (c->images[i])
1885 mark_image (c->images[i]); 1867 mark_image (c->images[i]);
@@ -2076,7 +2058,7 @@ x_create_x_image_and_pixmap (struct frame *f, int width, int height, int depth,
2076 DWORD err = GetLastError (); 2058 DWORD err = GetLastError ();
2077 Lisp_Object errcode; 2059 Lisp_Object errcode;
2078 /* All system errors are < 10000, so the following is safe. */ 2060 /* All system errors are < 10000, so the following is safe. */
2079 XSETINT (errcode, (int) err); 2061 XSETINT (errcode, err);
2080 image_error ("Unable to create bitmap, error code %d", errcode, Qnil); 2062 image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
2081 x_destroy_x_image (*ximg); 2063 x_destroy_x_image (*ximg);
2082 return 0; 2064 return 0;
@@ -2355,7 +2337,7 @@ xbm_image_p (Lisp_Object object)
2355 else 2337 else
2356 { 2338 {
2357 Lisp_Object data; 2339 Lisp_Object data;
2358 EMACS_INT width, height; 2340 int width, height;
2359 2341
2360 /* Entries for `:width', `:height' and `:data' must be present. */ 2342 /* Entries for `:width', `:height' and `:data' must be present. */
2361 if (!kw[XBM_WIDTH].count 2343 if (!kw[XBM_WIDTH].count
@@ -3587,9 +3569,8 @@ xpm_load (struct frame *f, struct image *img)
3587#endif /* HAVE_NTGUI */ 3569#endif /* HAVE_NTGUI */
3588 3570
3589 /* Remember allocated colors. */ 3571 /* Remember allocated colors. */
3572 img->colors = xnmalloc (attrs.nalloc_pixels, sizeof *img->colors);
3590 img->ncolors = attrs.nalloc_pixels; 3573 img->ncolors = attrs.nalloc_pixels;
3591 img->colors = (unsigned long *) xmalloc (img->ncolors
3592 * sizeof *img->colors);
3593 for (i = 0; i < attrs.nalloc_pixels; ++i) 3574 for (i = 0; i < attrs.nalloc_pixels; ++i)
3594 { 3575 {
3595 img->colors[i] = attrs.alloc_pixels[i]; 3576 img->colors[i] = attrs.alloc_pixels[i];
@@ -3813,8 +3794,8 @@ xpm_get_color_table_h (Lisp_Object color_table,
3813 int chars_len) 3794 int chars_len)
3814{ 3795{
3815 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table); 3796 struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
3816 int i = hash_lookup (table, make_unibyte_string (chars_start, chars_len), 3797 ptrdiff_t i =
3817 NULL); 3798 hash_lookup (table, make_unibyte_string (chars_start, chars_len), NULL);
3818 3799
3819 return i >= 0 ? HASH_VALUE (table, i) : Qnil; 3800 return i >= 0 ? HASH_VALUE (table, i) : Qnil;
3820} 3801}
@@ -4163,6 +4144,12 @@ static struct ct_color **ct_table;
4163/* Number of entries in the color table. */ 4144/* Number of entries in the color table. */
4164 4145
4165static int ct_colors_allocated; 4146static int ct_colors_allocated;
4147enum
4148{
4149 ct_colors_allocated_max =
4150 min (INT_MAX,
4151 min (PTRDIFF_MAX, SIZE_MAX) / sizeof (unsigned long))
4152};
4166 4153
4167/* Initialize the color table. */ 4154/* Initialize the color table. */
4168 4155
@@ -4249,7 +4236,14 @@ lookup_rgb_color (struct frame *f, int r, int g, int b)
4249 XColor color; 4236 XColor color;
4250 Colormap cmap; 4237 Colormap cmap;
4251 int rc; 4238 int rc;
4239#else
4240 COLORREF color;
4241#endif
4252 4242
4243 if (ct_colors_allocated_max <= ct_colors_allocated)
4244 return FRAME_FOREGROUND_PIXEL (f);
4245
4246#ifdef HAVE_X_WINDOWS
4253 color.red = r; 4247 color.red = r;
4254 color.green = g; 4248 color.green = g;
4255 color.blue = b; 4249 color.blue = b;
@@ -4271,7 +4265,6 @@ lookup_rgb_color (struct frame *f, int r, int g, int b)
4271 return FRAME_FOREGROUND_PIXEL (f); 4265 return FRAME_FOREGROUND_PIXEL (f);
4272 4266
4273#else 4267#else
4274 COLORREF color;
4275#ifdef HAVE_NTGUI 4268#ifdef HAVE_NTGUI
4276 color = PALETTERGB (r, g, b); 4269 color = PALETTERGB (r, g, b);
4277#else 4270#else
@@ -4312,6 +4305,9 @@ lookup_pixel_color (struct frame *f, unsigned long pixel)
4312 Colormap cmap; 4305 Colormap cmap;
4313 int rc; 4306 int rc;
4314 4307
4308 if (ct_colors_allocated_max <= ct_colors_allocated)
4309 return FRAME_FOREGROUND_PIXEL (f);
4310
4315#ifdef HAVE_X_WINDOWS 4311#ifdef HAVE_X_WINDOWS
4316 cmap = FRAME_X_COLORMAP (f); 4312 cmap = FRAME_X_COLORMAP (f);
4317 color.pixel = pixel; 4313 color.pixel = pixel;
@@ -4450,7 +4446,9 @@ x_to_xcolors (struct frame *f, struct image *img, int rgb_p)
4450 HGDIOBJ prev; 4446 HGDIOBJ prev;
4451#endif /* HAVE_NTGUI */ 4447#endif /* HAVE_NTGUI */
4452 4448
4453 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors); 4449 if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width < img->height)
4450 memory_full (SIZE_MAX);
4451 colors = (XColor *) xmalloc (sizeof *colors * img->width * img->height);
4454 4452
4455#ifndef HAVE_NTGUI 4453#ifndef HAVE_NTGUI
4456 /* Get the X image IMG->pixmap. */ 4454 /* Get the X image IMG->pixmap. */
@@ -4602,7 +4600,9 @@ x_detect_edges (struct frame *f, struct image *img, int *matrix, int color_adjus
4602 4600
4603#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X)) 4601#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4604 4602
4605 new = (XColor *) xmalloc (img->width * img->height * sizeof *new); 4603 if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width < img->height)
4604 memory_full (SIZE_MAX);
4605 new = (XColor *) xmalloc (sizeof *new * img->width * img->height);
4606 4606
4607 for (y = 0; y < img->height; ++y) 4607 for (y = 0; y < img->height; ++y)
4608 { 4608 {
@@ -5880,7 +5880,7 @@ png_load (struct frame *f, struct image *img)
5880 5880
5881 for (x = 0; x < width; ++x) 5881 for (x = 0; x < width; ++x)
5882 { 5882 {
5883 unsigned r, g, b; 5883 int r, g, b;
5884 5884
5885 r = *p++ << 8; 5885 r = *p++ << 8;
5886 g = *p++ << 8; 5886 g = *p++ << 8;
@@ -6745,17 +6745,29 @@ tiff_size_of_memory (thandle_t data)
6745} 6745}
6746 6746
6747 6747
6748static void tiff_handler (const char *, const char *, const char *, va_list)
6749 ATTRIBUTE_FORMAT_PRINTF (3, 0);
6750static void
6751tiff_handler (const char *log_format, const char *title,
6752 const char *format, va_list ap)
6753{
6754 /* doprnt is not suitable here, as TIFF handlers are called from
6755 libtiff and are passed arbitrary printf directives. Instead, use
6756 vsnprintf, taking care to be portable to nonstandard environments
6757 where vsnprintf returns -1 on buffer overflow. Since it's just a
6758 log entry, it's OK to truncate it. */
6759 char buf[4000];
6760 int len = vsnprintf (buf, sizeof buf, format, ap);
6761 add_to_log (log_format, build_string (title),
6762 make_string (buf, max (0, min (len, sizeof buf - 1))));
6763}
6764
6748static void tiff_error_handler (const char *, const char *, va_list) 6765static void tiff_error_handler (const char *, const char *, va_list)
6749 ATTRIBUTE_FORMAT_PRINTF (2, 0); 6766 ATTRIBUTE_FORMAT_PRINTF (2, 0);
6750static void 6767static void
6751tiff_error_handler (const char *title, const char *format, va_list ap) 6768tiff_error_handler (const char *title, const char *format, va_list ap)
6752{ 6769{
6753 char buf[512]; 6770 tiff_handler ("TIFF error: %s %s", title, format, ap);
6754 int len;
6755
6756 len = sprintf (buf, "TIFF error: %s ", title);
6757 vsprintf (buf + len, format, ap);
6758 add_to_log (buf, Qnil, Qnil);
6759} 6771}
6760 6772
6761 6773
@@ -6764,12 +6776,7 @@ static void tiff_warning_handler (const char *, const char *, va_list)
6764static void 6776static void
6765tiff_warning_handler (const char *title, const char *format, va_list ap) 6777tiff_warning_handler (const char *title, const char *format, va_list ap)
6766{ 6778{
6767 char buf[512]; 6779 tiff_handler ("TIFF warning: %s %s", title, format, ap);
6768 int len;
6769
6770 len = sprintf (buf, "TIFF warning: %s ", title);
6771 vsprintf (buf + len, format, ap);
6772 add_to_log (buf, Qnil, Qnil);
6773} 6780}
6774 6781
6775 6782
@@ -6845,8 +6852,9 @@ tiff_load (struct frame *f, struct image *img)
6845 image = image_spec_value (img->spec, QCindex, NULL); 6852 image = image_spec_value (img->spec, QCindex, NULL);
6846 if (INTEGERP (image)) 6853 if (INTEGERP (image))
6847 { 6854 {
6848 int ino = XFASTINT (image); 6855 EMACS_INT ino = XFASTINT (image);
6849 if (!fn_TIFFSetDirectory (tiff, ino)) 6856 if (! (TYPE_MINIMUM (tdir_t) <= ino && ino <= TYPE_MAXIMUM (tdir_t)
6857 && fn_TIFFSetDirectory (tiff, ino)))
6850 { 6858 {
6851 image_error ("Invalid image number `%s' in image `%s'", 6859 image_error ("Invalid image number `%s' in image `%s'",
6852 image, img->spec); 6860 image, img->spec);
@@ -7144,7 +7152,7 @@ gif_load (struct frame *f, struct image *img)
7144 Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL); 7152 Lisp_Object specified_file = image_spec_value (img->spec, QCfile, NULL);
7145 Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL); 7153 Lisp_Object specified_data = image_spec_value (img->spec, QCdata, NULL);
7146 unsigned long bgcolor = 0; 7154 unsigned long bgcolor = 0;
7147 int idx; 7155 EMACS_INT idx;
7148 7156
7149 if (NILP (specified_data)) 7157 if (NILP (specified_data))
7150 { 7158 {
@@ -7379,7 +7387,7 @@ gif_load (struct frame *f, struct image *img)
7379 img->lisp_data = Qnil; 7387 img->lisp_data = Qnil;
7380 if (gif->SavedImages[idx].ExtensionBlockCount > 0) 7388 if (gif->SavedImages[idx].ExtensionBlockCount > 0)
7381 { 7389 {
7382 unsigned int delay = 0; 7390 int delay = 0;
7383 ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks; 7391 ExtensionBlock *ext = gif->SavedImages[idx].ExtensionBlocks;
7384 for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++) 7392 for (i = 0; i < gif->SavedImages[idx].ExtensionBlockCount; i++, ext++)
7385 /* Append (... FUNCTION "BYTES") */ 7393 /* Append (... FUNCTION "BYTES") */
@@ -7400,7 +7408,7 @@ gif_load (struct frame *f, struct image *img)
7400 if (delay) 7408 if (delay)
7401 img->lisp_data 7409 img->lisp_data
7402 = Fcons (Qdelay, 7410 = Fcons (Qdelay,
7403 Fcons (make_float (((double) delay) * 0.01), 7411 Fcons (make_float (delay / 100.0),
7404 img->lisp_data)); 7412 img->lisp_data));
7405 } 7413 }
7406 7414
@@ -7576,10 +7584,10 @@ imagemagick_load_image (struct frame *f, struct image *img,
7576 Lisp_Object image; 7584 Lisp_Object image;
7577 Lisp_Object value; 7585 Lisp_Object value;
7578 Lisp_Object crop; 7586 Lisp_Object crop;
7579 long ino; 7587 EMACS_INT ino;
7580 int desired_width, desired_height; 7588 int desired_width, desired_height;
7581 double rotation; 7589 double rotation;
7582 int imagemagick_rendermethod; 7590 EMACS_INT imagemagick_rendermethod;
7583 int pixelwidth; 7591 int pixelwidth;
7584 ImageInfo *image_info; 7592 ImageInfo *image_info;
7585 ExceptionInfo *exception; 7593 ExceptionInfo *exception;
@@ -7606,7 +7614,7 @@ imagemagick_load_image (struct frame *f, struct image *img,
7606 status = MagickPingImageBlob (ping_wand, contents, size); 7614 status = MagickPingImageBlob (ping_wand, contents, size);
7607 } 7615 }
7608 7616
7609 if (ino >= MagickGetNumberImages (ping_wand)) 7617 if (! (0 <= ino && ino < MagickGetNumberImages (ping_wand)))
7610 { 7618 {
7611 image_error ("Invalid image number `%s' in image `%s'", 7619 image_error ("Invalid image number `%s' in image `%s'",
7612 image, img->spec); 7620 image, img->spec);
@@ -7681,28 +7689,28 @@ imagemagick_load_image (struct frame *f, struct image *img,
7681 efficient. */ 7689 efficient. */
7682 crop = image_spec_value (img->spec, QCcrop, NULL); 7690 crop = image_spec_value (img->spec, QCcrop, NULL);
7683 7691
7684 if (CONSP (crop) && INTEGERP (XCAR (crop))) 7692 if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
7685 { 7693 {
7686 /* After some testing, it seems MagickCropImage is the fastest crop 7694 /* After some testing, it seems MagickCropImage is the fastest crop
7687 function in ImageMagick. This crop function seems to do less copying 7695 function in ImageMagick. This crop function seems to do less copying
7688 than the alternatives, but it still reads the entire image into memory 7696 than the alternatives, but it still reads the entire image into memory
7689 before croping, which is aparently difficult to avoid when using 7697 before cropping, which is apparently difficult to avoid when using
7690 imagemagick. */ 7698 imagemagick. */
7691 int w, h; 7699 size_t crop_width = XINT (XCAR (crop));
7692 w = XFASTINT (XCAR (crop));
7693 crop = XCDR (crop); 7700 crop = XCDR (crop);
7694 if (CONSP (crop) && INTEGERP (XCAR (crop))) 7701 if (CONSP (crop) && TYPE_RANGED_INTEGERP (size_t, XCAR (crop)))
7695 { 7702 {
7696 h = XFASTINT (XCAR (crop)); 7703 size_t crop_height = XINT (XCAR (crop));
7697 crop = XCDR (crop); 7704 crop = XCDR (crop);
7698 if (CONSP (crop) && INTEGERP (XCAR (crop))) 7705 if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
7699 { 7706 {
7700 x = XFASTINT (XCAR (crop)); 7707 ssize_t crop_x = XINT (XCAR (crop));
7701 crop = XCDR (crop); 7708 crop = XCDR (crop);
7702 if (CONSP (crop) && INTEGERP (XCAR (crop))) 7709 if (CONSP (crop) && TYPE_RANGED_INTEGERP (ssize_t, XCAR (crop)))
7703 { 7710 {
7704 y = XFASTINT (XCAR (crop)); 7711 ssize_t crop_y = XINT (XCAR (crop));
7705 MagickCropImage (image_wand, w, h, x, y); 7712 MagickCropImage (image_wand, crop_width, crop_height,
7713 crop_x, crop_y);
7706 } 7714 }
7707 } 7715 }
7708 } 7716 }
@@ -7748,9 +7756,11 @@ imagemagick_load_image (struct frame *f, struct image *img,
7748 7756
7749 init_color_table (); 7757 init_color_table ();
7750 imagemagick_rendermethod = (INTEGERP (Vimagemagick_render_type) 7758 imagemagick_rendermethod = (INTEGERP (Vimagemagick_render_type)
7751 ? XFASTINT (Vimagemagick_render_type) : 0); 7759 ? XINT (Vimagemagick_render_type) : 0);
7752 if (imagemagick_rendermethod == 0) 7760 if (imagemagick_rendermethod == 0)
7753 { 7761 {
7762 size_t image_height;
7763
7754 /* Try to create a x pixmap to hold the imagemagick pixmap. */ 7764 /* Try to create a x pixmap to hold the imagemagick pixmap. */
7755 if (!x_create_x_image_and_pixmap (f, width, height, 0, 7765 if (!x_create_x_image_and_pixmap (f, width, height, 0,
7756 &ximg, &img->pixmap)) 7766 &ximg, &img->pixmap))
@@ -7779,7 +7789,8 @@ imagemagick_load_image (struct frame *f, struct image *img,
7779 goto imagemagick_error; 7789 goto imagemagick_error;
7780 } 7790 }
7781 7791
7782 for (y = 0; y < (long) MagickGetImageHeight (image_wand); y++) 7792 image_height = MagickGetImageHeight (image_wand);
7793 for (y = 0; y < image_height; y++)
7783 { 7794 {
7784 pixels = PixelGetNextIteratorRow (iterator, &width); 7795 pixels = PixelGetNextIteratorRow (iterator, &width);
7785 if (pixels == (PixelWand **) NULL) 7796 if (pixels == (PixelWand **) NULL)
@@ -8285,10 +8296,10 @@ svg_load_image (struct frame *f, /* Pointer to emacs frame structure. *
8285 { 8296 {
8286 for (x = 0; x < width; ++x) 8297 for (x = 0; x < width; ++x)
8287 { 8298 {
8288 unsigned red; 8299 int red;
8289 unsigned green; 8300 int green;
8290 unsigned blue; 8301 int blue;
8291 unsigned opacity; 8302 int opacity;
8292 8303
8293 red = *pixels++; 8304 red = *pixels++;
8294 green = *pixels++; 8305 green = *pixels++;
@@ -8469,7 +8480,8 @@ gs_image_p (Lisp_Object object)
8469static int 8480static int
8470gs_load (struct frame *f, struct image *img) 8481gs_load (struct frame *f, struct image *img)
8471{ 8482{
8472 char buffer[100]; 8483 uprintmax_t printnum1, printnum2;
8484 char buffer[sizeof " " + INT_STRLEN_BOUND (printmax_t)];
8473 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width; 8485 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
8474 Lisp_Object frame; 8486 Lisp_Object frame;
8475 double in_width, in_height; 8487 double in_width, in_height;
@@ -8481,16 +8493,19 @@ gs_load (struct frame *f, struct image *img)
8481 info. */ 8493 info. */
8482 pt_width = image_spec_value (img->spec, QCpt_width, NULL); 8494 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
8483 in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0; 8495 in_width = INTEGERP (pt_width) ? XFASTINT (pt_width) / 72.0 : 0;
8484 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx; 8496 in_width *= FRAME_X_DISPLAY_INFO (f)->resx;
8485 pt_height = image_spec_value (img->spec, QCpt_height, NULL); 8497 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
8486 in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0; 8498 in_height = INTEGERP (pt_height) ? XFASTINT (pt_height) / 72.0 : 0;
8487 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy; 8499 in_height *= FRAME_X_DISPLAY_INFO (f)->resy;
8488 8500
8489 if (!check_image_size (f, img->width, img->height)) 8501 if (! (in_width <= INT_MAX && in_height <= INT_MAX
8502 && check_image_size (f, in_width, in_height)))
8490 { 8503 {
8491 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil); 8504 image_error ("Invalid image size (see `max-image-size')", Qnil, Qnil);
8492 return 0; 8505 return 0;
8493 } 8506 }
8507 img->width = in_width;
8508 img->height = in_height;
8494 8509
8495 /* Create the pixmap. */ 8510 /* Create the pixmap. */
8496 xassert (img->pixmap == NO_PIXMAP); 8511 xassert (img->pixmap == NO_PIXMAP);
@@ -8515,14 +8530,14 @@ gs_load (struct frame *f, struct image *img)
8515 if successful. We do not record_unwind_protect here because 8530 if successful. We do not record_unwind_protect here because
8516 other places in redisplay like calling window scroll functions 8531 other places in redisplay like calling window scroll functions
8517 don't either. Let the Lisp loader use `unwind-protect' instead. */ 8532 don't either. Let the Lisp loader use `unwind-protect' instead. */
8518 sprintf (buffer, "%lu %lu", 8533 printnum1 = FRAME_X_WINDOW (f);
8519 (unsigned long) FRAME_X_WINDOW (f), 8534 printnum2 = img->pixmap;
8520 (unsigned long) img->pixmap); 8535 sprintf (buffer, "%"pMu" %"pMu, printnum1, printnum2);
8521 window_and_pixmap_id = build_string (buffer); 8536 window_and_pixmap_id = build_string (buffer);
8522 8537
8523 sprintf (buffer, "%lu %lu", 8538 printnum1 = FRAME_FOREGROUND_PIXEL (f);
8524 FRAME_FOREGROUND_PIXEL (f), 8539 printnum2 = FRAME_BACKGROUND_PIXEL (f);
8525 FRAME_BACKGROUND_PIXEL (f)); 8540 sprintf (buffer, "%"pMu" %"pMu, printnum1, printnum2);
8526 pixel_colors = build_string (buffer); 8541 pixel_colors = build_string (buffer);
8527 8542
8528 XSETFRAME (frame, f); 8543 XSETFRAME (frame, f);
@@ -8547,7 +8562,8 @@ void
8547x_kill_gs_process (Pixmap pixmap, struct frame *f) 8562x_kill_gs_process (Pixmap pixmap, struct frame *f)
8548{ 8563{
8549 struct image_cache *c = FRAME_IMAGE_CACHE (f); 8564 struct image_cache *c = FRAME_IMAGE_CACHE (f);
8550 int class, i; 8565 int class;
8566 ptrdiff_t i;
8551 struct image *img; 8567 struct image *img;
8552 8568
8553 /* Find the image containing PIXMAP. */ 8569 /* Find the image containing PIXMAP. */
@@ -8651,7 +8667,7 @@ DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0,
8651DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "") 8667DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
8652 (Lisp_Object spec) 8668 (Lisp_Object spec)
8653{ 8669{
8654 int id = -1; 8670 ptrdiff_t id = -1;
8655 8671
8656 if (valid_image_p (spec)) 8672 if (valid_image_p (spec))
8657 id = lookup_image (SELECTED_FRAME (), spec); 8673 id = lookup_image (SELECTED_FRAME (), spec);