Sfoglia il codice sorgente

LibGfx/ISOBMFF: Read JPEG2000HeaderBox

Nico Weber 1 anno fa
parent
commit
a073b2d047

+ 16 - 0
Userland/Libraries/LibGfx/ImageFormats/ISOBMFF/Boxes.cpp

@@ -5,6 +5,7 @@
  */
  */
 
 
 #include "Boxes.h"
 #include "Boxes.h"
+#include "Reader.h"
 
 
 namespace Gfx::ISOBMFF {
 namespace Gfx::ISOBMFF {
 
 
@@ -103,4 +104,19 @@ void FileTypeBox::dump(String const& prepend) const
     outln("{}{}", prepend, compatible_brands_string.string_view());
     outln("{}{}", prepend, compatible_brands_string.string_view());
 }
 }
 
 
+ErrorOr<void> SuperBox::read_from_stream(BoxStream& stream)
+{
+    auto reader = TRY(Gfx::ISOBMFF::Reader::create(MaybeOwned { stream }));
+    m_child_boxes = TRY(reader.read_entire_file());
+    return {};
+}
+
+void SuperBox::dump(String const& prepend) const
+{
+    Box::dump(prepend);
+    auto indented_prepend = add_indent(prepend);
+    for (auto const& child_box : m_child_boxes)
+        child_box->dump(indented_prepend);
+}
+
 }
 }

+ 21 - 0
Userland/Libraries/LibGfx/ImageFormats/ISOBMFF/Boxes.h

@@ -93,4 +93,25 @@ struct FileTypeBox final : public Box {
     Vector<BrandIdentifier> compatible_brands;
     Vector<BrandIdentifier> compatible_brands;
 };
 };
 
 
+// A box that contains other boxes.
+struct SuperBox : public Box {
+    static ErrorOr<NonnullOwnPtr<SuperBox>> create_from_stream(BoxType type, BoxStream& stream)
+    {
+        auto box = TRY(try_make<SuperBox>(type));
+        TRY(box->read_from_stream(stream));
+        return box;
+    }
+    SuperBox(BoxType type)
+        : m_box_type(type)
+    {
+    }
+    virtual ErrorOr<void> read_from_stream(BoxStream&) override;
+    virtual BoxType box_type() const override { return m_box_type; }
+    virtual void dump(String const& prepend = {}) const override;
+
+private:
+    BoxType m_box_type { BoxType::None };
+    BoxList m_child_boxes;
+};
+
 }
 }

+ 8 - 0
Userland/Libraries/LibGfx/ImageFormats/ISOBMFF/Reader.cpp

@@ -14,6 +14,11 @@ ErrorOr<Reader> Reader::create(MaybeOwned<SeekableStream> stream)
     return Reader(make<BoxStream>(move(stream), size));
     return Reader(make<BoxStream>(move(stream), size));
 }
 }
 
 
+ErrorOr<Reader> Reader::create(MaybeOwned<BoxStream> stream)
+{
+    return Reader(move(stream));
+}
+
 ErrorOr<BoxList> Reader::read_entire_file()
 ErrorOr<BoxList> Reader::read_entire_file()
 {
 {
     BoxList top_level_boxes;
     BoxList top_level_boxes;
@@ -26,6 +31,9 @@ ErrorOr<BoxList> Reader::read_entire_file()
         case BoxType::FileTypeBox:
         case BoxType::FileTypeBox:
             TRY(top_level_boxes.try_append(TRY(FileTypeBox::create_from_stream(box_stream))));
             TRY(top_level_boxes.try_append(TRY(FileTypeBox::create_from_stream(box_stream))));
             break;
             break;
+        case BoxType::JPEG2000HeaderBox:
+            TRY(top_level_boxes.try_append(TRY(SuperBox::create_from_stream(box_header.type, box_stream))));
+            break;
         default:
         default:
             TRY(top_level_boxes.try_append(TRY(UnknownBox::create_from_stream(box_header.type, box_stream))));
             TRY(top_level_boxes.try_append(TRY(UnknownBox::create_from_stream(box_header.type, box_stream))));
             break;
             break;

+ 1 - 0
Userland/Libraries/LibGfx/ImageFormats/ISOBMFF/Reader.h

@@ -16,6 +16,7 @@ namespace Gfx::ISOBMFF {
 class Reader {
 class Reader {
 public:
 public:
     static ErrorOr<Reader> create(MaybeOwned<SeekableStream> stream);
     static ErrorOr<Reader> create(MaybeOwned<SeekableStream> stream);
+    static ErrorOr<Reader> create(MaybeOwned<BoxStream> stream);
 
 
     ErrorOr<BoxList> read_entire_file();
     ErrorOr<BoxList> read_entire_file();