mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
image: Move image modification code to helper functions
No behavior change.
This commit is contained in:
parent
da243ebb8b
commit
a339f297d1
Notes:
sideshowbarker
2024-07-17 03:05:16 +09:00
Author: https://github.com/nico Commit: https://github.com/SerenityOS/serenity/commit/a339f297d1 Pull-request: https://github.com/SerenityOS/serenity/pull/22922 Reviewed-by: https://github.com/LucasChollet ✅
1 changed files with 42 additions and 32 deletions
|
@ -15,6 +15,44 @@
|
|||
#include <LibGfx/ImageFormats/PortableFormatWriter.h>
|
||||
#include <LibGfx/ImageFormats/QOIWriter.h>
|
||||
|
||||
static ErrorOr<void> do_move_alpha_to_rgb(RefPtr<Gfx::Bitmap> frame)
|
||||
{
|
||||
switch (frame->format()) {
|
||||
case Gfx::BitmapFormat::Invalid:
|
||||
return Error::from_string_view("Can't --move-alpha-to-rgb with invalid bitmaps"sv);
|
||||
case Gfx::BitmapFormat::RGBA8888:
|
||||
// No image decoder currently produces bitmaps with this format.
|
||||
// If that ever changes, preferrably fix the image decoder to use BGRA8888 instead :)
|
||||
// If there's a good reason for not doing that, implement support for this, I suppose.
|
||||
return Error::from_string_view("--move-alpha-to-rgb not implemented for RGBA8888"sv);
|
||||
case Gfx::BitmapFormat::BGRA8888:
|
||||
case Gfx::BitmapFormat::BGRx8888:
|
||||
// FIXME: If BitmapFormat::Gray8 existed (and image encoders made use of it to write grayscale images), we could use it here.
|
||||
for (auto& pixel : *frame) {
|
||||
u8 alpha = pixel >> 24;
|
||||
pixel = 0xff000000 | (alpha << 16) | (alpha << 8) | alpha;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static ErrorOr<void> do_strip_alpha(RefPtr<Gfx::Bitmap> frame)
|
||||
{
|
||||
switch (frame->format()) {
|
||||
case Gfx::BitmapFormat::Invalid:
|
||||
return Error::from_string_view("Can't --strip-alpha with invalid bitmaps"sv);
|
||||
case Gfx::BitmapFormat::RGBA8888:
|
||||
// No image decoder currently produces bitmaps with this format.
|
||||
// If that ever changes, preferrably fix the image decoder to use BGRA8888 instead :)
|
||||
// If there's a good reason for not doing that, implement support for this, I suppose.
|
||||
return Error::from_string_view("--strip-alpha not implemented for RGBA8888"sv);
|
||||
case Gfx::BitmapFormat::BGRA8888:
|
||||
case Gfx::BitmapFormat::BGRx8888:
|
||||
frame->strip_alpha_channel();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
Core::ArgsParser args_parser;
|
||||
|
@ -64,39 +102,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
auto frame = TRY(decoder->frame(frame_index)).image;
|
||||
|
||||
if (move_alpha_to_rgb) {
|
||||
switch (frame->format()) {
|
||||
case Gfx::BitmapFormat::Invalid:
|
||||
return Error::from_string_view("Can't --move-alpha-to-rgb with invalid bitmaps"sv);
|
||||
case Gfx::BitmapFormat::RGBA8888:
|
||||
// No image decoder currently produces bitmaps with this format.
|
||||
// If that ever changes, preferrably fix the image decoder to use BGRA8888 instead :)
|
||||
// If there's a good reason for not doing that, implement support for this, I suppose.
|
||||
return Error::from_string_view("--move-alpha-to-rgb not implemented for RGBA8888"sv);
|
||||
case Gfx::BitmapFormat::BGRA8888:
|
||||
case Gfx::BitmapFormat::BGRx8888:
|
||||
// FIXME: If BitmapFormat::Gray8 existed (and image encoders made use of it to write grayscale images), we could use it here.
|
||||
for (auto& pixel : *frame) {
|
||||
u8 alpha = pixel >> 24;
|
||||
pixel = 0xff000000 | (alpha << 16) | (alpha << 8) | alpha;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (move_alpha_to_rgb)
|
||||
TRY(do_move_alpha_to_rgb(frame));
|
||||
|
||||
if (strip_alpha) {
|
||||
switch (frame->format()) {
|
||||
case Gfx::BitmapFormat::Invalid:
|
||||
return Error::from_string_view("Can't --strip-alpha with invalid bitmaps"sv);
|
||||
case Gfx::BitmapFormat::RGBA8888:
|
||||
// No image decoder currently produces bitmaps with this format.
|
||||
// If that ever changes, preferrably fix the image decoder to use BGRA8888 instead :)
|
||||
// If there's a good reason for not doing that, implement support for this, I suppose.
|
||||
return Error::from_string_view("--strip-alpha not implemented for RGBA8888"sv);
|
||||
case Gfx::BitmapFormat::BGRA8888:
|
||||
case Gfx::BitmapFormat::BGRx8888:
|
||||
frame->strip_alpha_channel();
|
||||
}
|
||||
}
|
||||
if (strip_alpha)
|
||||
TRY(do_strip_alpha(frame));
|
||||
|
||||
Optional<ReadonlyBytes> icc_data = TRY(decoder->icc_data());
|
||||
|
||||
|
|
Loading…
Reference in a new issue