image.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/ArgsParser.h>
  7. #include <LibCore/File.h>
  8. #include <LibCore/MappedFile.h>
  9. #include <LibGfx/ImageFormats/BMPWriter.h>
  10. #include <LibGfx/ImageFormats/ImageDecoder.h>
  11. #include <LibGfx/ImageFormats/PNGWriter.h>
  12. #include <LibGfx/ImageFormats/PortableFormatWriter.h>
  13. #include <LibGfx/ImageFormats/QOIWriter.h>
  14. ErrorOr<int> serenity_main(Main::Arguments arguments)
  15. {
  16. Core::ArgsParser args_parser;
  17. StringView in_path;
  18. args_parser.add_positional_argument(in_path, "Path to input image file", "FILE");
  19. StringView out_path;
  20. args_parser.add_option(out_path, "Path to output image file", "output", 'o', "FILE");
  21. bool ppm_ascii;
  22. args_parser.add_option(ppm_ascii, "Convert to a PPM in ASCII", "ppm-ascii", {});
  23. StringView assign_color_profile_path;
  24. args_parser.add_option(assign_color_profile_path, "Load color profile from file and assign it to output image", "assign-color-profile", {}, "FILE");
  25. bool strip_color_profile;
  26. args_parser.add_option(strip_color_profile, "Do not write color profile to output", "strip-color-profile", {});
  27. args_parser.parse(arguments);
  28. if (out_path.is_empty()) {
  29. warnln("-o is required");
  30. return 1;
  31. }
  32. auto file = TRY(Core::MappedFile::map(in_path));
  33. auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(file->bytes());
  34. if (!decoder) {
  35. warnln("Failed to decode input file '{}'", in_path);
  36. return 1;
  37. }
  38. // This uses ImageDecoder instead of Bitmap::load_from_file() to have more control
  39. // over selecting a frame, access color profile data, and so on in the future.
  40. auto frame = TRY(decoder->frame(0)).image;
  41. Optional<ReadonlyBytes> icc_data = TRY(decoder->icc_data());
  42. RefPtr<Core::MappedFile> icc_file;
  43. if (!assign_color_profile_path.is_empty()) {
  44. icc_file = TRY(Core::MappedFile::map(assign_color_profile_path));
  45. icc_data = icc_file->bytes();
  46. }
  47. if (strip_color_profile)
  48. icc_data.clear();
  49. ByteBuffer bytes;
  50. if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) {
  51. bytes = TRY(Gfx::BMPWriter::encode(*frame, { .icc_data = icc_data }));
  52. } else if (out_path.ends_with(".png"sv, CaseSensitivity::CaseInsensitive)) {
  53. bytes = TRY(Gfx::PNGWriter::encode(*frame, { .icc_data = icc_data }));
  54. } else if (out_path.ends_with(".ppm"sv, CaseSensitivity::CaseInsensitive)) {
  55. auto const format = ppm_ascii ? Gfx::PortableFormatWriter::Options::Format::ASCII : Gfx::PortableFormatWriter::Options::Format::Raw;
  56. bytes = TRY(Gfx::PortableFormatWriter::encode(*frame, { .format = format }));
  57. } else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) {
  58. bytes = TRY(Gfx::QOIWriter::encode(*frame));
  59. } else {
  60. warnln("can only write .bmp, .png, .ppm, and .qoi");
  61. return 1;
  62. }
  63. auto output_stream = TRY(Core::File::open(out_path, Core::File::OpenMode::Write));
  64. TRY(output_stream->write_until_depleted(bytes));
  65. return 0;
  66. }