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

Change bits per channel #534

Closed
strarsis opened this issue Aug 1, 2016 · 8 comments
Closed

Change bits per channel #534

strarsis opened this issue Aug 1, 2016 · 8 comments
Labels

Comments

@strarsis
Copy link

strarsis commented Aug 1, 2016

Is there a feature in sharp for changing bits per channel (e.g. from 32bit or 16bit to 8bit)
before processing/saving the loaded image?

@strarsis strarsis changed the title Change depth Change bits per channel Aug 1, 2016
@lovell
Copy link
Owner

lovell commented Aug 2, 2016

At the end of the processing pipeline, before generating the output image, 16-bit pixel values are cast to 8-bit - see

sharp/src/pipeline.cc

Lines 675 to 677 in 7ada9db

if (sharp::Is16Bit(image.interpretation())) {
image = image.cast(VIPS_FORMAT_USHORT);
}

When you say 32-bits per channel, do you mean an 8-bit per pixel image with 4 channels? If so, you may be looking for quantisation, which is not supported.

@lovell lovell added the question label Aug 2, 2016
@jdalton
Copy link

jdalton commented Sep 13, 2016

I ran into what I think is a related issue with compatibility with pngjs for

As input any color type is accepted (grayscale, rgb, palette, grayscale with alpha, rgb with alpha) but 8 bit per sample (channel) is the only supported bit depth. Interlaced mode is not supported.

Is there was a way to output a compatible png?

@lovell
Copy link
Owner

lovell commented Sep 13, 2016

Hello @jdalton, the following code seems to produce PNG output images from sharp that can be parsed by pngjs, at least when testing locally with a few different formats from the fixtures.

const assert = require('assert');
const pngjs = require('pngjs');
const sharp = require('sharp');

const input = '5_webp_a.webp';
const width = 100;
const height = 100;
let channels = 0;

const pngParser = new pngjs.PNG();

pngParser
  .on('parsed', (data) => {
    assert.strictEqual(data.length, width * height * channels);
  })
  .on('error', (err) => {
    assert.ifError(err);
  });

const transcoder = sharp(input)
  .resize(width, height)
  .png();

transcoder.on('info', (info) => {
  channels = info.channels;
});

transcoder.pipe(pngParser);

Perhaps a particularly tricky or complex input image is causing problems?

@jdalton
Copy link

jdalton commented Sep 13, 2016

Perhaps a particularly tricky or complex input image is causing problems?

Ah yeah. Turns out it was a OptiPNG optimization causing the issue.

@lovell
Copy link
Owner

lovell commented Sep 13, 2016

@strarsis has your original question been answered?

@strarsis
Copy link
Author

strarsis commented Sep 13, 2016

This was meant to be used to solve this issue: peterbraden/node-opencv#436
So sharp should remove alpha-transparency completely,
including the channel itself so opencv can handle the matching.

@lovell
Copy link
Owner

lovell commented Sep 13, 2016

@strarsis If OpenCV's matchTemplate feature doesn't support an alpha-transparency channel then I'd suggest using flatten() to remove it.

@strarsis
Copy link
Author

strarsis commented Sep 13, 2016

Thanks, this works. And for the other way around
(make image 32bit (with alphatransparency)), #554 (comment) can be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants