From 51433b94f074de7287bcdd064996d9809c9e0de9 Mon Sep 17 00:00:00 2001 From: Patrik Torstensson Date: Sat, 30 Jan 2021 18:16:25 -0800 Subject: minui: Add support for ARGB (LSB alpha) - Called RGBA to follow convention in minui where big-endian description of format is used and to avoid conflict with existing ARGB - Handle alpha being LSB in png and text rendering Change-Id: I0d9afef55aad05028ed0b8b4204f1d52aaa683c3 (cherry picked from commit cd1c327427febe7cf76e6fa4835847f0d4f398e3) --- minui/resources.cpp | 56 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) (limited to 'minui/resources.cpp') diff --git a/minui/resources.cpp b/minui/resources.cpp index d7b927700..1521c8f17 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -153,32 +153,57 @@ static void TransformRgbToDraw(const uint8_t* input_row, uint8_t* output_row, in int width) { const uint8_t* ip = input_row; uint8_t* op = output_row; + PixelFormat pixel_format = gr_pixel_format(); switch (channels) { case 1: // expand gray level to RGBX for (int x = 0; x < width; ++x) { - *op++ = *ip; - *op++ = *ip; - *op++ = *ip; - *op++ = 0xff; + if (pixel_format == PixelFormat::RGBA) { + *op++ = 0xff; + *op++ = *ip; + *op++ = *ip; + *op++ = *ip; + } else { + *op++ = *ip; + *op++ = *ip; + *op++ = *ip; + *op++ = 0xff; + } ip++; } break; case 3: - // expand RGBA to RGBX for (int x = 0; x < width; ++x) { - *op++ = *ip++; - *op++ = *ip++; - *op++ = *ip++; - *op++ = 0xff; + // expand RGBA to RGBX + if (pixel_format == PixelFormat::RGBA) { + *op++ = 0xff; + *op++ = *ip++; + *op++ = *ip++; + *op++ = *ip++; + } else { + *op++ = *ip++; + *op++ = *ip++; + *op++ = *ip++; + *op++ = 0xff; + } } break; case 4: - // copy RGBA to RGBX - memcpy(output_row, input_row, width * 4); + if (pixel_format == PixelFormat::RGBA) { + for (int x = 0; x < width; ++x) { + *op++ = *(ip + 3); + *op++ = *ip++; + *op++ = *ip++; + *op++ = *ip++; + ip++; + } + } else { + // copy RGBA to RGBX + memcpy(output_row, input_row, width * 4); + } break; } } @@ -201,6 +226,8 @@ int res_create_display_surface(const char* name, GRSurface** pSurface) { PixelFormat pixel_format = gr_pixel_format(); if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) { png_set_bgr(png_ptr); + } else if (pixel_format == PixelFormat::RGBA) { + png_set_swap_alpha(png_ptr); } for (png_uint_32 y = 0; y < height; ++y) { @@ -273,6 +300,8 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, if (gr_pixel_format() == PixelFormat::ARGB || gr_pixel_format() == PixelFormat::BGRA) { png_set_bgr(png_ptr); + } else if (gr_pixel_format() == PixelFormat::RGBA) { + png_set_swap_alpha(png_ptr); } for (png_uint_32 y = 0; y < height; ++y) { @@ -316,11 +345,6 @@ int res_create_alpha_surface(const char* name, GRSurface** pSurface) { return -8; } - PixelFormat pixel_format = gr_pixel_format(); - if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) { - png_set_bgr(png_ptr); - } - for (png_uint_32 y = 0; y < height; ++y) { uint8_t* p_row = surface->data() + y * surface->row_bytes; png_read_row(png_ptr, p_row, nullptr); -- cgit v1.2.3