diff --git a/Userland/Utilities/image.cpp b/Userland/Utilities/image.cpp index 311f56d0cc9..4c8c7415e49 100644 --- a/Userland/Utilities/image.cpp +++ b/Userland/Utilities/image.cpp @@ -15,6 +15,44 @@ #include #include +static ErrorOr do_move_alpha_to_rgb(RefPtr 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 do_strip_alpha(RefPtr 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 serenity_main(Main::Arguments arguments) { Core::ArgsParser args_parser; @@ -64,39 +102,11 @@ ErrorOr 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 icc_data = TRY(decoder->icc_data());