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

Improvements to the handling of images with multiple alpha channels #2266

Open
webbery opened this issue Jun 21, 2020 · 4 comments
Open

Improvements to the handling of images with multiple alpha channels #2266

webbery opened this issue Jun 21, 2020 · 4 comments

Comments

@webbery
Copy link

webbery commented Jun 21, 2020

Hi, I use sharp v0.25.4.
I load a 16bit tiff and want to display on web canvas. Web chanvas require 4 channel containning alpha. In my code, I write it like this:

let {data, info} = await sharp(tiffpath).jpeg({force: true}).ensureAlpha()
          .raw().toBuffer({ resolveWithObject: true })

I see info is still 6 channel but not 4. Then my code throw an error which can not create ImageData. Is it a bug?

@webbery webbery added the triage label Jun 21, 2020
@lovell
Copy link
Owner

lovell commented Jun 21, 2020

Hi, are you able to provide a sample input image?

As an aside, the use of jpeg() in this code sample is ignored as the use of raw() later in the chain takes precedence.

@webbery
Copy link
Author

webbery commented Jun 21, 2020

Here is my image.
sample.zip
Although I can convert 6 channel to 4 channel, I think it's slowly in javscript. It is best to finish this work in C++.

@lovell
Copy link
Owner

lovell commented Jun 22, 2020

This input TIFF image has 5 channels, RGB and what appears to be two alpha channels, which is unusual.

The raw, uncompressed output pixel data has five channels, for example:

const { info } = await sharp('Shipyard Cranes Masked (8).tif')
  .raw()
  .toBuffer();

produces:

{
  format: 'raw',
  width: 3363,
  height: 4000,
  channels: 5,
  premultiplied: false,
  size: 67260000
}

sharp currently doesn't understand "5 channel RGB" and does not know how treat channels 4 and 5, so using ensureAlpha() with this image adds a 6th channel.

const { info } = await sharp('Shipyard Cranes Masked (8).tif')
  .ensureAlpha()
  .raw()
  .toBuffer();

produces:

{
  format: 'raw',
  width: 3363,
  height: 4000,
  channels: 6,
  premultiplied: false,
  size: 80712000
}

There are two possible improvements here.

  1. ensureAlpha (and removeAlpha) might be able to better detect multiple alpha channels.
  2. raw should ignore/remove secondary alpha channels to meet its documented behaviour of providing RGBA.

@lovell lovell added enhancement and removed triage labels Jun 22, 2020
@lovell lovell changed the title 16bit tiff convert to jpeg raw still has 6 channel Improvements to the handling of images with multiple alpha channels Jun 22, 2020
@JamieVangeysel
Copy link

I don't use ensureAlpha or removeAlpha. For me it would work if the extra alpha channels are just ignored. The easiest way to implement that in code is by adding an option to the constructor ignoreSecondaryAlpha or something like that defaulting to false to not break alrady existing code

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

3 participants