Skip to content

Change bits per channel #534

Closed
Closed

Description

@strarsis

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?

Activity

  1. changed the title [-]Change depth[/-] [+]Change bits per channel[/+] on Aug 1, 2016
  2. lovell commented on Aug 2, 2016

    @lovell
    Owner

    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.

  3. jdalton commented on Sep 13, 2016

    @jdalton

    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?

  4. lovell commented on Sep 13, 2016

    @lovell
    Owner

    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?

  5. jdalton commented on Sep 13, 2016

    @jdalton

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

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

  6. lovell commented on Sep 13, 2016

    @lovell
    Owner

    @strarsis has your original question been answered?

  7. strarsis commented on Sep 13, 2016

    @strarsis
    Author

    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.

  8. lovell commented on Sep 13, 2016

    @lovell
    Owner

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

  9. strarsis commented on Sep 13, 2016

    @strarsis
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions