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

Enhancement: add support to recomb for 4x4 recombination matrices #4145

Closed
ton11797 opened this issue Jul 2, 2024 · 2 comments
Closed

Enhancement: add support to recomb for 4x4 recombination matrices #4145

ton11797 opened this issue Jul 2, 2024 · 2 comments

Comments

@ton11797
Copy link
Contributor

ton11797 commented Jul 2, 2024

Question about an existing feature

What are you trying to achieve?

I want to multiply the Alpha channel with an opacity value. This is what I came up with in my code

    async multiplyAlphaChannel(layerImage: Sharp, multiply: number) {
        const layerMetadata = await layerImage.metadata();
        const channels: Array<{ data: Buffer }> = await Promise.all([
            layerImage.extractChannel(0).raw().toBuffer({ resolveWithObject: true }),
            layerImage.extractChannel(1).raw().toBuffer({ resolveWithObject: true }),
            layerImage.extractChannel(2).raw().toBuffer({ resolveWithObject: true }),
            layerImage.extractChannel(3).raw().toBuffer({ resolveWithObject: true })
        ]);

        const pixelArray = new Uint8ClampedArray(channels[0].data.length * 4);

        for (let i = 0; i < channels[0].data.length; i++) {
            pixelArray[i * 4] = channels[0].data[i]; // Red
            pixelArray[i * 4 + 1] = channels[1].data[i]; // Green
            pixelArray[i * 4 + 2] = channels[2].data[i]; // Blue
            pixelArray[i * 4 + 3] = channels[3].data[i] * multiply; // Alpha
        }

        return await sharp(pixelArray, {
            raw: {
                width: layerMetadata.width,
                height: layerMetadata.height,
                channels: 4
            }
        }).png();
    }

Question

Are there any more efficient or simplified methods to achieve this using the Sharp library?

@lovell
Copy link
Owner

lovell commented Jul 2, 2024

This is a scenario that matrix multiplication via the recomb operation should allow but currently it only supports a 3x3 matrix for non-alpha channels.

Let's tag this as an enhancement for 4x4 matrix support.

// Proposed API to support 4x4 matrix - NOT YET AVAILABLE
const alphaMultiplication = 1.1;
const output = await sharp(input)
  .recomb([
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, alphaMultiplication
  ])
  .toBuffer();

@lovell lovell changed the title Multiply Alpha Channel Enhancement: add support to recomb for 4x4 recombination matrices Jul 2, 2024
@lovell lovell added this to the v0.33.5 milestone Jul 5, 2024
@lovell
Copy link
Owner

lovell commented Aug 16, 2024

v0.33.5 now available, thank you for implementing this feature.

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

No branches or pull requests

2 participants