C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
image_header_extractor.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../../io/abstract/bitstream_type_hint.hpp"
26#include "../image_general_header.hpp"
27
28#include <concepts>
29#include <cstddef>
30#include <istream>
31#include <memory>
32#include <optional>
33#include <span>
34#include <type_traits>
35#include <utility>
36
37namespace essence::imaging::abstract {
42 public:
43 template <typename T>
44 requires(!std::same_as<std::decay_t<T>, image_header_extractor>)
45 explicit image_header_extractor(T&& value) : wrapper_{std::make_shared<wrapper<T>>(std::forward<T>(value))} {}
46
52 return wrapper_->hint();
53 }
54
60 [[nodiscard]] std::optional<image_general_header> get(std::istream& stream) const {
61 return wrapper_->get(stream);
62 }
63
69 [[nodiscard]] std::optional<image_general_header> get(std::span<const std::byte> buffer) const {
70 return wrapper_->get(buffer);
71 }
72
73 private:
74 struct base {
75 virtual ~base() = default;
76 virtual io::abstract::bitstream_type_hint hint() = 0;
77 virtual std::optional<image_general_header> get(std::istream& stream) = 0;
78 virtual std::optional<image_general_header> get(std::span<const std::byte> buffer) = 0;
79 };
80
81 template <typename T>
82 class wrapper final : public base {
83 public:
84 template <std::convertible_to<T> U>
85 explicit wrapper(U&& value) : value_{std::forward<U>(value)} {}
86
87 io::abstract::bitstream_type_hint hint() override {
88 return value_.hint();
89 }
90
91 std::optional<image_general_header> get(std::istream& stream) override {
92 return value_.get(stream);
93 }
94
95 std::optional<image_general_header> get(std::span<const std::byte> buffer) override {
96 return value_.get(buffer);
97 }
98
99 private:
100 T value_;
101 };
102
103 std::shared_ptr<base> wrapper_;
104 };
105} // namespace essence::imaging::abstract
Extracts a general image header from image file data.
Definition image_header_extractor.hpp:41
std::optional< image_general_header > get(std::span< const std::byte > buffer) const
Extracts the general image header from a memory buffer.
Definition image_header_extractor.hpp:69
io::abstract::bitstream_type_hint hint() const
Gets the underlying type hint.
Definition image_header_extractor.hpp:51
std::optional< image_general_header > get(std::istream &stream) const
Extracts the general image header from a standard input stream.
Definition image_header_extractor.hpp:60
Hints the type of bitstream.
Definition bitstream_type_hint.hpp:39