Selaa lähdekoodia

LibGfx/ISOBMFF: Add JPEG2000URLBox

Nico Weber 1 vuosi sitten
vanhempi
commit
f080836127

+ 35 - 0
Userland/Libraries/LibGfx/ImageFormats/ISOBMFF/JPEG2000Boxes.cpp

@@ -161,6 +161,8 @@ ErrorOr<void> JPEG2000UUIDInfoBox::read_from_stream(BoxStream& stream)
         switch (type) {
         case BoxType::JPEG2000UUIDListBox:
             return TRY(JPEG2000UUIDListBox::create_from_stream(stream));
+        case BoxType::JPEG2000URLBox:
+            return TRY(JPEG2000URLBox::create_from_stream(stream));
         default:
             return OptionalNone {};
         }
@@ -198,4 +200,37 @@ void JPEG2000UUIDListBox::dump(String const& prepend) const
     }
 }
 
+ErrorOr<String> JPEG2000URLBox::url_as_string() const
+{
+    // Zero-terminated UTF-8 per spec.
+    if (url_bytes.is_empty() || url_bytes.bytes().last() != '\0')
+        return Error::from_string_literal("URL not zero-terminated");
+    return String::from_utf8(StringView { url_bytes.bytes().trim(url_bytes.size() - 1) });
+}
+
+ErrorOr<void> JPEG2000URLBox::read_from_stream(BoxStream& stream)
+{
+    version_number = TRY(stream.read_value<u8>());
+    flag = TRY(stream.read_value<u8>()) << 16;
+    flag |= TRY(stream.read_value<BigEndian<u16>>());
+
+    url_bytes = TRY(ByteBuffer::create_uninitialized(stream.remaining()));
+    TRY(stream.read_until_filled(url_bytes));
+
+    return {};
+}
+
+void JPEG2000URLBox::dump(String const& prepend) const
+{
+    Box::dump(prepend);
+    outln("{}- version_number = {}", prepend, version_number);
+    outln("{}- flag = {:#06x}", prepend, flag);
+
+    auto url_or_err = url_as_string();
+    if (url_or_err.is_error())
+        outln("{}- url = <invalid {}; {} bytes>", prepend, url_or_err.release_error(), url_bytes.size());
+    else
+        outln("{}- url = {}", prepend, url_or_err.release_value());
+}
+
 }

+ 11 - 0
Userland/Libraries/LibGfx/ImageFormats/ISOBMFF/JPEG2000Boxes.h

@@ -80,4 +80,15 @@ struct JPEG2000UUIDListBox final : public Box {
     Vector<Array<u8, 16>> uuids;
 };
 
+// I.7.3.2 Data Entry URL box
+struct JPEG2000URLBox final : public Box {
+    BOX_SUBTYPE(JPEG2000URLBox);
+
+    ErrorOr<String> url_as_string() const;
+
+    u8 version_number;
+    u32 flag;
+    ByteBuffer url_bytes;
+};
+
 }