32 constexpr std::array<unsigned char, 8> expected { 137, 80, 78, 71, 13, 10, 26, 10 };
33 internal_files::check_signature<byteme::RawFileReader>(path, expected.data(), expected.size(),
"PNG");
40 constexpr std::array<unsigned char, 4> iisig { 0x49, 0x49, 0x2A, 0x00 };
41 constexpr std::array<unsigned char, 4> mmsig { 0x4D, 0x4D, 0x00, 0x2A };
61 const std::string type_name =
"image_file";
62 const auto& obj = internal_json::extract_typed_object_from_metadata(metadata.
other, type_name);
64 const std::string vname =
"version";
65 const auto& vstring = internal_json::extract_string_from_typed_object(obj, vname, type_name);
66 auto version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(),
true);
67 if (version.major != 1) {
68 throw std::runtime_error(
"unsupported version string '" + vstring +
"'");
71 const std::string format_name =
"format";
72 const std::string& format = internal_json::extract_string(obj, format_name);
73 const std::string prefix = (path /
"file").
string();
75 if (format ==
"PNG") {
76 const std::filesystem::path ipath = prefix +
".png";
77 internal::validate_png(ipath);
79 }
else if (format ==
"TIFF") {
80 const std::filesystem::path ipath = prefix +
".tif";
81 internal::validate_tiff(ipath);
83 }
else if (format ==
"JPEG") {
84 auto ipath = prefix +
".jpg";
86 constexpr std::array<unsigned char, 2> expected { 0xFF, 0xD8 };
87 internal_files::check_signature<byteme::RawFileReader>(ipath, expected.data(), expected.size(),
"JPEG");
89 }
else if (format ==
"GIF") {
90 auto ipath = prefix +
".gif";
92 constexpr std::array<unsigned char, 4> expected{ 0x47, 0x49, 0x46, 0x38 };
93 internal_files::check_signature<byteme::RawFileReader>(ipath, expected.data(), expected.size(),
"GIF");
95 }
else if (format ==
"WEBP") {
96 auto ipath = prefix +
".webp";
97 std::array<unsigned char, 12> observed;
98 internal_files::extract_signature(ipath, observed.data(), observed.size());
99 constexpr std::array<unsigned char, 4> first4 { 0x52, 0x49, 0x46, 0x46 };
100 constexpr std::array<unsigned char, 4> last4 { 0x57, 0x45, 0x42, 0x50 };
101 std::array<unsigned char, 4> observed_first, observed_last;
102 std::copy_n(observed.begin(), 4, observed_first.begin());
103 std::copy_n(observed.begin() + 8, 4, observed_last.begin());
104 if (observed_first != first4 || observed_last != last4) {
105 throw std::runtime_error(
"incorrect WEBP file signature for '" + ipath +
"'");
109 throw std::runtime_error(
"unsupported format '" + format +
"'");