LibImage: name color to u32 function to to_argb from to_rgba

This is the actual format that it returns
This commit is contained in:
Bananymous 2024-10-13 22:01:46 +03:00
parent 8adc97980a
commit d36b64e0c8
4 changed files with 36 additions and 42 deletions

View File

@ -7,7 +7,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <math.h> #include <math.h>
#include <stdlib.h>
#include <sys/mman.h> #include <sys/mman.h>
namespace LibImage namespace LibImage
@ -87,9 +86,9 @@ namespace LibImage
constexpr Image::Color as_color() const constexpr Image::Color as_color() const
{ {
return Image::Color { return Image::Color {
.r = static_cast<uint8_t>(r < 0.0 ? 0.0 : r > 255.0 ? 255.0 : r),
.g = static_cast<uint8_t>(g < 0.0 ? 0.0 : g > 255.0 ? 255.0 : g),
.b = static_cast<uint8_t>(b < 0.0 ? 0.0 : b > 255.0 ? 255.0 : b), .b = static_cast<uint8_t>(b < 0.0 ? 0.0 : b > 255.0 ? 255.0 : b),
.g = static_cast<uint8_t>(g < 0.0 ? 0.0 : g > 255.0 ? 255.0 : g),
.r = static_cast<uint8_t>(r < 0.0 ? 0.0 : r > 255.0 ? 255.0 : r),
.a = static_cast<uint8_t>(a < 0.0 ? 0.0 : a > 255.0 ? 255.0 : a), .a = static_cast<uint8_t>(a < 0.0 ? 0.0 : a > 255.0 ? 255.0 : a),
}; };
} }

View File

@ -560,52 +560,47 @@ namespace LibImage
const auto extract_color = const auto extract_color =
[&](auto& bit_buffer) -> Image::Color [&](auto& bit_buffer) -> Image::Color
{ {
uint8_t tmp; Image::Color color;
switch (ihdr.colour_type) switch (ihdr.colour_type)
{ {
case ColourType::Greyscale: case ColourType::Greyscale:
tmp = extract_channel(bit_buffer); color.r = extract_channel(bit_buffer);
return Image::Color { color.g = color.r;
.r = tmp, color.b = color.r;
.g = tmp, color.a = 0xFF;
.b = tmp, break;
.a = 0xFF
};
case ColourType::Truecolour: case ColourType::Truecolour:
return Image::Color { color.r = extract_channel(bit_buffer);
.r = extract_channel(bit_buffer), color.g = extract_channel(bit_buffer);
.g = extract_channel(bit_buffer), color.b = extract_channel(bit_buffer);
.b = extract_channel(bit_buffer), color.a = 0xFF;
.a = 0xFF break;
};
case ColourType::IndexedColour: case ColourType::IndexedColour:
return palette[MUST(bit_buffer.get_bits(bits_per_channel))]; color = palette[MUST(bit_buffer.get_bits(bits_per_channel))];
break;
case ColourType::GreyscaleAlpha: case ColourType::GreyscaleAlpha:
tmp = extract_channel(bit_buffer); color.r = extract_channel(bit_buffer);
return Image::Color { color.g = color.r;
.r = tmp, color.b = color.r;
.g = tmp, color.a = extract_channel(bit_buffer);
.b = tmp, break;
.a = extract_channel(bit_buffer)
};
case ColourType::TruecolourAlpha: case ColourType::TruecolourAlpha:
return Image::Color { color.r = extract_channel(bit_buffer);
.r = extract_channel(bit_buffer), color.g = extract_channel(bit_buffer);
.g = extract_channel(bit_buffer), color.b = extract_channel(bit_buffer);
.b = extract_channel(bit_buffer), color.a = extract_channel(bit_buffer);
.a = extract_channel(bit_buffer) break;
};
} }
ASSERT_NOT_REACHED(); return color;
}; };
constexpr auto paeth_predictor = constexpr auto paeth_predictor =
[](int16_t a, int16_t b, int16_t c) -> uint8_t [](int16_t a, int16_t b, int16_t c) -> uint8_t
{ {
int16_t p = a + b - c; const int16_t p = a + b - c;
int16_t pa = BAN::Math::abs(p - a); const int16_t pa = BAN::Math::abs(p - a);
int16_t pb = BAN::Math::abs(p - b); const int16_t pb = BAN::Math::abs(p - b);
int16_t pc = BAN::Math::abs(p - c); const int16_t pc = BAN::Math::abs(p - c);
if (pa <= pb && pa <= pc) if (pa <= pb && pa <= pc)
return a; return a;
if (pb <= pc) if (pb <= pc)

View File

@ -13,9 +13,9 @@ namespace LibImage
public: public:
struct Color struct Color
{ {
uint8_t r;
uint8_t g;
uint8_t b; uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a; uint8_t a;
// Calculate weighted average of colors // Calculate weighted average of colors
@ -25,14 +25,14 @@ namespace LibImage
const double b_mult = weight < 0.0 ? 0.0 : weight > 1.0 ? 1.0 : weight; const double b_mult = weight < 0.0 ? 0.0 : weight > 1.0 ? 1.0 : weight;
const double a_mult = 1.0 - b_mult; const double a_mult = 1.0 - b_mult;
return Color { return Color {
.r = static_cast<uint8_t>(a.r * a_mult + b.r * b_mult),
.g = static_cast<uint8_t>(a.g * a_mult + b.g * b_mult),
.b = static_cast<uint8_t>(a.b * a_mult + b.b * b_mult), .b = static_cast<uint8_t>(a.b * a_mult + b.b * b_mult),
.g = static_cast<uint8_t>(a.g * a_mult + b.g * b_mult),
.r = static_cast<uint8_t>(a.r * a_mult + b.r * b_mult),
.a = static_cast<uint8_t>(a.a * a_mult + b.a * b_mult), .a = static_cast<uint8_t>(a.a * a_mult + b.a * b_mult),
}; };
} }
uint32_t as_rgba() const uint32_t as_argb() const
{ {
return ((uint32_t)a << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; return ((uint32_t)a << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
} }

View File

@ -330,7 +330,7 @@ void WindowServer::invalidate(Rectangle area)
ASSERT(m_background_image->height() == (uint64_t)m_framebuffer.height); ASSERT(m_background_image->height() == (uint64_t)m_framebuffer.height);
for (int32_t y = area.y; y < area.y + area.height; y++) for (int32_t y = area.y; y < area.y + area.height; y++)
for (int32_t x = area.x; x < area.x + area.width; x++) for (int32_t x = area.x; x < area.x + area.width; x++)
m_framebuffer.mmap[y * m_framebuffer.width + x] = m_background_image->get_color(x, y).as_rgba(); m_framebuffer.mmap[y * m_framebuffer.width + x] = m_background_image->get_color(x, y).as_argb();
} }
else else
{ {