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

Some unpackers are misnamed #8021

Open
Yay295 opened this issue Apr 27, 2024 · 6 comments · May be fixed by #8158
Open

Some unpackers are misnamed #8021

Yay295 opened this issue Apr 27, 2024 · 6 comments · May be fixed by #8158

Comments

@Yay295
Copy link
Contributor

Yay295 commented Apr 27, 2024

RGB15 actually reads data as XBGR (XBBBBBGGGGGRRRRR).

void
ImagingUnpackRGB15(UINT8 *out, const UINT8 *in, int pixels) {
int i, pixel;
/* RGB, 5 bits per pixel */
for (i = 0; i < pixels; i++) {
pixel = in[0] + (in[1] << 8);
out[R] = (pixel & 31) * 255 / 31;
out[G] = ((pixel >> 5) & 31) * 255 / 31;
out[B] = ((pixel >> 10) & 31) * 255 / 31;
out[A] = 255;
out += 4;
in += 2;
}
}

RGBA15 actually reads data as ABGR (ABBBBBGGGGGRRRRR).

void
ImagingUnpackRGBA15(UINT8 *out, const UINT8 *in, int pixels) {
int i, pixel;
/* RGB, 5/5/5/1 bits per pixel */
for (i = 0; i < pixels; i++) {
pixel = in[0] + (in[1] << 8);
out[R] = (pixel & 31) * 255 / 31;
out[G] = ((pixel >> 5) & 31) * 255 / 31;
out[B] = ((pixel >> 10) & 31) * 255 / 31;
out[A] = (pixel >> 15) * 255;
out += 4;
in += 2;
}
}

RGBA4B actually reads data as ABGR (AAAABBBBGGGGRRRR).

void
ImagingUnpackRGBA4B(UINT8 *out, const UINT8 *in, int pixels) {
int i, pixel;
/* RGBA, 4 bits per pixel */
for (i = 0; i < pixels; i++) {
pixel = in[0] + (in[1] << 8);
out[R] = (pixel & 15) * 17;
out[G] = ((pixel >> 4) & 15) * 17;
out[B] = ((pixel >> 8) & 15) * 17;
out[A] = ((pixel >> 12) & 15) * 17;
out += 4;
in += 2;
}
}

etc.

Basically, all of the unpackers that read less than 8 bits per band appear to be backwards.

@Yay295
Copy link
Contributor Author

Yay295 commented Apr 27, 2024

This is related to #8019, because I wrote those packers in the correct order, which is actually the opposite of the unpackers.

@Yay295
Copy link
Contributor Author

Yay295 commented Apr 27, 2024

Here's the names they should be based on what they actually do and the format specified in Unpack.c.

Pillow/src/libImaging/Unpack.c

Lines 1534 to 1543 in 824db71

/* raw mode syntax is "<mode>;<bits><flags>" where "bits" defaults
depending on mode (1 for "1", 8 for "P" and "L", etc), and
"flags" should be given in alphabetical order. if both bits
and flags have their default values, the ; should be left out */
/* flags: "I" inverted data; "R" reversed bit order; "B" big
endian byte order (default is little endian); "L" line
interleave, "S" signed, "F" floating point */
/* exception: rawmodes "I" and "F" are always native endian byte order */

RGB;15 → XBGR;1555
RGB;16 → BGR;565
BGR;5 → XRGB;1555
BGR;15 → XRGB;1555
BGR;16 → RGB;565
RGB;4B → XBGR;4
RGBA;4B → ABGR;4
RGBA;15 → ARGB;1555
BGRA;15 → ABGR;1555

Unfortunately the specified format doesn't quite work because the bands in these modes aren't all the same size, so I just listed all of the sizes for those modes.

The good thing about listing out the band sizes though is that those names aren't already being used, so I could add all of them as "new" rawmodes and the old ones could be deprecated without interfering with one another.

@radarhere
Copy link
Member

radarhere commented Apr 27, 2024

There's a slightly awkward overlap in code that would change with this and with #7965.

@radarhere radarhere changed the title Some of the unpackers are misnamed Some unpackers are misnamed Apr 27, 2024
@Yay295
Copy link
Contributor Author

Yay295 commented Apr 27, 2024

Yes. This change would supersede both #7965 and #8019.

@Yay295
Copy link
Contributor Author

Yay295 commented May 8, 2024

I have a branch for these changes, but it's based on #8026 because it affects the same test file.

@Yay295 Yay295 linked a pull request Jun 21, 2024 that will close this issue
@radarhere
Copy link
Member

That branch has now become #8158

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 a pull request may close this issue.

2 participants