Skip to content
Open
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
64 changes: 27 additions & 37 deletions src/libImaging/Convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,17 +587,11 @@ l2i(UINT8 *out_, const UINT8 *in, int xsize) {

static void
i2l(UINT8 *out, const UINT8 *in_, int xsize) {
int x;
for (x = 0; x < xsize; x++, out++, in_ += 4) {
for (int x = 0; x < xsize; x++, out++, in_ += 4) {
INT32 v;
memcpy(&v, in_, sizeof(v));
if (v <= 0) {
*out = 0;
} else if (v >= 255) {
*out = 255;
} else {
*out = (UINT8)v;
}
// Branchless saturation
*out = (UINT8)(v <= 0 ? 0 : (v >= 255 ? 255 : v));
}
}

Expand Down Expand Up @@ -980,27 +974,30 @@ pa2f(UINT8 *out_, const UINT8 *in, int xsize, ImagingPalette palette) {
}
}

// Set the alpha channel of the UINT32 `v` in-place to the given value.
#ifdef WORDS_BIGENDIAN
#define SET_ALPHA_32(v, alpha) v = ((v & 0xFFFFFF00u) | (alpha))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define SET_ALPHA_32(v, alpha) v = ((v & 0xFFFFFF00u) | (alpha))
#define SET_ALPHA_32(v, alpha) v = ((v & 0xFFFFFF00u) | alpha)

@akx akx Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - defensive parentheses if alpha expands to an expression.

EDIT: Prior art in e.g. the B16/L16/S16 macros in _imaging.c.

#else
#define SET_ALPHA_32(v, alpha) v = ((v & 0x00FFFFFFu) | ((UINT32)(alpha) << 24))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define SET_ALPHA_32(v, alpha) v = ((v & 0x00FFFFFFu) | ((UINT32)(alpha) << 24))
#define SET_ALPHA_32(v, alpha) v = ((v & 0x00FFFFFFu) | ((UINT32)alpha << 24))

@akx akx Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather not, because alpha could be a more complex expression.

EDIT: Prior art in e.g. the B16/L16/S16 macros in _imaging.c.

#endif

static void
p2rgb(UINT8 *out, const UINT8 *in, int xsize, ImagingPalette palette) {
int x;
for (x = 0; x < xsize; x++) {
const UINT8 *rgb = &palette->palette[*in++ * 4];
*out++ = rgb[0];
*out++ = rgb[1];
*out++ = rgb[2];
*out++ = 255;
for (int x = 0; x < xsize; x++, in++, out += 4) {
UINT32 v;
memcpy(&v, &palette->palette[in[0] * 4], sizeof(v));
SET_ALPHA_32(v, 0xFF);
memcpy(out, &v, sizeof(v));
}
}

static void
pa2rgb(UINT8 *out, const UINT8 *in, int xsize, ImagingPalette palette) {
int x;
for (x = 0; x < xsize; x++, in += 4) {
const UINT8 *rgb = &palette->palette[in[0] * 4];
*out++ = rgb[0];
*out++ = rgb[1];
*out++ = rgb[2];
*out++ = 255;
for (int x = 0; x < xsize; x++, in += 4, out += 4) {
UINT32 v;
memcpy(&v, &palette->palette[in[0] * 4], sizeof(v));
SET_ALPHA_32(v, 0xFF);
memcpy(out, &v, sizeof(v));
}
}

Expand All @@ -1026,25 +1023,18 @@ pa2hsv(UINT8 *out, const UINT8 *in, int xsize, ImagingPalette palette) {

static void
p2rgba(UINT8 *out, const UINT8 *in, int xsize, ImagingPalette palette) {
int x;
for (x = 0; x < xsize; x++) {
const UINT8 *rgba = &palette->palette[*in++ * 4];
*out++ = rgba[0];
*out++ = rgba[1];
*out++ = rgba[2];
*out++ = rgba[3];
for (int x = 0; x < xsize; x++, in++, out += 4) {
memcpy(out, &palette->palette[in[0] * 4], 4);
}
}

static void
pa2rgba(UINT8 *out, const UINT8 *in, int xsize, ImagingPalette palette) {
int x;
for (x = 0; x < xsize; x++, in += 4) {
const UINT8 *rgb = &palette->palette[in[0] * 4];
*out++ = rgb[0];
*out++ = rgb[1];
*out++ = rgb[2];
*out++ = in[3];
for (int x = 0; x < xsize; x++, in += 4, out += 4) {
UINT32 v;
memcpy(&v, &palette->palette[in[0] * 4], sizeof(v));
SET_ALPHA_32(v, in[3]);
memcpy(out, &v, sizeof(v));
}
}

Expand Down
Loading