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

4 channel LZW TIFF output returns a value of 3 for info.channels #2064

Closed
BryceCicada opened this issue Jan 30, 2020 · 5 comments
Closed

4 channel LZW TIFF output returns a value of 3 for info.channels #2064

BryceCicada opened this issue Jan 30, 2020 · 5 comments

Comments

@BryceCicada
Copy link

  System:
    OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
    CPU: (8) x64 Intel(R) Xeon(R) CPU E3-1505M v6 @ 3.00GHz
    Memory: 1.68 GB / 15.52 GB
    Container: Yes
    Shell: 4.4.19 - /bin/bash
  Binaries:
    Node: 12.10.0 - /usr/bin/node
    npm: 6.12.0 - /usr/bin/npm
  Utilities:
    CMake: 3.10.2 - /usr/bin/cmake
    Make: 4.1 - /usr/bin/make
    GCC: 7.4.0 - /usr/bin/gcc
    Git: 2.17.1 - /usr/bin/git
    Subversion: 1.9.7 - /usr/bin/svn
    FFmpeg: 3.4.6 - /usr/bin/ffmpeg
  Languages:
    Bash: 4.4.19 - /bin/bash
    Go: 1.10.4 - /usr/bin/go
    Perl: 5.26.1 - /usr/bin/perl
    Python: 2.7.15+ - /usr/bin/python
    Python3: 3.6.8 - /usr/bin/python3
    Ruby: 2.5.1 - /usr/bin/ruby

I'm trying to create a 4-channel tiff from raw pixel data. Unfortunately, when this is written to disk, the image has only 3 channels. Here's snippet using sharp 0.24.0

import * as sharp from 'sharp'

async function main () {
  const width = 32
  const height = 32
  const channels = 4
  const info = await sharp(
    Buffer.from(new Array(width * height * channels).fill(0)),
    {
      raw: { width, height, channels }
    }
  ).toFile('test.tif')

  if (info.channels !== channels) {
    throw new Error(`Unexpected number of channels in output image. Expected ${channels}, but got ${info.channels}`)
  }
}

main()

Issue also occurs when saving jpgs (eg toFile('test.jpg')). Does not occur saving pngs.

I can't see anything obvious in the docs that excludes this possibility. Advice appreciated.

@BryceCicada
Copy link
Author

BryceCicada commented Jan 30, 2020

If I set the tiff compression to 'lzw', then the output saves to disk as 4-channel tiff. The returned info still has info.channels === 3 though:

  const info = await sharp(
    Buffer.from(new Array(width * height * channels).fill(0)),
    {
      raw: { width, height, channels }
    }
  )
  .tiff({
    compression: 'lzw'
  })
  .toFile('test.tif')

@lovell
Copy link
Owner

lovell commented Jan 31, 2020

The default TIFF compression setting is jpeg, which only supports 3 channels, so a 4th channel if any is considered alpha and flattened out.

As you've discovered, the lzw compression setting does support 4 channels, so the value of 3 returned in info.channels looks like a bug.

@lovell lovell changed the title Unable to save 4-channel tiffs or jpgs. 4 channel LZW TIFF output returns a value of 3 for info.channels Jan 31, 2020
@lovell lovell added bug and removed triage labels Jan 31, 2020
@BryceCicada
Copy link
Author

Thanks for the advice about jpeg and 3 channels. Minor brain fail on that one.

Triage to bug for lzw image info appreciated.

@lovell lovell added this to the v0.24.1 milestone Feb 15, 2020
@lovell
Copy link
Owner

lovell commented Feb 15, 2020

Fixed and tests added in commit a2314c4. This will be in v0.24.1, thanks for reporting.

@lovell
Copy link
Owner

lovell commented Feb 15, 2020

v0.24.1 now available.

@lovell lovell closed this as completed Feb 15, 2020
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