mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
LibGfx/PortableFormat: Write directly to the bitmap
No need to copy the data first to a Vector and then to the Bitmap. We can skip this unnecessary step.
This commit is contained in:
parent
d8c1150f6b
commit
d00a563462
Notes:
sideshowbarker
2024-07-18 01:43:16 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/d00a563462 Pull-request: https://github.com/SerenityOS/serenity/pull/19283 Reviewed-by: https://github.com/gmta ✅ Reviewed-by: https://github.com/kleinesfilmroellchen Reviewed-by: https://github.com/nico ✅ Reviewed-by: https://github.com/timschumi
@ -12,19 +12,19 @@ namespace Gfx {
|
||||
|
||||
ErrorOr<void> read_image_data(PBMLoadingContext& context)
|
||||
{
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
auto& stream = *context.stream;
|
||||
Vector<Gfx::Color> color_data;
|
||||
|
||||
auto const context_size = context.width * context.height;
|
||||
color_data.resize(context_size);
|
||||
|
||||
if (context.type == PBMLoadingContext::Type::ASCII) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
auto const byte = TRY(stream.read_value<u8>());
|
||||
if (byte == '0')
|
||||
color_data[i] = Color::White;
|
||||
context.bitmap->set_pixel(i % context.width, i / context.width, Color::White);
|
||||
else if (byte == '1')
|
||||
color_data[i] = Color::Black;
|
||||
context.bitmap->set_pixel(i % context.width, i / context.width, Color::Black);
|
||||
else
|
||||
i--;
|
||||
}
|
||||
@ -35,9 +35,9 @@ ErrorOr<void> read_image_data(PBMLoadingContext& context)
|
||||
auto const val = byte & 0x80;
|
||||
|
||||
if (val == 0)
|
||||
color_data[color_index] = Color::White;
|
||||
context.bitmap->set_pixel(color_index % context.width, color_index / context.width, Color::White);
|
||||
else
|
||||
color_data[color_index] = Color::Black;
|
||||
context.bitmap->set_pixel(color_index % context.width, color_index / context.width, Color::Black);
|
||||
|
||||
byte = byte << 1;
|
||||
color_index++;
|
||||
@ -49,10 +49,6 @@ ErrorOr<void> read_image_data(PBMLoadingContext& context)
|
||||
}
|
||||
}
|
||||
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
set_pixels(context, color_data);
|
||||
|
||||
context.state = PBMLoadingContext::State::Bitmap;
|
||||
return {};
|
||||
}
|
||||
|
@ -10,28 +10,12 @@
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
static void set_adjusted_pixels(PGMLoadingContext& context, Vector<Gfx::Color> const& color_data)
|
||||
{
|
||||
size_t index = 0;
|
||||
for (size_t y = 0; y < context.height; ++y) {
|
||||
for (size_t x = 0; x < context.width; ++x) {
|
||||
Color color = color_data.at(index);
|
||||
if (context.format_details.max_val < 255) {
|
||||
color = adjust_color(context.format_details.max_val, color);
|
||||
}
|
||||
context.bitmap->set_pixel(x, y, color);
|
||||
++index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ErrorOr<void> read_image_data(PGMLoadingContext& context)
|
||||
{
|
||||
auto& stream = *context.stream;
|
||||
Vector<Gfx::Color> color_data;
|
||||
auto const context_size = context.width * context.height;
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
color_data.resize(context_size);
|
||||
auto& stream = *context.stream;
|
||||
auto const context_size = context.width * context.height;
|
||||
|
||||
if (context.type == PGMLoadingContext::Type::ASCII) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
@ -39,19 +23,19 @@ ErrorOr<void> read_image_data(PGMLoadingContext& context)
|
||||
|
||||
TRY(read_whitespace(context));
|
||||
|
||||
color_data[i] = { static_cast<u8>(value), static_cast<u8>(value), static_cast<u8>(value) };
|
||||
Color color { static_cast<u8>(value), static_cast<u8>(value), static_cast<u8>(value) };
|
||||
if (context.format_details.max_val < 255)
|
||||
color = adjust_color(context.format_details.max_val, color);
|
||||
|
||||
context.bitmap->set_pixel(i % context.width, i / context.width, color);
|
||||
}
|
||||
} else if (context.type == PGMLoadingContext::Type::RAWBITS) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
auto const pixel = TRY(stream.read_value<u8>());
|
||||
color_data[i] = { pixel, pixel, pixel };
|
||||
context.bitmap->set_pixel(i % context.width, i / context.width, { pixel, pixel, pixel });
|
||||
}
|
||||
}
|
||||
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
set_adjusted_pixels(context, color_data);
|
||||
|
||||
context.state = PGMLoadingContext::State::Bitmap;
|
||||
return {};
|
||||
}
|
||||
|
@ -12,9 +12,9 @@ namespace Gfx {
|
||||
|
||||
ErrorOr<void> read_image_data(PPMLoadingContext& context)
|
||||
{
|
||||
Vector<Gfx::Color> color_data;
|
||||
auto const context_size = context.width * context.height;
|
||||
color_data.resize(context_size);
|
||||
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
auto& stream = *context.stream;
|
||||
|
||||
@ -32,7 +32,7 @@ ErrorOr<void> read_image_data(PPMLoadingContext& context)
|
||||
Color color { static_cast<u8>(red), static_cast<u8>(green), static_cast<u8>(blue) };
|
||||
if (context.format_details.max_val < 255)
|
||||
color = adjust_color(context.format_details.max_val, color);
|
||||
color_data[i] = color;
|
||||
context.bitmap->set_pixel(i % context.width, i / context.width, color);
|
||||
}
|
||||
} else if (context.type == PPMLoadingContext::Type::RAWBITS) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
@ -41,14 +41,10 @@ ErrorOr<void> read_image_data(PPMLoadingContext& context)
|
||||
|
||||
TRY(stream.read_until_filled(buffer));
|
||||
|
||||
color_data[i] = { pixel[0], pixel[1], pixel[2] };
|
||||
context.bitmap->set_pixel(i % context.width, i / context.width, { pixel[0], pixel[1], pixel[2] });
|
||||
}
|
||||
}
|
||||
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
set_pixels(context, color_data);
|
||||
|
||||
context.state = PPMLoadingContext::State::Bitmap;
|
||||
return {};
|
||||
}
|
||||
|
@ -175,18 +175,6 @@ static ErrorOr<void> create_bitmap(TContext& context)
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename TContext>
|
||||
static void set_pixels(TContext& context, Vector<Gfx::Color> const& color_data)
|
||||
{
|
||||
size_t index = 0;
|
||||
for (size_t y = 0; y < context.height; ++y) {
|
||||
for (size_t x = 0; x < context.width; ++x) {
|
||||
context.bitmap->set_pixel(x, y, color_data.at(index));
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename TContext>
|
||||
static ErrorOr<void> decode(TContext& context)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user