mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 21:40:33 +00:00
image: Add a --move-alpha-to-rgb flag
I didn't put this as a method on Bitmap since it doesn't seem generally useful. Easy to move the impl over to Bitmap in the future if we want to use it elsewhere.
This commit is contained in:
parent
ca35b5d767
commit
0db8ac7465
Notes:
sideshowbarker
2024-07-17 09:49:33 +09:00
Author: https://github.com/nico Commit: https://github.com/SerenityOS/serenity/commit/0db8ac7465 Pull-request: https://github.com/SerenityOS/serenity/pull/19393
1 changed files with 28 additions and 0 deletions
|
@ -30,6 +30,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
int frame_index = 0;
|
||||
args_parser.add_option(frame_index, "Which frame of a multi-frame input image (0-based)", "frame-index", {}, "INDEX");
|
||||
|
||||
bool move_alpha_to_rgb = false;
|
||||
args_parser.add_option(move_alpha_to_rgb, "Copy alpha channel to rgb, clear alpha", "move-alpha-to-rgb", {});
|
||||
|
||||
bool ppm_ascii = false;
|
||||
args_parser.add_option(ppm_ascii, "Convert to a PPM in ASCII", "ppm-ascii", {});
|
||||
|
||||
|
@ -61,6 +64,31 @@ 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:
|
||||
case Gfx::BitmapFormat::Indexed1:
|
||||
case Gfx::BitmapFormat::Indexed2:
|
||||
case Gfx::BitmapFormat::Indexed4:
|
||||
case Gfx::BitmapFormat::Indexed8:
|
||||
warnln("Can't --strip-alpha with indexed or invalid bitmaps");
|
||||
return 1;
|
||||
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.
|
||||
warnln("Can't --strip-alpha not implemented for RGBA8888");
|
||||
return 1;
|
||||
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 (strip_alpha) {
|
||||
switch (frame->format()) {
|
||||
case Gfx::BitmapFormat::Invalid:
|
||||
|
|
Loading…
Reference in a new issue