Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addalpha has different behaviour compared to C #457

Closed
RiskoZoSlovenska opened this issue Mar 7, 2024 · 1 comment
Closed

addalpha has different behaviour compared to C #457

RiskoZoSlovenska opened this issue Mar 7, 2024 · 1 comment

Comments

@RiskoZoSlovenska
Copy link

The following Python code:

import pyvips

img = pyvips.Image.black(1, 1).colourspace(pyvips.enums.Interpretation.SCRGB).addalpha()

print(".".join(str(pyvips.base.version(flag)) for flag in range(3)))
print(img.interpretation)
print(img.avg())

produces

8.15.1
scrgb
63.75

whereas the (hopefully) equivalent C code:

Click to open

(this is my first time calling libvips from C — if I'm doing something wrong, please let me know!)

#include <stdio.h>
#include <vips/vips.h>

int main(int argc, char **argv) {
	if (VIPS_INIT(argv[0]))
		vips_error_exit(NULL);

	VipsImage *raw;
	if (vips_black(&raw, 1, 1, NULL))
		vips_error_exit(NULL);

	VipsImage *scrgb;
	if (vips_colourspace(raw, &scrgb, VIPS_INTERPRETATION_scRGB, NULL))
		vips_error_exit(NULL);

	g_object_unref(raw);

	VipsImage *with_alpha;
	if (vips_addalpha(scrgb, &with_alpha, NULL))
		vips_error_exit(NULL);

	g_object_unref(scrgb);

	double mean;
	if (vips_avg(with_alpha, &mean, NULL))
		vips_error_exit(NULL);

	printf("%s\n", VIPS_VERSION_STRING);
	printf("%d\n", vips_image_get_interpretation(with_alpha) == VIPS_INTERPRETATION_scRGB);
	printf("%f\n", mean);

	g_object_unref(with_alpha);

	return 0;
}

produces

8.15.1
1
0.250000

This happens because pyvips re-implements the addalpha function, and this (re-)implementation wasn't updated when the upstream changed.

I'm opening this as an issue instead of directly PRing a fix because it may be worthwhile to consider using the libvips API directly so that issues like this can be avoided in the future.

@jcupitt
Copy link
Member

jcupitt commented Mar 7, 2024

Hello again,

I think the best solutions would be to either make vips_addalpha a true operation (and so skip the wrapping), or to fix the python implementation of this utility function. Calling into the C wrapper from the python wrapper would be fiddly and error-prone.

I pushed 3c92762 and credited you.

@jcupitt jcupitt closed this as completed Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants