1 | #ifndef FASTUIDRAW_DEMO_IMAGELOADER_HPP |
2 | #define FASTUIDRAW_DEMO_IMAGELOADER_HPP |
3 | |
4 | #include <SDL_image.h> |
5 | #include <vector> |
6 | #include <stdint.h> |
7 | #include <fastuidraw/util/vecN.hpp> |
8 | #include <fastuidraw/image.hpp> |
9 | #include <fastuidraw/gl_backend/ngl_header.hpp> |
10 | #include <fastuidraw/gl_backend/texture_image_gl.hpp> |
11 | |
12 | #include "cast_c_array.hpp" |
13 | |
14 | fastuidraw::ivec2 |
15 | load_image_to_array(const SDL_Surface *img, |
16 | std::vector<fastuidraw::u8vec4> &out_bytes, |
17 | bool flip = false); |
18 | |
19 | fastuidraw::ivec2 |
20 | load_image_to_array(const std::string &pfilename, |
21 | std::vector<fastuidraw::u8vec4> &out_bytes, |
22 | bool flip = false); |
23 | |
24 | void |
25 | create_mipmap_level(fastuidraw::ivec2 sz, |
26 | fastuidraw::c_array<const fastuidraw::u8vec4> in_data, |
27 | std::vector<fastuidraw::u8vec4> &out_data); |
28 | |
29 | class ImageLoaderData |
30 | { |
31 | public: |
32 | ImageLoaderData(const std::string &pfilename, bool flip = false); |
33 | |
34 | fastuidraw::uvec2 |
35 | dimensions(void) const |
36 | { |
37 | return m_dimensions; |
38 | } |
39 | |
40 | int |
41 | width(void) const |
42 | { |
43 | return m_dimensions.x(); |
44 | } |
45 | |
46 | int |
47 | height(void) const |
48 | { |
49 | return m_dimensions.y(); |
50 | } |
51 | |
52 | bool |
53 | non_empty(void) |
54 | { |
55 | return m_dimensions.x() > 0u |
56 | && m_dimensions.y() > 0u; |
57 | } |
58 | |
59 | fastuidraw::c_array<const fastuidraw::c_array<const fastuidraw::u8vec4> > |
60 | data(void) const |
61 | { |
62 | return cast_c_array(m_data_as_arrays); |
63 | } |
64 | |
65 | private: |
66 | fastuidraw::uvec2 m_dimensions; |
67 | std::vector<std::vector<fastuidraw::u8vec4> > m_mipmap_levels; |
68 | std::vector<fastuidraw::c_array<const fastuidraw::u8vec4> > m_data_as_arrays; |
69 | }; |
70 | |
71 | class ImageLoader: |
72 | public ImageLoaderData, |
73 | public fastuidraw::ImageSourceCArray |
74 | { |
75 | public: |
76 | explicit |
77 | ImageLoader(const std::string &pfilename, bool flip = false): |
78 | ImageLoaderData(pfilename, flip), |
79 | fastuidraw::ImageSourceCArray(dimensions(), data(), fastuidraw::Image::rgba_format) |
80 | {} |
81 | }; |
82 | |
83 | #endif |
84 | |