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

Add support for unpacking 16-bit BGRA #6167

Merged
merged 3 commits into from
Apr 3, 2022

Conversation

gmarkall
Copy link
Contributor

Adds support for unpacking 16-bit BGRA format. This format is used by ImageMagick 6 for many platforms - the addition of this support enables converting directly from Magick::PixelPacket* into PIL images.

I have used this new functionality in an example image-processing library I wrote to accompany a talk on Python APIs for CUDA-accelerated applications (see e.g. https://github.com/gmarkall/numba-accelerated-udfs/blob/main/filigree/api.py#L47), but I think it is generally useful for other applications to be able to work directly with ImageMagick 6 data.

@nulano
Copy link
Contributor

nulano commented Mar 31, 2022

Related to #1888.

It seems this PR scales 16-bit images to 8-bit by discarding the lower 8 bits. Note that this was considered controversial for I mode images in #3011.

@gmarkall
Copy link
Contributor Author

It seems this PR scales 16-bit images to 8-bit by discarding the lower 8 bits.

Yes, it does - I just followed the same pattern used in unpackRGBA16L and unpackRGBA16B which seem to do the same:

Pillow/src/libImaging/Unpack.c

Lines 1010 to 1030 in dae82d1

void
unpackRGBA16L(UINT8 *_out, const UINT8 *in, int pixels) {
int i;
/* 16-bit RGBA, little-endian order */
for (i = 0; i < pixels; i++, _out += 4) {
UINT32 iv = MAKE_UINT32(in[1], in[3], in[5], in[7]);
memcpy(_out, &iv, sizeof(iv));
in += 8;
}
}
void
unpackRGBA16B(UINT8 *_out, const UINT8 *in, int pixels) {
int i;
/* 16-bit RGBA, big-endian order */
for (i = 0; i < pixels; i++, _out += 4) {
UINT32 iv = MAKE_UINT32(in[0], in[2], in[4], in[6]);
memcpy(_out, &iv, sizeof(iv));
in += 8;
}
}

(in fact I now realise I have a copy/paste error in the comment from those functions - will fix shortly)

@gmarkall
Copy link
Contributor Author

(in fact I now realise I have a copy/paste error in the comment from those functions - will fix shortly)

... actually I didn't - apologies!... I found the comments I added confusing, but they're consistent with the other comments in the file as far as I can tell.

src/libImaging/Unpack.c Outdated Show resolved Hide resolved
src/libImaging/Unpack.c Outdated Show resolved Hide resolved
@radarhere radarhere merged commit ba5f2d7 into python-pillow:main Apr 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants