forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image2bin.cpp
39 lines (34 loc) · 1.17 KB
/
image2bin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Copyright (c) 2023, Liav A. <[email protected]>
*
* 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::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;
}