File: heifsave-improve_alpha_channel_detection.patch

package info (click to toggle)
vips 8.16.1-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 51,940 kB
  • sloc: ansic: 169,179; cpp: 11,890; python: 4,620; xml: 4,353; sh: 732; perl: 40; makefile: 19
file content (82 lines) | stat: -rw-r--r-- 3,016 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
From 8effeb9120ac7e9214973c60d44b98d775749b39 Mon Sep 17 00:00:00 2001
From: Lovell Fuller <lovell@users.noreply.github.com>
Date: Tue, 11 Mar 2025 20:52:59 +0000
Subject: [PATCH] heifsave: improve alpha channel detection (#4394)

---
 ChangeLog                  |  1 +
 libvips/foreign/heifsave.c | 19 +++++--------------
 2 files changed, 6 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d46e377e0..8d81352d1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -27,6 +27,7 @@
 - tiffload: add missing read loop [kleisauke]
 - prevent possible use-after-free when debugging via `--vips-leak` flag [lovell]
 - avoid possible overflow when multiplication result is cast up [lovell]
+- heifsave: improve alpha channel detection [lovell]
 
 10/10/24 8.16.0
 
diff --git a/libvips/foreign/heifsave.c b/libvips/foreign/heifsave.c
index 7479943ef..27cd7fb9a 100644
--- a/libvips/foreign/heifsave.c
+++ b/libvips/foreign/heifsave.c
@@ -289,8 +289,7 @@ vips_foreign_save_heif_write_page(VipsFo
 #endif /*HAVE_HEIF_COLOR_PROFILE*/
 
 	options = heif_encoding_options_alloc();
-	if (vips_image_hasalpha(save->ready))
-		options->save_alpha_channel = 1;
+	options->save_alpha_channel = save->ready->Bands > 3;
 
 #ifdef HAVE_HEIF_ENCODING_OPTIONS_OUTPUT_NCLX_PROFILE
 	/* Matrix coefficients have to be identity (CICP x/y/0) in lossless
@@ -522,6 +521,7 @@ vips_foreign_save_heif_build(VipsObject
 	char *chroma;
 	const struct heif_encoder_descriptor *out_encoder;
 	const struct heif_encoder_parameter *const *param;
+	gboolean has_alpha;
 
 	if (VIPS_OBJECT_CLASS(vips_foreign_save_heif_parent_class)->
 		build(object))
@@ -683,21 +683,13 @@ vips_foreign_save_heif_build(VipsObject *object)
 	heif->page_width = save->ready->Xsize;
 	heif->page_height = vips_image_get_page_height(save->ready);
 	heif->n_pages = save->ready->Ysize / heif->page_height;
+	has_alpha = save->ready->Bands > 3;
 
 	if (heif->page_width > 16384 || heif->page_height > 16384) {
 		vips_error("heifsave", _("image too large"));
 		return -1;
 	}
 
-	/* Reject multiband images.
-	 */
-	if (save->ready->Type == VIPS_INTERPRETATION_MULTIBAND) {
-		vips_error("heifsave", _("Unsupported interpretation: %s"),
-			vips_enum_nick(VIPS_TYPE_INTERPRETATION,
-				save->ready->Type));
-		return -1;
-	}
-
 	/* Make a heif image the size of a page. We send sink_disc() output
 	 * here and write a frame each time it fills.
 	 */
@@ -705,12 +697,11 @@ vips_foreign_save_heif_build(VipsObject *object)
 	printf("vips_foreign_save_heif_build:\n");
 	printf("\twidth = %d\n", heif->page_width);
 	printf("\theight = %d\n", heif->page_height);
-	printf("\talpha = %d\n", vips_image_hasalpha(save->ready));
+	printf("\talpha = %d\n", has_alpha);
 #endif /*DEBUG*/
 	error = heif_image_create(heif->page_width, heif->page_height,
 		heif_colorspace_RGB,
-		vips__heif_chroma(heif->bitdepth,
-			vips_image_hasalpha(save->ready)),
+		vips__heif_chroma(heif->bitdepth, has_alpha),
 		&heif->img);
 	if (error.code) {
 		vips__heif_error(&error);