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

Accept raw buffer as input #220

Closed
mikemorris opened this issue May 15, 2015 · 12 comments
Closed

Accept raw buffer as input #220

mikemorris opened this issue May 15, 2015 · 12 comments
Milestone

Comments

@mikemorris
Copy link

For reading pixels from the GPU and then compressing to PNG or JPEG.

Error: Input buffer contains unsupported image formatVipsForeignLoad: buffer is not in a known format
@mikemorris
Copy link
Author

It looks like this should be supported upstream by vips_image_new_from_memory, but may not be detected/implemented because of the differing naming convention from vips_image_new_from_buffer. My local libvips-8.0.2 installed via Homebrew reports raw file input/output support when I add "raw" to https://github.com/lovell/sharp/blob/master/src/utilities.cc#L99 and remove the manual raw support description.

@lovell
Copy link
Owner

lovell commented May 15, 2015

Yes, it should be quite easy to add this, thanks for the suggestion.

We'd need to work out a way for a user to provide the width, height and number of channels alongside the Buffer containing raw data, either as a second parameter to the constructor or via a new method.

This feature would probably have to assume, at least initially, that the raw data is 8 bits per pixel, with 1x channel for greyscale, 2x channel for greyscale+alpha, 3x channel for sRGB or 4x channel for sRGB+alpha.

@lovell lovell added this to the v1.0.0 milestone Jun 28, 2015
@pajtai
Copy link

pajtai commented Aug 5, 2015

After trying to pass a buffer from the request npm, I can see that this does not currently work, but from the docs, what kind of buffers can be passed to the constructor?

Buffer containing JPEG, PNG, WebP, GIF* or TIFF image data,

@lovell
Copy link
Owner

lovell commented Aug 6, 2015

@pajtai This issue relates to the addition of support for uncompressed pixel data input.

The sharp.format attribute will tell you which compressed formats are available to you (depends on sharp+libvips versions).

"I can see that this does not currently work"

If you're still having problems, feel free to create a new issue with more details of the error you're experiencing and, if possible, a code sample.

@pajtai
Copy link

pajtai commented Aug 6, 2015

Thanks @lovell - I created #247 with code snippet and explanation of what I'm trying to do.

@lovell lovell modified the milestones: v0.13.0, v1.0.0 Feb 1, 2016
@lovell
Copy link
Owner

lovell commented Feb 3, 2016

Commit e380576 on the mind branch adds support for raw, uncompressed pixel data input via Buffer or Stream.

@nicolaspanel
Copy link

@lovell : could you share an example of how to use raw inputs?

I tried this:

var H = 100, W = 100, C = 1, n = H*W*C, data = new Uint8Array(n);

for (var i=0; i<n; i++) {data[i] = 0;}

sharp(new Buffer(data.buffer), { rawWidth: W, rawHeight: H, rawChannels: C})
        .toBuffer()
        .then(function () {});

but it throws an error ("Input buffer contains unsupported image format")

NB: I'm using v0.13.0 (from master branch)

@lovell
Copy link
Owner

lovell commented Feb 10, 2016

@nicolaspanel Bonjour, please can you try something like { raw: { width: W, height: H, channels: C} } (as rawWidth etc. are internal attributes).

Test/example: https://github.com/lovell/sharp/blob/master/test/unit/io.js#L915-L928

@nicolaspanel
Copy link

It works now 👍
Thanks a lot

@lovell
Copy link
Owner

lovell commented Feb 15, 2016

v0.13.0 now available, thanks everyone for the original suggestion and help with testing.

@knyga
Copy link

knyga commented Aug 15, 2021

I am currently on 0.28.3 and it looks like raw input is not supported. I try to execute basic case mentioned in the case: https://github.com/lovell/sharp/blob/master/test/unit/io.js#L915-L928

Repository owner locked and limited conversation to collaborators Aug 15, 2021
@lovell
Copy link
Owner

lovell commented Aug 15, 2021

@knyga https://sharp.pixelplumbing.com/api-constructor#examples contains an example of using uncompressed pixel data as input.

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

No branches or pull requests

5 participants