From d66c1cae45915be22e41af34f45af4051986cec3 Mon Sep 17 00:00:00 2001
From: Lovell Fuller <github@lovell.info>
Date: Wed, 9 Jan 2019 20:21:45 +0000
Subject: [PATCH] PNG: ensure XMP metadata is ignored when strip=TRUE

---
 libvips/foreign/vipspng.c | 92 ++++++++++++++++++++-------------------
 1 file changed, 47 insertions(+), 45 deletions(-)

diff --git a/libvips/foreign/vipspng.c b/libvips/foreign/vipspng.c
index 5d980425..599bd7b3 100644
--- a/libvips/foreign/vipspng.c
+++ b/libvips/foreign/vipspng.c
@@ -1067,71 +1067,73 @@ write_vips( Write *write,
 		VIPS_RINT( in->Xres * 1000 ), VIPS_RINT( in->Yres * 1000 ), 
 		PNG_RESOLUTION_METER );
 
-	/* Set ICC Profile.
+	/* Metadata
 	 */
-	if( profile && 
-		!strip ) {
-		if( strcmp( profile, "none" ) != 0 ) { 
+	if( !strip ) {
+		/* Set ICC Profile.
+		 */
+		if( profile ) {
+			if( strcmp( profile, "none" ) != 0 ) {
+				void *data;
+				size_t length;
+
+				if( !(data = vips__file_read_name( profile,
+					vips__icc_dir(), &length )) )
+					return( -1 );
+
+#ifdef DEBUG
+				printf( "write_vips: "
+					"attaching %zd bytes of ICC profile\n",
+					length );
+#endif /*DEBUG*/
+
+				png_set_iCCP( write->pPng, write->pInfo, "icc",
+					PNG_COMPRESSION_TYPE_BASE, data, length );
+			}
+		}
+		else if( vips_image_get_typeof( in, VIPS_META_ICC_NAME ) ) {
 			void *data;
 			size_t length;
 
-			if( !(data = vips__file_read_name( profile, 
-				vips__icc_dir(), &length )) ) 
+			if( vips_image_get_blob( in, VIPS_META_ICC_NAME,
+				&data, &length ) )
 				return( -1 );
 
 #ifdef DEBUG
-			printf( "write_vips: "
-				"attaching %zd bytes of ICC profile\n",
+			printf( "write_vips: attaching %zd bytes of ICC profile\n",
 				length );
 #endif /*DEBUG*/
 
-			png_set_iCCP( write->pPng, write->pInfo, "icc", 
+			png_set_iCCP( write->pPng, write->pInfo, "icc",
 				PNG_COMPRESSION_TYPE_BASE, data, length );
 		}
-	}
-	else if( vips_image_get_typeof( in, VIPS_META_ICC_NAME ) &&
-		!strip ) {
-		void *data;
-		size_t length;
 
-		if( vips_image_get_blob( in, VIPS_META_ICC_NAME, 
-			&data, &length ) ) 
-			return( -1 ); 
+		if( vips_image_get_typeof( in, VIPS_META_XMP_NAME ) ) {
+			void *data;
+			size_t length;
+			char *str;
 
-#ifdef DEBUG
-		printf( "write_vips: attaching %zd bytes of ICC profile\n",
-			length );
-#endif /*DEBUG*/
+			/* XMP is attached as a BLOB with no null-termination. We
+			 * must re-add this.
+			 */
+			if( vips_image_get_blob( in,
+				VIPS_META_XMP_NAME, &data, &length ) )
+				return( -1 );
 
-		png_set_iCCP( write->pPng, write->pInfo, "icc", 
-			PNG_COMPRESSION_TYPE_BASE, data, length );
-	}
+			str = g_malloc( length + 1 );
+			vips_strncpy( str, data, length + 1 );
+			vips__png_set_text( write->pPng, write->pInfo,
+				"XML:com.adobe.xmp", str );
+			g_free( str );
+		}
 
-	if( vips_image_get_typeof( in, VIPS_META_XMP_NAME ) ) {
-		void *data;
-		size_t length;
-		char *str;
-
-		/* XMP is attached as a BLOB with no null-termination. We
-		 * must re-add this.
+		/* Set any "png-comment-xx-yyy" metadata items.
 		 */
-		if( vips_image_get_blob( in, 
-			VIPS_META_XMP_NAME, &data, &length ) )
+		if( vips_image_map( in,
+			write_png_comment, write ) )
 			return( -1 );
-
-		str = g_malloc( length + 1 );
-		vips_strncpy( str, data, length + 1 );
-		vips__png_set_text( write->pPng, write->pInfo, 
-			"XML:com.adobe.xmp", str );
-		g_free( str );
 	}
 
-	/* Set any "png-comment-xx-yyy" metadata items.
-	 */
-	if( vips_image_map( in, 
-		write_png_comment, write ) )
-		return( -1 );
-
 #ifdef HAVE_IMAGEQUANT
 	if( palette ) {
 		VipsImage *im_index;