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

Scale values when converting RGB to BGR;15/16 #7972

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
scale values when converting to bgr;15/16
instead of just cutting off the lower three bytes
  • Loading branch information
Yay295 committed Apr 13, 2024
commit e1cdd22ded01ea5a9713ad98350a390f28847f36
30 changes: 18 additions & 12 deletions src/libImaging/Convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,24 +280,30 @@ rgb2f(UINT8 *out_, const UINT8 *in, int xsize) {
}

static void
rgb2bgr15(UINT8 *out_, const UINT8 *in, int xsize) {
rgb2bgr15(UINT8 *out, const UINT8 *in, const int xsize) {
int x;
for (x = 0; x < xsize; x++, in += 4, out_ += 2) {
UINT16 v = ((((UINT16)in[0]) << 7) & 0x7c00) +
((((UINT16)in[1]) << 2) & 0x03e0) +
((((UINT16)in[2]) >> 3) & 0x001f);
memcpy(out_, &v, sizeof(v));
for (x = 0; x < xsize; x++, in += 4, out += 2) {
const UINT16 r = (UINT16)in[0] * 31 / 255;
const UINT16 g = (UINT16)in[1] * 31 / 255;
const UINT16 b = (UINT16)in[2] * 31 / 255;
const UINT16 v = ((b & 31) << 10) +
((g & 31) << 5) +
(r & 31);
memcpy(out, &v, sizeof(v));
}
}

static void
rgb2bgr16(UINT8 *out_, const UINT8 *in, int xsize) {
rgb2bgr16(UINT8 *out, const UINT8 *in, const int xsize) {
int x;
for (x = 0; x < xsize; x++, in += 4, out_ += 2) {
UINT16 v = ((((UINT16)in[0]) << 8) & 0xf800) +
((((UINT16)in[1]) << 3) & 0x07e0) +
((((UINT16)in[2]) >> 3) & 0x001f);
memcpy(out_, &v, sizeof(v));
for (x = 0; x < xsize; x++, in += 4, out += 2) {
const UINT16 r = (UINT16)in[0] * 31 / 255;
const UINT16 g = (UINT16)in[1] * 63 / 255;
const UINT16 b = (UINT16)in[2] * 31 / 255;
const UINT16 v = ((b & 31) << 11) +
((g & 63) << 6) +
(r & 31);
memcpy(out, &v, sizeof(v));
}
}

Expand Down
Loading