OGLplus  (0.59.0) a C++ wrapper for rendering APIs

image_file.hpp
Go to the documentation of this file.
1 
9 #ifndef OGLPLUS_UTILS_IMAGE_FILE_HPP
10 #define OGLPLUS_UTILS_IMAGE_FILE_HPP
11 
12 #include "image_file_hdr.hpp"
13 #include "image_spec.hpp"
14 #include <eagine/file_contents.hpp>
15 #include <eagine/string_span.hpp>
16 
17 namespace eagine::oglp {
18 //------------------------------------------------------------------------------
19 class texture_image_block {
20 private:
21  structured_memory_block<const image_data_header> _header;
22 
23 public:
24  texture_image_block(memory::const_block blk)
25  : _header(blk) {}
26 
27  texture_image_block(const image_data_header* ptr)
28  : _header(as_bytes(view_one(ptr))) {}
29 
30  auto is_valid() const noexcept -> bool {
31  return _header->magic.is_valid();
32  }
33 
34  auto dimensions() const noexcept -> image_dimensions {
35  return image_dimensions(
36  _header->width, _header->height, _header->depth, _header->channels);
37  }
38 
39  auto format() const noexcept -> image_pixel_format {
40  return image_pixel_format(
41  pixel_format(_header->format),
42  pixel_internal_format(_header->internal_format));
43  }
44 
45  auto format(pixel_internal_format ifmt) const noexcept
46  -> image_pixel_format {
47  return image_pixel_format(pixel_format(_header->format), ifmt);
48  }
49 
50  auto pixel_data() const noexcept -> image_pixel_data {
51  return image_pixel_data(
52  pixel_data_type(_header->data_type),
53  as_bytes(_header->pixels),
54  sizeof(GLubyte));
55  }
56 
57  auto spec() const noexcept -> image_spec {
58  return {dimensions(), format(), pixel_data()};
59  }
60 
61  auto spec(pixel_internal_format ifmt) const noexcept {
62  return image_spec(dimensions(), format(ifmt), pixel_data());
63  }
64 
65  inline operator image_spec() const noexcept {
66  return spec();
67  }
68 };
69 //------------------------------------------------------------------------------
70 class texture_image_file
71  : protected_member<file_contents>
72  , public texture_image_block {
73 public:
74  texture_image_file(file_contents&& fc)
75  : protected_member<file_contents>(std::move(fc))
76  , texture_image_block(get_the_member()) {}
77 
78  texture_image_file(string_view path)
79  : texture_image_file(file_contents(path)) {}
80 
81  texture_image_file(const std::string& path)
82  : texture_image_file(string_view(path)) {}
83 };
84 //------------------------------------------------------------------------------
85 } // namespace eagine::oglp
86 
87 #endif // OGLPLUS_UTILS_IMAGE_FILE_HPP
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
static constexpr auto as_bytes(basic_span< T, P, S > spn) noexcept -> basic_block< std::is_const_v< T >>
Converts a span into a basic_block.
Definition: block.hpp:39
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
static auto format(std::string &&fmt_str) noexcept -> format_string_and_list< 0 >
Function taking a format string, returning an object for variable specification.
Definition: str_format.hpp:118

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).