Skip to content

Commit a68de01

Browse files
committed
convert_to: support for RGBa -> BGR.
1 parent c88616a commit a68de01

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

pillow_heif/helpers.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,25 @@ void convert_rgba_premultiplied_to_rgb(const uint8_t *in, int in_stride, uint8_t
503503
}
504504
}
505505

506+
void convert_rgba_premultiplied_to_bgr(const uint8_t *in, int in_stride, uint8_t *out, int out_stride, int n_rows)
507+
{
508+
const uint32_t* in_row = (uint32_t*)in;
509+
uint8_t* out_row = out;
510+
in_stride = in_stride / 4;
511+
int stride_elements = out_stride / 3 > in_stride ? in_stride : out_stride / 3;
512+
for (int i = 0; i < n_rows; i++) {
513+
for (int i2 = 0; i2 < stride_elements; i2++) {
514+
uint32_t p = in_row[i2];
515+
uint8_t a = (p >> 24) & 0xFF;
516+
out_row[i2 * 3 + 0] = (((p >> 16) & 0xFF) * a) / 255;
517+
out_row[i2 * 3 + 1] = (((p >> 8) & 0xFF) * a) / 255;
518+
out_row[i2 * 3 + 2] = (((p >> 0) & 0xFF) * a) / 255;
519+
}
520+
in_row += in_stride;
521+
out_row += out_stride;
522+
}
523+
}
524+
506525
#ifdef __cplusplus
507526
}
508527
#endif

pillow_heif/helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ void convert_bgra_rgba(const uint8_t *in, int in_stride, uint8_t *out, int out_s
5151
void convert_bgr_rgb(const uint8_t *in, int in_stride, uint8_t *out, int out_stride, int n_rows);
5252

5353
void convert_rgba_premultiplied_to_rgb(const uint8_t *in, int in_stride, uint8_t *out, int out_stride, int n_rows);
54+
55+
void convert_rgba_premultiplied_to_bgr(const uint8_t *in, int in_stride, uint8_t *out, int out_stride, int n_rows);

pillow_heif/private.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def get_pure_stride(mode: str, width: int):
7272
"RGBa;16": lib.convert_rgba_to_rgba16,
7373
"BGRa;16": lib.convert_rgba_to_bgra16,
7474
"RGB": lib.convert_rgba_premultiplied_to_rgb,
75+
"BGR": lib.convert_rgba_premultiplied_to_bgr,
7576
},
7677
"RGB": {"BGR": lib.convert_bgr_rgb, "RGB;16": lib.convert_rgb_to_rgb16, "BGR;16": lib.convert_rgb_to_bgr16},
7778
}

tests/mode_convert_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,20 @@ def test_rgba_premultiplied_to_rgb():
132132
"f3e3e3f3d423f40413f40413f3f403f3f403f3e3f3f3e3f3f3d3e3f3d423f40423f3f413f3f41"
133133
"3f3e403f3e403f3d3f3f3d3f3f3c433f3f423f3f423f3e413f3e413f3d403f3d403f3c3f3f3c"
134134
)
135+
136+
137+
def test_rgba_premultiplied_to_bgr():
138+
im_heif = from_pillow(helpers.gradient_rgba().crop((124, 124, 132, 132)))
139+
im_heif.premultiplied_alpha = True
140+
assert im_heif.mode == "RGBa"
141+
im_heif.convert_to("BGR")
142+
assert im_heif.mode == "BGR"
143+
im_heif.convert_to("RGB")
144+
assert (
145+
im_heif.to_pillow().tobytes().hex()
146+
== "3f3f433f3f423e3f423e3f413d3f413d3f403c3f403c3f3f403f423f3f423f3f413e3f413e3f4"
147+
"03d3f403d3f3f3c3f3f403f42403f413f3f413f3f403e3f403e3f3f3d3f3f3d3f3e413f41403f"
148+
"41403f403f3f403f3f3f3e3f3f3e3f3e3d3f3e413f41413f40403f40403f3f3f3f3f3f3f3e3e3"
149+
"f3e3e3f3d423f40413f40413f3f403f3f403f3e3f3f3e3f3f3d3e3f3d423f40423f3f413f3f41"
150+
"3f3e403f3e403f3d3f3f3d3f3f3c433f3f423f3f423f3e413f3e413f3d403f3d403f3c3f3f3c"
151+
)

0 commit comments

Comments
 (0)