>From b21f001a21fa3f5ea6f6ffb0a2c4a753faf420b4 Mon Sep 17 00:00:00 2001
From: Alan Third
Date: Tue, 14 Apr 2020 22:56:06 +0100
Subject: [PATCH] Use native image API for NS
* configure.ac (NATIVE_IMAGE_API): Move above NS definitions.
(HAVE_NATIVE_IMAGE_API): Set for NS.
(HAVE_PNG, HAVE_JPEG, HAVE_GIF, HAVE_TIFF): Enable on NS builds.
* src/image.c (HAVE_NS): Fix a number of #if's so they no longer rely
on HAVE_NS.
(PIX_MASK_DRAW): Add for HAVE_NS so libpng support will compile.
(image_can_use_native_api):
(native_image_load): Add NS support.
(png_load):
(jpeg_load):
(tiff_load):
(gif_load): Remove NS specific definitions.
* src/nsimage.m (ns_can_use_native_image_api): New function.
* src/nsterm.h: (ns_can_use_native_image_api): New function.
---
configure.ac | 25 +++++++++------
src/image.c | 89 +++++++++++++++------------------------------------
src/nsimage.m | 49 ++++++++++++++++++++++++++++
src/nsterm.h | 1 +
4 files changed, 91 insertions(+), 73 deletions(-)
diff --git a/configure.ac b/configure.ac
index b0a2cc466b..cdb8378abe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1910,6 +1910,8 @@ AC_DEFUN
bitmapdir=${bmd_acc#:}
fi
+NATIVE_IMAGE_API=no
+
test "${with_ns}" = maybe && test "${opsys}" != darwin && with_ns=no
HAVE_NS=no
NS_GNUSTEP_CONFIG=no
@@ -2021,6 +2023,11 @@ AC_DEFUN
AC_MSG_ERROR([Mac OS X 10.6 or newer is required]);
fi
fi
+
+ if test "${with_native_image_api}" = yes; then
+ AC_DEFINE(HAVE_NATIVE_IMAGE_API, 1, [Define to use native OS APIs for images.])
+ NATIVE_IMAGE_API="yes (ns)"
+ fi
fi
AC_SUBST(LIBS_GNUSTEP)
@@ -2127,7 +2134,6 @@ AC_DEFUN
NTLIB=
CM_OBJ="cm.o"
XARGS_LIMIT=
-NATIVE_IMAGE_API=no
if test "${HAVE_W32}" = "yes"; then
AC_DEFINE(HAVE_NTGUI, 1, [Define to use native MS Windows GUI.])
if test "$with_toolkit_scroll_bars" = "no"; then
@@ -3575,9 +3581,8 @@ AC_DEFUN
### Use -ljpeg if available, unless '--with-jpeg=no'.
HAVE_JPEG=no
LIBJPEG=
-if test "${NS_IMPL_COCOA}" = yes; then
- : # Cocoa provides its own jpeg support, so do nothing.
-elif test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes"; then
+if test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes" \
+ || test "${HAVE_NS}" = "yes"; then
if test "${with_jpeg}" != "no"; then
AC_CACHE_CHECK([for jpeglib 6b or later],
[emacs_cv_jpeglib],
@@ -3726,13 +3731,12 @@ AC_DEFUN
HAVE_PNG=no
LIBPNG=
PNG_CFLAGS=
-if test "${NS_IMPL_COCOA}" = yes; then
- : # Cocoa provides its own png support, so do nothing.
-elif test "${with_png}" != no; then
+if test "${with_png}" != no; then
# mingw32 loads the library dynamically.
if test "$opsys" = mingw32; then
AC_CHECK_HEADER([png.h], [HAVE_PNG=yes])
- elif test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes"; then
+ elif test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes" \
+ || test "${HAVE_NS}" = "yes"; then
EMACS_CHECK_MODULES([PNG], [libpng >= 1.0.0])
if test $HAVE_PNG = yes; then
LIBPNG=$PNG_LIBS
@@ -3806,7 +3810,8 @@ AC_DEFUN
if test "${HAVE_TIFF}" = "yes"; then
AC_DEFINE(HAVE_TIFF, 1, [Define to 1 if you have the tiff library (-ltiff).])
fi
-elif test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes"; then
+elif test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes" \
+ || test "${HAVE_NS}" = "yes"; then
if test "${with_tiff}" != "no"; then
AC_CHECK_HEADER(tiffio.h,
[tifflibs="-lz -lm"
@@ -3835,7 +3840,7 @@ AC_DEFUN
AC_DEFINE(HAVE_GIF, 1, [Define to 1 if you have a gif (or ungif) library.])
fi
elif test "${HAVE_X11}" = "yes" && test "${with_gif}" != "no" \
- || test "${HAVE_W32}" = "yes"; then
+ || test "${HAVE_W32}" = "yes" || test "${HAVE_NS}" = "yes"; then
AC_CHECK_HEADER(gif_lib.h,
# EGifPutExtensionLast only exists from version libungif-4.1.0b1.
# Earlier versions can crash Emacs, but version 5.0 removes EGifPutExtensionLast.
diff --git a/src/image.c b/src/image.c
index aacaf0b734..210c83a5fe 100644
--- a/src/image.c
+++ b/src/image.c
@@ -24,7 +24,7 @@ Copyright (C) 1989, 1992-2020 Free Software Foundation, Inc.
/* Include this before including to work around bugs with
older libpng; see Bug#17429. */
-#if defined HAVE_PNG && !defined HAVE_NS
+#if defined HAVE_PNG
# include
#endif
@@ -125,6 +125,7 @@ #define PUT_PIXEL XPutPixel
#define NO_PIXMAP 0
#define PIX_MASK_RETAIN 0
+#define PIX_MASK_DRAW 1
#endif /* HAVE_NS */
@@ -6242,6 +6243,8 @@ image_can_use_native_api (Lisp_Object type)
#if HAVE_NATIVE_IMAGE_API
# ifdef HAVE_NTGUI
return w32_can_use_native_image_api (type);
+# elif defined HAVE_NS
+ return ns_can_use_native_image_api (type);
# else
return false;
# endif
@@ -6315,6 +6318,10 @@ native_image_load (struct frame *f, struct image *img)
return w32_load_image (f, img,
image_spec_value (img->spec, QCfile, NULL),
image_spec_value (img->spec, QCdata, NULL));
+# elif defined HAVE_NS
+ return ns_load_image (f, img,
+ image_spec_value (img->spec, QCfile, NULL),
+ image_spec_value (img->spec, QCdata, NULL));
# else
return 0;
# endif
@@ -6327,7 +6334,7 @@ native_image_load (struct frame *f, struct image *img)
PNG
***********************************************************************/
-#if defined (HAVE_PNG) || defined (HAVE_NS)
+#if defined (HAVE_PNG)
/* Indices of image specification fields in png_format, below. */
@@ -6378,10 +6385,10 @@ png_image_p (Lisp_Object object)
return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
}
-#endif /* HAVE_PNG || HAVE_NS */
+#endif /* HAVE_PNG */
-#if defined HAVE_PNG && !defined HAVE_NS
+#ifdef HAVE_PNG
# ifdef WINDOWSNT
/* PNG library details. */
@@ -6971,17 +6978,7 @@ png_load (struct frame *f, struct image *img)
return png_load_body (f, img, &c);
}
-#elif defined HAVE_NS
-
-static bool
-png_load (struct frame *f, struct image *img)
-{
- return ns_load_image (f, img,
- image_spec_value (img->spec, QCfile, NULL),
- image_spec_value (img->spec, QCdata, NULL));
-}
-
-#endif /* HAVE_NS */
+#endif /* HAVE_PNG */
@@ -6989,7 +6986,7 @@ png_load (struct frame *f, struct image *img)
JPEG
***********************************************************************/
-#if defined (HAVE_JPEG) || defined (HAVE_NS)
+#if defined (HAVE_JPEG)
/* Indices of image specification fields in gs_format, below. */
@@ -7041,7 +7038,7 @@ jpeg_image_p (Lisp_Object object)
return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
}
-#endif /* HAVE_JPEG || HAVE_NS */
+#endif /* HAVE_JPEG */
#ifdef HAVE_JPEG
@@ -7543,18 +7540,6 @@ jpeg_load (struct frame *f, struct image *img)
return jpeg_load_body (f, img, &mgr);
}
-#else /* HAVE_JPEG */
-
-#ifdef HAVE_NS
-static bool
-jpeg_load (struct frame *f, struct image *img)
-{
- return ns_load_image (f, img,
- image_spec_value (img->spec, QCfile, NULL),
- image_spec_value (img->spec, QCdata, NULL));
-}
-#endif /* HAVE_NS */
-
#endif /* !HAVE_JPEG */
@@ -7563,7 +7548,7 @@ jpeg_load (struct frame *f, struct image *img)
TIFF
***********************************************************************/
-#if defined (HAVE_TIFF) || defined (HAVE_NS)
+#if defined (HAVE_TIFF)
/* Indices of image specification fields in tiff_format, below. */
@@ -7616,7 +7601,7 @@ tiff_image_p (Lisp_Object object)
return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
}
-#endif /* HAVE_TIFF || HAVE_NS */
+#endif /* HAVE_TIFF */
#ifdef HAVE_TIFF
@@ -7984,16 +7969,6 @@ tiff_load (struct frame *f, struct image *img)
return 1;
}
-#elif defined HAVE_NS
-
-static bool
-tiff_load (struct frame *f, struct image *img)
-{
- return ns_load_image (f, img,
- image_spec_value (img->spec, QCfile, NULL),
- image_spec_value (img->spec, QCdata, NULL));
-}
-
#endif
@@ -8002,7 +7977,7 @@ tiff_load (struct frame *f, struct image *img)
GIF
***********************************************************************/
-#if defined (HAVE_GIF) || defined (HAVE_NS)
+#if defined (HAVE_GIF)
/* Indices of image specification fields in gif_format, below. */
@@ -8064,7 +8039,7 @@ gif_image_p (Lisp_Object object)
return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
}
-#endif /* HAVE_GIF || HAVE_NS */
+#endif /* HAVE_GIF */
#ifdef HAVE_GIF
@@ -8581,18 +8556,6 @@ gif_load (struct frame *f, struct image *img)
return 1;
}
-#else /* !HAVE_GIF */
-
-#ifdef HAVE_NS
-static bool
-gif_load (struct frame *f, struct image *img)
-{
- return ns_load_image (f, img,
- image_spec_value (img->spec, QCfile, NULL),
- image_spec_value (img->spec, QCdata, NULL));
-}
-#endif /* HAVE_NS */
-
#endif /* HAVE_GIF */
@@ -10259,19 +10222,19 @@ initialize_image_type (struct image_type const *type)
{ SYMBOL_INDEX (Qsvg), svg_image_p, svg_load, image_clear_image,
IMAGE_TYPE_INIT (init_svg_functions) },
#endif
-#if defined HAVE_PNG || defined HAVE_NS
+#if defined HAVE_PNG
{ SYMBOL_INDEX (Qpng), png_image_p, png_load, image_clear_image,
IMAGE_TYPE_INIT (init_png_functions) },
#endif
-#if defined HAVE_GIF || defined HAVE_NS
+#if defined HAVE_GIF
{ SYMBOL_INDEX (Qgif), gif_image_p, gif_load, gif_clear_image,
IMAGE_TYPE_INIT (init_gif_functions) },
#endif
-#if defined HAVE_TIFF || defined HAVE_NS
+#if defined HAVE_TIFF
{ SYMBOL_INDEX (Qtiff), tiff_image_p, tiff_load, image_clear_image,
IMAGE_TYPE_INIT (init_tiff_functions) },
#endif
-#if defined HAVE_JPEG || defined HAVE_NS
+#if defined HAVE_JPEG
{ SYMBOL_INDEX (Qjpeg), jpeg_image_p, jpeg_load, image_clear_image,
IMAGE_TYPE_INIT (init_jpeg_functions) },
#endif
@@ -10421,22 +10384,22 @@ syms_of_image (void)
add_image_type (Qxpm);
#endif
-#if defined (HAVE_JPEG) || defined (HAVE_NS) || defined (HAVE_NATIVE_IMAGE_API)
+#if defined (HAVE_JPEG) || defined (HAVE_NATIVE_IMAGE_API)
DEFSYM (Qjpeg, "jpeg");
add_image_type (Qjpeg);
#endif
-#if defined (HAVE_TIFF) || defined (HAVE_NS) || defined (HAVE_NATIVE_IMAGE_API)
+#if defined (HAVE_TIFF) || defined (HAVE_NATIVE_IMAGE_API)
DEFSYM (Qtiff, "tiff");
add_image_type (Qtiff);
#endif
-#if defined (HAVE_GIF) || defined (HAVE_NS) || defined (HAVE_NATIVE_IMAGE_API)
+#if defined (HAVE_GIF) || defined (HAVE_NATIVE_IMAGE_API)
DEFSYM (Qgif, "gif");
add_image_type (Qgif);
#endif
-#if defined (HAVE_PNG) || defined (HAVE_NS) || defined(HAVE_NATIVE_IMAGE_API)
+#if defined (HAVE_PNG) || defined (HAVE_NATIVE_IMAGE_API)
DEFSYM (Qpng, "png");
add_image_type (Qpng);
#endif
diff --git a/src/nsimage.m b/src/nsimage.m
index 3cccc984ca..07750de95f 100644
--- a/src/nsimage.m
+++ b/src/nsimage.m
@@ -45,6 +45,55 @@ Updated by Christian Limpach (address@hidden)
========================================================================== */
+bool
+ns_can_use_native_image_api (Lisp_Object type)
+{
+ NSString *imageType = @"unknown";
+ NSArray *types;
+
+ NSTRACE ("ns_can_use_native_image_api");
+
+ if (EQ (type, Qnative_image))
+ return YES;
+
+#ifdef NS_IMPL_COCOA
+ /* Work out the UTI of the image type. */
+ if (EQ (type, Qjpeg))
+ imageType = @"public.jpeg";
+ else if (EQ (type, Qpng))
+ imageType = @"public.png";
+ else if (EQ (type, Qgif))
+ imageType = @"com.compuserve.gif";
+ else if (EQ (type, Qtiff))
+ imageType = @"public.tiff";
+ else if (EQ (type, Qsvg))
+ imageType = @"public.svg-image";
+
+ /* NSImage also supports a host of other types such as PDF and BMP,
+ but we don't yet support these in image.c. */
+
+ types = [NSImage imageTypes];
+#else
+ /* Work out the image type. */
+ if (EQ (type, Qjpeg))
+ imageType = @"jpeg";
+ else if (EQ (type, Qpng))
+ imageType = @"png";
+ else if (EQ (type, Qgif))
+ imageType = @"gif";
+ else if (EQ (type, Qtiff))
+ imageType = @"tiff";
+
+ types = [NSImage imageFileTypes];
+#endif
+
+ /* Check if the type is supported on this system. */
+ if ([types indexOfObject:imageType] != NSNotFound)
+ return YES;
+ else
+ return NO;
+}
+
void *
ns_image_from_XBM (char *bits, int width, int height,
unsigned long fg, unsigned long bg)
diff --git a/src/nsterm.h b/src/nsterm.h
index f5d3c32b8b..8d5371c8f2 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -1189,6 +1189,7 @@ #define NSAPP_DATA2_RUNFILEDIALOG 11
/* From nsimage.m, needed in image.c */
struct image;
+extern bool ns_can_use_native_image_api (Lisp_Object type);
extern void *ns_image_from_XBM (char *bits, int width, int height,
unsigned long fg, unsigned long bg);
extern void *ns_image_for_XPM (int width, int height, int depth);
--
2.26.1