mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
Utilities: Add new utility for converting images to raw bitmap binaries
I used this utility to check if the possible TGA images' cases for different origins (explictly the Y origin) are generating the same bitmap, as I felt that my eyes are not a good-enough measurement tool for this kind of task. This might be useful in the future for testing other implementations so I rather have this nice utility in our codebase.
This commit is contained in:
parent
b2626d3bc1
commit
01db302a33
Notes:
sideshowbarker
2024-07-17 11:34:34 +09:00
Author: https://github.com/supercomputer7 Commit: https://github.com/SerenityOS/serenity/commit/01db302a33 Pull-request: https://github.com/SerenityOS/serenity/pull/16574 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/gmta ✅
3 changed files with 63 additions and 0 deletions
23
Base/usr/share/man/man1/image2bin.md
Normal file
23
Base/usr/share/man/man1/image2bin.md
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
## Name
|
||||||
|
|
||||||
|
image2bin - convert an image to a binary bitmap
|
||||||
|
|
||||||
|
## Synopsis
|
||||||
|
|
||||||
|
```**sh
|
||||||
|
$ image2bin <path-to-image>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
`image2bin` uses LibGfx to decode a specified image to a raw bitmap, so it could be stored
|
||||||
|
in a raw binary format for further examination.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Convert a PNG image to raw bitmap
|
||||||
|
$ image2bin example.png > example.bin
|
||||||
|
# Convert a JPG image to raw bitmap
|
||||||
|
$ image2bin another_example.jpg > another_example.bin
|
||||||
|
```
|
|
@ -91,6 +91,7 @@ target_link_libraries(gunzip PRIVATE LibCompress)
|
||||||
target_link_libraries(gzip PRIVATE LibCompress)
|
target_link_libraries(gzip PRIVATE LibCompress)
|
||||||
target_link_libraries(headless-browser PRIVATE LibCrypto LibGemini LibGfx LibHTTP LibTLS LibWeb LibWebSocket LibIPC LibJS)
|
target_link_libraries(headless-browser PRIVATE LibCrypto LibGemini LibGfx LibHTTP LibTLS LibWeb LibWebSocket LibIPC LibJS)
|
||||||
target_link_libraries(icc PRIVATE LibGfx)
|
target_link_libraries(icc PRIVATE LibGfx)
|
||||||
|
target_link_libraries(image2bin PRIVATE LibGfx)
|
||||||
target_link_libraries(jail-attach PRIVATE LibCore LibMain)
|
target_link_libraries(jail-attach PRIVATE LibCore LibMain)
|
||||||
target_link_libraries(jail-create PRIVATE LibCore LibMain)
|
target_link_libraries(jail-create PRIVATE LibCore LibMain)
|
||||||
target_link_libraries(js PRIVATE LibCrypto LibJS LibLine LibLocale LibTextCodec)
|
target_link_libraries(js PRIVATE LibCrypto LibJS LibLine LibLocale LibTextCodec)
|
||||||
|
|
39
Userland/Utilities/image2bin.cpp
Normal file
39
Userland/Utilities/image2bin.cpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <AK/DeprecatedString.h>
|
||||||
|
#include <AK/Random.h>
|
||||||
|
#include <AK/StringBuilder.h>
|
||||||
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/DirIterator.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
#include <LibGUI/Application.h>
|
||||||
|
#include <LibGUI/Desktop.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
|
|
||||||
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
{
|
||||||
|
TRY(Core::System::pledge("stdio rpath unix"));
|
||||||
|
DeprecatedString path;
|
||||||
|
|
||||||
|
Core::ArgsParser args_parser;
|
||||||
|
args_parser.add_positional_argument(path, "Path to image", "path");
|
||||||
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
|
auto bitmap = TRY(Gfx::Bitmap::try_load_from_file(path));
|
||||||
|
|
||||||
|
TRY(Core::System::pledge("stdio"));
|
||||||
|
Vector<u8> data;
|
||||||
|
for (auto height = 0; height < bitmap->size().height(); height++) {
|
||||||
|
auto* scanline = bitmap->scanline_u8(height);
|
||||||
|
for (auto byte_index_in_row = 0u; byte_index_in_row < bitmap->pitch(); byte_index_in_row++) {
|
||||||
|
TRY(data.try_append(scanline[byte_index_in_row]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VERIFY(data.size() == bitmap->size_in_bytes());
|
||||||
|
TRY(Core::System::write(STDOUT_FILENO, data.span()));
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue